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 #ifndef NATIVE_LEAK_UTIL_H
16 #define NATIVE_LEAK_UTIL_H
17 
18 #include <memory>
19 #include <string>
20 
21 #include <sys/ioctl.h>
22 
23 #include "fault_info_base.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 constexpr uint32_t MTYPE_USER_PSS_JAVA = 1;
28 constexpr uint32_t MTYPE_USER_PSS_NATIVE = (1 << 1);
29 constexpr uint32_t MTYPE_USER_PSS = (MTYPE_USER_PSS_JAVA | MTYPE_USER_PSS_NATIVE);
30 
31 enum {
32     NATIVE_MEMORY = 0,
33     MEMORY_TYPE_CNT  // CNT or MAX ?
34 };
35 
36 constexpr uint32_t MEMCHECK_STACKINFO_MAXSIZE = 600 * 1024;
37 constexpr uint32_t MEMCHECK_DETAILINFO_MAXSIZE = 100 * 1024;
38 constexpr uint32_t MEMORY_ERROR_GRADE = 10 * 1024;
39 constexpr uint32_t MEMORY_WARNING_GRADE = 6 * 1024;
40 constexpr uint32_t MEMORY_RATING_LINE = 20 * 1024;
41 constexpr uint32_t DEFAULT_THRESHOLD = 800 * 1024;
42 constexpr float MEMORY_WARNING_RATIO = 0.3;
43 constexpr float MEMORY_ERROR_RATIO = 0.5;
44 constexpr uint32_t MEMORY_WARNING_RATIO_GRADE = 50 * 1024;
45 constexpr uint32_t MEMORY_ERROR_RATIO_GRADE = 100 * 1024;
46 
47 constexpr int EVENT_ID_MEMORY_LEAK = 901003023;
48 constexpr int32_t MEMCHECK_PID_INVALID = 0xFFDEAE00;
49 constexpr uint32_t MEMCHECK_MAGIC = 0x5377FEFA;
50 constexpr uint32_t MEMCHECKIO = 0xAE;
51 constexpr uint32_t LOGGER_MEMCHECK_RSS_READ = _IO(MEMCHECKIO, 1);
52 constexpr uint32_t LOGGER_MEMCHECK_COMMAND = _IO(MEMCHECKIO, 2);
53 constexpr uint32_t LOGGER_MEMCHECK_STACK_READ = _IO(MEMCHECKIO, 3);
54 constexpr uint32_t LOGGER_MEMCHECK_DETAIL_READ = _IO(MEMCHECKIO, 4);
55 const std::string BBOX_PATH = "/dev/bbox";
56 const std::string RENDER_SERVICE_PROCESS = "render_service";
57 
58 enum MemCmd {
59     MEMCMD_NONE,
60     MEMCMD_ENABLE,
61     MEMCMD_DISABLE,
62     MEMCMD_SAVE_LOG,
63     MEMCMD_CLEAR_LOG,
64     MEMCMD_MAX
65 };
66 
67 class NativeLeakUtil {
68 public:
69     static void RemoveInvalidFile(std::shared_ptr<FaultInfoBase> monitorInfo);
70     static uint64_t GetRSSMemoryThreshold(uint64_t threshold);
71 };
72 
73 } // namespace HiviewDFX
74 } // namespace OHOS
75 #endif // NATIVE_LEAK_UTIL_H
76