diff options
Diffstat (limited to 'schulung_tools/malloc/stats.c')
| -rw-r--r-- | schulung_tools/malloc/stats.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/schulung_tools/malloc/stats.c b/schulung_tools/malloc/stats.c new file mode 100644 index 0000000..4b17323 --- /dev/null +++ b/schulung_tools/malloc/stats.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> +#include <time.h> +#include <sys/types.h> +#include <sys/stat.h> + +void print_stats(int postcall) +{ + static struct timespec tp1; + static struct timespec tp2; + static int fd = -1; + char buf[32]; + int virt; + int res; + + if (fd == -1) + fd = open("/proc/self/statm", O_RDONLY); + + if (!postcall) { + clock_gettime(CLOCK_MONOTONIC, &tp1); + return; + } + + printf(" time="); + if (tp1.tv_sec && clock_gettime(CLOCK_MONOTONIC, &tp2) == 0) { + if (tp1.tv_nsec > tp2.tv_nsec) { + tp2.tv_sec--; + tp2.tv_nsec += 1000000000; + } + tp2.tv_sec -= tp1.tv_sec; + tp2.tv_nsec -= tp1.tv_nsec; + printf("%d.%06ds ", tp2.tv_sec, tp2.tv_nsec / 1000); + } else { + tp2.tv_sec = 0; + printf("? "); + } + tp1 = tp2; + + if (fd < 0) + goto out; + + lseek(fd, 0, SEEK_SET); + if (read(fd, buf, sizeof(buf)) <= 0) + goto out; + + if (sscanf(buf, "%d %d ", &virt, &res) != 2) + goto out; + + printf("virt=%dKB res=%dKB", virt * 4, res * 4); +out: + printf("\n"); + fflush(stdout); +} |
