[PATCH] blktrace: allow client/server to signal end-of-run properly
[blktrace.git] / blktrace.c
CommitLineData
d0ca268b
JA
1/*
2 * block queue tracing application
3 *
d956a2cd
JA
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 *
d0ca268b
JA
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>
b9d4294e 30#include <sys/param.h>
e3e74029 31#include <sys/statfs.h>
eb3c8108 32#include <sys/poll.h>
b7106311 33#include <sys/mman.h>
8e86c98a 34#include <sys/socket.h>
d0ca268b
JA
35#include <stdio.h>
36#include <stdlib.h>
37#include <sched.h>
d39c04ca
AB
38#include <ctype.h>
39#include <getopt.h>
da39451f 40#include <errno.h>
8e86c98a
JA
41#include <netinet/in.h>
42#include <arpa/inet.h>
43#include <netdb.h>
32f18c48 44#include <sys/sendfile.h>
d0ca268b
JA
45
46#include "blktrace.h"
21f55651 47#include "barrier.h"
d0ca268b 48
13d928f0 49static char blktrace_version[] = "0.99";
52724a0e 50
8f551a39
JA
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 */
007c233c 55#define BUF_SIZE (512 * 1024)
d0ca268b
JA
56#define BUF_NR (4)
57
007c233c
JA
58#define OFILE_BUF (128 * 1024)
59
e3e74029
NS
60#define RELAYFS_TYPE 0xF0B4A981
61
32f18c48 62#define S_OPTS "d:a:A:r:o:kw:Vb:n:D:lh:p:s"
d5396421 63static struct option l_opts[] = {
5c86134e 64 {
d39c04ca 65 .name = "dev",
428683db 66 .has_arg = required_argument,
d39c04ca
AB
67 .flag = NULL,
68 .val = 'd'
69 },
5c86134e 70 {
d39c04ca 71 .name = "act-mask",
428683db 72 .has_arg = required_argument,
d39c04ca
AB
73 .flag = NULL,
74 .val = 'a'
75 },
5c86134e 76 {
d39c04ca 77 .name = "set-mask",
428683db 78 .has_arg = required_argument,
d39c04ca
AB
79 .flag = NULL,
80 .val = 'A'
81 },
5c86134e 82 {
5270dddd 83 .name = "relay",
428683db 84 .has_arg = required_argument,
5270dddd
JA
85 .flag = NULL,
86 .val = 'r'
87 },
d5396421
JA
88 {
89 .name = "output",
428683db 90 .has_arg = required_argument,
d5396421
JA
91 .flag = NULL,
92 .val = 'o'
93 },
bc39777c
JA
94 {
95 .name = "kill",
428683db 96 .has_arg = no_argument,
bc39777c
JA
97 .flag = NULL,
98 .val = 'k'
99 },
ece238a6
NS
100 {
101 .name = "stopwatch",
428683db 102 .has_arg = required_argument,
ece238a6
NS
103 .flag = NULL,
104 .val = 'w'
105 },
52724a0e
JA
106 {
107 .name = "version",
108 .has_arg = no_argument,
109 .flag = NULL,
57ea8602 110 .val = 'V'
52724a0e 111 },
129aa440 112 {
3f65c585 113 .name = "buffer-size",
129aa440
JA
114 .has_arg = required_argument,
115 .flag = NULL,
116 .val = 'b'
117 },
118 {
3f65c585 119 .name = "num-sub-buffers",
129aa440
JA
120 .has_arg = required_argument,
121 .flag = NULL,
122 .val = 'n'
123 },
d1d7f15f 124 {
3f65c585 125 .name = "output-dir",
d1d7f15f
JA
126 .has_arg = required_argument,
127 .flag = NULL,
128 .val = 'D'
129 },
8e86c98a
JA
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 },
32f18c48
JA
148 {
149 .name = "sendfile",
150 .has_arg = no_argument,
151 .flag = NULL,
152 .val = 's'
153 },
71ef8b7c
JA
154 {
155 .name = NULL,
156 }
d39c04ca
AB
157};
158
9db17354 159struct tip_subbuf {
9db17354 160 void *buf;
5be4bdaf
JA
161 unsigned int len;
162 unsigned int max_len;
32f18c48 163 off_t offset;
9db17354
JA
164};
165
21f55651
JA
166#define FIFO_SIZE (1024) /* should be plenty big! */
167#define CL_SIZE (128) /* cache line, any bigger? */
168
169struct tip_subbuf_fifo {
170 int tail __attribute__((aligned(CL_SIZE)));
171 int head __attribute__((aligned(CL_SIZE)));
172 struct tip_subbuf *q[FIFO_SIZE];
173};
174
d0ca268b
JA
175struct thread_information {
176 int cpu;
177 pthread_t thread;
b9d4294e
JA
178
179 int fd;
a3e4d330 180 void *fd_buf;
b9d4294e
JA
181 char fn[MAXPATHLEN + 64];
182
007c233c
JA
183 FILE *ofile;
184 char *ofile_buffer;
32f18c48 185 off_t ofile_offset;
9db17354 186 int ofile_stdout;
8e86c98a 187 int ofile_mmap;
007c233c 188
0cc7d25e
JA
189 int (*get_subbuf)(struct thread_information *, unsigned int);
190 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
191 int (*read_data)(struct thread_information *, void *, unsigned int);
192
d0ca268b 193 unsigned long events_processed;
b7106311 194 unsigned long long data_read;
e7c9f3ff 195 struct device_information *device;
9db17354
JA
196
197 int exited;
198
b7106311
JA
199 /*
200 * piped fifo buffers
201 */
21f55651 202 struct tip_subbuf_fifo fifo;
7de86b12 203 struct tip_subbuf *leftover_ts;
b7106311
JA
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;
d0ca268b
JA
213};
214
e7c9f3ff
NS
215struct device_information {
216 int fd;
217 char *path;
218 char buts_name[32];
99c1f5ab 219 volatile int trace_started;
eb3c8108 220 unsigned long drop_count;
e7c9f3ff
NS
221 struct thread_information *threads;
222};
d0ca268b 223
e7c9f3ff 224static int ncpus;
d0ca268b 225static struct thread_information *thread_information;
e7c9f3ff
NS
226static int ndevs;
227static struct device_information *device_information;
228
229/* command line option globals */
230static char *relay_path;
d5396421 231static char *output_name;
d1d7f15f 232static char *output_dir;
5c86134e 233static int act_mask = ~0U;
bc39777c 234static int kill_running_trace;
eb3c8108
JA
235static unsigned long buf_size = BUF_SIZE;
236static unsigned long buf_nr = BUF_NR;
b7106311 237static unsigned int page_size;
d39c04ca 238
e7c9f3ff
NS
239#define is_done() (*(volatile int *)(&done))
240static volatile int done;
241
eb3c8108
JA
242#define is_trace_stopped() (*(volatile int *)(&trace_stopped))
243static volatile int trace_stopped;
244
245#define is_stat_shown() (*(volatile int *)(&stat_shown))
246static volatile int stat_shown;
a3e4d330 247
8e86c98a
JA
248int data_is_native = -1;
249
72ca8801
NS
250static void exit_trace(int status);
251
99c1f5ab
JA
252#define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
253#define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
254
255#define __for_each_dip(__d, __i, __e) \
256 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
257
258#define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
9db17354
JA
259#define for_each_tip(__d, __t, __j) \
260 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
99c1f5ab 261
8e86c98a
JA
262/*
263 * networking stuff follows. we include a magic number so we know whether
264 * to endianness convert or not
265 */
266struct blktrace_net_hdr {
267 u32 magic; /* same as trace magic */
22cd0c02 268 char buts_name[32]; /* trace name */
8e86c98a 269 u32 cpu; /* for which cpu */
22cd0c02 270 u32 max_cpus;
8e86c98a
JA
271 u32 len; /* length of following trace data */
272};
273
274#define TRACE_NET_PORT (8462)
275
276enum {
277 Net_none = 0,
278 Net_server,
279 Net_client,
280};
281
282/*
283 * network cmd line params
284 */
285static char hostname[MAXHOSTNAMELEN];
286static int net_port = TRACE_NET_PORT;
287static int net_mode = 0;
32f18c48 288static int net_sendfile;
8e86c98a
JA
289
290static int net_in_fd = -1;
291static int net_out_fd = -1;
292
293static void handle_sigint(__attribute__((__unused__)) int sig)
294{
295 done = 1;
296}
297
eb3c8108
JA
298static int get_dropped_count(const char *buts_name)
299{
300 int fd;
301 char tmp[MAXPATHLEN + 64];
302
303 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
304 relay_path, buts_name);
305
306 fd = open(tmp, O_RDONLY);
307 if (fd < 0) {
308 /*
309 * this may be ok, if the kernel doesn't support dropped counts
310 */
311 if (errno == ENOENT)
312 return 0;
313
314 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
315 return -1;
316 }
317
318 if (read(fd, tmp, sizeof(tmp)) < 0) {
319 perror(tmp);
320 close(fd);
321 return -1;
322 }
323
324 close(fd);
325
326 return atoi(tmp);
327}
328
e7c9f3ff 329static int start_trace(struct device_information *dip)
d0ca268b
JA
330{
331 struct blk_user_trace_setup buts;
332
1f79c4a0 333 memset(&buts, 0, sizeof(buts));
129aa440
JA
334 buts.buf_size = buf_size;
335 buts.buf_nr = buf_nr;
d39c04ca 336 buts.act_mask = act_mask;
d0ca268b 337
ed71a31e
JA
338 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
339 perror("BLKTRACESETUP");
340 return 1;
341 }
342
343 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
344 perror("BLKTRACESTART");
d0ca268b
JA
345 return 1;
346 }
347
e7c9f3ff 348 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
99c1f5ab 349 dip_set_tracing(dip, 1);
d0ca268b
JA
350 return 0;
351}
352
e7c9f3ff 353static void stop_trace(struct device_information *dip)
d0ca268b 354{
99c1f5ab
JA
355 if (dip_tracing(dip) || kill_running_trace) {
356 dip_set_tracing(dip, 0);
cf9208ea 357
ed71a31e
JA
358 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
359 perror("BLKTRACESTOP");
360 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
361 perror("BLKTRACETEARDOWN");
cf9208ea 362
e7c9f3ff 363 close(dip->fd);
cf9208ea 364 dip->fd = -1;
707b0914 365 }
d0ca268b
JA
366}
367
e7c9f3ff
NS
368static void stop_all_traces(void)
369{
370 struct device_information *dip;
371 int i;
372
eb3c8108
JA
373 for_each_dip(dip, i) {
374 dip->drop_count = get_dropped_count(dip->buts_name);
e7c9f3ff 375 stop_trace(dip);
eb3c8108 376 }
e7c9f3ff
NS
377}
378
eb3c8108
JA
379static void wait_for_data(struct thread_information *tip)
380{
381 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
382
9db17354 383 do {
b4aabcb3 384 poll(&pfd, 1, 100);
9db17354
JA
385 if (pfd.revents & POLLIN)
386 break;
387 if (tip->ofile_stdout)
388 break;
389 } while (!is_done());
eb3c8108
JA
390}
391
0cc7d25e
JA
392static int read_data_file(struct thread_information *tip, void *buf,
393 unsigned int len)
d0ca268b 394{
ae9f71b3 395 int ret = 0;
bbabf03a 396
9db17354
JA
397 do {
398 wait_for_data(tip);
ae9f71b3 399
9db17354
JA
400 ret = read(tip->fd, buf, len);
401 if (!ret)
402 continue;
403 else if (ret > 0)
404 return ret;
405 else {
bbabf03a 406 if (errno != EAGAIN) {
a3e4d330
JA
407 perror(tip->fn);
408 fprintf(stderr,"Thread %d failed read of %s\n",
409 tip->cpu, tip->fn);
410 break;
411 }
9db17354 412 continue;
bbabf03a 413 }
9db17354 414 } while (!is_done());
8a43bac5 415
bbabf03a 416 return ret;
8e86c98a 417
8a43bac5
JA
418}
419
0cc7d25e
JA
420static int read_data_net(struct thread_information *tip, void *buf,
421 unsigned int len)
8e86c98a
JA
422{
423 unsigned int bytes_left = len;
424 int ret = 0;
425
426 do {
427 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
428
429 if (!ret)
430 continue;
431 else if (ret < 0) {
432 if (errno != EAGAIN) {
433 perror(tip->fn);
434 fprintf(stderr, "server: failed read\n");
435 return 0;
436 }
437 continue;
438 } else {
439 buf += ret;
440 bytes_left -= ret;
441 }
442 } while (!is_done() && bytes_left);
443
410d7c62 444 return len - bytes_left;
8e86c98a
JA
445}
446
0cc7d25e
JA
447static int read_data(struct thread_information *tip, void *buf,
448 unsigned int len)
8e86c98a 449{
0cc7d25e 450 int ret = tip->read_data(tip, buf, len);
8e86c98a 451
410d7c62
JA
452 if (ret > 0)
453 tip->data_read += ret;
454
455 return ret;
8e86c98a
JA
456}
457
458static inline struct tip_subbuf *
459subbuf_fifo_dequeue(struct thread_information *tip)
a3e4d330 460{
21f55651
JA
461 const int head = tip->fifo.head;
462 const int next = (head + 1) & (FIFO_SIZE - 1);
463
464 if (head != tip->fifo.tail) {
465 struct tip_subbuf *ts = tip->fifo.q[head];
466
467 store_barrier();
468 tip->fifo.head = next;
469 return ts;
470 }
471
472 return NULL;
9db17354 473}
eb3c8108 474
21f55651
JA
475static inline int subbuf_fifo_queue(struct thread_information *tip,
476 struct tip_subbuf *ts)
9db17354 477{
21f55651
JA
478 const int tail = tip->fifo.tail;
479 const int next = (tail + 1) & (FIFO_SIZE - 1);
480
481 if (next != tip->fifo.head) {
482 tip->fifo.q[tail] = ts;
483 store_barrier();
484 tip->fifo.tail = next;
485 return 0;
486 }
487
488 fprintf(stderr, "fifo too small!\n");
489 return 1;
a3e4d330
JA
490}
491
b7106311
JA
492/*
493 * For file output, truncate and mmap the file appropriately
494 */
8e86c98a 495static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
b7106311
JA
496{
497 int ofd = fileno(tip->ofile);
498 int ret;
499
500 /*
501 * extend file, if we have to. use chunks of 16 subbuffers.
502 */
503 if (tip->fs_off + buf_size > tip->fs_buf_len) {
504 if (tip->fs_buf) {
5975d309 505 munlock(tip->fs_buf, tip->fs_buf_len);
b7106311
JA
506 munmap(tip->fs_buf, tip->fs_buf_len);
507 tip->fs_buf = NULL;
508 }
509
510 tip->fs_off = tip->fs_size & (page_size - 1);
511 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
512 tip->fs_max_size += tip->fs_buf_len;
513
514 if (ftruncate(ofd, tip->fs_max_size) < 0) {
515 perror("ftruncate");
516 return -1;
517 }
518
519 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
520 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
521 if (tip->fs_buf == MAP_FAILED) {
522 perror("mmap");
523 return -1;
524 }
5975d309 525 mlock(tip->fs_buf, tip->fs_buf_len);
b7106311
JA
526 }
527
8e86c98a 528 ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
b7106311 529 if (ret >= 0) {
b7106311
JA
530 tip->fs_size += ret;
531 tip->fs_off += ret;
532 return 0;
533 }
534
535 return -1;
536}
537
32f18c48
JA
538static int get_subbuf_sendfile(struct thread_information *tip,
539 unsigned int maxlen)
540{
541 struct tip_subbuf *ts = malloc(sizeof(*ts));
542 struct stat sb;
543
544 ts->buf = malloc(buf_size);
545 ts->max_len = maxlen;
546 ts->buf = NULL;
547
548 if (fstat(tip->fd, &sb) < 0) {
549 perror("trace stat");
550 return 1;
551 }
552
553 ts->len = sb.st_size - tip->ofile_offset;
554 ts->max_len = ts->len;
555 ts->offset = tip->ofile_offset;
556 tip->ofile_offset += ts->len;
557 return subbuf_fifo_queue(tip, ts);
558}
559
b7106311 560/*
22cd0c02 561 * Use the copy approach for pipes and network
b7106311 562 */
0cc7d25e 563static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
a3e4d330 564{
8e86c98a 565 struct tip_subbuf *ts = malloc(sizeof(*ts));
22cd0c02 566 int ret;
8e86c98a 567
22cd0c02 568 ts->buf = malloc(buf_size);
0cc7d25e 569 ts->max_len = maxlen;
8e86c98a 570
22cd0c02
JA
571 ret = read_data(tip, ts->buf, ts->max_len);
572 if (ret > 0) {
573 ts->len = ret;
574 return subbuf_fifo_queue(tip, ts);
9db17354 575 }
a3e4d330 576
b7106311 577 return ret;
a3e4d330
JA
578}
579
9db17354 580static void close_thread(struct thread_information *tip)
a3e4d330 581{
9db17354
JA
582 if (tip->fd != -1)
583 close(tip->fd);
584 if (tip->ofile)
585 fclose(tip->ofile);
586 if (tip->ofile_buffer)
587 free(tip->ofile_buffer);
588 if (tip->fd_buf)
589 free(tip->fd_buf);
1c99bc21 590
9db17354
JA
591 tip->fd = -1;
592 tip->ofile = NULL;
593 tip->ofile_buffer = NULL;
594 tip->fd_buf = NULL;
a3e4d330
JA
595}
596
8e86c98a
JA
597static void tip_ftrunc_final(struct thread_information *tip)
598{
599 /*
600 * truncate to right size and cleanup mmap
601 */
602 if (tip->ofile_mmap) {
603 int ofd = fileno(tip->ofile);
604
605 if (tip->fs_buf)
606 munmap(tip->fs_buf, tip->fs_buf_len);
607
608 ftruncate(ofd, tip->fs_size);
609 }
610}
611
9db17354 612static void *thread_main(void *arg)
a3e4d330 613{
9db17354
JA
614 struct thread_information *tip = arg;
615 pid_t pid = getpid();
616 cpu_set_t cpu_mask;
a3e4d330 617
9db17354
JA
618 CPU_ZERO(&cpu_mask);
619 CPU_SET((tip->cpu), &cpu_mask);
a3e4d330 620
9db17354
JA
621 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
622 perror("sched_setaffinity");
623 exit_trace(1);
624 }
a3e4d330 625
9db17354
JA
626 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
627 relay_path, tip->device->buts_name, tip->cpu);
628 tip->fd = open(tip->fn, O_RDONLY);
629 if (tip->fd < 0) {
630 perror(tip->fn);
631 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
632 tip->fn);
633 exit_trace(1);
a3e4d330
JA
634 }
635
b7106311 636 while (!is_done()) {
0cc7d25e
JA
637 if (tip->get_subbuf(tip, buf_size))
638 break;
b7106311
JA
639 }
640
8e86c98a
JA
641 tip_ftrunc_final(tip);
642 tip->exited = 1;
643 return NULL;
644}
b7106311 645
8e86c98a
JA
646static int write_data_net(int fd, void *buf, unsigned int buf_len)
647{
648 unsigned int bytes_left = buf_len;
649 int ret;
b7106311 650
8e86c98a
JA
651 while (bytes_left) {
652 ret = send(fd, buf, bytes_left, 0);
653 if (ret < 0) {
654 perror("send");
655 return 1;
656 }
657
658 buf += ret;
659 bytes_left -= ret;
9db17354 660 }
a3e4d330 661
8e86c98a 662 return 0;
a3e4d330
JA
663}
664
32f18c48 665static int net_send_header(struct thread_information *tip, unsigned int len)
8e86c98a
JA
666{
667 struct blktrace_net_hdr hdr;
8e86c98a
JA
668
669 hdr.magic = BLK_IO_TRACE_MAGIC;
22cd0c02 670 strcpy(hdr.buts_name, tip->device->buts_name);
8e86c98a 671 hdr.cpu = tip->cpu;
22cd0c02 672 hdr.max_cpus = ncpus;
32f18c48 673 hdr.len = len;
8e86c98a 674
32f18c48
JA
675 return write_data_net(net_out_fd, &hdr, sizeof(hdr));
676}
8e86c98a 677
6a752c90
JA
678/*
679 * send header with 0 length to signal end-of-run
680 */
681static void net_client_send_close(void)
682{
683 struct blktrace_net_hdr hdr;
684
685 hdr.magic = BLK_IO_TRACE_MAGIC;
686 hdr.cpu = 0;
687 hdr.max_cpus = ncpus;
688 hdr.len = 0;
689
690 write_data_net(net_out_fd, &hdr, sizeof(hdr));
691}
692
32f18c48
JA
693static int flush_subbuf_net(struct thread_information *tip,
694 struct tip_subbuf *ts)
695{
696 if (net_send_header(tip, ts->len))
697 return 1;
22cd0c02
JA
698 if (write_data_net(net_out_fd, ts->buf, ts->len))
699 return 1;
8e86c98a 700
f0597a7e 701 free(ts->buf);
8e86c98a
JA
702 free(ts);
703 return 0;
704}
705
32f18c48
JA
706static int flush_subbuf_sendfile(struct thread_information *tip,
707 struct tip_subbuf *ts)
708{
709 if (net_send_header(tip, ts->len))
710 return 1;
711 if (sendfile(net_out_fd, tip->fd, &ts->offset, ts->len) < 0) {
712 perror("sendfile");
713 return 1;
714 }
715
716 free(ts);
717 return 0;
718}
719
8e86c98a
JA
720static int write_data(struct thread_information *tip, void *buf,
721 unsigned int buf_len)
8a43bac5 722{
7126171a 723 int ret;
8a43bac5 724
6480258a
JA
725 if (!buf_len)
726 return 0;
727
7126171a
JA
728 while (1) {
729 ret = fwrite(buf, buf_len, 1, tip->ofile);
007c233c 730 if (ret == 1)
8a43bac5
JA
731 break;
732
db6fe5bc
JA
733 if (ret < 0) {
734 perror("write");
735 return 1;
8a43bac5 736 }
d0ca268b
JA
737 }
738
9db17354 739 if (tip->ofile_stdout)
7126171a
JA
740 fflush(tip->ofile);
741
8a43bac5
JA
742 return 0;
743}
744
8e86c98a
JA
745static int flush_subbuf_file(struct thread_information *tip,
746 struct tip_subbuf *ts)
8a43bac5 747{
9db17354
JA
748 unsigned int offset = 0;
749 struct blk_io_trace *t;
750 int pdu_len, events = 0;
8a43bac5 751
9db17354 752 /*
7de86b12 753 * surplus from last run
9db17354 754 */
7de86b12
AB
755 if (tip->leftover_ts) {
756 struct tip_subbuf *prev_ts = tip->leftover_ts;
757
9e8b753c 758 if (prev_ts->len + ts->len > prev_ts->max_len) {
7de86b12
AB
759 prev_ts->max_len += ts->len;
760 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
761 }
762
9e8b753c 763 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
7de86b12
AB
764 prev_ts->len += ts->len;
765
766 free(ts->buf);
767 free(ts);
768
769 ts = prev_ts;
770 tip->leftover_ts = NULL;
9db17354 771 }
d0ca268b 772
9db17354
JA
773 while (offset + sizeof(*t) <= ts->len) {
774 t = ts->buf + offset;
3a9d6c13 775
9cfa6c2b
AB
776 if (verify_trace(t)) {
777 write_data(tip, ts->buf, offset);
9db17354 778 return -1;
9cfa6c2b 779 }
3a9d6c13 780
9db17354 781 pdu_len = t->pdu_len;
3a9d6c13 782
9db17354 783 if (offset + sizeof(*t) + pdu_len > ts->len)
3a9d6c13 784 break;
4b5db44a 785
9db17354
JA
786 offset += sizeof(*t) + pdu_len;
787 tip->events_processed++;
b7106311 788 tip->data_read += sizeof(*t) + pdu_len;
9db17354 789 events++;
3a9d6c13
JA
790 }
791
9cfa6c2b
AB
792 if (write_data(tip, ts->buf, offset))
793 return -1;
794
3a9d6c13 795 /*
9db17354 796 * leftover bytes, save them for next time
3a9d6c13 797 */
9db17354 798 if (offset != ts->len) {
7de86b12 799 tip->leftover_ts = ts;
9e8b753c
JA
800 ts->len -= offset;
801 memmove(ts->buf, ts->buf + offset, ts->len);
7de86b12
AB
802 } else {
803 free(ts->buf);
804 free(ts);
9db17354 805 }
4b5db44a 806
9db17354 807 return events;
4b5db44a
JA
808}
809
9db17354 810static int write_tip_events(struct thread_information *tip)
d5396421 811{
21f55651 812 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
d5396421 813
0cc7d25e
JA
814 if (ts)
815 return tip->flush_subbuf(tip, ts);
91816d54 816
9db17354 817 return 0;
91816d54
JA
818}
819
9db17354
JA
820/*
821 * scans the tips we know and writes out the subbuffers we accumulate
822 */
823static void get_and_write_events(void)
d0ca268b 824{
9db17354
JA
825 struct device_information *dip;
826 struct thread_information *tip;
27223f19 827 int i, j, events, ret, tips_running;
d0ca268b 828
9db17354
JA
829 while (!is_done()) {
830 events = 0;
d0ca268b 831
9db17354
JA
832 for_each_dip(dip, i) {
833 for_each_tip(dip, tip, j) {
834 ret = write_tip_events(tip);
835 if (ret > 0)
836 events += ret;
837 }
838 }
d0ca268b 839
9db17354
JA
840 if (!events)
841 usleep(10);
d0ca268b
JA
842 }
843
a3e4d330 844 /*
9db17354 845 * reap stored events
a3e4d330 846 */
9db17354
JA
847 do {
848 events = 0;
27223f19 849 tips_running = 0;
9db17354
JA
850 for_each_dip(dip, i) {
851 for_each_tip(dip, tip, j) {
852 ret = write_tip_events(tip);
853 if (ret > 0)
854 events += ret;
27223f19 855 tips_running += !tip->exited;
9db17354 856 }
69e65a9e 857 }
9db17354 858 usleep(10);
27223f19 859 } while (events || tips_running);
d0ca268b
JA
860}
861
b7106311
JA
862static void wait_for_threads(void)
863{
864 /*
8e86c98a
JA
865 * for piped or network output, poll and fetch data for writeout.
866 * for files, we just wait around for trace threads to exit
b7106311 867 */
8e86c98a
JA
868 if ((output_name && !strcmp(output_name, "-")) ||
869 net_mode == Net_client)
b7106311
JA
870 get_and_write_events();
871 else {
872 struct device_information *dip;
873 struct thread_information *tip;
874 int i, j, tips_running;
875
876 do {
877 tips_running = 0;
878 usleep(1000);
879
880 for_each_dip(dip, i)
881 for_each_tip(dip, tip, j)
882 tips_running += !tip->exited;
883 } while (tips_running);
884 }
6a752c90
JA
885
886 if (net_mode == Net_client)
887 net_client_send_close();
b7106311
JA
888}
889
8e86c98a
JA
890static void fill_ofname(char *dst, char *buts_name, int cpu)
891{
892 int len = 0;
893
894 if (output_dir)
895 len = sprintf(dst, "%s/", output_dir);
896
897 if (output_name)
898 sprintf(dst + len, "%s.blktrace.%d", output_name, cpu);
899 else
900 sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu);
901}
902
0cc7d25e
JA
903static void fill_ops(struct thread_information *tip)
904{
905 /*
906 * setup ops
907 */
32f18c48
JA
908 if (net_mode == Net_client) {
909 if (net_sendfile) {
910 tip->get_subbuf = get_subbuf_sendfile;
911 tip->flush_subbuf = flush_subbuf_sendfile;
912 } else {
913 tip->get_subbuf = get_subbuf;
914 tip->flush_subbuf = flush_subbuf_net;
915 }
916 } else {
917 if (tip->ofile_mmap)
918 tip->get_subbuf = mmap_subbuf;
919 else
920 tip->get_subbuf = get_subbuf;
0cc7d25e 921
0cc7d25e 922 tip->flush_subbuf = flush_subbuf_file;
32f18c48
JA
923 }
924
0cc7d25e
JA
925 if (net_mode == Net_server)
926 tip->read_data = read_data_net;
927 else
928 tip->read_data = read_data_file;
929}
930
e7c9f3ff 931static int start_threads(struct device_information *dip)
d0ca268b
JA
932{
933 struct thread_information *tip;
e7c9f3ff 934 int j, pipeline = output_name && !strcmp(output_name, "-");
8e86c98a 935 int mode, vbuf_size;
22cd0c02 936 char op[64];
d0ca268b 937
99c1f5ab 938 for_each_tip(dip, tip, j) {
e7c9f3ff
NS
939 tip->cpu = j;
940 tip->device = dip;
d0ca268b 941 tip->events_processed = 0;
21f55651 942 memset(&tip->fifo, 0, sizeof(tip->fifo));
9db17354 943 tip->leftover_ts = NULL;
d0ca268b 944
e7c9f3ff 945 if (pipeline) {
007c233c 946 tip->ofile = fdopen(STDOUT_FILENO, "w");
9db17354 947 tip->ofile_stdout = 1;
8e86c98a 948 tip->ofile_mmap = 0;
007c233c 949 mode = _IOLBF;
57e8a2ad 950 vbuf_size = 512;
d5396421 951 } else {
22cd0c02
JA
952 fill_ofname(op, dip->buts_name, tip->cpu);
953 tip->ofile = fopen(op, "w+");
9db17354 954 tip->ofile_stdout = 0;
8e86c98a 955 tip->ofile_mmap = 1;
007c233c 956 mode = _IOFBF;
57e8a2ad 957 vbuf_size = OFILE_BUF;
d5396421
JA
958 }
959
007c233c 960 if (tip->ofile == NULL) {
22cd0c02 961 perror(op);
e7c9f3ff 962 return 1;
d5396421
JA
963 }
964
57e8a2ad
TZ
965 tip->ofile_buffer = malloc(vbuf_size);
966 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
007c233c
JA
967 perror("setvbuf");
968 close_thread(tip);
969 return 1;
970 }
971
0cc7d25e
JA
972 fill_ops(tip);
973
9db17354 974 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
e7c9f3ff 975 perror("pthread_create");
007c233c 976 close_thread(tip);
e7c9f3ff 977 return 1;
d0ca268b
JA
978 }
979 }
980
e7c9f3ff 981 return 0;
d0ca268b
JA
982}
983
e7c9f3ff 984static void stop_threads(struct device_information *dip)
3aabcd89 985{
e7c9f3ff 986 struct thread_information *tip;
91816d54 987 unsigned long ret;
007c233c
JA
988 int i;
989
9db17354 990 for_each_tip(dip, tip, i) {
91816d54 991 (void) pthread_join(tip->thread, (void *) &ret);
9db17354
JA
992 close_thread(tip);
993 }
3aabcd89
JA
994}
995
e7c9f3ff 996static void stop_all_threads(void)
72ca8801 997{
e7c9f3ff 998 struct device_information *dip;
72ca8801
NS
999 int i;
1000
99c1f5ab 1001 for_each_dip(dip, i)
e7c9f3ff
NS
1002 stop_threads(dip);
1003}
1004
1005static void stop_all_tracing(void)
1006{
1007 struct device_information *dip;
91816d54 1008 int i;
007c233c 1009
91816d54 1010 for_each_dip(dip, i)
e7c9f3ff 1011 stop_trace(dip);
72ca8801
NS
1012}
1013
1014static void exit_trace(int status)
1015{
eb3c8108
JA
1016 if (!is_trace_stopped()) {
1017 trace_stopped = 1;
1018 stop_all_threads();
1019 stop_all_tracing();
1020 }
1021
72ca8801
NS
1022 exit(status);
1023}
1024
e7c9f3ff
NS
1025static int resize_devices(char *path)
1026{
1027 int size = (ndevs + 1) * sizeof(struct device_information);
1028
1029 device_information = realloc(device_information, size);
1030 if (!device_information) {
1031 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1032 return 1;
1033 }
1034 device_information[ndevs].path = path;
1035 ndevs++;
1036 return 0;
1037}
1038
1039static int open_devices(void)
d0ca268b 1040{
e7c9f3ff 1041 struct device_information *dip;
d0ca268b 1042 int i;
d0ca268b 1043
99c1f5ab 1044 for_each_dip(dip, i) {
cf9208ea 1045 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
e7c9f3ff
NS
1046 if (dip->fd < 0) {
1047 perror(dip->path);
1048 return 1;
1049 }
1050 }
99c1f5ab 1051
e7c9f3ff
NS
1052 return 0;
1053}
1054
1055static int start_devices(void)
1056{
1057 struct device_information *dip;
1058 int i, j, size;
1059
1060 size = ncpus * sizeof(struct thread_information);
1061 thread_information = malloc(size * ndevs);
1062 if (!thread_information) {
1063 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1064 return 1;
1065 }
d5396421 1066
99c1f5ab 1067 for_each_dip(dip, i) {
e7c9f3ff
NS
1068 if (start_trace(dip)) {
1069 close(dip->fd);
1070 fprintf(stderr, "Failed to start trace on %s\n",
1071 dip->path);
1072 break;
1073 }
1074 }
99c1f5ab 1075
e7c9f3ff 1076 if (i != ndevs) {
99c1f5ab 1077 __for_each_dip(dip, j, i)
e7c9f3ff 1078 stop_trace(dip);
99c1f5ab 1079
e7c9f3ff
NS
1080 return 1;
1081 }
1082
99c1f5ab 1083 for_each_dip(dip, i) {
e7c9f3ff
NS
1084 dip->threads = thread_information + (i * ncpus);
1085 if (start_threads(dip)) {
1086 fprintf(stderr, "Failed to start worker threads\n");
1087 break;
1088 }
1089 }
99c1f5ab 1090
e7c9f3ff 1091 if (i != ndevs) {
99c1f5ab 1092 __for_each_dip(dip, j, i)
e7c9f3ff 1093 stop_threads(dip);
99c1f5ab 1094 for_each_dip(dip, i)
e7c9f3ff 1095 stop_trace(dip);
99c1f5ab 1096
e7c9f3ff 1097 return 1;
d0ca268b
JA
1098 }
1099
e7c9f3ff 1100 return 0;
d0ca268b
JA
1101}
1102
e7c9f3ff
NS
1103static void show_stats(void)
1104{
e7c9f3ff
NS
1105 struct device_information *dip;
1106 struct thread_information *tip;
b7106311 1107 unsigned long long events_processed, data_read;
eb3c8108 1108 unsigned long total_drops;
2f903295 1109 int i, j, no_stdout = 0;
eb3c8108
JA
1110
1111 if (is_stat_shown())
1112 return;
1113
2f903295
JA
1114 if (output_name && !strcmp(output_name, "-"))
1115 no_stdout = 1;
1116
eb3c8108 1117 stat_shown = 1;
428683db 1118
56070ea4 1119 total_drops = 0;
99c1f5ab 1120 for_each_dip(dip, i) {
2f903295 1121 if (!no_stdout)
56070ea4 1122 printf("Device: %s\n", dip->path);
e7c9f3ff 1123 events_processed = 0;
b7106311 1124 data_read = 0;
99c1f5ab 1125 for_each_tip(dip, tip, j) {
2f903295 1126 if (!no_stdout)
b7106311
JA
1127 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1128 tip->cpu, tip->events_processed,
1129 tip->data_read >> 10);
e7c9f3ff 1130 events_processed += tip->events_processed;
b7106311 1131 data_read += tip->data_read;
e7c9f3ff 1132 }
eb3c8108 1133 total_drops += dip->drop_count;
2f903295 1134 if (!no_stdout)
b7106311
JA
1135 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1136 events_processed, dip->drop_count,
1137 data_read >> 10);
e7c9f3ff 1138 }
56070ea4
JA
1139
1140 if (total_drops)
1141 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
e7c9f3ff 1142}
52724a0e 1143
22cd0c02 1144static struct device_information *net_get_dip(char *buts_name)
8e86c98a 1145{
22cd0c02 1146 struct device_information *dip;
8e86c98a
JA
1147 int i;
1148
22cd0c02
JA
1149 for (i = 0; i < ndevs; i++) {
1150 dip = &device_information[i];
8e86c98a 1151
22cd0c02
JA
1152 if (!strcmp(dip->buts_name, buts_name))
1153 return dip;
8e86c98a
JA
1154 }
1155
22cd0c02
JA
1156 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1157 dip = &device_information[ndevs];
1158 strcpy(dip->buts_name, buts_name);
410d7c62 1159 strcpy(dip->path, buts_name);
22cd0c02
JA
1160 ndevs++;
1161 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1162 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1163
1164 /*
1165 * open all files
1166 */
1167 for (i = 0; i < ncpus; i++) {
1168 struct thread_information *tip = &dip->threads[i];
1169 char op[64];
8e86c98a 1170
22cd0c02
JA
1171 tip->cpu = i;
1172 tip->ofile_stdout = 0;
1173 tip->ofile_mmap = 1;
1174 tip->device = dip;
8e86c98a 1175
22cd0c02 1176 fill_ofname(op, dip->buts_name, tip->cpu);
8e86c98a 1177
22cd0c02
JA
1178 tip->ofile = fopen(op, "w+");
1179 if (!tip->ofile) {
1180 perror("fopen");
1181 return NULL;
1182 }
8e86c98a
JA
1183 }
1184
22cd0c02
JA
1185 return dip;
1186}
1187
1188static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh)
1189{
1190 struct device_information *dip;
1191
1192 ncpus = bnh->max_cpus;
1193 dip = net_get_dip(bnh->buts_name);
1194 return &dip->threads[bnh->cpu];
8e86c98a
JA
1195}
1196
1197static int net_get_header(struct blktrace_net_hdr *bnh)
1198{
1199 int fl = fcntl(net_in_fd, F_GETFL);
1200 int bytes_left, ret;
1201 void *p = bnh;
1202
1203 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1204 bytes_left = sizeof(*bnh);
1205 while (bytes_left && !is_done()) {
1206 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1207 if (ret < 0) {
1208 if (errno != EAGAIN) {
1209 perror("recv header");
1210 return 1;
1211 }
1212 usleep(100);
1213 continue;
1214 } else if (!ret) {
1215 usleep(100);
1216 continue;
1217 } else {
1218 p += ret;
1219 bytes_left -= ret;
1220 }
1221 }
1222 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1223 return 0;
1224}
1225
1226static int net_server_loop(void)
1227{
1228 struct thread_information *tip;
1229 struct blktrace_net_hdr bnh;
1230
1231 if (net_get_header(&bnh))
1232 return 1;
1233
1234 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1235 fprintf(stderr, "server: received data is bad\n");
1236 return 1;
1237 }
1238
1239 if (!data_is_native) {
1240 bnh.cpu = be32_to_cpu(bnh.cpu);
1241 bnh.len = be32_to_cpu(bnh.len);
1242 }
1243
6a752c90
JA
1244 /*
1245 * len == 0 means that the other end signalled end-of-run
1246 */
1247 if (!bnh.len) {
1248 fprintf(stderr, "server: end of run\n");
1249 return 1;
1250 }
1251
8e86c98a
JA
1252 tip = net_get_tip(&bnh);
1253 if (!tip)
1254 return 1;
1255
1256 if (mmap_subbuf(tip, bnh.len))
1257 return 1;
1258
1259 return 0;
1260}
1261
1262/*
1263 * Start here when we are in server mode - just fetch data from the network
1264 * and dump to files
1265 */
1266static int net_server(void)
1267{
1268 struct sockaddr_in addr;
1269 socklen_t socklen;
22cd0c02 1270 int fd, opt, i, j;
8e86c98a
JA
1271
1272 fd = socket(AF_INET, SOCK_STREAM, 0);
1273 if (fd < 0) {
1274 perror("server: socket");
1275 return 1;
1276 }
1277
1278 opt = 1;
1279 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1280 perror("setsockopt");
1281 return 1;
1282 }
1283
1284 memset(&addr, 0, sizeof(addr));
1285 addr.sin_family = AF_INET;
1286 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1287 addr.sin_port = htons(net_port);
1288
1289 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1290 perror("bind");
1291 return 1;
1292 }
1293
1294 if (listen(fd, 1) < 0) {
1295 perror("listen");
1296 return 1;
1297 }
1298
6a752c90 1299repeat:
8e86c98a
JA
1300 printf("blktrace: waiting for incoming connection...\n");
1301
1302 socklen = sizeof(addr);
1303 net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen);
1304 if (net_in_fd < 0) {
1305 perror("accept");
1306 return 1;
1307 }
1308
1309 signal(SIGINT, handle_sigint);
1310 signal(SIGHUP, handle_sigint);
1311 signal(SIGTERM, handle_sigint);
1312 signal(SIGALRM, handle_sigint);
1313
1314 printf("blktrace: connected!\n");
1315
1316 while (!is_done()) {
1317 if (net_server_loop())
1318 break;
1319 }
1320
22cd0c02
JA
1321 for (i = 0; i < ndevs; i++) {
1322 struct device_information *dip = &device_information[i];
1323
1324 for (j = 0; j < ncpus; j++)
1325 tip_ftrunc_final(&dip->threads[j]);
1326 }
8e86c98a 1327
410d7c62 1328 show_stats();
6a752c90
JA
1329
1330 if (is_done())
1331 return 0;
1332
1333 goto repeat;
8e86c98a
JA
1334}
1335
1336/*
1337 * Setup outgoing network connection where we will transmit data
1338 */
1339static int net_setup_client(void)
1340{
1341 struct sockaddr_in addr;
1342 int fd;
1343
1344 fd = socket(AF_INET, SOCK_STREAM, 0);
1345 if (fd < 0) {
1346 perror("client: socket");
1347 return 1;
1348 }
1349
1350 memset(&addr, 0, sizeof(addr));
1351 addr.sin_family = AF_INET;
1352 addr.sin_port = htons(net_port);
1353
1354 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1355 struct hostent *hent = gethostbyname(hostname);
1356 if (!hent) {
1357 perror("gethostbyname");
1358 return 1;
1359 }
1360
1361 memcpy(&addr.sin_addr, hent->h_addr, 4);
1362 strcpy(hostname, hent->h_name);
1363 }
1364
1365 printf("blktrace: connecting to %s\n", hostname);
1366
1367 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1368 perror("client: connect");
1369 return 1;
1370 }
1371
1372 printf("blktrace: connected!\n");
1373 net_out_fd = fd;
1374 return 0;
1375}
1376
52724a0e
JA
1377static char usage_str[] = \
1378 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1379 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1380 "\t-d Use specified device. May also be given last after options\n" \
1381 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1382 "\t-o File(s) to send output to\n" \
d1d7f15f 1383 "\t-D Directory to prepend to output file names\n" \
52724a0e
JA
1384 "\t-k Kill a running trace\n" \
1385 "\t-w Stop after defined time, in seconds\n" \
1386 "\t-a Only trace specified actions. See documentation\n" \
1387 "\t-A Give trace mask as a single value. See documentation\n" \
129aa440
JA
1388 "\t-b Sub buffer size in KiB\n" \
1389 "\t-n Number of sub buffers\n" \
f531b94d
JA
1390 "\t-l Run in network listen mode (blktrace server)\n" \
1391 "\t-h Run in network client mode, connecting to the given host\n" \
1392 "\t-p Network port to use (default 8462)\n" \
1393 "\t-s Make the network client use sendfile() to transfer data\n" \
1394 "\t-V Print program version info\n\n";
52724a0e 1395
ee1f4158
NS
1396static void show_usage(char *program)
1397{
52724a0e 1398 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
ee1f4158 1399}
d0ca268b
JA
1400
1401int main(int argc, char *argv[])
1402{
5270dddd 1403 static char default_relay_path[] = "/relay";
e3e74029 1404 struct statfs st;
d39c04ca 1405 int i, c;
ece238a6 1406 int stop_watch = 0;
d39c04ca
AB
1407 int act_mask_tmp = 0;
1408
1409 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1410 switch (c) {
1411 case 'a':
1412 i = find_mask_map(optarg);
1413 if (i < 0) {
ab197ca7 1414 fprintf(stderr,"Invalid action mask %s\n",
d39c04ca 1415 optarg);
7425d456 1416 return 1;
d39c04ca
AB
1417 }
1418 act_mask_tmp |= i;
1419 break;
1420
1421 case 'A':
98f8386b
AB
1422 if ((sscanf(optarg, "%x", &i) != 1) ||
1423 !valid_act_opt(i)) {
d39c04ca 1424 fprintf(stderr,
ab197ca7 1425 "Invalid set action mask %s/0x%x\n",
d39c04ca 1426 optarg, i);
7425d456 1427 return 1;
d39c04ca
AB
1428 }
1429 act_mask_tmp = i;
1430 break;
d0ca268b 1431
d39c04ca 1432 case 'd':
e7c9f3ff
NS
1433 if (resize_devices(optarg) != 0)
1434 return 1;
d39c04ca
AB
1435 break;
1436
5270dddd
JA
1437 case 'r':
1438 relay_path = optarg;
1439 break;
1440
d5396421 1441 case 'o':
66efebf8 1442 output_name = optarg;
d5396421 1443 break;
bc39777c
JA
1444 case 'k':
1445 kill_running_trace = 1;
1446 break;
ece238a6
NS
1447 case 'w':
1448 stop_watch = atoi(optarg);
1449 if (stop_watch <= 0) {
1450 fprintf(stderr,
1451 "Invalid stopwatch value (%d secs)\n",
1452 stop_watch);
1453 return 1;
1454 }
1455 break;
57ea8602 1456 case 'V':
52724a0e
JA
1457 printf("%s version %s\n", argv[0], blktrace_version);
1458 return 0;
129aa440 1459 case 'b':
eb3c8108 1460 buf_size = strtoul(optarg, NULL, 10);
183a0855 1461 if (buf_size <= 0 || buf_size > 16*1024) {
129aa440 1462 fprintf(stderr,
eb3c8108 1463 "Invalid buffer size (%lu)\n",buf_size);
129aa440
JA
1464 return 1;
1465 }
1466 buf_size <<= 10;
1467 break;
1468 case 'n':
eb3c8108 1469 buf_nr = strtoul(optarg, NULL, 10);
129aa440
JA
1470 if (buf_nr <= 0) {
1471 fprintf(stderr,
eb3c8108 1472 "Invalid buffer nr (%lu)\n", buf_nr);
129aa440
JA
1473 return 1;
1474 }
1475 break;
d1d7f15f
JA
1476 case 'D':
1477 output_dir = optarg;
1478 break;
8e86c98a
JA
1479 case 'h':
1480 net_mode = Net_client;
1481 strcpy(hostname, optarg);
1482 break;
1483 case 'l':
1484 net_mode = Net_server;
1485 break;
1486 case 'p':
1487 net_port = atoi(optarg);
1488 break;
32f18c48
JA
1489 case 's':
1490 net_sendfile = 1;
1491 break;
d39c04ca 1492 default:
ee1f4158 1493 show_usage(argv[0]);
7425d456 1494 return 1;
d39c04ca
AB
1495 }
1496 }
1497
8e86c98a
JA
1498 setlocale(LC_NUMERIC, "en_US");
1499
1500 page_size = getpagesize();
1501
1502 if (net_mode == Net_server)
1503 return net_server();
1504
22cd0c02
JA
1505 while (optind < argc) {
1506 if (resize_devices(argv[optind++]) != 0)
1507 return 1;
1508 }
1509
e7c9f3ff 1510 if (ndevs == 0) {
ee1f4158 1511 show_usage(argv[0]);
7425d456 1512 return 1;
d39c04ca
AB
1513 }
1514
5270dddd
JA
1515 if (!relay_path)
1516 relay_path = default_relay_path;
1517
d5396421 1518 if (act_mask_tmp != 0)
d39c04ca 1519 act_mask = act_mask_tmp;
d0ca268b 1520
e3e74029
NS
1521 if (statfs(relay_path, &st) < 0) {
1522 perror("statfs");
1523 fprintf(stderr,"%s does not appear to be a valid path\n",
1524 relay_path);
1525 return 1;
64acacae 1526 } else if (st.f_type != (long) RELAYFS_TYPE) {
e3e74029 1527 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
d0ca268b 1528 relay_path);
7425d456 1529 return 1;
d0ca268b
JA
1530 }
1531
e7c9f3ff 1532 if (open_devices() != 0)
7425d456 1533 return 1;
bc39777c
JA
1534
1535 if (kill_running_trace) {
e7c9f3ff 1536 stop_all_traces();
7425d456 1537 return 0;
bc39777c
JA
1538 }
1539
e7c9f3ff
NS
1540 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1541 if (ncpus < 0) {
1542 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
7425d456 1543 return 1;
d0ca268b
JA
1544 }
1545
d0ca268b
JA
1546 signal(SIGINT, handle_sigint);
1547 signal(SIGHUP, handle_sigint);
1548 signal(SIGTERM, handle_sigint);
ece238a6 1549 signal(SIGALRM, handle_sigint);
d0ca268b 1550
8e86c98a
JA
1551 if (net_mode == Net_client && net_setup_client())
1552 return 1;
1553
1554 if (start_devices() != 0)
1555 return 1;
1556
e7c9f3ff 1557 atexit(stop_all_tracing);
830fd65c 1558
ece238a6
NS
1559 if (stop_watch)
1560 alarm(stop_watch);
1561
b7106311 1562 wait_for_threads();
d0ca268b 1563
eb3c8108
JA
1564 if (!is_trace_stopped()) {
1565 trace_stopped = 1;
91816d54
JA
1566 stop_all_threads();
1567 stop_all_traces();
91816d54 1568 }
d0ca268b 1569
eb3c8108
JA
1570 show_stats();
1571
d0ca268b
JA
1572 return 0;
1573}
1574