Skip to content

Instantly share code, notes, and snippets.

@tausen
Created March 8, 2022 15:43
Show Gist options
  • Save tausen/647dced916006b4ce69d205b66787ccb to your computer and use it in GitHub Desktop.
Save tausen/647dced916006b4ce69d205b66787ccb to your computer and use it in GitHub Desktop.
memusage dat parser
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
struct entry {
uint64_t heap;
uint64_t stack;
uint32_t time_low;
uint32_t time_high;
};
int main(void)
{
FILE *f = fopen("mem.dat", "r");
if (f == NULL) {
fprintf(stderr, "%s:%i: open error\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
FILE *of = fopen("mem.txt", "w");
if (of == NULL) {
fprintf(stderr, "%s:%i: open error\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
struct entry e, h1, h2;
int nread = fread(&h1, sizeof(struct entry), 1, f);
if (nread != 1) {
printf("got %i bytes, halting\n", nread);
exit(EXIT_FAILURE);
}
nread = fread(&h2, sizeof(struct entry), 1, f);
if (nread != 1) {
printf("got %i bytes, halting\n", nread);
exit(EXIT_FAILURE);
}
uint64_t start_time = ((uint64_t) h1.time_high) << 32 | h1.time_low;
uint64_t end_time = ((uint64_t) h2.time_high) << 32 | h2.time_low;
uint64_t total_time = end_time - start_time;
while (1) {
nread = fread(&e, sizeof(struct entry), 1, f);
if (nread != 1) {
printf("got %i bytes, halting\n", nread);
break;
}
uint64_t now = ((uint64_t) e.time_high) << 32 | e.time_low;
double t = ((double)now - (double)start_time) / (double)total_time;
printf("heap: %lu, stack: %lu, time: %lf\n", e.heap, e.stack, t);
fprintf(of, "%lf\t%lu\t%lu\n", t, e.heap, e.stack);
}
fclose(of);
fclose(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment