summaryrefslogtreecommitdiff
path: root/user/kvmtrace.c
blob: de3c1897f466082cdd536d43e03442bec7cd2e1b (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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
 * kvm tracing application
 *
 * This tool is used for collecting trace buffer data
 * for kvm trace.
 *
 * Based on blktrace 0.99.3
 *
 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
 * Copyright (C) 2008 Eric Liu <eric.e.liu@intel.com>
 *
 * This work is licensed under the GNU LGPL license, version 2.
 */

#define _GNU_SOURCE

#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/statfs.h>
#include <sys/poll.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <getopt.h>
#include <errno.h>
#include <sched.h>

#ifndef __user
#define __user
#endif
#include <linux/kvm.h>

static char kvmtrace_version[] = "0.1";

/*
 * You may want to increase this even more, if you are logging at a high
 * rate and see skipped/missed events
 */
#define BUF_SIZE	(512 * 1024)
#define BUF_NR		(8)

#define OFILE_BUF	(128 * 1024)

#define DEBUGFS_TYPE	0x64626720

#define max(a, b)	((a) > (b) ? (a) : (b))

#define S_OPTS	"r:o:w:?Vb:n:D:"
static struct option l_opts[] = {
	{
		.name = "relay",
		.has_arg = required_argument,
		.flag = NULL,
		.val = 'r'
	},
	{
		.name = "output",
		.has_arg = required_argument,
		.flag = NULL,
		.val = 'o'
	},
	{
		.name = "stopwatch",
		.has_arg = required_argument,
		.flag = NULL,
		.val = 'w'
	},
	{
		.name = "version",
		.has_arg = no_argument,
		.flag = NULL,
		.val = 'V'
	},
	{
		.name = "buffer-size",
		.has_arg = required_argument,
		.flag = NULL,
		.val = 'b'
	},
	{
		.name = "num-sub-buffers",
		.has_arg = required_argument,
		.flag = NULL,
		.val = 'n'
	},
	{
		.name = "output-dir",
		.has_arg = required_argument,
		.flag = NULL,
		.val = 'D'
	},
	{
		.name = NULL,
	}
};

struct thread_information {
	int cpu;
	pthread_t thread;

	int fd;
	char fn[MAXPATHLEN + 64];

	FILE *ofile;
	char *ofile_buffer;

	int (*get_subbuf)(struct thread_information *, unsigned int);
	int (*read_data)(struct thread_information *, void *, unsigned int);

	unsigned long long data_read;

	struct kvm_trace_information *trace_info;

	int exited;

	/*
	 * mmap controlled output files
	 */
	unsigned long long fs_size;
	unsigned long long fs_max_size;
	unsigned long fs_off;
	void *fs_buf;
	unsigned long fs_buf_len;

};

struct kvm_trace_information {
	int fd;
	volatile int trace_started;
	unsigned long lost_records;
	struct thread_information *threads;
	unsigned long buf_size;
	unsigned long buf_nr;
};

static struct kvm_trace_information trace_information;

static int ncpus;
static char default_debugfs_path[] = "/sys/kernel/debug";

/* command line option globals */
static char *debugfs_path;
static char *output_name;
static char *output_dir;
static int stop_watch;
static unsigned long buf_size = BUF_SIZE;
static unsigned long buf_nr = BUF_NR;
static unsigned int page_size;

#define for_each_cpu_online(cpu) \
	for (cpu = 0; cpu < ncpus; cpu++)
#define for_each_tip(tip, i) \
	for (i = 0, tip = trace_information.threads; i < ncpus; i++, tip++)

#define is_done()	(*(volatile int *)(&done))
static volatile int done;

#define is_trace_stopped()	(*(volatile int *)(&trace_stopped))
static volatile int trace_stopped;

static void exit_trace(int status);

static void handle_sigint(__attribute__((__unused__)) int sig)
{
	ioctl(trace_information.fd, KVM_TRACE_PAUSE);
	done = 1;
}

static int get_lost_records()
{
	int fd;
	char tmp[MAXPATHLEN + 64];

	snprintf(tmp, sizeof(tmp), "%s/kvm/lost_records", debugfs_path);
	fd = open(tmp, O_RDONLY);
	if (fd < 0) {
		/*
		 * this may be ok, if the kernel doesn't support dropped counts
		 */
		if (errno == ENOENT)
			return 0;

		fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
		return -1;
	}

	if (read(fd, tmp, sizeof(tmp)) < 0) {
		perror(tmp);
		close(fd);
		return -1;
	}
	close(fd);

	return atoi(tmp);
}

static void wait_for_data(struct thread_information *tip, int timeout)
{
	struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };

	while (!is_done()) {
		if (poll(&pfd, 1, timeout) < 0) {
			perror("poll");
			break;
		}
		if (pfd.revents & POLLIN)
			break;
	}
}

static int read_data(struct thread_information *tip, void *buf,
			  unsigned int len)
{
	int ret = 0;

	do {
		wait_for_data(tip, 100);

		ret = read(tip->fd, buf, len);

		if (!ret)
			continue;
		else if (ret > 0)
			return ret;
		else {
			if (errno != EAGAIN) {
				perror(tip->fn);
				fprintf(stderr, "Thread %d failed read of %s\n",
					tip->cpu, tip->fn);
				break;
			}
			continue;
		}
	} while (!is_done());

	return ret;

}

/*
 * For file output, truncate and mmap the file appropriately
 */
static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
{
	int ofd = fileno(tip->ofile);
	int ret;
	unsigned long nr;
	unsigned long size;

	/*
	 * extend file, if we have to. use chunks of 16 subbuffers.
	 */
	if (tip->fs_off + maxlen > tip->fs_buf_len) {
		if (tip->fs_buf) {
			munlock(tip->fs_buf, tip->fs_buf_len);
			munmap(tip->fs_buf, tip->fs_buf_len);
			tip->fs_buf = NULL;
		}

		tip->fs_off = tip->fs_size & (page_size - 1);
		nr = max(16, tip->trace_info->buf_nr);
		size = tip->trace_info->buf_size;
		tip->fs_buf_len = (nr * size) - tip->fs_off;
		tip->fs_max_size += tip->fs_buf_len;

		if (ftruncate(ofd, tip->fs_max_size) < 0) {
			perror("ftruncate");
			return -1;
		}

		tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
				   MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
		if (tip->fs_buf == MAP_FAILED) {
			perror("mmap");
			return -1;
		}
		mlock(tip->fs_buf, tip->fs_buf_len);
	}

	ret = tip->read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
	if (ret >= 0) {
		tip->data_read += ret;
		tip->fs_size += ret;
		tip->fs_off += ret;
		return 0;
	}

	return -1;
}

static void tip_ftrunc_final(struct thread_information *tip)
{
	/*
	 * truncate to right size and cleanup mmap
	 */
	if (tip->ofile) {
		int ofd = fileno(tip->ofile);

		if (tip->fs_buf)
			munmap(tip->fs_buf, tip->fs_buf_len);

		ftruncate(ofd, tip->fs_size);
	}
}

static void *thread_main(void *arg)
{
	struct thread_information *tip = arg;
	pid_t pid = getpid();
	cpu_set_t cpu_mask;

	CPU_ZERO(&cpu_mask);
	CPU_SET((tip->cpu), &cpu_mask);

	if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
		perror("sched_setaffinity");
		exit_trace(1);
	}

	snprintf(tip->fn, sizeof(tip->fn), "%s/kvm/trace%d",
			debugfs_path, tip->cpu);
	tip->fd = open(tip->fn, O_RDONLY);
	if (tip->fd < 0) {
		perror(tip->fn);
		fprintf(stderr, "Thread %d failed open of %s\n", tip->cpu,
			tip->fn);
		exit_trace(1);
	}
	while (!is_done()) {
		if (tip->get_subbuf(tip, tip->trace_info->buf_size) < 0)
			break;
	}

	/*
	 * trace is stopped, pull data until we get a short read
	 */
	while (tip->get_subbuf(tip, tip->trace_info->buf_size) > 0)
		;

	tip_ftrunc_final(tip);
	tip->exited = 1;
	return NULL;
}

static int fill_ofname(struct thread_information *tip, char *dst)
{
	struct stat sb;
	int len = 0;

	if (output_dir)
		len = sprintf(dst, "%s/", output_dir);
	else
		len = sprintf(dst, "./");

	if (stat(dst, &sb) < 0) {
		if (errno != ENOENT) {
			perror("stat");
			return 1;
		}
		if (mkdir(dst, 0755) < 0) {
			perror(dst);
			fprintf(stderr, "Can't make output dir\n");
			return 1;
		}
	}

	sprintf(dst + len, "%s.kvmtrace.%d", output_name, tip->cpu);

	return 0;
}

static void fill_ops(struct thread_information *tip)
{
	tip->get_subbuf = mmap_subbuf;
	tip->read_data = read_data;
}

static void close_thread(struct thread_information *tip)
{
	if (tip->fd != -1)
		close(tip->fd);
	if (tip->ofile)
		fclose(tip->ofile);
	if (tip->ofile_buffer)
		free(tip->ofile_buffer);

	tip->fd = -1;
	tip->ofile = NULL;
	tip->ofile_buffer = NULL;
}

static int tip_open_output(struct thread_information *tip)
{
	int mode, vbuf_size;
	char op[NAME_MAX];

	if (fill_ofname(tip, op))
		return 1;

	tip->ofile = fopen(op, "w+");
	mode = _IOFBF;
	vbuf_size = OFILE_BUF;

	if (tip->ofile == NULL) {
		perror(op);
		return 1;
	}

	tip->ofile_buffer = malloc(vbuf_size);
	if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
		perror("setvbuf");
		close_thread(tip);
		return 1;
	}

	fill_ops(tip);
	return 0;
}

static int start_threads(int cpu)
{
	struct thread_information *tip;

	tip = trace_information.threads + cpu;
	tip->cpu = cpu;
	tip->trace_info = &trace_information;
	tip->fd = -1;

	if (tip_open_output(tip))
	    return 1;

	if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
		perror("pthread_create");
		close_thread(tip);
		return 1;
	}

	return 0;
}

static void stop_threads()
{
	struct thread_information *tip;
	unsigned long ret;
	int i;

	for_each_tip(tip, i) {
		if (tip->thread)
			(void) pthread_join(tip->thread, (void *) &ret);
		close_thread(tip);
	}
}

static int start_trace(void)
{
	int fd;
	struct kvm_user_trace_setup kuts;

	fd = trace_information.fd = open("/dev/kvm", O_RDWR);
	if (fd == -1) {
		perror("/dev/kvm");
		return 1;
	}

	memset(&kuts, 0, sizeof(kuts));
	kuts.buf_size = trace_information.buf_size = buf_size;
	kuts.buf_nr = trace_information.buf_nr = buf_nr;

	if (ioctl(trace_information.fd , KVM_TRACE_ENABLE, &kuts) < 0) {
		perror("KVM_TRACE_ENABLE");
		close(fd);
		return 1;
	}
	trace_information.trace_started = 1;

	return 0;
}

static void cleanup_trace(void)
{
	if (trace_information.fd == -1)
		return;

	trace_information.lost_records = get_lost_records();

	if (trace_information.trace_started) {
		trace_information.trace_started = 0;
		if (ioctl(trace_information.fd, KVM_TRACE_DISABLE) < 0)
			perror("KVM_TRACE_DISABLE");
	}

	close(trace_information.fd);
	trace_information.fd  = -1;
}

static void stop_all_traces(void)
{
	if (!is_trace_stopped()) {
		trace_stopped = 1;
		stop_threads();
		cleanup_trace();
	}
}

static void exit_trace(int status)
{
	stop_all_traces();
	exit(status);
}

static int start_kvm_trace(void)
{
	int i, size;
	struct thread_information *tip;

	size = ncpus * sizeof(struct thread_information);
	tip = malloc(size);
	if (!tip) {
		fprintf(stderr, "Out of memory, threads (%d)\n", size);
		return 1;
	}
	memset(tip, 0, size);
	trace_information.threads = tip;

	if (start_trace())
		return 1;

	for_each_cpu_online(i) {
		if (start_threads(i)) {
			fprintf(stderr, "Failed to start worker threads\n");
			break;
		}
	}

	if (i != ncpus) {
		stop_threads();
		cleanup_trace();
		return 1;
	}

	return 0;
}

static void wait_for_threads(void)
{
	struct thread_information *tip;
	int i, tips_running;

	do {
		tips_running = 0;
		usleep(100000);

		for_each_tip(tip, i)
			tips_running += !tip->exited;

	} while (tips_running);
}

static void show_stats(void)
{
	struct thread_information *tip;
	unsigned long long data_read;
	int i;

	data_read = 0;
	for_each_tip(tip, i) {
		printf("  CPU%3d: %8llu KiB data\n",
			tip->cpu, (tip->data_read + 1023) >> 10);
		data_read += tip->data_read;
	}

	printf("  Total:  lost %lu, %8llu KiB data\n",
		trace_information.lost_records, (data_read + 1023) >> 10);

	if (trace_information.lost_records)
		fprintf(stderr, "You have lost records, "
				"consider using a larger buffer size (-b)\n");
}

static char usage_str[] = \
	"[ -r debugfs path ] [ -D output dir ] [ -b buffer size ]\n" \
	"[ -n number of buffers] [ -o <output file> ] [ -w time  ] [ -V ]\n\n" \
	"\t-r Path to mounted debugfs, defaults to /sys/kernel/debug\n" \
	"\t-o File(s) to send output to\n" \
	"\t-D Directory to prepend to output file names\n" \
	"\t-w Stop after defined time, in seconds\n" \
	"\t-b Sub buffer size in KiB\n" \
	"\t-n Number of sub buffers\n" \
	"\t-V Print program version info\n\n";

static void show_usage(char *prog)
{
	fprintf(stderr, "Usage: %s %s %s", prog, kvmtrace_version, usage_str);
	exit(EXIT_FAILURE);
}

void parse_args(int argc, char **argv)
{
	int c;

	while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
		switch (c) {
		case 'r':
			debugfs_path = optarg;
			break;
		case 'o':
			output_name = optarg;
			break;
		case 'w':
			stop_watch = atoi(optarg);
			if (stop_watch <= 0) {
				fprintf(stderr,
					"Invalid stopwatch value (%d secs)\n",
					stop_watch);
				exit(EXIT_FAILURE);
			}
			break;
		case 'V':
			printf("%s version %s\n", argv[0], kvmtrace_version);
			exit(EXIT_SUCCESS);
		case 'b':
			buf_size = strtoul(optarg, NULL, 10);
			if (buf_size <= 0 || buf_size > 16*1024) {
				fprintf(stderr,
					"Invalid buffer size (%lu)\n",
					buf_size);
				exit(EXIT_FAILURE);
			}
			buf_size <<= 10;
			break;
		case 'n':
			buf_nr = strtoul(optarg, NULL, 10);
			if (buf_nr <= 0) {
				fprintf(stderr,
					"Invalid buffer nr (%lu)\n", buf_nr);
				exit(EXIT_FAILURE);
			}
			break;
		case 'D':
			output_dir = optarg;
			break;
		default:
			show_usage(argv[0]);
		}
	}

	if (optind < argc || output_name == NULL)
		show_usage(argv[0]);
}

int main(int argc, char *argv[])
{
	struct statfs st;

	parse_args(argc, argv);

	if (!debugfs_path)
		debugfs_path = default_debugfs_path;

	if (statfs(debugfs_path, &st) < 0) {
		perror("statfs");
		fprintf(stderr, "%s does not appear to be a valid path\n",
			debugfs_path);
		return 1;
	} else if (st.f_type != (long) DEBUGFS_TYPE) {
		fprintf(stderr, "%s does not appear to be a debug filesystem,"
			" please mount debugfs.\n",
			debugfs_path);
		return 1;
	}

	page_size = getpagesize();

	ncpus = sysconf(_SC_NPROCESSORS_ONLN);
	if (ncpus < 0) {
		fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
		return 1;
	}

	signal(SIGINT, handle_sigint);
	signal(SIGHUP, handle_sigint);
	signal(SIGTERM, handle_sigint);
	signal(SIGALRM, handle_sigint);
	signal(SIGPIPE, SIG_IGN);

	if (start_kvm_trace() != 0)
		return 1;

	if (stop_watch)
		alarm(stop_watch);

	wait_for_threads();
	stop_all_traces();
	show_stats();

	return 0;
}