开源!OpenHarmony手机CPU消息运行

想了解更多对于开源的内容,请访问:

鸿蒙开发者社区

笔者最近写了一个OpenHarmony开发者手机运行开失掉手机的各种消息,源码开源如下: CPU_device_information

1.运行装置步骤

2.成功性能

成功了开发者手机以下消息的失掉。

- CPU外围数- SOC型号- GPU温度- 主板温度- 系统运转期间- RAM总内存- RAM可用内存- RAM闲暇内存- 缓存经常使用内存- Swaps交流分区- 系统启动以来创立的进程数- 高低文切换的总数- SOC温度- CPU应用率- CPU大核7温度和应用率- CPU中核6温度和应用率- CPU中核5温度和应用率- CPU中核4温度和应用率- CPU小核3温度和应用率- CPU小核2温度和应用率- CPU小核1温度和应用率- CPU小核0温度和应用率- 设施电量- 电池电压- 电池型号- 电池充电形态- 系统版本- RTC期间和日期- 内核版本消息- 电池消息

3.性能成功逻辑

(1)经过Native C++ 开发形式读取开发板端文件失掉手机各项消息

# 失掉SOC型号proc/device-tree/cpuinfo_hardware# 失掉rtc期间/sys/class/rtc/rtc0/time# 失掉内核消息/proc/version# 失掉RTC系统日期/sys/class/rtc/rtc0/date# 交流分区大小/proc/swaps# 失掉主板热区/sys/class/thermal/thermal_zone27/temp# 失掉GPU热区/sys/class/thermal/thermal_zone17/temp# 失掉lit0-thmzone 小外围 0 热区/sys/class/thermal/thermal_zone13/temp# 失掉lit1-thmzone 小外围 1 热区/sys/class/thermal/thermal_zone14/temp# 失掉lit2-thmzone 小外围 2 热区/sys/class/thermal/thermal_zone15/temp# 失掉lit3-thmzone 小外围 3 热区/sys/class/thermal/thermal_zone16/temp# 失掉mid4-thmzone 中外围 4 热区/sys/class/thermal/thermal_zone9/temp# 失掉mid5-thmzone 中外围 5 热区/sys/class/thermal/thermal_zone10/temp# 失掉mid6-thmzone 中外围 6 热区/sys/class/thermal/thermal_zone11/temp# 失掉big7-thmzone 大外围 7 热区/sys/class/thermal/thermal_zone7/temp# 失掉soc-thmzone系统芯片热区/sys/class/thermal/thermal_zone5/temp# /proc/uptime 是一个不凡的文件,它提供了系统的运转期间消息。文件中蕴含了两个数值,区分示意系统的总运转期间和闲暇期间。/proc/uptime# 失掉内存消息/proc/meminfo# 失掉cpu info/proc/cpuinfo# 计算cpu应用率,进程计数器,正在运转的进程计数器,阻塞的进程计数器,系统出现的高低文切换次数/proc/stat

(2)Native C++开发的api

export const getCpuCount: () => Number;//失掉cpu外围数export const getMemTotal: () => String;//失掉RAM总内存大小export const getFreeMem: () => String;//失掉闲暇内存大小export const getCachedMem: () => String;//失掉缓存经常使用内存大小export const getAvailableMem: () => String;//失掉可用内存大小export const getCpuInfo: () => any;//失掉CPU消息export const getMemoryInfo: () => any;//失掉RAM消息export const getUptime: () => String;//读取/proc/uptime,/proc/uptime 是一个不凡的文件,它提供了系统的运转期间消息。文件中蕴含了两个数值,区分示意系统的总运转期间和闲暇期间。export const getSOCtemp: () => String;//失掉soc-thmzone系统芯片热区 /sys/class/thermal/thermal_zone5/tempexport const getCPU_CORE_big7_thmzonetemp: () => String; //失掉big7-thmzone 大外围 7 热区export const getCPU_CORE_mid6_thmzonetemp: () => String; //失掉mid6-thmzone 中外围 6 热区export const getCPU_CORE_mid5_thmzonetemp: () => String; //失掉mid6-thmzone 中外围 5 热区export const getCPU_CORE_mid4_thmzonetemp: () => String; //失掉mid6-thmzone 中外围 4 热区export const getCPU_CORE_lit3_thmzonetemp: () => String; //失掉lit3-thmzone 小外围 3 热区export const getCPU_CORE_lit2_thmzonetemp: () => String; //失掉lit2-thmzone 小外围 2 热区export const getCPU_CORE_lit1_thmzonetemp: () => String; //失掉lit1-thmzone 小外围 1 热区export const getCPU_CORE_lit0_thmzonetemp: () => String; //失掉lit0-thmzone 小外围 0 热区export const getGPU_temp: () => String;//失掉GPU 热区export const getBoard_temp: () => String;//失掉主板 热区export const getSwaps: () => String;//失掉交流分区大小export const getRTC_Date_temp: () => String;//失掉rtc日期export const getKernel_version: () => String;//失掉内核消息export const getRTC_Time_temp: () => String;//失掉rtc期间export const getCpu_stat_cpu: () => String;//失掉cpu以及各个核应用率export const getprocesses: () => String;//失掉正在运转的进程数export const getctxt: () => String;//失掉正在运转的进程数export const getcpuinfo_hardware: () => String;//失掉SOC型号

4.性能成功逻辑剖析

(1)底部导航栏、顶部形态栏设置

参考: 沉迷式界面开发

Index.etsimport window from '@ohos.window';import common from '@ohos.app.ability.common';//沉迷式界面开发:common.UIAbilityContext = getContext(this) as common.UIAbilityContextasync setSystemBar() {let windowClass = await window.getLastWindow(this.context)//设置导航栏,形态栏无法见/** let names: Array<'status' | 'navigation'> = ['navigation'];//设置顶部形态栏无法见* let names: Array<'status' | 'navigation'> = ['status'];//设置底部导航栏无法见* let names: Array<'status' | 'navigation'> = [];//设置*/let names: Array<'status' | 'navigation'> = ["navigation"];await windowClass.setWindowSystemBarEnable(names)}aboutToAppear() {this.setSystemBar()}

(2)失掉SOC型号

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

TestStatisticsInfo.cpp// proc/device-tree/cpuinfo_hardware// 检查SOC型号napi_value TestStatisticsInfo::Getcpuinfo_hardware(napi_env env, napi_callback_info info) {if ((nullptr == env) || (nullptr == info)) {LOGE("TestStatisticsInfo::Getcpuinfo_hardware: env or info is null");return nullptr;}napi_value thisArg;if (napi_ok != napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr)) {LOGE("TestStatisticsInfo::Getcpuinfo_hardware: napi_get_cb_info fail");return nullptr;}std::string time = getcpuinfo_hardware();LOGI("getcpuinfo_hardware success! %{public}s", time.c_str());napi_value res;napi_create_string_utf8(env, time.c_str(), strlen(time.c_str()), &res);return res;}std::string TestStatisticsInfo::getcpuinfo_hardware() {FILE *fp0 = fopen("proc/device-tree/cpuinfo_hardware", "r");if (NULL == fp0) {LOGE("TestStatisticsInfo:getcpuinfo_hardware failed to open cpuinfo =======");return 0;}std::string temp0 = "";char buffer0[1024]{};fgets(buffer0, sizeof(buffer0), fp0);temp0.assign(buffer0); // 将buffer转换为字符串类型并赋值给time。LOGE("TestStatisticsInfo::getcpuinfo_hardware %{public}d =======", buffer0);fclose(fp0);return temp0;}
index.d.tsexport const getcpuinfo_hardware: () => String;//失掉SOC型号
@State cpuinfo_hardware: String = '';//aboutToAppear函数在创立自定义组件的新实例后,在口头其build()函数之前口头。准许在aboutToAppear函数中扭转形态变量,更改将在后续口头build()函数中失效。aboutToAppear() {//getcpuinfo_hardwarethis.cpuinfo_hardware = testStatisticsApi.getcpuinfo_hardware();console.log("========Cpu_stat_cpu is ",this.cpuinfo_hardware)}

(3)失掉cpu以及各个核应用率、正在运转的进程数、高低文切换的总数

读取开发板proc/stat目录失掉。

# cat proc/statcpu136846 473 429582 992274 115 40307 14266 0 0 0cpu0 37440 30 120671 435245 107 10714 3552 0 0 0cpu1 35618 28 108085 58002 1 8694 3314 0 0 0cpu2 17470 20 58705 74576 6 8023 3100 0 0 0cpu3 13264 13 53196 77765 0 7046 2920 0 0 0cpu4 12691 121 33814 81271 0 2239 554 0 0 0cpu5 12134 134 34550 84395 0 2245 444 0 0 0cpu6 5407 83 14615 87894 0 1123 236 0 0 0cpu7 2819 41 5944 93124 0 219 143 0 0 0intr 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 1933ctxt 45507242btime 534processes 6746procs_running 5procs_blocked 0softirq 7457395 112 685658 199 4997 15826 0 3684 2582028 0 4164891

以上参数含意解释如下:

留意:须要修正开发板/vendor/etc/init.uis7885.cfg文件将proc/stat的权限改为777。

TestStatisticsInfo.cppstd::string TestStatisticsInfo::getCpu_stat(std::string field) {std::ifstream meminfo("/proc/stat");std::string line;std::string cpu0Field = "cpu0";std::string cpu1Field = "cpu1";std::string cpu2Field = "cpu2";std::string cpu3Field = "cpu3";std::string cpu4Field = "cpu4";std::string cpu5Field = "cpu5";std::string cpu6Field = "cpu6";std::string cpu7Field = "cpu7";std::string cpuField = "cpu\u0020";std::string processesField = "processes"; // processes: 进程计数器统计。这个字段示意运转的进程数量。std::string procs_runningField = "procs_running"; //procs_running: 正在运转的进程计数器统计。这个字段示意正在运转的进程数量。std::string procs_blockedField = "procs_blocked";//procs_blocked: 阻塞的进程计数器统计。这个字段示意被阻塞的进程数量。std::string ctxtField = "ctxt";//高低文切换计数器统计。这个字段示意系统出现的高低文切换次数,可以用于评价系统的调度性能。while (getline(meminfo, line)) {if (line.find(processesField) != std::string::npos) {std::string res = line;_Cpu_stat[processesField] = res;}if (line.find(procs_runningField) != std::string::npos) {std::string res = line;_Cpu_stat[procs_runningField] = res;}if (line.find(procs_blockedField) != std::string::npos) {std::string res = line;_Cpu_stat[procs_blockedField] = res;}if (line.find(ctxtField) != std::string::npos) {std::string res = line;_Cpu_stat[ctxtField] = res;}if (line.find(cpu7Field) != std::string::npos) {std::string res = line;_Cpu_stat[cpu7Field] = res;}if (line.find(cpu6Field) != std::string::npos) {std::string res = line;_Cpu_stat[cpu6Field] = res;}if (line.find(cpu5Field) != std::string::npos) {std::string res = line;_Cpu_stat[cpu5Field] = res;}if (line.find(cpu4Field) != std::string::npos) {std::string res = line;_Cpu_stat[cpu4Field] = res;}if (line.find(cpu3Field) != std::string::npos) {std::string res = line;_Cpu_stat[cpu3Field] = res;}if (line.find(cpu2Field) != std::string::npos) {std::string res = line;_Cpu_stat[cpu2Field] = res;}if (line.find(cpu1Field) != std::string::npos) {std::string res = line;_Cpu_stat[cpu1Field] = res;}if (line.find(cpu0Field) != std::string::npos) {std::string res = line;_Cpu_stat[cpu0Field] = res;}if (line.find(cpuField) != std::string::npos) {std::string res = line;_Cpu_stat[cpuField] = res;}}return _Cpu_stat[field];}

(4)失掉cpu以及各个核应用率

依据/proc/stat 文件内容可以计算 Linux CPU 应用率。

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

这是一个 CPU(中央解决器)经常使用状况的统计消息。每一行都示意一个 CPU 外围的经常使用状况。上方是对每一列的解释:

cpu: 总体统计消息cpu0、cpu1、cpu2、等等:各个 CPU 外围的统计消息user: 用户形式下运转期间nice: 优先级较低的用户形式下运转期间system: 内核形式下运转期间idle: 闲暇期间iowait: 期待输入/输入成功的期间irq: 解决配件终止的期间softirq: 解决软件终止的期间steal: 被虚构化服务器偷取的期间guest: 运转虚构 CPU 的期间guest_nice: 运转虚构 CPU 且优先级较低的期间在 /proc/stat 文件中,CPU 应用率的期间单位是“时钟滴答”(clock ticks)。每个 Linux 系统都有一个时钟线程(clock tick),它以固定的速率生成时钟滴答来驱动系统的计时器。时钟滴答的大小依赖于系统的配件和性能。它通常以毫秒(ms)为单位,但也或者以微秒(μs)或纳秒(ns)为单位,详细取决于系统。要失掉实践的期间单位,你可以检查 /proc/timer_list 或 /proc/timer_stats 文件中的消息。须要留意的是,这些时钟滴答并不是以独立的单位存在的,它们仅用于相对测量和计算 CPU 的应用率。因此,在剖析 CPU 应用率时,咱们通常关注的是两个期间点之间的差异,而不是实践的时钟滴答值自身。
TestStatisticsInfo.cpp//计算cpu应用率std::string TestStatisticsInfo::calculateCpuUtilization(std::string& a,std::string& a_second) {std::istringstream iss_a(a);std::istringstream iss_a_second(a_second);// 提取每个字段的值std::string cpu_name, cpu_name_second;int user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;int user_second, nice_second, system_second, idle_second, iowait_second, irq_second, softirq_second, steal_second,guest_second, guest_nice_second;iss_a >> cpu_name >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice;iss_a_second >> cpu_name_second >> user_second >> nice_second >> system_second >> idle_second >> iowait_second >>irq_second >> softirq_second >> steal_second >> guest_second >> guest_nice_second;// 计算总的 CPU 期间和闲暇 CPU 期间int total_time = user + nice + system + idle + iowait + irq + softirq + steal;int total_time_second = user_second + nice_second + system_second + idle_second + iowait_second + irq_second +softirq_second + steal_second;int idle_time = idle + iowait;int idle_time_second = idle_second + iowait_second;// 计算 CPU 应用率double cpu_utilization =100.0 * (1.0 - (idle_time_second - idle_time) / static_cast<double>(total_time_second - total_time));return std::to_string(cpu_utilization);}// 计算cpu应用率napi_value TestStatisticsInfo::GetCpu_stat_cpu(napi_env env, napi_callback_info info) {if ((nullptr == env) || (nullptr == info)) {LOGE("TestStatisticsInfo::GetCachedMem: env or info is null");return nullptr;}napi_value thisArg;if (napi_ok != napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr)) {LOGE("TestStatisticsInfo::GetCpu_stat_cpu: napi_get_cb_info fail");return nullptr;}std::string cpu_cached0 = getCpu_stat("cpu\u0020");std::string cpu0_cached0 = getCpu_stat("cpu0");std::string cpu1_cached0 = getCpu_stat("cpu1");std::string cpu2_cached0 = getCpu_stat("cpu2");std::string cpu3_cached0 = getCpu_stat("cpu3");std::string cpu4_cached0 = getCpu_stat("cpu4");std::string cpu5_cached0 = getCpu_stat("cpu5");std::string cpu6_cached0 = getCpu_stat("cpu6");std::string cpu7_cached0 = getCpu_stat("cpu7");std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 延时 100 毫秒std::string cpu_cached1 = getCpu_stat("cpu\u0020");std::string cpu0_cached1 = getCpu_stat("cpu0");std::string cpu1_cached1 = getCpu_stat("cpu1");std::string cpu2_cached1 = getCpu_stat("cpu2");std::string cpu3_cached1 = getCpu_stat("cpu3");std::string cpu4_cached1 = getCpu_stat("cpu4");std::string cpu5_cached1 = getCpu_stat("cpu5");std::string cpu6_cached1 = getCpu_stat("cpu6");std::string cpu7_cached1 = getCpu_stat("cpu7");std::string cpu = calculateCpuUtilization(cpu_cached0, cpu_cached1);std::string cpu0 = calculateCpuUtilization(cpu0_cached0, cpu0_cached1);std::string cpu1 = calculateCpuUtilization(cpu1_cached0, cpu1_cached1);std::string cpu2 = calculateCpuUtilization(cpu2_cached0, cpu2_cached1);std::string cpu3 = calculateCpuUtilization(cpu3_cached0, cpu3_cached1);std::string cpu4 = calculateCpuUtilization(cpu4_cached0, cpu4_cached1);std::string cpu5 = calculateCpuUtilization(cpu5_cached0, cpu5_cached1);std::string cpu6 = calculateCpuUtilization(cpu6_cached0, cpu6_cached1);std::string cpu7 = calculateCpuUtilization(cpu7_cached0, cpu7_cached1);std::string aaa = cpu + " " + cpu0 + " " + cpu1 + " " + cpu2 + " " + cpu3 + " " + cpu4 + " " + cpu5 + " " + cpu6 + " " + cpu7;LOGI("GetCpu_stat_cpu success! Cached is %{public}s", aaa.c_str());napi_value res;napi_create_string_utf8(env, aaa.c_str(), strlen(aaa.c_str()), &res);return res;}
index.d.tsexport const getCpu_stat_cpu: () => String;//失掉cpu以及各个核应用率
Index.tsimport testStatisticsApi from 'libentry.so';@State Cpu_stat_cpu: String = '';//getCpu_stat_cputhis.Cpu_stat_cpu = testStatisticsApi.getCpu_stat_cpu();console.log("========Cpu_stat_cpu is ",this.Cpu_stat_cpu)Text("SOC温度:"+ (Number(this.soctemp)/1000).toFixed(3) +"°C" + "\t\t\tCPU应用率:" + (Number(this.Cpu_stat_cpu.split(" ")[0])).toFixed(3) +"\nCPU大核7温度:"+(Number(this.CPU_CORE_big7_temp)/1000).toFixed(3) +"°C" + "\tCPU大核7应用率:" +(Number(this.Cpu_stat_cpu.split(" ")[8])).toFixed(3) +"\nCPU中核6温度:"+(Number(this.CPU_CORE_mid6_temp)/1000).toFixed(3) +"°C" + "\tCPU中核6应用率:" +(Number(this.Cpu_stat_cpu.split(" ")[7])).toFixed(3) +"\nCPU中核5温度:"+(Number(this.CPU_CORE_mid5_temp)/1000).toFixed(3) +"°C" + "\tCPU中核5应用率:" +(Number(this.Cpu_stat_cpu.split(" ")[6])).toFixed(3) +"\nCPU中核4温度:"+(Number(this.CPU_CORE_mid4_temp)/1000).toFixed(3) +"°C" + "\tCPU中核4应用率:" +(Number(this.Cpu_stat_cpu.split(" ")[5])).toFixed(3) +"\nCPU小核3温度:"+(Number(this.CPU_CORE_lit3_temp)/1000).toFixed(3) +"°C" + "\tCPU小核3应用率:" +(Number(this.Cpu_stat_cpu.split(" ")[4])).toFixed(3) +"\nCPU小核2温度:"+(Number(this.CPU_CORE_lit2_temp)/1000).toFixed(3) +"°C" + "\tCPU小核2应用率:" +(Number(this.Cpu_stat_cpu.split(" ")[3])).toFixed(3) +"\nCPU小核1温度:"+(Number(this.CPU_CORE_lit1_temp)/1000).toFixed(3) +"°C" + "\tCPU小核1应用率:" +(Number(this.Cpu_stat_cpu.split(" ")[2])).toFixed(3) +"\nCPU小核0温度:"+(Number(this.CPU_CORE_lit0_temp)/1000).toFixed(3) +"°C" + "\tCPU小核0应用率:" +(Number(this.Cpu_stat_cpu.split(" ")[1])).toFixed(3))

(5)失掉高低文切换的总数

TestStatisticsInfo.cpp// 失掉proc/stat文件的ctxt: 高低文切换的总数napi_value TestStatisticsInfo::Getctxt(napi_env env, napi_callback_info info) {if ((nullptr == env) || (nullptr == info)) {LOGE("TestStatisticsInfo::Getctxt: env or info is null");return nullptr;}napi_value thisArg;if (napi_ok != napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr)) {LOGE("TestStatisticsInfo::Getctxt: napi_get_cb_info fail");return nullptr;}std::string cached = getCpu_stat("ctxt");LOGI("getCpu_stat success! Cached is %{public}s", cached.c_str());napi_value res;napi_create_string_utf8(env, cached.c_str(), strlen(cached.c_str()), &res);return res;}

(6)启动的进程数

TestStatisticsInfo.cpp//失掉proc/stat文件的processes: 启动的进程数napi_value TestStatisticsInfo::Getprocesses(napi_env env, napi_callback_info info) {if ((nullptr == env) || (nullptr == info)) {LOGE("TestStatisticsInfo::Getprocesses: env or info is null");return nullptr;}napi_value thisArg;if (napi_ok != napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr)) {LOGE("TestStatisticsInfo::Getprocesses: napi_get_cb_info fail");return nullptr;}std::string cached = getCpu_stat("processes");LOGI("getCpu_stat success! Cached is %{public}s", cached.c_str());napi_value res;napi_create_string_utf8(env, cached.c_str(), strlen(cached.c_str()), &res);return res;}

想了解更多对于开源的内容,请访问:

鸿蒙开发者社区

您可能还会对下面的文章感兴趣: