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 
16 #include "cpu_decorator.h"
17 
18 namespace OHOS {
19 namespace HiviewDFX {
20 namespace UCollectUtil {
21 const std::string CPU_COLLECTOR_NAME = "CpuCollector";
22 const std::string THREAD_CPU_COLLECTOR_NAME = "ThreadCpuCollector";
23 StatInfoWrapper CpuDecorator::statInfoWrapper_;
24 
CollectSysCpuLoad()25 CollectResult<SysCpuLoad> CpuDecorator::CollectSysCpuLoad()
26 {
27     CollectResult<SysCpuLoad> result;
28     if (cpuCollector_ == nullptr) {
29         return result;
30     }
31     auto task = [this] { return cpuCollector_->CollectSysCpuLoad(); };
32     return Invoke(task, statInfoWrapper_, CPU_COLLECTOR_NAME + UC_SEPARATOR + __func__);
33 }
34 
CollectSysCpuUsage(bool isNeedUpdate)35 CollectResult<SysCpuUsage> CpuDecorator::CollectSysCpuUsage(bool isNeedUpdate)
36 {
37     CollectResult<SysCpuUsage> result;
38     if (cpuCollector_ == nullptr) {
39         return result;
40     }
41     auto task = [this, &isNeedUpdate] { return cpuCollector_->CollectSysCpuUsage(isNeedUpdate); };
42     return Invoke(task, statInfoWrapper_, CPU_COLLECTOR_NAME + UC_SEPARATOR + __func__);
43 }
44 
GetSysCpuUsage()45 CollectResult<double> CpuDecorator::GetSysCpuUsage()
46 {
47     CollectResult<double> result;
48     if (cpuCollector_ == nullptr) {
49         return result;
50     }
51     auto task = [this] { return cpuCollector_->GetSysCpuUsage(); };
52     return Invoke(task, statInfoWrapper_, CPU_COLLECTOR_NAME + UC_SEPARATOR + __func__);
53 }
54 
CollectProcessCpuStatInfo(int32_t pid,bool isNeedUpdate)55 CollectResult<ProcessCpuStatInfo> CpuDecorator::CollectProcessCpuStatInfo(int32_t pid, bool isNeedUpdate)
56 {
57     CollectResult<ProcessCpuStatInfo> result;
58     if (cpuCollector_ == nullptr) {
59         return result;
60     }
61     auto task = [this, &pid, &isNeedUpdate] { return cpuCollector_->CollectProcessCpuStatInfo(pid, isNeedUpdate); };
62     return Invoke(task, statInfoWrapper_, CPU_COLLECTOR_NAME + UC_SEPARATOR + __func__);
63 }
64 
CollectCpuFrequency()65 CollectResult<std::vector<CpuFreq>> CpuDecorator::CollectCpuFrequency()
66 {
67     CollectResult<std::vector<CpuFreq>> result;
68     if (cpuCollector_ == nullptr) {
69         return result;
70     }
71     auto task = [this] { return cpuCollector_->CollectCpuFrequency(); };
72     return Invoke(task, statInfoWrapper_, CPU_COLLECTOR_NAME + UC_SEPARATOR + __func__);
73 }
74 
CollectProcessCpuStatInfos(bool isNeedUpdate)75 CollectResult<std::vector<ProcessCpuStatInfo>> CpuDecorator::CollectProcessCpuStatInfos(bool isNeedUpdate)
76 {
77     CollectResult<std::vector<ProcessCpuStatInfo>> result;
78     if (cpuCollector_ == nullptr) {
79         return result;
80     }
81     auto task = [this, &isNeedUpdate] { return cpuCollector_->CollectProcessCpuStatInfos(isNeedUpdate); };
82     return Invoke(task, statInfoWrapper_, CPU_COLLECTOR_NAME + UC_SEPARATOR + __func__);
83 }
84 
CreateThreadCollector(int pid)85 std::shared_ptr<ThreadCpuCollector> CpuDecorator::CreateThreadCollector(int pid)
86 {
87     return cpuCollector_->CreateThreadCollector(pid);
88 }
89 
CollectThreadStatInfos(bool isNeedUpdate)90 CollectResult<std::vector<ThreadCpuStatInfo>> CpuDecorator::CollectThreadStatInfos(bool isNeedUpdate)
91 {
92     CollectResult<std::vector<ThreadCpuStatInfo>> result;
93     if (threadCpuCollector_ == nullptr) {
94         return result;
95     }
96     auto task = [this, &isNeedUpdate] { return threadCpuCollector_->CollectThreadStatInfos(isNeedUpdate); };
97     return Invoke(task, statInfoWrapper_, THREAD_CPU_COLLECTOR_NAME + UC_SEPARATOR + __func__);
98 }
99 
GetCollectPid()100 int CpuDecorator::GetCollectPid()
101 {
102     if (threadCpuCollector_ == nullptr) {
103         return -1;
104     }
105     return threadCpuCollector_->GetCollectPid();
106 }
107 
108 
SaveStatCommonInfo()109 void CpuDecorator::SaveStatCommonInfo()
110 {
111     std::map<std::string, StatInfo> statInfo = statInfoWrapper_.GetStatInfo();
112     std::vector<std::string> formattedStatInfo;
113     for (const auto& record : statInfo) {
114         formattedStatInfo.push_back(record.second.ToString());
115     }
116     WriteLinesToFile(formattedStatInfo, false);
117 }
118 
ResetStatInfo()119 void CpuDecorator::ResetStatInfo()
120 {
121     statInfoWrapper_.ResetStatInfo();
122 }
123 } // namespace UCollectUtil
124 } // namespace HiviewDFX
125 } // namespace OHOS
126