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
16 #include "memory/rs_memory_flow_control.h"
17 #include "platform/common/rs_log.h"
18
19 namespace OHOS {
20 namespace Rosen {
Instance()21 MemoryFlowControl& MemoryFlowControl::Instance()
22 {
23 static MemoryFlowControl instance;
24 return instance;
25 }
26
AddAshmemStatistic(pid_t callingPid,uint32_t bufferSize)27 bool MemoryFlowControl::AddAshmemStatistic(pid_t callingPid, uint32_t bufferSize)
28 {
29 if (callingPid == 0) {
30 // skip flow control when callingPid is meaningless
31 return true;
32 }
33 if (bufferSize == 0) {
34 return true;
35 }
36 if (bufferSize > ASHMEM_BUFFER_SIZE_UPPER_BOUND_FOR_EACH_PID) {
37 ROSEN_LOGE("MemoryFlowControl::AddAshmemStatistic reject ashmem buffer size %{public}" PRIu32 " from pid "
38 "%{public}d, ashmem is too large", bufferSize, static_cast<int>(callingPid));
39 return false;
40 }
41 std::lock_guard<std::mutex> lock(pidToAshmemBufferSizeMapMutex_);
42 auto [it, isNewElement] = pidToAshmemBufferSizeMap_.try_emplace(callingPid, bufferSize);
43 if (isNewElement) {
44 return true;
45 }
46 uint32_t availableSize = ASHMEM_BUFFER_SIZE_UPPER_BOUND_FOR_EACH_PID - it->second;
47 if (bufferSize > availableSize) {
48 ROSEN_LOGE("MemoryFlowControl::AddAshmemStatistic reject ashmem buffer size %{public}" PRIu32 " from pid "
49 "%{public}d, available size %{public}" PRIu32, bufferSize, static_cast<int>(callingPid), availableSize);
50 return false;
51 }
52 it->second += bufferSize;
53 return true;
54 }
55
RemoveAshmemStatistic(pid_t callingPid,uint32_t bufferSize)56 void MemoryFlowControl::RemoveAshmemStatistic(pid_t callingPid, uint32_t bufferSize)
57 {
58 if (callingPid == 0) {
59 // skip flow control when callingPid is meaningless
60 return;
61 }
62 if (bufferSize == 0) {
63 return;
64 }
65 std::lock_guard<std::mutex> lock(pidToAshmemBufferSizeMapMutex_);
66 auto it = pidToAshmemBufferSizeMap_.find(callingPid);
67 if (it == pidToAshmemBufferSizeMap_.end()) {
68 return;
69 }
70 if (bufferSize >= it->second) {
71 pidToAshmemBufferSizeMap_.erase(it);
72 return;
73 }
74 it->second -= bufferSize;
75 }
76
CheckOverflowAndCreateInstance(pid_t pid,uint32_t size)77 std::shared_ptr<AshmemFlowControlUnit> AshmemFlowControlUnit::CheckOverflowAndCreateInstance(pid_t pid, uint32_t size)
78 {
79 bool success = MemoryFlowControl::Instance().AddAshmemStatistic(pid, size);
80 if (!success) {
81 return nullptr;
82 }
83 auto instance = std::make_shared<AshmemFlowControlUnit>(pid, size);
84 instance->needStatistic_ = true;
85 return instance;
86 }
87
AshmemFlowControlUnit(pid_t pid,uint32_t size)88 AshmemFlowControlUnit::AshmemFlowControlUnit(pid_t pid, uint32_t size) : callingPid_(pid), bufferSize_(size) {}
89
~AshmemFlowControlUnit()90 AshmemFlowControlUnit::~AshmemFlowControlUnit()
91 {
92 if (!needStatistic_) {
93 return;
94 }
95 MemoryFlowControl::Instance().RemoveAshmemStatistic(callingPid_, bufferSize_);
96 }
97 } // namespace Rosen
98 } // namespace OHOS