-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmig_mon.c
237 lines (215 loc) · 6.47 KB
/
mig_mon.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "mig_mon.h"
const char *prog_name = NULL;
long n_cpus;
short mig_mon_port = MIG_MON_PORT;
/*
* huge_page_size stands for the real page size we used. page_size will always
* be the smallest page size of the system, as that's the size that guest
* hypervisor will track dirty.
*/
long page_size, huge_page_size;
const char *pattern_str[PATTERN_NUM] = { "sequential", "random", "once" };
void version(void)
{
printf("Version: %s\n", MIG_MON_VERSION);
puts("");
}
void usage(void)
{
puts("");
puts("This tool is a toolset of VM live migration testing & debugging.");
puts("For detailed usage, please try '-h/--help' for each sub-command.");
puts("");
puts("Usage:");
printf(" %s [-h|--help]\tShow full help message\n", prog_name);
usage_downtime_short();
usage_mm_dirty_short();
usage_vm_short();
puts("");
}
dirty_pattern parse_dirty_pattern(const char *str)
{
int i;
for (i = 0; i < PATTERN_NUM; i++) {
if (!strcmp(pattern_str[i], str)) {
return i;
}
}
/* Let's allow some short forms.. */
if (!strcmp(str, "seq"))
return PATTERN_SEQ;
else if (!strcmp(str, "ran"))
return PATTERN_RAND;
fprintf(stderr, "Dirty pattern unknown: %s\n", str);
exit(1);
}
int parse_huge_page_size(const char *size)
{
#ifdef __linux__
if (!strcmp(size, "2m") || !strcmp(size, "2M")) {
huge_page_size = 2UL << 20;
return MAP_HUGETLB | MAP_HUGE_2MB;
} else if (!strcmp(size, "1g") || !strcmp(size, "1G")) {
huge_page_size = 1UL << 30;
return MAP_HUGETLB | MAP_HUGE_1GB;
} else if (!strcmp(size, "4k") || !strcmp(size, "4K")) {
return 0;
} else {
printf("Unknown page size (%s), please specify 4K/2M/1G\n", size);
exit(1);
}
#else
printf("Specify page size is not supported on non-Linux arch yet.\n");
exit(1);
#endif
}
unsigned int vm_test_parse(const char *name)
{
if (!strcmp(name, "precopy"))
return VM_TEST_PRECOPY;
else if (!strcmp(name, "postcopy"))
return VM_TEST_POSTCOPY;
printf("Unknown vm test type: '%s'\n", name);
exit(1);
}
int main(int argc, char *argv[])
{
int ret = 0;
int interval_ms = MIG_MON_INT_DEF;
const char *work_mode = NULL;
const char *server_ip = NULL;
const char *spike_log = MIG_MON_SPIKE_LOG_DEF;
n_cpus = sysconf(_SC_NPROCESSORS_ONLN);
page_size = huge_page_size = getpagesize();
prog_name = argv[0];
if (argc == 1) {
usage();
version();
return -1;
}
srand(time(NULL));
work_mode = argv[1];
if (!strcmp(work_mode, "-h") || !strcmp(work_mode, "--help")) {
usage_downtime();
usage_mm_dirty();
usage_vm();
return -1;
} else if (!strcmp(work_mode, "-v") || !strcmp(work_mode, "--version")) {
version();
return -1;
} else if (!strcmp(work_mode, "server")) {
puts("starting server mode...");
if (argc >= 3) {
spike_log = argv[2];
}
ret = mon_server(spike_log, mon_server_callback);
} else if (!strcmp(work_mode, "client")) {
if (argc < 3) {
usage_downtime();
return -1;
}
server_ip = argv[2];
if (argc >= 4) {
interval_ms = strtol(argv[3], NULL, 10);
}
puts("starting client mode...");
printf("server ip: %s, interval: %d (ms)\n", server_ip, interval_ms);
ret = mon_client(server_ip, interval_ms, NULL, mon_client_callback);
} else if (!strcmp(work_mode, "server_rr")) {
printf("starting server_rr...\n");
ret = mon_server(NULL, mon_server_rr_callback);
} else if (!strcmp(work_mode, "client_rr")) {
if (argc < 3) {
usage_downtime();
return -1;
}
server_ip = argv[2];
if (argc >= 4) {
interval_ms = strtol(argv[3], NULL, 10);
}
if (argc >= 5) {
spike_log = argv[4];
}
ret = mon_client(server_ip, interval_ms, spike_log,
mon_client_rr_callback);
} else if (!strcmp(work_mode, "vm")) {
vm_args args = {
.target = EMULATE_NONE,
.vm_size = DEF_VM_SIZE,
};
int c;
while ((c = getopt(argc-1, argv+1, "dhp:st:H:S:")) != -1) {
switch (c) {
case 'd':
args.target = EMULATE_DST;
break;
case 'p':
mig_mon_port = atoi(optarg);
break;
case 's':
args.target = EMULATE_SRC;
break;
case 't':
args.tests |= vm_test_parse(optarg);
break;
case 'H':
args.src_target_ip = strdup(optarg);
break;
case 'S':
args.vm_size = atoi(optarg) * (1UL << 30);
break;
case 'h':
default:
usage_vm();
return -1;
}
}
ret = mon_vm(&args);
} else if (!strcmp(work_mode, "mm_dirty")) {
mm_dirty_args args = {
.dirty_rate = 0,
.mm_size = DEF_MM_DIRTY_SIZE,
.pattern = DEF_MM_DIRTY_PATTERN,
.map_flags = MAP_ANONYMOUS | MAP_PRIVATE,
.record_latencies = false,
};
int c;
while ((c = getopt(argc-1, argv+1, "hLm:p:P:r:")) != -1) {
switch (c) {
case 'm':
args.mm_size = parse_size_to_mega(optarg);
break;
case 'r':
args.dirty_rate = parse_size_to_mega(optarg);
break;
case 'p':
args.pattern = parse_dirty_pattern(optarg);
break;
case 'L':
args.record_latencies = true;
break;
case 'P':
args.map_flags |= parse_huge_page_size(optarg);
break;
case 'h':
default:
usage_mm_dirty();
return -1;
}
}
/*
* We should have consumed all parameters. This will dump an error if
* the user used the old mig_mon mm_dirty parameters.
*/
if (optind != argc-1) {
printf("Unknown extra parameters detected.\n");
usage_mm_dirty();
return -1;
}
ret = mon_mm_dirty(&args);
} else {
usage();
return -1;
}
return ret;
}