{"id":50392603,"url":"https://github.com/Gracker/android_cpu_usage_calculation","last_synced_at":"2026-06-16T10:01:00.323Z","repository":{"id":79108542,"uuid":"255839496","full_name":"Gracker/android_cpu_usage_calculation","owner":"Gracker","description":null,"archived":false,"fork":false,"pushed_at":"2020-12-30T02:42:43.000Z","size":163,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-23T01:50:01.158Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Gracker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2020-04-15T07:32:26.000Z","updated_at":"2025-05-18T14:22:13.000Z","dependencies_parsed_at":"2023-07-10T13:44:18.687Z","dependency_job_id":null,"html_url":"https://github.com/Gracker/android_cpu_usage_calculation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Gracker/android_cpu_usage_calculation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gracker%2Fandroid_cpu_usage_calculation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gracker%2Fandroid_cpu_usage_calculation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gracker%2Fandroid_cpu_usage_calculation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gracker%2Fandroid_cpu_usage_calculation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gracker","download_url":"https://codeload.github.com/Gracker/android_cpu_usage_calculation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gracker%2Fandroid_cpu_usage_calculation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34400456,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-16T02:00:06.860Z","response_time":126,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-05-30T19:00:29.698Z","updated_at":"2026-06-16T10:01:00.316Z","avatar_url":"https://github.com/Gracker.png","language":"Python","funding_links":[],"categories":["🛠 开发工具"],"sub_categories":[],"readme":"# Android\u0026\u0026Linux  Cpu Usage Calculation\n\n# 整机 cpu 使用率计算\n\n数据来源 ： /proc/stat （具体信息可以参考这里 http://man7.org/linux/man-pages/man5/proc.5.html ）\n\n```shell\n$ adb shell cat /proc/stat\ncpu  24296 3660 18105 899033 640 3576 2202 0 0 0\ncpu0 3473 285 3485 105976 58 1911 1120 0 0 0\ncpu1 3180 292 3299 110424 75 523 384 0 0 0\ncpu2 4209 760 3152 109585 68 501 340 0 0 0\ncpu3 3860 790 3005 110329 54 426 276 0 0 0\ncpu4 2120 416 1442 115725 100 65 21 0 0 0\ncpu5 2015 454 1257 115964 81 74 38 0 0 0\ncpu6 3153 394 1358 114858 123 41 10 0 0 0\ncpu7 2284 266 1104 116170 78 33 8 0 0 0\nintr 2389074 0 0 0 0 492483 0 152708 42 1121 52561 0 0 0 1349 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6396 15 52113 0 0 0 0 0 0 0 2 0 120654 2800 38 17712 28640 0 716 0 0 0 0 24 0 1957 6038 0 0 0 0 0 0 80 61 76 71 62 61 136 89 13 0 0 0 0 0 0 0 0 16865 0 0 0 0 0 0 0 0 0 0 0 823 0 0 0 0 0 0 0 0 0 0 141 461 0 1 17278 0 0 0 0 0 2289 0 0 0 0 0 0 0 0 0 0 9216 237 0 0 3 0 3 0 0 0 0 0 0 0 0 0 0 2 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 31249 0 0 0 4 0 4554 81 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2233 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\nctxt 3555767\nbtime 1586953851\nprocesses 11046\nprocs_running 4\nprocs_blocked 0\nsoftirq 1279600 718 431045 9410 31637 11581 0 106547 338769 0 349893\n```\n\n## 数据分析\n\n其中第一行 ：cpu  24296 3660 18105 899033 640 3576 2202 0 0 0\n\n|参数  |解析（单位：jiffies）|  原文解释 |\n| ----------   | -----------: | --------|\n|user (24296) |    从系统启动开始累计到当前时刻，处于用户态的运行时间，不包含 nice值为负进程。|Time spent in user mode |\n|nice (3660)   |   从系统启动开始累计到当前时刻，nice值为负的进程所占用的CPU时间|Time spent in user mode with low priority(nice) |\n|system (18105)|  从系统启动开始累计到当前时刻，处于核心态的运行时间|Time spent in system mode |\n|idle (899033) |  从系统启动开始累计到当前时刻，除IO等待时间以外的其它等待时间 | Time spent in the idle task.  This value should be USER_HZ times the second entry in the /proc/uptime pseudo-file|\n|iowait (640)| 从系统启动开始累计到当前时刻，IO等待时间(since 2.5.41)|Time waiting for I/O to complete |\n|irq (3576)     | 从系统启动开始累计到当前时刻，硬中断时间(since 2.6.0-test4)| Time servicing interrupts|\n|softirq (2202)   |   从系统启动开始累计到当前时刻，软中断时间(since 2.6.0-test4)|Time servicing softirqs. |\n\n那么总的 cpu 时间 totalCpuTime = user + nice + system + idle + iowait + irq + softirq \n\n## 使用率计算\n\n取一段时间内的 两份数据  totalCpuTime 和 idle time\n\n1. totalCpuTimeA ，idleA\n2. totalCpuTimeB ，idleB\n\n计算这一段时间内的 cpu 使用率：\n\ncpu 使用率 = ((totalCpuTimeB - totalCpuTimeA) - (idleB - idleA) ) / (totalCpuTimeB - totalCpuTimeA) * 100\n\n# 单个进程 cpu 使用率计算\n\n数据来源 ： /proc/pid_of_process/stat\n\n以华为手机 system_server 进程为例\n\n```shell\n$ adb shell cat /proc/1286/stat\n1286 (system_server) S 889 889 0 0 -1 1077952832 404759 0 2760 0 9595 4059 0 0 18 -2 265 0 599 10783543296 86828 18446744073709551615 1 1 0 0 0 0 4612 0 1073794299 0 0 0 17 1 0 0 76 0 0 0 0 0 0 0 0 0 0\n```\n\n## 数据分析\n\n说明：以下只解释对我们计算Cpu使用率有用相关参数\n\n|参数|解释|原文|\n|------|------  | ----|\n|pid=1286|             进程号|The process ID.|\n|comm=system_server| 进程名信息|The filename of the executable, in parentheses. This is visible whether or not the executable is swapped out|\n|utime=9595 |          该任务在用户态运行的时间，单位为jiffies|Amount of time that this process has been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)).  This includes guest time, guest_time (time spent running a virtual CPU, see below), so that applications that are not aware of the guest time field do not lose that time from their calculations.|\n|stime=4059    |       该任务在核心态运行的时间，单位为jiffies|Amount of time that this process has been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)).|\n|cutime=0       |      用户态等待子进程的消耗，单位为jiffies|Amount of time that this process's waited-for chil‐ dren have been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)).  (See also times(2).)  This includes guest time, cguest_time (time spent running a virtual CPU, see  below)|\n|cstime=0   |          内核态等待子进程的消耗，单位为jiffies|Amount of time that this process's waited-for chil‐ dren have been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK))|\n\n那么总的进程 cpu 时间 totalProcessCpuTime = utime + stime + cutime + cstime\n\n\n## 使用率计算\n\n取一段时间内的两份数据\n\n1. totalCpuTimeA ，totalProcessCpuTimeA\n2. totalCpuTimeB ，totalProcessCpuTimeB\n\n\ncpu 使用率 = (totalProcessCpuTimeB - ，totalProcessCpuTimeA ) / (totalCpuTimeB - totalCpuTimeA) * 100 * CPU_CORE_COUNT\n\n其中 CPU_CORE_COUNT 是手机的核数\n\n# 示例\n\n\n以一段复杂的操作为例，下面可以计算出这一段时间内的整机 cpu 使用率和 SystemServer(或者其他你比较关系的进程) 的 cpu 使用率\n\n\n```shell\npython cpu_usage_calculation.py -p system_server -f 2 -t 20\n```\n\n-p : 指定包名\n-f : 指定抓取频率(单位为 s)\n-t : 指定抓取时长(单位为 s)\n\n\n通过使用率的对比就可以分析这一段时间内的负载，可以作为竞品对比或者优化验证\n\n```shell\nAll devices cpuinfo (整机 cpu 使用率)\n0s to 5s  cpu usage :98.0\n5s to 10s  cpu usage :80.0\n10s to 15s  cpu usage :57.0\n15s to 20s  cpu usage :21.0\n\nSystemServer cpuinfo (SystemServer  cpu 使用率)\n0s to 5s  cpu usage :48.0\n5s to 10s  cpu usage :35.0\n10s to 15s  cpu usage :24.0\n15s to 20s  cpu usage :2.0\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGracker%2Fandroid_cpu_usage_calculation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGracker%2Fandroid_cpu_usage_calculation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGracker%2Fandroid_cpu_usage_calculation/lists"}