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