在Android的一个过程的计算CPU使用情况 [英] Calculating CPU Usage of a Process in Android

查看:495
本文介绍了在Android的一个过程的计算CPU使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算的Andr​​oid进程的CPU使用情况如下,但是我不知道它的权利,由于输出产生的。

从jiffie转换成秒:jiffie /赫兹

第1步:获得使用的/ proc /运行时间文件的第一个参数的正常运行时间。

第二步:得到每秒时钟滴答数 / SYS /设备/系统/ CPU / CPU0 / CPU频率/ scaling_cur_freq

第三步:获得通过的过程中花对于totalTime(UTIME(14)+ STIME(15))从<$ C $参数C>的/ proc / [PID] / STAT

第四步:获得从的/ proc / [PID] / STAT 的过程中开始时间(22) 该值pssed时钟EX $ P $ 2.6的Linux后,蜱(通过的sysconf(_SC_CLK_TCK鸿沟))。

第五步:获取过程的总时间,因为它开始(运行时间 - (开始时间/频率)(因为运行时间是秒,开始时间是在时钟滴答)。

第6步:获得CPU使用率((totaltime /赫兹)/ elapsedTime)* 100

的计算后的输出是一样的东西5.702244483458246E-6,其大致等于〜0.000005702244483

修改

输出

步骤1:226.06 1211.19

步骤2:1.00

第三步:9347(example.com)S 3573 3573 0 0 -1 1077952832 8971 0 1 0 38 32 0 0 20 0 25 0 13137 983830528 14330 4294967295 1 1 0 0 0 0 0 4612 38136 4294967295 0 0 17 5 0 0 0 0 0 0 0 0 0 0 0 0 0

参考:<一href="http://stackoverflow.com/questions/16726779/total-cpu-usage-of-an-application-from-proc-pid-stat">total应用程序的CPU使用率从/ proc / PID / STAT

解决方案

错误的解释

我怀疑你使用中的值 / SYS /设备/系统/ CPU / CPU0 / CPU频率/ scaling_cur_freq 作为赫兹值。这是不正确,因为该文件给你的CPU硬件时钟频率,但必须使用Linux内核的时钟频率为您的赫兹值。

CPU硬件时钟和Linux内核的时钟是不同的。 Linux内核 - 其中机器人运行 - 有其自己的计时器(时钟),它更新在一定的频率;频率在此计时器的更新是内核赫兹( HZ )的值。

有关历史原因,时钟滴答值列于Linux的proc和SYS文件从通过Linux内核 USER_HZ 恒内核 HZ 频率共用频率缩放。正是这种 USER_HZ 常数,我们必须在我们的计算中赫兹值中使用。

数据采集

  • 运行时间 226.06
  • UTIME 38 时钟滴答
  • STIME 32 时钟滴答
  • 开始时间 13137 时钟滴答
  • 赫兹 100 (Linux内核 USER_HZ 恒)

计算

TOTAL_TIME = UTIME + STIME = 38 + 32 = 70

秒=的正常运行时间 - (开始时间/赫兹)= 226.06 - (100分之13137)= 94.69

cpu_usage = 100 *((TOTAL_TIME /赫兹)/秒)= 100 *((70/100)/ 94.69)= 0.7392 ...

解决方案

您进程的CPU使用总量约为 0.739%。如果这看起来小,记住,你的程序共享CPU的所有系统上的其他进程:大多数正常过程是空闲的大多数他们的生活,所以任何一个过程中通常会平均低CPU的总用量。

I am trying to calculate the CPU usage of a process in Android as follows, however i am not sure if its right due to the output produced.

To convert from jiffie to seconds: jiffie / hertz

1st step: get the uptime using the 1st parameter of /proc/uptime file.

2nd step: get the number of clock ticks per second from /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq.

3rd step: get the totaltime spent by the process (utime(14) +stime(15)) parameters from /proc/[pid]/stat

4th step: get the starttime(22) of the process from /proc/[pid]/stat the value is expressed in clock ticks (divide by sysconf(_SC_CLK_TCK)) after Linux 2.6.

5th step: get the total elapsed time of the process since it started (uptime - (starttime / hertz) (since uptime is in seconds and starttime is in clock ticks).

6th step: get the CPU usage percentage ((totaltime / hertz) / elapsedTime) * 100.

The output after the calculation is something like 5.702244483458246E-6 which is approximately equal to ~0.000005702244483

EDIT

Output

Step 1: 226.06 1211.19

Step 2: 1000000

Step 3: 9347 (example.com) S 3573 3573 0 0 -1 1077952832 8971 0 1 0 38 32 0 0 20 0 25 0 13137 983830528 14330 4294967295 1 1 0 0 0 0 4612 0 38136 4294967295 0 0 17 5 0 0 0 0 0 0 0 0 0 0 0 0 0

Reference: total cpu usage of an application from /proc/pid/stat

解决方案

Explanation of error

I suspect that you used the value in /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq as your hertz value. This is incorrect since that file gives you the CPU hardware clock frequency, but you must use the Linux kernel clock frequency as your hertz value.

The CPU hardware clock and Linux kernel clock are different. The Linux kernel -- which Android runs -- has its own timer (clock) which it updates at a certain frequency; the frequency at which this timer updates is the kernel hertz (HZ) value.

For historical reasons, the clock tick values listed in Linux proc and sys files are scaled from the kernel HZ frequency to a common frequency via the Linux kernel USER_HZ constant. It is this USER_HZ constant which we must use as the hertz value in our calculations.

Acquisition of data

  • uptime: 226.06 seconds
  • utime: 38 clock ticks
  • stime: 32 clock ticks
  • starttime: 13137 clock ticks
  • Hertz: 100 (Linux kernel USER_HZ constant)

Computation

total_time = utime + stime = 38 + 32 = 70

seconds = uptime - (starttime / Hertz) = 226.06 - (13137 / 100) = 94.69

cpu_usage = 100 * ((total_time / Hertz) / seconds) = 100 * ((70 / 100) / 94.69) = 0.7392...

Solution

The total CPU usage of your process is about 0.739%. If this seems small, remember that your process shares the CPU with all the other processes on the system: the majority of normal processes are idle for most of their life, so any one process will typically average a low total CPU usage.

这篇关于在Android的一个过程的计算CPU使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆