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 #include <memory>
17 #include <string>
18 #include <utility>
19
20 #include <unistd.h>
21
22 #include "inetwork_latency_switcher.h"
23 #include "noop_network_latency_switcher.h"
24 #include "pmqos_network_latency_switcher.h"
25 #include "res_sched_log.h"
26
27 #include "network_latency_controller.h"
28
29 namespace OHOS::ResourceSchedule {
Init()30 void NetworkLatencyController::Init()
31 {
32 // use PMQoS switch if available
33 int err = access(PmqosNetworkLatencySwitcher::PMQOS_PATH.data(), W_OK);
34 if (!err) {
35 RESSCHED_LOGI("%{public}s: using pmqos latency switcher", __func__);
36 Init(std::make_unique<PmqosNetworkLatencySwitcher>());
37 return;
38 }
39
40 // Another latency switchers can be implemented if required.
41 // If nothing matched, use default object, which is noop switcher.
42 RESSCHED_LOGI("%{public}s: using default latency switcher", __func__);
43 Init(std::make_unique<NoopNetworkLatencySwitcher>());
44 }
45
Init(std::unique_ptr<INetworkLatencySwitcher> sw)46 void NetworkLatencyController::Init(std::unique_ptr<INetworkLatencySwitcher> sw)
47 {
48 switcher = std::move(sw);
49 }
50
HandleRequest(long long value,const std::string & identity)51 void NetworkLatencyController::HandleRequest(long long value, const std::string &identity)
52 {
53 if (identity.empty()) {
54 RESSCHED_LOGW("%{public}s: empty identity provided", __func__);
55 return;
56 }
57
58 switch (value) {
59 case NETWORK_LATENCY_REQUEST_LOW:
60 AddRequest(identity);
61 break;
62 case NETWORK_LATENCY_REQUEST_NORMAL:
63 DelRequest(identity);
64 break;
65 default:
66 RESSCHED_LOGW("%{public}s: invalid value: %{public}lld", __func__, value);
67 return;
68 }
69 }
70
AddRequest(const std::string & identity)71 void NetworkLatencyController::AddRequest(const std::string &identity)
72 {
73 bool wasEmpty = requests.empty();
74
75 if (requests.find(identity) != requests.end()) {
76 RESSCHED_LOGW("%{public}s: identity %{public}s already registered", __func__, identity.c_str());
77 return;
78 }
79
80 requests.insert(identity);
81 if (wasEmpty) {
82 RESSCHED_LOGD("%{public}s: activating low latency", __func__);
83 switcher->LowLatencyOn();
84 }
85 }
86
DelRequest(const std::string & identity)87 void NetworkLatencyController::DelRequest(const std::string &identity)
88 {
89 if (requests.erase(identity) == 0) {
90 RESSCHED_LOGW("%{public}s: identity %{public}s not registered", __func__, identity.c_str());
91 return;
92 }
93
94 if (requests.empty()) {
95 RESSCHED_LOGD("%{public}s: restore normal latency", __func__);
96 switcher->LowLatencyOff();
97 }
98 }
99 } // namespace OHOS::ResourceSchedule
100