[v1.03] Apply refactoring in src/common/udev.c

This commit is contained in:
Dr-Noob
2023-05-06 10:06:16 +02:00
parent 8e95828fb1
commit 8a8b1dffa8

View File

@@ -199,7 +199,13 @@ bool maps_equal(uint32_t* map1, uint32_t* map2, int n) {
return true; return true;
} }
int get_num_caches_from_files(char** paths, int num_paths) { // Generic function to count the number of distinct
// elements in the list of files passed in char** paths.
// An element can be potentially anything.
// We use this function to count:
// - The number of caches
// - The number of sockets
int get_num_elements_from_files(char** paths, int num_paths) {
int filelen; int filelen;
char* buf; char* buf;
char* tmpbuf; char* tmpbuf;
@@ -214,10 +220,10 @@ int get_num_caches_from_files(char** paths, int num_paths) {
num_bitmasks += (buf[i] == ','); num_bitmasks += (buf[i] == ',');
} }
// 2. Read cpu_shared_map from every core // 2. Read map from every core
uint32_t** shared_maps = emalloc(sizeof(uint32_t *) * num_paths); uint32_t** maps = emalloc(sizeof(uint32_t *) * num_paths);
for(int i=0; i < num_paths; i++) { for(int i=0; i < num_paths; i++) {
shared_maps[i] = emalloc(sizeof(uint32_t) * num_bitmasks); maps[i] = emalloc(sizeof(uint32_t) * num_bitmasks);
if((buf = read_file(paths[i], &filelen)) == NULL) { if((buf = read_file(paths[i], &filelen)) == NULL) {
printWarn("Could not open '%s'", paths[i]); printWarn("Could not open '%s'", paths[i]);
@@ -242,35 +248,43 @@ int get_num_caches_from_files(char** paths, int num_paths) {
return -1; return -1;
} }
shared_maps[i][j] = (uint32_t) ret; maps[i][j] = (uint32_t) ret;
buf = commaend + 1; buf = commaend + 1;
free(tmpbuf); free(tmpbuf);
} }
} }
// 2. Count number of different masks; this is the number of caches // 2. Count number of different masks; this is the number of elements
int num_caches = 0; int num_elements = 0;
bool found = false; bool found = false;
uint32_t** unique_shared_maps = emalloc(sizeof(uint32_t *) * num_paths); uint32_t** unique_maps = emalloc(sizeof(uint32_t *) * num_paths);
for(int i=0; i < num_paths; i++) { for(int i=0; i < num_paths; i++) {
unique_shared_maps[i] = emalloc(sizeof(uint32_t) * num_bitmasks); unique_maps[i] = emalloc(sizeof(uint32_t) * num_bitmasks);
for(int j=0; j < num_bitmasks; j++) { for(int j=0; j < num_bitmasks; j++) {
unique_shared_maps[i][j] = 0; unique_maps[i][j] = 0;
} }
} }
for(int i=0; i < num_paths; i++) { for(int i=0; i < num_paths; i++) {
for(int j=0; j < num_paths && !found; j++) { for(int j=0; j < num_paths && !found; j++) {
if(maps_equal(shared_maps[i], unique_shared_maps[j], num_bitmasks)) found = true; if(maps_equal(maps[i], unique_maps[j], num_bitmasks)) found = true;
} }
if(!found) { if(!found) {
add_shared_map(shared_maps, i, unique_shared_maps, num_caches, num_bitmasks); add_shared_map(maps, i, unique_maps, num_elements, num_bitmasks);
num_caches++; num_elements++;
} }
found = false; found = false;
} }
return num_caches; return num_elements;
}
int get_num_caches_from_files(char** paths, int num_paths) {
return get_num_elements_from_files(paths, num_paths);
}
int get_num_sockets_from_files(char** paths, int num_paths) {
return get_num_elements_from_files(paths, num_paths);
} }
int get_num_caches_by_level(struct cpuInfo* cpu, uint32_t level) { int get_num_caches_by_level(struct cpuInfo* cpu, uint32_t level) {
@@ -300,82 +314,6 @@ int get_num_caches_by_level(struct cpuInfo* cpu, uint32_t level) {
return ret; return ret;
} }
// TODO: This function is very similar to get_num_caches_from_files,
// refactoring should be considered
int get_num_sockets_from_files(char** paths, int num_paths) {
int filelen;
char* buf;
char* tmpbuf;
// 1. Count the number of bitmasks per file
if((buf = read_file(paths[0], &filelen)) == NULL) {
printWarn("Could not open '%s'", paths[0]);
return -1;
}
int num_bitmasks = 1;
for(int i=0; buf[i]; i++) {
num_bitmasks += (buf[i] == ',');
}
// 2. Read package_cpus from every core
uint32_t** package_cpus = emalloc(sizeof(uint32_t *) * num_paths);
for(int i=0; i < num_paths; i++) {
package_cpus[i] = emalloc(sizeof(uint32_t) * num_bitmasks);
if((buf = read_file(paths[i], &filelen)) == NULL) {
printWarn("Could not open '%s'", paths[i]);
return -1;
}
for(int j=0; j < num_bitmasks; j++) {
char* end;
tmpbuf = emalloc(sizeof(char) * (strlen(buf) + 1));
char* commaend = strstr(buf, ",");
if(commaend == NULL) {
strcpy(tmpbuf, buf);
}
else {
strncpy(tmpbuf, buf, commaend-buf);
}
errno = 0;
long ret = strtol(tmpbuf, &end, 16);
if(errno != 0) {
printf("strtol: %s", strerror(errno));
free(buf);
return -1;
}
package_cpus[i][j] = (uint32_t) ret;
buf = commaend + 1;
free(tmpbuf);
}
}
// 3. Count number of different packages; this is the number of sockets
int num_sockets = 0;
bool found = false;
uint32_t** unique_package_cpu = emalloc(sizeof(uint32_t *) * num_paths);
for(int i=0; i < num_paths; i++) {
unique_package_cpu[i] = emalloc(sizeof(uint32_t) * num_bitmasks);
for(int j=0; j < num_bitmasks; j++) {
unique_package_cpu[i][j] = 0;
}
}
for(int i=0; i < num_paths; i++) {
for(int j=0; j < num_paths && !found; j++) {
if(maps_equal(package_cpus[i], unique_package_cpu[j], num_bitmasks)) found = true;
}
if(!found) {
add_shared_map(package_cpus, i, unique_package_cpu, num_sockets, num_bitmasks);
num_sockets++;
}
found = false;
}
return num_sockets;
}
int get_num_sockets_package_cpus(struct topology* topo) { int get_num_sockets_package_cpus(struct topology* topo) {
// Get number of sockets using // Get number of sockets using
// /sys/devices/system/cpu/cpu*/topology/package_cpus // /sys/devices/system/cpu/cpu*/topology/package_cpus