4 * Library for getting process statistics
6 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #ifndef __LIB_PROC_STAT__
23 #define __LIB_PROC_STAT__
27 #endif /* !__cplusplus */
30 #include <resourced.h>
34 #endif /* __cplusplus */
35 typedef struct proc_stat_process_info /** process statistics */
37 pid_t pid; /**< the process ID */
38 char name[NAME_MAX]; /**< the name of process */
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 */
48 unsigned int rss; /**<resident set size of this process by Kb */
50 } proc_stat_process_info;
53 typedef struct proc_stat_system_time /** The time information system spent, measured in units of USER_HZ **/
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;
67 * The following APIs are not thread safe !!!
72 * @brief Initialize internal resources which are used for managing process statistics
76 * This function initializes internal resources which are used for managing process statistics so should be called firstly
78 void proc_stat_init(void);
81 * @brief Release internal resources which are used for managing process statistics
85 * This function releases internal resources which are used for managing process statistics
88 void proc_stat_finalize(void);
92 * @brief Get process statistics between two consecutive its calls
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.
100 * This function gets process statistics between two consecutive its calls
103 bool proc_stat_get_process_info(GArray *valid_proc_infos, GArray *terminated_proc_infos,
104 unsigned long *total_valid_proc_time);
107 * @brief Get the difference of system time between two consecutive its calls
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.
112 * This function gets the difference of system time between two consecutive its calls
114 bool proc_stat_get_system_time_diff(proc_stat_system_time *st_diff);
118 * @brief get total memory size by MB unit from /proc/meminfo
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
123 * This function gets total memory size by MB unit from /proc/meminfo
124 * total is from "MemTotal"
127 bool proc_stat_get_total_mem_size(unsigned int *total_mem);
130 * @brief get free memory size by MB unit from /proc/meminfo
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
135 * This function gets free memory size by MB unit from /proc/meminfo
136 * free_mem is calculated by "MemFree" + "Buffers" + "Cached" + "SwapCache" - "Shmem"
139 bool proc_stat_get_free_mem_size(unsigned int *free_mem);
142 * @brief get CPU time by pid
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
149 * This function gets CPU usage of a process by clock ticks unit from /proc/<pid>/stat
152 bool proc_stat_get_cpu_time_by_pid(pid_t pid, unsigned long *utime, unsigned long *stime);
155 * @brief get memory usage by pid
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
161 * This function gets memory usage of a process by KB unit from rss of /proc/<pid>/statm
164 bool proc_stat_get_mem_usage_by_pid(pid_t pid, unsigned int *rss);
169 * @brief Get process name
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.
176 * This function gets process name
179 bool proc_stat_get_name_by_pid(pid_t pid, char *name);
184 * @brief Get pids under /proc file system
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.
192 * This function fills pids(param) with pids under /proc file system.
195 bool proc_stat_get_pids(pid_t **pids, int *cnt);
199 * @brief return whether currently GPU is on or off
201 * @return true on GPU being on, false on GPU being off
203 * This function returns whether currently GPU is on or off
206 bool proc_stat_is_gpu_on(void);
210 * @brief return GPU clock by MHz unit
212 * @return return GPU clock on success , -1 on false
214 * This function returns GPU clock
218 unsigned int proc_stat_get_gpu_clock(void);
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,
237 * @desc Set processes to foreground.
239 resourced_ret_c proc_cgroup_foregrd(void);
242 * @desc Set processes to background.
244 resourced_ret_c proc_cgroup_backgrd(void);
247 * @desc Set process to active
249 resourced_ret_c proc_cgroup_active(pid_t pid);
252 * @desc Set process to inactive
254 resourced_ret_c proc_cgroup_inactive(pid_t pid);
257 * @desc Change process status about cgroup with type
259 resourced_ret_c proc_group_change_status(int type, pid_t pid, char* app_id);
262 * @desc Send process launch request
264 resourced_ret_c proc_cgroup_launch(int type, pid_t pid, char* app_id, char* pkg_id);
268 * @brief sweep memory about background processes
270 * @return return num of swept processes
272 * This function returns GPU clock
275 resourced_ret_c proc_cgroup_sweep_memory(void);
279 #endif /* __cplusplus */
281 #endif /* __LIB_PROC_STAT__ */