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 #ifndef FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_CACHE_MONITOR_H 17 #define FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_CACHE_MONITOR_H 18 19 #include "memory_collector.h" 20 #include "trace_behavior_storage.h" 21 #include "trace_flow_controller.h" 22 23 namespace OHOS { 24 namespace HiviewDFX { 25 26 namespace { 27 enum TraceCacheMonitorState { 28 EXIT = 0, 29 RUNNING = 1, 30 INTERRUPT = 2, 31 }; 32 } 33 34 class TraceCacheMonitor { 35 public: 36 TraceCacheMonitor(); 37 ~TraceCacheMonitor(); 38 void SetLowMemThreshold(int32_t threshold); 39 void RunMonitorLoop(); 40 void ExitMonitorLoop(); 41 private: 42 void MonitorFfrtTask(); 43 void RunMonitorCycle(int32_t interval); 44 bool IsLowMemState(); 45 void SetCacheOn(); 46 void SetCacheOff(); 47 void CountDownCacheOff(); 48 void SetCacheStatus(int32_t interval); 49 50 private: 51 std::mutex stateMutex_; 52 TraceCacheMonitorState monitorState_ = EXIT; 53 std::shared_ptr<UCollectUtil::MemoryCollector> collector_; 54 bool isCacheOn_ = false; 55 bool isWaitingForRecovery_ = false; 56 int32_t lowMemThreshold_ = 0; 57 int32_t cacheDuration_ = 0; 58 int32_t cacheOffCountdown_ = 0; 59 }; 60 61 } // namespace HiviewDFX 62 } // namespace OHOS 63 #endif // FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_CACHE_MONITOR_H 64