Android在多核设备状态下读取CPU时间 [英] Android read CPU time in states for multicore devices

查看:83
本文介绍了Android在多核设备状态下读取CPU时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的应用显示每个频率下CPU所花费的时间.主要问题是当CPU脱机(由于热插拔或深度睡眠)时,文件/CPUN/cpufreq/stats/time_in_state被删除,并且时间重置为全0.

I'm trying to have my app show time spent by the CPU at each frequency. The main issue is when a CPU goes offline (due to hotplugging or deep sleep) the file /CPUN/cpufreq/stats/time_in_state gets removed and times reset to all 0s.

所以这限制了我只能在CPU 0上显示时间.

So this limits me to only be able to show the times on CPU 0.

我尝试过的事情

  • 使用FileObserver服务监视文件的创建/删除从理论上讲应该可以,但是由于某种原因,当设备进入深度睡眠状态时,它无法报告这些文件的删除情况.与唤醒时相同,必须先创建文件,然后FileObserver才能继续监视.
  • Use FileObserver service to monitor the creation/deletion of the file This should work in theory but for whatever reason when the device goes into deep sleep it fails to report the deleting of those files. Same for when waking, the files must be created before the FileObserver is able to resume monitoring.

这么短的唤醒锁,是否有可能监视脱机的CPU中的时间,清除time_in_state文件?

So short of a wakelock, is it at all possible to monitor the times from those CPUs that go offline, clearing time_in_state files?

推荐答案

尝试一下:

String path = "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state"; // CPU0
String path1 = "/sys/devices/system/cpu/cpu1/cpufreq/stats/time_in_state"; // CPU1
String path2 = "/sys/devices/system/cpu/cpu2/cpufreq/stats/time_in_state"; // CPU2
String path3 = "/sys/devices/system/cpu/cpu3/cpufreq/stats/time_in_state"; // CPU3
//...
InputStream states = new FileInputStream(path);
InputStreamReader reader = new InputStreamReader(states);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = "";
 while ((line = bufferedReader.readLine()) != null) {
      System.out.println("RESULT: " + line);
}

您可以使用for循环获取所有CPU.获取可用数量:
int cores = Runtime.getRuntime().availableProcessors()

You can use a for loop to get all CPUs. To get Number of available use:
int cores = Runtime.getRuntime().availableProcessors()

所以for从0cores - 1

这篇关于Android在多核设备状态下读取CPU时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆