实现统计 android手机 CPU使用率

 1 # -*- coding:utf-8 -*-
 2 '''
 3 Created on Sep 10, 2018
 4 
 5 @author: SaShuangYiBing
 6 '''
 7 import subprocess
 8 import time
 9 
10 class CPU_usage(object):
11     """
12     CPU统计方法是参照:https://blog.csdn.net/xiaodanpeng/article/details/53503076
13     
14     """
15     def __init__(self,t0):
16         self.time = t0
17     
18     def read_cpu(self):
19         cpu_info0 = []
20         cpu_info1 = subprocess.check_output('adb shell cat /proc/stat').decode().split()[1:11]
21         for i in cpu_info1:
22             cpu_info0.append(int(i))
23         return cpu_info0
24     
25     def get_idle(self):
26         cpu_idle = self.read_cpu()[3]
27         return cpu_idle
28     
29     def cal_cpu(self):
30         t1_total = sum(self.read_cpu())
31         t1_idle = self.get_idle()
32         time.sleep(self.time)
33         t2_total = sum(self.read_cpu())
34         t2_idle = self.get_idle()
35         cpu_usage = (1 - (t2_idle - t1_idle)/(t2_total - t1_total))*100
36         if cpu_usage < 0:
37             return cpu_usage == 0
38         else:
39             return cpu_usage
40 
41 if __name__ == "__main__":
42        cal_cpu = CPU_usage(1)
43        while True:
44            print (time.strftime('%Y-%m-%d %H:%M:%S') + " The CPU usage is %d" %cal_cpu.cal_cpu() + "%")
45            

 

转载于:https://www.cnblogs.com/aziji/p/9628327.html

Android中,可以使用/proc/stat文件来获取CPU使用率信息。/proc/stat文件包含有关CPU和系统活动的许多统计信息,包括CPU时间片的使用情况。以下是获取CPU使用率的方法: 1. 读取/proc/stat文件。 2. 提取CPU时间片的总时间和空闲时间。 3. 通过计算CPU的总时间和空闲时间之间的差值,可以获得CPU使用率。 4. 可以使用定时器和循环来定期读取/proc/stat文件并更新CPU使用率。 示例代码如下: ``` public class CpuUsageMonitor { private long mLastCpuTime = 0; private long mLastIdleTime = 0; private long mLastUpdateTime = System.currentTimeMillis(); public float getCpuUsage() { long cpuTime = 0; long idleTime = 0; try { BufferedReader br = new BufferedReader(new FileReader("/proc/stat")); String line = br.readLine(); if (line.startsWith("cpu")) { String[] values = line.split("\\s+"); cpuTime = Long.parseLong(values[1]) + Long.parseLong(values[2]) + Long.parseLong(values[3]) + Long.parseLong(values[4]) + Long.parseLong(values[6]) + Long.parseLong(values[5]) + Long.parseLong(values[7]); idleTime = Long.parseLong(values[4]); } br.close(); } catch (IOException e) { e.printStackTrace(); } long currentTime = System.currentTimeMillis(); long elapsedTime = currentTime - mLastUpdateTime; mLastUpdateTime = currentTime; float cpuUsage = 0.0f; if (mLastCpuTime != 0) { float totalDiff = (float) (cpuTime - mLastCpuTime); float idleDiff = (float) (idleTime - mLastIdleTime); cpuUsage = (totalDiff - idleDiff) / totalDiff; } mLastCpuTime = cpuTime; mLastIdleTime = idleTime; return cpuUsage; } } ``` 注意,由于CPU时间片在短时间内可能会发生变化,因此获取的CPU使用率可能不是100%准确的。此外,需要注意在计算CPU使用率时的单位和精度,以确保得到正确的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值