Android获取CPU的使用率

发布于:2024-06-17 ⋅ 阅读:(34) ⋅ 点赞:(0)

1、参考博客

android系统之获取CPU的使用率
安卓性能测试之cpu占用率统计方法总结
CPU使用率指标内核源码分析
Android获取CPU,内存,磁盘使用率

2、使用读取/proc/stat方案

计算方法:读取
1、采样两个足够短的时间间隔的CPU快照,分别记作cpu1,cpu2,其中结构均为:
 (user、nice、system、idle、iowait、irq、softirq、stealstolen、guest)
 cpu 28679 2418 11790 74968 2574 1195 567 0 0 0
 cpu 28718 2425 11806 75494 2575 1197 569 0 0 0


2、计算总的CPU时间片totalCpuTime
 a)把第一次的所有cpu使用情况求和,得到cpu1;
 b)把第二次的所有cpu使用情况求和,得到cpu2;
 c)cpu2-cpu1得到这个时间间隔内的所有时间片 ;


3、计算空闲时间idle
 idle对应第四列的数据,用第二次的第四列 - 第一次的第四列即可
 idle=idle2-idle1


4、计算cpu使用率
 pcpu =100* ((cpu2-cpu1) - (idle2-idle1))/(cpu2-cpu1)

下面是去读两次文件: cat /proc/stat

cpu  28679 2418 11790 74968 2574 1195 567 0 0 0
cpu0 3171 230 1516 9720 344 194 115 0 0 0
cpu1 3287 390 1327 9701 341 152 56 0 0 0
cpu2 3664 231 1642 9356 258 162 72 0 0 0
cpu3 3741 299 1560 9124 331 171 72 0 0 0
cpu4 3871 368 1861 8664 335 129 80 0 0 0
cpu5 3659 333 1379 9314 354 129 55 0 0 0
cpu6 3667 265 1267 9484 321 125 56 0 0 0
cpu7 3614 297 1235 9601 287 129 58 0 0 0
intr 2250088 0 906620 49392 0 0 307 32453 0 0 0 0 0 1053917 0 0 0 0 0 3485 1529 1 2371 795 69 0 42596 24619 148 0 0 0 0 2512 0 0 0 0 36120 0 8867 0 0 0 0 68207 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 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 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 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 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 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 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 9985 0 0 6 3 6 16 896 2153 3015 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
ctxt 3899441
btime 1716015046
processes 5873
procs_running 1
procs_blocked 0
softirq 442893 6 43647 33 6478 27946 0 28988 103549 0 232246
cpu  28718 2425 11806 75494 2575 1197 569 0 0 0
cpu0 3173 231 1519 9788 344 194 116 0 0 0
cpu1 3289 390 1329 9772 341 153 56 0 0 0
cpu2 3666 231 1642 9427 258 162 72 0 0 0
cpu3 3745 300 1562 9192 331 171 72 0 0 0
cpu4 3881 369 1863 8724 336 129 80 0 0 0
cpu5 3669 337 1382 9371 355 129 55 0 0 0
cpu6 3671 265 1270 9550 321 126 56 0 0 0
cpu7 3620 298 1236 9665 287 130 58 0 0 0
intr 2254806 0 908585 49516 0 0 312 32464 0 0 0 0 0 1055950 0 0 0 0 0 3485 1540 1 2371 795 69 0 42832 24695 150 0 0 0 0 2512 0 0 0 0 36148 0 8911 0 0 0 0 68307 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 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 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 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 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 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 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 10009 0 0 6 3 6 16 920 2153 3050 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
ctxt 3905927
btime 1716015046
processes 5884
procs_running 2
procs_blocked 0
softirq 444477 6 43831 33 6487 27956 0 29124 103953 0 233087

2.1 读取/proc/stat权限

非预制应用seLabel标签untrusted_app,这里就有可能出现neverallow

allow system_app proc_stat:file { read getattr open }

日志:avc: denied { getattr } for path=“/proc/stat” dev=“proc” ino=4026532043 scontext=u:r:system_app:s0 tcontext=u:object_r:proc_stat:s0 tclass=file permissive=1

2.2 CPU使用率获取代码

这里读取一行,就是总的CPU使用率计算;这里足够短时间值得关注Thread.sleep(100);

public static float getCpuUsage() {
    try {
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        String load = reader.readLine();
        String[] toks = load.split(" ");
        long idle1 = Long.parseLong(toks[5]);
        long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]) + idle1
                + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            e.printStackTrace();
        }
        reader.seek(0);
        load = reader.readLine();
        reader.close();
        toks = load.split(" ");
        long idle2 = Long.parseLong(toks[5]);
        long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]) + idle2
                + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
        return (float) (100 * ((cpu2-cpu1) - (idle2-idle1)) / (cpu2-cpu1));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return 0;
}

网站公告

今日签到

点亮在社区的每一天
去签到