-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin-stat.c
127 lines (105 loc) · 2.85 KB
/
builtin-stat.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <kvm/util.h>
#include <kvm/kvm-cmd.h>
#include <kvm/builtin-stat.h>
#include <kvm/kvm.h>
#include <kvm/parse-options.h>
#include <kvm/kvm-ipc.h>
#include <sys/select.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <linux/virtio_balloon.h>
static bool mem;
static bool all;
static const char *instance_name;
static const char * const stat_usage[] = {
"lkvm stat [command] [--all] [-n name]",
NULL
};
static const struct option stat_options[] = {
OPT_GROUP("Commands options:"),
OPT_BOOLEAN('m', "memory", &mem, "Display memory statistics"),
OPT_GROUP("Instance options:"),
OPT_BOOLEAN('a', "all", &all, "All instances"),
OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
OPT_END()
};
static void parse_stat_options(int argc, const char **argv)
{
while (argc != 0) {
argc = parse_options(argc, argv, stat_options, stat_usage,
PARSE_OPT_STOP_AT_NON_OPTION);
if (argc != 0)
kvm_stat_help();
}
}
void kvm_stat_help(void)
{
usage_with_options(stat_usage, stat_options);
}
static int do_memstat(const char *name, int sock)
{
struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
fd_set fdset;
struct timeval t = { .tv_sec = 1 };
int r;
u8 i;
FD_ZERO(&fdset);
FD_SET(sock, &fdset);
r = kvm_ipc__send(sock, KVM_IPC_STAT);
if (r < 0)
return r;
r = select(1, &fdset, NULL, NULL, &t);
if (r < 0) {
pr_err("Could not retrieve mem stats from %s", name);
return r;
}
r = read(sock, &stats, sizeof(stats));
if (r < 0)
return r;
printf("\n\n\t*** Guest memory statistics ***\n\n");
for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
switch (stats[i].tag) {
case VIRTIO_BALLOON_S_SWAP_IN:
printf("The amount of memory that has been swapped in (in bytes):");
break;
case VIRTIO_BALLOON_S_SWAP_OUT:
printf("The amount of memory that has been swapped out to disk (in bytes):");
break;
case VIRTIO_BALLOON_S_MAJFLT:
printf("The number of major page faults that have occurred:");
break;
case VIRTIO_BALLOON_S_MINFLT:
printf("The number of minor page faults that have occurred:");
break;
case VIRTIO_BALLOON_S_MEMFREE:
printf("The amount of memory not being used for any purpose (in bytes):");
break;
case VIRTIO_BALLOON_S_MEMTOT:
printf("The total amount of memory available (in bytes):");
break;
}
printf("%llu\n", (unsigned long long)stats[i].val);
}
printf("\n");
return 0;
}
int kvm_cmd_stat(int argc, const char **argv, const char *prefix)
{
int instance;
int r = 0;
parse_stat_options(argc, argv);
if (!mem)
usage_with_options(stat_usage, stat_options);
if (mem && all)
return kvm__enumerate_instances(do_memstat);
if (instance_name == NULL)
kvm_stat_help();
instance = kvm__get_sock_by_instance(instance_name);
if (instance <= 0)
die("Failed locating instance");
if (mem)
r = do_memstat(instance_name, instance);
close(instance);
return r;
}