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