• 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 "trace_cache_monitor.h"
17 
18 #include <ctime>
19 
20 #include "ffrt.h"
21 #include "hiview_logger.h"
22 #include "parameter_ex.h"
23 #include "string_util.h"
24 #include "time_util.h"
25 #include "trace_state_machine.h"
26 #include "trace_flow_controller.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 DEFINE_LOG_TAG("TraceCacheMonitor");
31 namespace {
32 constexpr int32_t HITRACE_CACHE_DURATION_LIMIT_PER_EVENT = 2 * 60; // 2 minutes
33 constexpr uint64_t S_TO_NS = 1000000000;
34 constexpr int32_t CACHE_OFF_CONDITION_COUNTDOWN = 2;
35 constexpr int32_t MONITOR_INTERVAL = 5;
36 constexpr char PARAM_KEY_CACHE_LOW_MEM_THRESHOLD[] = "hiviewdfx.ucollection.memthreshold";
37 constexpr int32_t HIVIEW_CACHE_LOW_MEM_THRESHOLD = HIVIEW_LOW_MEM_THRESHOLD;
38 const std::string BEHAVIOR = "behavior";
39 
GetNextDay()40 std::chrono::system_clock::time_point GetNextDay()
41 {
42     auto now = std::chrono::system_clock::now();
43     std::time_t nowTime = std::chrono::system_clock::to_time_t(now);
44     std::tm nowTm;
45     if (localtime_r(&nowTime, &nowTm) == nullptr) {
46         HIVIEW_LOGW("Failed to get localtime, return 24h later");
47         now += std::chrono::hours(24); // set to 24h later
48         return now;
49     }
50     nowTm.tm_sec = 0;
51     nowTm.tm_min = 0;
52     nowTm.tm_hour = 0;
53     nowTm.tm_mday += 1;
54     std::time_t nextDayTime = std::mktime(&nowTm);
55     return std::chrono::system_clock::from_time_t(nextDayTime);
56 }
57 
OnLowMemThresholdChange(const char * key,const char * value,void * context)58 void OnLowMemThresholdChange(const char *key, const char *value, void *context)
59 {
60     if (context == nullptr || key == nullptr || value == nullptr) {
61         HIVIEW_LOGW("invalid input");
62         return;
63     }
64     if (strncmp(key, PARAM_KEY_CACHE_LOW_MEM_THRESHOLD, strlen(PARAM_KEY_CACHE_LOW_MEM_THRESHOLD)) != 0) {
65         HIVIEW_LOGE("key error");
66         return;
67     }
68     int32_t threshold = StringUtil::StrToInt(value);
69     if (threshold < 0) {
70         HIVIEW_LOGW("invalid threshold: %{public}s", value);
71         return;
72     }
73     TraceCacheMonitor *monitor = static_cast<TraceCacheMonitor *>(context);
74     monitor->SetLowMemThreshold(threshold);
75     HIVIEW_LOGI("set low mem threshold to %{public}d", threshold);
76 }
77 }  // namespace
78 
TraceCacheMonitor()79 TraceCacheMonitor::TraceCacheMonitor()
80 {
81     collector_ = UCollectUtil::MemoryCollector::Create();
82     lowMemThreshold_ = HIVIEW_CACHE_LOW_MEM_THRESHOLD;
83     flowController_ = std::make_shared<TraceFlowController>(BEHAVIOR);
84     int32_t ret = Parameter::WatchParamChange(PARAM_KEY_CACHE_LOW_MEM_THRESHOLD, OnLowMemThresholdChange, this);
85     HIVIEW_LOGI("watchParamChange ret: %{public}d", ret);
86 }
87 
~TraceCacheMonitor()88 TraceCacheMonitor::~TraceCacheMonitor()
89 {
90     if (isCacheOn_) {
91         SetCacheOff();
92     }
93 }
94 
SetLowMemThreshold(int32_t threshold)95 void TraceCacheMonitor::SetLowMemThreshold(int32_t threshold)
96 {
97     lowMemThreshold_ = threshold;
98 }
99 
RunMonitorLoop()100 void TraceCacheMonitor::RunMonitorLoop()
101 {
102     CollectResult<SysMemory> data = collector_->CollectSysMemory();
103     if (data.retCode != UCollect::UcError::SUCCESS || data.data.memTotal < lowMemThreshold_) {
104         HIVIEW_LOGW("monitor task prerequisites not met, memory collection ret: %{public}d, "
105             "threshold: %{public}d, total memory: %{public}d",
106             data.retCode, lowMemThreshold_, data.data.memTotal);
107         return;
108     }
109     std::lock_guard<std::mutex> lock(stateMutex_);
110     if (monitorState_ != EXIT) {
111         HIVIEW_LOGW("monitorLoop is already running");
112         return;
113     }
114     HIVIEW_LOGI("start hiview monitor task");
115     monitorState_ = RUNNING;
116     auto task = [this] { this->MonitorFfrtTask(); };
117     ffrt::submit(task, {}, {}, ffrt::task_attr().name("dft_uc_Monitor"));
118 }
119 
ExitMonitorLoop()120 void TraceCacheMonitor::ExitMonitorLoop()
121 {
122     std::lock_guard<std::mutex> lock(stateMutex_);
123     if (monitorState_ == RUNNING) {
124         HIVIEW_LOGI("interrupting monitor running state.");
125         monitorState_ = INTERRUPT;
126     } else {
127         HIVIEW_LOGW("monitor is not in running state, exit.");
128     }
129 }
130 
MonitorFfrtTask()131 void TraceCacheMonitor::MonitorFfrtTask()
132 {
133     while (monitorState_ == RUNNING) {
134         RunMonitorCycle(MONITOR_INTERVAL);
135     }
136     std::lock_guard<std::mutex> lock(stateMutex_);
137     if (monitorState_ == INTERRUPT) {
138         monitorState_ = EXIT;
139     }
140     HIVIEW_LOGI("exit hiview monitor task");
141 }
142 
RunMonitorCycle(int32_t interval)143 void TraceCacheMonitor::RunMonitorCycle(int32_t interval)
144 {
145     bool isTargetCacheOn = IsLowMemState();
146     if (isWaitingForRecovery_) {
147         if (isTargetCacheOn) {
148             ffrt::this_task::sleep_for(std::chrono::seconds(interval));
149             return;
150         } else {
151             isWaitingForRecovery_ = false;
152         }
153     }
154     if (isTargetCacheOn != isCacheOn_) {
155         if (isTargetCacheOn) {
156             SetCacheOn();
157         } else {
158             CountDownCacheOff();
159         }
160     } else {
161         cacheOffCountdown_ = CACHE_OFF_CONDITION_COUNTDOWN;
162     }
163     if (!isCacheOn_) {
164         ffrt::this_task::sleep_for(std::chrono::seconds(interval));
165         return;
166     }
167     SetCacheStatus(interval);
168 }
169 
SetCacheStatus(int32_t interval)170 void TraceCacheMonitor::SetCacheStatus(int32_t interval)
171 {
172     struct timespec bts = {0, 0};
173     clock_gettime(CLOCK_BOOTTIME, &bts);
174     uint64_t startTime = static_cast<uint64_t>(bts.tv_sec * S_TO_NS + bts.tv_nsec);
175     ffrt::this_task::sleep_for(std::chrono::seconds(interval));
176     clock_gettime(CLOCK_BOOTTIME, &bts);
177     uint64_t endTime = static_cast<uint64_t>(bts.tv_sec * S_TO_NS + bts.tv_nsec);
178     int32_t timeDiff = static_cast<int32_t>((endTime - startTime) / S_TO_NS);
179     switch (flowController_->UseCacheTimeQuota(timeDiff)) {
180         case CacheFlow::EXIT:
181             HIVIEW_LOGE("failed to get and insert record, close task");
182             ExitMonitorLoop();
183             break;
184         case CacheFlow::OVER_FLOW:
185             SetCacheOff();
186             HIVIEW_LOGW("quota exceeded, sleep until the next day");
187             ffrt::this_task::sleep_until(GetNextDay());
188             break;
189         case CacheFlow::SUCCESS:
190             cacheDuration_ += timeDiff;
191             if (cacheDuration_ >= HITRACE_CACHE_DURATION_LIMIT_PER_EVENT) {
192                 SetCacheOff();
193                 HIVIEW_LOGW("reach cache duration limit, wait for system to recover from low mem state");
194                 isWaitingForRecovery_ = true;
195             }
196             break;
197         default:
198             break;
199     }
200 }
201 
IsLowMemState()202 bool TraceCacheMonitor::IsLowMemState()
203 {
204     CollectResult<SysMemory> data = collector_->CollectSysMemory();
205     return (data.retCode == UCollect::UcError::SUCCESS) && (data.data.memAvailable < lowMemThreshold_);
206 }
207 
SetCacheOn()208 void TraceCacheMonitor::SetCacheOn()
209 {
210     TraceRet ret = TraceStateMachine::GetInstance().TraceCacheOn();
211     isCacheOn_ = ret.IsSuccess();
212     if (!isCacheOn_) {
213         HIVIEW_LOGE("fail state:%{public}d error:%{public}d ", static_cast<int>(ret.GetStateError()),
214             ret.codeError_);
215     }
216     cacheDuration_ = 0;
217     cacheOffCountdown_ = CACHE_OFF_CONDITION_COUNTDOWN;
218 }
219 
SetCacheOff()220 void TraceCacheMonitor::SetCacheOff()
221 {
222     isCacheOn_ = false;
223     TraceRet ret = TraceStateMachine::GetInstance().TraceCacheOff();
224     if (!ret.IsSuccess()) {
225         HIVIEW_LOGE("fail state:%{public}d error:%{public}d ", static_cast<int>(ret.GetStateError()),
226             ret.codeError_);
227     }
228 }
229 
CountDownCacheOff()230 void TraceCacheMonitor::CountDownCacheOff()
231 {
232     cacheOffCountdown_--;
233     if (cacheOffCountdown_ <= 0) { // two cycles above threshold to turn off cache
234         isCacheOn_ = false;
235         TraceRet ret = TraceStateMachine::GetInstance().TraceCacheOff();
236         if (!ret.IsSuccess()) {
237             HIVIEW_LOGE("fail state:%{public}d error:%{public}d ", static_cast<int>(ret.GetStateError()),
238                 ret.codeError_);
239         }
240     }
241 }
242 }  // namespace HiviewDFX
243 }  // namespace OHOS
244