forked from cyrillos/criu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.c
95 lines (78 loc) · 1.99 KB
/
stats.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <unistd.h>
#include <sys/time.h>
#include "protobuf.h"
#include "stats.h"
#include "crtools.h"
#include "protobuf/stats.pb-c.h"
struct timing {
struct timeval start;
struct timeval total;
};
static struct timing timings[TIME_NR_STATS];
static unsigned long counts[CNT_NR_STATS];
void cnt_add(int c, unsigned long val)
{
BUG_ON(c >= CNT_NR_STATS);
counts[c] += val;
}
static void timeval_accumulate(const struct timeval *from, const struct timeval *to,
struct timeval *res)
{
suseconds_t usec;
res->tv_sec += to->tv_sec - from->tv_sec;
usec = to->tv_usec;
if (usec < from->tv_usec) {
usec += USEC_PER_SEC;
res->tv_sec -= 1;
}
res->tv_usec += usec - from->tv_usec;
if (res->tv_usec > USEC_PER_SEC) {
res->tv_usec -= USEC_PER_SEC;
res->tv_sec += 1;
}
}
void timing_start(int t)
{
BUG_ON(t >= TIME_NR_STATS);
gettimeofday(&timings[t].start, NULL);
}
void timing_stop(int t)
{
struct timeval now;
gettimeofday(&now, NULL);
timeval_accumulate(&timings[t].start, &now, &timings[t].total);
}
void show_stats(int fd)
{
do_pb_show_plain(fd, PB_STATS, 1, NULL,
"1.1:%u 1.2:%u 1.3:%u 1.4:%u 1.5:%Lu 1.6:%Lu 1.7:%Lu");
}
static void encode_time(int t, u_int32_t *to)
{
*to = timings[t].total.tv_sec * USEC_PER_SEC + timings[t].total.tv_usec;
}
void write_stats(int what)
{
StatsEntry stats = STATS_ENTRY__INIT;
DumpStatsEntry dstats = DUMP_STATS_ENTRY__INIT;
char *name;
int fd;
pr_info("Writing stats\n");
if (what == DUMP_STATS) {
stats.dump = &dstats;
encode_time(TIME_FREEZING, &dstats.freezing_time);
encode_time(TIME_FROZEN, &dstats.frozen_time);
encode_time(TIME_MEMDUMP, &dstats.memdump_time);
encode_time(TIME_MEMWRITE, &dstats.memwrite_time);
dstats.pages_scanned = counts[CNT_PAGES_SCANNED];
dstats.pages_skipped_parent = counts[CNT_PAGES_SKIPPED_PARENT];
dstats.pages_written = counts[CNT_PAGES_WRITTEN];
name = "dump";
} else
return;
fd = open_image(CR_FD_STATS, O_DUMP, name);
if (fd >= 0) {
pb_write_one(fd, &stats, PB_STATS);
close(fd);
}
}