1 /*
2 * Copyright (C) 2023-2024 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 #include "cpu_collection_task.h"
16
17 #include <unistd.h>
18
19 #include "common_utils.h"
20 #include "hiview_logger.h"
21 #include "parameter_ex.h"
22 #include "trace_collector.h"
23
24 using namespace OHOS::HiviewDFX::UCollectUtil;
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_TAG("Hiview-CpuCollectionTask");
29 struct CpuThresholdItem {
30 std::string processName;
31 UCollect::TraceCaller caller;
32 double cpuLoadThreshold = 0.0;
33 bool hasOverThreshold = false;
34 };
35
CpuCollectionTask(const std::string & workPath)36 CpuCollectionTask::CpuCollectionTask(const std::string& workPath) : workPath_(workPath)
37 {
38 InitCpuCollector();
39 if (Parameter::IsBetaVersion()) {
40 InitCpuStorage();
41 }
42 #ifdef HAS_HIPERF
43 InitCpuPerfDump();
44 #endif
45 }
46
Collect()47 void CpuCollectionTask::Collect()
48 {
49 if (Parameter::IsBetaVersion()) {
50 ReportCpuCollectionEvent();
51 }
52 CollectCpuData();
53 }
54
InitCpuCollector()55 void CpuCollectionTask::InitCpuCollector()
56 {
57 cpuCollector_ = UCollectUtil::CpuCollector::Create();
58 threadCpuCollector_ = cpuCollector_->CreateThreadCollector(getprocpid());
59 }
60
InitCpuStorage()61 void CpuCollectionTask::InitCpuStorage()
62 {
63 cpuStorage_ = std::make_shared<CpuStorage>(workPath_);
64 }
65
66 #ifdef HAS_HIPERF
InitCpuPerfDump()67 void CpuCollectionTask::InitCpuPerfDump()
68 {
69 cpuPerfDump_ = std::make_shared<CpuPerfDump>();
70 }
71 #endif
72
ReportCpuCollectionEvent()73 void CpuCollectionTask::ReportCpuCollectionEvent()
74 {
75 cpuStorage_->Report();
76 }
77
CheckAndDumpTraceData()78 void CpuCollectionTask::CheckAndDumpTraceData()
79 {
80 static std::vector<CpuThresholdItem> checkItems = {
81 {"hiview", UCollect::TraceCaller::HIVIEW, 0.07, false}, // 0.07 : 7% cpu load
82 {"foundation", UCollect::TraceCaller::FOUNDATION, 0.2, false}, // 0.2 : 20% cpu load
83 };
84
85 for (auto &item : checkItems) {
86 int32_t pid = CommonUtils::GetPidByName(item.processName);
87 if (pid <= 0) {
88 HIVIEW_LOGW("get pid failed, process:%{public}s", item.processName.c_str());
89 continue;
90 }
91 auto processCpuStatInfo = cpuCollector_->CollectProcessCpuStatInfo(pid);
92 double cpuLoad = processCpuStatInfo.data.cpuLoad;
93 if (!item.hasOverThreshold && cpuLoad >= item.cpuLoadThreshold) {
94 HIVIEW_LOGI("over threshold, current cpu load:%{public}f", cpuLoad);
95 item.hasOverThreshold = true;
96 return;
97 }
98
99 // when cpu load restore to normal, start capture history trace
100 if (item.hasOverThreshold && cpuLoad < item.cpuLoadThreshold) {
101 HIVIEW_LOGI("capture history trace");
102 auto traceCollector = TraceCollector::Create();
103 traceCollector->DumpTrace(item.caller);
104 item.hasOverThreshold = false;
105 }
106 }
107 }
108
CollectCpuData()109 void CpuCollectionTask::CollectCpuData()
110 {
111 auto cpuCollectionsResult = cpuCollector_->CollectProcessCpuStatInfos(true);
112 if (cpuCollectionsResult.retCode == UCollect::UcError::SUCCESS) {
113 if (Parameter::IsBetaVersion()) {
114 cpuStorage_->StoreProcessDatas(cpuCollectionsResult.data);
115 }
116
117 #ifdef HAS_HIPERF
118 cpuPerfDump_->CheckAndDumpPerfData(cpuCollectionsResult.data);
119 #endif
120 }
121 if (threadCpuCollector_ != nullptr) {
122 auto threadCpuCollectResult = threadCpuCollector_ ->CollectThreadStatInfos(true);
123 if (Parameter::IsBetaVersion() && threadCpuCollectResult.retCode == UCollect::UcError::SUCCESS) {
124 cpuStorage_->StoreThreadDatas(threadCpuCollectResult.data);
125 }
126 }
127 // collect the system cpu usage periodically for hidumper
128 cpuCollector_->CollectSysCpuUsage(true);
129
130 if (Parameter::IsBetaVersion()) {
131 CheckAndDumpTraceData();
132 }
133 }
134 } // namespace HiviewDFX
135 } // namespace OHOS
136