1 /*
2 * Copyright (c) 2021 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 "base/memory/memory_monitor.h"
17
18 #include <map>
19 #include <mutex>
20
21 #if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM) and !defined(IOS_PLATFORM) and !defined(LINUX_PLATFORM)
22 #include <malloc.h>
23 #endif
24
25 #include "base/log/dump_log.h"
26 #include "base/log/log.h"
27 #include "base/utils/system_properties.h"
28
29 namespace OHOS::Ace {
30
PurgeMallocCache()31 void PurgeMallocCache()
32 {
33 #if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM) and !defined(IOS_PLATFORM) and !defined(LINUX_PLATFORM)
34 #if defined(__BIONIC__)
35 mallopt(M_PURGE, 0);
36 #endif
37 #endif
38 }
39
40 bool MemoryMonitor::isEnable_ = SystemProperties::GetIsUseMemoryMonitor();
41
42 class MemoryMonitorImpl : public MemoryMonitor {
43 public:
Add(void * ptr)44 void Add(void* ptr) final
45 {
46 std::lock_guard<std::mutex> lock(mutex_);
47 auto result = memoryMap_.emplace(ptr, MemInfo());
48 if (!result.second) {
49 return;
50 }
51
52 count_++;
53 }
54
Remove(void * ptr)55 void Remove(void* ptr) final
56 {
57 std::lock_guard<std::mutex> lock(mutex_);
58 auto it = memoryMap_.find(ptr);
59 if (it == memoryMap_.end()) {
60 return;
61 }
62 count_--;
63
64 if (it->second.size > 0) {
65 total_ -= it->second.size;
66 auto& info = typeMap_[it->second.typeName];
67 info.count--;
68 info.total -= it->second.size;
69 }
70 memoryMap_.erase(it);
71 }
72
Update(void * ptr,size_t size,const std::string & typeName)73 void Update(void* ptr, size_t size, const std::string& typeName) final
74 {
75 std::lock_guard<std::mutex> lock(mutex_);
76 auto it = memoryMap_.find(ptr);
77 if (it == memoryMap_.end()) {
78 return;
79 }
80
81 it->second.size = size;
82 it->second.typeName = typeName;
83
84 total_ += size;
85 auto& info = typeMap_[typeName];
86 info.count++;
87 info.total += size;
88 }
89
Dump() const90 void Dump() const final
91 {
92 if (!IsEnable()) {
93 DumpLog::GetInstance().Print(0, "Set `persist.ace.memorymonitor.enabled = 1` to enable this feature");
94 return;
95 }
96 std::lock_guard<std::mutex> lock(mutex_);
97 std::string out = "total = " + std::to_string(total_) + ", count = " + std::to_string(count_);
98 DumpLog::GetInstance().Print(0, out);
99 for (auto&& [typeName, info] : typeMap_) {
100 if (info.total == 0) {
101 continue;
102 }
103 out = typeName + ": total = " + std::to_string(info.total) + ", count = " + std::to_string(info.count);
104 DumpLog::GetInstance().Print(1, out);
105 }
106 }
107
108 private:
109 struct MemInfo {
110 size_t size = 0;
111 std::string typeName = "Unknown";
112 };
113
114 struct TypeInfo {
115 size_t count = 0;
116 size_t total = 0;
117 };
118
119 std::map<void*, MemInfo> memoryMap_;
120 std::map<std::string, TypeInfo> typeMap_;
121 size_t total_ = 0;
122 size_t count_ = 0;
123
124 mutable std::mutex mutex_;
125 };
126
GetInstance()127 MemoryMonitor& MemoryMonitor::GetInstance()
128 {
129 static MemoryMonitorImpl instance;
130 return instance;
131 }
132
133 } // namespace OHOS::Ace
134