0


鸿蒙开源!OpenHarmony——手机的CPU信息应用

1.应用安装步骤

  • 应用下载地址与源码开源如下: CPU_device_information

2.实现功能

完成了开发者手机以下信息的获取

  1. - CPU核心数
  2. - SOC型号
  3. - GPU温度
  4. - 主板温度
  5. - 系统运行时间
  6. - RAM总内存
  7. - RAM可用内存
  8. - RAM空闲内存
  9. - 缓存使用内存
  10. - Swaps交换分区
  11. - 系统启动以来创建的进程数
  12. - 上下文切换的总数
  13. - SOC温度
  14. - CPU利用率
  15. - CPU大核7温度和利用率
  16. - CPU中核6温度和利用率
  17. - CPU中核5温度和利用率
  18. - CPU中核4温度和利用率
  19. - CPU小核3温度和利用率
  20. - CPU小核2温度和利用率
  21. - CPU小核1温度和利用率
  22. - CPU小核0温度和利用率
  23. - 设备电量
  24. - 电池电压
  25. - 电池型号
  26. - 电池充电状态
  27. - 系统版本
  28. - RTC时间和日期
  29. - 内核版本信息
  30. - 电池信息

【开发者手机开箱】开源!OpenHarmony手机 CPU信息应用_开发者手机

3.功能实现逻辑

3.1 通过Native C++ 开发方式读取开发板端文件获取手机各项信息。

  1. # 获取SOC型号
  2. proc/device-tree/cpuinfo_hardware
  3. # 获取rtc时间
  4. /sys/class/rtc/rtc0/time
  5. # 获取内核信息
  6. /proc/version
  7. # 获取RTC系统日期
  8. /sys/class/rtc/rtc0/date
  9. # 交换分区大小
  10. /proc/swaps
  11. # 获取主板热区
  12. /sys/class/thermal/thermal_zone27/temp
  13. # 获取GPU热区
  14. /sys/class/thermal/thermal_zone17/temp
  15. # 获取lit0-thmzone 小核心 0 热区
  16. /sys/class/thermal/thermal_zone13/temp
  17. # 获取lit1-thmzone 小核心 1 热区
  18. /sys/class/thermal/thermal_zone14/temp
  19. # 获取lit2-thmzone 小核心 2 热区
  20. /sys/class/thermal/thermal_zone15/temp
  21. # 获取lit3-thmzone 小核心 3 热区
  22. /sys/class/thermal/thermal_zone16/temp
  23. # 获取mid4-thmzone 中核心 4 热区
  24. /sys/class/thermal/thermal_zone9/temp
  25. # 获取mid5-thmzone 中核心 5 热区
  26. /sys/class/thermal/thermal_zone10/temp
  27. # 获取mid6-thmzone 中核心 6 热区
  28. /sys/class/thermal/thermal_zone11/temp
  29. # 获取big7-thmzone 大核心 7 热区
  30. /sys/class/thermal/thermal_zone7/temp
  31. # 获取soc-thmzone系统芯片热区
  32. /sys/class/thermal/thermal_zone5/temp
  33. # /proc/uptime 是一个特殊的文件,它提供了当前系统的运行时间信息。文件中包含了两个数值,分别表示系统的总运行时间和空闲时间。
  34. /proc/uptime
  35. # 获取内存信息
  36. /proc/meminfo
  37. # 获取cpu info
  38. /proc/cpuinfo
  39. # 计算cpu利用率,进程计数器,正在运行的进程计数器,阻塞的进程计数器,系统发生的上下文切换次数
  40. /proc/stat
3.2 Native C++开发的api
  1. export const getCpuCount: () => Number; //获取cpu核心数
  2. export const getMemTotal: () => String; //获取RAM总内存大小
  3. export const getFreeMem: () => String; //获取空闲内存大小
  4. export const getCachedMem: () => String; //获取缓存使用内存大小
  5. export const getAvailableMem: () => String; //获取可用内存大小
  6. export const getCpuInfo: () => any; //获取CPU信息
  7. export const getMemoryInfo: () => any; //获取RAM信息
  8. export const getUptime: () => String; //读取/proc/uptime,/proc/uptime 是一个特殊的文件,它提供了当前系统的运行时间信息。文件中包含了两个数值,分别表示系统的总运行时间和空闲时间。
  9. export const getSOCtemp: () => String; //获取soc-thmzone系统芯片热区 /sys/class/thermal/thermal_zone5/temp
  10. export const getCPU_CORE_big7_thmzonetemp: () => String; //获取big7-thmzone 大核心 7 热区
  11. export const getCPU_CORE_mid6_thmzonetemp: () => String; //获取mid6-thmzone 中核心 6 热区
  12. export const getCPU_CORE_mid5_thmzonetemp: () => String; //获取mid6-thmzone 中核心 5 热区
  13. export const getCPU_CORE_mid4_thmzonetemp: () => String; //获取mid6-thmzone 中核心 4 热区
  14. export const getCPU_CORE_lit3_thmzonetemp: () => String; //获取lit3-thmzone 小核心 3 热区
  15. export const getCPU_CORE_lit2_thmzonetemp: () => String; //获取lit2-thmzone 小核心 2 热区
  16. export const getCPU_CORE_lit1_thmzonetemp: () => String; //获取lit1-thmzone 小核心 1 热区
  17. export const getCPU_CORE_lit0_thmzonetemp: () => String; //获取lit0-thmzone 小核心 0 热区
  18. export const getGPU_temp: () => String; //获取GPU 热区
  19. export const getBoard_temp: () => String; //获取主板 热区
  20. export const getSwaps: () => String; //获取交换分区大小
  21. export const getRTC_Date_temp: () => String; //获取rtc日期
  22. export const getKernel_version: () => String; //获取内核信息
  23. export const getRTC_Time_temp: () => String; //获取rtc时间
  24. export const getCpu_stat_cpu: () => String; //获取cpu以及各个核利用率
  25. export const getprocesses: () => String; //获取正在运行的进程数
  26. export const getctxt: () => String; //获取正在运行的进程数
  27. export const getcpuinfo_hardware: () => String; //获取SOC型号

4.功能实现逻辑剖析

4.1 底部导航栏、顶部状态栏设置

  1. Index.ets
  2. import window from '@ohos.window';
  3. import common from '@ohos.app.ability.common';
  4. //沉浸式界面开发:https://gitee.com/openharmony/docs/blob/master/zh-cn/third-party-cases/immersion-mode.md#%E5%8F%82%E8%80%83
  5. context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
  6. async setSystemBar() {
  7. let windowClass = await window.getLastWindow(this.context)
  8. //设置导航栏,状态栏不可见
  9. /*
  10. * let names: Array<'status'|'navigation'> = ['navigation'];//设置顶部状态栏不可见
  11. * let names: Array<'status'|'navigation'> = ['status'];//设置底部导航栏不可见
  12. * let names: Array<'status'|'navigation'> = [];//设置
  13. */
  14. let names: Array<'status'|'navigation'> = ["navigation"];
  15. await windowClass.setWindowSystemBarEnable(names)
  16. }
  17. aboutToAppear() {
  18. this.setSystemBar()
  19. }

4.2 获取SOC型号

读取开发板proc/device-tree/cpuinfo_hardware文件获取SOC型号。

【开发者手机开箱】开源!OpenHarmony手机 CPU信息应用_开发者手机_02

  1. TestStatisticsInfo.cpp
  2. // proc/device-tree/cpuinfo_hardware
  3. // 查看SOC型号
  4. napi_value TestStatisticsInfo::Getcpuinfo_hardware(napi_env env, napi_callback_info info) {
  5. if ((nullptr == env) || (nullptr == info)) {
  6. LOGE("TestStatisticsInfo::Getcpuinfo_hardware: env or info is null");
  7. return nullptr;
  8. }
  9. napi_value thisArg;
  10. if (napi_ok != napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr)) {
  11. LOGE("TestStatisticsInfo::Getcpuinfo_hardware: napi_get_cb_info fail");
  12. return nullptr;
  13. }
  14. std::string time = getcpuinfo_hardware();
  15. LOGI("getcpuinfo_hardware success! %{public}s", time.c_str());
  16. napi_value res;
  17. napi_create_string_utf8(env, time.c_str(), strlen(time.c_str()), &res);
  18. return res;
  19. }
  20. std::string TestStatisticsInfo::getcpuinfo_hardware() {
  21. FILE *fp0 = fopen("proc/device-tree/cpuinfo_hardware", "r");
  22. if (NULL == fp0) {
  23. LOGE("TestStatisticsInfo:getcpuinfo_hardware failed to open cpuinfo =======");
  24. return 0;
  25. }
  26. std::string temp0 = "";
  27. char buffer0[1024]{};
  28. fgets(buffer0, sizeof(buffer0), fp0);
  29. temp0.assign(buffer0); // 将buffer转换为字符串类型并赋值给time。
  30. LOGE("TestStatisticsInfo::getcpuinfo_hardware %{public}d =======", buffer0);
  31. fclose(fp0);
  32. return temp0;
  33. }
  1. index.d.ts
  2. export const getcpuinfo_hardware: () => String; //获取SOC型号
  1. @State cpuinfo_hardware: String = '';
  2. //aboutToAppear函数在创建自定义组件的新实例后,在执行其build()函数之前执行。允许在aboutToAppear函数中改变状态变量,更改将在后续执行build()函数中生效。
  3. aboutToAppear() {
  4. //getcpuinfo_hardware
  5. this.cpuinfo_hardware = testStatisticsApi.getcpuinfo_hardware();
  6. console.log("========Cpu_stat_cpu is ",this.cpuinfo_hardware)
  7. }

4.3 获取cpu以及各个核利用率、正在运行的进程数、上下文切换的总数

读取开发板proc/stat目录获取
【开发者手机开箱】开源!OpenHarmony手机 CPU信息应用_开发者手机_03

  1. # cat proc/stat
  2. cpu 136846 473 429582 992274 115 40307 14266 0 0 0
  3. cpu0 37440 30 120671 435245 107 10714 3552 0 0 0
  4. cpu1 35618 28 108085 58002 1 8694 3314 0 0 0
  5. cpu2 17470 20 58705 74576 6 8023 3100 0 0 0
  6. cpu3 13264 13 53196 77765 0 7046 2920 0 0 0
  7. cpu4 12691 121 33814 81271 0 2239 554 0 0 0
  8. cpu5 12134 134 34550 84395 0 2245 444 0 0 0
  9. cpu6 5407 83 14615 87894 0 1123 236 0 0 0
  10. cpu7 2819 41 5944 93124 0 219 143 0 0 0
  11. intr 28465308 0 303012 15607597 0 0 926124 2759258 0 0 0 0 6706602 0 0 0 0 0 0 0 0 0 0 0 0 0 1506 0 0 0 69 0 734 0 20 302 26293 19428 358 11614 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49979 0 0 0 0 0 0 1916 0 0 0 0 0 0 1477101 13 0 225612 3534 0 0 0 0 0 105 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 62 8 51 3 0 0 0 0 0 0 0 0 0 2 0 0 49 0 341969 17 30 0 1933
  12. ctxt 45507242
  13. btime 534
  14. processes 6746
  15. procs_running 5
  16. procs_blocked 0
  17. softirq 7457395 112 685658 199 4997 15826 0 3684 2582028 0 4164891

以上参数含义解释如下:

  • cpu: 包含了 CPU 的使用情况统计信息,依次表示用户态时间、Nice 值为负的进程在用户态的CPU时间、系统态时间、空闲时间、等待IO的时间、硬中断时间、软中断时间和虚拟化的CPU操作时间。
  • cpu0 - cpu7: 每个核心的 CPU 使用情况统计信息,格式与全局的 cpu 行相同。
  • intr: 包含了中断请求的统计信息,依次表示总的中断数以及每种中断的具体计数。
  • ctxt: 上下文切换的次数,包括进程切换和内核线程切换。
  • btime: 系统启动时间
  • processes: 启动以来创建的进程数。
  • procs_running: 当前正在执行的进程数量。
  • procs_blocked: 当前被阻塞的进程数量。
  • softirq: 软中断的统计信息,依次表示软中断的次数和每种软中断的具体计数。

注意:需要修改开发板

  1. /vendor/etc/init.uis7885.cfg

文件将

  1. proc/stat

的权限改为

  1. 777
  1. TestStatisticsInfo.cpp
  2. std::string TestStatisticsInfo::getCpu_stat(std::string field) {
  3. std::ifstream meminfo("/proc/stat");
  4. std::string line;
  5. std::string cpu0Field = "cpu0";
  6. std::string cpu1Field = "cpu1";
  7. std::string cpu2Field = "cpu2";
  8. std::string cpu3Field = "cpu3";
  9. std::string cpu4Field = "cpu4";
  10. std::string cpu5Field = "cpu5";
  11. std::string cpu6Field = "cpu6";
  12. std::string cpu7Field = "cpu7";
  13. std::string cpuField = "cpu\u0020";
  14. std::string processesField = "processes"; // processes: 进程计数器统计。这个字段表示当前运行的进程数量。
  15. std::string procs_runningField = "procs_running"; //procs_running: 正在运行的进程计数器统计。这个字段表示当前正在运行的进程数量。
  16. std::string procs_blockedField = "procs_blocked";//procs_blocked: 阻塞的进程计数器统计。这个字段表示当前被阻塞的进程数量。
  17. std::string ctxtField = "ctxt";//上下文切换计数器统计。这个字段表示系统发生的上下文切换次数,可以用于评估系统的调度性能。
  18. while (getline(meminfo, line)) {
  19. if (line.find(processesField) != std::string::npos) {
  20. std::string res = line;
  21. _Cpu_stat[processesField] = res;
  22. }
  23. if (line.find(procs_runningField) != std::string::npos) {
  24. std::string res = line;
  25. _Cpu_stat[procs_runningField] = res;
  26. }
  27. if (line.find(procs_blockedField) != std::string::npos) {
  28. std::string res = line;
  29. _Cpu_stat[procs_blockedField] = res;
  30. }
  31. if (line.find(ctxtField) != std::string::npos) {
  32. std::string res = line;
  33. _Cpu_stat[ctxtField] = res;
  34. }
  35. if (line.find(cpu7Field) != std::string::npos) {
  36. std::string res = line;
  37. _Cpu_stat[cpu7Field] = res;
  38. }
  39. if (line.find(cpu6Field) != std::string::npos) {
  40. std::string res = line;
  41. _Cpu_stat[cpu6Field] = res;
  42. }
  43. if (line.find(cpu5Field) != std::string::npos) {
  44. std::string res = line;
  45. _Cpu_stat[cpu5Field] = res;
  46. }
  47. if (line.find(cpu4Field) != std::string::npos) {
  48. std::string res = line;
  49. _Cpu_stat[cpu4Field] = res;
  50. }
  51. if (line.find(cpu3Field) != std::string::npos) {
  52. std::string res = line;
  53. _Cpu_stat[cpu3Field] = res;
  54. }
  55. if (line.find(cpu2Field) != std::string::npos) {
  56. std::string res = line;
  57. _Cpu_stat[cpu2Field] = res;
  58. }
  59. if (line.find(cpu1Field) != std::string::npos) {
  60. std::string res = line;
  61. _Cpu_stat[cpu1Field] = res;
  62. }
  63. if (line.find(cpu0Field) != std::string::npos) {
  64. std::string res = line;
  65. _Cpu_stat[cpu0Field] = res;
  66. }
  67. if (line.find(cpuField) != std::string::npos) {
  68. std::string res = line;
  69. _Cpu_stat[cpuField] = res;
  70. }
  71. }
  72. return _Cpu_stat[field];
  73. }
4.3.1 获取cpu以及各个核利用率

根据/proc/stat 文件内容可以计算 Linux CPU 利用率

【开发者手机开箱】开源!OpenHarmony手机 CPU信息应用_开发者手机_04

  1. # cat proc/stat
  2. cpu 136846 473 429582 992274 115 40307 14266 0 0 0
  3. cpu0 37440 30 120671 435245 107 10714 3552 0 0 0
  4. cpu1 35618 28 108085 58002 1 8694 3314 0 0 0
  5. cpu2 17470 20 58705 74576 6 8023 3100 0 0 0
  6. cpu3 13264 13 53196 77765 0 7046 2920 0 0 0
  7. cpu4 12691 121 33814 81271 0 2239 554 0 0 0
  8. cpu5 12134 134 34550 84395 0 2245 444 0 0 0
  9. cpu6 5407 83 14615 87894 0 1123 236 0 0 0
  10. cpu7 2819 41 5944 93124 0 219 143 0 0 0

这是一个 CPU(中央处理器)使用情况的统计信息。每一行都表示一个 CPU 核心的使用情况。
下面是对每一列的解释:

  1. cpu: 总体统计信息
  2. cpu0cpu1cpu2、等等:各个 CPU 核心的统计信息
  3. user: 用户模式下运行时间
  4. nice: 优先级较低的用户模式下运行时间
  5. system: 内核模式下运行时间
  6. idle: 空闲时间
  7. iowait: 等待输入/输出完成的时间
  8. irq: 处理硬件中断的时间
  9. softirq: 处理软件中断的时间
  10. steal: 被虚拟化主机偷取的时间
  11. guest: 运行虚拟 CPU 的时间
  12. guest_nice: 运行虚拟 CPU 且优先级较低的时间
  13. /proc/stat 文件中,CPU 利用率的时间单位是“时钟滴答”(clock ticks)。每个 Linux 系统都有一个时钟线程(clock tick),它以固定的速率生成时钟滴答来驱动系统的计时器。时钟滴答的大小依赖于系统的硬件和配置。它通常以毫秒(ms)为单位,但也可能以微秒(μs)或纳秒(ns)为单位,具体取决于系统。要获取实际的时间单位,你可以查看 /proc/timer_list /proc/timer_stats 文件中的信息。需要注意的是,这些时钟滴答并不是以独立的单位存在的,它们仅用于相对测量和计算 CPU 的利用率。因此,在分析 CPU 利用率时,我们通常关注的是两个时间点之间的差异,而不是实际的时钟滴答值本身。
  • 计算方式:

cpu总时间 = user + nice + system + idle + iowait + irq + softirq + stealstolen + guest + guest_nice
在一段时间内获取两次cpu时间分配信息。
两次的cpu总时间:total_2 - total_1
两次的cpu剩余时间:idle_2 - idle_1
两次的cpu使用时间:used = (total_2 - total_1) - (idle_2 - idle_1)
cpu使用率 = 使用时间 / 总时间 * 100% = used / total * 100%

  1. TestStatisticsInfo.cpp
  2. //计算cpu利用率
  3. std::string TestStatisticsInfo::calculateCpuUtilization(std::string& a,std::string& a_second) {
  4. std::istringstream iss_a(a);
  5. std::istringstream iss_a_second(a_second);
  6. // 提取每个字段的值
  7. std::string cpu_name, cpu_name_second;
  8. int user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
  9. int user_second, nice_second, system_second, idle_second, iowait_second, irq_second, softirq_second, steal_second,
  10. guest_second, guest_nice_second;
  11. iss_a >> cpu_name >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice;
  12. iss_a_second >> cpu_name_second >> user_second >> nice_second >> system_second >> idle_second >> iowait_second >>
  13. irq_second >> softirq_second >> steal_second >> guest_second >> guest_nice_second;
  14. // 计算总的 CPU 时间和空闲 CPU 时间
  15. int total_time = user + nice + system + idle + iowait + irq + softirq + steal;
  16. int total_time_second = user_second + nice_second + system_second + idle_second + iowait_second + irq_second +
  17. softirq_second + steal_second;
  18. int idle_time = idle + iowait;
  19. int idle_time_second = idle_second + iowait_second;
  20. // 计算 CPU 利用率
  21. double cpu_utilization =
  22. 100.0 * (1.0 - (idle_time_second - idle_time) / static_cast<double>(total_time_second - total_time));
  23. return std::to_string(cpu_utilization);
  24. }
  25. // 计算cpu利用率
  26. napi_value TestStatisticsInfo::GetCpu_stat_cpu(napi_env env, napi_callback_info info) {
  27. if ((nullptr == env) || (nullptr == info)) {
  28. LOGE("TestStatisticsInfo::GetCachedMem: env or info is null");
  29. return nullptr;
  30. }
  31. napi_value thisArg;
  32. if (napi_ok != napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr)) {
  33. LOGE("TestStatisticsInfo::GetCpu_stat_cpu: napi_get_cb_info fail");
  34. return nullptr;
  35. }
  36. std::string cpu_cached0 = getCpu_stat("cpu\u0020");
  37. std::string cpu0_cached0 = getCpu_stat("cpu0");
  38. std::string cpu1_cached0 = getCpu_stat("cpu1");
  39. std::string cpu2_cached0 = getCpu_stat("cpu2");
  40. std::string cpu3_cached0 = getCpu_stat("cpu3");
  41. std::string cpu4_cached0 = getCpu_stat("cpu4");
  42. std::string cpu5_cached0 = getCpu_stat("cpu5");
  43. std::string cpu6_cached0 = getCpu_stat("cpu6");
  44. std::string cpu7_cached0 = getCpu_stat("cpu7");
  45. std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 延时 100 毫秒
  46. std::string cpu_cached1 = getCpu_stat("cpu\u0020");
  47. std::string cpu0_cached1 = getCpu_stat("cpu0");
  48. std::string cpu1_cached1 = getCpu_stat("cpu1");
  49. std::string cpu2_cached1 = getCpu_stat("cpu2");
  50. std::string cpu3_cached1 = getCpu_stat("cpu3");
  51. std::string cpu4_cached1 = getCpu_stat("cpu4");
  52. std::string cpu5_cached1 = getCpu_stat("cpu5");
  53. std::string cpu6_cached1 = getCpu_stat("cpu6");
  54. std::string cpu7_cached1 = getCpu_stat("cpu7");
  55. std::string cpu = calculateCpuUtilization(cpu_cached0, cpu_cached1);
  56. std::string cpu0 = calculateCpuUtilization(cpu0_cached0, cpu0_cached1);
  57. std::string cpu1 = calculateCpuUtilization(cpu1_cached0, cpu1_cached1);
  58. std::string cpu2 = calculateCpuUtilization(cpu2_cached0, cpu2_cached1);
  59. std::string cpu3 = calculateCpuUtilization(cpu3_cached0, cpu3_cached1);
  60. std::string cpu4 = calculateCpuUtilization(cpu4_cached0, cpu4_cached1);
  61. std::string cpu5 = calculateCpuUtilization(cpu5_cached0, cpu5_cached1);
  62. std::string cpu6 = calculateCpuUtilization(cpu6_cached0, cpu6_cached1);
  63. std::string cpu7 = calculateCpuUtilization(cpu7_cached0, cpu7_cached1);
  64. std::string aaa = cpu + " " + cpu0 + " " + cpu1 + " " + cpu2 + " " + cpu3 + " " + cpu4 + " " + cpu5 + " " + cpu6 + " " + cpu7;
  65. LOGI("GetCpu_stat_cpu success! Cached is %{public}s", aaa.c_str());
  66. napi_value res;
  67. napi_create_string_utf8(env, aaa.c_str(), strlen(aaa.c_str()), &res);
  68. return res;
  69. }
  1. index.d.ts
  2. export const getCpu_stat_cpu: () => String; //获取cpu以及各个核利用率
  1. Index.ts
  2. import testStatisticsApi from 'libentry.so';
  3. @State Cpu_stat_cpu: String = '';
  4. //getCpu_stat_cpu
  5. this.Cpu_stat_cpu = testStatisticsApi.getCpu_stat_cpu();
  6. console.log("========Cpu_stat_cpu is ",this.Cpu_stat_cpu)
  7. Text(
  8. "SOC温度:"+ (Number(this.soctemp)/1000).toFixed(3) +"°C" + "\t\t\t CPU利用率:" + (Number(this.Cpu_stat_cpu.split(" ")[0])).toFixed(3) +
  9. "\nCPU大核7温度:"+(Number(this.CPU_CORE_big7_temp)/1000).toFixed(3) +"°C" + "\tCPU大核7利用率:" + (Number(this.Cpu_stat_cpu.split(" ")[8])).toFixed(3) +
  10. "\nCPU中核6温度:"+(Number(this.CPU_CORE_mid6_temp)/1000).toFixed(3) +"°C" + "\tCPU中核6利用率:" + (Number(this.Cpu_stat_cpu.split(" ")[7])).toFixed(3) +
  11. "\nCPU中核5温度:"+(Number(this.CPU_CORE_mid5_temp)/1000).toFixed(3) +"°C" + "\tCPU中核5利用率:" + (Number(this.Cpu_stat_cpu.split(" ")[6])).toFixed(3) +
  12. "\nCPU中核4温度:"+(Number(this.CPU_CORE_mid4_temp)/1000).toFixed(3) +"°C" + "\tCPU中核4利用率:" + (Number(this.Cpu_stat_cpu.split(" ")[5])).toFixed(3) +
  13. "\nCPU小核3温度:"+(Number(this.CPU_CORE_lit3_temp)/1000).toFixed(3) +"°C" + "\tCPU小核3利用率:" + (Number(this.Cpu_stat_cpu.split(" ")[4])).toFixed(3) +
  14. "\nCPU小核2温度:"+(Number(this.CPU_CORE_lit2_temp)/1000).toFixed(3) +"°C" + "\tCPU小核2利用率:" + (Number(this.Cpu_stat_cpu.split(" ")[3])).toFixed(3) +
  15. "\nCPU小核1温度:"+(Number(this.CPU_CORE_lit1_temp)/1000).toFixed(3) +"°C" + "\tCPU小核1利用率:" + (Number(this.Cpu_stat_cpu.split(" ")[2])).toFixed(3) +
  16. "\nCPU小核0温度:"+(Number(this.CPU_CORE_lit0_temp)/1000).toFixed(3) +"°C" + "\tCPU小核0利用率:" + (Number(this.Cpu_stat_cpu.split(" ")[1])).toFixed(3)
  17. )
4.3.2 获取上下文切换的总数
  1. TestStatisticsInfo.cpp
  2. // 获取proc/stat文件的ctxt: 上下文切换的总数
  3. napi_value TestStatisticsInfo::Getctxt(napi_env env, napi_callback_info info) {
  4. if ((nullptr == env) || (nullptr == info)) {
  5. LOGE("TestStatisticsInfo::Getctxt: env or info is null");
  6. return nullptr;
  7. }
  8. napi_value thisArg;
  9. if (napi_ok != napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr)) {
  10. LOGE("TestStatisticsInfo::Getctxt: napi_get_cb_info fail");
  11. return nullptr;
  12. }
  13. std::string cached = getCpu_stat("ctxt");
  14. LOGI("getCpu_stat success! Cached is %{public}s", cached.c_str());
  15. napi_value res;
  16. napi_create_string_utf8(env, cached.c_str(), strlen(cached.c_str()), &res);
  17. return res;
  18. }
4.3.3 启动的进程数
  1. TestStatisticsInfo.cpp
  2. //获取proc/stat文件的processes: 启动的进程数
  3. napi_value TestStatisticsInfo::Getprocesses(napi_env env, napi_callback_info info) {
  4. if ((nullptr == env) || (nullptr == info)) {
  5. LOGE("TestStatisticsInfo::Getprocesses: env or info is null");
  6. return nullptr;
  7. }
  8. napi_value thisArg;
  9. if (napi_ok != napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr)) {
  10. LOGE("TestStatisticsInfo::Getprocesses: napi_get_cb_info fail");
  11. return nullptr;
  12. }
  13. std::string cached = getCpu_stat("processes");
  14. LOGI("getCpu_stat success! Cached is %{public}s", cached.c_str());
  15. napi_value res;
  16. napi_create_string_utf8(env, cached.c_str(), strlen(cached.c_str()), &res);
  17. return res;
  18. }

062b7e9f99042d0461e287c5c0ee8749.png


本文转载自: https://blog.csdn.net/m0_62167422/article/details/136237001
版权归原作者 移不动开发技术 所有, 如有侵权,请联系我们删除。

“鸿蒙开源!OpenHarmony——手机的CPU信息应用”的评论:

还没有评论