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 PMQOS_NETWORK_LATENCY_CONTROLLER_H 17 #define PMQOS_NETWORK_LATENCY_CONTROLLER_H 18 19 #include <cerrno> 20 #include <cstddef> 21 #include <optional> 22 #include <string> 23 #include <string_view> 24 #include <utility> 25 26 #include <fcntl.h> 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 #include <unistd.h> 30 31 #include <unique_fd.h> 32 33 #include "inetwork_latency_switcher.h" 34 #include "res_sched_log.h" 35 36 namespace OHOS::ResourceSchedule { 37 class PmqosNetworkLatencySwitcher : public INetworkLatencySwitcher { 38 public: 39 static inline std::string_view PMQOS_PATH = "/dev/network_latency"; 40 LowLatencyOn()41 virtual void LowLatencyOn() override 42 { 43 if (pmqosFd) { 44 RESSCHED_LOGE("%{public}s: fd is already opened", __func__); 45 return; 46 } 47 48 UniqueFd fd(open(PMQOS_PATH.data(), O_WRONLY)); 49 if (fd == -1) { 50 RESSCHED_LOGE("%{public}s: cannot open device file: %{public}d", __func__, errno); 51 return; 52 } 53 54 ssize_t ret = write(fd, &PMQOS_LOW_LATENCY_VALUE, sizeof(PMQOS_LOW_LATENCY_VALUE)); 55 if (ret == -1) { 56 RESSCHED_LOGE("%{public}s: cannot write to device: %{public}d", __func__, errno); 57 return; 58 } 59 60 pmqosFd = std::move(fd); 61 RESSCHED_LOGD("%{public}s: pmqos activated", __func__); 62 } 63 LowLatencyOff()64 virtual void LowLatencyOff() override 65 { 66 if (!pmqosFd) { 67 RESSCHED_LOGE("%{public}s: fd is already closed", __func__); 68 return; 69 } 70 71 pmqosFd.reset(); 72 RESSCHED_LOGD("%{public}s: pmqos deactivated", __func__); 73 } 74 private: 75 static inline int32_t PMQOS_LOW_LATENCY_VALUE = 0; 76 std::optional<UniqueFd> pmqosFd; 77 }; 78 } // namespace OHOS::ResourceSchedule 79 80 #endif // PMQOS_NETWORK_LATENCY_CONTROLLER_H 81