tizen 2.3.1 release
[kernel/api/system-resource.git] / include / proc_stat.h
1 /*
2  * resourced
3  *
4  * Library for getting process statistics
5  *
6  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #ifndef __LIB_PROC_STAT__
23 #define __LIB_PROC_STAT__
24
25 #ifndef __cplusplus
26 #include <stdbool.h>
27 #endif /* !__cplusplus */
28
29 #include <glib.h>
30 #include <resourced.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35 typedef struct proc_stat_process_info   /** process statistics */
36 {
37         pid_t pid;      /**< the process ID */
38         char name[NAME_MAX]; /**< the name of process */
39
40         bool valid; /** < whether this information is valid (can be used)*/
41         bool active; /**< whether this process is active */
42         bool fresh; /**< whether this process is newly created */
43         unsigned long utime_diff; /**< user mode time this process spent during this measurement interval */
44         unsigned long stime_diff;  /**< kernel mode time this process spent during this measurement interval */
45         unsigned long utime_prev;  /**< previous user mode time this process spent during the last measurement interval */
46         unsigned long stime_prev; /**< previous kernel mode time this process spent during the last measurement interval */
47
48         unsigned int rss; /**<resident set size of this process by Kb */
49
50 } proc_stat_process_info;
51
52
53 typedef struct proc_stat_system_time    /** The time information system spent, measured in units of USER_HZ **/
54 {
55         unsigned long long total_time;          /**< the total time system spent */
56         unsigned long long user_time;           /**< the time system spent in user mode */
57         unsigned long long nice_time;           /**< the time system spent in user mode with low priority(nice) */
58         unsigned long long system_time;         /**< the time system spent in system mode */
59         unsigned long long idle_time;           /**< the time system spent in idle task */
60         unsigned long long iowait_time;         /**< the time system spent waiting for IO */
61         unsigned long long irq_time;            /**< the time system spent servicing interrupts */
62         unsigned long long softirq_time;        /**< the time system spent servicing softirqs */
63 } proc_stat_system_time;
64
65
66 /**
67 * The following APIs are not thread safe !!!
68 *
69 */
70
71 /**
72  * @brief Initialize internal resources which are used for managing process statistics
73  *
74  * @return nothing
75  *
76  * This function initializes internal resources which are used for managing process statistics so should be called firstly
77  */
78 void proc_stat_init(void);
79
80 /**
81  * @brief Release internal resources which are used for managing process statistics
82  *
83  * @return nothing
84  *
85  * This function releases internal resources which are used for managing process statistics
86   */
87
88 void proc_stat_finalize(void);
89
90
91 /**
92  * @brief Get process statistics between two consecutive its calls
93  *
94  * @param valid_proc_infos GArray instance to be filled with valid proc_stat_process_info
95  * @param terminated_proc_infos GArray instance to be filled with proc_stat_process_info instances which were terminated between two consecutive its calls
96                   ,pass NULL if this information is not necessary
97  * @param total_valid_proc_time the sum of time spent by all valid proc_stat_process_info instance, pass NULL if if this information is not necessary
98  * @return  true on success.
99  *
100  * This function gets process statistics between two consecutive its calls
101  */
102
103 bool proc_stat_get_process_info(GArray *valid_proc_infos, GArray *terminated_proc_infos,
104                                            unsigned long *total_valid_proc_time);
105
106 /**
107  * @brief Get the difference of system time between two consecutive its calls
108  *
109  * @param st_diff the difference of system time
110  * @return  true on success, false when it is called first because it can't get the time difference.
111  *
112  * This function gets the difference of system time between two consecutive its calls
113  */
114 bool proc_stat_get_system_time_diff(proc_stat_system_time *st_diff);
115
116
117 /**
118  * @brief get total memory size by MB unit from /proc/meminfo
119  *
120  * @param total_mem to get the value of MemTotal
121  * @return true on success, false when it doesn't get values from /proc/meminfo
122  *
123  * This function gets total memory size by MB unit from /proc/meminfo
124  * total is from "MemTotal"
125  */
126
127 bool proc_stat_get_total_mem_size(unsigned int *total_mem);
128
129 /**
130  * @brief get free memory size by MB unit from /proc/meminfo
131  *
132  * @param free_mem to get free size of memory
133  * @return true on success, false when it doesn't get values from /proc/meminfo
134  *
135  * This function gets free memory size by MB unit from /proc/meminfo
136  * free_mem is calculated by "MemFree" + "Buffers" + "Cached" + "SwapCache" - "Shmem"
137  */
138
139 bool proc_stat_get_free_mem_size(unsigned int *free_mem);
140
141 /**
142  * @brief get CPU time by pid
143  *
144  * @param pid which process to get CPU time
145  * @param utime user mode time this process spent
146  * @param stime kernel mode time this process spent
147  * @return true on success, false when it doesn't get values from /proc/<pid>/stat
148  *
149  * This function gets CPU usage of a process by clock ticks unit from /proc/<pid>/stat
150  */
151
152 bool proc_stat_get_cpu_time_by_pid(pid_t pid, unsigned long *utime, unsigned long *stime);
153
154 /**
155  * @brief get memory usage by pid
156  *
157  * @param pid which process to get memory usage
158  * @param rss a process's memory usage
159  * @return true on success, false when it doesn't get values from /proc/<pid>/statm
160  *
161  * This function gets memory usage of a process by KB unit from rss of /proc/<pid>/statm
162  */
163
164 bool proc_stat_get_mem_usage_by_pid(pid_t pid, unsigned int *rss);
165
166
167
168 /**
169  * @brief Get process name
170  *
171  * @param pid which process to get name
172  * @name  name a process's name
173  *        the size of name should be equal or larger than NAME_MAX
174  * @return  true on success, false on failure.
175  *
176  * This function gets process name
177  *
178  */
179 bool proc_stat_get_name_by_pid(pid_t pid, char *name);
180
181
182
183 /**
184  * @brief Get pids under /proc file system
185  *
186  * @param pids which is filled with pids under /proc file system
187  *        The memory to accommodate pids will be allocated in this fuction
188  *        so the caller has reponsibility to free this memory
189  * @param cnt which is the count of pids
190  * @return  true on success, false on failure.
191  *
192  * This function fills pids(param) with pids under /proc file system.
193  *
194  */
195 bool proc_stat_get_pids(pid_t **pids, int *cnt);
196
197
198 /**
199  * @brief return whether currently GPU is on or off
200  *
201  * @return  true on GPU being on, false on GPU being off
202  *
203  * This function returns whether currently GPU is on or off
204  *
205  */
206 bool proc_stat_is_gpu_on(void);
207
208
209 /**
210  * @brief return GPU clock by MHz unit
211  *
212  * @return return GPU clock on success , -1 on false
213  *
214  * This function returns GPU clock
215  *
216  */
217
218 unsigned int proc_stat_get_gpu_clock(void);
219
220
221 enum proc_cgroup_cmd_type { /** cgroup command type **/
222         PROC_CGROUP_SET_FOREGRD,
223         PROC_CGROUP_SET_ACTIVE,
224         PROC_CGROUP_SET_BACKGRD,
225         PROC_CGROUP_SET_INACTIVE,
226         PROC_CGROUP_SET_LAUNCH_REQUEST,
227         PROC_CGROUP_SET_RESUME_REQUEST,
228         PROC_CGROUP_SET_TERMINATE_REQUEST,
229         PROC_CGROUP_SET_SERVICE_REQUEST,
230         PROC_CGROUP_SET_NOTI_REQUEST,
231         PROC_CGROUP_SET_PROC_EXCLUDE_REQUEST,
232         PROC_CGROUP_GET_MEMSWEEP,
233 };
234
235
236 /**
237  * @desc Set processes to foreground.
238  */
239 resourced_ret_c proc_cgroup_foregrd(void);
240
241 /**
242  * @desc Set processes to background.
243  */
244 resourced_ret_c proc_cgroup_backgrd(void);
245
246 /**
247  * @desc Set process to active
248  */
249 resourced_ret_c proc_cgroup_active(pid_t pid);
250
251 /**
252  * @desc Set process to inactive
253  */
254 resourced_ret_c proc_cgroup_inactive(pid_t pid);
255
256 /**
257  * @desc Change process status about cgroup with type
258  */
259 resourced_ret_c proc_group_change_status(int type, pid_t pid, char* app_id);
260
261 /**
262  * @desc Send process launch request
263  */
264 resourced_ret_c proc_cgroup_launch(int type, pid_t pid, char* app_id, char* pkg_id);
265
266
267 /**
268  * @brief sweep memory about background processes
269  *
270  * @return return num of swept processes
271  *
272  * This function returns GPU clock
273  *
274  */
275 resourced_ret_c proc_cgroup_sweep_memory(void);
276
277 #ifdef __cplusplus
278 }
279 #endif /* __cplusplus */
280
281 #endif /* __LIB_PROC_STAT__ */