• 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 "hitrace_meter.h"
22 #include "hiview_logger.h"
23 #include "parameter_ex.h"
24 #include "string_util.h"
25 #include "time_util.h"
26 #include "trace_state_machine.h"
27 #include "trace_flow_controller.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 DEFINE_LOG_TAG("TraceCacheMonitor");
32 namespace {
33 constexpr int32_t HITRACE_CACHE_DURATION_LIMIT_PER_EVENT = 2 * 60; // 2 minutes
34 constexpr uint64_t S_TO_NS = 1000000000;
35 constexpr int32_t CACHE_OFF_CONDITION_COUNTDOWN = 2;
36 constexpr int32_t MONITOR_INTERVAL = 5;
37 constexpr char PARAM_KEY_CACHE_LOW_MEM_THRESHOLD[] = "hiviewdfx.ucollection.memthreshold";
38 constexpr int32_t HIVIEW_CACHE_LOW_MEM_THRESHOLD = HIVIEW_LOW_MEM_THRESHOLD;
39 constexpr char BEHAVIOR[] = "behavior";
40 
GetNextDay()41 std::chrono::system_clock::time_point GetNextDay()
42 {
43     auto now = std::chrono::system_clock::now();
44     std::time_t nowTime = std::chrono::system_clock::to_time_t(now);
45     std::tm nowTm;
46     if (localtime_r(&nowTime, &nowTm) == nullptr) {
47         HIVIEW_LOGW("Failed to get localtime, return 24h later");
48         now += std::chrono::hours(24); // set to 24h later
49         return now;
50     }
51     nowTm.tm_sec = 0;
52     nowTm.tm_min = 0;
53     nowTm.tm_hour = 0;
54     nowTm.tm_mday += 1;
55     std::time_t nextDayTime = std::mktime(&nowTm);
56     return std::chrono::system_clock::from_time_t(nextDayTime);
57 }
58 
OnLowMemThresholdChange(const char * key,const char * value,void * context)59 void OnLowMemThresholdChange(const char *key, const char *value, void *context)
60 {
61     if (context == nullptr || key == nullptr || value == nullptr) {
62         HIVIEW_LOGW("invalid input");
63         return;
64     }
65     if (strncmp(key, PARAM_KEY_CACHE_LOW_MEM_THRESHOLD, strlen(PARAM_KEY_CACHE_LOW_MEM_THRESHOLD)) != 0) {
66         HIVIEW_LOGE("key error");
67         return;
68     }
69     int32_t threshold = StringUtil::StrToInt(value);
70     if (threshold < 0) {
71         HIVIEW_LOGW("invalid threshold: %{public}s", value);
72         return;
73     }
74     TraceCacheMonitor *monitor = static_cast<TraceCacheMonitor *>(context);
75     monitor->SetLowMemThreshold(threshold);
76     HIVIEW_LOGI("set low mem threshold to %{public}d", threshold);
77 }
78 }  // namespace
79 
TraceCacheMonitor()80 TraceCacheMonitor::TraceCacheMonitor()
81 {
82     collector_ = UCollectUtil::MemoryCollector::Create();
83     lowMemThreshold_ = HIVIEW_CACHE_LOW_MEM_THRESHOLD;
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 (TraceFlowController(BEHAVIOR).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     HitraceScoped trace(HITRACE_TAG_OHOS, std::string(__func__));
205     CollectResult<SysMemory> data = collector_->CollectSysMemory();
206     return (data.retCode == UCollect::UcError::SUCCESS) && (data.data.memAvailable < lowMemThreshold_);
207 }
208 
SetCacheOn()209 void TraceCacheMonitor::SetCacheOn()
210 {
211     TraceRet ret = TraceStateMachine::GetInstance().TraceCacheOn();
212     isCacheOn_ = ret.IsSuccess();
213     if (!isCacheOn_) {
214         HIVIEW_LOGE("fail state:%{public}d error:%{public}d ", static_cast<int>(ret.GetStateError()),
215             ret.codeError_);
216     }
217     cacheDuration_ = 0;
218     cacheOffCountdown_ = CACHE_OFF_CONDITION_COUNTDOWN;
219 }
220 
SetCacheOff()221 void TraceCacheMonitor::SetCacheOff()
222 {
223     isCacheOn_ = false;
224     TraceRet ret = TraceStateMachine::GetInstance().TraceCacheOff();
225     if (!ret.IsSuccess()) {
226         HIVIEW_LOGE("fail state:%{public}d error:%{public}d ", static_cast<int>(ret.GetStateError()),
227             ret.codeError_);
228     }
229 }
230 
CountDownCacheOff()231 void TraceCacheMonitor::CountDownCacheOff()
232 {
233     cacheOffCountdown_--;
234     if (cacheOffCountdown_ <= 0) { // two cycles above threshold to turn off cache
235         isCacheOn_ = false;
236         TraceRet ret = TraceStateMachine::GetInstance().TraceCacheOff();
237         if (!ret.IsSuccess()) {
238             HIVIEW_LOGE("fail state:%{public}d error:%{public}d ", static_cast<int>(ret.GetStateError()),
239                 ret.codeError_);
240         }
241     }
242 }
243 }  // namespace HiviewDFX
244 }  // namespace OHOS
245