• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include "memory_catcher.h"
16 
17 #include "collect_result.h"
18 #include "file_util.h"
19 #include "freeze_common.h"
20 #include "hiview_logger.h"
21 #include "memory_collector.h"
22 #include "string_util.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
27     static constexpr const char* const ASHMEM_PATH = "/proc/ashmem_process_info";
28     static constexpr const char* const DMAHEAP_PATH = "/proc/dmaheap_process_info";
29     static constexpr const char* const GPUMEM_PATH = "/proc/gpumem_process_info";
30     static constexpr const char* const ASHMEM = "AshmemUsed";
31     static constexpr const char* const DMAHEAP = "DmaHeapTotalUsed";
32     static constexpr const char* const GPUMEM = "GpuTotalUsed";
33     static constexpr const char* const LONG_PRESS = "LONG_PRESS";
34     static constexpr const char* const AP_S_PRESS6S = "AP_S_PRESS6S";
35     static constexpr const char* const PROC_PRESSUER_MEMORY = "/proc/pressure/memory";
36     static constexpr const char* const PROC_MEMORYVIEW = "/proc/memview";
37     static constexpr const char* const PROC_MEMORYINFO = "/proc/meminfo";
38     static constexpr int OVER_MEM_SIZE = 2 * 1024 * 1024;
39     static constexpr int DECIMEL = 10;
40 }
41 #ifdef USAGE_CATCHER_ENABLE
42 using namespace UCollect;
43 using namespace UCollectUtil;
44 DEFINE_LOG_LABEL(0xD002D01, "EventLogger-MemoryCatcher");
MemoryCatcher()45 MemoryCatcher::MemoryCatcher() : EventLogCatcher()
46 {
47     name_ = "MemoryCatcher";
48 }
49 
Initialize(const std::string & strParam1,int intParam1,int intParam2)50 bool MemoryCatcher::Initialize(const std::string& strParam1, int intParam1, int intParam2)
51 {
52     // this catcher do not need parameters, just return true
53     description_ = "MemoryCatcher --\n";
54     return true;
55 };
56 
Catch(int fd,int jsonFd)57 int MemoryCatcher::Catch(int fd, int jsonFd)
58 {
59     int originSize = GetFdSize(fd);
60     std::string freezeMemory = event_ != nullptr ? event_->GetEventValue("FREEZE_MEMORY") : "";
61     std::string content = freezeMemory.empty() ? CollectFreezeSysMemory() : freezeMemory;
62     std::string data = "";
63     if (!content.empty()) {
64         std::vector<std::string> vec;
65         StringUtil::SplitStr(content, "\\n", vec);
66         for (const std::string& mem : vec) {
67             FileUtil::SaveStringToFd(fd, mem + "\n");
68             CheckString(fd, mem, data, ASHMEM, ASHMEM_PATH);
69             CheckString(fd, mem, data, DMAHEAP, DMAHEAP_PATH);
70             CheckString(fd, mem, data, GPUMEM, GPUMEM_PATH);
71         }
72     }
73     if (!data.empty()) {
74         FreezeCommon::WriteStartInfoToFd(fd, "collect ashmem dmaheap gpumem start time: ");
75         FileUtil::SaveStringToFd(fd, data);
76         FreezeCommon::WriteEndInfoToFd(fd, "\ncollect ashmem dmaheap gpumem end time: ");
77     } else {
78         FileUtil::SaveStringToFd(fd, "don't collect ashmem dmaheap gpumem");
79     }
80 
81     logSize_ = GetFdSize(fd) - originSize;
82     if (logSize_ <= 0) {
83         FileUtil::SaveStringToFd(fd, "sysMemory content is empty!");
84     }
85     return logSize_;
86 };
87 
CollectMemInfo()88 void MemoryCatcher::CollectMemInfo()
89 {
90     HIVIEW_LOGI("Collect rawMemInfo and export memView start");
91     std::shared_ptr<MemoryCollector> collector = MemoryCollector::Create();
92     collector->CollectRawMemInfo();
93     collector->ExportMemView();
94     HIVIEW_LOGI("Collect rawMemInfo and export memView end");
95 }
96 
SetEvent(std::shared_ptr<SysEvent> event)97 void MemoryCatcher::SetEvent(std::shared_ptr<SysEvent> event)
98 {
99     event_ = event;
100 }
101 
GetStringFromFile(const std::string path)102 std::string MemoryCatcher::GetStringFromFile(const std::string path)
103 {
104     std::string content;
105     FileUtil::LoadStringFromFile(path, content);
106     return content;
107 }
108 
GetNumFromString(const std::string & mem)109 int MemoryCatcher::GetNumFromString(const std::string &mem)
110 {
111     int num = 0;
112     for (const char &c : mem) {
113         if (isdigit(c)) {
114             num = num * DECIMEL + (c - '0');
115         }
116         if (num > INT_MAX) {
117             return INT_MAX;
118         }
119     }
120     return num;
121 }
122 
CheckString(int fd,const std::string & mem,std::string & data,const std::string key,const std::string path)123 void MemoryCatcher::CheckString(
124     int fd, const std::string &mem, std::string &data, const std::string key, const std::string path)
125 {
126     if (mem.find(key) != std::string::npos) {
127         int memsize = GetNumFromString(mem);
128         if (memsize > OVER_MEM_SIZE) {
129             data += GetStringFromFile(path);
130         }
131     }
132 }
133 
CollectFreezeSysMemory()134 std::string MemoryCatcher::CollectFreezeSysMemory()
135 {
136     std::string content = "";
137     content += GetStringFromFile(PROC_PRESSUER_MEMORY) + "\n";
138     std::string memInfoPath = PROC_MEMORYVIEW;
139     if (!FileUtil::FileExists(memInfoPath)) {
140         memInfoPath = PROC_MEMORYINFO;
141     }
142     content += GetStringFromFile(memInfoPath);
143     return content;
144 }
145 #endif // USAGE_CATCHER_ENABLE
146 } // namespace HiviewDFX
147 } // namespace OHOS
148