【性能分析】安卓/LINUX平台如何分析CPU利用率?

本文详细介绍Linux环境下使用dumpsys和top命令进行系统性能监控的方法,包括CPU和内存使用情况的解析,以及如何通过调整CPU频率和在线状态实现高性能模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. dumpsys

dumpsys [options]
               meminfo 显示内存信息
               cpuinfo 显示CPU信息
               account 显示accounts信息
               activity 显示所有的activities的信息
               window 显示键盘,窗口和它们的关系
               wifi 显示wifi信息

dumpsys cpuinfo | grep camera_service

2. top
msm8953_64:/ # top
User 1%, System 12%, IOW 0%, IRQ 0%
User 2 + Nice 0 + Sys 23 + Idle 155 + IOW 0 + IRQ 0 + SIRQ 0 = 180
  PID USER     PR  NI CPU% S  #THR     VSS     RSS PCY Name
 5617 root     20   0   8% R     1   9268K   2160K  fg top
 2163 root     20   0   2% S     8 218960K  62632K  fg /system/bin/meig_camera_service
  555 system   -2  -8   0% S    13  92164K  15632K  fg /system/bin/surfaceflinger
   44 root     20   0   0% S     1      0K      0K  fg rcuop/4
    7 root     20   0   0% S     1      0K      0K  fg rcu_preempt
    8 root     20   0   0% S     1      0K      0K  fg rcu_sched
    9 root     20   0   0% S     1      0K      0K  fg rcu_bh
   10 root     20   0   0% S     1      0K      0K  fg rcuop/0
   11 root     20   0   0% S     1      0K      0K  fg rcuos/0
   12 root     20   0   0% S     1      0K      0K  fg rcuob/0

PID:      进程在系统中的ID
CPU% - 当前瞬时所以使用CPU占用率
#THR - 程序当前所用的线程数
UID - 运行当前进程的用户id
Name - 程序名称org.xmpp.app
VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)
一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS
附:
Linux top命令输出中PR值和NI值有什么不同
NI 是优先值,是用户层面的概念, PR是进程的实际优先级, 是给内核(kernel)看(用)的。
一般情况下,PR=NI+20, 如果一个进程的优先级PR是20, 那么它的NI(nice)值就是20-20=0

Usage: top [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ]
    -m num  Maximum number of processes to display.
    -n num  Updates to show before exiting.
    -d num  Seconds to wait between updates.
    -s col  Column to sort by (cpu,vss,rss,thr).
    -H      Show threads instead of processes.
    -h      Display this help screen.


*********************************************************************************************************************************
//linux系统top命令显示如下
   top - 11:35:41 up 6 days,  1:30, 21 users,  load average: 0.14, 0.15, 0.55
Tasks: 560 total,   1 running, 559 sleeping,   0 stopped,   0 zombie
%Cpu(s):  8.1 us,  0.9 sy,  0.0 ni, 88.0 id,  3.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 32891140 total,   764992 free,   339344 used, 31786804 buff/cache
KiB Swap: 33448956 total, 24199324 free,  9249632 used. 31624708 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 88444 kuangji+  20   0   42208   4220   3192 R   6.2  0.0   0:00.01 top
     1 root      20   0   38196   4284   2964 S   0.0  0.0   0:12.98 systemd
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.74 kthreadd
     3 root      20   0       0      0      0 S   0.0  0.0   0:22.95 ksoftirqd/0
     5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
     7 root      20   0       0      0      0 S   0.0  0.0   4:52.01 rcu_sched
     8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh
     9 root      rt   0       0      0      0 S   0.0  0.0   0:03.69 migration/0
*********************************************************************************************************************************
a. top----cpu占用率
top显示的CPU指标都是来源于/proc/stat文件信息:
msm8953_64:/ # cat /proc/stat
//第一行代表的总的CPU信息,后面的是一个CPU的详细信息。
cpu  75114 1592 77580 3395249 4383 18186 30482 0 0 0
cpu0 537 60 1321 453213 222 575 553 0 0 0
cpu1 439 51 1012 454120 199 458 447 0 0 0
cpu2 365 46 835 454484 181 429 40 0 0 0
cpu3 333 36 690 454700 165 441 432 0 0 0
cpu4 19723 385 19235 390244 1077 5501 6958 0 0 0
cpu5 23133 361 18331 392717 937 2904 6113 0 0 0
cpu6 11660 332 18513 393852 911 5583 11574 0 0 0
cpu7 18924 321 17643 401919 691 2295 4365 0 0 0
intr 4500521 0 0 0 284296 0 566496 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 6698814
btime 5
processes 5599
procs_running 2
procs_blocked 0
softirq 2919569 0 783814 453 0 34713 0 818187 717534 1569 563299

user   (1) Time spent in user mode.
nice   (2) Time spent in user mode with low priority (nice).
system (3) Time spent in system mode.
idle   (4) Time spent in the idle task.  This value should be USER_HZ times the second  entry  in  the /proc/uptime pseudo-file.
iowait (since Linux 2.5.41)(5) Time waiting for I/O to complete.
irq (since Linux 2.6.0-test4)(6) Time servicing interrupts.
softirq (since Linux 2.6.0-test4)(7) Time servicing softirqs.
steal (since Linux 2.6.11)(8)  Stolen time, which is the time spent in other operating systems when running in a virtual‐ized environment
guest (since Linux 2.6.24)(9) Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.
guest_nice (since Linux 2.6.33)(10)  Time  spent running a niced guest (virtual CPU for guest operating systems under the con‐trol of the Linux kernel).
也就是说从第二列开始往后分别是user,nice,system,idle,iowait,irq(硬中断),softirq(软中断),steal,guest,guest_nice的CPU时间,单位通常是10ms。那么top里面的比例又是怎么算出的呢?
由于CPU时间是一个累加值,所以我们要求一个时间段差值来反映当前的CPU情况,top默认是3s。例如现在取一个user值user1,和当前的一个总量的CPU时间total1
其中total等于上面各项相加,也就是total=user+nice+system+idle+iowait+irq+softirq+steal+guest+guest_nice。3秒后再去一个user值user2和一个总量total2。
那么这3秒钟的user平均cpu占比就等于((user2-user1)/ (total2-total1))/ 3 * 100%。另外每个具体的CPU计算方式同理。
*********************************************************************************************************************************
b. top----内存利用
top内存相关的指标直接读取/proc/meminfo文件的对应字段:
msm8953_64:/ # cat /proc/meminfo
//其中total对应于MemTotal,free 对应于MemFree,avail 对应于MemAailable。
MemTotal:        1844112 kB
MemFree:           27908 kB
MemAvailable:     888240 kB
Buffers:           30004 kB
Cached:           745468 kB
SwapCached:         5944 kB
Active:           684032 kB
Inactive:         486752 kB
Active(anon):     295428 kB
Inactive(anon):   104544 kB
Active(file):     388604 kB
Inactive(file):   382208 kB
Unevictable:         256 kB
Mlocked:             256 kB
SwapTotal:        524284 kB
SwapFree:         449568 kB
Dirty:                12 kB
Writeback:             0 kB
AnonPages:        395568 kB
Mapped:           281708 kB
Shmem:              4620 kB
Slab:             340032 kB
SReclaimable:     129864 kB
SUnreclaim:       210168 kB
KernelStack:       24624 kB
PageTables:        26448 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     1446340 kB
Committed_AS:   53255876 kB
VmallocTotal:   244318144 kB
VmallocUsed:      131152 kB
VmallocChunk:   244058596 kB

3. 开启高性能模式

echo 1 > /sys/devices/system/cpu/cpu0/online
echo 1 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu2/online
echo 1 > /sys/devices/system/cpu/cpu3/online
echo 1 > /sys/devices/system/cpu/cpu4/online
echo 1 > /sys/devices/system/cpu/cpu5/online
echo 1 > /sys/devices/system/cpu/cpu6/online
echo 1 > /sys/devices/system/cpu/cpu7/online
echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu5/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu6/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu7/cpufreq/scaling_governor
echo performance > /sys/class/kgsl/kgsl-3d0/devfreq/governor
echo performance > /sys/class/devfreq/soc:qcom,cpubw/governor
echo performance > /sys/class/devfreq/soc:qcom,mincpubw/governor
echo 1401000 > /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
echo 1401000 > /sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_cur_freq
echo 1401000 > /sys/devices/system/cpu/cpu2/cpufreq/cpuinfo_cur_freq
echo 1401000 > /sys/devices/system/cpu/cpu3/cpufreq/cpuinfo_cur_freq
echo 1401000 > /sys/devices/system/cpu/cpu4/cpufreq/cpuinfo_cur_freq
echo 1401000 > /sys/devices/system/cpu/cpu5/cpufreq/cpuinfo_cur_freq
echo 1401000 > /sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_cur_freq
echo 1401000 > /sys/devices/system/cpu/cpu7/cpufreq/cpuinfo_cur_freq

4. 查询最新配置

cat /sys/devices/system/cpu/cpu0/online
cat /sys/devices/system/cpu/cpu1/online
cat /sys/devices/system/cpu/cpu2/online
cat /sys/devices/system/cpu/cpu3/online
cat /sys/devices/system/cpu/cpu4/online
cat /sys/devices/system/cpu/cpu5/online
cat /sys/devices/system/cpu/cpu6/online
cat /sys/devices/system/cpu/cpu7/online
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
cat /sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_cur_freq
cat /sys/devices/system/cpu/cpu2/cpufreq/cpuinfo_cur_freq
cat /sys/devices/system/cpu/cpu3/cpufreq/cpuinfo_cur_freq
cat /sys/devices/system/cpu/cpu4/cpufreq/cpuinfo_cur_freq
cat /sys/devices/system/cpu/cpu5/cpufreq/cpuinfo_cur_freq
cat /sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_cur_freq
cat /sys/devices/system/cpu/cpu7/cpufreq/cpuinfo_cur_freq
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
cat /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
cat /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor
cat /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor
cat /sys/devices/system/cpu/cpu5/cpufreq/scaling_governor
cat /sys/devices/system/cpu/cpu6/cpufreq/scaling_governor
cat /sys/devices/system/cpu/cpu7/cpufreq/scaling_governor
cat /sys/class/devfreq/soc:qcom,cpubw/governor
cat /sys/class/devfreq/soc:qcom,mincpubw/governor

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值