1 /* 2 * Copyright (c) 2022 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 OHOS_MEMORY_MEMMGR_MEMORY_PRESSURE_MONITOR_H 17 #define OHOS_MEMORY_MEMMGR_MEMORY_PRESSURE_MONITOR_H 18 19 #include <sys/epoll.h> 20 #include <sys/types.h> 21 #include <fcntl.h> 22 #include "event_handler.h" 23 24 #define MS_PER_SECOND 1000 25 26 #define NS_PER_MS 1000000 27 28 #define US_PER_MS 1000 29 30 #define WINDOW_IN_MS 1000 31 32 #define MEMORY_PRESSURE_FILE "/proc/pressure/memory" 33 34 #define POLL_PERIOD_MS 10 35 36 #define POLL_COUNT (WINDOW_IN_MS / POLL_PERIOD_MS) 37 38 namespace OHOS { 39 namespace Memory { 40 struct MemPressCallback { 41 std::function<void(const int &level)> OnMemPressLevelUploaded; 42 }; 43 44 enum MemPressureLevel { 45 LEVEL_0 = 0, 46 MEM_PRESS_LEVEL_COUNT 47 }; 48 49 enum StallType { 50 SOME, 51 FULL, 52 STALL_TYPE_COUNT 53 }; 54 55 struct LevelHandler { 56 int data; 57 void (*handler)(int data, uint32_t events); 58 }; 59 60 struct MemPressLevelCfg { 61 enum MemPressureLevel level; 62 enum StallType stallType; 63 int thresholdInMs; 64 int levelFileFd; 65 struct LevelHandler levelHandler; 66 }; 67 68 static struct MemPressLevelCfg levelConfigArr[MEM_PRESS_LEVEL_COUNT] = { 69 { LEVEL_0, SOME, 70, -1}, /* 70ms out of 1sec for partial stall */ 70 }; 71 72 73 void HandleLevelReport(int level, uint32_t events); 74 75 class MemoryPressureMonitor { 76 public: 77 MemoryPressureMonitor(const MemPressCallback &callback); 78 ~MemoryPressureMonitor(); 79 private: 80 // callback is used to call event center (and is unused now) 81 MemPressCallback callback_; 82 // run MainLoop on handler thread 83 std::shared_ptr<AppExecFwk::EventHandler> handler_; 84 int epollfd_ = -1; 85 // current monitor level count 86 int curLevelCount_ = 0; 87 int polling_ = 0; 88 89 struct timespec lastTime_ = {0, 0}; 90 struct timespec currentTime_ = {0, 0}; 91 struct LevelHandler *handlerInfo_; 92 struct LevelHandler *pollHandler_ = NULL; 93 94 void Init(); 95 96 bool MonitorLevel(MemPressureLevel level); 97 int CreateLevelFileFd(StallType stallType, int thresholdInUs, int windowInUs); 98 int AddLevelFileFdToEpoll(int epollfd, int fd, void* data); 99 void UnMonitorLevel(MemPressureLevel level); 100 int delLevelFileFdFromEpoll(int epollfd, int fd); 101 void CloseLevelFileFd(int fd); 102 void MainLoop(void); 103 void HandleTimeOut(); 104 void HandleEpollEvent(struct epoll_event *curEpollEvent); 105 }; 106 } // namespace Memory 107 } // namespace OHOS 108 #endif // OHOS_MEMORY_MEMMGR_MEMORY_PRESSURE_MONITOR_H 109