/
qrb5165_info.c
407 lines (321 loc) · 9.5 KB
/
qrb5165_info.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright (c) 2020 Qualcomm Innovation Center, Inc. All Rights Reserved.
*
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <memory.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/sysinfo.h>
#define FILE_CPUINFO "/proc/cpuinfo"
#define FILE_GPUINFO "/sys/devices/platform/soc/soc:qcom,gpubw/devfreq/soc:qcom,gpubw/%s"
#define FILE_IOMEM "/proc/iomem"
#define FILE_FB0 "/dev/fb0"
#define FILE_SUBNAME "/sys/bus/msm_subsys/devices/subsys%d/name"
#define FILE_SUBSTATE "/sys/bus/msm_subsys/devices/subsys%d/state"
#define FILE_HDMISTATE "/sys/class/drm/card0-DSI-1/status"
#define FILE_CAN_FLAGS "/sys/class/net/can0/flags"
#define GPU_GOVERNOR "governor"
#define GPU_CUR_FREQ "cur_freq"
#define GPU_MIN_FREQ "min_freq"
#define GPU_MAX_FREQ "max_freq"
#define NET_UP (1)
#define NET_BROADCAST (1 << 1)
#define NET_LOOPBACK (1 << 3)
#define NET_RUNNING (1 << 6)
#define NET_NOARP (1 << 7)
#define NET_MASTER (1 << 10)
#define NET_MULTICAST (1 << 12)
#define CPUSTART_NUM 12
enum cpufreqtype{
freq_cur,
freq_min,
freq_max,
};
struct io_mem{
void* add_start;
void* add_end;
struct io_mem* next;
char io_name[20];
};
static int getcpuinfo_int(const char* attr_name, int* attr_val)
{
char read_buf[1024] = {0};
char* c_attr_val = NULL;
FILE* fp;
fp = fopen(FILE_CPUINFO, "r");
if (fp == NULL) {
printf("Can not open file: %s\n", FILE_CPUINFO);
return -11;
}
*attr_val = 0;
while (fscanf(fp, "%[^\n]", read_buf) != EOF)
{
if (strstr(read_buf, attr_name)) {
c_attr_val = read_buf + CPUSTART_NUM;
if (!(sscanf(c_attr_val, "%d", attr_val))) {
fclose(fp);
return -1;
}
}
fgetc(fp);
}
fclose(fp);
if (c_attr_val == NULL)
return -2;
return 0;
}
static int getcpufreq(int core_num, enum cpufreqtype type)
{
char cpu_freq_file[64] = {0};
int freq_val;
FILE* fp;
if (type == freq_cur) {
snprintf(cpu_freq_file, sizeof(cpu_freq_file),
"/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", core_num);
} else if (type == freq_min) {
snprintf(cpu_freq_file, sizeof(cpu_freq_file),
"/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", core_num);
} else {
snprintf(cpu_freq_file, sizeof(cpu_freq_file),
"/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", core_num);
}
fp = fopen(cpu_freq_file, "r");
if (fp == NULL) {
printf("Can not open file: %s\n", cpu_freq_file);
return -11;
}
fscanf(fp, "%d", &freq_val);
fclose(fp);
return freq_val;
}
static int getcpumode(int core_num, char* cpu_mode)
{
char cpu_governor_file[64] = {0};
int freq_val;
FILE* fp;
snprintf(cpu_governor_file, sizeof(cpu_governor_file),
"/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", core_num);
fp = fopen(cpu_governor_file, "r");
if (fp == NULL) {
printf("Can not open file: %s\n", cpu_governor_file);
return -11;
}
fscanf(fp, "%s", cpu_mode);
fclose(fp);
return 0;
}
static int getgpuinfo(const char* attr_name, char* attr_info)
{
char gpu_info_file[128] = {0};
FILE* fp;
snprintf(gpu_info_file, sizeof(gpu_info_file), FILE_GPUINFO, attr_name);
fp = fopen(gpu_info_file, "r");
if (fp == NULL) {
printf("Can not open file: %s\n", gpu_info_file);
return -11;
}
fscanf(fp, "%s", attr_info);
fclose(fp);
return 0;
}
static void* get_io_map()
{
char read_buf[64];
char* map_name;
struct io_mem* first_io = NULL;
FILE* fp;
fp = fopen(FILE_IOMEM, "r");
if (fp == NULL) {
printf("Can not open file: %s\n", FILE_IOMEM);
return NULL;
}
first_io = (struct io_mem*)malloc(sizeof(struct io_mem));
memset(first_io, 0, sizeof(struct io_mem));
free(first_io);
while (fscanf(fp, "%[^\n]", read_buf) != EOF)
{
map_name = read_buf;
while (*map_name == ' ')
{
map_name++;
}
printf("%s\n", map_name);
fgetc(fp);
}
fclose(fp);
return NULL;
}
static int getscreeninfo(int* width, int* height, int* bpp)
{
int fd;
struct fb_var_screeninfo fb_var;
fd = open(FILE_FB0, O_RDWR);
if (fd < 0) {
printf("Open File: %s error!\n", FILE_FB0);
return -11;
}
ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
close(fd);
*width = fb_var.xres;
*height = fb_var.yres;
*bpp = fb_var.bits_per_pixel;
return 0;
}
static int getsubsysname(int subsys_num, char* subsys_name)
{
char sub_sys_file[123] = {0};
FILE* fp;
snprintf(sub_sys_file, sizeof(sub_sys_file), FILE_SUBNAME, subsys_num);
fp = fopen(sub_sys_file, "r");
if (fp == NULL) {
printf("Can not open file: %s\n", sub_sys_file);
return -11;
}
fscanf(fp, "%s", subsys_name);
fclose(fp);
return 0;
}
static int getsubsysstate(int subsys_num, char* subsys_state)
{
char sub_sys_file[123] = {0};
FILE* fp;
snprintf(sub_sys_file, sizeof(sub_sys_file), FILE_SUBSTATE, subsys_num);
fp = fopen(sub_sys_file, "r");
if (fp == NULL) {
printf("Can not open file: %s\n", sub_sys_file);
return -11;
}
fscanf(fp, "%s", subsys_state);
fclose(fp);
return 0;
}
static int getHDMIstatus(char* status)
{
FILE* fp;
fp = fopen(FILE_HDMISTATE, "r");
if (fp == NULL) {
printf("Can not open file: %s\n", FILE_HDMISTATE);
return -11;
}
fscanf(fp, "%s", status);
fclose(fp);
return 0;
}
static int getCANflags(int* flags)
{
FILE* fp;
fp = fopen(FILE_CAN_FLAGS, "r");
if (fp == NULL) {
printf("Can not open file: %s\n", FILE_CAN_FLAGS);
return -11;
}
fscanf(fp, "%x", flags);
fclose(fp);
return 0;
}
int main()
{
char hostname[60] = {0};
char cpu_per_mode[16] = {0};
char gpu_info[16] = {0};
char sub_system_name[16] = {0};
char sub_system_state[16] = {0};
char HDMI_status[16] = {0};
int cpu_cores;
int can_flags;
int screen_width, screen_height, screen_bpp;
struct sysinfo si;
int i;
int ret;
ret = gethostname(hostname, sizeof(hostname));
if (ret == 0)
printf("hostname: %s\n", hostname);
printf("\n*Peripheral device information*\n");
printf("USB device connection :\n");
system("lsusb");
ret = getHDMIstatus(HDMI_status);
if (ret == 0)
printf("\nHDMI device connection : %s\n", HDMI_status);
else
printf("HDMI get status error!\n");
ret = getCANflags(&can_flags);
if (ret == 0)
printf("\nCAN device connection : %s\n",
(can_flags & NET_RUNNING) ? "connected" : "disconnected");
else
printf("Get CAN device error!\n");
printf("\n*****Subsystem information*****\n");
for (i = 0; i < 10; i++)
{
ret = getsubsysname(i, sub_system_name);
if (ret == 0) {
printf("%s :", sub_system_name);
ret = getsubsysstate(i, sub_system_name);
if (ret == 0) {
printf(" %s\n", sub_system_name);
} else {
printf("get subsystem state error!\n");
}
} else {
printf("get subsystem name error!\n");
}
}
printf("\n********CPU information********\n");
ret = getcpuinfo_int("processor", &cpu_cores);
if (ret == 0)
printf("CPU cores : %d\n", cpu_cores + 1);
else
printf("CPU cores : error: %d\n", ret);
for (i = 0; i <= cpu_cores; i++)
{
printf("CPU%d current frequency : %d\n", i, getcpufreq(i, freq_cur));
printf("CPU%d MIN frequency : %d\n", i, getcpufreq(i, freq_min));
printf("CPU%d MAX frequency : %d\n", i, getcpufreq(i, freq_max));
ret = getcpumode(cpu_cores, cpu_per_mode);
if (ret == 0)
printf("CPU performance mode : %s\n", cpu_per_mode);
else
printf("CPU performance mode get error!\n");
}
printf("\n********GPU information********\n");
ret = getgpuinfo(GPU_GOVERNOR, gpu_info);
if (ret == 0)
printf("GPU governors : %s\n", gpu_info);
else
printf("GPU governors get error!\n");
ret = getgpuinfo(GPU_CUR_FREQ, gpu_info);
if (ret == 0)
printf("GPU cureent frequency : %s\n", gpu_info);
else
printf("GPU cureent frequency get error!\n");
ret = getgpuinfo(GPU_MIN_FREQ, gpu_info);
if (ret == 0)
printf("GPU min frequency : %s\n", gpu_info);
else
printf("GPU min frequency get error!\n");
ret = getgpuinfo(GPU_MAX_FREQ, gpu_info);
if (ret == 0)
printf("GPU max frequency : %s\n", gpu_info);
else
printf("GPU max frequency get error!\n");
printf("\n******Display information******\n");
ret = getscreeninfo(&screen_width, &screen_height, &screen_bpp);
if (ret == 0)
printf("Screen resolution : %d*%d-%dbpp\n",
screen_width, screen_height, screen_bpp);
else
printf("GPU max frequency get error!\n");
printf("\n*******Memory information******\n");
sysinfo(&si);
printf("Total RAM : %lu\n", si.totalram);
printf("Available RAM : %lu\n", si.freeram);
printf("\n*******IO map information******\n");
get_io_map();
return 0;
}