• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <vector>
23 
24 namespace OHOS {
25 namespace Rosen {
26 struct MemorySnapshotInfo {
27     pid_t pid = 0;
28     int32_t uid;
29     std::string bundleName = "";
30     size_t cpuMemory = 0;
31     size_t gpuMemory = 0;
32     size_t subThreadGpuMemory = 0;
33 
TotalMemoryMemorySnapshotInfo34     size_t TotalMemory() const
35     {
36         return cpuMemory + gpuMemory + subThreadGpuMemory;
37     }
38 };
39 
40 using MemoryOverflowCalllback = std::function<void(pid_t, uint64_t, bool)>;
41 class RSB_EXPORT MemorySnapshot {
42 public:
43     static MemorySnapshot& Instance();
44     void AddCpuMemory(const pid_t pid, const size_t size);
45     void RemoveCpuMemory(const pid_t pid, const size_t size);
46     bool GetMemorySnapshotInfoByPid(const pid_t pid, MemorySnapshotInfo& info);
47     void EraseSnapshotInfoByPid(const std::set<pid_t>& exitedPidSet);
48     void UpdateGpuMemoryInfo(const std::unordered_map<pid_t, size_t>& uniRenderGpuInfo,
49         const std::unordered_map<pid_t, size_t>& subThreadGpuInfo,
50         std::unordered_map<pid_t, MemorySnapshotInfo>& pidForReport, bool& isTotalOver);
51     void InitMemoryLimit(MemoryOverflowCalllback callback, uint64_t warning, uint64_t overflow, uint64_t totalSize);
52     void GetMemorySnapshot(std::unordered_map<pid_t, MemorySnapshotInfo>& map);
53     void GetDirtyMemorySnapshot(std::vector<pid_t>& list);
54     void FillMemorySnapshot(std::unordered_map<pid_t, MemorySnapshotInfo>& infoMap);
55     size_t GetTotalMemory();
56     void PrintMemorySnapshotToHilog();
57     void SetMemSnapshotPrintHilogLimit(int memSnapshotPrintHilogLimit);
58     int GetMemSnapshotPrintHilogLimit();
59 private:
60     MemorySnapshot() = default;
61     ~MemorySnapshot() = default;
62     MemorySnapshot(const MemorySnapshot&) = delete;
63     MemorySnapshot(const MemorySnapshot&&) = delete;
64     MemorySnapshot& operator=(const MemorySnapshot&) = delete;
65     MemorySnapshot& operator=(const MemorySnapshot&&) = delete;
66 
67     void FindMaxValues(std::vector<MemorySnapshotInfo>& memorySnapshotsList,
68         size_t& maxCpu, size_t& maxGpu, size_t& maxSum);
69     float CalculateRiskScore(const MemorySnapshotInfo memorySnapshotInfo, size_t maxCpu, size_t maxGpu, size_t maxSum);
70 
71     std::mutex mutex_;
72     std::unordered_map<pid_t, MemorySnapshotInfo> appMemorySnapshots_;
73     std::vector<pid_t> dirtyMemorySnapshots_;
74 
75     uint64_t singleMemoryWarning_ = UINT64_MAX; // warning threshold for total memory of a single process
76     uint64_t singleCpuMemoryLimit_ = UINT64_MAX; // error threshold for cpu memory of a single process
77     uint64_t totalMemoryLimit_ = UINT64_MAX; // error threshold for total memory of all process
78     size_t totalMemory_ = 0; // record the total memory of all processes
79     MemoryOverflowCalllback callback_ = nullptr;
80     int64_t memorySnapshotHilogTime_ = 0;
81     int memSnapshotPrintHilogLimit_ = 0;
82 };
83 } // namespace OHOS
84 } // namespace Rosen
85 #endif