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 #ifdef HAS_HIPERF
16 #include "perf_decorator.h"
17 
18 namespace OHOS {
19 namespace HiviewDFX {
20 namespace UCollectUtil {
21 const std::string PERF_COLLECTOR_NAME = "PerfCollector";
22 StatInfoWrapper PerfDecorator::statInfoWrapper_;
23 
StartPerf(const std::string & logDir)24 CollectResult<bool> PerfDecorator::StartPerf(const std::string &logDir)
25 {
26     auto task = [this, &logDir] { return perfCollector_->StartPerf(logDir); };
27     return Invoke(task, statInfoWrapper_, PERF_COLLECTOR_NAME + UC_SEPARATOR + __func__);
28 }
29 
SetSelectPids(const std::vector<pid_t> & selectPids)30 void PerfDecorator::SetSelectPids(const std::vector<pid_t> &selectPids)
31 {
32     perfCollector_->SetSelectPids(selectPids);
33 }
34 
SetTargetSystemWide(bool enable)35 void PerfDecorator::SetTargetSystemWide(bool enable)
36 {
37     perfCollector_->SetTargetSystemWide(enable);
38 }
39 
SetTimeStopSec(int timeStopSec)40 void PerfDecorator::SetTimeStopSec(int timeStopSec)
41 {
42     perfCollector_->SetTimeStopSec(timeStopSec);
43 }
44 
SetFrequency(int frequency)45 void PerfDecorator::SetFrequency(int frequency)
46 {
47     perfCollector_->SetFrequency(frequency);
48 }
49 
SetOffCPU(bool offCPU)50 void PerfDecorator::SetOffCPU(bool offCPU)
51 {
52     perfCollector_->SetOffCPU(offCPU);
53 }
54 
SetOutputFilename(const std::string & outputFilename)55 void PerfDecorator::SetOutputFilename(const std::string &outputFilename)
56 {
57     perfCollector_->SetOutputFilename(outputFilename);
58 }
59 
SetCallGraph(const std::string & sampleTypes)60 void PerfDecorator::SetCallGraph(const std::string &sampleTypes)
61 {
62     perfCollector_->SetCallGraph(sampleTypes);
63 }
64 
SetSelectEvents(const std::vector<std::string> & selectEvents)65 void PerfDecorator::SetSelectEvents(const std::vector<std::string> &selectEvents)
66 {
67     perfCollector_->SetSelectEvents(selectEvents);
68 }
69 
SetCpuPercent(int cpuPercent)70 void PerfDecorator::SetCpuPercent(int cpuPercent)
71 {
72     perfCollector_->SetCpuPercent(cpuPercent);
73 }
74 
SetReport(bool enable)75 void PerfDecorator::SetReport(bool enable)
76 {
77     perfCollector_->SetReport(enable);
78 }
79 
SaveStatCommonInfo()80 void PerfDecorator::SaveStatCommonInfo()
81 {
82     std::map<std::string, StatInfo> statInfo = statInfoWrapper_.GetStatInfo();
83     std::vector<std::string> formattedStatInfo;
84     for (const auto& record : statInfo) {
85         formattedStatInfo.push_back(record.second.ToString());
86     }
87     WriteLinesToFile(formattedStatInfo, false);
88 }
89 
ResetStatInfo()90 void PerfDecorator::ResetStatInfo()
91 {
92     statInfoWrapper_.ResetStatInfo();
93 }
94 
Prepare(const std::string & logDir)95 CollectResult<bool> PerfDecorator::Prepare(const std::string &logDir)
96 {
97     auto task = [this, &logDir] { return perfCollector_->Prepare(logDir); };
98     return Invoke(task, statInfoWrapper_, PERF_COLLECTOR_NAME + UC_SEPARATOR + __func__);
99 }
100 
StartRun()101 CollectResult<bool> PerfDecorator::StartRun()
102 {
103     auto task = [this] { return perfCollector_->StartRun(); };
104     return Invoke(task, statInfoWrapper_, PERF_COLLECTOR_NAME + UC_SEPARATOR + __func__);
105 }
106 
Pause()107 CollectResult<bool> PerfDecorator::Pause()
108 {
109     auto task = [this] { return perfCollector_->Pause(); };
110     return Invoke(task, statInfoWrapper_, PERF_COLLECTOR_NAME + UC_SEPARATOR + __func__);
111 }
112 
Resume()113 CollectResult<bool> PerfDecorator::Resume()
114 {
115     auto task = [this] { return perfCollector_->Resume(); };
116     return Invoke(task, statInfoWrapper_, PERF_COLLECTOR_NAME + UC_SEPARATOR + __func__);
117 }
118 
Stop()119 CollectResult<bool> PerfDecorator::Stop()
120 {
121     auto task = [this] { return perfCollector_->Stop(); };
122     return Invoke(task, statInfoWrapper_, PERF_COLLECTOR_NAME + UC_SEPARATOR + __func__);
123 }
124 } // namespace UCollectUtil
125 } // namespace HiviewDFX
126 } // namespace OHOS
127 #endif // HAS_HIPERF
128