1 /* 2 * Copyright (C) 2025 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 AVCODEC_INSTANCE_MEMORY_UPDATE_EVENT_HANDLER_H 17 #define AVCODEC_INSTANCE_MEMORY_UPDATE_EVENT_HANDLER_H 18 19 #include <optional> 20 #include <functional> 21 #include <unordered_map> 22 #include <unordered_set> 23 #include <shared_mutex> 24 #include <memory> 25 #include "avcodec_xcollie.h" 26 #include "meta.h" 27 #include "instance_info.h" 28 29 namespace OHOS { 30 namespace MediaAVCodec { 31 using CalculatorType = std::function<uint32_t(uint32_t)>; 32 class InstanceMemoryUpdateEventHandler { 33 public: 34 static InstanceMemoryUpdateEventHandler &GetInstance(); 35 void OnInstanceMemoryUpdate(const Media::Meta &meta); 36 void OnInstanceMemoryReset(const Media::Meta &meta); 37 void OnInstanceRelease(const Media::Meta &meta); 38 void RemoveTimer(pid_t pid); 39 void AddApp2ExceedThresholdList(pid_t pid); 40 41 private: 42 InstanceMemoryUpdateEventHandler(); 43 44 std::optional<CalculatorType> GetCalculator(const Media::Meta &meta); 45 uint32_t GetBlockCount(const Media::Meta &meta); 46 std::optional<InstanceInfo> UpdateInstanceMemory(int32_t instanceId, uint32_t memory); 47 48 void UpdateAppMemoryThreshold(); 49 static uint32_t SumAppMemory(pid_t callerPid, pid_t actualCallerPid); 50 static void ReportAppMemory(pid_t callerPid, pid_t actualCallerPid, bool isInTimer = true, uint32_t memory = 0); 51 void DeterminAppMemoryExceedThresholdAndReport(pid_t callerPid, pid_t forwardCallerPid); 52 53 uint32_t appMemoryThreshold_ = 0; 54 std::shared_mutex timerMutex_; 55 std::shared_mutex appMemoryExceedThresholdListMutex_; 56 std::unordered_map<pid_t, std::shared_ptr<AVCodecXcollieTimer>> timerMap_; 57 std::unordered_set<pid_t> appMemoryExceedThresholdList_; 58 59 private: 60 class ThresholdParser { 61 public: 62 static uint32_t GetThreshold(); 63 }; 64 }; 65 } // namespace MediaAVCodec 66 } // namespace OHOS 67 #endif // AVCODEC_INSTANCE_MEMORY_UPDATE_EVENT_HANDLER_H