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