From e1e6511cd68b646234dcc8f5f86b25b7ea48a720 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Thu, 28 Mar 2019 15:17:34 +0106 Subject: schulung_tools: rename/modify malloc to avoid system() A more complex but cleaner implementation of retrieving/printing the stats has been implemented. The implementation is put into a separate stats.c file so that the main program (malloc.) can be easily reviewed to see what the program is doing. Signed-off-by: John Ogness --- schulung_tools/malloc/stats.c | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 schulung_tools/malloc/stats.c (limited to 'schulung_tools/malloc/stats.c') 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 +#include +#include +#include +#include +#include +#include +#include + +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); +} -- cgit v1.2.3