1 /* 2 * Copyright (c) 2022 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 NETWORK_LATENCY_CONTROLLER_H 17 #define NETWORK_LATENCY_CONTROLLER_H 18 19 #include <chrono> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <utility> 24 #include <unordered_set> 25 26 #include "ffrt.h" 27 28 #include "inetwork_latency_switcher.h" 29 30 namespace OHOS::ResourceSchedule { 31 class NetworkLatencyController { 32 public: 33 void Init(); 34 void Init(std::unique_ptr<INetworkLatencySwitcher> switcher); 35 void HandleRequest(long long value, const std::string &identity); 36 37 static const long long NETWORK_LATENCY_REQUEST_LOW = 0; 38 static const long long NETWORK_LATENCY_REQUEST_NORMAL = 1; 39 40 private: 41 void HandleAddRequest(const std::string &identity); 42 void HandleDelRequest(const std::string &identity); 43 void AddRequest(const std::string &identity); 44 void DelRequest(const std::string &identity); 45 void AutoDisableTask(const std::string &identity); 46 47 std::mutex mtx; 48 std::shared_ptr<ffrt::queue> ffrtQueue_{nullptr}; 49 ffrt::mutex latencyFfrtMutex_; 50 int32_t switchToFfrt_ = 1000; 51 std::unique_ptr<INetworkLatencySwitcher> switcher; 52 std::unordered_set<std::string> requests; 53 std::unordered_map<std::string, ffrt::task_handle> taskHandlerMap_; 54 }; 55 } // namespace OHOS::ResourceSchedule 56 57 #endif // NETWORK_LATENCY_CONTROLLER_H 58