summaryrefslogtreecommitdiff
path: root/schulung_tools/malloc/stats.c
blob: 4b17323e14f03998b55e70b1a3d8f6397e91cc2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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);
}