偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

開(kāi)源!OpenHarmony手機(jī)CPU信息應(yīng)用

系統(tǒng) OpenHarmony
筆者最近寫了一個(gè)OpenHarmony開(kāi)發(fā)者手機(jī)應(yīng)用開(kāi)獲取手機(jī)的各種信息,通過(guò)Native C++ 開(kāi)發(fā)方式讀取開(kāi)發(fā)板端文件獲取手機(jī)各項(xiàng)信息。

想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO 鴻蒙開(kāi)發(fā)者社區(qū)

https://ost.51cto.com

筆者最近寫了一個(gè)OpenHarmony開(kāi)發(fā)者手機(jī)應(yīng)用開(kāi)獲取手機(jī)的各種信息,源碼開(kāi)源如下:CPU_device_information

  • 開(kāi)發(fā)環(huán)境
  1. API10
  2. 硬件:OpenHarmony開(kāi)發(fā)者手機(jī)
  3. OpenHarmony 4.O.10.309

OpenHarmony 開(kāi)發(fā)者手機(jī)購(gòu)買鏈接:https://xslht.com/dp

1.應(yīng)用安裝步驟

2.實(shí)現(xiàn)功能

完成了開(kāi)發(fā)者手機(jī)以下信息的獲取。

- CPU核心數(shù)
- SOC型號(hào)
- GPU溫度
- 主板溫度
- 系統(tǒng)運(yùn)行時(shí)間
- RAM總內(nèi)存
- RAM可用內(nèi)存
- RAM空閑內(nèi)存
- 緩存使用內(nèi)存
- Swaps交換分區(qū)
- 系統(tǒng)啟動(dòng)以來(lái)創(chuàng)建的進(jìn)程數(shù)
- 上下文切換的總數(shù)
- SOC溫度
- CPU利用率
- CPU大核7溫度和利用率
- CPU中核6溫度和利用率
- CPU中核5溫度和利用率
- CPU中核4溫度和利用率
- CPU小核3溫度和利用率
- CPU小核2溫度和利用率
- CPU小核1溫度和利用率
- CPU小核0溫度和利用率
- 設(shè)備電量
- 電池電壓
- 電池型號(hào)
- 電池充電狀態(tài)
- 系統(tǒng)版本
- RTC時(shí)間和日期
- 內(nèi)核版本信息
- 電池信息

3.功能實(shí)現(xiàn)邏輯

(1)通過(guò)Native C++ 開(kāi)發(fā)方式讀取開(kāi)發(fā)板端文件獲取手機(jī)各項(xiàng)信息

# 獲取SOC型號(hào)
proc/device-tree/cpuinfo_hardware 

# 獲取rtc時(shí)間
/sys/class/rtc/rtc0/time

# 獲取內(nèi)核信息
/proc/version

# 獲取RTC系統(tǒng)日期
/sys/class/rtc/rtc0/date

# 交換分區(qū)大小
/proc/swaps

# 獲取主板熱區(qū)
/sys/class/thermal/thermal_zone27/temp

# 獲取GPU熱區(qū)
/sys/class/thermal/thermal_zone17/temp

# 獲取lit0-thmzone 小核心 0 熱區(qū)
/sys/class/thermal/thermal_zone13/temp

# 獲取lit1-thmzone 小核心 1 熱區(qū)
/sys/class/thermal/thermal_zone14/temp

# 獲取lit2-thmzone 小核心 2 熱區(qū)
/sys/class/thermal/thermal_zone15/temp

# 獲取lit3-thmzone 小核心 3 熱區(qū)
/sys/class/thermal/thermal_zone16/temp

# 獲取mid4-thmzone 中核心 4 熱區(qū)
/sys/class/thermal/thermal_zone9/temp

# 獲取mid5-thmzone 中核心 5 熱區(qū)
/sys/class/thermal/thermal_zone10/temp

# 獲取mid6-thmzone 中核心 6 熱區(qū)
/sys/class/thermal/thermal_zone11/temp

# 獲取big7-thmzone 大核心 7 熱區(qū)
/sys/class/thermal/thermal_zone7/temp

# 獲取soc-thmzone系統(tǒng)芯片熱區(qū) 
/sys/class/thermal/thermal_zone5/temp

# /proc/uptime 是一個(gè)特殊的文件,它提供了當(dāng)前系統(tǒng)的運(yùn)行時(shí)間信息。文件中包含了兩個(gè)數(shù)值,分別表示系統(tǒng)的總運(yùn)行時(shí)間和空閑時(shí)間。
/proc/uptime

# 獲取內(nèi)存信息
/proc/meminfo

# 獲取cpu info
/proc/cpuinfo

# 計(jì)算cpu利用率,進(jìn)程計(jì)數(shù)器,正在運(yùn)行的進(jìn)程計(jì)數(shù)器,阻塞的進(jìn)程計(jì)數(shù)器,系統(tǒng)發(fā)生的上下文切換次數(shù)
/proc/stat

(2)Native C++開(kāi)發(fā)的api

export const getCpuCount: () => Number;                  //獲取cpu核心數(shù)
export const getMemTotal: () => String;                  //獲取RAM總內(nèi)存大小
export const getFreeMem: () => String;                   //獲取空閑內(nèi)存大小
export const getCachedMem: () => String;                 //獲取緩存使用內(nèi)存大小
export const getAvailableMem: () => String;              //獲取可用內(nèi)存大小
export const getCpuInfo: () => any;                      //獲取CPU信息
export const getMemoryInfo: () => any;                   //獲取RAM信息
export const getUptime: () => String;                    //讀取/proc/uptime,/proc/uptime 是一個(gè)特殊的文件,它提供了當(dāng)前系統(tǒng)的運(yùn)行時(shí)間信息。文件中包含了兩個(gè)數(shù)值,分別表示系統(tǒng)的總運(yùn)行時(shí)間和空閑時(shí)間。
export const getSOCtemp: () => String;                   //獲取soc-thmzone系統(tǒng)芯片熱區(qū) /sys/class/thermal/thermal_zone5/temp
export const getCPU_CORE_big7_thmzonetemp: () => String; //獲取big7-thmzone 大核心 7 熱區(qū)
export const getCPU_CORE_mid6_thmzonetemp: () => String; //獲取mid6-thmzone 中核心 6 熱區(qū)
export const getCPU_CORE_mid5_thmzonetemp: () => String; //獲取mid6-thmzone 中核心 5 熱區(qū)
export const getCPU_CORE_mid4_thmzonetemp: () => String; //獲取mid6-thmzone 中核心 4 熱區(qū)
export const getCPU_CORE_lit3_thmzonetemp: () => String; //獲取lit3-thmzone 小核心 3 熱區(qū)
export const getCPU_CORE_lit2_thmzonetemp: () => String; //獲取lit2-thmzone 小核心 2 熱區(qū)
export const getCPU_CORE_lit1_thmzonetemp: () => String; //獲取lit1-thmzone 小核心 1 熱區(qū)
export const getCPU_CORE_lit0_thmzonetemp: () => String; //獲取lit0-thmzone 小核心 0 熱區(qū)
export const getGPU_temp: () => String;                  //獲取GPU 熱區(qū)
export const getBoard_temp: () => String;                //獲取主板 熱區(qū)
export const getSwaps: () => String;                     //獲取交換分區(qū)大小
export const getRTC_Date_temp: () => String;             //獲取rtc日期
export const getKernel_version: () => String;            //獲取內(nèi)核信息
export const getRTC_Time_temp: () => String;             //獲取rtc時(shí)間
export const getCpu_stat_cpu: () => String;              //獲取cpu以及各個(gè)核利用率
export const getprocesses: () => String;                 //獲取正在運(yùn)行的進(jìn)程數(shù)
export const getctxt: () => String;                      //獲取正在運(yùn)行的進(jìn)程數(shù)
export const getcpuinfo_hardware: () => String;          //獲取SOC型號(hào)

4.功能實(shí)現(xiàn)邏輯剖析

(1)底部導(dǎo)航欄、頂部狀態(tài)欄設(shè)置

參考:沉浸式界面開(kāi)發(fā)

Index.ets

import window from '@ohos.window';
import common from '@ohos.app.ability.common';

  //沉浸式界面開(kāi)發(fā):https://gitee.com/openharmony/docs/blob/master/zh-cn/third-party-cases/immersion-mode.md#%E5%8F%82%E8%80%83
  context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
  async setSystemBar() {

    let windowClass = await window.getLastWindow(this.context)
    //設(shè)置導(dǎo)航欄,狀態(tài)欄不可見(jiàn)
    /*
     * let names: Array<'status' | 'navigation'> = ['navigation'];//設(shè)置頂部狀態(tài)欄不可見(jiàn)
     * let names: Array<'status' | 'navigation'> = ['status'];//設(shè)置底部導(dǎo)航欄不可見(jiàn)
     * let names: Array<'status' | 'navigation'> = [];//設(shè)置
    */
    let names: Array<'status' | 'navigation'> = ["navigation"];
    await windowClass.setWindowSystemBarEnable(names)
  }

aboutToAppear() {

this.setSystemBar()

}

(2)獲取SOC型號(hào)

讀取開(kāi)發(fā)板proc/device-tree/cpuinfo_hardware文件獲取SOC型號(hào)。

TestStatisticsInfo.cpp

// proc/device-tree/cpuinfo_hardware
// 查看SOC型號(hào)
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轉(zhuǎn)換為字符串類型并賦值給time。
    LOGE("TestStatisticsInfo::getcpuinfo_hardware %{public}d =======", buffer0);
    fclose(fp0);

    return temp0;
}
index.d.ts

export const getcpuinfo_hardware: () => String;          //獲取SOC型號(hào)
@State cpuinfo_hardware: String = '';

  //aboutToAppear函數(shù)在創(chuàng)建自定義組件的新實(shí)例后,在執(zhí)行其build()函數(shù)之前執(zhí)行。允許在aboutToAppear函數(shù)中改變狀態(tài)變量,更改將在后續(xù)執(zhí)行build()函數(shù)中生效。
  aboutToAppear() {

    //getcpuinfo_hardware
    this.cpuinfo_hardware = testStatisticsApi.getcpuinfo_hardware();
    console.log("========Cpu_stat_cpu is ",this.cpuinfo_hardware)

}

(3)獲取cpu以及各個(gè)核利用率、正在運(yùn)行的進(jìn)程數(shù)、上下文切換的總數(shù)

讀取開(kāi)發(fā)板proc/stat目錄獲取。

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

以上參數(shù)含義解釋如下:

  • cpu: 包含了 CPU 的使用情況統(tǒng)計(jì)信息,依次表示用戶態(tài)時(shí)間、Nice 值為負(fù)的進(jìn)程在用戶態(tài)的CPU時(shí)間、系統(tǒng)態(tài)時(shí)間、空閑時(shí)間、等待IO的時(shí)間、硬中斷時(shí)間、軟中斷時(shí)間和虛擬化的CPU操作時(shí)間。
  • cpu0 - cpu7: 每個(gè)核心的 CPU 使用情況統(tǒng)計(jì)信息,格式與全局的 cpu 行相同。
  • intr: 包含了中斷請(qǐng)求的統(tǒng)計(jì)信息,依次表示總的中斷數(shù)以及每種中斷的具體計(jì)數(shù)。
  • ctxt: 上下文切換的次數(shù),包括進(jìn)程切換和內(nèi)核線程切換。
  • btime: 系統(tǒng)啟動(dòng)時(shí)間
  • processes: 啟動(dòng)以來(lái)創(chuàng)建的進(jìn)程數(shù)。
  • procs_running: 當(dāng)前正在執(zhí)行的進(jìn)程數(shù)量。
  • procs_blocked: 當(dāng)前被阻塞的進(jìn)程數(shù)量。
  • softirq: 軟中斷的統(tǒng)計(jì)信息,依次表示軟中斷的次數(shù)和每種軟中斷的具體計(jì)數(shù)。

注意:需要修改開(kāi)發(fā)板/vendor/etc/init.uis7885.cfg文件將proc/stat的權(quán)限改為777。

TestStatisticsInfo.cpp

std::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: 進(jìn)程計(jì)數(shù)器統(tǒng)計(jì)。這個(gè)字段表示當(dāng)前運(yùn)行的進(jìn)程數(shù)量。
    std::string procs_runningField = "procs_running"; //procs_running: 正在運(yùn)行的進(jìn)程計(jì)數(shù)器統(tǒng)計(jì)。這個(gè)字段表示當(dāng)前正在運(yùn)行的進(jìn)程數(shù)量。
    std::string procs_blockedField = "procs_blocked";//procs_blocked: 阻塞的進(jìn)程計(jì)數(shù)器統(tǒng)計(jì)。這個(gè)字段表示當(dāng)前被阻塞的進(jìn)程數(shù)量。
    std::string ctxtField = "ctxt";//上下文切換計(jì)數(shù)器統(tǒng)計(jì)。這個(gè)字段表示系統(tǒng)發(fā)生的上下文切換次數(shù),可以用于評(píng)估系統(tǒng)的調(diào)度性能。
    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以及各個(gè)核利用率

根據(jù)/proc/stat 文件內(nèi)容可以計(jì)算 Linux CPU 利用率。

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

這是一個(gè) CPU(中央處理器)使用情況的統(tǒng)計(jì)信息。每一行都表示一個(gè) CPU 核心的使用情況。
下面是對(duì)每一列的解釋:

cpu: 總體統(tǒng)計(jì)信息
cpu0、cpu1、cpu2、等等:各個(gè) CPU 核心的統(tǒng)計(jì)信息
user: 用戶模式下運(yùn)行時(shí)間
nice: 優(yōu)先級(jí)較低的用戶模式下運(yùn)行時(shí)間
system: 內(nèi)核模式下運(yùn)行時(shí)間
idle: 空閑時(shí)間
iowait: 等待輸入/輸出完成的時(shí)間
irq: 處理硬件中斷的時(shí)間
softirq: 處理軟件中斷的時(shí)間
steal: 被虛擬化主機(jī)偷取的時(shí)間
guest: 運(yùn)行虛擬 CPU 的時(shí)間
guest_nice: 運(yùn)行虛擬 CPU 且優(yōu)先級(jí)較低的時(shí)間

在 /proc/stat 文件中,CPU 利用率的時(shí)間單位是“時(shí)鐘滴答”(clock ticks)。每個(gè) Linux 系統(tǒng)都有一個(gè)時(shí)鐘線程(clock tick),它以固定的速率生成時(shí)鐘滴答來(lái)驅(qū)動(dòng)系統(tǒng)的計(jì)時(shí)器。時(shí)鐘滴答的大小依賴于系統(tǒng)的硬件和配置。它通常以毫秒(ms)為單位,但也可能以微秒(μs)或納秒(ns)為單位,具體取決于系統(tǒng)。要獲取實(shí)際的時(shí)間單位,你可以查看 /proc/timer_list 或 /proc/timer_stats 文件中的信息。需要注意的是,這些時(shí)鐘滴答并不是以獨(dú)立的單位存在的,它們僅用于相對(duì)測(cè)量和計(jì)算 CPU 的利用率。因此,在分析 CPU 利用率時(shí),我們通常關(guān)注的是兩個(gè)時(shí)間點(diǎn)之間的差異,而不是實(shí)際的時(shí)鐘滴答值本身。
  • 計(jì)算方式:

cpu總時(shí)間 = user + nice + system + idle + iowait + irq + softirq + stealstolen + guest + guest_nice
在一段時(shí)間內(nèi)獲取兩次cpu時(shí)間分配信息。
兩次的cpu總時(shí)間:total_2 - total_1
兩次的cpu剩余時(shí)間:idle_2 - idle_1
兩次的cpu使用時(shí)間:used = (total_2 - total_1) - (idle_2 - idle_1)
cpu使用率 = 使用時(shí)間 / 總時(shí)間 * 100% = used / total * 100%

TestStatisticsInfo.cpp

//計(jì)算cpu利用率
std::string TestStatisticsInfo::calculateCpuUtilization(std::string& a,std::string& a_second) {
    std::istringstream iss_a(a);
    std::istringstream iss_a_second(a_second);

    // 提取每個(gè)字段的值
    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;

    // 計(jì)算總的 CPU 時(shí)間和空閑 CPU 時(shí)間
    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;

    // 計(jì)算 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);
}

// 計(jì)算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)); // 延時(shí) 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.ts

export const getCpu_stat_cpu: () => String;              //獲取cpu以及各個(gè)核利用率
Index.ts

import testStatisticsApi from 'libentry.so';

  @State Cpu_stat_cpu: String = '';

      //getCpu_stat_cpu
      this.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\t  CPU利用率:" + (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)獲取上下文切換的總數(shù)

TestStatisticsInfo.cpp

// 獲取proc/stat文件的ctxt: 上下文切換的總數(shù)
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)啟動(dòng)的進(jìn)程數(shù)

TestStatisticsInfo.cpp

//獲取proc/stat文件的processes: 啟動(dòng)的進(jìn)程數(shù)
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;
}

想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO 鴻蒙開(kāi)發(fā)者社區(qū)

https://ost.51cto.com

責(zé)任編輯:jianghua 來(lái)源: 51CTO 鴻蒙開(kāi)發(fā)者社區(qū)
相關(guān)推薦

2022-03-02 16:08:31

Harmony應(yīng)用開(kāi)發(fā)鴻蒙

2022-04-18 10:37:01

鴻蒙操作系統(tǒng)開(kāi)發(fā)工具

2022-05-18 09:38:45

開(kāi)源手機(jī)Android應(yīng)用智能手機(jī)

2021-06-06 16:05:31

OpenHarmony

2010-02-04 15:19:38

C++獲取CPU信息

2023-04-17 16:14:55

靜態(tài)訂閱鴻蒙

2023-03-01 14:55:09

2022-09-21 14:58:11

OH應(yīng)用簽名鴻蒙

2022-04-29 14:56:40

通話應(yīng)用源碼剖析

2022-11-04 14:58:59

應(yīng)用開(kāi)發(fā)鴻蒙

2023-05-19 16:43:10

移植無(wú)人機(jī)鴻蒙

2021-10-13 07:18:40

鴻蒙HarmonyOS應(yīng)用

2023-05-23 14:53:26

鴻蒙應(yīng)用開(kāi)發(fā)

2022-10-08 16:19:40

智能喂食器鴻蒙

2022-06-20 15:27:00

socket對(duì)話鴻蒙

2023-08-17 15:04:22

2022-10-08 16:26:23

APP應(yīng)用開(kāi)發(fā)

2022-03-14 15:11:01

harmony鴻蒙操作系統(tǒng)

2022-11-02 15:49:45

應(yīng)用開(kāi)發(fā)鴻蒙
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)