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 "native_leak_util.h"
16
17 #include <memory>
18 #include <string>
19 #include <vector>
20
21 #include "fault_detector_util.h"
22 #include "fault_info_base.h"
23 #include "hiview_logger.h"
24 #include "native_leak_info.h"
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_TAG("NativeLeakUtil");
29 using std::vector;
30 using std::pair;
31 using std::shared_ptr;
32 using std::string;
33 using std::static_pointer_cast;
34
35 namespace {
36 // calculate rssthreshold base on pssthreshold
37 vector<pair<uint64_t, float>> g_rssthresholdTimes = {
38 {2048000, 1.1},
39 {1024000, 1.3},
40 {512000, 1.5},
41 {307200, 2},
42 {102400, 3},
43 {51200, 4},
44 {10240, 6},
45 {0, 10}
46 };
47 }
48
RemoveInvalidFile(shared_ptr<FaultInfoBase> monitorInfo)49 void NativeLeakUtil::RemoveInvalidFile(shared_ptr<FaultInfoBase> monitorInfo)
50 {
51 string path = MEMORY_LEAK_PATH;
52 auto userMonitorInfo = static_pointer_cast<NativeLeakInfo>(monitorInfo);
53 vector<string> subFiles = FaultDetectorUtil::GetSubFile(path, true);
54 for (const auto &each : subFiles) {
55 if (each == userMonitorInfo->GetSampleFilePath()) {
56 string filePath = path + "/" + each;
57 string newPath = path + "/" + userMonitorInfo->GetProcessName() + "_died";
58 if (!FaultDetectorUtil::RenameFile(filePath, newPath)) {
59 HIVIEW_LOGE("failed to remove file: %{public}s", each.c_str());
60 }
61 }
62 }
63 }
64
GetRSSMemoryThreshold(uint64_t threshold)65 uint64_t NativeLeakUtil::GetRSSMemoryThreshold(uint64_t threshold)
66 {
67 auto it = find_if(g_rssthresholdTimes.begin(), g_rssthresholdTimes.end(),
68 [threshold](const pair<uint64_t, uint64_t>& rssthresholdTime) {
69 return threshold > rssthresholdTime.first;
70 });
71 if (it != g_rssthresholdTimes.end()) {
72 return threshold * it->second;
73 }
74 return threshold;
75 }
76
77 } // namespace HiviewDFX
78 } // namespace OHOS
79