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 #ifndef RS_MEMORY_FLOW_CONTROL_H 17 #define RS_MEMORY_FLOW_CONTROL_H 18 19 #include <memory> 20 #include <mutex> 21 #include <unistd.h> 22 #include <unordered_map> 23 24 #include "common/rs_macros.h" 25 #include "nocopyable.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 class RSB_EXPORT MemoryFlowControl { 30 public: 31 static MemoryFlowControl& Instance(); GetAshmemFlowControlThreshold()32 static constexpr uint32_t GetAshmemFlowControlThreshold() 33 { 34 return ASHMEM_BUFFER_SIZE_UPPER_BOUND_FOR_EACH_PID; 35 } 36 bool AddAshmemStatistic(pid_t callingPid, uint32_t bufferSize); 37 void RemoveAshmemStatistic(pid_t callingPid, uint32_t bufferSize); 38 39 private: 40 MemoryFlowControl() = default; 41 ~MemoryFlowControl() = default; 42 DISALLOW_COPY_AND_MOVE(MemoryFlowControl); 43 44 // handle exceptions when a callingPid is submitting >1GB ashmem parcel data to RS simultaneously 45 static constexpr uint32_t ASHMEM_BUFFER_SIZE_UPPER_BOUND_FOR_EACH_PID = 1024 * 1024 * 1024; 46 std::unordered_map<pid_t, uint32_t> pidToAshmemBufferSizeMap_; 47 std::mutex pidToAshmemBufferSizeMapMutex_; 48 }; 49 50 class RSB_EXPORT AshmemFlowControlUnit { 51 public: 52 static std::shared_ptr<AshmemFlowControlUnit> CheckOverflowAndCreateInstance(pid_t pid, uint32_t size); 53 AshmemFlowControlUnit(pid_t pid, uint32_t size); 54 ~AshmemFlowControlUnit(); 55 56 private: 57 AshmemFlowControlUnit() = delete; 58 DISALLOW_COPY_AND_MOVE(AshmemFlowControlUnit); 59 60 const pid_t callingPid_; 61 const uint32_t bufferSize_; 62 bool needStatistic_ = false; 63 }; 64 } // namespace OHOS 65 } // namespace Rosen 66 67 #endif // RS_MEMORY_FLOW_CONTROL_H