[PATCH] blktrace: fix get_subbuf() leak
[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
a3225fed 49static char blktrace_version[] = "0.99.1";
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
3d06efea 60#define DEBUGFS_TYPE 0x64626720
e3e74029 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 148 {
79971f43 149 .name = "no-sendfile",
32f18c48
JA
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;
9db17354
JA
163};
164
21f55651
JA
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
d0ca268b
JA
174struct thread_information {
175 int cpu;
176 pthread_t thread;
b9d4294e
JA
177
178 int fd;
a3e4d330 179 void *fd_buf;
b9d4294e
JA
180 char fn[MAXPATHLEN + 64];
181
007c233c
JA
182 FILE *ofile;
183 char *ofile_buffer;
32f18c48 184 off_t ofile_offset;
9db17354 185 int ofile_stdout;
8e86c98a 186 int ofile_mmap;
007c233c 187
0cc7d25e
JA
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
d0ca268b 192 unsigned long events_processed;
b7106311 193 unsigned long long data_read;
bcbeb60f 194 unsigned long long data_queued;
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;
ff11d54c
TZ
213
214 struct net_connection *nc;
d0ca268b
JA
215};
216
e7c9f3ff
NS
217struct device_information {
218 int fd;
219 char *path;
220 char buts_name[32];
99c1f5ab 221 volatile int trace_started;
eb3c8108 222 unsigned long drop_count;
e7c9f3ff 223 struct thread_information *threads;
ff11d54c
TZ
224
225 struct cl_host *ch;
226 u32 cl_id;
227 time_t cl_connect_time;
e7c9f3ff 228};
d0ca268b 229
e7c9f3ff 230static int ncpus;
d0ca268b 231static struct thread_information *thread_information;
e7c9f3ff
NS
232static int ndevs;
233static struct device_information *device_information;
234
235/* command line option globals */
3d06efea 236static char *debugfs_path;
d5396421 237static char *output_name;
d1d7f15f 238static char *output_dir;
5c86134e 239static int act_mask = ~0U;
bc39777c 240static int kill_running_trace;
eb3c8108
JA
241static unsigned long buf_size = BUF_SIZE;
242static unsigned long buf_nr = BUF_NR;
b7106311 243static unsigned int page_size;
d39c04ca 244
e7c9f3ff
NS
245#define is_done() (*(volatile int *)(&done))
246static volatile int done;
247
eb3c8108
JA
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;
a3e4d330 253
8e86c98a
JA
254int data_is_native = -1;
255
72ca8801
NS
256static void exit_trace(int status);
257
99c1f5ab
JA
258#define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
259#define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
260
ce020676 261#define __for_each_dip(__d, __di, __e, __i) \
e0a1988b
JA
262 for (__i = 0, __d = __di; __i < __e; __i++, __d++)
263
264#define for_each_dip(__d, __i) \
ce020676 265 __for_each_dip(__d, device_information, ndevs, __i)
e0a1988b 266#define for_each_nc_dip(__nc, __d, __i) \
ff11d54c 267 __for_each_dip(__d, (__nc)->ch->device_information, (__nc)->ch->ndevs, __i)
99c1f5ab 268
ce020676
JA
269#define __for_each_tip(__d, __t, __ncpus, __j) \
270 for (__j = 0, __t = (__d)->threads; __j < __ncpus; __j++, __t++)
9db17354 271#define for_each_tip(__d, __t, __j) \
ce020676 272 __for_each_tip(__d, __t, ncpus, __j)
ff11d54c
TZ
273#define for_each_cl_host(__c) \
274 for (__c = cl_host_list; __c; __c = __c->list_next)
99c1f5ab 275
8e86c98a
JA
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 */
22cd0c02 282 char buts_name[32]; /* trace name */
8e86c98a 283 u32 cpu; /* for which cpu */
22cd0c02 284 u32 max_cpus;
8e86c98a 285 u32 len; /* length of following trace data */
ff11d54c 286 u32 cl_id; /* id for set of client per-cpu connections */
8e86c98a
JA
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;
79971f43 303static int net_use_sendfile = 1;
8e86c98a 304
ff11d54c
TZ
305struct cl_host {
306 struct cl_host *list_next;
e0a1988b 307 struct in_addr cl_in_addr;
ff11d54c
TZ
308 struct net_connection *net_connections;
309 int nconn;
e0a1988b
JA
310 struct device_information *device_information;
311 int ndevs;
312 int ncpus;
ff11d54c 313 int ndevs_done;
e0a1988b
JA
314};
315
ff11d54c
TZ
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;
e0a1988b 327static int net_connects;
ff11d54c
TZ
328
329static int *net_out_fd;
8e86c98a
JA
330
331static void handle_sigint(__attribute__((__unused__)) int sig)
332{
7035d92d
JA
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) {
921b05fe
JA
340 if (dip->fd == -1)
341 continue;
7035d92d
JA
342 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
343 perror("BLKTRACESTOP");
344 }
345
8e86c98a
JA
346 done = 1;
347}
348
eb3c8108
JA
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",
3d06efea 355 debugfs_path, buts_name);
eb3c8108
JA
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
e7c9f3ff 380static int start_trace(struct device_information *dip)
d0ca268b
JA
381{
382 struct blk_user_trace_setup buts;
383
1f79c4a0 384 memset(&buts, 0, sizeof(buts));
129aa440
JA
385 buts.buf_size = buf_size;
386 buts.buf_nr = buf_nr;
d39c04ca 387 buts.act_mask = act_mask;
d0ca268b 388
ed71a31e
JA
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");
d0ca268b
JA
396 return 1;
397 }
398
e7c9f3ff 399 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
99c1f5ab 400 dip_set_tracing(dip, 1);
d0ca268b
JA
401 return 0;
402}
403
e7c9f3ff 404static void stop_trace(struct device_information *dip)
d0ca268b 405{
99c1f5ab
JA
406 if (dip_tracing(dip) || kill_running_trace) {
407 dip_set_tracing(dip, 0);
cf9208ea 408
7035d92d
JA
409 /*
410 * should be stopped, just don't complain if it isn't
411 */
412 ioctl(dip->fd, BLKTRACESTOP);
413
ed71a31e
JA
414 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
415 perror("BLKTRACETEARDOWN");
cf9208ea 416
e7c9f3ff 417 close(dip->fd);
cf9208ea 418 dip->fd = -1;
707b0914 419 }
d0ca268b
JA
420}
421
e7c9f3ff
NS
422static void stop_all_traces(void)
423{
424 struct device_information *dip;
425 int i;
426
eb3c8108
JA
427 for_each_dip(dip, i) {
428 dip->drop_count = get_dropped_count(dip->buts_name);
e7c9f3ff 429 stop_trace(dip);
eb3c8108 430 }
e7c9f3ff
NS
431}
432
ff11d54c 433static void wait_for_data(struct thread_information *tip, int timeout)
eb3c8108 434{
ff11d54c 435 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
eb3c8108 436
ff11d54c
TZ
437 while (!is_done()) {
438 if (poll(&pfd, 1, timeout) < 0) {
7934e668
JA
439 perror("poll");
440 break;
441 }
ff11d54c 442 if (pfd.revents & POLLIN)
9db17354
JA
443 break;
444 if (tip->ofile_stdout)
445 break;
ff11d54c 446 }
eb3c8108
JA
447}
448
0cc7d25e
JA
449static int read_data_file(struct thread_information *tip, void *buf,
450 unsigned int len)
d0ca268b 451{
ae9f71b3 452 int ret = 0;
bbabf03a 453
9db17354 454 do {
ff11d54c 455 wait_for_data(tip, 100);
ae9f71b3 456
9db17354
JA
457 ret = read(tip->fd, buf, len);
458 if (!ret)
459 continue;
460 else if (ret > 0)
461 return ret;
462 else {
bbabf03a 463 if (errno != EAGAIN) {
a3e4d330
JA
464 perror(tip->fn);
465 fprintf(stderr,"Thread %d failed read of %s\n",
466 tip->cpu, tip->fn);
467 break;
468 }
9db17354 469 continue;
bbabf03a 470 }
9db17354 471 } while (!is_done());
8a43bac5 472
bbabf03a 473 return ret;
8e86c98a 474
8a43bac5
JA
475}
476
0cc7d25e
JA
477static int read_data_net(struct thread_information *tip, void *buf,
478 unsigned int len)
8e86c98a 479{
ff11d54c 480 struct net_connection *nc = tip->nc;
8e86c98a
JA
481 unsigned int bytes_left = len;
482 int ret = 0;
483
484 do {
e0a1988b 485 ret = recv(nc->in_fd, buf, bytes_left, MSG_WAITALL);
8e86c98a
JA
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
410d7c62 502 return len - bytes_left;
8e86c98a
JA
503}
504
8e86c98a
JA
505static inline struct tip_subbuf *
506subbuf_fifo_dequeue(struct thread_information *tip)
a3e4d330 507{
21f55651
JA
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;
9db17354 520}
eb3c8108 521
21f55651
JA
522static inline int subbuf_fifo_queue(struct thread_information *tip,
523 struct tip_subbuf *ts)
9db17354 524{
21f55651
JA
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;
a3e4d330
JA
537}
538
b7106311
JA
539/*
540 * For file output, truncate and mmap the file appropriately
541 */
8e86c98a 542static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
b7106311
JA
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) {
5975d309 552 munlock(tip->fs_buf, tip->fs_buf_len);
b7106311
JA
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 }
5975d309 572 mlock(tip->fs_buf, tip->fs_buf_len);
b7106311
JA
573 }
574
7934e668 575 ret = tip->read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
b7106311 576 if (ret >= 0) {
dbfbd6db 577 tip->data_read += ret;
b7106311
JA
578 tip->fs_size += ret;
579 tip->fs_off += ret;
580 return 0;
581 }
582
583 return -1;
584}
585
18eed2a7
JA
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
7934e668 597 ret = tip->read_data(tip, ts->buf, ts->max_len);
18eed2a7
JA
598 if (ret > 0) {
599 ts->len = ret;
dbfbd6db 600 tip->data_read += ret;
7035d92d 601 if (subbuf_fifo_queue(tip, ts))
2f064793
JA
602 ret = -1;
603 }
604
605 if (ret <= 0) {
606 free(ts->buf);
607 free(ts);
18eed2a7
JA
608 }
609
610 return ret;
611}
612
9db17354 613static void close_thread(struct thread_information *tip)
a3e4d330 614{
9db17354
JA
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);
1c99bc21 623
9db17354
JA
624 tip->fd = -1;
625 tip->ofile = NULL;
626 tip->ofile_buffer = NULL;
627 tip->fd_buf = NULL;
a3e4d330
JA
628}
629
8e86c98a
JA
630static void tip_ftrunc_final(struct thread_information *tip)
631{
632 /*
633 * truncate to right size and cleanup mmap
634 */
c196b5f2 635 if (tip->ofile_mmap && tip->ofile) {
8e86c98a
JA
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
9db17354 645static void *thread_main(void *arg)
a3e4d330 646{
9db17354
JA
647 struct thread_information *tip = arg;
648 pid_t pid = getpid();
649 cpu_set_t cpu_mask;
a3e4d330 650
9db17354
JA
651 CPU_ZERO(&cpu_mask);
652 CPU_SET((tip->cpu), &cpu_mask);
a3e4d330 653
9db17354
JA
654 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
655 perror("sched_setaffinity");
656 exit_trace(1);
657 }
a3e4d330 658
9db17354 659 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
3d06efea 660 debugfs_path, tip->device->buts_name, tip->cpu);
9db17354
JA
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);
a3e4d330
JA
667 }
668
b7106311 669 while (!is_done()) {
7035d92d 670 if (tip->get_subbuf(tip, buf_size) < 0)
0cc7d25e 671 break;
b7106311
JA
672 }
673
7035d92d
JA
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
8e86c98a
JA
680 tip_ftrunc_final(tip);
681 tip->exited = 1;
682 return NULL;
683}
b7106311 684
8e86c98a
JA
685static int write_data_net(int fd, void *buf, unsigned int buf_len)
686{
687 unsigned int bytes_left = buf_len;
688 int ret;
b7106311 689
8e86c98a
JA
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;
9db17354 699 }
a3e4d330 700
8e86c98a 701 return 0;
a3e4d330
JA
702}
703
32f18c48 704static int net_send_header(struct thread_information *tip, unsigned int len)
8e86c98a
JA
705{
706 struct blktrace_net_hdr hdr;
8e86c98a
JA
707
708 hdr.magic = BLK_IO_TRACE_MAGIC;
22cd0c02 709 strcpy(hdr.buts_name, tip->device->buts_name);
8e86c98a 710 hdr.cpu = tip->cpu;
22cd0c02 711 hdr.max_cpus = ncpus;
32f18c48 712 hdr.len = len;
ff11d54c 713 hdr.cl_id = getpid();
8e86c98a 714
ff11d54c 715 return write_data_net(net_out_fd[tip->cpu], &hdr, sizeof(hdr));
32f18c48 716}
8e86c98a 717
6a752c90
JA
718/*
719 * send header with 0 length to signal end-of-run
720 */
721static void net_client_send_close(void)
722{
7934e668 723 struct device_information *dip;
6a752c90 724 struct blktrace_net_hdr hdr;
7934e668 725 int i;
6a752c90 726
7934e668 727 for_each_dip(dip, i) {
7ab2f837
JA
728 hdr.magic = BLK_IO_TRACE_MAGIC;
729 hdr.max_cpus = ncpus;
730 hdr.len = 0;
7934e668 731 strcpy(hdr.buts_name, dip->buts_name);
7ab2f837 732 hdr.cpu = get_dropped_count(dip->buts_name);
ff11d54c 733 hdr.cl_id = getpid();
7ab2f837 734
ff11d54c 735 write_data_net(net_out_fd[0], &hdr, sizeof(hdr));
7934e668
JA
736 }
737
6a752c90
JA
738}
739
32f18c48
JA
740static int flush_subbuf_net(struct thread_information *tip,
741 struct tip_subbuf *ts)
742{
743 if (net_send_header(tip, ts->len))
7934e668 744 return -1;
ff11d54c 745 if (write_data_net(net_out_fd[tip->cpu], ts->buf, ts->len))
7934e668 746 return -1;
8e86c98a 747
f0597a7e 748 free(ts->buf);
8e86c98a 749 free(ts);
7934e668 750 return 1;
8e86c98a
JA
751}
752
f6fead25
JA
753static int net_sendfile(struct thread_information *tip, struct tip_subbuf *ts)
754{
ff11d54c 755 int ret = sendfile(net_out_fd[tip->cpu], tip->fd, NULL, ts->len);
11629347
JA
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
32f18c48
JA
768static int flush_subbuf_sendfile(struct thread_information *tip,
769 struct tip_subbuf *ts)
770{
7934e668 771 int ret = -1;
18eed2a7 772
f6fead25 773 if (net_send_header(tip, ts->len))
11629347 774 goto err;
f6fead25 775 if (net_sendfile(tip, ts))
11629347 776 goto err;
32f18c48 777
f6fead25 778 tip->data_read += ts->len;
e076d33b 779 tip->ofile_offset += buf_size;
7934e668 780 ret = 1;
11629347 781err:
32f18c48 782 free(ts);
11629347 783 return ret;
32f18c48
JA
784}
785
ff11d54c 786static int get_subbuf_sendfile(struct thread_information *tip,
d042efdf 787 __attribute__((__unused__)) unsigned int maxlen)
ff11d54c
TZ
788{
789 struct tip_subbuf *ts;
790 struct stat sb;
791 unsigned int ready;
ff11d54c 792
2689be58 793 wait_for_data(tip, -1);
ff11d54c
TZ
794
795 if (fstat(tip->fd, &sb) < 0) {
796 perror("trace stat");
797 return -1;
798 }
4aeec019 799
ff11d54c
TZ
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
d042efdf
JA
812 if (flush_subbuf_sendfile(tip, ts) < 0)
813 return -1;
ff11d54c
TZ
814
815 return ready;
816}
817
8e86c98a
JA
818static int write_data(struct thread_information *tip, void *buf,
819 unsigned int buf_len)
8a43bac5 820{
7126171a 821 int ret;
8a43bac5 822
6480258a
JA
823 if (!buf_len)
824 return 0;
825
7126171a
JA
826 while (1) {
827 ret = fwrite(buf, buf_len, 1, tip->ofile);
007c233c 828 if (ret == 1)
8a43bac5
JA
829 break;
830
db6fe5bc
JA
831 if (ret < 0) {
832 perror("write");
833 return 1;
8a43bac5 834 }
d0ca268b
JA
835 }
836
9db17354 837 if (tip->ofile_stdout)
7126171a
JA
838 fflush(tip->ofile);
839
8a43bac5
JA
840 return 0;
841}
842
8e86c98a
JA
843static int flush_subbuf_file(struct thread_information *tip,
844 struct tip_subbuf *ts)
8a43bac5 845{
9db17354
JA
846 unsigned int offset = 0;
847 struct blk_io_trace *t;
848 int pdu_len, events = 0;
8a43bac5 849
9db17354 850 /*
7de86b12 851 * surplus from last run
9db17354 852 */
7de86b12
AB
853 if (tip->leftover_ts) {
854 struct tip_subbuf *prev_ts = tip->leftover_ts;
855
9e8b753c 856 if (prev_ts->len + ts->len > prev_ts->max_len) {
7de86b12
AB
857 prev_ts->max_len += ts->len;
858 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
859 }
860
9e8b753c 861 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
7de86b12
AB
862 prev_ts->len += ts->len;
863
864 free(ts->buf);
865 free(ts);
866
867 ts = prev_ts;
868 tip->leftover_ts = NULL;
9db17354 869 }
d0ca268b 870
9db17354
JA
871 while (offset + sizeof(*t) <= ts->len) {
872 t = ts->buf + offset;
3a9d6c13 873
9cfa6c2b
AB
874 if (verify_trace(t)) {
875 write_data(tip, ts->buf, offset);
9db17354 876 return -1;
9cfa6c2b 877 }
3a9d6c13 878
9db17354 879 pdu_len = t->pdu_len;
3a9d6c13 880
9db17354 881 if (offset + sizeof(*t) + pdu_len > ts->len)
3a9d6c13 882 break;
4b5db44a 883
9db17354
JA
884 offset += sizeof(*t) + pdu_len;
885 tip->events_processed++;
b7106311 886 tip->data_read += sizeof(*t) + pdu_len;
9db17354 887 events++;
3a9d6c13
JA
888 }
889
9cfa6c2b
AB
890 if (write_data(tip, ts->buf, offset))
891 return -1;
892
3a9d6c13 893 /*
9db17354 894 * leftover bytes, save them for next time
3a9d6c13 895 */
9db17354 896 if (offset != ts->len) {
7de86b12 897 tip->leftover_ts = ts;
9e8b753c
JA
898 ts->len -= offset;
899 memmove(ts->buf, ts->buf + offset, ts->len);
7de86b12
AB
900 } else {
901 free(ts->buf);
902 free(ts);
9db17354 903 }
4b5db44a 904
9db17354 905 return events;
4b5db44a
JA
906}
907
9db17354 908static int write_tip_events(struct thread_information *tip)
d5396421 909{
21f55651 910 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
d5396421 911
0cc7d25e
JA
912 if (ts)
913 return tip->flush_subbuf(tip, ts);
91816d54 914
9db17354 915 return 0;
91816d54
JA
916}
917
9db17354
JA
918/*
919 * scans the tips we know and writes out the subbuffers we accumulate
920 */
921static void get_and_write_events(void)
d0ca268b 922{
9db17354
JA
923 struct device_information *dip;
924 struct thread_information *tip;
27223f19 925 int i, j, events, ret, tips_running;
d0ca268b 926
9db17354
JA
927 while (!is_done()) {
928 events = 0;
d0ca268b 929
9db17354
JA
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 }
d0ca268b 937
9db17354 938 if (!events)
7934e668 939 usleep(100000);
d0ca268b
JA
940 }
941
a3e4d330 942 /*
9db17354 943 * reap stored events
a3e4d330 944 */
9db17354
JA
945 do {
946 events = 0;
27223f19 947 tips_running = 0;
9db17354
JA
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;
27223f19 953 tips_running += !tip->exited;
9db17354 954 }
69e65a9e 955 }
9db17354 956 usleep(10);
27223f19 957 } while (events || tips_running);
d0ca268b
JA
958}
959
b7106311
JA
960static void wait_for_threads(void)
961{
962 /*
8e86c98a
JA
963 * for piped or network output, poll and fetch data for writeout.
964 * for files, we just wait around for trace threads to exit
b7106311 965 */
8e86c98a 966 if ((output_name && !strcmp(output_name, "-")) ||
ff11d54c 967 ((net_mode == Net_client) && !net_use_sendfile))
b7106311
JA
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;
7934e668 976 usleep(100000);
b7106311
JA
977
978 for_each_dip(dip, i)
979 for_each_tip(dip, tip, j)
980 tips_running += !tip->exited;
981 } while (tips_running);
982 }
6a752c90
JA
983
984 if (net_mode == Net_client)
985 net_client_send_close();
b7106311
JA
986}
987
97159c02
JA
988static int fill_ofname(struct device_information *dip,
989 struct thread_information *tip, char *dst,
e3bf54d8 990 char *buts_name)
8e86c98a 991{
e3bf54d8 992 struct stat sb;
8e86c98a
JA
993 int len = 0;
994
995 if (output_dir)
996 len = sprintf(dst, "%s/", output_dir);
dd870ef6
AB
997 else
998 len = sprintf(dst, "./");
8e86c98a 999
e3bf54d8 1000 if (net_mode == Net_server) {
ff11d54c 1001 struct net_connection *nc = tip->nc;
e0a1988b 1002
ff11d54c
TZ
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));
e3bf54d8
JA
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
8e86c98a 1019 if (output_name)
e3bf54d8 1020 sprintf(dst + len, "%s.blktrace.%d", output_name, tip->cpu);
8e86c98a 1021 else
e3bf54d8
JA
1022 sprintf(dst + len, "%s.blktrace.%d", buts_name, tip->cpu);
1023
1024 return 0;
8e86c98a
JA
1025}
1026
0cc7d25e
JA
1027static void fill_ops(struct thread_information *tip)
1028{
1029 /*
1030 * setup ops
1031 */
32f18c48 1032 if (net_mode == Net_client) {
36808255 1033 if (net_use_sendfile) {
32f18c48 1034 tip->get_subbuf = get_subbuf_sendfile;
ff11d54c 1035 tip->flush_subbuf = NULL;
32f18c48
JA
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;
0cc7d25e 1045
0cc7d25e 1046 tip->flush_subbuf = flush_subbuf_file;
32f18c48
JA
1047 }
1048
0cc7d25e
JA
1049 if (net_mode == Net_server)
1050 tip->read_data = read_data_net;
1051 else
1052 tip->read_data = read_data_file;
1053}
1054
ddf22842
JA
1055static int tip_open_output(struct device_information *dip,
1056 struct thread_information *tip)
d0ca268b 1057{
ddf22842 1058 int pipeline = output_name && !strcmp(output_name, "-");
8e86c98a 1059 int mode, vbuf_size;
e3bf54d8 1060 char op[128];
d0ca268b 1061
ddf22842
JA
1062 if (net_mode == Net_client) {
1063 tip->ofile = NULL;
1064 tip->ofile_stdout = 0;
1065 tip->ofile_mmap = 0;
0c0b75b4 1066 goto done;
ddf22842
JA
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 {
97159c02 1074 if (fill_ofname(dip, tip, op, dip->buts_name))
e3bf54d8 1075 return 1;
ddf22842
JA
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 }
d5396421 1082
0c0b75b4 1083 if (tip->ofile == NULL) {
ddf22842
JA
1084 perror(op);
1085 return 1;
1086 }
d5396421 1087
0c0b75b4
JA
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;
ddf22842
JA
1093 }
1094
0c0b75b4 1095done:
ddf22842
JA
1096 fill_ops(tip);
1097 return 0;
1098}
007c233c 1099
ddf22842
JA
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;
11eedd9b 1109 tip->fd = -1;
ddf22842
JA
1110 memset(&tip->fifo, 0, sizeof(tip->fifo));
1111 tip->leftover_ts = NULL;
1112
1113 if (tip_open_output(dip, tip))
1114 return 1;
0cc7d25e 1115
9db17354 1116 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
e7c9f3ff 1117 perror("pthread_create");
007c233c 1118 close_thread(tip);
e7c9f3ff 1119 return 1;
d0ca268b
JA
1120 }
1121 }
1122
e7c9f3ff 1123 return 0;
d0ca268b
JA
1124}
1125
e7c9f3ff 1126static void stop_threads(struct device_information *dip)
3aabcd89 1127{
e7c9f3ff 1128 struct thread_information *tip;
91816d54 1129 unsigned long ret;
007c233c
JA
1130 int i;
1131
9db17354 1132 for_each_tip(dip, tip, i) {
91816d54 1133 (void) pthread_join(tip->thread, (void *) &ret);
9db17354
JA
1134 close_thread(tip);
1135 }
3aabcd89
JA
1136}
1137
e7c9f3ff 1138static void stop_all_threads(void)
72ca8801 1139{
e7c9f3ff 1140 struct device_information *dip;
72ca8801
NS
1141 int i;
1142
99c1f5ab 1143 for_each_dip(dip, i)
e7c9f3ff
NS
1144 stop_threads(dip);
1145}
1146
1147static void stop_all_tracing(void)
1148{
1149 struct device_information *dip;
91816d54 1150 int i;
007c233c 1151
91816d54 1152 for_each_dip(dip, i)
e7c9f3ff 1153 stop_trace(dip);
72ca8801
NS
1154}
1155
1156static void exit_trace(int status)
1157{
eb3c8108
JA
1158 if (!is_trace_stopped()) {
1159 trace_stopped = 1;
1160 stop_all_threads();
1161 stop_all_tracing();
1162 }
1163
72ca8801
NS
1164 exit(status);
1165}
1166
e7c9f3ff
NS
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)
d0ca268b 1182{
e7c9f3ff 1183 struct device_information *dip;
d0ca268b 1184 int i;
d0ca268b 1185
99c1f5ab 1186 for_each_dip(dip, i) {
cf9208ea 1187 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
e7c9f3ff
NS
1188 if (dip->fd < 0) {
1189 perror(dip->path);
1190 return 1;
1191 }
1192 }
99c1f5ab 1193
e7c9f3ff
NS
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 }
d5396421 1208
99c1f5ab 1209 for_each_dip(dip, i) {
e7c9f3ff
NS
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 }
99c1f5ab 1217
e7c9f3ff 1218 if (i != ndevs) {
ce020676 1219 __for_each_dip(dip, device_information, i, j)
e7c9f3ff 1220 stop_trace(dip);
99c1f5ab 1221
e7c9f3ff
NS
1222 return 1;
1223 }
1224
99c1f5ab 1225 for_each_dip(dip, i) {
e7c9f3ff
NS
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 }
99c1f5ab 1232
e7c9f3ff 1233 if (i != ndevs) {
ce020676 1234 __for_each_dip(dip, device_information, i, j)
e7c9f3ff 1235 stop_threads(dip);
99c1f5ab 1236 for_each_dip(dip, i)
e7c9f3ff 1237 stop_trace(dip);
99c1f5ab 1238
e7c9f3ff 1239 return 1;
d0ca268b
JA
1240 }
1241
e7c9f3ff 1242 return 0;
d0ca268b
JA
1243}
1244
e0a1988b 1245static void show_stats(struct device_information *dips, int ndips, int cpus)
e7c9f3ff 1246{
e7c9f3ff
NS
1247 struct device_information *dip;
1248 struct thread_information *tip;
b7106311 1249 unsigned long long events_processed, data_read;
eb3c8108 1250 unsigned long total_drops;
2f903295 1251 int i, j, no_stdout = 0;
eb3c8108
JA
1252
1253 if (is_stat_shown())
1254 return;
1255
2f903295
JA
1256 if (output_name && !strcmp(output_name, "-"))
1257 no_stdout = 1;
1258
eb3c8108 1259 stat_shown = 1;
428683db 1260
56070ea4 1261 total_drops = 0;
ce020676 1262 __for_each_dip(dip, dips, ndips, i) {
2f903295 1263 if (!no_stdout)
56070ea4 1264 printf("Device: %s\n", dip->path);
e7c9f3ff 1265 events_processed = 0;
b7106311 1266 data_read = 0;
ce020676 1267 __for_each_tip(dip, tip, cpus, j) {
2f903295 1268 if (!no_stdout)
b7106311
JA
1269 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1270 tip->cpu, tip->events_processed,
54824c20 1271 (tip->data_read + 1023) >> 10);
e7c9f3ff 1272 events_processed += tip->events_processed;
b7106311 1273 data_read += tip->data_read;
e7c9f3ff 1274 }
eb3c8108 1275 total_drops += dip->drop_count;
2f903295 1276 if (!no_stdout)
b7106311
JA
1277 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1278 events_processed, dip->drop_count,
18d8437d 1279 (data_read + 1023) >> 10);
e7c9f3ff 1280 }
56070ea4
JA
1281
1282 if (total_drops)
1283 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
e7c9f3ff 1284}
52724a0e 1285
e0a1988b 1286static struct device_information *net_get_dip(struct net_connection *nc,
ff11d54c 1287 char *buts_name, u32 cl_id)
8e86c98a 1288{
ff11d54c
TZ
1289 struct device_information *dip, *cl_dip = NULL;
1290 struct cl_host *ch = nc->ch;
8e86c98a
JA
1291 int i;
1292
ff11d54c
TZ
1293 for (i = 0; i < ch->ndevs; i++) {
1294 dip = &ch->device_information[i];
8e86c98a 1295
22cd0c02
JA
1296 if (!strcmp(dip->buts_name, buts_name))
1297 return dip;
ff11d54c
TZ
1298
1299 if (dip->cl_id == cl_id)
1300 cl_dip = dip;
8e86c98a
JA
1301 }
1302
ff11d54c
TZ
1303 ch->device_information = realloc(ch->device_information, (ch->ndevs + 1) * sizeof(*dip));
1304 dip = &ch->device_information[ch->ndevs];
921b05fe
JA
1305 memset(dip, 0, sizeof(*dip));
1306 dip->fd = -1;
ff11d54c
TZ
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;
22cd0c02 1313 strcpy(dip->buts_name, buts_name);
921b05fe 1314 dip->path = strdup(buts_name);
7ab2f837 1315 dip->trace_started = 1;
ff11d54c 1316 ch->ndevs++;
e0a1988b
JA
1317 dip->threads = malloc(nc->ncpus * sizeof(struct thread_information));
1318 memset(dip->threads, 0, nc->ncpus * sizeof(struct thread_information));
22cd0c02
JA
1319
1320 /*
1321 * open all files
1322 */
e0a1988b 1323 for (i = 0; i < nc->ncpus; i++) {
22cd0c02 1324 struct thread_information *tip = &dip->threads[i];
8e86c98a 1325
22cd0c02 1326 tip->cpu = i;
22cd0c02 1327 tip->device = dip;
1366e53a 1328 tip->fd = -1;
ff11d54c
TZ
1329 tip->nc = nc;
1330
ddf22842 1331 if (tip_open_output(dip, tip))
22cd0c02 1332 return NULL;
ff11d54c
TZ
1333
1334 tip->nc = NULL;
8e86c98a
JA
1335 }
1336
22cd0c02
JA
1337 return dip;
1338}
1339
e0a1988b
JA
1340static struct thread_information *net_get_tip(struct net_connection *nc,
1341 struct blktrace_net_hdr *bnh)
22cd0c02
JA
1342{
1343 struct device_information *dip;
ff11d54c 1344 struct thread_information *tip;
22cd0c02 1345
ff11d54c 1346 dip = net_get_dip(nc, bnh->buts_name, bnh->cl_id);
7ab2f837
JA
1347 if (!dip->trace_started) {
1348 fprintf(stderr, "Events for closed devices %s\n", dip->buts_name);
1349 return NULL;
1350 }
1351
ff11d54c
TZ
1352 tip = &dip->threads[bnh->cpu];
1353 if (!tip->nc)
1354 tip->nc = nc;
1355
1356 return tip;
8e86c98a
JA
1357}
1358
e0a1988b
JA
1359static int net_get_header(struct net_connection *nc,
1360 struct blktrace_net_hdr *bnh)
8e86c98a 1361{
e0a1988b 1362 int fl = fcntl(nc->in_fd, F_GETFL);
8e86c98a
JA
1363 int bytes_left, ret;
1364 void *p = bnh;
1365
e0a1988b 1366 fcntl(nc->in_fd, F_SETFL, fl | O_NONBLOCK);
8e86c98a
JA
1367 bytes_left = sizeof(*bnh);
1368 while (bytes_left && !is_done()) {
e0a1988b 1369 ret = recv(nc->in_fd, p, bytes_left, MSG_WAITALL);
8e86c98a
JA
1370 if (ret < 0) {
1371 if (errno != EAGAIN) {
1372 perror("recv header");
1373 return 1;
1374 }
7934e668 1375 usleep(1000);
8e86c98a
JA
1376 continue;
1377 } else if (!ret) {
7934e668 1378 usleep(1000);
8e86c98a
JA
1379 continue;
1380 } else {
1381 p += ret;
1382 bytes_left -= ret;
1383 }
1384 }
e0a1988b 1385 fcntl(nc->in_fd, F_SETFL, fl & ~O_NONBLOCK);
227f89ff 1386 return bytes_left;
8e86c98a
JA
1387}
1388
e0a1988b
JA
1389/*
1390 * finalize a net client: truncate files, show stats, cleanup, etc
1391 */
ff11d54c 1392static void device_done(struct net_connection *nc, struct device_information *dip)
e0a1988b 1393{
e0a1988b 1394 struct thread_information *tip;
ff11d54c 1395 int i;
e0a1988b 1396
ff11d54c
TZ
1397 __for_each_tip(dip, tip, nc->ncpus, i)
1398 tip_ftrunc_final(tip);
e0a1988b 1399
ff11d54c 1400 show_stats(dip, 1, nc->ncpus);
e0a1988b
JA
1401
1402 /*
1403 * cleanup for next run
1404 */
ff11d54c
TZ
1405 __for_each_tip(dip, tip, nc->ncpus, i) {
1406 if (tip->ofile)
1407 fclose(tip->ofile);
e0a1988b
JA
1408 }
1409
ff11d54c
TZ
1410 free(dip->threads);
1411 free(dip->path);
e0a1988b
JA
1412
1413 close(nc->in_fd);
1414 nc->in_fd = -1;
1415
ff11d54c
TZ
1416 stat_shown = 0;
1417}
e0a1988b 1418
ff11d54c
TZ
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;
e0a1988b 1445 }
ff11d54c 1446}
e0a1988b 1447
ff11d54c
TZ
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
ff11d54c
TZ
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);
e0a1988b
JA
1468}
1469
1470/*
1471 * handle incoming events from a net client
1472 */
1473static int net_client_data(struct net_connection *nc)
8e86c98a
JA
1474{
1475 struct thread_information *tip;
1476 struct blktrace_net_hdr bnh;
1477
e0a1988b 1478 if (net_get_header(nc, &bnh))
8e86c98a
JA
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) {
227f89ff 1487 bnh.magic = be32_to_cpu(bnh.magic);
8e86c98a 1488 bnh.cpu = be32_to_cpu(bnh.cpu);
4aeec019 1489 bnh.max_cpus = be32_to_cpu(bnh.max_cpus);
8e86c98a 1490 bnh.len = be32_to_cpu(bnh.len);
ff11d54c 1491 bnh.cl_id = be32_to_cpu(bnh.cl_id);
8e86c98a
JA
1492 }
1493
227f89ff
JA
1494 if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1495 fprintf(stderr, "server: bad data magic\n");
1496 return 1;
1497 }
1498
4aeec019
JA
1499 if (nc->ncpus == -1)
1500 nc->ncpus = bnh.max_cpus;
1501
6a752c90
JA
1502 /*
1503 * len == 0 means that the other end signalled end-of-run
1504 */
1505 if (!bnh.len) {
7934e668
JA
1506 /*
1507 * overload cpu count with dropped events
1508 */
1509 struct device_information *dip;
1510
ff11d54c 1511 dip = net_get_dip(nc, bnh.buts_name, bnh.cl_id);
7934e668 1512 dip->drop_count = bnh.cpu;
7ab2f837 1513 dip->trace_started = 0;
7934e668 1514
e0a1988b 1515 printf("server: end of run for %s\n", dip->buts_name);
4aeec019 1516
ff11d54c
TZ
1517 device_done(nc, dip);
1518
1519 if (++nc->ch->ndevs_done == nc->ch->ndevs)
1520 net_client_host_done(nc->ch);
4aeec019 1521
e0a1988b 1522 return 0;
6a752c90
JA
1523 }
1524
e0a1988b 1525 tip = net_get_tip(nc, &bnh);
8e86c98a
JA
1526 if (!tip)
1527 return 1;
1528
1529 if (mmap_subbuf(tip, bnh.len))
1530 return 1;
1531
1532 return 0;
1533}
1534
e0a1988b 1535static void net_add_connection(int listen_fd, struct sockaddr_in *addr)
659bcc3f 1536{
e0a1988b
JA
1537 socklen_t socklen = sizeof(*addr);
1538 struct net_connection *nc;
ff11d54c
TZ
1539 struct cl_host *ch;
1540 int in_fd;
659bcc3f 1541
ff11d54c
TZ
1542 in_fd = accept(listen_fd, (struct sockaddr *) addr, &socklen);
1543 if (in_fd < 0) {
1544 perror("accept");
e0a1988b 1545 return;
659bcc3f 1546 }
659bcc3f 1547
ff11d54c
TZ
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);
506cdb6d
JA
1558
1559 printf("server: connection from %s\n", inet_ntoa(addr->sin_addr));
659bcc3f
JA
1560 }
1561
ff11d54c
TZ
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
e0a1988b 1566 time(&nc->connect_time);
ff11d54c
TZ
1567 nc->ch = ch;
1568 nc->in_fd = in_fd;
4aeec019 1569 nc->ncpus = -1;
e0a1988b
JA
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{
ff11d54c
TZ
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
e0a1988b
JA
1587 printf("server: waiting for connections...\n");
1588
1589 while (!is_done()) {
ff11d54c
TZ
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 }
e0a1988b
JA
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;
ff11d54c
TZ
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 }
e0a1988b
JA
1609 }
1610
ff11d54c 1611 events = poll(pfds, 1 + nconns, -1);
e0a1988b
JA
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
ff11d54c 1626 for (i = 0; events && i < nconns; i++) {
e0a1988b 1627 if (pfds[i + 1].revents & POLLIN) {
ff11d54c 1628 net_client_data(ncs[i]);
e0a1988b
JA
1629 events--;
1630 }
1631 }
1632 }
659bcc3f
JA
1633}
1634
8e86c98a
JA
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;
e0a1988b 1642 int fd, opt;
8e86c98a
JA
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
e0a1988b
JA
1671 net_server_handle_connections(fd, &addr);
1672 return 0;
8e86c98a
JA
1673}
1674
1675/*
1676 * Setup outgoing network connection where we will transmit data
1677 */
ff11d54c 1678static int net_setup_client_cpu(int i, struct sockaddr_in *addr)
8e86c98a 1679{
8e86c98a
JA
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
ff11d54c
TZ
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
8e86c98a
JA
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
ff11d54c
TZ
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;
8e86c98a
JA
1723 }
1724
1725 printf("blktrace: connected!\n");
ff11d54c 1726
8e86c98a
JA
1727 return 0;
1728}
1729
52724a0e 1730static char usage_str[] = \
3d06efea 1731 "-d <dev> [ -r debugfs path ] [ -o <output> ] [-k ] [ -w time ]\n" \
52724a0e
JA
1732 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1733 "\t-d Use specified device. May also be given last after options\n" \
3d06efea 1734 "\t-r Path to mounted debugfs, defaults to /debug\n" \
52724a0e 1735 "\t-o File(s) to send output to\n" \
d1d7f15f 1736 "\t-D Directory to prepend to output file names\n" \
52724a0e
JA
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" \
129aa440
JA
1741 "\t-b Sub buffer size in KiB\n" \
1742 "\t-n Number of sub buffers\n" \
f531b94d
JA
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" \
ff11d54c 1746 "\t-s Make the network client NOT use sendfile() to transfer data\n" \
f531b94d 1747 "\t-V Print program version info\n\n";
52724a0e 1748
ee1f4158
NS
1749static void show_usage(char *program)
1750{
52724a0e 1751 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
ee1f4158 1752}
d0ca268b
JA
1753
1754int main(int argc, char *argv[])
1755{
3d06efea 1756 static char default_debugfs_path[] = "/debug";
e3e74029 1757 struct statfs st;
d39c04ca 1758 int i, c;
ece238a6 1759 int stop_watch = 0;
d39c04ca
AB
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) {
ab197ca7 1767 fprintf(stderr,"Invalid action mask %s\n",
d39c04ca 1768 optarg);
7425d456 1769 return 1;
d39c04ca
AB
1770 }
1771 act_mask_tmp |= i;
1772 break;
1773
1774 case 'A':
98f8386b
AB
1775 if ((sscanf(optarg, "%x", &i) != 1) ||
1776 !valid_act_opt(i)) {
d39c04ca 1777 fprintf(stderr,
ab197ca7 1778 "Invalid set action mask %s/0x%x\n",
d39c04ca 1779 optarg, i);
7425d456 1780 return 1;
d39c04ca
AB
1781 }
1782 act_mask_tmp = i;
1783 break;
d0ca268b 1784
d39c04ca 1785 case 'd':
e7c9f3ff
NS
1786 if (resize_devices(optarg) != 0)
1787 return 1;
d39c04ca
AB
1788 break;
1789
5270dddd 1790 case 'r':
3d06efea 1791 debugfs_path = optarg;
5270dddd
JA
1792 break;
1793
d5396421 1794 case 'o':
66efebf8 1795 output_name = optarg;
d5396421 1796 break;
bc39777c
JA
1797 case 'k':
1798 kill_running_trace = 1;
1799 break;
ece238a6
NS
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;
57ea8602 1809 case 'V':
52724a0e
JA
1810 printf("%s version %s\n", argv[0], blktrace_version);
1811 return 0;
129aa440 1812 case 'b':
eb3c8108 1813 buf_size = strtoul(optarg, NULL, 10);
183a0855 1814 if (buf_size <= 0 || buf_size > 16*1024) {
129aa440 1815 fprintf(stderr,
eb3c8108 1816 "Invalid buffer size (%lu)\n",buf_size);
129aa440
JA
1817 return 1;
1818 }
1819 buf_size <<= 10;
1820 break;
1821 case 'n':
eb3c8108 1822 buf_nr = strtoul(optarg, NULL, 10);
129aa440
JA
1823 if (buf_nr <= 0) {
1824 fprintf(stderr,
eb3c8108 1825 "Invalid buffer nr (%lu)\n", buf_nr);
129aa440
JA
1826 return 1;
1827 }
1828 break;
d1d7f15f
JA
1829 case 'D':
1830 output_dir = optarg;
1831 break;
8e86c98a
JA
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;
32f18c48 1842 case 's':
79971f43 1843 net_use_sendfile = 0;
32f18c48 1844 break;
d39c04ca 1845 default:
ee1f4158 1846 show_usage(argv[0]);
7425d456 1847 return 1;
d39c04ca
AB
1848 }
1849 }
1850
8e86c98a
JA
1851 setlocale(LC_NUMERIC, "en_US");
1852
1853 page_size = getpagesize();
1854
1855 if (net_mode == Net_server)
1856 return net_server();
1857
22cd0c02
JA
1858 while (optind < argc) {
1859 if (resize_devices(argv[optind++]) != 0)
1860 return 1;
1861 }
1862
e7c9f3ff 1863 if (ndevs == 0) {
ee1f4158 1864 show_usage(argv[0]);
7425d456 1865 return 1;
d39c04ca
AB
1866 }
1867
d5396421 1868 if (act_mask_tmp != 0)
d39c04ca 1869 act_mask = act_mask_tmp;
d0ca268b 1870
3d06efea
JA
1871 if (!debugfs_path)
1872 debugfs_path = default_debugfs_path;
1873
1874 if (statfs(debugfs_path, &st) < 0) {
e3e74029
NS
1875 perror("statfs");
1876 fprintf(stderr,"%s does not appear to be a valid path\n",
3d06efea 1877 debugfs_path);
e3e74029 1878 return 1;
3d06efea
JA
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);
7425d456 1882 return 1;
d0ca268b
JA
1883 }
1884
e7c9f3ff 1885 if (open_devices() != 0)
7425d456 1886 return 1;
bc39777c
JA
1887
1888 if (kill_running_trace) {
e7c9f3ff 1889 stop_all_traces();
7425d456 1890 return 0;
bc39777c
JA
1891 }
1892
e7c9f3ff
NS
1893 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1894 if (ncpus < 0) {
1895 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
7425d456 1896 return 1;
d0ca268b
JA
1897 }
1898
d0ca268b
JA
1899 signal(SIGINT, handle_sigint);
1900 signal(SIGHUP, handle_sigint);
1901 signal(SIGTERM, handle_sigint);
ece238a6 1902 signal(SIGALRM, handle_sigint);
d0ca268b 1903
8e86c98a
JA
1904 if (net_mode == Net_client && net_setup_client())
1905 return 1;
1906
1907 if (start_devices() != 0)
1908 return 1;
1909
e7c9f3ff 1910 atexit(stop_all_tracing);
830fd65c 1911
ece238a6
NS
1912 if (stop_watch)
1913 alarm(stop_watch);
1914
b7106311 1915 wait_for_threads();
d0ca268b 1916
eb3c8108
JA
1917 if (!is_trace_stopped()) {
1918 trace_stopped = 1;
91816d54
JA
1919 stop_all_threads();
1920 stop_all_traces();
91816d54 1921 }
d0ca268b 1922
e0a1988b 1923 show_stats(device_information, ndevs, ncpus);
eb3c8108 1924
d0ca268b
JA
1925 return 0;
1926}
1927