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 usleep(twenty * thousand);
40 std::map<std::string, std::string> result;
41 std::vector<CpuFreqs> cpuFreqInfo = GetCpuFreq();
42 for (size_t i = 0; i < cpuFreqInfo.size(); i++) {
43 std::string cpuFreqStr = std::to_string(cpuFreqInfo[i].curFreq);
44 std::string cpuId = std::to_string(cpuFreqInfo[i].cpuId);
45 result["cpu" + cpuId + "Frequency"] = cpuFreqStr;
46 }
47 std::vector<CpuUsageInfos> workLoads = GetCpuUsage();
48 const size_t oneHundred = 100;
49 if (workLoads.empty()) {
50 return result;
51 }
52 for (size_t i = 0; i < workLoads.size(); i++) {
53 std::string cpuIdStr = workLoads[i].cpuId;
54 std::string userUsageStr = std::to_string(workLoads[i].userUsage * oneHundred);
55 std::string niceUsageStr = std::to_string(workLoads[i].niceUsage * oneHundred);
56 std::string systemUsageStr = std::to_string(workLoads[i].systemUsage * oneHundred);
57 std::string idleUsageStr = std::to_string(workLoads[i].idleUsage * oneHundred);
58 std::string ioWaitUsageStr = std::to_string(workLoads[i].ioWaitUsage * oneHundred);
59 std::string irqUsageStr = std::to_string(workLoads[i].irqUsage * oneHundred);
60 std::string softIrqUsageStr = std::to_string(workLoads[i].softIrqUsage * oneHundred);
61 std::string totalUsageStr = std::to_string((workLoads[i].userUsage + workLoads[i].niceUsage +
62 workLoads[i].systemUsage + workLoads[i].ioWaitUsage + workLoads[i].irqUsage + workLoads[i].softIrqUsage) *
63 oneHundred);
64 if (cpuIdStr == cpustr) {
65 cpuIdStr = totalcpu;
66 }
67 result[cpuIdStr + "userUsage"] = userUsageStr;
68 result[cpuIdStr + "niceUsage"] = niceUsageStr;
69 result[cpuIdStr + "systemUsage"] = systemUsageStr;
70 result[cpuIdStr + "idleUsage"] = idleUsageStr;
71 result[cpuIdStr + "ioWaitUsage"] = ioWaitUsageStr;
72 result[cpuIdStr + "irqUsage"] = irqUsageStr;
73 result[cpuIdStr + "softIrqUsage"] = softIrqUsageStr;
74 result[cpuIdStr + "Usage"] = totalUsageStr;
75 }
76 if (!packageName.empty()) {
77 std::map<std::string, std::string> processCpuInfo = CPU::GetSysProcessCpuLoad();
78 if (!processCpuInfo.empty()) {
79 for (const auto& item : processCpuInfo) {
80 result.insert(item);
81 }
82 }
83 }
84
85 LOGD("CPU::ItemData map size(%u)", result.size());
86 return result;
87 }
88
SetPackageName(const std::string & pName)89 void CPU::SetPackageName(const std::string &pName)
90 {
91 packageName = pName;
92 LOGD("CPU SetPackageName name(%s)", pName.c_str());
93 }
94
SetProcessId(const std::string & pid)95 void CPU::SetProcessId(const std::string &pid)
96 {
97 processId = pid;
98 }
99
GetCpuFreq()100 std::vector<CpuFreqs> CPU::GetCpuFreq()
101 {
102 OHOS::SmartPerf::CpuFreqs cpuFreqs;
103 std::vector<CpuFreqs> cpuFrequency;
104 std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
105 CollectResult<std::vector<CpuFreq>> result = collector->CollectCpuFrequency();
106 std::vector<CpuFreq> &cpufreq = result.data;
107 for (size_t i = 0; i < cpufreq.size(); i++) {
108 cpuFreqs.cpuId = cpufreq[i].cpuId;
109 cpuFreqs.curFreq = cpufreq[i].curFreq;
110 cpuFrequency.push_back(cpuFreqs);
111 LOGD("cpuFreqs.cpuId: %s, cpuFreqs.curFreq: %s",
112 std::to_string(cpufreq[i].cpuId).c_str(), std::to_string(cpufreq[i].curFreq).c_str());
113 }
114 return cpuFrequency;
115 }
116
GetCpuUsage()117 std::vector<CpuUsageInfos> CPU::GetCpuUsage()
118 {
119 OHOS::SmartPerf::CpuUsageInfos cpuUsageInfos;
120 std::vector<CpuUsageInfos> workload;
121 std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
122 CollectResult<SysCpuUsage> result = collector->CollectSysCpuUsage(true);
123 SysCpuUsage &sysCpuUsage = result.data;
124 if (sysCpuUsage.cpuInfos.empty()) {
125 return workload;
126 }
127 for (auto &cpuInfo : sysCpuUsage.cpuInfos) {
128 cpuUsageInfos.cpuId = cpuInfo.cpuId;
129 cpuUsageInfos.userUsage = cpuInfo.userUsage;
130 cpuUsageInfos.niceUsage = cpuInfo.niceUsage;
131 cpuUsageInfos.systemUsage = cpuInfo.systemUsage;
132 cpuUsageInfos.idleUsage = cpuInfo.idleUsage;
133 cpuUsageInfos.ioWaitUsage = cpuInfo.ioWaitUsage;
134 cpuUsageInfos.irqUsage = cpuInfo.irqUsage;
135 cpuUsageInfos.softIrqUsage = cpuInfo.softIrqUsage;
136 workload.push_back(cpuUsageInfos);
137 LOGD("UsageCpuId: %s, userUsage: %s, niceUsage: %s, systemUsage: %s",
138 "idleUsage: %s, ioWaitUsage: %s, irqUsage: %s, softIrqUsage: %s",
139 cpuInfo.cpuId.c_str(), std::to_string(cpuInfo.userUsage).c_str(),
140 std::to_string(cpuInfo.niceUsage).c_str(), std::to_string(cpuInfo.systemUsage).c_str(),
141 std::to_string(cpuInfo.idleUsage).c_str(), std::to_string(cpuInfo.ioWaitUsage).c_str(),
142 std::to_string(cpuInfo.irqUsage).c_str(), std::to_string(cpuInfo.softIrqUsage).c_str());
143 }
144 return workload;
145 }
146
GetSysProcessCpuLoad() const147 std::map<std::string, std::string> CPU::GetSysProcessCpuLoad() const
148 {
149 std::map<std::string, std::string> processCpuInfo;
150 const size_t oneHundred = 100;
151 if (!processId.empty()) {
152 int32_t procId = 0;
153 procId = SPUtilesTye::StringToSometype<int32_t>(processId);
154 std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
155 auto collectResult = collector->CollectProcessCpuStatInfo(procId, true);
156 auto data = collectResult.data;
157 processCpuInfo["ProcId"] = std::to_string(data.pid);
158 processCpuInfo["ProcAppName"] = data.procName;
159 processCpuInfo["ProcCpuLoad"] = std::to_string(data.cpuLoad * oneHundred);
160 processCpuInfo["ProcCpuUsage"] = std::to_string(data.cpuUsage * oneHundred);
161 processCpuInfo["ProcUCpuUsage"] = std::to_string(data.uCpuUsage * oneHundred);
162 processCpuInfo["ProcSCpuUsage"] = std::to_string(data.sCpuUsage * oneHundred);
163 LOGD("ProcId: %s, ProcAppName: %s, ProcCpuLoad: %s, ProcCpuUsage: %s, ProcUCpuUsage: %s, ProcSCpuUsage: %s",
164 std::to_string(data.pid).c_str(), data.procName.c_str(), std::to_string(data.cpuLoad).c_str(),
165 std::to_string(data.cpuUsage).c_str(), std::to_string(data.uCpuUsage).c_str(),
166 std::to_string(data.sCpuUsage).c_str());
167 } else {
168 processCpuInfo["ProcId"] = "NA";
169 processCpuInfo["ProcAppName"] = packageName;
170 processCpuInfo["ProcCpuLoad"] = "NA";
171 processCpuInfo["ProcCpuUsage"] = "NA";
172 processCpuInfo["ProcUCpuUsage"] = "NA";
173 processCpuInfo["ProcSCpuUsage"] = "NA";
174 }
175 if (processCpuInfo.find("ProcAppName") != processCpuInfo.end() && processCpuInfo["ProcAppName"].empty()) {
176 processCpuInfo["ProcId"] = "0";
177 processCpuInfo["ProcAppName"] = packageName;
178 processCpuInfo["ProcCpuLoad"] = "0";
179 processCpuInfo["ProcCpuUsage"] = "0";
180 processCpuInfo["ProcUCpuUsage"] = "0";
181 processCpuInfo["ProcSCpuUsage"] = "0";
182 }
183 return processCpuInfo;
184 }
185 }
186 }
187