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 "mem_profiler_decorator.h"
17
18 namespace OHOS {
19 namespace HiviewDFX {
20 namespace UCollectUtil {
21 const std::string MEM_PROFILER_COLLECTOR_NAME = "MemProfilerCollector";
22 StatInfoWrapper MemProfilerDecorator::statInfoWrapper_;
23
Prepare()24 int MemProfilerDecorator::Prepare()
25 {
26 auto task = [this] { return memProfilerCollector_->Prepare(); };
27 return Invoke(task, statInfoWrapper_, MEM_PROFILER_COLLECTOR_NAME + UC_SEPARATOR + __func__);
28 }
29
Start(ProfilerType type,int pid,int duration,int sampleInterval)30 int MemProfilerDecorator::Start(ProfilerType type, int pid, int duration, int sampleInterval)
31 {
32 auto task = [this, &type, &pid, &duration, &sampleInterval] {
33 return memProfilerCollector_->Start(type, pid, duration, sampleInterval);
34 };
35 // has same func name, rename it with num "-1"
36 return Invoke(task, statInfoWrapper_, MEM_PROFILER_COLLECTOR_NAME + UC_SEPARATOR + __func__ + "-1");
37 }
38
Stop(int pid)39 int MemProfilerDecorator::Stop(int pid)
40 {
41 auto task = [this, &pid] { return memProfilerCollector_->Stop(pid); };
42 // has same func name, rename it with num "-1"
43 return Invoke(task, statInfoWrapper_, MEM_PROFILER_COLLECTOR_NAME + UC_SEPARATOR + __func__ + "-1");
44 }
45
Stop(const std::string & processName)46 int MemProfilerDecorator::Stop(const std::string& processName)
47 {
48 auto task = [this, &processName] { return memProfilerCollector_->Stop(processName); };
49 // has same func name, rename it with num "-1"
50 return Invoke(task, statInfoWrapper_, MEM_PROFILER_COLLECTOR_NAME + UC_SEPARATOR + __func__ + "-2");
51 }
52
Start(int fd,ProfilerType type,int pid,int duration,int sampleInterval)53 int MemProfilerDecorator::Start(int fd, ProfilerType type, int pid, int duration, int sampleInterval)
54 {
55 auto task = [this, &fd, &type, &pid, &duration, &sampleInterval] {
56 return memProfilerCollector_->Start(fd, type, pid, duration, sampleInterval);
57 };
58 // has same func name, rename it with num "-2"
59 return Invoke(task, statInfoWrapper_, MEM_PROFILER_COLLECTOR_NAME + UC_SEPARATOR + __func__ + "-2");
60 }
61
StartPrintNmd(int fd,int pid,int type)62 int MemProfilerDecorator::StartPrintNmd(int fd, int pid, int type)
63 {
64 auto task = [this, &fd, &pid, &type] { return memProfilerCollector_->StartPrintNmd(fd, pid, type); };
65 return Invoke(task, statInfoWrapper_, MEM_PROFILER_COLLECTOR_NAME + UC_SEPARATOR + __func__);
66 }
67
Start(int fd,ProfilerType type,std::string processName,int duration,int sampleInterval,bool startup)68 int MemProfilerDecorator::Start(int fd, ProfilerType type, std::string processName, int duration,
69 int sampleInterval, bool startup)
70 {
71 auto task = [this, &fd, &type, &processName, &duration, &sampleInterval, &startup] {
72 return memProfilerCollector_->Start(fd, type, processName, duration, sampleInterval, startup);
73 };
74 // has same func name, rename it with num "-2"
75 return Invoke(task, statInfoWrapper_, MEM_PROFILER_COLLECTOR_NAME + UC_SEPARATOR + __func__ + "-3");
76 }
77
SaveStatCommonInfo()78 void MemProfilerDecorator::SaveStatCommonInfo()
79 {
80 std::map<std::string, StatInfo> statInfo = statInfoWrapper_.GetStatInfo();
81 std::vector<std::string> formattedStatInfo;
82 for (const auto& record : statInfo) {
83 formattedStatInfo.push_back(record.second.ToString());
84 }
85 WriteLinesToFile(formattedStatInfo, false);
86 }
87
ResetStatInfo()88 void MemProfilerDecorator::ResetStatInfo()
89 {
90 statInfoWrapper_.ResetStatInfo();
91 }
92 } // namespace UCollectUtil
93 } // namespace HiviewDFX
94 } // namespace OHOS
95