-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
385 lines (316 loc) · 11.6 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright (c) 2012-2013,2015-2018, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define LOG_NIDEBUG 0
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "hint-data.h"
#include "list.h"
#include "power-common.h"
#include "utils.h"
#define LOG_TAG "QTI PowerHAL"
#include <log/log.h>
#define USINSEC 1000000L
#define NSINUS 1000L
#define SOC_ID_0 "/sys/devices/soc0/soc_id"
#define SOC_ID_1 "/sys/devices/system/soc/soc0/id"
const char* scaling_gov_path[8] = {"/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor",
"/sys/devices/system/cpu/cpu1/cpufreq/scaling_governor",
"/sys/devices/system/cpu/cpu2/cpufreq/scaling_governor",
"/sys/devices/system/cpu/cpu3/cpufreq/scaling_governor",
"/sys/devices/system/cpu/cpu4/cpufreq/scaling_governor",
"/sys/devices/system/cpu/cpu5/cpufreq/scaling_governor",
"/sys/devices/system/cpu/cpu6/cpufreq/scaling_governor",
"/sys/devices/system/cpu/cpu7/cpufreq/scaling_governor"};
#define PERF_HAL_PATH "libqti-perfd-client.so"
static void* qcopt_handle;
static int (*perf_lock_acq)(int handle, int duration, int list[], int numArgs);
static int (*perf_lock_rel)(int handle);
static int (*perf_hint)(int, const char*, int, int);
static struct list_node active_hint_list_head;
const char* pkg = "QTI PowerHAL";
static void* get_qcopt_handle() {
char qcopt_lib_path[PATH_MAX] = {0};
void* handle = NULL;
dlerror();
if (property_get("ro.vendor.extension_library", qcopt_lib_path, NULL)) {
handle = dlopen(qcopt_lib_path, RTLD_NOW);
if (!handle) {
ALOGE("Unable to open %s: %s\n", qcopt_lib_path, dlerror());
}
}
if (!handle) {
handle = dlopen(PERF_HAL_PATH, RTLD_NOW);
if (!handle) {
ALOGE("Unable to open %s: %s\n", PERF_HAL_PATH, dlerror());
}
}
return handle;
}
static void __attribute__((constructor)) initialize(void) {
qcopt_handle = get_qcopt_handle();
if (!qcopt_handle) {
ALOGE("Failed to get qcopt handle.\n");
} else {
/*
* qc-opt handle obtained. Get the perflock acquire/release
* function pointers.
*/
perf_lock_acq = dlsym(qcopt_handle, "perf_lock_acq");
if (!perf_lock_acq) {
ALOGE("Unable to get perf_lock_acq function handle.\n");
}
perf_lock_rel = dlsym(qcopt_handle, "perf_lock_rel");
if (!perf_lock_rel) {
ALOGE("Unable to get perf_lock_rel function handle.\n");
}
perf_hint = dlsym(qcopt_handle, "perf_hint");
if (!perf_hint) {
ALOGE("Unable to get perf_hint function handle.\n");
}
}
}
static void __attribute__((destructor)) cleanup(void) {
if (qcopt_handle) {
if (dlclose(qcopt_handle)) ALOGE("Error occurred while closing qc-opt library.");
}
}
int sysfs_read(const char* path, char* s, int num_bytes) {
char buf[80];
int count;
int ret = 0;
int fd = open(path, O_RDONLY);
if (fd < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error opening %s: %s\n", path, buf);
return -1;
}
if ((count = read(fd, s, num_bytes - 1)) < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error writing to %s: %s\n", path, buf);
ret = -1;
} else {
s[count] = '\0';
}
close(fd);
return ret;
}
int sysfs_write(const char* path, char* s) {
char buf[80];
int len;
int ret = 0;
int fd = open(path, O_WRONLY);
if (fd < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error opening %s: %s\n", path, buf);
return -1;
}
len = write(fd, s, strlen(s));
if (len < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error writing to %s: %s\n", path, buf);
ret = -1;
}
close(fd);
return ret;
}
int get_scaling_governor(char governor[], int size) {
for (size_t i = 0; i < ARRAY_SIZE(scaling_gov_path); i++) {
if (get_scaling_governor_check_cores(governor, size, i) == 0) {
// Obtained the scaling governor. Return.
return 0;
}
}
return -1;
}
int get_scaling_governor_check_cores(char governor[], int size, int core_num) {
if (sysfs_read(scaling_gov_path[core_num], governor, size) == -1) {
// Can't obtain the scaling governor. Return.
return -1;
}
// Strip newline at the end.
int len = strlen(governor);
len--;
while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r')) governor[len--] = '\0';
return 0;
}
int is_interactive_governor(char* governor) {
if (strncmp(governor, INTERACTIVE_GOVERNOR, (strlen(INTERACTIVE_GOVERNOR) + 1)) == 0) return 1;
return 0;
}
int is_schedutil_governor(char* governor) {
if (strncmp(governor, SCHEDUTIL_GOVERNOR, (strlen(SCHEDUTIL_GOVERNOR) + 1)) == 0) return 1;
return 0;
}
#ifndef INTERACTION_BOOST
void interaction(int duration, int num_args, int opt_list[]) {
#else
void interaction(int duration, int num_args, int opt_list[]) {
static int lock_handle = 0;
if (duration < 0 || num_args < 1 || opt_list[0] == 0) return;
if (qcopt_handle) {
if (perf_lock_acq) {
lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
if (lock_handle == -1) ALOGE("Failed to acquire lock.");
}
}
#endif
}
int interaction_with_handle(int lock_handle, int duration, int num_args, int opt_list[]) {
if (duration < 0 || num_args < 1 || opt_list[0] == 0) return 0;
if (qcopt_handle) {
if (perf_lock_acq) {
lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
if (lock_handle == -1) ALOGE("Failed to acquire lock.");
}
}
return lock_handle;
}
// this is interaction_with_handle using perf_hint instead of
// perf_lock_acq
int perf_hint_enable(int hint_id, int duration) {
int lock_handle = 0;
if (duration < 0) return 0;
if (qcopt_handle) {
if (perf_hint) {
lock_handle = perf_hint(hint_id, pkg, duration, -1);
if (lock_handle == -1) ALOGE("Failed to acquire lock for hint_id: %X.", hint_id);
}
}
return lock_handle;
}
// Same as perf_hint_enable, but with the ability to
// choose the type
int perf_hint_enable_with_type(int hint_id, int duration, int type) {
int lock_handle = 0;
if (qcopt_handle) {
if (perf_hint) {
lock_handle = perf_hint(hint_id, NULL, duration, type);
if (lock_handle == -1) ALOGE("Failed to acquire lock.");
}
}
return lock_handle;
}
void release_request(int lock_handle) {
if (qcopt_handle && perf_lock_rel) perf_lock_rel(lock_handle);
}
int perform_hint_action(int hint_id, int resource_values[], int num_resources) {
if (qcopt_handle && perf_lock_acq) {
/* Acquire an indefinite lock for the requested resources. */
int lock_handle = perf_lock_acq(0, 0, resource_values, num_resources);
if (lock_handle == -1) {
ALOGE("Failed to acquire lock.");
return -EINVAL;
}
/* Add this handle to our internal hint-list. */
struct hint_data* new_hint = (struct hint_data*)malloc(sizeof(struct hint_data));
if (!new_hint) {
/* Can't keep track of this lock. Release it. */
if (perf_lock_rel) perf_lock_rel(lock_handle);
ALOGE("Failed to process hint.");
return -ENOMEM;
}
if (!active_hint_list_head.compare) {
active_hint_list_head.compare = (int (*)(void*, void*))hint_compare;
active_hint_list_head.dump = (void (*)(void*))hint_dump;
}
new_hint->hint_id = hint_id;
new_hint->perflock_handle = lock_handle;
if (add_list_node(&active_hint_list_head, new_hint) == NULL) {
free(new_hint);
/* Can't keep track of this lock. Release it. */
if (perf_lock_rel) perf_lock_rel(lock_handle);
ALOGE("Failed to process hint.");
return -ENOMEM;
}
}
return 0;
}
void undo_hint_action(int hint_id) {
if (qcopt_handle) {
if (perf_lock_rel) {
/* Get hint-data associated with this hint-id */
struct list_node* found_node;
struct hint_data temp_hint_data = {.hint_id = hint_id};
found_node = find_node(&active_hint_list_head, &temp_hint_data);
if (found_node) {
/* Release this lock. */
struct hint_data* found_hint_data = (struct hint_data*)(found_node->data);
if (found_hint_data) {
if (perf_lock_rel(found_hint_data->perflock_handle) == -1)
ALOGE("Perflock release failed.");
}
if (found_node->data) {
/* We can free the hint-data for this node. */
free(found_node->data);
}
remove_list_node(&active_hint_list_head, found_node);
} else {
ALOGE("Invalid hint ID.");
}
}
}
}
/*
* Used to release initial lock holding
* two cores online when the display is on
*/
void undo_initial_hint_action() {
if (qcopt_handle) {
if (perf_lock_rel) {
perf_lock_rel(1);
}
}
}
int get_soc_id(void) {
int fd;
int soc_id = -1;
char buf[10] = {0};
if (!access(SOC_ID_0, F_OK))
fd = open(SOC_ID_0, O_RDONLY);
else
fd = open(SOC_ID_1, O_RDONLY);
if (fd >= 0) {
if (read(fd, buf, sizeof(buf) - 1) == -1)
ALOGW("Unable to read soc_id");
else
soc_id = atoi(buf);
}
close(fd);
return soc_id;
}
long long calc_timespan_us(struct timespec start, struct timespec end) {
long long diff_in_us = 0;
diff_in_us += (end.tv_sec - start.tv_sec) * USINSEC;
diff_in_us += (end.tv_nsec - start.tv_nsec) / NSINUS;
return diff_in_us;
}