• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 #include "decorator_util.h"
18 
19 namespace OHOS {
20 namespace HiviewDFX {
21 namespace UCollectUtil {
22 constexpr char MEM_PROFILER_COLLECTOR_NAME[] = "MemProfilerCollector";
23 StatInfoWrapper MemProfilerDecorator::statInfoWrapper_;
24 
Prepare()25 int MemProfilerDecorator::Prepare()
26 {
27     auto task = [this] { return memProfilerCollector_->Prepare(); };
28     return Invoke(task, statInfoWrapper_, std::string(MEM_PROFILER_COLLECTOR_NAME) + UC_SEPARATOR + __func__);
29 }
30 
Start(const MemoryProfilerConfig & memoryProfilerConfig)31 int MemProfilerDecorator::Start(const MemoryProfilerConfig& memoryProfilerConfig)
32 {
33     auto task = [this, &memoryProfilerConfig] {
34         return memProfilerCollector_->Start(memoryProfilerConfig);
35     };
36     // has same func name, rename it with num "-1"
37     return Invoke(task, statInfoWrapper_, std::string(MEM_PROFILER_COLLECTOR_NAME) + UC_SEPARATOR + __func__ + "-1");
38 }
39 
Stop(int pid)40 int MemProfilerDecorator::Stop(int pid)
41 {
42     auto task = [this, &pid] { return memProfilerCollector_->Stop(pid); };
43     // has same func name, rename it with num "-1"
44     return Invoke(task, statInfoWrapper_, std::string(MEM_PROFILER_COLLECTOR_NAME) + UC_SEPARATOR + __func__ + "-1");
45 }
46 
Stop(const std::string & processName)47 int MemProfilerDecorator::Stop(const std::string& processName)
48 {
49     auto task = [this, &processName] { return memProfilerCollector_->Stop(processName); };
50     // has same func name, rename it with num "-1"
51     return Invoke(task, statInfoWrapper_, std::string(MEM_PROFILER_COLLECTOR_NAME) + UC_SEPARATOR + __func__ + "-2");
52 }
53 
Start(int fd,const MemoryProfilerConfig & memoryProfilerConfig)54 int MemProfilerDecorator::Start(int fd, const MemoryProfilerConfig& memoryProfilerConfig)
55 {
56     auto task = [this, &fd, &memoryProfilerConfig] {
57         return memProfilerCollector_->Start(fd, memoryProfilerConfig);
58     };
59     // has same func name, rename it with num "-2"
60     return Invoke(task, statInfoWrapper_, std::string(MEM_PROFILER_COLLECTOR_NAME) + UC_SEPARATOR + __func__ + "-2");
61 }
62 
StartPrintNmd(int fd,int pid,int type)63 int MemProfilerDecorator::StartPrintNmd(int fd, int pid, int type)
64 {
65     auto task = [this, &fd, &pid, &type] { return memProfilerCollector_->StartPrintNmd(fd, pid, type); };
66     return Invoke(task, statInfoWrapper_, std::string(MEM_PROFILER_COLLECTOR_NAME) + UC_SEPARATOR + __func__);
67 }
68 
Start(int fd,bool startup,const MemoryProfilerConfig & memoryProfilerConfig)69 int MemProfilerDecorator::Start(int fd, bool startup, const MemoryProfilerConfig& memoryProfilerConfig)
70 {
71     auto task = [this, &fd, &startup, &memoryProfilerConfig] {
72         return memProfilerCollector_->Start(fd, startup, memoryProfilerConfig);
73     };
74     // has same func name, rename it with num "-3"
75     return Invoke(task, statInfoWrapper_, std::string(MEM_PROFILER_COLLECTOR_NAME) + UC_SEPARATOR + __func__ + "-3");
76 }
77 
Start(int fd,pid_t pid,uint32_t duration,const SimplifiedMemConfig & config)78 int MemProfilerDecorator::Start(int fd, pid_t pid, uint32_t duration, const SimplifiedMemConfig& config)
79 {
80     auto task = [this, &fd, &pid, &duration, &config] {
81         return memProfilerCollector_->Start(fd, pid, duration, config);
82     };
83     // has same func name, rename it with num "-4"
84     return Invoke(task, statInfoWrapper_, std::string(MEM_PROFILER_COLLECTOR_NAME) + UC_SEPARATOR + __func__ + "-4");
85 }
86 
StartPrintSimplifiedNmd(pid_t pid,std::vector<SimplifiedMemStats> & memStats)87 int MemProfilerDecorator::StartPrintSimplifiedNmd(pid_t pid, std::vector<SimplifiedMemStats>& memStats)
88 {
89     auto task = [this, &pid, &memStats] { return memProfilerCollector_->StartPrintSimplifiedNmd(pid, memStats); };
90     return Invoke(task, statInfoWrapper_, std::string(MEM_PROFILER_COLLECTOR_NAME) + UC_SEPARATOR + __func__);
91 }
92 
SaveStatCommonInfo()93 void MemProfilerDecorator::SaveStatCommonInfo()
94 {
95     std::map<std::string, StatInfo> statInfo = statInfoWrapper_.GetStatInfo();
96     std::list<std::string> formattedStatInfo;
97     for (const auto& record : statInfo) {
98         formattedStatInfo.push_back(record.second.ToString());
99     }
100     WriteLinesToFile(formattedStatInfo, false, UC_STAT_LOG_PATH);
101 }
102 
ResetStatInfo()103 void MemProfilerDecorator::ResetStatInfo()
104 {
105     statInfoWrapper_.ResetStatInfo();
106 }
107 } // namespace UCollectUtil
108 } // namespace HiviewDFX
109 } // namespace OHOS
110