From db433cca717ddc0ec8842eb6a3c155817e95a7fc Mon Sep 17 00:00:00 2001 From: FlorealRISSO <floreal.risso@univ-tlse3.fr> Date: Tue, 28 Mar 2023 10:00:50 +0200 Subject: [PATCH] update: create api --- src/memory_counters.c | 74 +++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/src/memory_counters.c b/src/memory_counters.c index 4572ff0..7225352 100644 --- a/src/memory_counters.c +++ b/src/memory_counters.c @@ -1,4 +1,6 @@ #include "meminfo_option.h" +#include <bits/stdint-uintn.h> +#include <fcntl.h> #include <info_reader.h> #include <inttypes.h> #include <stdio.h> @@ -7,6 +9,12 @@ static const char *path = "/proc/meminfo"; +typedef struct { + KeyFinder *keys; + unsigned int count; + FILE *file; +} MemoryCounters; + GenericPointer long_allocator(char *s) { long value = atol(s); return (GenericPointer)value; @@ -54,42 +62,48 @@ void memory_list(char *memory_string, unsigned int *count, } } -int main(int argc, char **argv) { - - if (argc != 2) { - fprintf(stderr, "Usage ... [elem1,elem2...]\n"); - exit(EXIT_FAILURE); - } - +unsigned int init_memory_counters(char *args, void **ptr) { unsigned int indexes[NB_COUNTERS]; unsigned int count = 0; - memory_list(argv[1], &count, indexes); - - printf("%d, count \n", count); + memory_list(args, &count, indexes); KeyFinder *keys = build_keyfinder(count, indexes); - uint64_t value[count]; + FILE *file = fopen(path, "r"); - // -- Init the parser - Parser parser = {.storage = (GenericPointer)&value, - .capacity = 1, - .storage_struct_size = sizeof(uint64_t) * count, - .keys = keys, - .nb_keys = count, - .file = fopen(path, "r")}; - - // -- Parse the file - while (1) { - parse(&parser); - for (unsigned int i = 0; i < count; i++) { - printf("%s: %" PRIu64 "\n", keys[i].key, value[i]); - } - } + MemoryCounters *counters = calloc(1, sizeof(MemoryCounters)); + counters->keys = keys; + counters->count = count; + counters->file = file; - free(keys); + *ptr = (void *)counters; + return count; +} - // Print and free the results +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; + } +} - fclose(parser.file); - return 0; +void clean_memory_counters(void *ptr) { + MemoryCounters *counters = (MemoryCounters *)ptr; + fclose(counters->file); + free(counters->keys); + free(ptr); } -- GitLab