Loading

Android CPU使用率

CPU占用

cpu使用率原理

android系统内核是基于linux,在linux系统下cpu利用率分别为:用户态、系统态、空闲态

用户态:cpu处于用户态执行的时间

系统态:系统内核执行的时间

空闲态:空闲系统进程执行的时间

CPU利用率:CPU执行非系统空闲进程的时间/CPU总的执行时间

cpu测试方法

adb 命令

复制代码
C:\Users\Administrator>adb shell top --help
usage: top [-Hbq] [-k FIELD,] [-o FIELD,] [-s SORT] [-n NUMBER] [-m LINES] [-d SECONDS] [-p PID,] [-u USER,]

Show process activity in real time.

-H      Show threads
-k      Fallback sort FIELDS (default -S,-%CPU,-ETIME,-PID)
-o      Show FIELDS (def PID,USER,PR,NI,VIRT,RES,SHR,S,%CPU,%MEM,TIME+,CMDLINE)
-O      Add FIELDS (replacing PR,NI,VIRT,RES,SHR,S from default)
-s      Sort by field number (1-X, default 9)
-b      Batch mode (no tty)
-d      Delay SECONDS between each cycle (default 3)
-m      Maximum number of tasks to show
-n      Exit after NUMBER iterations
-p      Show these PIDs
-u      Show these USERs
-q      Quiet (no header lines)

Cursor LEFT/RIGHT to change sort, UP/DOWN move list, space to force
update, R to reverse sort, Q to exit.
复制代码
adb shell top -n 2 -d 1 -s 9 | findstr package
复制代码
import csv,os,time


class Monitoring(object):
    def __init__(self,count,pkg):
        self.counter = count
        self.pkg = pkg
        self.cpudata=[('timestamp','cpustatus')]
        self.memdata=[('timestamp','memstatus')]


    def getCurrentTime(self):
        currentTime=time.strftime("%H:%M:%S",time.localtime())
        return currentTime

    def getCurrentDate(self):
        datetime = time.strftime("%Y-%m-%d %H_%M_%S")
        return datetime

    def monitoring_cpu(self):
        result = os.popen("adb shell top -n 2 -d 1 -s 9 | findstr " +self.pkg)
        res = result.readline().split(" ")
        print(res)

        if res == [""]:
            print("no data!!!")
        else:
            cpuvalue = list(filter(None,res))[8]
            print(cpuvalue)
            currenttime = self.getCurrentTime()
            print("current time is:",currenttime)
            print("cpu data is",cpuvalue)
            self.cpudata.append([currenttime,cpuvalue])
            print(self.cpudata)

    def get_cpu_datas(self):
        while self.counter > 0:
            self.monitoring_cpu()
            self.counter=self.counter-1
            time.sleep(1)

    def saveDataToCsv(self,data_type):
        now = self.getCurrentDate()
        if data_type == "cpu":
            csvfile=open('./cpustatus_' +now+ '.csv','w',encoding="utf-8",newline='')
            write = csv.writer(csvfile)
            write.writerows(self.cpudata)
            csvfile.close()
        elif data_type == "mem":
            csvfile = open('./memstatus_' +now+ '.csv', 'w', encoding="utf-8", newline='')
            write = csv.writer(csvfile)
            write.writerows(self.memdata)
            csvfile.close()
        else:
            print('data_type error!')


if __name__ == "__main__":
    m = Monitoring(20,"com.youku.phone")
    m.get_cpu_datas()
    m.saveDataToCsv("cpu")
复制代码

 

作者:solomon-zj

出处:https://www.cnblogs.com/solomon-zj/p/17259196.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   solomon-zj  阅读(126)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示