

Int lM圜PUUsage = getAppCPUUsage( lMyProcessID ) Integer lMyProcessID = android.os.Process.myPid() I did this in Android, and it makes a kernel top call and gets the CPU usage for your apps PID using what top returns. Here is another way that I got my App's CPU usage.

Resident and data_and_stack parameters used for getting memory usage and obtained from /proc//statm file. State, ppid, priority, nice, num_threads parameters obtained also from /proc//stat file. Utime, stime, cutime, cstime, starttime used for getting cpu usage and obtained from /proc//stat file. There is two versions simple monochrome(fast) and colored version(little bit slow, but useful especially for monitoring the statе of processes). It is a linux/unix system monitor and process manager through procfs, like " top" or " ps".

Here is my simple solution written in BASH. How to get total cpu usage in Linux (c++)Ĭalculating CPU usage of a process in Linux Top and ps not showing the same cpu result Next we get the total elapsed time in seconds since the process started: seconds = uptime - (starttime / Hertz)įinally we calculate the CPU usage percentage: cpu_usage = 100 * ((total_time / Hertz) / seconds) If we do, then we add those values to total_time: total_time = total_time + cutime + cstime We also have to decide whether we want to include the time from children processes. The sysconf(_SC_CLK_TCK) C function call may also be used to return the hertz value.įirst we determine the total time spent for the process: total_time = utime + stime.In most cases, getconf CLK_TCK can be used to return the number of clock ticks.Hertz (number of clock ticks per second) of your system.#22 starttime - Time when the process started, measured in clock ticks.#17 cstime - Waited-for children's CPU time spent in kernel code (in clock ticks).#16 cutime - Waited-for children's CPU time spent in user code (in clock ticks).

#15 stime - CPU time spent in kernel code, measured in clock ticks.#14 utime - CPU time spent in user code, measured in clock ticks.To calculate CPU usage for a specific process you'll need the following:
