Skip to content
Snippets Groups Projects
Commit db433cca authored by floreal.risso's avatar floreal.risso
Browse files

update: create api

parent 5149f2fe
No related branches found
No related tags found
2 merge requests!9fix sensor example (doc),!7Add memory counters and fix makefile
#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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment