1 /*
2 * Copyright (c) 2023 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 "rtg_perf_ctrl.h"
17 #include "dfx/log/ffrt_log_api.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <stdbool.h>
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <errno.h>
29
open_perf_ctrl(void)30 static int open_perf_ctrl(void)
31 {
32 static bool perf_ctrl_available = true;
33 int fd = -1;
34
35 if (!perf_ctrl_available) {
36 return -1;
37 }
38
39 fd = open("/dev/hisi_perf_ctrl", O_RDWR);
40 if (fd < 0) {
41 FFRT_LOGW("open perf_ctrl failed");
42 perf_ctrl_available = false;
43 }
44
45 return fd;
46 }
47
set_task_rtg(pid_t tid,unsigned int grp_id)48 void set_task_rtg(pid_t tid, unsigned int grp_id)
49 {
50 struct rtg_group_task data = {tid, grp_id, 0};
51 int fd = open_perf_ctrl();
52 if (fd < 0) {
53 return;
54 }
55
56 if (ioctl(fd, PERF_CTRL_SET_TASK_RTG, &data)) {
57 FFRT_LOGW("Error set rtg %d,%u. %s", tid, grp_id, strerror(errno));
58 close(fd);
59 return;
60 }
61 close(fd);
62 }
63
set_rtg_status(unsigned long long status)64 void set_rtg_status(unsigned long long status)
65 {
66 int fd = open_perf_ctrl();
67 if (fd < 0) {
68 return;
69 }
70
71 if (ioctl(fd, PERF_CTRL_SET_FRAME_STATUS, &status)) {
72 FFRT_LOGW("Error set rtg status=%llu. %s", status, strerror(errno));
73 close(fd);
74 return;
75 }
76 close(fd);
77 }
78
set_rtg_qos(int qos)79 void set_rtg_qos(int qos) // MHZ
80 {
81 int fd = open_perf_ctrl();
82 if (fd < 0) {
83 return;
84 }
85
86 if (ioctl(fd, PERF_CTRL_SET_FRAME_RATE, &qos)) {
87 FFRT_LOGW("Error set rtg qos=%d. %s", qos, strerror(errno));
88 close(fd);
89 return;
90 }
91 close(fd);
92 }
93
set_rtg_load_mode(unsigned int grp_id,bool util_enabled,bool freq_enabled)94 void set_rtg_load_mode(unsigned int grp_id, bool util_enabled, bool freq_enabled)
95 {
96 struct rtg_load_mode load_mode;
97
98 memset(&load_mode, 0, sizeof(struct rtg_load_mode));
99 load_mode.grp_id = grp_id;
100 load_mode.util_enabled = !!util_enabled;
101 load_mode.freq_enabled = !!freq_enabled;
102
103 int fd = open_perf_ctrl();
104 if (fd < 0) {
105 return;
106 }
107
108 if (ioctl(fd, PERF_CTRL_SET_RTG_LOAD_MODE, &load_mode)) {
109 FFRT_LOGW("Error set rtg load_mode %d:%d/%d. %s", load_mode.grp_id, load_mode.util_enabled,
110 load_mode.freq_enabled, strerror(errno));
111 close(fd);
112 return;
113 }
114 close(fd);
115 }
116
set_task_min_util(pid_t tid,unsigned int util)117 void set_task_min_util(pid_t tid, unsigned int util)
118 {
119 struct task_config cfg = {tid, util};
120 int fd = open_perf_ctrl();
121 if (fd < 0) {
122 return;
123 }
124
125 if (ioctl(fd, PERF_CTRL_SET_TASK_MIN_UTIL, &cfg)) {
126 FFRT_LOGW("Error set min util %d,%u. %s", tid, util, strerror(errno));
127 close(fd);
128 return;
129 }
130 close(fd);
131 }
132