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 "memory_decorator.h"
17
18 namespace OHOS {
19 namespace HiviewDFX {
20 namespace UCollectUtil {
21 const std::string MEM_COLLECTOR_NAME = "MemoryCollector";
22 StatInfoWrapper MemoryDecorator::statInfoWrapper_;
23
CollectProcessMemory(int32_t pid)24 CollectResult<ProcessMemory> MemoryDecorator::CollectProcessMemory(int32_t pid)
25 {
26 auto task = [this, &pid] { return memoryCollector_->CollectProcessMemory(pid); };
27 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
28 }
29
CollectSysMemory()30 CollectResult<SysMemory> MemoryDecorator::CollectSysMemory()
31 {
32 auto task = [this] { return memoryCollector_->CollectSysMemory(); };
33 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
34 }
35
CollectRawMemInfo()36 CollectResult<std::string> MemoryDecorator::CollectRawMemInfo()
37 {
38 auto task = [this] { return memoryCollector_->CollectRawMemInfo(); };
39 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
40 }
41
ExportMemView()42 CollectResult<std::string> MemoryDecorator::ExportMemView()
43 {
44 auto task = [this] { return memoryCollector_->ExportMemView(); };
45 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
46 }
47
CollectAllProcessMemory()48 CollectResult<std::vector<ProcessMemory>> MemoryDecorator::CollectAllProcessMemory()
49 {
50 auto task = [this] { return memoryCollector_->CollectAllProcessMemory(); };
51 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
52 }
ExportAllProcessMemory()53 CollectResult<std::string> MemoryDecorator::ExportAllProcessMemory()
54 {
55 auto task = [this] { return memoryCollector_->ExportAllProcessMemory(); };
56 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
57 }
58
CollectRawSlabInfo()59 CollectResult<std::string> MemoryDecorator::CollectRawSlabInfo()
60 {
61 auto task = [this] { return memoryCollector_->CollectRawSlabInfo(); };
62 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
63 }
64
CollectRawPageTypeInfo()65 CollectResult<std::string> MemoryDecorator::CollectRawPageTypeInfo()
66 {
67 auto task = [this] { return memoryCollector_->CollectRawPageTypeInfo(); };
68 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
69 }
70
CollectRawDMA()71 CollectResult<std::string> MemoryDecorator::CollectRawDMA()
72 {
73 auto task = [this] { return memoryCollector_->CollectRawDMA(); };
74 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
75 }
76
CollectAllAIProcess()77 CollectResult<std::vector<AIProcessMem>> MemoryDecorator::CollectAllAIProcess()
78 {
79 auto task = [this] { return memoryCollector_->CollectAllAIProcess(); };
80 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
81 }
82
ExportAllAIProcess()83 CollectResult<std::string> MemoryDecorator::ExportAllAIProcess()
84 {
85 auto task = [this] { return memoryCollector_->ExportAllAIProcess(); };
86 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
87 }
88
CollectRawSmaps(int32_t pid)89 CollectResult<std::string> MemoryDecorator::CollectRawSmaps(int32_t pid)
90 {
91 auto task = [this, &pid] { return memoryCollector_->CollectRawSmaps(pid); };
92 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
93 }
94
CollectHprof(int32_t pid)95 CollectResult<std::string> MemoryDecorator::CollectHprof(int32_t pid)
96 {
97 auto task = [this, &pid] { return memoryCollector_->CollectHprof(pid); };
98 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
99 }
100
CollectProcessVss(int32_t pid)101 CollectResult<uint64_t> MemoryDecorator::CollectProcessVss(int32_t pid)
102 {
103 auto task = [this, &pid] { return memoryCollector_->CollectProcessVss(pid); };
104 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
105 }
106
CollectMemoryLimit()107 CollectResult<MemoryLimit> MemoryDecorator::CollectMemoryLimit()
108 {
109 auto task = [this] { return memoryCollector_->CollectMemoryLimit(); };
110 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
111 }
112
CollectDdrFreq()113 CollectResult<uint32_t> MemoryDecorator::CollectDdrFreq()
114 {
115 auto task = [this] { return memoryCollector_->CollectDdrFreq(); };
116 return Invoke(task, statInfoWrapper_, MEM_COLLECTOR_NAME + UC_SEPARATOR + __func__);
117 }
118
SaveStatCommonInfo()119 void MemoryDecorator::SaveStatCommonInfo()
120 {
121 std::map<std::string, StatInfo> statInfo = statInfoWrapper_.GetStatInfo();
122 std::vector<std::string> formattedStatInfo;
123 for (const auto& record : statInfo) {
124 formattedStatInfo.push_back(record.second.ToString());
125 }
126 WriteLinesToFile(formattedStatInfo, false);
127 }
128
ResetStatInfo()129 void MemoryDecorator::ResetStatInfo()
130 {
131 statInfoWrapper_.ResetStatInfo();
132 }
133 } // namespace UCollectUtil
134 } // namespace HiviewDFX
135 } // namespace OHOS
136