#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); }