• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GNU_SOURCE
17 #define GNU_SOURCE
18 #endif
19 
20 #include "qos_interface.h"
21 
22 #include <cerrno>
23 #include <cstdio>
24 #include <fcntl.h>
25 #include <unistd.h>
26 
27 #include <sys/ioctl.h>
28 
29 #include "concurrent_task_log.h"
30 
31 #if !defined(CROSS_PLATFORM)
32 #include "concurrent_task_utils.h"
33 #endif
34 
TrivalOpenRtgNode(void)35 static int TrivalOpenRtgNode(void)
36 {
37     char fileName[] = "/proc/self/sched_rtg_ctrl";
38     int fd = open(fileName, O_RDWR);
39     if (fd < 0) {
40         CONCUR_LOGE("[Interface] task %{public}d belong to user %{public}d open rtg node failed, errno = %{public}d",
41                     getpid(), getuid(), errno);
42     }
43     return fd;
44 }
45 
TrivalOpenQosCtrlNode(void)46 static int TrivalOpenQosCtrlNode(void)
47 {
48     char fileName[] = "/proc/thread-self/sched_qos_ctrl";
49     int fd = open(fileName, O_RDWR);
50     if (fd < 0) {
51         CONCUR_LOGE("[Interface] task %{public}d belong to user %{public}d open qos node failed, errno = %{public}d",
52                     getpid(), getuid(), errno);
53     }
54     return fd;
55 }
56 
EnableRtg(bool flag)57 int EnableRtg(bool flag)
58 {
59     char configStr[] = "load_freq_switch:1;sched_cycle:1;frame_max_util:1024";
60     struct RtgEnableData enableData;
61     enableData.enable = flag;
62     enableData.len = sizeof(configStr);
63     enableData.data = configStr;
64     int fd = TrivalOpenRtgNode();
65     if (fd < 0) {
66         return fd;
67     }
68 #if !defined(CROSS_PLATFORM)
69     fdsan_exchange_owner_tag(fd, 0, GetAddrTag(static_cast<void*>(&fd)));
70 #endif
71 
72     int ret = ioctl(fd, CMD_ID_SET_ENABLE, &enableData);
73     if (ret < 0) {
74         CONCUR_LOGE("set rtg config enable failed.");
75     }
76 #if !defined(CROSS_PLATFORM)
77     fdsan_close_with_tag(fd, GetAddrTag(static_cast<void*>(&fd)));
78 #else
79     close(fd);
80 #endif
81 
82     return 0;
83 };
84 
QosApply(unsigned int level)85 int QosApply(unsigned int level)
86 {
87     int tid = gettid();
88     int ret = QosApplyForOther(level, tid);
89     return ret;
90 }
91 
QosApplyForOther(unsigned int level,int tid)92 int QosApplyForOther(unsigned int level, int tid)
93 {
94     int fd = TrivalOpenQosCtrlNode();
95     if (fd < 0) {
96         return fd;
97     }
98 #if !defined(CROSS_PLATFORM)
99     fdsan_exchange_owner_tag(fd, 0, GetAddrTag(static_cast<void*>(&fd)));
100 #endif
101 
102     struct QosCtrlData data;
103     data.level = level;
104     data.type = static_cast<unsigned int>(QosManipulateType::QOS_APPLY);
105     data.pid = tid;
106 
107     int ret = ioctl(fd, QOS_CTRL_BASIC_OPERATION, &data);
108     if (ret < 0) {
109         CONCUR_LOGE("[Interface] task %{public}d apply qos failed, errno = %{public}d", tid, errno);
110     }
111 #if !defined(CROSS_PLATFORM)
112     fdsan_close_with_tag(fd, GetAddrTag(static_cast<void*>(&fd)));
113 #else
114     close(fd);
115 #endif
116     return ret;
117 }
118 
QosLeave(void)119 int QosLeave(void)
120 {
121     int fd = TrivalOpenQosCtrlNode();
122     if (fd < 0) {
123         return fd;
124     }
125 #if !defined(CROSS_PLATFORM)
126     fdsan_exchange_owner_tag(fd, 0, GetAddrTag(static_cast<void*>(&fd)));
127 #endif
128 
129     struct QosCtrlData data;
130     data.type = static_cast<unsigned int>(QosManipulateType::QOS_LEAVE);
131     data.pid = gettid();
132 
133     int ret = ioctl(fd, QOS_CTRL_BASIC_OPERATION, &data);
134     if (ret < 0) {
135         CONCUR_LOGE("[Interface] task %{public}d leave qos failed, errno = %{public}d", gettid(), errno);
136     }
137 #if !defined(CROSS_PLATFORM)
138     fdsan_close_with_tag(fd, GetAddrTag(static_cast<void*>(&fd)));
139 #else
140     close(fd);
141 #endif
142     return ret;
143 }
144 
QosLeaveForOther(int tid)145 int QosLeaveForOther(int tid)
146 {
147     int fd = TrivalOpenQosCtrlNode();
148     if (fd < 0) {
149         return fd;
150     }
151 #if !defined(CROSS_PLATFORM)
152     fdsan_exchange_owner_tag(fd, 0, GetAddrTag(static_cast<void*>(&fd)));
153 #endif
154 
155     struct QosCtrlData data;
156     data.type = static_cast<unsigned int>(QosManipulateType::QOS_LEAVE);
157     data.pid = tid;
158 
159     int ret = ioctl(fd, QOS_CTRL_BASIC_OPERATION, &data);
160     if (ret < 0) {
161         CONCUR_LOGE("[Interface] task %{public}d leave qos failed, errno = %{public}d", tid, errno);
162     }
163 #if !defined(CROSS_PLATFORM)
164     fdsan_close_with_tag(fd, GetAddrTag(static_cast<void*>(&fd)));
165 #else
166     close(fd);
167 #endif
168     return ret;
169 }
170 
QosPolicySet(const struct QosPolicyDatas * policyDatas)171 int QosPolicySet(const struct QosPolicyDatas* policyDatas)
172 {
173     int fd = TrivalOpenQosCtrlNode();
174     if (fd < 0) {
175         return fd;
176     }
177 #if !defined(CROSS_PLATFORM)
178     fdsan_exchange_owner_tag(fd, 0, GetAddrTag(static_cast<void*>(&fd)));
179 #endif
180 
181     int ret = ioctl(fd, QOS_CTRL_POLICY_OPERATION, policyDatas);
182     if (ret < 0) {
183         CONCUR_LOGE("[Interface] set qos policy failed, errno = %{public}d", errno);
184     }
185 #if !defined(CROSS_PLATFORM)
186     fdsan_close_with_tag(fd, GetAddrTag(static_cast<void*>(&fd)));
187 #else
188     close(fd);
189 #endif
190     return ret;
191 }
192 
QosGet(int & level)193 int QosGet(int& level)
194 {
195     int tid = gettid();
196     return QosGetForOther(tid, level);
197 }
198 
QosGetForOther(int tid,int & level)199 int QosGetForOther(int tid, int& level)
200 {
201     int fd = TrivalOpenQosCtrlNode();
202     if (fd < 0) {
203         return fd;
204     }
205 #if !defined(CROSS_PLATFORM)
206     fdsan_exchange_owner_tag(fd, 0, GetAddrTag(static_cast<void*>(&fd)));
207 #endif
208 
209     struct QosCtrlData data;
210     data.type = static_cast<unsigned int>(QosManipulateType::QOS_GET);
211     data.pid = tid;
212     data.qos = -1;
213 
214     int ret = ioctl(fd, QOS_CTRL_BASIC_OPERATION, &data);
215     if (ret < 0) {
216         CONCUR_LOGE("[Interface] task %{public}d get qos failed, errno = %{public}d", tid, errno);
217     }
218     level = data.qos;
219 #if !defined(CROSS_PLATFORM)
220     fdsan_close_with_tag(fd, GetAddrTag(static_cast<void*>(&fd)));
221 #else
222     close(fd);
223 #endif
224     return ret;
225 }