1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "include/CPU.h"
17 #include <sstream>
18 #include <cstdio>
19 #include <unistd.h>
20 #include <cstring>
21 #include <string>
22 #include <iostream>
23 #include <climits>
24 #include "securec.h"
25 #include "include/sp_utils.h"
26 #include "cpu_collector.h"
27 #include "collect_result.h"
28 #include "include/startup_delay.h"
29 #include "include/sp_log.h"
30
31 using namespace OHOS::HiviewDFX;
32 using namespace OHOS::HiviewDFX::UCollectUtil;
33 using namespace OHOS::HiviewDFX::UCollect;
34
35 namespace OHOS {
36 namespace SmartPerf {
ItemData()37 std::map<std::string, std::string> CPU::ItemData()
38 {
39 std::map<std::string, std::string> result;
40 std::vector<CpuFreqs> cpuFreqInfo = GetCpuFreq();
41 for (size_t i = 0; i < cpuFreqInfo.size(); i++) {
42 std::string cpuFreqStr = std::to_string(cpuFreqInfo[i].curFreq);
43 std::string cpuId = std::to_string(cpuFreqInfo[i].cpuId);
44 result["cpu" + cpuId + "Frequency"] = cpuFreqStr;
45 }
46 std::vector<CpuUsageInfos> workLoads = GetCpuUsage();
47 const size_t oneHundred = 100;
48 if (workLoads.empty()) {
49 return result;
50 }
51 for (size_t i = 1; i < workLoads.size(); i++) {
52 std::string cpuIdStr = workLoads[i].cpuId;
53 std::string userUsageStr = std::to_string(workLoads[i].userUsage * oneHundred);
54 std::string niceUsageStr = std::to_string(workLoads[i].niceUsage * oneHundred);
55 std::string systemUsageStr = std::to_string(workLoads[i].systemUsage * oneHundred);
56 std::string idleUsageStr = std::to_string(workLoads[i].idleUsage * oneHundred);
57 std::string ioWaitUsageStr = std::to_string(workLoads[i].ioWaitUsage * oneHundred);
58 std::string irqUsageStr = std::to_string(workLoads[i].irqUsage * oneHundred);
59 std::string softIrqUsageStr = std::to_string(workLoads[i].softIrqUsage * oneHundred);
60 std::string totalUsageStr = std::to_string((workLoads[i].userUsage + workLoads[i].niceUsage +
61 workLoads[i].systemUsage + workLoads[i].ioWaitUsage +
62 workLoads[i].irqUsage + workLoads[i].softIrqUsage) * oneHundred);
63 result[cpuIdStr + "userUsage"] = userUsageStr;
64 result[cpuIdStr + "niceUsage"] = niceUsageStr;
65 result[cpuIdStr + "systemUsage"] = systemUsageStr;
66 result[cpuIdStr + "idleUsage"] = idleUsageStr;
67 result[cpuIdStr + "ioWaitUsage"] = ioWaitUsageStr;
68 result[cpuIdStr + "irqUsage"] = irqUsageStr;
69 result[cpuIdStr + "softIrqUsage"] = softIrqUsageStr;
70 result[cpuIdStr + "Usage"] = totalUsageStr;
71 }
72 std::map<std::string, std::string> processCpuInfo = CPU::GetSysProcessCpuLoad();
73 if (!processCpuInfo.empty()) {
74 for (auto it = processCpuInfo.begin(); it != processCpuInfo.end(); ++it) {
75 result.insert(*it);
76 }
77 }
78 return result;
79 }
80
SetPackageName(std::string pName)81 void CPU::SetPackageName(std::string pName)
82 {
83 packageName = pName;
84 }
85
GetCpuFreq()86 std::vector<CpuFreqs> CPU::GetCpuFreq()
87 {
88 OHOS::SmartPerf::CpuFreqs cpuFreqs;
89 std::vector<CpuFreqs> cpuFreqs2;
90 std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
91 CollectResult<std::vector<CpuFreq>> result = collector->CollectCpuFrequency();
92 std::vector<CpuFreq>& cpufreq = result.data;
93 for (size_t i = 0; i < cpufreq.size(); i++) {
94 cpuFreqs.cpuId = cpufreq[i].cpuId;
95 cpuFreqs.curFreq = cpufreq[i].curFreq;
96 cpuFreqs2.push_back(cpuFreqs);
97 LOGI("cpuFreqs.cpuId: %s", std::to_string(cpufreq[i].cpuId).c_str());
98 LOGI("cpuFreqs.curFreq: %s", std::to_string(cpufreq[i].curFreq).c_str());
99 }
100 return cpuFreqs2;
101 }
102
GetCpuUsage()103 std::vector<CpuUsageInfos> CPU::GetCpuUsage()
104 {
105 OHOS::SmartPerf::CpuUsageInfos cpuUsageInfos;
106 std::vector<CpuUsageInfos> workload;
107 std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
108 CollectResult<SysCpuUsage> result = collector->CollectSysCpuUsage(true);
109 SysCpuUsage& sysCpuUsage = result.data;
110 if (sysCpuUsage.cpuInfos.empty()) {
111 return workload;
112 }
113 for (auto& cpuInfo : sysCpuUsage.cpuInfos) {
114 cpuUsageInfos.cpuId = cpuInfo.cpuId;
115 cpuUsageInfos.userUsage = cpuInfo.userUsage;
116 cpuUsageInfos.niceUsage = cpuInfo.niceUsage;
117 cpuUsageInfos.systemUsage = cpuInfo.systemUsage;
118 cpuUsageInfos.idleUsage = cpuInfo.idleUsage;
119 cpuUsageInfos.ioWaitUsage = cpuInfo.ioWaitUsage;
120 cpuUsageInfos.irqUsage = cpuInfo.irqUsage;
121 cpuUsageInfos.softIrqUsage = cpuInfo.softIrqUsage;
122 workload.push_back(cpuUsageInfos);
123 LOGI("UsageCpuId: %s", cpuInfo.cpuId.c_str());
124 LOGI("userUsage: %s", std::to_string(cpuInfo.userUsage).c_str());
125 LOGI("niceUsage: %s", std::to_string(cpuInfo.niceUsage).c_str());
126 LOGI("systemUsage: %s", std::to_string(cpuInfo.systemUsage).c_str());
127 LOGI("idleUsage: %s", std::to_string(cpuInfo.idleUsage).c_str());
128 LOGI("ioWaitUsage: %s", std::to_string(cpuInfo.ioWaitUsage).c_str());
129 LOGI("irqUsage: %s", std::to_string(cpuInfo.irqUsage).c_str());
130 LOGI("softIrqUsage: %s", std::to_string(cpuInfo.softIrqUsage).c_str());
131 }
132 return workload;
133 }
134
GetSysProcessCpuLoad() const135 std::map<std::string, std::string> CPU::GetSysProcessCpuLoad() const
136 {
137 std::string processId = "";
138 OHOS::SmartPerf::StartUpDelay sp;
139 processId = sp.GetPidByPkg(packageName);
140 std::map<std::string, std::string> processCpuInfo;
141 const size_t oneHundred = 100;
142 if (processId.length() > 0) {
143 int32_t procId = 0;
144 procId = std::stoi(processId);
145 std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
146 auto collectResult = collector->CollectProcessCpuStatInfo(procId, true);
147 auto data = collectResult.data;
148 processCpuInfo["ProcId"] = std::to_string(data.pid);
149 processCpuInfo["ProcAppName"] = data.procName;
150 processCpuInfo["ProcCpuLoad"] = std::to_string(data.cpuLoad * oneHundred);
151 processCpuInfo["ProcCpuUsage"] = std::to_string(data.cpuUsage * oneHundred);
152 processCpuInfo["ProcUCpuUsage"] = std::to_string(data.uCpuUsage * oneHundred);
153 processCpuInfo["ProcSCpuUsage"] = std::to_string(data.sCpuUsage * oneHundred);
154 LOGI("ProcId: %s", std::to_string(data.pid).c_str());
155 LOGI("ProcAppName: %s", data.procName.c_str());
156 LOGI("ProcCpuLoad: %s", std::to_string(data.cpuLoad).c_str());
157 LOGI("ProcCpuUsage: %s", std::to_string(data.cpuUsage).c_str());
158 LOGI("ProcUCpuUsage: %s", std::to_string(data.uCpuUsage).c_str());
159 LOGI("ProcSCpuUsage: %s", std::to_string(data.sCpuUsage).c_str());
160 return processCpuInfo;
161 }
162 return processCpuInfo;
163 }
164 }
165 }
166