-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsysdetect.c
More file actions
583 lines (470 loc) · 14.9 KB
/
sysdetect.c
File metadata and controls
583 lines (470 loc) · 14.9 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <cpuid.h>
#include <sys/sysinfo.h>
#include <sched.h>
#include <time.h>
#include <stdlib.h>
#include "log.h"
#include "sysdetect.h"
#define TAG "SYSDETECT"
// Function to determine if a cpu core is an atom e-core type or not.
// Arguments: Cpu core's id number.
// Returns core_value. If 0x20, then core is an e-core.
static int get_core_type(int core_number)
{
cpu_set_t cpu_set;
cpu_set_t original_set;
// Get current cpu affinity.
if (sched_getaffinity(0, sizeof(cpu_set_t), &original_set) == -1) {
loge(TAG, "Error getting current CPU affinity\n");
return -1;
}
// Reset & init cpu affinity.
CPU_ZERO(&cpu_set);
CPU_SET(core_number, &cpu_set);
// Set cpu affinity for the next task.
if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) == -1) {
loge(TAG, "Error setting CPU affinity\n");
return -2;
}
// Get core type.
unsigned int values[4];
unsigned int leaf = 0x1a;
unsigned int subleaf = 0;
__cpuid_count(leaf, subleaf, values[0], values[1],
values[2], values[3]);
// Get core value. If 0x20, core is an e-core.
int core_value = (values[0] >> 24);
//Clear cpu from set
CPU_CLR(core_number, &cpu_set);
// Restore original cpu affinity.
if (sched_setaffinity(0, sizeof(cpu_set_t), &original_set) == -1) {
loge(TAG, "Error restoring original CPU affinity\n");
return -3;
}
return core_value;
}
// Function to detect the processor is a hybrid or not.
// Arguments: No arguments.
// Returns 1 if hybrid.
static int get_hybridflag(void)
{
unsigned int values[4];
unsigned int hybrid;
unsigned int leaf = 0x07;
unsigned int subleaf = 0;
__cpuid_count(leaf, subleaf, values[0], values[1],
values[2], values[3]);
//Hybrid detection.
hybrid = (values[3] >> 15) & 0x01;
return hybrid;
}
// Function to get the first and the last efficiency core id's.
// Arguments: No arguments.
// Returns a struct containing the first and last e-core id's.
struct e_cores_layout_s get_efficient_core_ids(void)
{
struct e_cores_layout_s core_locations;
//Initialise first/last e-core location values.
core_locations.first_efficiency_core = -1;
core_locations.last_efficiency_core = -1;
//E-core count.
int efficient_count = 0;
//Get available cores from OS.
int total_cores = get_nprocs_conf();
int available_cores = get_nprocs();
//Warning (if there are unavailable cores).
if (total_cores > available_cores) {
logi(TAG, "%u core(s) might be busy or unavailable.\n",
total_cores - available_cores);
}
//If hybrid
if (get_hybridflag() == 1) {
logv(TAG, "Hybrid processor detected.\n");
for (int i = 0; i < available_cores; i++) {
//If get_core_type returns 0x20, core is an e-core.
if (get_core_type(i) == 0x20) {
if (efficient_count == 0) {
core_locations.first_efficiency_core =
i;
}
core_locations.last_efficiency_core = i;
efficient_count++;
}
}
} else { //All Cores are of the same type.
logv(TAG, "Non-hybrid processor detected.\n");
if (get_core_type(0) == 0x20) {
//All Efficient Cores.
core_locations.first_efficiency_core = 0;
core_locations.last_efficiency_core = available_cores -
1;
}
}
//Results
logv(TAG, "First Atom E-Core: CPU(%d)\n",
core_locations.first_efficiency_core);
logv(TAG, "Last Atom E-Core: CPU(%d)\n",
core_locations.last_efficiency_core);
return core_locations;
}
// DDR BANDWIDTH FROM BIOS/DMI SETTINGS
// Function to read a type 17 field from the file
// it accept a pointer, sizeof, and a file pointer
// return -1 if an error occured and 1 upon success
static int dmi_read_type17_field(void *field, size_t size, FILE *file)
{
if (fread(field, size, 1, file) != 1)
return -1;
return 1;
}
// read Type 17 structure from the file based on length
// It accept the length of type 17, type 17 struct pointer, and a file pointer
// It returns -1 if an error occured and 1 upon success
static int dmi_read_type17(int length, struct type17_s *type17, FILE *file)
{
// Common fields to all lengths
if (!dmi_read_type17_field(&type17->handle, sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->physical_memory,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->memory_error_handle,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->total_width,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->data_width,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->size, sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->form_factor,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->device_set,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->device_locator,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->bank_locator,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->memory_type,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->type_detail,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->speed, sizeof(uint16_t), file)) {
loge(TAG, "Error reading common fields\n");
return -1;
}
// Additional fields for length greater than or equal to 27
if (length >= DMI_TYPE17_VERSION_TWO) {
if (!dmi_read_type17_field(&type17->manufacturer,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->serial_number,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->asset_tag,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->part_number,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->attributes,
sizeof(uint8_t), file)) {
loge(TAG, "Error reading fields for length >= 27\n");
return -1;
}
}
// Additional fields for length greater than or equal to 34
if (length >= DMI_TYPE17_VERSION_THREE) {
if (!dmi_read_type17_field(&type17->extended_size,
sizeof(uint32_t), file) ||
!dmi_read_type17_field(&type17->configured_speed,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->minimum_voltage,
sizeof(uint16_t), file)) {
loge(TAG, "Error reading fields for length >=34\n");
return -1;
}
}
// Additional fields for length greater than or equal to 40
if (length >= DMI_TYPE17_VERSION_FOUR) {
if (!dmi_read_type17_field(&type17->maximum_voltage,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->configured_voltage,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->memory_technology,
sizeof(uint8_t), file)) {
loge(TAG, "Error reading fields for length >= 40\n");
return -1;
}
}
// Additional fields for length greater than or equal to 84
if (length >= DMI_TYPE17_VERSION_FIVE) {
if (!dmi_read_type17_field(&type17->operating_mode_capacity,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->firmware_version,
sizeof(uint8_t), file) ||
!dmi_read_type17_field(&type17->manufacturer_id,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->product_id,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&
type17->memory_subsystem_controller_manufacturer_id,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&
type17->memory_subsystem_controller_product_id,
sizeof(uint16_t), file) ||
!dmi_read_type17_field(&type17->non_volatile_size,
sizeof(uint64_t), file) ||
!dmi_read_type17_field(&type17->volatile_size,
sizeof(uint64_t), file) ||
!dmi_read_type17_field(&type17->cache_size,
sizeof(uint64_t), file) ||
!dmi_read_type17_field(&type17->logical_size,
sizeof(uint64_t), file) ||
!dmi_read_type17_field(&type17->extended_speed,
sizeof(uint32_t), file)) {
loge(TAG, "Error reading fields for length >= 84\n");
return -1;
}
}
// Additional fields for length greater than or equal to 92
if (length >= DMI_TYPE17_VERSION_SIX) {
if (!dmi_read_type17_field(&
type17->extended_configured_memory_speed,
sizeof(uint32_t), file) ||
!dmi_read_type17_field(&type17->pmic0_manufacturer_id,
sizeof(uint16_t), file)) {
loge(TAG, "Error reading fields for length >= 92\n");
return -1;
}
}
return 0;
}
// check if all the respective sizes are equal
// call with pointer to an array and number of elements
// It returns a negative number if not equal and positive when equal
static int dmi_check_same_size(int type17_total, struct type17_s *type17)
{
int memory_size = type17[0].size;
if (memory_size == 0)
memory_size = type17[1].size;
int count = 0;
for (int i = 1; i < type17_total; i++) {
if (type17[i].size == 0)
continue;
if (type17[i].size == memory_size) {
count++;
} else {
logv(TAG, "Memory sizes are not equal\n");
return -1;
}
}
return count;
}
// check if all the respective speeds are equal
// It accept the total number of type 17 and type17 struct pointer / array
// It returns a negative number if not equal and positive when equal
static int dmi_check_same_speed(int type17_total, struct type17_s *type17)
{
int memory_speed = type17[0].speed;
if (memory_speed == 0)
memory_speed = type17[1].speed;
int count = 0;
for (int i = 1; i < type17_total; i++) {
if (type17[i].speed == 0)
continue;
if (type17[i].speed == memory_speed)
count++;
else
return -1;
}
return count;
}
// Reads all the various DMI types from a file and then calculates the total
// memory bandwidth based on type 17 (memory device) data and returns the result
// It handles errors during file access, data reading, and ensures bandwidth is valid.
// Returns the total calculated bandwidth, or -1 if an error occurs.
int dmi_get_bandwidth(void) {
struct type17_s type17[MAX_DMI_MEM_CH];
struct dmi_type_header_s current_header;
int type17_count = 0;
int populated_count = 0;
FILE *fp;
// Open the file in binary read mode
fp = fopen(DMI_FILE, "rb");
if (fp == NULL) {
loge(TAG, "Error opening file\n");
return -1;
}
// Record all type 17 entries (memory controller)
while (fread(¤t_header, sizeof(struct dmi_type_header_s), 1, fp)) {
int type = current_header.type;
int length = current_header.length;
if (length < 1) {
loge(TAG, "Invalid type17 entry length encountered\n");
fclose(fp);
return -1;
}
if (type == 17) {
type17[type17_count].type = type;
type17[type17_count].length = length;
int type17_version = dmi_read_type17(length, &type17[type17_count], fp);
if (type17_version < 0) {
loge(TAG, "Error reading type 17 fields\n");
fclose(fp);
return -1;
}
// Log information about the found type17 entry: speed and size
logv(TAG, "Found type17 #%d Speed = %d Size = %d\n",
type17_count + 1, type17[type17_count].speed,
type17[type17_count].size);
if (type17[type17_count].size > 0) {
populated_count++;
}
type17_count++;
} else {
// Move to the next bytes based on the length
if (fseek(fp, length - 2, SEEK_CUR) != 0) {
loge(TAG, "Error seeking in file\n");
fclose(fp);
return -1;
}
}
// Search for the 00 00 sequence
uint8_t byte1, byte2;
int zero_ret;
zero_ret = fread(&byte1, sizeof(uint8_t), 1, fp);
if (zero_ret != 1) {
loge(TAG, "Error reading zero sequence\n");
fclose(fp);
return -1;
}
zero_ret = fread(&byte2, sizeof(uint8_t), 1, fp);
if (zero_ret != 1) {
loge(TAG, "Error reading zero sequence\n");
fclose(fp);
return -1;
}
while (!(byte1 == 0x00 && byte2 == 0x00)) {
byte1 = byte2;
zero_ret = fread(&byte2, sizeof(uint8_t), 1, fp);
if (zero_ret != 1) {
loge(TAG, "Error reading zero sequence\n");
fclose(fp);
return -1;
}
}
}
fclose(fp);
if (populated_count == 0) {
loge(TAG, "No populated memory channels found\n");
return -1;
}
int memory_speed = 0;
// Get the lowest non-zero speed
for (int i = 0; i < type17_count; i++) {
if (type17[i].size == 0 || type17[i].speed == 0)
continue;
if (memory_speed == 0 || type17[i].speed < memory_speed)
memory_speed = type17[i].speed;
}
if (memory_speed == 0) {
loge(TAG, "No valid memory speed found\n");
return -1;
}
int equal_speed = dmi_check_same_speed(type17_count, type17);
if (equal_speed < 0)
logv(TAG, "Memory speeds are not equal\n");
int equal_size = dmi_check_same_size(type17_count, type17);
if (equal_size < 0)
logv(TAG, "Memory sizes are not equal across populated channels\n");
// Calculate total bandwidth based on populated channels
int total_bandwidth = populated_count * (memory_speed * 8);
if (total_bandwidth <= 0) {
loge(TAG, "Bandwidth is %d\n", total_bandwidth);
return -1;
}
logv(TAG, "Total memory BW: %d MB/s @ %d ch\n",
total_bandwidth, populated_count);
return total_bandwidth;
}
// MEASURE MEMORY BANDWIDTH
uint64_t *parray_one;
uint64_t *parray_two;
// Allocates memory for the arrays, checks for successful allocation
// If memory allocation fails, logs an error message and returns -1.
// On successful initialization, returns 0.
int ddrmembw_init(void) {
parray_one = (uint64_t *)malloc(sizeof(uint64_t) * BWTEST_ARRAY_SIZE);
if (parray_one == NULL) {
loge(TAG, "Memory allocation failed\n");
goto RETURN_ERROR;
}
parray_two = (uint64_t *)malloc(sizeof(uint64_t) * BWTEST_ARRAY_SIZE);
if (parray_two == NULL) {
loge(TAG, "Memory allocation failed\n");
goto FREE_ONE;
}
for (int i = 0; i < BWTEST_ARRAY_SIZE; i++) {
parray_one[i] = 1.0;
parray_two[i] = 0.0;
}
return 0;
FREE_ONE:
free(parray_one);
RETURN_ERROR:
return -1;
}
int ddrmembw_deinit(void) {
free(parray_one);
free(parray_two);
return 0;
}
// Copy with memory barriers to ensure proper memory access
int ddrmembw_copy(void) {
for (int i = 0; i < BWTEST_ARRAY_SIZE; i++)
parray_two[i] = parray_one[i];
return 0;
}
// Identify the minimum/best execution time
// Accepts an array of execution times
// Returns the minimum time
double ddrmembw_min_time(double *time_array) {
// Check if the array is empty
if (time_array[0] == 0)
return -1;
double min = time_array[1];
for (int i = 1; i < NTIMES; i++) {
if (min > time_array[i])
min = time_array[i];
}
return min;
}
// Measures DDR memory bandwidth for the copy operation. Executes the
// copy operation multiple times, records the execution time for each run,
// and calculates the minimum execution time. The bandwidth in megabytes
// per second is computed based on the minimum time and the total
// data transferred, which is logged for performance analysis.
int ddrmembw_measurement(void) {
clock_t start_time, end_time;
double bandwidth;
size_t total_byte;
double time_taken_array[NTIMES];
double min_time;
// Perform copy operation by NTIMES
// Get the various execution times for copy and push to array
for (int i = 0; i < NTIMES; i++) {
start_time = clock();
ddrmembw_copy();
end_time = clock();
time_taken_array[i] = (double)(end_time - start_time) / CLOCKS_PER_SEC;
}
min_time = ddrmembw_min_time(time_taken_array);
if (min_time < 0) {
loge(TAG, "No time data available for 'copy' operation\n");
return -1;
}
total_byte = 2 * BWTEST_ARRAY_SIZE * sizeof(uint64_t);
bandwidth = (total_byte / min_time) / MEGABYTE;
logv(TAG, "Bandwidth Measured: %f\n", bandwidth);
if (bandwidth < 1) {
loge(TAG, "Bandwidth is %f\n", bandwidth);
return 0;
}
return bandwidth;
}