From 079b25060bce14922749bc006e2dd8b08486baa1 Mon Sep 17 00:00:00 2001 From: FlorealRISSO <floreal.risso@univ-tlse3.fr> Date: Thu, 20 Apr 2023 10:15:52 +0200 Subject: [PATCH] format --- doc/info_reader_ex.c | 85 +++++++++++++------- src/memory_counters.c | 180 ++++++++++++++++++++++-------------------- 2 files changed, 152 insertions(+), 113 deletions(-) diff --git a/doc/info_reader_ex.c b/doc/info_reader_ex.c index f1e6d96..d99e916 100644 --- a/doc/info_reader_ex.c +++ b/doc/info_reader_ex.c @@ -18,8 +18,10 @@ *******************************************************/ +// ~/mojitos/doc/$ gcc -Wall -Wextra -Wpedantic -O3 -o info_reader_ex +// info_reader_ex.c ./../src/util.c && ./info_reader_ex -// ~/mojitos/doc/$ gcc -Wall -Wextra -Wpedantic -O3 -o info_reader_ex info_reader_ex.c ./../src/util.c && ./info_reader_ex +#include "./../lib/info_reader.c" #include "./../lib/info_reader.h" #define MAX_PROCS 64 @@ -40,7 +42,7 @@ typedef struct { GenericPointer int_allocator(char *s) { unsigned int value = atoi(s); - return (GenericPointer) value; + return (GenericPointer)value; } // -- Define the behaviour if the attempted value is a string @@ -48,49 +50,49 @@ GenericPointer string_allocator(char *s) { char *value = malloc(strlen(s) + 1); strcpy(value, s); - return (GenericPointer) value; + return (GenericPointer)value; } // -- Define the processor setter void set_processor(GenericPointer storage, GenericPointer data) { - Cpu *cpu = (Cpu *) storage; + Cpu *cpu = (Cpu *)storage; cpu->processor = data; } // -- Define the vendor_id setter void set_vendor_id(GenericPointer storage, GenericPointer data) { - Cpu *cpu = (Cpu *) storage; - cpu->vendor_id = (char *) data; + Cpu *cpu = (Cpu *)storage; + cpu->vendor_id = (char *)data; } // -- Define the family setter void set_family(GenericPointer storage, GenericPointer data) { - Cpu *cpu = (Cpu *) storage; + Cpu *cpu = (Cpu *)storage; cpu->family = data; } // -- Define the core_id setter void set_core_id(GenericPointer storage, GenericPointer data) { - Cpu *cpu = (Cpu *) storage; + Cpu *cpu = (Cpu *)storage; cpu->core_id = data; } // -- Define the physical_id setter void set_physical_id(GenericPointer storage, GenericPointer data) { - Cpu *cpu = (Cpu *) storage; + Cpu *cpu = (Cpu *)storage; cpu->physical_id = data; } // -- Define the model_name setter void set_model_name(GenericPointer storage, GenericPointer data) { - Cpu *cpu = (Cpu *) storage; - cpu->model_name = (char *) data; + Cpu *cpu = (Cpu *)storage; + cpu->model_name = (char *)data; } int main() @@ -98,26 +100,54 @@ int main() Cpu cpus[MAX_PROCS]; // -- Define the setter, the allocator for each key / separator. - KeyFinder keys[] = { - {.key = "processor", .delimiter = ": ", .copy = (CopyAllocator *) int_allocator, .set = (Setter *)set_processor}, - {.key = "vendor_id", .delimiter = ": ", .copy = (CopyAllocator *) string_allocator, .set = (Setter *)set_vendor_id}, - {.key = "cpu family", .delimiter = ": ", .copy = (CopyAllocator *) int_allocator, .set = (Setter *)set_family}, - {.key = "core id", .delimiter = ": ", .copy = (CopyAllocator *) int_allocator, .set = (Setter *)set_core_id}, - {.key = "physical id", .delimiter = ": ", .copy = (CopyAllocator *) int_allocator, .set = (Setter *)set_physical_id}, - {.key = "model name", .delimiter = ": ", .copy = (CopyAllocator *) string_allocator, .set = (Setter *)set_model_name} + KeyFinder keys[] = {{ + .key = "processor", + .delimiter = ": ", + .copy = (CopyAllocator *)int_allocator, + .set = (Setter *)set_processor + }, + { + .key = "vendor_id", + .delimiter = ": ", + .copy = (CopyAllocator *)string_allocator, + .set = (Setter *)set_vendor_id + }, + { + .key = "cpu family", + .delimiter = ": ", + .copy = (CopyAllocator *)int_allocator, + .set = (Setter *)set_family + }, + { + .key = "core id", + .delimiter = ": ", + .copy = (CopyAllocator *)int_allocator, + .set = (Setter *)set_core_id + }, + { + .key = "physical id", + .delimiter = ": ", + .copy = (CopyAllocator *)int_allocator, + .set = (Setter *)set_physical_id + }, + { + .key = "model name", + .delimiter = ": ", + .copy = (CopyAllocator *)string_allocator, + .set = (Setter *)set_model_name + } }; - size_t nb_keys = sizeof(keys)/sizeof(KeyFinder); + size_t nb_keys = sizeof(keys) / sizeof(KeyFinder); // -- Init the parser - Parser parser = { - .storage = (GenericPointer) cpus, - .capacity = MAX_PROCS, - .storage_struct_size = sizeof(Cpu), - .keys = keys, - .nb_keys = nb_keys, - .file = fopen("/proc/cpuinfo", "r") - }; + Parser parser = {.storage = (GenericPointer)cpus, + .capacity = MAX_PROCS, + .storage_struct_size = sizeof(Cpu), + .keys = keys, + .nb_keys = nb_keys, + .file = fopen("/proc/cpuinfo", "r") + }; // -- Parse the file parse(&parser); @@ -137,4 +167,3 @@ int main() printf("==============================\n"); return 0; } - diff --git a/src/memory_counters.c b/src/memory_counters.c index 8e8e916..185f454 100644 --- a/src/memory_counters.c +++ b/src/memory_counters.c @@ -10,111 +10,121 @@ static const char *path = "/proc/meminfo"; typedef struct { - KeyFinder *keys; - unsigned int count; - FILE *file; + KeyFinder *keys; + unsigned int count; + FILE *file; } MemoryCounters; -GenericPointer long_allocator(char *s) { - long value = atol(s); - return (GenericPointer)value; +GenericPointer long_allocator(char *s) +{ + long value = atol(s); + return (GenericPointer)value; } -KeyFinder *build_keyfinder(unsigned int count, unsigned int *indexes) { - KeyFinder *keys = (KeyFinder *)calloc(count, sizeof(KeyFinder)); - for (unsigned int i = 0; i < count; i++) { - unsigned int idx = indexes[i]; - KeyFinder key = {.key = memory_counters[idx], - .delimiter = ":", - .copy = long_allocator, - .set = setter_functions[i]}; - memcpy(&keys[i], &key, sizeof(KeyFinder)); - } - return keys; +KeyFinder *build_keyfinder(unsigned int count, unsigned int *indexes) +{ + KeyFinder *keys = (KeyFinder *)calloc(count, sizeof(KeyFinder)); + for (unsigned int i = 0; i < count; i++) { + unsigned int idx = indexes[i]; + KeyFinder key = {.key = memory_counters[idx], + .delimiter = ":", + .copy = long_allocator, + .set = setter_functions[i] + }; + memcpy(&keys[i], &key, sizeof(KeyFinder)); + } + return keys; } void memory_list(char *memory_string, unsigned int *count, - unsigned int *indexes) { - char *token; - *count = 0; - - while ((token = strtok(memory_string, ",")) != NULL) { - memory_string = NULL; - - unsigned int i; - for (i = 0; i < NB_COUNTERS; i++) { - if (strcmp(memory_counters[i], token) == 0) { - (*count)++; - indexes[*count - 1] = i; - break; - } - } - - if (i == NB_COUNTERS) { - fprintf(stderr, "Unknown memory counter: %s\n", token); - exit(EXIT_FAILURE); + unsigned int *indexes) +{ + char *token; + *count = 0; + + while ((token = strtok(memory_string, ",")) != NULL) { + memory_string = NULL; + + unsigned int i; + for (i = 0; i < NB_COUNTERS; i++) { + if (strcmp(memory_counters[i], token) == 0) { + (*count)++; + indexes[*count - 1] = i; + break; + } + } + + if (i == NB_COUNTERS) { + fprintf(stderr, "Unknown memory counter: %s\n", token); + exit(EXIT_FAILURE); + } + + if ((*count) > NB_COUNTERS) { + fprintf(stderr, "Too much counters, there are probably duplicates\n"); + exit(EXIT_FAILURE); + } } - - if ((*count) > NB_COUNTERS) { - fprintf(stderr, "Too much counters, there are probably duplicates\n"); - exit(EXIT_FAILURE); - } - } } -unsigned int init_memory_counters(char *args, void **ptr) { - unsigned int indexes[NB_COUNTERS]; - unsigned int count = 0; - memory_list(args, &count, indexes); +unsigned int init_memory_counters(char *args, void **ptr) +{ + unsigned int indexes[NB_COUNTERS]; + unsigned int count = 0; + memory_list(args, &count, indexes); - KeyFinder *keys = build_keyfinder(count, indexes); - FILE *file = fopen(path, "r"); + KeyFinder *keys = build_keyfinder(count, indexes); + FILE *file = fopen(path, "r"); - MemoryCounters *counters = calloc(1, sizeof(MemoryCounters)); - counters->keys = keys; - counters->count = count; - counters->file = file; + MemoryCounters *counters = calloc(1, sizeof(MemoryCounters)); + counters->keys = keys; + counters->count = count; + counters->file = file; - *ptr = (void *)counters; - return count; + *ptr = (void *)counters; + return count; } -unsigned int get_memory_counters(uint64_t *results, void *ptr) { - MemoryCounters *counters = (MemoryCounters *)ptr; - fseek(counters->file, 0, SEEK_SET); - Parser parser = {.storage = (GenericPointer)results, - .capacity = 1, - .nb_stored = 0, - .storage_struct_size = sizeof(uint64_t) * counters->count, - .keys = counters->keys, - .nb_keys = counters->count, - .file = counters->file}; - - parse(&parser); - return counters->count; +unsigned int get_memory_counters(uint64_t *results, void *ptr) +{ + MemoryCounters *counters = (MemoryCounters *)ptr; + fseek(counters->file, 0, SEEK_SET); + Parser parser = {.storage = (GenericPointer)results, + .capacity = 1, + .nb_stored = 0, + .storage_struct_size = sizeof(uint64_t) * counters->count, + .keys = counters->keys, + .nb_keys = counters->count, + .file = counters->file + }; + + parse(&parser); + return counters->count; } -void label_memory_counters(char **labels, void *ptr) { - MemoryCounters *counters = (MemoryCounters *)ptr; - for (unsigned int i = 0; i < counters->count; i++) { - labels[i] = counters->keys[i].key; - } +void label_memory_counters(char **labels, void *ptr) +{ + MemoryCounters *counters = (MemoryCounters *)ptr; + for (unsigned int i = 0; i < counters->count; i++) { + labels[i] = counters->keys[i].key; + } } -void clean_memory_counters(void *ptr) { - MemoryCounters *counters = (MemoryCounters *)ptr; - fclose(counters->file); - free(counters->keys); - free(ptr); +void clean_memory_counters(void *ptr) +{ + MemoryCounters *counters = (MemoryCounters *)ptr; + fclose(counters->file); + free(counters->keys); + free(ptr); } -void *show_all_memory_counters(void *none1, size_t none2) { - for (unsigned int i = 0; i < NB_COUNTERS; i++) { - printf("%s\n", memory_counters[i]); - } +void *show_all_memory_counters(void *none1, size_t none2) +{ + for (unsigned int i = 0; i < NB_COUNTERS; i++) { + printf("%s\n", memory_counters[i]); + } - UNUSED(none1); - UNUSED(none2); - exit(EXIT_SUCCESS); - return NULL; /* not reached */ + UNUSED(none1); + UNUSED(none2); + exit(EXIT_SUCCESS); + return NULL; /* not reached */ } -- GitLab