Exercise on memory management analyzer

This commit is contained in:
Fabio Scotto di Santolo
2025-08-21 18:19:45 +02:00
parent 04b33c03ef
commit 99cbf7d4fb
7 changed files with 371 additions and 11 deletions

View File

@@ -0,0 +1,47 @@
#include "mem_utils.h"
#include <stdio.h>
#include <string.h>
void print_stats(struct Stats *stats)
{
printf("Allocator: %s\nBlocks: %ld\nAvarage Allocation Time: %.2f "
"μs\nAvarage Free Time: %.2f μs\nPeak Memory Usage: %.2f MB\n",
stats->allocator_type, stats->nr_allocations,
stats->avg_alloc_time*1e6, stats->avg_free_time*1e6,
stats->mem_usage_mb);
}
double avg(double *values, int size)
{
double sum = 0.0;
for (int i = 0; i < size; i++) {
sum += *(values + i);
}
return size > 0 ? sum / size : 0.0;
}
double get_mem_usage()
{
FILE *fp = fopen("/proc/self/status", "r");
if (!fp) {
perror("fopen");
return 1;
}
int found = 0;
char line[256];
while (fgets(line, sizeof(line), fp)) {
if (strncmp(line, "VmRSS:", 6) == 0) {
found = 1;
break;
}
}
fclose(fp);
if (!found) {
fprintf(stderr, "VmRSS not found in /proc/self/status\n");
return 0.0;
}
return strtod(line + 6, NULL) / 1024.0;
}