• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "dfx_dump_catcher_slow_policy.h"
17 
18 #include <vector>
19 #include <unistd.h>
20 
21 #include "dfx_log.h"
22 #include "dfx_util.h"
23 
24 #ifdef LOG_DOMAIN
25 #undef LOG_DOMAIN
26 #define LOG_DOMAIN 0xD002D11
27 #endif
28 
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #define LOG_TAG "DfxDumpCatcherSlow"
32 #endif
33 
34 namespace OHOS {
35 namespace HiviewDFX {
36 
37 static const uint64_t DUMP_CATCH_SLOW_PERIOD_MS = 5000; // 5000 : 5s
38 static const int DUMP_CATCH_SLOW_MAX_RECORD_NUM = 50;
39 
GetInstance()40 DfxDumpCatcherSlowPolicy& DfxDumpCatcherSlowPolicy::GetInstance()
41 {
42     static DfxDumpCatcherSlowPolicy instance;
43     return instance;
44 }
45 
IsDumpCatcherInSlowPeriod(pid_t pid)46 bool DfxDumpCatcherSlowPolicy::IsDumpCatcherInSlowPeriod(pid_t pid)
47 {
48     std::unique_lock<std::mutex> lck(dumpSlowRecordMtx_);
49     auto it = dumpSlowRecordMap_.find(pid);
50     if (it == dumpSlowRecordMap_.end()) {
51         return false;
52     }
53 
54     uint64_t now = GetAbsTimeMilliSeconds();
55     if (now - it->second > DUMP_CATCH_SLOW_PERIOD_MS) {
56         dumpSlowRecordMap_.erase(pid);
57         return false;
58     }
59     return true;
60 }
61 
DumpCatcherSlowRecordAgingClean(uint64_t time)62 void DfxDumpCatcherSlowPolicy::DumpCatcherSlowRecordAgingClean(uint64_t time)
63 {
64     for (auto it = dumpSlowRecordMap_.begin(); it != dumpSlowRecordMap_.end();) {
65         if (time - it->second > DUMP_CATCH_SLOW_PERIOD_MS) {
66             it = dumpSlowRecordMap_.erase(it);
67         } else {
68             ++it;
69         }
70     }
71 
72     if (dumpSlowRecordMap_.size() >= DUMP_CATCH_SLOW_MAX_RECORD_NUM) {
73         dumpSlowRecordMap_.clear();
74     }
75 }
76 
SetDumpCatcherSlowStat(pid_t pid)77 void DfxDumpCatcherSlowPolicy::SetDumpCatcherSlowStat(pid_t pid)
78 {
79     std::unique_lock<std::mutex> lck(dumpSlowRecordMtx_);
80     uint64_t now = GetAbsTimeMilliSeconds();
81     DumpCatcherSlowRecordAgingClean(now);
82     dumpSlowRecordMap_[pid] = now;
83 }
84 
85 } // namespace HiviewDFX
86 } // namespace OHOS
87