diff options
| author | John Ogness <john.ogness@linutronix.de> | 2019-03-28 15:17:34 +0106 |
|---|---|---|
| committer | John Ogness <john.ogness@linutronix.de> | 2019-03-28 15:17:34 +0106 |
| commit | e1e6511cd68b646234dcc8f5f86b25b7ea48a720 (patch) | |
| tree | 859af07101563fb34d00dfbf256c4a76b3294f99 /schulung_tools/malloc/stats.c | |
| parent | a39de4609225129c19df931d556b1d032d806403 (diff) | |
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 <john.ogness@linutronix.de>
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); +} |
