1 /* 2 * Copyright (c) 2024 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 #ifndef RS_MEMORY_SNAPSHOT 16 #define RS_MEMORY_SNAPSHOT 17 18 #include <mutex> 19 #include <set> 20 21 #include "common/rs_common_def.h" 22 23 namespace OHOS { 24 namespace Rosen { 25 struct MemorySnapshotInfo { 26 pid_t pid = 0; 27 size_t cpuMemory = 0; 28 size_t gpuMemory = 0; 29 TotalMemoryMemorySnapshotInfo30 size_t TotalMemory() const 31 { 32 return cpuMemory + gpuMemory; 33 } 34 }; 35 36 using MemoryOverflowCalllback = std::function<void(pid_t, uint64_t, bool)>; 37 class RSB_EXPORT MemorySnapshot { 38 public: 39 static MemorySnapshot& Instance(); 40 void AddCpuMemory(const pid_t pid, const size_t size); 41 void RemoveCpuMemory(const pid_t pid, const size_t size); 42 bool GetMemorySnapshotInfoByPid(const pid_t pid, MemorySnapshotInfo& info); 43 void EraseSnapshotInfoByPid(const std::set<pid_t>& exitedPidSet); 44 void UpdateGpuMemoryInfo(const std::unordered_map<pid_t, size_t>& gpuInfo, 45 std::unordered_map<pid_t, MemorySnapshotInfo>& pidForReport, bool& isTotalOver); 46 void InitMemoryLimit(MemoryOverflowCalllback callback, uint64_t warning, uint64_t overflow, uint64_t totalSize); 47 void GetMemorySnapshot(std::unordered_map<pid_t, MemorySnapshotInfo>& map); 48 size_t GetTotalMemory(); 49 void PrintMemorySnapshotToHilog(); 50 private: 51 MemorySnapshot() = default; 52 ~MemorySnapshot() = default; 53 MemorySnapshot(const MemorySnapshot&) = delete; 54 MemorySnapshot(const MemorySnapshot&&) = delete; 55 MemorySnapshot& operator=(const MemorySnapshot&) = delete; 56 MemorySnapshot& operator=(const MemorySnapshot&&) = delete; 57 58 void FindMaxValues(std::vector<MemorySnapshotInfo>& memorySnapshotsList, 59 size_t& maxCpu, size_t& maxGpu, size_t& maxSum); 60 float CalculateRiskScore(const MemorySnapshotInfo memorySnapshotInfo, size_t maxCpu, size_t maxGpu, size_t maxSum); 61 62 std::mutex mutex_; 63 std::unordered_map<pid_t, MemorySnapshotInfo> appMemorySnapshots_; 64 65 uint64_t singleMemoryWarning_ = UINT64_MAX; // warning threshold for total memory of a single process 66 uint64_t singleCpuMemoryLimit_ = UINT64_MAX; // error threshold for cpu memory of a single process 67 uint64_t totalMemoryLimit_ = UINT64_MAX; // error threshold for total memory of all process 68 size_t totalMemory_ = 0; // record the total memory of all processes 69 MemoryOverflowCalllback callback_ = nullptr; 70 int64_t memorySnapshotHilogTime_ = 0; 71 }; 72 } // namespace OHOS 73 } // namespace Rosen 74 #endif