Skip to content
Snippets Groups Projects

Add memory counters and fix makefile

Merged floreal.risso requested to merge pr_final into devel
1 file
+ 44
30
Compare changes
  • Side-by-side
  • Inline
+ 44
30
#include "meminfo_option.h"
#include "meminfo_option.h"
 
#include <bits/stdint-uintn.h>
 
#include <fcntl.h>
#include <info_reader.h>
#include <info_reader.h>
#include <inttypes.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdio.h>
@@ -7,6 +9,12 @@
@@ -7,6 +9,12 @@
static const char *path = "/proc/meminfo";
static const char *path = "/proc/meminfo";
 
typedef struct {
 
KeyFinder *keys;
 
unsigned int count;
 
FILE *file;
 
} MemoryCounters;
 
GenericPointer long_allocator(char *s) {
GenericPointer long_allocator(char *s) {
long value = atol(s);
long value = atol(s);
return (GenericPointer)value;
return (GenericPointer)value;
@@ -54,42 +62,48 @@ void memory_list(char *memory_string, unsigned int *count,
@@ -54,42 +62,48 @@ void memory_list(char *memory_string, unsigned int *count,
}
}
}
}
int main(int argc, char **argv) {
unsigned int init_memory_counters(char *args, void **ptr) {
if (argc != 2) {
fprintf(stderr, "Usage ... [elem1,elem2...]\n");
exit(EXIT_FAILURE);
}
unsigned int indexes[NB_COUNTERS];
unsigned int indexes[NB_COUNTERS];
unsigned int count = 0;
unsigned int count = 0;
memory_list(argv[1], &count, indexes);
memory_list(args, &count, indexes);
printf("%d, count \n", count);
KeyFinder *keys = build_keyfinder(count, indexes);
KeyFinder *keys = build_keyfinder(count, indexes);
uint64_t value[count];
FILE *file = fopen(path, "r");
// -- Init the parser
MemoryCounters *counters = calloc(1, sizeof(MemoryCounters));
Parser parser = {.storage = (GenericPointer)&value,
counters->keys = keys;
.capacity = 1,
counters->count = count;
.storage_struct_size = sizeof(uint64_t) * count,
counters->file = file;
.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]);
}
}
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);
void clean_memory_counters(void *ptr) {
return 0;
MemoryCounters *counters = (MemoryCounters *)ptr;
 
fclose(counters->file);
 
free(counters->keys);
 
free(ptr);
}
}
Loading