Skip to content
Snippets Groups Projects

Add memory counters and fix makefile

Merged floreal.risso requested to merge pr_final into devel
2 files
+ 98
88
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 95
85
@@ -10,111 +10,121 @@
@@ -10,111 +10,121 @@
static const char *path = "/proc/meminfo";
static const char *path = "/proc/meminfo";
typedef struct {
typedef struct {
KeyFinder *keys;
KeyFinder *keys;
unsigned int count;
unsigned int count;
FILE *file;
FILE *file;
} MemoryCounters;
} MemoryCounters;
GenericPointer long_allocator(char *s) {
GenericPointer long_allocator(char *s)
long value = atol(s);
{
return (GenericPointer)value;
long value = atol(s);
 
return (GenericPointer)value;
}
}
KeyFinder *build_keyfinder(unsigned int count, unsigned int *indexes) {
KeyFinder *build_keyfinder(unsigned int count, unsigned int *indexes)
KeyFinder *keys = (KeyFinder *)calloc(count, sizeof(KeyFinder));
{
for (unsigned int i = 0; i < count; i++) {
KeyFinder *keys = (KeyFinder *)calloc(count, sizeof(KeyFinder));
unsigned int idx = indexes[i];
for (unsigned int i = 0; i < count; i++) {
KeyFinder key = {.key = memory_counters[idx],
unsigned int idx = indexes[i];
.delimiter = ":",
KeyFinder key = {.key = memory_counters[idx],
.copy = long_allocator,
.delimiter = ":",
.set = setter_functions[i]};
.copy = long_allocator,
memcpy(&keys[i], &key, sizeof(KeyFinder));
.set = setter_functions[i]
}
};
return keys;
memcpy(&keys[i], &key, sizeof(KeyFinder));
 
}
 
return keys;
}
}
void memory_list(char *memory_string, unsigned int *count,
void memory_list(char *memory_string, unsigned int *count,
unsigned int *indexes) {
unsigned int *indexes)
char *token;
{
*count = 0;
char *token;
*count = 0;
while ((token = strtok(memory_string, ",")) != NULL) {
memory_string = NULL;
while ((token = strtok(memory_string, ",")) != NULL) {
memory_string = NULL;
unsigned int i;
for (i = 0; i < NB_COUNTERS; i++) {
unsigned int i;
if (strcmp(memory_counters[i], token) == 0) {
for (i = 0; i < NB_COUNTERS; i++) {
(*count)++;
if (strcmp(memory_counters[i], token) == 0) {
indexes[*count - 1] = i;
(*count)++;
break;
indexes[*count - 1] = i;
}
break;
}
}
}
if (i == NB_COUNTERS) {
fprintf(stderr, "Unknown memory counter: %s\n", token);
if (i == NB_COUNTERS) {
exit(EXIT_FAILURE);
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 init_memory_counters(char *args, void **ptr)
unsigned int indexes[NB_COUNTERS];
{
unsigned int count = 0;
unsigned int indexes[NB_COUNTERS];
memory_list(args, &count, indexes);
unsigned int count = 0;
 
memory_list(args, &count, indexes);
KeyFinder *keys = build_keyfinder(count, indexes);
KeyFinder *keys = build_keyfinder(count, indexes);
FILE *file = fopen(path, "r");
FILE *file = fopen(path, "r");
MemoryCounters *counters = calloc(1, sizeof(MemoryCounters));
MemoryCounters *counters = calloc(1, sizeof(MemoryCounters));
counters->keys = keys;
counters->keys = keys;
counters->count = count;
counters->count = count;
counters->file = file;
counters->file = file;
*ptr = (void *)counters;
*ptr = (void *)counters;
return count;
return count;
}
}
unsigned int get_memory_counters(uint64_t *results, void *ptr) {
unsigned int get_memory_counters(uint64_t *results, void *ptr)
MemoryCounters *counters = (MemoryCounters *)ptr;
{
fseek(counters->file, 0, SEEK_SET);
MemoryCounters *counters = (MemoryCounters *)ptr;
Parser parser = {.storage = (GenericPointer)results,
fseek(counters->file, 0, SEEK_SET);
.capacity = 1,
Parser parser = {.storage = (GenericPointer)results,
.nb_stored = 0,
.capacity = 1,
.storage_struct_size = sizeof(uint64_t) * counters->count,
.nb_stored = 0,
.keys = counters->keys,
.storage_struct_size = sizeof(uint64_t) * counters->count,
.nb_keys = counters->count,
.keys = counters->keys,
.file = counters->file};
.nb_keys = counters->count,
.file = counters->file
parse(&parser);
};
return counters->count;
 
parse(&parser);
 
return counters->count;
}
}
void label_memory_counters(char **labels, void *ptr) {
void label_memory_counters(char **labels, void *ptr)
MemoryCounters *counters = (MemoryCounters *)ptr;
{
for (unsigned int i = 0; i < counters->count; i++) {
MemoryCounters *counters = (MemoryCounters *)ptr;
labels[i] = counters->keys[i].key;
for (unsigned int i = 0; i < counters->count; i++) {
}
labels[i] = counters->keys[i].key;
 
}
}
}
void clean_memory_counters(void *ptr) {
void clean_memory_counters(void *ptr)
MemoryCounters *counters = (MemoryCounters *)ptr;
{
fclose(counters->file);
MemoryCounters *counters = (MemoryCounters *)ptr;
free(counters->keys);
fclose(counters->file);
free(ptr);
free(counters->keys);
 
free(ptr);
}
}
void *show_all_memory_counters(void *none1, size_t none2) {
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]);
for (unsigned int i = 0; i < NB_COUNTERS; i++) {
}
printf("%s\n", memory_counters[i]);
 
}
UNUSED(none1);
UNUSED(none1);
UNUSED(none2);
UNUSED(none2);
exit(EXIT_SUCCESS);
exit(EXIT_SUCCESS);
return NULL; /* not reached */
return NULL; /* not reached */
}
}
Loading