forked from projectceladon/external-mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintel_check.c
More file actions
326 lines (288 loc) · 8.1 KB
/
intel_check.c
File metadata and controls
326 lines (288 loc) · 8.1 KB
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
#include <cutils/log.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <stdbool.h>
#include <string.h>
#include <cutils/properties.h>
#include "util/macros.h"
#include "util/log.h"
#include "drm-uapi/i915_drm.h"
#include "intel_check.h"
#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif
#ifndef PROPERTY_VALUE_MAX
#define PROPERTY_VALUE_MAX 92
#endif
#define MAX_LINE_LENGTH 1024
#define EXTERNAL_DGPU_RENDER_CMDLINE_LIST_PATH "/data/dgpu-render-list.txt"
static int get_full_command(pid_t pid, char *task_name, int task_name_size) {
char path[64];
snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
int ret = 0;
FILE *file = fopen(path, "r");
if (!file) {
ALOGE("Failed to open file %s\n", path);
return -1;
}
size_t n = fread(task_name, 1, task_name_size - 1, file);
if (n > 0) {
task_name[n] = '\0';
} else {
ALOGE("cannot read command for process %d\n", pid);
ret = -1;
}
fclose(file);
return ret;
}
static int
get_pid_name(pid_t pid, char *task_name, int size)
{
char process_path[BUF_SIZE];
char name_buf[BUF_SIZE];
int ret = 0;
snprintf(process_path, sizeof(process_path), "/proc/%d/status", pid);
FILE* fp = fopen(process_path, "r");
if (fp == NULL) {
mesa_loge("failed to open %s\n", process_path);
return -1;
}
if (fgets(name_buf, size - 1, fp)) {
if (sscanf(name_buf, "%*s %s", task_name) != 1) {
mesa_loge("failed to parse program name %s\n", name_buf);
ret = -1;
}
} else {
mesa_loge("get pid name fail\n");
fclose(fp);
ret = -1;
}
fclose(fp);
return ret;
}
static bool
use_dgpu_render(char *target)
{
char dGPU_prop[BUF_SIZE];
char vendor_buf[PROPERTY_VALUE_MAX];
snprintf(dGPU_prop, sizeof(dGPU_prop), "persist.vendor.intel.dGPUwSys%s", target);
if (property_get(dGPU_prop, vendor_buf, NULL) > 0) {
if (vendor_buf[0] == '1') {
return true;
}
}
char dGPU_prop1[BUF_SIZE];
char vendor_buf1[PROPERTY_VALUE_MAX];
snprintf(dGPU_prop1, sizeof(dGPU_prop1), "persist.vendor.intel.dGPUwLocal%s", target);
if (property_get(dGPU_prop1, vendor_buf1, NULL) > 0) {
if (vendor_buf1[0] == '1') {
return true;
}
}
return false;
}
// Function to read a file and return an array of strings
static char **
read_file_lines(const char *filename, int *num_lines) {
FILE *file = fopen(filename, "r");
if (!file) {
*num_lines = 0;
return NULL;
}
char **lines = NULL;
char buffer[MAX_LINE_LENGTH];
*num_lines = 0;
while (fgets(buffer, MAX_LINE_LENGTH, file)) {
// Remove trailing newline
size_t len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
}
// Allocate memory for a new line
char *line = strdup(buffer);
if (!line) {
fclose(file);
return NULL;
}
// Expand the array and add the new line
char **temp = realloc(lines, (*num_lines + 1) * sizeof(char *));
if (!temp) {
free(line);
fclose(file);
return NULL;
}
lines = temp;
lines[*num_lines] = line;
(*num_lines)++;
}
fclose(file);
return lines;
}
static void
free_lines(char **lines, int num_lines) {
for (int i = 0; i < num_lines; i++) {
free(lines[i]);
}
free(lines);
}
static const char *builtin_dgpu_render_cmdline_list[] = {
"com.chery.ivi.vds.engine3d:service",
"com.chery.ivi.vds.engine3dcube:service",
"com.desaysv.ivi.launcher",
};
static bool
is_cmdline_in_dgpu_render_list(const char *cmdline)
{
char **external_dgpu_render_cmdline_list = NULL;
int num_lines;
for (int i = 0; i < ARRAY_SIZE(builtin_dgpu_render_cmdline_list); ++i) {
if (strstr(cmdline, builtin_dgpu_render_cmdline_list[i])) {
mesa_logi("Use dGPU rendering for process %s\n", cmdline);
return true;
}
}
external_dgpu_render_cmdline_list =
read_file_lines(EXTERNAL_DGPU_RENDER_CMDLINE_LIST_PATH, &num_lines);
for (int i = 0; i < num_lines; ++i) {
if (strstr(cmdline, external_dgpu_render_cmdline_list[i])) {
mesa_logi("Use dGPU rendering for process %s\n", cmdline);
return true;
}
}
free_lines(external_dgpu_render_cmdline_list, num_lines);
return false;
}
static bool
is_target_process(const char *target)
{
static const char *str_char[] = {
"k.auto:transfer",
"android.vending",
"k.auto:refinery",
".fisheye",
"mark.auto:video",
NULL
};
const char **ptr = str_char;
// Prefer dGPU for compositing in surfaceflinger since dGPU covers more
// scenarios than iGPU.
if (!strcmp(target, "surfaceflinger"))
return true;
for (ptr = str_char; *ptr != NULL; ptr++) {
if (!strcmp(target, *ptr)) {
char vendor_buf[PROPERTY_VALUE_MAX];
char vendor_buf1[PROPERTY_VALUE_MAX];
if (property_get("persist.vendor.intel.dGPUwSys.auto",
vendor_buf, NULL) > 0) {
if (vendor_buf[0] == '1') {
return true;
}
}
if (property_get("persist.vendor.intel.dGPUwLocal.auto",
vendor_buf1, NULL) > 0) {
if (vendor_buf1[0] == '1') {
return true;
}
}
}
}
return false;
}
bool intel_is_dgpu_render(void)
{
pid_t process_id = getpid();
char process_name[BUF_SIZE] = {0};
if (get_pid_name(process_id, process_name, sizeof(process_name)) < 0) {
return false;
}
char *app_name = strrchr(process_name, '.');
if (app_name == NULL)
app_name = process_name;
if (use_dgpu_render(app_name) || is_target_process(process_name)) {
return true;
}
if (get_full_command(process_id, process_name, sizeof(process_name)) < 0) {
return false;
}
return is_cmdline_in_dgpu_render_list(process_name);
}
bool intel_lower_ctx_priority(void)
{
pid_t process_id = getpid();
char process_name[BUF_SIZE] = {0};
if (get_pid_name(process_id, process_name, sizeof(process_name)) < 0) {
return false;
}
char *app_name = strrchr(process_name, '.');
if (app_name == NULL)
app_name = process_name;
char lower_pri[BUF_SIZE];
char vendor_buf[PROPERTY_VALUE_MAX];
snprintf(lower_pri, sizeof(lower_pri), "persist.vendor.intel.lowPir%s", app_name);
if (property_get(lower_pri, vendor_buf, NULL) > 0) {
if (vendor_buf[0] == '1') {
return true;
}
}
return false;
}
static bool has_local_mem(int fd)
{
struct drm_i915_query_item item = {
.query_id = DRM_I915_QUERY_MEMORY_REGIONS,
};
struct drm_i915_query query = {
.num_items = 1, .items_ptr = (uintptr_t)&item,
};
if (drmIoctl(fd, DRM_IOCTL_I915_QUERY, &query)) {
mesa_loge("ioctl fail\n");
return false;
}
struct drm_i915_query_memory_regions *meminfo = calloc(1, item.length);
if (!meminfo) {
mesa_loge("memory fail\n");
return false;
}
item.data_ptr = (uintptr_t)meminfo;
if (drmIoctl(fd, DRM_IOCTL_I915_QUERY, &query) || item.length <= 0) {
free(meminfo);
mesa_loge("item.length <= 0\n");
return false;
}
uint64_t lmem_regions = 0;
for (uint32_t i = 0; i < meminfo->num_regions; i++) {
const struct drm_i915_memory_region_info *mem = &meminfo->regions[i];
if (mem->region.memory_class == I915_MEMORY_CLASS_DEVICE)
lmem_regions += 1;
}
free(meminfo);
return lmem_regions > 0;
}
bool is_dgpu(int fd)
{
return has_local_mem(fd);
}
int get_intel_node_num(void)
{
drmDevicePtr devices[8];
int num_devs;
num_devs = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
int intel_node_num = 0;
for (int i = 0; i < num_devs; i++) {
if (!(devices[i]->available_nodes & (1 << DRM_NODE_RENDER)) ||
devices[i]->bustype != DRM_BUS_PCI ||
devices[i]->deviceinfo.pci->vendor_id != 0x8086) {
continue;
}
else {
++intel_node_num;
}
}
return intel_node_num;
}