[PATCH] blktrace: fix get_subbuf() leak
[blktrace.git] / blktrace.c
... / ...
CommitLineData
1/*
2 * block queue tracing application
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#include <pthread.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <locale.h>
26#include <signal.h>
27#include <fcntl.h>
28#include <string.h>
29#include <sys/ioctl.h>
30#include <sys/param.h>
31#include <sys/statfs.h>
32#include <sys/poll.h>
33#include <sys/mman.h>
34#include <sys/socket.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <sched.h>
38#include <ctype.h>
39#include <getopt.h>
40#include <errno.h>
41#include <netinet/in.h>
42#include <arpa/inet.h>
43#include <netdb.h>
44#include <sys/sendfile.h>
45
46#include "blktrace.h"
47#include "barrier.h"
48
49static char blktrace_version[] = "0.99.1";
50
51/*
52 * You may want to increase this even more, if you are logging at a high
53 * rate and see skipped/missed events
54 */
55#define BUF_SIZE (512 * 1024)
56#define BUF_NR (4)
57
58#define OFILE_BUF (128 * 1024)
59
60#define DEBUGFS_TYPE 0x64626720
61
62#define S_OPTS "d:a:A:r:o:kw:Vb:n:D:lh:p:s"
63static struct option l_opts[] = {
64 {
65 .name = "dev",
66 .has_arg = required_argument,
67 .flag = NULL,
68 .val = 'd'
69 },
70 {
71 .name = "act-mask",
72 .has_arg = required_argument,
73 .flag = NULL,
74 .val = 'a'
75 },
76 {
77 .name = "set-mask",
78 .has_arg = required_argument,
79 .flag = NULL,
80 .val = 'A'
81 },
82 {
83 .name = "relay",
84 .has_arg = required_argument,
85 .flag = NULL,
86 .val = 'r'
87 },
88 {
89 .name = "output",
90 .has_arg = required_argument,
91 .flag = NULL,
92 .val = 'o'
93 },
94 {
95 .name = "kill",
96 .has_arg = no_argument,
97 .flag = NULL,
98 .val = 'k'
99 },
100 {
101 .name = "stopwatch",
102 .has_arg = required_argument,
103 .flag = NULL,
104 .val = 'w'
105 },
106 {
107 .name = "version",
108 .has_arg = no_argument,
109 .flag = NULL,
110 .val = 'V'
111 },
112 {
113 .name = "buffer-size",
114 .has_arg = required_argument,
115 .flag = NULL,
116 .val = 'b'
117 },
118 {
119 .name = "num-sub-buffers",
120 .has_arg = required_argument,
121 .flag = NULL,
122 .val = 'n'
123 },
124 {
125 .name = "output-dir",
126 .has_arg = required_argument,
127 .flag = NULL,
128 .val = 'D'
129 },
130 {
131 .name = "listen",
132 .has_arg = no_argument,
133 .flag = NULL,
134 .val = 'l'
135 },
136 {
137 .name = "host",
138 .has_arg = required_argument,
139 .flag = NULL,
140 .val = 'h'
141 },
142 {
143 .name = "port",
144 .has_arg = required_argument,
145 .flag = NULL,
146 .val = 'p'
147 },
148 {
149 .name = "no-sendfile",
150 .has_arg = no_argument,
151 .flag = NULL,
152 .val = 's'
153 },
154 {
155 .name = NULL,
156 }
157};
158
159struct tip_subbuf {
160 void *buf;
161 unsigned int len;
162 unsigned int max_len;
163};
164
165#define FIFO_SIZE (1024) /* should be plenty big! */
166#define CL_SIZE (128) /* cache line, any bigger? */
167
168struct tip_subbuf_fifo {
169 int tail __attribute__((aligned(CL_SIZE)));
170 int head __attribute__((aligned(CL_SIZE)));
171 struct tip_subbuf *q[FIFO_SIZE];
172};
173
174struct thread_information {
175 int cpu;
176 pthread_t thread;
177
178 int fd;
179 void *fd_buf;
180 char fn[MAXPATHLEN + 64];
181
182 FILE *ofile;
183 char *ofile_buffer;
184 off_t ofile_offset;
185 int ofile_stdout;
186 int ofile_mmap;
187
188 int (*get_subbuf)(struct thread_information *, unsigned int);
189 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
190 int (*read_data)(struct thread_information *, void *, unsigned int);
191
192 unsigned long events_processed;
193 unsigned long long data_read;
194 unsigned long long data_queued;
195 struct device_information *device;
196
197 int exited;
198
199 /*
200 * piped fifo buffers
201 */
202 struct tip_subbuf_fifo fifo;
203 struct tip_subbuf *leftover_ts;
204
205 /*
206 * mmap controlled output files
207 */
208 unsigned long long fs_size;
209 unsigned long long fs_max_size;
210 unsigned long fs_off;
211 void *fs_buf;
212 unsigned long fs_buf_len;
213
214 struct net_connection *nc;
215};
216
217struct device_information {
218 int fd;
219 char *path;
220 char buts_name[32];
221 volatile int trace_started;
222 unsigned long drop_count;
223 struct thread_information *threads;
224
225 struct cl_host *ch;
226 u32 cl_id;
227 time_t cl_connect_time;
228};
229
230static int ncpus;
231static struct thread_information *thread_information;
232static int ndevs;
233static struct device_information *device_information;
234
235/* command line option globals */
236static char *debugfs_path;
237static char *output_name;
238static char *output_dir;
239static int act_mask = ~0U;
240static int kill_running_trace;
241static unsigned long buf_size = BUF_SIZE;
242static unsigned long buf_nr = BUF_NR;
243static unsigned int page_size;
244
245#define is_done() (*(volatile int *)(&done))
246static volatile int done;
247
248#define is_trace_stopped() (*(volatile int *)(&trace_stopped))
249static volatile int trace_stopped;
250
251#define is_stat_shown() (*(volatile int *)(&stat_shown))
252static volatile int stat_shown;
253
254int data_is_native = -1;
255
256static void exit_trace(int status);
257
258#define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
259#define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
260
261#define __for_each_dip(__d, __di, __e, __i) \
262 for (__i = 0, __d = __di; __i < __e; __i++, __d++)
263
264#define for_each_dip(__d, __i) \
265 __for_each_dip(__d, device_information, ndevs, __i)
266#define for_each_nc_dip(__nc, __d, __i) \
267 __for_each_dip(__d, (__nc)->ch->device_information, (__nc)->ch->ndevs, __i)
268
269#define __for_each_tip(__d, __t, __ncpus, __j) \
270 for (__j = 0, __t = (__d)->threads; __j < __ncpus; __j++, __t++)
271#define for_each_tip(__d, __t, __j) \
272 __for_each_tip(__d, __t, ncpus, __j)
273#define for_each_cl_host(__c) \
274 for (__c = cl_host_list; __c; __c = __c->list_next)
275
276/*
277 * networking stuff follows. we include a magic number so we know whether
278 * to endianness convert or not
279 */
280struct blktrace_net_hdr {
281 u32 magic; /* same as trace magic */
282 char buts_name[32]; /* trace name */
283 u32 cpu; /* for which cpu */
284 u32 max_cpus;
285 u32 len; /* length of following trace data */
286 u32 cl_id; /* id for set of client per-cpu connections */
287};
288
289#define TRACE_NET_PORT (8462)
290
291enum {
292 Net_none = 0,
293 Net_server,
294 Net_client,
295};
296
297/*
298 * network cmd line params
299 */
300static char hostname[MAXHOSTNAMELEN];
301static int net_port = TRACE_NET_PORT;
302static int net_mode = 0;
303static int net_use_sendfile = 1;
304
305struct cl_host {
306 struct cl_host *list_next;
307 struct in_addr cl_in_addr;
308 struct net_connection *net_connections;
309 int nconn;
310 struct device_information *device_information;
311 int ndevs;
312 int ncpus;
313 int ndevs_done;
314};
315
316struct net_connection {
317 int in_fd;
318 struct pollfd pfd;
319 time_t connect_time;
320 struct cl_host *ch;
321 int ncpus;
322};
323
324#define NET_MAX_CL_HOSTS (1024)
325static struct cl_host *cl_host_list;
326static int cl_hosts;
327static int net_connects;
328
329static int *net_out_fd;
330
331static void handle_sigint(__attribute__((__unused__)) int sig)
332{
333 struct device_information *dip;
334 int i;
335
336 /*
337 * stop trace so we can reap currently produced data
338 */
339 for_each_dip(dip, i) {
340 if (dip->fd == -1)
341 continue;
342 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
343 perror("BLKTRACESTOP");
344 }
345
346 done = 1;
347}
348
349static int get_dropped_count(const char *buts_name)
350{
351 int fd;
352 char tmp[MAXPATHLEN + 64];
353
354 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
355 debugfs_path, buts_name);
356
357 fd = open(tmp, O_RDONLY);
358 if (fd < 0) {
359 /*
360 * this may be ok, if the kernel doesn't support dropped counts
361 */
362 if (errno == ENOENT)
363 return 0;
364
365 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
366 return -1;
367 }
368
369 if (read(fd, tmp, sizeof(tmp)) < 0) {
370 perror(tmp);
371 close(fd);
372 return -1;
373 }
374
375 close(fd);
376
377 return atoi(tmp);
378}
379
380static int start_trace(struct device_information *dip)
381{
382 struct blk_user_trace_setup buts;
383
384 memset(&buts, 0, sizeof(buts));
385 buts.buf_size = buf_size;
386 buts.buf_nr = buf_nr;
387 buts.act_mask = act_mask;
388
389 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
390 perror("BLKTRACESETUP");
391 return 1;
392 }
393
394 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
395 perror("BLKTRACESTART");
396 return 1;
397 }
398
399 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
400 dip_set_tracing(dip, 1);
401 return 0;
402}
403
404static void stop_trace(struct device_information *dip)
405{
406 if (dip_tracing(dip) || kill_running_trace) {
407 dip_set_tracing(dip, 0);
408
409 /*
410 * should be stopped, just don't complain if it isn't
411 */
412 ioctl(dip->fd, BLKTRACESTOP);
413
414 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
415 perror("BLKTRACETEARDOWN");
416
417 close(dip->fd);
418 dip->fd = -1;
419 }
420}
421
422static void stop_all_traces(void)
423{
424 struct device_information *dip;
425 int i;
426
427 for_each_dip(dip, i) {
428 dip->drop_count = get_dropped_count(dip->buts_name);
429 stop_trace(dip);
430 }
431}
432
433static void wait_for_data(struct thread_information *tip, int timeout)
434{
435 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
436
437 while (!is_done()) {
438 if (poll(&pfd, 1, timeout) < 0) {
439 perror("poll");
440 break;
441 }
442 if (pfd.revents & POLLIN)
443 break;
444 if (tip->ofile_stdout)
445 break;
446 }
447}
448
449static int read_data_file(struct thread_information *tip, void *buf,
450 unsigned int len)
451{
452 int ret = 0;
453
454 do {
455 wait_for_data(tip, 100);
456
457 ret = read(tip->fd, buf, len);
458 if (!ret)
459 continue;
460 else if (ret > 0)
461 return ret;
462 else {
463 if (errno != EAGAIN) {
464 perror(tip->fn);
465 fprintf(stderr,"Thread %d failed read of %s\n",
466 tip->cpu, tip->fn);
467 break;
468 }
469 continue;
470 }
471 } while (!is_done());
472
473 return ret;
474
475}
476
477static int read_data_net(struct thread_information *tip, void *buf,
478 unsigned int len)
479{
480 struct net_connection *nc = tip->nc;
481 unsigned int bytes_left = len;
482 int ret = 0;
483
484 do {
485 ret = recv(nc->in_fd, buf, bytes_left, MSG_WAITALL);
486
487 if (!ret)
488 continue;
489 else if (ret < 0) {
490 if (errno != EAGAIN) {
491 perror(tip->fn);
492 fprintf(stderr, "server: failed read\n");
493 return 0;
494 }
495 continue;
496 } else {
497 buf += ret;
498 bytes_left -= ret;
499 }
500 } while (!is_done() && bytes_left);
501
502 return len - bytes_left;
503}
504
505static inline struct tip_subbuf *
506subbuf_fifo_dequeue(struct thread_information *tip)
507{
508 const int head = tip->fifo.head;
509 const int next = (head + 1) & (FIFO_SIZE - 1);
510
511 if (head != tip->fifo.tail) {
512 struct tip_subbuf *ts = tip->fifo.q[head];
513
514 store_barrier();
515 tip->fifo.head = next;
516 return ts;
517 }
518
519 return NULL;
520}
521
522static inline int subbuf_fifo_queue(struct thread_information *tip,
523 struct tip_subbuf *ts)
524{
525 const int tail = tip->fifo.tail;
526 const int next = (tail + 1) & (FIFO_SIZE - 1);
527
528 if (next != tip->fifo.head) {
529 tip->fifo.q[tail] = ts;
530 store_barrier();
531 tip->fifo.tail = next;
532 return 0;
533 }
534
535 fprintf(stderr, "fifo too small!\n");
536 return 1;
537}
538
539/*
540 * For file output, truncate and mmap the file appropriately
541 */
542static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
543{
544 int ofd = fileno(tip->ofile);
545 int ret;
546
547 /*
548 * extend file, if we have to. use chunks of 16 subbuffers.
549 */
550 if (tip->fs_off + buf_size > tip->fs_buf_len) {
551 if (tip->fs_buf) {
552 munlock(tip->fs_buf, tip->fs_buf_len);
553 munmap(tip->fs_buf, tip->fs_buf_len);
554 tip->fs_buf = NULL;
555 }
556
557 tip->fs_off = tip->fs_size & (page_size - 1);
558 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
559 tip->fs_max_size += tip->fs_buf_len;
560
561 if (ftruncate(ofd, tip->fs_max_size) < 0) {
562 perror("ftruncate");
563 return -1;
564 }
565
566 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
567 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
568 if (tip->fs_buf == MAP_FAILED) {
569 perror("mmap");
570 return -1;
571 }
572 mlock(tip->fs_buf, tip->fs_buf_len);
573 }
574
575 ret = tip->read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
576 if (ret >= 0) {
577 tip->data_read += ret;
578 tip->fs_size += ret;
579 tip->fs_off += ret;
580 return 0;
581 }
582
583 return -1;
584}
585
586/*
587 * Use the copy approach for pipes and network
588 */
589static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
590{
591 struct tip_subbuf *ts = malloc(sizeof(*ts));
592 int ret;
593
594 ts->buf = malloc(buf_size);
595 ts->max_len = maxlen;
596
597 ret = tip->read_data(tip, ts->buf, ts->max_len);
598 if (ret > 0) {
599 ts->len = ret;
600 tip->data_read += ret;
601 if (subbuf_fifo_queue(tip, ts))
602 ret = -1;
603 }
604
605 if (ret <= 0) {
606 free(ts->buf);
607 free(ts);
608 }
609
610 return ret;
611}
612
613static void close_thread(struct thread_information *tip)
614{
615 if (tip->fd != -1)
616 close(tip->fd);
617 if (tip->ofile)
618 fclose(tip->ofile);
619 if (tip->ofile_buffer)
620 free(tip->ofile_buffer);
621 if (tip->fd_buf)
622 free(tip->fd_buf);
623
624 tip->fd = -1;
625 tip->ofile = NULL;
626 tip->ofile_buffer = NULL;
627 tip->fd_buf = NULL;
628}
629
630static void tip_ftrunc_final(struct thread_information *tip)
631{
632 /*
633 * truncate to right size and cleanup mmap
634 */
635 if (tip->ofile_mmap && tip->ofile) {
636 int ofd = fileno(tip->ofile);
637
638 if (tip->fs_buf)
639 munmap(tip->fs_buf, tip->fs_buf_len);
640
641 ftruncate(ofd, tip->fs_size);
642 }
643}
644
645static void *thread_main(void *arg)
646{
647 struct thread_information *tip = arg;
648 pid_t pid = getpid();
649 cpu_set_t cpu_mask;
650
651 CPU_ZERO(&cpu_mask);
652 CPU_SET((tip->cpu), &cpu_mask);
653
654 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
655 perror("sched_setaffinity");
656 exit_trace(1);
657 }
658
659 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
660 debugfs_path, tip->device->buts_name, tip->cpu);
661 tip->fd = open(tip->fn, O_RDONLY);
662 if (tip->fd < 0) {
663 perror(tip->fn);
664 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
665 tip->fn);
666 exit_trace(1);
667 }
668
669 while (!is_done()) {
670 if (tip->get_subbuf(tip, buf_size) < 0)
671 break;
672 }
673
674 /*
675 * trace is stopped, pull data until we get a short read
676 */
677 while (tip->get_subbuf(tip, buf_size) > 0)
678 ;
679
680 tip_ftrunc_final(tip);
681 tip->exited = 1;
682 return NULL;
683}
684
685static int write_data_net(int fd, void *buf, unsigned int buf_len)
686{
687 unsigned int bytes_left = buf_len;
688 int ret;
689
690 while (bytes_left) {
691 ret = send(fd, buf, bytes_left, 0);
692 if (ret < 0) {
693 perror("send");
694 return 1;
695 }
696
697 buf += ret;
698 bytes_left -= ret;
699 }
700
701 return 0;
702}
703
704static int net_send_header(struct thread_information *tip, unsigned int len)
705{
706 struct blktrace_net_hdr hdr;
707
708 hdr.magic = BLK_IO_TRACE_MAGIC;
709 strcpy(hdr.buts_name, tip->device->buts_name);
710 hdr.cpu = tip->cpu;
711 hdr.max_cpus = ncpus;
712 hdr.len = len;
713 hdr.cl_id = getpid();
714
715 return write_data_net(net_out_fd[tip->cpu], &hdr, sizeof(hdr));
716}
717
718/*
719 * send header with 0 length to signal end-of-run
720 */
721static void net_client_send_close(void)
722{
723 struct device_information *dip;
724 struct blktrace_net_hdr hdr;
725 int i;
726
727 for_each_dip(dip, i) {
728 hdr.magic = BLK_IO_TRACE_MAGIC;
729 hdr.max_cpus = ncpus;
730 hdr.len = 0;
731 strcpy(hdr.buts_name, dip->buts_name);
732 hdr.cpu = get_dropped_count(dip->buts_name);
733 hdr.cl_id = getpid();
734
735 write_data_net(net_out_fd[0], &hdr, sizeof(hdr));
736 }
737
738}
739
740static int flush_subbuf_net(struct thread_information *tip,
741 struct tip_subbuf *ts)
742{
743 if (net_send_header(tip, ts->len))
744 return -1;
745 if (write_data_net(net_out_fd[tip->cpu], ts->buf, ts->len))
746 return -1;
747
748 free(ts->buf);
749 free(ts);
750 return 1;
751}
752
753static int net_sendfile(struct thread_information *tip, struct tip_subbuf *ts)
754{
755 int ret = sendfile(net_out_fd[tip->cpu], tip->fd, NULL, ts->len);
756
757 if (ret < 0) {
758 perror("sendfile");
759 return 1;
760 } else if (ret < (int) ts->len) {
761 fprintf(stderr, "short sendfile send (%d of %d)\n", ret, ts->len);
762 return 1;
763 }
764
765 return 0;
766}
767
768static int flush_subbuf_sendfile(struct thread_information *tip,
769 struct tip_subbuf *ts)
770{
771 int ret = -1;
772
773 if (net_send_header(tip, ts->len))
774 goto err;
775 if (net_sendfile(tip, ts))
776 goto err;
777
778 tip->data_read += ts->len;
779 tip->ofile_offset += buf_size;
780 ret = 1;
781err:
782 free(ts);
783 return ret;
784}
785
786static int get_subbuf_sendfile(struct thread_information *tip,
787 __attribute__((__unused__)) unsigned int maxlen)
788{
789 struct tip_subbuf *ts;
790 struct stat sb;
791 unsigned int ready;
792
793 wait_for_data(tip, -1);
794
795 if (fstat(tip->fd, &sb) < 0) {
796 perror("trace stat");
797 return -1;
798 }
799
800 ready = sb.st_size - tip->data_queued;
801 if (!ready) {
802 usleep(1000);
803 return 0;
804 }
805
806 ts = malloc(sizeof(*ts));
807 ts->buf = NULL;
808 ts->max_len = 0;
809 ts->len = ready;
810 tip->data_queued += ready;
811
812 if (flush_subbuf_sendfile(tip, ts) < 0)
813 return -1;
814
815 return ready;
816}
817
818static int write_data(struct thread_information *tip, void *buf,
819 unsigned int buf_len)
820{
821 int ret;
822
823 if (!buf_len)
824 return 0;
825
826 while (1) {
827 ret = fwrite(buf, buf_len, 1, tip->ofile);
828 if (ret == 1)
829 break;
830
831 if (ret < 0) {
832 perror("write");
833 return 1;
834 }
835 }
836
837 if (tip->ofile_stdout)
838 fflush(tip->ofile);
839
840 return 0;
841}
842
843static int flush_subbuf_file(struct thread_information *tip,
844 struct tip_subbuf *ts)
845{
846 unsigned int offset = 0;
847 struct blk_io_trace *t;
848 int pdu_len, events = 0;
849
850 /*
851 * surplus from last run
852 */
853 if (tip->leftover_ts) {
854 struct tip_subbuf *prev_ts = tip->leftover_ts;
855
856 if (prev_ts->len + ts->len > prev_ts->max_len) {
857 prev_ts->max_len += ts->len;
858 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
859 }
860
861 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
862 prev_ts->len += ts->len;
863
864 free(ts->buf);
865 free(ts);
866
867 ts = prev_ts;
868 tip->leftover_ts = NULL;
869 }
870
871 while (offset + sizeof(*t) <= ts->len) {
872 t = ts->buf + offset;
873
874 if (verify_trace(t)) {
875 write_data(tip, ts->buf, offset);
876 return -1;
877 }
878
879 pdu_len = t->pdu_len;
880
881 if (offset + sizeof(*t) + pdu_len > ts->len)
882 break;
883
884 offset += sizeof(*t) + pdu_len;
885 tip->events_processed++;
886 tip->data_read += sizeof(*t) + pdu_len;
887 events++;
888 }
889
890 if (write_data(tip, ts->buf, offset))
891 return -1;
892
893 /*
894 * leftover bytes, save them for next time
895 */
896 if (offset != ts->len) {
897 tip->leftover_ts = ts;
898 ts->len -= offset;
899 memmove(ts->buf, ts->buf + offset, ts->len);
900 } else {
901 free(ts->buf);
902 free(ts);
903 }
904
905 return events;
906}
907
908static int write_tip_events(struct thread_information *tip)
909{
910 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
911
912 if (ts)
913 return tip->flush_subbuf(tip, ts);
914
915 return 0;
916}
917
918/*
919 * scans the tips we know and writes out the subbuffers we accumulate
920 */
921static void get_and_write_events(void)
922{
923 struct device_information *dip;
924 struct thread_information *tip;
925 int i, j, events, ret, tips_running;
926
927 while (!is_done()) {
928 events = 0;
929
930 for_each_dip(dip, i) {
931 for_each_tip(dip, tip, j) {
932 ret = write_tip_events(tip);
933 if (ret > 0)
934 events += ret;
935 }
936 }
937
938 if (!events)
939 usleep(100000);
940 }
941
942 /*
943 * reap stored events
944 */
945 do {
946 events = 0;
947 tips_running = 0;
948 for_each_dip(dip, i) {
949 for_each_tip(dip, tip, j) {
950 ret = write_tip_events(tip);
951 if (ret > 0)
952 events += ret;
953 tips_running += !tip->exited;
954 }
955 }
956 usleep(10);
957 } while (events || tips_running);
958}
959
960static void wait_for_threads(void)
961{
962 /*
963 * for piped or network output, poll and fetch data for writeout.
964 * for files, we just wait around for trace threads to exit
965 */
966 if ((output_name && !strcmp(output_name, "-")) ||
967 ((net_mode == Net_client) && !net_use_sendfile))
968 get_and_write_events();
969 else {
970 struct device_information *dip;
971 struct thread_information *tip;
972 int i, j, tips_running;
973
974 do {
975 tips_running = 0;
976 usleep(100000);
977
978 for_each_dip(dip, i)
979 for_each_tip(dip, tip, j)
980 tips_running += !tip->exited;
981 } while (tips_running);
982 }
983
984 if (net_mode == Net_client)
985 net_client_send_close();
986}
987
988static int fill_ofname(struct device_information *dip,
989 struct thread_information *tip, char *dst,
990 char *buts_name)
991{
992 struct stat sb;
993 int len = 0;
994
995 if (output_dir)
996 len = sprintf(dst, "%s/", output_dir);
997 else
998 len = sprintf(dst, "./");
999
1000 if (net_mode == Net_server) {
1001 struct net_connection *nc = tip->nc;
1002
1003 len += sprintf(dst + len, "%s-", inet_ntoa(nc->ch->cl_in_addr));
1004 len += strftime(dst + len, 64, "%F-%T/", gmtime(&dip->cl_connect_time));
1005 }
1006
1007 if (stat(dst, &sb) < 0) {
1008 if (errno != ENOENT) {
1009 perror("stat");
1010 return 1;
1011 }
1012 if (mkdir(dst, 0755) < 0) {
1013 perror(dst);
1014 fprintf(stderr, "Can't make output dir\n");
1015 return 1;
1016 }
1017 }
1018
1019 if (output_name)
1020 sprintf(dst + len, "%s.blktrace.%d", output_name, tip->cpu);
1021 else
1022 sprintf(dst + len, "%s.blktrace.%d", buts_name, tip->cpu);
1023
1024 return 0;
1025}
1026
1027static void fill_ops(struct thread_information *tip)
1028{
1029 /*
1030 * setup ops
1031 */
1032 if (net_mode == Net_client) {
1033 if (net_use_sendfile) {
1034 tip->get_subbuf = get_subbuf_sendfile;
1035 tip->flush_subbuf = NULL;
1036 } else {
1037 tip->get_subbuf = get_subbuf;
1038 tip->flush_subbuf = flush_subbuf_net;
1039 }
1040 } else {
1041 if (tip->ofile_mmap)
1042 tip->get_subbuf = mmap_subbuf;
1043 else
1044 tip->get_subbuf = get_subbuf;
1045
1046 tip->flush_subbuf = flush_subbuf_file;
1047 }
1048
1049 if (net_mode == Net_server)
1050 tip->read_data = read_data_net;
1051 else
1052 tip->read_data = read_data_file;
1053}
1054
1055static int tip_open_output(struct device_information *dip,
1056 struct thread_information *tip)
1057{
1058 int pipeline = output_name && !strcmp(output_name, "-");
1059 int mode, vbuf_size;
1060 char op[128];
1061
1062 if (net_mode == Net_client) {
1063 tip->ofile = NULL;
1064 tip->ofile_stdout = 0;
1065 tip->ofile_mmap = 0;
1066 goto done;
1067 } else if (pipeline) {
1068 tip->ofile = fdopen(STDOUT_FILENO, "w");
1069 tip->ofile_stdout = 1;
1070 tip->ofile_mmap = 0;
1071 mode = _IOLBF;
1072 vbuf_size = 512;
1073 } else {
1074 if (fill_ofname(dip, tip, op, dip->buts_name))
1075 return 1;
1076 tip->ofile = fopen(op, "w+");
1077 tip->ofile_stdout = 0;
1078 tip->ofile_mmap = 1;
1079 mode = _IOFBF;
1080 vbuf_size = OFILE_BUF;
1081 }
1082
1083 if (tip->ofile == NULL) {
1084 perror(op);
1085 return 1;
1086 }
1087
1088 tip->ofile_buffer = malloc(vbuf_size);
1089 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
1090 perror("setvbuf");
1091 close_thread(tip);
1092 return 1;
1093 }
1094
1095done:
1096 fill_ops(tip);
1097 return 0;
1098}
1099
1100static int start_threads(struct device_information *dip)
1101{
1102 struct thread_information *tip;
1103 int j;
1104
1105 for_each_tip(dip, tip, j) {
1106 tip->cpu = j;
1107 tip->device = dip;
1108 tip->events_processed = 0;
1109 tip->fd = -1;
1110 memset(&tip->fifo, 0, sizeof(tip->fifo));
1111 tip->leftover_ts = NULL;
1112
1113 if (tip_open_output(dip, tip))
1114 return 1;
1115
1116 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
1117 perror("pthread_create");
1118 close_thread(tip);
1119 return 1;
1120 }
1121 }
1122
1123 return 0;
1124}
1125
1126static void stop_threads(struct device_information *dip)
1127{
1128 struct thread_information *tip;
1129 unsigned long ret;
1130 int i;
1131
1132 for_each_tip(dip, tip, i) {
1133 (void) pthread_join(tip->thread, (void *) &ret);
1134 close_thread(tip);
1135 }
1136}
1137
1138static void stop_all_threads(void)
1139{
1140 struct device_information *dip;
1141 int i;
1142
1143 for_each_dip(dip, i)
1144 stop_threads(dip);
1145}
1146
1147static void stop_all_tracing(void)
1148{
1149 struct device_information *dip;
1150 int i;
1151
1152 for_each_dip(dip, i)
1153 stop_trace(dip);
1154}
1155
1156static void exit_trace(int status)
1157{
1158 if (!is_trace_stopped()) {
1159 trace_stopped = 1;
1160 stop_all_threads();
1161 stop_all_tracing();
1162 }
1163
1164 exit(status);
1165}
1166
1167static int resize_devices(char *path)
1168{
1169 int size = (ndevs + 1) * sizeof(struct device_information);
1170
1171 device_information = realloc(device_information, size);
1172 if (!device_information) {
1173 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1174 return 1;
1175 }
1176 device_information[ndevs].path = path;
1177 ndevs++;
1178 return 0;
1179}
1180
1181static int open_devices(void)
1182{
1183 struct device_information *dip;
1184 int i;
1185
1186 for_each_dip(dip, i) {
1187 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1188 if (dip->fd < 0) {
1189 perror(dip->path);
1190 return 1;
1191 }
1192 }
1193
1194 return 0;
1195}
1196
1197static int start_devices(void)
1198{
1199 struct device_information *dip;
1200 int i, j, size;
1201
1202 size = ncpus * sizeof(struct thread_information);
1203 thread_information = malloc(size * ndevs);
1204 if (!thread_information) {
1205 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1206 return 1;
1207 }
1208
1209 for_each_dip(dip, i) {
1210 if (start_trace(dip)) {
1211 close(dip->fd);
1212 fprintf(stderr, "Failed to start trace on %s\n",
1213 dip->path);
1214 break;
1215 }
1216 }
1217
1218 if (i != ndevs) {
1219 __for_each_dip(dip, device_information, i, j)
1220 stop_trace(dip);
1221
1222 return 1;
1223 }
1224
1225 for_each_dip(dip, i) {
1226 dip->threads = thread_information + (i * ncpus);
1227 if (start_threads(dip)) {
1228 fprintf(stderr, "Failed to start worker threads\n");
1229 break;
1230 }
1231 }
1232
1233 if (i != ndevs) {
1234 __for_each_dip(dip, device_information, i, j)
1235 stop_threads(dip);
1236 for_each_dip(dip, i)
1237 stop_trace(dip);
1238
1239 return 1;
1240 }
1241
1242 return 0;
1243}
1244
1245static void show_stats(struct device_information *dips, int ndips, int cpus)
1246{
1247 struct device_information *dip;
1248 struct thread_information *tip;
1249 unsigned long long events_processed, data_read;
1250 unsigned long total_drops;
1251 int i, j, no_stdout = 0;
1252
1253 if (is_stat_shown())
1254 return;
1255
1256 if (output_name && !strcmp(output_name, "-"))
1257 no_stdout = 1;
1258
1259 stat_shown = 1;
1260
1261 total_drops = 0;
1262 __for_each_dip(dip, dips, ndips, i) {
1263 if (!no_stdout)
1264 printf("Device: %s\n", dip->path);
1265 events_processed = 0;
1266 data_read = 0;
1267 __for_each_tip(dip, tip, cpus, j) {
1268 if (!no_stdout)
1269 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1270 tip->cpu, tip->events_processed,
1271 (tip->data_read + 1023) >> 10);
1272 events_processed += tip->events_processed;
1273 data_read += tip->data_read;
1274 }
1275 total_drops += dip->drop_count;
1276 if (!no_stdout)
1277 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1278 events_processed, dip->drop_count,
1279 (data_read + 1023) >> 10);
1280 }
1281
1282 if (total_drops)
1283 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1284}
1285
1286static struct device_information *net_get_dip(struct net_connection *nc,
1287 char *buts_name, u32 cl_id)
1288{
1289 struct device_information *dip, *cl_dip = NULL;
1290 struct cl_host *ch = nc->ch;
1291 int i;
1292
1293 for (i = 0; i < ch->ndevs; i++) {
1294 dip = &ch->device_information[i];
1295
1296 if (!strcmp(dip->buts_name, buts_name))
1297 return dip;
1298
1299 if (dip->cl_id == cl_id)
1300 cl_dip = dip;
1301 }
1302
1303 ch->device_information = realloc(ch->device_information, (ch->ndevs + 1) * sizeof(*dip));
1304 dip = &ch->device_information[ch->ndevs];
1305 memset(dip, 0, sizeof(*dip));
1306 dip->fd = -1;
1307 dip->ch = ch;
1308 dip->cl_id = cl_id;
1309 if (cl_dip)
1310 dip->cl_connect_time = cl_dip->cl_connect_time;
1311 else
1312 dip->cl_connect_time = nc->connect_time;
1313 strcpy(dip->buts_name, buts_name);
1314 dip->path = strdup(buts_name);
1315 dip->trace_started = 1;
1316 ch->ndevs++;
1317 dip->threads = malloc(nc->ncpus * sizeof(struct thread_information));
1318 memset(dip->threads, 0, nc->ncpus * sizeof(struct thread_information));
1319
1320 /*
1321 * open all files
1322 */
1323 for (i = 0; i < nc->ncpus; i++) {
1324 struct thread_information *tip = &dip->threads[i];
1325
1326 tip->cpu = i;
1327 tip->device = dip;
1328 tip->fd = -1;
1329 tip->nc = nc;
1330
1331 if (tip_open_output(dip, tip))
1332 return NULL;
1333
1334 tip->nc = NULL;
1335 }
1336
1337 return dip;
1338}
1339
1340static struct thread_information *net_get_tip(struct net_connection *nc,
1341 struct blktrace_net_hdr *bnh)
1342{
1343 struct device_information *dip;
1344 struct thread_information *tip;
1345
1346 dip = net_get_dip(nc, bnh->buts_name, bnh->cl_id);
1347 if (!dip->trace_started) {
1348 fprintf(stderr, "Events for closed devices %s\n", dip->buts_name);
1349 return NULL;
1350 }
1351
1352 tip = &dip->threads[bnh->cpu];
1353 if (!tip->nc)
1354 tip->nc = nc;
1355
1356 return tip;
1357}
1358
1359static int net_get_header(struct net_connection *nc,
1360 struct blktrace_net_hdr *bnh)
1361{
1362 int fl = fcntl(nc->in_fd, F_GETFL);
1363 int bytes_left, ret;
1364 void *p = bnh;
1365
1366 fcntl(nc->in_fd, F_SETFL, fl | O_NONBLOCK);
1367 bytes_left = sizeof(*bnh);
1368 while (bytes_left && !is_done()) {
1369 ret = recv(nc->in_fd, p, bytes_left, MSG_WAITALL);
1370 if (ret < 0) {
1371 if (errno != EAGAIN) {
1372 perror("recv header");
1373 return 1;
1374 }
1375 usleep(1000);
1376 continue;
1377 } else if (!ret) {
1378 usleep(1000);
1379 continue;
1380 } else {
1381 p += ret;
1382 bytes_left -= ret;
1383 }
1384 }
1385 fcntl(nc->in_fd, F_SETFL, fl & ~O_NONBLOCK);
1386 return bytes_left;
1387}
1388
1389/*
1390 * finalize a net client: truncate files, show stats, cleanup, etc
1391 */
1392static void device_done(struct net_connection *nc, struct device_information *dip)
1393{
1394 struct thread_information *tip;
1395 int i;
1396
1397 __for_each_tip(dip, tip, nc->ncpus, i)
1398 tip_ftrunc_final(tip);
1399
1400 show_stats(dip, 1, nc->ncpus);
1401
1402 /*
1403 * cleanup for next run
1404 */
1405 __for_each_tip(dip, tip, nc->ncpus, i) {
1406 if (tip->ofile)
1407 fclose(tip->ofile);
1408 }
1409
1410 free(dip->threads);
1411 free(dip->path);
1412
1413 close(nc->in_fd);
1414 nc->in_fd = -1;
1415
1416 stat_shown = 0;
1417}
1418
1419static inline int in_addr_eq(struct in_addr a, struct in_addr b)
1420{
1421 return a.s_addr == b.s_addr;
1422}
1423
1424static void net_add_client_host(struct cl_host *ch)
1425{
1426 ch->list_next = cl_host_list;
1427 cl_host_list = ch;
1428 cl_hosts++;
1429}
1430
1431static void net_remove_client_host(struct cl_host *ch)
1432{
1433 struct cl_host *p, *c;
1434
1435 for (p = c = cl_host_list; c; c = c->list_next) {
1436 if (c == ch) {
1437 if (p == c)
1438 cl_host_list = c->list_next;
1439 else
1440 p->list_next = c->list_next;
1441 cl_hosts--;
1442 return;
1443 }
1444 p = c;
1445 }
1446}
1447
1448static struct cl_host *net_find_client_host(struct in_addr cl_in_addr)
1449{
1450 struct cl_host *ch = cl_host_list;
1451
1452 while (ch) {
1453 if (in_addr_eq(ch->cl_in_addr, cl_in_addr))
1454 return ch;
1455 ch = ch->list_next;
1456 }
1457
1458 return NULL;
1459}
1460
1461static void net_client_host_done(struct cl_host *ch)
1462{
1463 free(ch->device_information);
1464 free(ch->net_connections);
1465 net_connects -= ch->nconn;
1466 net_remove_client_host(ch);
1467 free(ch);
1468}
1469
1470/*
1471 * handle incoming events from a net client
1472 */
1473static int net_client_data(struct net_connection *nc)
1474{
1475 struct thread_information *tip;
1476 struct blktrace_net_hdr bnh;
1477
1478 if (net_get_header(nc, &bnh))
1479 return 1;
1480
1481 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1482 fprintf(stderr, "server: received data is bad\n");
1483 return 1;
1484 }
1485
1486 if (!data_is_native) {
1487 bnh.magic = be32_to_cpu(bnh.magic);
1488 bnh.cpu = be32_to_cpu(bnh.cpu);
1489 bnh.max_cpus = be32_to_cpu(bnh.max_cpus);
1490 bnh.len = be32_to_cpu(bnh.len);
1491 bnh.cl_id = be32_to_cpu(bnh.cl_id);
1492 }
1493
1494 if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1495 fprintf(stderr, "server: bad data magic\n");
1496 return 1;
1497 }
1498
1499 if (nc->ncpus == -1)
1500 nc->ncpus = bnh.max_cpus;
1501
1502 /*
1503 * len == 0 means that the other end signalled end-of-run
1504 */
1505 if (!bnh.len) {
1506 /*
1507 * overload cpu count with dropped events
1508 */
1509 struct device_information *dip;
1510
1511 dip = net_get_dip(nc, bnh.buts_name, bnh.cl_id);
1512 dip->drop_count = bnh.cpu;
1513 dip->trace_started = 0;
1514
1515 printf("server: end of run for %s\n", dip->buts_name);
1516
1517 device_done(nc, dip);
1518
1519 if (++nc->ch->ndevs_done == nc->ch->ndevs)
1520 net_client_host_done(nc->ch);
1521
1522 return 0;
1523 }
1524
1525 tip = net_get_tip(nc, &bnh);
1526 if (!tip)
1527 return 1;
1528
1529 if (mmap_subbuf(tip, bnh.len))
1530 return 1;
1531
1532 return 0;
1533}
1534
1535static void net_add_connection(int listen_fd, struct sockaddr_in *addr)
1536{
1537 socklen_t socklen = sizeof(*addr);
1538 struct net_connection *nc;
1539 struct cl_host *ch;
1540 int in_fd;
1541
1542 in_fd = accept(listen_fd, (struct sockaddr *) addr, &socklen);
1543 if (in_fd < 0) {
1544 perror("accept");
1545 return;
1546 }
1547
1548 ch = net_find_client_host(addr->sin_addr);
1549 if (!ch) {
1550 if (cl_hosts == NET_MAX_CL_HOSTS) {
1551 fprintf(stderr, "server: no more clients allowed\n");
1552 return;
1553 }
1554 ch = malloc(sizeof(struct cl_host));
1555 memset(ch, 0, sizeof(*ch));
1556 ch->cl_in_addr = addr->sin_addr;
1557 net_add_client_host(ch);
1558
1559 printf("server: connection from %s\n", inet_ntoa(addr->sin_addr));
1560 }
1561
1562 ch->net_connections = realloc(ch->net_connections, (ch->nconn + 1) * sizeof(*nc));
1563 nc = &ch->net_connections[ch->nconn++];
1564 memset(nc, 0, sizeof(*nc));
1565
1566 time(&nc->connect_time);
1567 nc->ch = ch;
1568 nc->in_fd = in_fd;
1569 nc->ncpus = -1;
1570 net_connects++;
1571}
1572
1573/*
1574 * event driven loop, handle new incoming connections and data from
1575 * existing connections
1576 */
1577static void net_server_handle_connections(int listen_fd,
1578 struct sockaddr_in *addr)
1579{
1580 struct pollfd *pfds = NULL;
1581 struct net_connection **ncs = NULL;
1582 int max_connects = 0;
1583 int i, nconns, events;
1584 struct cl_host *ch;
1585 struct net_connection *nc;
1586
1587 printf("server: waiting for connections...\n");
1588
1589 while (!is_done()) {
1590 if (net_connects >= max_connects) {
1591 pfds = realloc(pfds, (net_connects + 1) * sizeof(*pfds));
1592 ncs = realloc(ncs, (net_connects + 1) * sizeof(*ncs));
1593 max_connects = net_connects + 1;
1594 }
1595 /*
1596 * the zero entry is for incoming connections, remaining
1597 * entries for clients
1598 */
1599 pfds[0].fd = listen_fd;
1600 pfds[0].events = POLLIN;
1601 nconns = 0;
1602 for_each_cl_host(ch) {
1603 for (i = 0; i < ch->nconn; i++) {
1604 nc = &ch->net_connections[i];
1605 pfds[nconns + 1].fd = nc->in_fd;
1606 pfds[nconns + 1].events = POLLIN;
1607 ncs[nconns++] = nc;
1608 }
1609 }
1610
1611 events = poll(pfds, 1 + nconns, -1);
1612 if (events < 0) {
1613 if (errno == EINTR)
1614 continue;
1615
1616 perror("poll");
1617 break;
1618 } else if (!events)
1619 continue;
1620
1621 if (pfds[0].revents & POLLIN) {
1622 net_add_connection(listen_fd, addr);
1623 events--;
1624 }
1625
1626 for (i = 0; events && i < nconns; i++) {
1627 if (pfds[i + 1].revents & POLLIN) {
1628 net_client_data(ncs[i]);
1629 events--;
1630 }
1631 }
1632 }
1633}
1634
1635/*
1636 * Start here when we are in server mode - just fetch data from the network
1637 * and dump to files
1638 */
1639static int net_server(void)
1640{
1641 struct sockaddr_in addr;
1642 int fd, opt;
1643
1644 fd = socket(AF_INET, SOCK_STREAM, 0);
1645 if (fd < 0) {
1646 perror("server: socket");
1647 return 1;
1648 }
1649
1650 opt = 1;
1651 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1652 perror("setsockopt");
1653 return 1;
1654 }
1655
1656 memset(&addr, 0, sizeof(addr));
1657 addr.sin_family = AF_INET;
1658 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1659 addr.sin_port = htons(net_port);
1660
1661 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1662 perror("bind");
1663 return 1;
1664 }
1665
1666 if (listen(fd, 1) < 0) {
1667 perror("listen");
1668 return 1;
1669 }
1670
1671 net_server_handle_connections(fd, &addr);
1672 return 0;
1673}
1674
1675/*
1676 * Setup outgoing network connection where we will transmit data
1677 */
1678static int net_setup_client_cpu(int i, struct sockaddr_in *addr)
1679{
1680 int fd;
1681
1682 fd = socket(AF_INET, SOCK_STREAM, 0);
1683 if (fd < 0) {
1684 perror("client: socket");
1685 return 1;
1686 }
1687
1688 if (connect(fd, (struct sockaddr *) addr, sizeof(*addr)) < 0) {
1689 perror("client: connect");
1690 return 1;
1691 }
1692
1693 net_out_fd[i] = fd;
1694 return 0;
1695}
1696
1697static int net_setup_client(void)
1698{
1699 struct sockaddr_in addr;
1700 int i;
1701
1702 memset(&addr, 0, sizeof(addr));
1703 addr.sin_family = AF_INET;
1704 addr.sin_port = htons(net_port);
1705
1706 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1707 struct hostent *hent = gethostbyname(hostname);
1708 if (!hent) {
1709 perror("gethostbyname");
1710 return 1;
1711 }
1712
1713 memcpy(&addr.sin_addr, hent->h_addr, 4);
1714 strcpy(hostname, hent->h_name);
1715 }
1716
1717 printf("blktrace: connecting to %s\n", hostname);
1718
1719 net_out_fd = malloc(ncpus * sizeof(*net_out_fd));
1720 for (i = 0; i < ncpus; i++) {
1721 if (net_setup_client_cpu(i, &addr))
1722 return 1;
1723 }
1724
1725 printf("blktrace: connected!\n");
1726
1727 return 0;
1728}
1729
1730static char usage_str[] = \
1731 "-d <dev> [ -r debugfs path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1732 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1733 "\t-d Use specified device. May also be given last after options\n" \
1734 "\t-r Path to mounted debugfs, defaults to /debug\n" \
1735 "\t-o File(s) to send output to\n" \
1736 "\t-D Directory to prepend to output file names\n" \
1737 "\t-k Kill a running trace\n" \
1738 "\t-w Stop after defined time, in seconds\n" \
1739 "\t-a Only trace specified actions. See documentation\n" \
1740 "\t-A Give trace mask as a single value. See documentation\n" \
1741 "\t-b Sub buffer size in KiB\n" \
1742 "\t-n Number of sub buffers\n" \
1743 "\t-l Run in network listen mode (blktrace server)\n" \
1744 "\t-h Run in network client mode, connecting to the given host\n" \
1745 "\t-p Network port to use (default 8462)\n" \
1746 "\t-s Make the network client NOT use sendfile() to transfer data\n" \
1747 "\t-V Print program version info\n\n";
1748
1749static void show_usage(char *program)
1750{
1751 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1752}
1753
1754int main(int argc, char *argv[])
1755{
1756 static char default_debugfs_path[] = "/debug";
1757 struct statfs st;
1758 int i, c;
1759 int stop_watch = 0;
1760 int act_mask_tmp = 0;
1761
1762 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1763 switch (c) {
1764 case 'a':
1765 i = find_mask_map(optarg);
1766 if (i < 0) {
1767 fprintf(stderr,"Invalid action mask %s\n",
1768 optarg);
1769 return 1;
1770 }
1771 act_mask_tmp |= i;
1772 break;
1773
1774 case 'A':
1775 if ((sscanf(optarg, "%x", &i) != 1) ||
1776 !valid_act_opt(i)) {
1777 fprintf(stderr,
1778 "Invalid set action mask %s/0x%x\n",
1779 optarg, i);
1780 return 1;
1781 }
1782 act_mask_tmp = i;
1783 break;
1784
1785 case 'd':
1786 if (resize_devices(optarg) != 0)
1787 return 1;
1788 break;
1789
1790 case 'r':
1791 debugfs_path = optarg;
1792 break;
1793
1794 case 'o':
1795 output_name = optarg;
1796 break;
1797 case 'k':
1798 kill_running_trace = 1;
1799 break;
1800 case 'w':
1801 stop_watch = atoi(optarg);
1802 if (stop_watch <= 0) {
1803 fprintf(stderr,
1804 "Invalid stopwatch value (%d secs)\n",
1805 stop_watch);
1806 return 1;
1807 }
1808 break;
1809 case 'V':
1810 printf("%s version %s\n", argv[0], blktrace_version);
1811 return 0;
1812 case 'b':
1813 buf_size = strtoul(optarg, NULL, 10);
1814 if (buf_size <= 0 || buf_size > 16*1024) {
1815 fprintf(stderr,
1816 "Invalid buffer size (%lu)\n",buf_size);
1817 return 1;
1818 }
1819 buf_size <<= 10;
1820 break;
1821 case 'n':
1822 buf_nr = strtoul(optarg, NULL, 10);
1823 if (buf_nr <= 0) {
1824 fprintf(stderr,
1825 "Invalid buffer nr (%lu)\n", buf_nr);
1826 return 1;
1827 }
1828 break;
1829 case 'D':
1830 output_dir = optarg;
1831 break;
1832 case 'h':
1833 net_mode = Net_client;
1834 strcpy(hostname, optarg);
1835 break;
1836 case 'l':
1837 net_mode = Net_server;
1838 break;
1839 case 'p':
1840 net_port = atoi(optarg);
1841 break;
1842 case 's':
1843 net_use_sendfile = 0;
1844 break;
1845 default:
1846 show_usage(argv[0]);
1847 return 1;
1848 }
1849 }
1850
1851 setlocale(LC_NUMERIC, "en_US");
1852
1853 page_size = getpagesize();
1854
1855 if (net_mode == Net_server)
1856 return net_server();
1857
1858 while (optind < argc) {
1859 if (resize_devices(argv[optind++]) != 0)
1860 return 1;
1861 }
1862
1863 if (ndevs == 0) {
1864 show_usage(argv[0]);
1865 return 1;
1866 }
1867
1868 if (act_mask_tmp != 0)
1869 act_mask = act_mask_tmp;
1870
1871 if (!debugfs_path)
1872 debugfs_path = default_debugfs_path;
1873
1874 if (statfs(debugfs_path, &st) < 0) {
1875 perror("statfs");
1876 fprintf(stderr,"%s does not appear to be a valid path\n",
1877 debugfs_path);
1878 return 1;
1879 } else if (st.f_type != (long) DEBUGFS_TYPE) {
1880 fprintf(stderr,"%s does not appear to be a debug filesystem\n",
1881 debugfs_path);
1882 return 1;
1883 }
1884
1885 if (open_devices() != 0)
1886 return 1;
1887
1888 if (kill_running_trace) {
1889 stop_all_traces();
1890 return 0;
1891 }
1892
1893 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1894 if (ncpus < 0) {
1895 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1896 return 1;
1897 }
1898
1899 signal(SIGINT, handle_sigint);
1900 signal(SIGHUP, handle_sigint);
1901 signal(SIGTERM, handle_sigint);
1902 signal(SIGALRM, handle_sigint);
1903
1904 if (net_mode == Net_client && net_setup_client())
1905 return 1;
1906
1907 if (start_devices() != 0)
1908 return 1;
1909
1910 atexit(stop_all_tracing);
1911
1912 if (stop_watch)
1913 alarm(stop_watch);
1914
1915 wait_for_threads();
1916
1917 if (!is_trace_stopped()) {
1918 trace_stopped = 1;
1919 stop_all_threads();
1920 stop_all_traces();
1921 }
1922
1923 show_stats(device_information, ndevs, ncpus);
1924
1925 return 0;
1926}
1927