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