2 * block queue tracing application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
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.
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.
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
22 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/param.h>
31 #include <sys/statfs.h>
34 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
44 #include <sys/sendfile.h>
49 static char blktrace_version[] = "0.99";
52 * You may want to increase this even more, if you are logging at a high
53 * rate and see skipped/missed events
55 #define BUF_SIZE (512 * 1024)
58 #define OFILE_BUF (128 * 1024)
60 #define RELAYFS_TYPE 0xF0B4A981
62 #define S_OPTS "d:a:A:r:o:kw:Vb:n:D:lh:p:s"
63 static struct option l_opts[] = {
66 .has_arg = required_argument,
72 .has_arg = required_argument,
78 .has_arg = required_argument,
84 .has_arg = required_argument,
90 .has_arg = required_argument,
96 .has_arg = no_argument,
102 .has_arg = required_argument,
108 .has_arg = no_argument,
113 .name = "buffer-size",
114 .has_arg = required_argument,
119 .name = "num-sub-buffers",
120 .has_arg = required_argument,
125 .name = "output-dir",
126 .has_arg = required_argument,
132 .has_arg = no_argument,
138 .has_arg = required_argument,
144 .has_arg = required_argument,
150 .has_arg = no_argument,
162 unsigned int max_len;
165 #define FIFO_SIZE (1024) /* should be plenty big! */
166 #define CL_SIZE (128) /* cache line, any bigger? */
168 struct tip_subbuf_fifo {
169 int tail __attribute__((aligned(CL_SIZE)));
170 int head __attribute__((aligned(CL_SIZE)));
171 struct tip_subbuf *q[FIFO_SIZE];
174 struct thread_information {
180 char fn[MAXPATHLEN + 64];
182 struct in_addr cl_in_addr;
189 volatile int sendfile_pending;
191 int (*get_subbuf)(struct thread_information *, unsigned int);
192 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
193 int (*read_data)(struct thread_information *, void *, unsigned int);
195 unsigned long events_processed;
196 unsigned long long data_read;
197 unsigned long long data_queued;
198 struct device_information *device;
205 struct tip_subbuf_fifo fifo;
206 struct tip_subbuf *leftover_ts;
209 * mmap controlled output files
211 unsigned long long fs_size;
212 unsigned long long fs_max_size;
213 unsigned long fs_off;
215 unsigned long fs_buf_len;
218 struct device_information {
222 volatile int trace_started;
223 unsigned long drop_count;
224 struct thread_information *threads;
228 static struct thread_information *thread_information;
230 static struct device_information *device_information;
232 /* command line option globals */
233 static char *relay_path;
234 static char *output_name;
235 static char *output_dir;
236 static int act_mask = ~0U;
237 static int kill_running_trace;
238 static unsigned long buf_size = BUF_SIZE;
239 static unsigned long buf_nr = BUF_NR;
240 static unsigned int page_size;
242 #define is_done() (*(volatile int *)(&done))
243 static volatile int done;
245 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
246 static volatile int trace_stopped;
248 #define is_stat_shown() (*(volatile int *)(&stat_shown))
249 static volatile int stat_shown;
251 int data_is_native = -1;
253 static void exit_trace(int status);
255 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
256 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
258 #define __for_each_dip(__d, __i, __e) \
259 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
261 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
262 #define for_each_tip(__d, __t, __j) \
263 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
266 * networking stuff follows. we include a magic number so we know whether
267 * to endianness convert or not
269 struct blktrace_net_hdr {
270 u32 magic; /* same as trace magic */
271 char buts_name[32]; /* trace name */
272 u32 cpu; /* for which cpu */
274 u32 len; /* length of following trace data */
277 #define TRACE_NET_PORT (8462)
286 * network cmd line params
288 static char hostname[MAXHOSTNAMELEN];
289 static int net_port = TRACE_NET_PORT;
290 static int net_mode = 0;
291 static int net_use_sendfile;
293 static int net_in_fd = -1;
294 static int net_out_fd = -1;
296 static void handle_sigint(__attribute__((__unused__)) int sig)
298 struct device_information *dip;
302 * stop trace so we can reap currently produced data
304 for_each_dip(dip, i) {
307 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
308 perror("BLKTRACESTOP");
314 static int get_dropped_count(const char *buts_name)
317 char tmp[MAXPATHLEN + 64];
319 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
320 relay_path, buts_name);
322 fd = open(tmp, O_RDONLY);
325 * this may be ok, if the kernel doesn't support dropped counts
330 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
334 if (read(fd, tmp, sizeof(tmp)) < 0) {
345 static int start_trace(struct device_information *dip)
347 struct blk_user_trace_setup buts;
349 memset(&buts, 0, sizeof(buts));
350 buts.buf_size = buf_size;
351 buts.buf_nr = buf_nr;
352 buts.act_mask = act_mask;
354 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
355 perror("BLKTRACESETUP");
359 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
360 perror("BLKTRACESTART");
364 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
365 dip_set_tracing(dip, 1);
369 static void stop_trace(struct device_information *dip)
371 if (dip_tracing(dip) || kill_running_trace) {
372 dip_set_tracing(dip, 0);
375 * should be stopped, just don't complain if it isn't
377 ioctl(dip->fd, BLKTRACESTOP);
379 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
380 perror("BLKTRACETEARDOWN");
387 static void stop_all_traces(void)
389 struct device_information *dip;
392 for_each_dip(dip, i) {
393 dip->drop_count = get_dropped_count(dip->buts_name);
398 static void wait_for_data(struct thread_information *tip)
400 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
404 if (pfd.revents & POLLIN)
406 if (tip->ofile_stdout)
408 } while (!is_done());
411 static int read_data_file(struct thread_information *tip, void *buf,
419 ret = read(tip->fd, buf, len);
425 if (errno != EAGAIN) {
427 fprintf(stderr,"Thread %d failed read of %s\n",
433 } while (!is_done());
439 static int read_data_net(struct thread_information *tip, void *buf,
442 unsigned int bytes_left = len;
446 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
451 if (errno != EAGAIN) {
453 fprintf(stderr, "server: failed read\n");
461 } while (!is_done() && bytes_left);
463 return len - bytes_left;
466 static int read_data(struct thread_information *tip, void *buf,
469 return tip->read_data(tip, buf, len);
472 static inline struct tip_subbuf *
473 subbuf_fifo_dequeue(struct thread_information *tip)
475 const int head = tip->fifo.head;
476 const int next = (head + 1) & (FIFO_SIZE - 1);
478 if (head != tip->fifo.tail) {
479 struct tip_subbuf *ts = tip->fifo.q[head];
482 tip->fifo.head = next;
489 static inline int subbuf_fifo_queue(struct thread_information *tip,
490 struct tip_subbuf *ts)
492 const int tail = tip->fifo.tail;
493 const int next = (tail + 1) & (FIFO_SIZE - 1);
495 if (next != tip->fifo.head) {
496 tip->fifo.q[tail] = ts;
498 tip->fifo.tail = next;
502 fprintf(stderr, "fifo too small!\n");
507 * For file output, truncate and mmap the file appropriately
509 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
511 int ofd = fileno(tip->ofile);
515 * extend file, if we have to. use chunks of 16 subbuffers.
517 if (tip->fs_off + buf_size > tip->fs_buf_len) {
519 munlock(tip->fs_buf, tip->fs_buf_len);
520 munmap(tip->fs_buf, tip->fs_buf_len);
524 tip->fs_off = tip->fs_size & (page_size - 1);
525 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
526 tip->fs_max_size += tip->fs_buf_len;
528 if (ftruncate(ofd, tip->fs_max_size) < 0) {
533 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
534 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
535 if (tip->fs_buf == MAP_FAILED) {
539 mlock(tip->fs_buf, tip->fs_buf_len);
542 ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
544 tip->data_read += ret;
554 * Use the copy approach for pipes and network
556 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
558 struct tip_subbuf *ts = malloc(sizeof(*ts));
561 ts->buf = malloc(buf_size);
562 ts->max_len = maxlen;
564 ret = read_data(tip, ts->buf, ts->max_len);
567 tip->data_read += ret;
568 if (subbuf_fifo_queue(tip, ts))
575 static int get_subbuf_sendfile(struct thread_information *tip,
578 struct tip_subbuf *ts;
585 * hack to get last data out, we can't use sendfile for that
588 return get_subbuf(tip, maxlen);
590 if (tip->sendfile_pending) {
595 if (fstat(tip->fd, &sb) < 0) {
596 perror("trace stat");
599 ready = sb.st_size - tip->data_queued;
603 ts = malloc(sizeof(*ts));
607 tip->data_queued += ready;
609 if (subbuf_fifo_queue(tip, ts))
612 tip->sendfile_pending++;
616 static void close_thread(struct thread_information *tip)
622 if (tip->ofile_buffer)
623 free(tip->ofile_buffer);
629 tip->ofile_buffer = NULL;
633 static void tip_ftrunc_final(struct thread_information *tip)
636 * truncate to right size and cleanup mmap
638 if (tip->ofile_mmap) {
639 int ofd = fileno(tip->ofile);
642 munmap(tip->fs_buf, tip->fs_buf_len);
644 ftruncate(ofd, tip->fs_size);
648 static void *thread_main(void *arg)
650 struct thread_information *tip = arg;
651 pid_t pid = getpid();
655 CPU_SET((tip->cpu), &cpu_mask);
657 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
658 perror("sched_setaffinity");
662 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
663 relay_path, tip->device->buts_name, tip->cpu);
664 tip->fd = open(tip->fn, O_RDONLY);
667 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
673 if (tip->get_subbuf(tip, buf_size) < 0)
678 * trace is stopped, pull data until we get a short read
680 while (tip->get_subbuf(tip, buf_size) > 0)
683 tip_ftrunc_final(tip);
688 static int write_data_net(int fd, void *buf, unsigned int buf_len)
690 unsigned int bytes_left = buf_len;
694 ret = send(fd, buf, bytes_left, 0);
707 static int net_send_header(struct thread_information *tip, unsigned int len)
709 struct blktrace_net_hdr hdr;
711 hdr.magic = BLK_IO_TRACE_MAGIC;
712 strcpy(hdr.buts_name, tip->device->buts_name);
714 hdr.max_cpus = ncpus;
717 return write_data_net(net_out_fd, &hdr, sizeof(hdr));
721 * send header with 0 length to signal end-of-run
723 static void net_client_send_close(void)
725 struct blktrace_net_hdr hdr;
727 hdr.magic = BLK_IO_TRACE_MAGIC;
729 hdr.max_cpus = ncpus;
732 write_data_net(net_out_fd, &hdr, sizeof(hdr));
735 static int flush_subbuf_net(struct thread_information *tip,
736 struct tip_subbuf *ts)
738 if (net_send_header(tip, ts->len))
740 if (write_data_net(net_out_fd, ts->buf, ts->len))
748 static int net_sendfile(struct thread_information *tip, struct tip_subbuf *ts)
750 int ret = sendfile(net_out_fd, tip->fd, NULL, ts->len);
755 } else if (ret < (int) ts->len) {
756 fprintf(stderr, "short sendfile send (%d of %d)\n", ret, ts->len);
763 static int flush_subbuf_sendfile(struct thread_information *tip,
764 struct tip_subbuf *ts)
769 * currently we cannot use sendfile() on the last bytes read, as they
770 * may not be a full subbuffer. get_subbuf_sendfile() falls back to
771 * the read approach for those, so use send() to ship them out
774 return flush_subbuf_net(tip, ts);
776 if (net_send_header(tip, ts->len))
778 if (net_sendfile(tip, ts))
781 tip->data_read += ts->len;
782 tip->ofile_offset += buf_size;
785 tip->sendfile_pending--;
790 static int write_data(struct thread_information *tip, void *buf,
791 unsigned int buf_len)
799 ret = fwrite(buf, buf_len, 1, tip->ofile);
809 if (tip->ofile_stdout)
815 static int flush_subbuf_file(struct thread_information *tip,
816 struct tip_subbuf *ts)
818 unsigned int offset = 0;
819 struct blk_io_trace *t;
820 int pdu_len, events = 0;
823 * surplus from last run
825 if (tip->leftover_ts) {
826 struct tip_subbuf *prev_ts = tip->leftover_ts;
828 if (prev_ts->len + ts->len > prev_ts->max_len) {
829 prev_ts->max_len += ts->len;
830 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
833 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
834 prev_ts->len += ts->len;
840 tip->leftover_ts = NULL;
843 while (offset + sizeof(*t) <= ts->len) {
844 t = ts->buf + offset;
846 if (verify_trace(t)) {
847 write_data(tip, ts->buf, offset);
851 pdu_len = t->pdu_len;
853 if (offset + sizeof(*t) + pdu_len > ts->len)
856 offset += sizeof(*t) + pdu_len;
857 tip->events_processed++;
858 tip->data_read += sizeof(*t) + pdu_len;
862 if (write_data(tip, ts->buf, offset))
866 * leftover bytes, save them for next time
868 if (offset != ts->len) {
869 tip->leftover_ts = ts;
871 memmove(ts->buf, ts->buf + offset, ts->len);
880 static int write_tip_events(struct thread_information *tip)
882 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
885 return tip->flush_subbuf(tip, ts);
891 * scans the tips we know and writes out the subbuffers we accumulate
893 static void get_and_write_events(void)
895 struct device_information *dip;
896 struct thread_information *tip;
897 int i, j, events, ret, tips_running;
902 for_each_dip(dip, i) {
903 for_each_tip(dip, tip, j) {
904 ret = write_tip_events(tip);
920 for_each_dip(dip, i) {
921 for_each_tip(dip, tip, j) {
922 ret = write_tip_events(tip);
925 tips_running += !tip->exited;
929 } while (events || tips_running);
932 static void wait_for_threads(void)
935 * for piped or network output, poll and fetch data for writeout.
936 * for files, we just wait around for trace threads to exit
938 if ((output_name && !strcmp(output_name, "-")) ||
939 net_mode == Net_client)
940 get_and_write_events();
942 struct device_information *dip;
943 struct thread_information *tip;
944 int i, j, tips_running;
951 for_each_tip(dip, tip, j)
952 tips_running += !tip->exited;
953 } while (tips_running);
956 if (net_mode == Net_client)
957 net_client_send_close();
960 static int fill_ofname(struct thread_information *tip, char *dst,
968 len = sprintf(dst, "%s/", output_dir);
970 if (net_mode == Net_server) {
971 len += sprintf(dst + len, "%s-", inet_ntoa(tip->cl_in_addr));
973 len += strftime(dst + len, 64, "%F-%T/", gmtime(&t));
976 if (stat(dst, &sb) < 0) {
977 if (errno != ENOENT) {
981 if (mkdir(dst, 0755) < 0) {
983 fprintf(stderr, "Can't make output dir\n");
989 sprintf(dst + len, "%s.blktrace.%d", output_name, tip->cpu);
991 sprintf(dst + len, "%s.blktrace.%d", buts_name, tip->cpu);
996 static void fill_ops(struct thread_information *tip)
1001 if (net_mode == Net_client) {
1002 if (net_use_sendfile) {
1003 tip->get_subbuf = get_subbuf_sendfile;
1004 tip->flush_subbuf = flush_subbuf_sendfile;
1006 tip->get_subbuf = get_subbuf;
1007 tip->flush_subbuf = flush_subbuf_net;
1010 if (tip->ofile_mmap)
1011 tip->get_subbuf = mmap_subbuf;
1013 tip->get_subbuf = get_subbuf;
1015 tip->flush_subbuf = flush_subbuf_file;
1018 if (net_mode == Net_server)
1019 tip->read_data = read_data_net;
1021 tip->read_data = read_data_file;
1024 static int tip_open_output(struct device_information *dip,
1025 struct thread_information *tip)
1027 int pipeline = output_name && !strcmp(output_name, "-");
1028 int mode, vbuf_size;
1031 if (net_mode == Net_client) {
1033 tip->ofile_stdout = 0;
1034 tip->ofile_mmap = 0;
1036 } else if (pipeline) {
1037 tip->ofile = fdopen(STDOUT_FILENO, "w");
1038 tip->ofile_stdout = 1;
1039 tip->ofile_mmap = 0;
1043 if (fill_ofname(tip, op, dip->buts_name))
1045 tip->ofile = fopen(op, "w+");
1046 tip->ofile_stdout = 0;
1047 tip->ofile_mmap = 1;
1049 vbuf_size = OFILE_BUF;
1052 if (tip->ofile == NULL) {
1057 tip->ofile_buffer = malloc(vbuf_size);
1058 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
1069 static int start_threads(struct device_information *dip)
1071 struct thread_information *tip;
1074 for_each_tip(dip, tip, j) {
1077 tip->events_processed = 0;
1079 memset(&tip->fifo, 0, sizeof(tip->fifo));
1080 tip->leftover_ts = NULL;
1082 if (tip_open_output(dip, tip))
1085 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
1086 perror("pthread_create");
1095 static void stop_threads(struct device_information *dip)
1097 struct thread_information *tip;
1101 for_each_tip(dip, tip, i) {
1102 (void) pthread_join(tip->thread, (void *) &ret);
1107 static void stop_all_threads(void)
1109 struct device_information *dip;
1112 for_each_dip(dip, i)
1116 static void stop_all_tracing(void)
1118 struct device_information *dip;
1121 for_each_dip(dip, i)
1125 static void exit_trace(int status)
1127 if (!is_trace_stopped()) {
1136 static int resize_devices(char *path)
1138 int size = (ndevs + 1) * sizeof(struct device_information);
1140 device_information = realloc(device_information, size);
1141 if (!device_information) {
1142 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1145 device_information[ndevs].path = path;
1150 static int open_devices(void)
1152 struct device_information *dip;
1155 for_each_dip(dip, i) {
1156 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1166 static int start_devices(void)
1168 struct device_information *dip;
1171 size = ncpus * sizeof(struct thread_information);
1172 thread_information = malloc(size * ndevs);
1173 if (!thread_information) {
1174 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1178 for_each_dip(dip, i) {
1179 if (start_trace(dip)) {
1181 fprintf(stderr, "Failed to start trace on %s\n",
1188 __for_each_dip(dip, j, i)
1194 for_each_dip(dip, i) {
1195 dip->threads = thread_information + (i * ncpus);
1196 if (start_threads(dip)) {
1197 fprintf(stderr, "Failed to start worker threads\n");
1203 __for_each_dip(dip, j, i)
1205 for_each_dip(dip, i)
1214 static void show_stats(void)
1216 struct device_information *dip;
1217 struct thread_information *tip;
1218 unsigned long long events_processed, data_read;
1219 unsigned long total_drops;
1220 int i, j, no_stdout = 0;
1222 if (is_stat_shown())
1225 if (output_name && !strcmp(output_name, "-"))
1231 for_each_dip(dip, i) {
1233 printf("Device: %s\n", dip->path);
1234 events_processed = 0;
1236 for_each_tip(dip, tip, j) {
1238 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1239 tip->cpu, tip->events_processed,
1240 (tip->data_read + 1023) >> 10);
1241 events_processed += tip->events_processed;
1242 data_read += tip->data_read;
1244 total_drops += dip->drop_count;
1246 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1247 events_processed, dip->drop_count,
1248 (data_read + 1023) >> 10);
1252 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1255 static struct device_information *net_get_dip(char *buts_name,
1256 struct in_addr *cl_in_addr)
1258 struct device_information *dip;
1261 for (i = 0; i < ndevs; i++) {
1262 dip = &device_information[i];
1264 if (!strcmp(dip->buts_name, buts_name))
1268 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1269 dip = &device_information[ndevs];
1270 memset(dip, 0, sizeof(*dip));
1272 strcpy(dip->buts_name, buts_name);
1273 dip->path = strdup(buts_name);
1275 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1276 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1281 for (i = 0; i < ncpus; i++) {
1282 struct thread_information *tip = &dip->threads[i];
1287 tip->cl_in_addr = *cl_in_addr;
1289 if (tip_open_output(dip, tip))
1296 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh,
1297 struct in_addr *cl_in_addr)
1299 struct device_information *dip;
1301 ncpus = bnh->max_cpus;
1302 dip = net_get_dip(bnh->buts_name, cl_in_addr);
1303 return &dip->threads[bnh->cpu];
1306 static int net_get_header(struct blktrace_net_hdr *bnh)
1308 int fl = fcntl(net_in_fd, F_GETFL);
1309 int bytes_left, ret;
1312 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1313 bytes_left = sizeof(*bnh);
1314 while (bytes_left && !is_done()) {
1315 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1317 if (errno != EAGAIN) {
1318 perror("recv header");
1331 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1335 static int net_server_loop(struct in_addr *cl_in_addr)
1337 struct thread_information *tip;
1338 struct blktrace_net_hdr bnh;
1340 if (net_get_header(&bnh))
1343 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1344 fprintf(stderr, "server: received data is bad\n");
1348 if (!data_is_native) {
1349 bnh.magic = be32_to_cpu(bnh.magic);
1350 bnh.cpu = be32_to_cpu(bnh.cpu);
1351 bnh.len = be32_to_cpu(bnh.len);
1354 if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1355 fprintf(stderr, "server: bad data magic\n");
1360 * len == 0 means that the other end signalled end-of-run
1363 fprintf(stderr, "server: end of run\n");
1367 tip = net_get_tip(&bnh, cl_in_addr);
1371 if (mmap_subbuf(tip, bnh.len))
1377 static int get_connection(int fd, struct sockaddr_in *addr)
1379 struct pollfd pfd = { .fd = fd, .events = POLLIN };
1382 printf("blktrace: waiting for incoming connection...\n");
1384 if (poll(&pfd, 1, -1) < 0) {
1385 perror("poll for connection");
1388 if ((pfd.revents & POLLIN) == 0)
1391 socklen = sizeof(*addr);
1392 net_in_fd = accept(fd, (struct sockaddr *) addr, &socklen);
1393 if (net_in_fd < 0) {
1398 printf("blktrace: connection from %s\n", inet_ntoa(addr->sin_addr));
1403 * Start here when we are in server mode - just fetch data from the network
1406 static int net_server(void)
1408 struct device_information *dip;
1409 struct thread_information *tip;
1410 struct sockaddr_in addr;
1413 fd = socket(AF_INET, SOCK_STREAM, 0);
1415 perror("server: socket");
1420 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1421 perror("setsockopt");
1425 memset(&addr, 0, sizeof(addr));
1426 addr.sin_family = AF_INET;
1427 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1428 addr.sin_port = htons(net_port);
1430 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1435 if (listen(fd, 1) < 0) {
1441 if (get_connection(fd, &addr))
1444 while (!is_done()) {
1445 if (net_server_loop(&addr.sin_addr))
1449 for_each_dip(dip, i)
1450 for_each_tip(dip, tip, j)
1451 tip_ftrunc_final(tip);
1459 * cleanup for next run
1461 for_each_dip(dip, i) {
1462 for_each_tip(dip, tip, j)
1469 free(device_information);
1470 device_information = NULL;
1480 * Setup outgoing network connection where we will transmit data
1482 static int net_setup_client(void)
1484 struct sockaddr_in addr;
1487 fd = socket(AF_INET, SOCK_STREAM, 0);
1489 perror("client: socket");
1493 memset(&addr, 0, sizeof(addr));
1494 addr.sin_family = AF_INET;
1495 addr.sin_port = htons(net_port);
1497 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1498 struct hostent *hent = gethostbyname(hostname);
1500 perror("gethostbyname");
1504 memcpy(&addr.sin_addr, hent->h_addr, 4);
1505 strcpy(hostname, hent->h_name);
1508 printf("blktrace: connecting to %s\n", hostname);
1510 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1511 perror("client: connect");
1515 printf("blktrace: connected!\n");
1520 static char usage_str[] = \
1521 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1522 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1523 "\t-d Use specified device. May also be given last after options\n" \
1524 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1525 "\t-o File(s) to send output to\n" \
1526 "\t-D Directory to prepend to output file names\n" \
1527 "\t-k Kill a running trace\n" \
1528 "\t-w Stop after defined time, in seconds\n" \
1529 "\t-a Only trace specified actions. See documentation\n" \
1530 "\t-A Give trace mask as a single value. See documentation\n" \
1531 "\t-b Sub buffer size in KiB\n" \
1532 "\t-n Number of sub buffers\n" \
1533 "\t-l Run in network listen mode (blktrace server)\n" \
1534 "\t-h Run in network client mode, connecting to the given host\n" \
1535 "\t-p Network port to use (default 8462)\n" \
1536 "\t-s Make the network client use sendfile() to transfer data\n" \
1537 "\t-V Print program version info\n\n";
1539 static void show_usage(char *program)
1541 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1544 int main(int argc, char *argv[])
1546 static char default_relay_path[] = "/relay";
1550 int act_mask_tmp = 0;
1552 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1555 i = find_mask_map(optarg);
1557 fprintf(stderr,"Invalid action mask %s\n",
1565 if ((sscanf(optarg, "%x", &i) != 1) ||
1566 !valid_act_opt(i)) {
1568 "Invalid set action mask %s/0x%x\n",
1576 if (resize_devices(optarg) != 0)
1581 relay_path = optarg;
1585 output_name = optarg;
1588 kill_running_trace = 1;
1591 stop_watch = atoi(optarg);
1592 if (stop_watch <= 0) {
1594 "Invalid stopwatch value (%d secs)\n",
1600 printf("%s version %s\n", argv[0], blktrace_version);
1603 buf_size = strtoul(optarg, NULL, 10);
1604 if (buf_size <= 0 || buf_size > 16*1024) {
1606 "Invalid buffer size (%lu)\n",buf_size);
1612 buf_nr = strtoul(optarg, NULL, 10);
1615 "Invalid buffer nr (%lu)\n", buf_nr);
1620 output_dir = optarg;
1623 net_mode = Net_client;
1624 strcpy(hostname, optarg);
1627 net_mode = Net_server;
1630 net_port = atoi(optarg);
1633 net_use_sendfile = 1;
1636 show_usage(argv[0]);
1641 setlocale(LC_NUMERIC, "en_US");
1643 page_size = getpagesize();
1645 if (net_mode == Net_server)
1646 return net_server();
1648 while (optind < argc) {
1649 if (resize_devices(argv[optind++]) != 0)
1654 show_usage(argv[0]);
1659 relay_path = default_relay_path;
1661 if (act_mask_tmp != 0)
1662 act_mask = act_mask_tmp;
1664 if (statfs(relay_path, &st) < 0) {
1666 fprintf(stderr,"%s does not appear to be a valid path\n",
1669 } else if (st.f_type != (long) RELAYFS_TYPE) {
1670 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1675 if (open_devices() != 0)
1678 if (kill_running_trace) {
1683 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1685 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1689 signal(SIGINT, handle_sigint);
1690 signal(SIGHUP, handle_sigint);
1691 signal(SIGTERM, handle_sigint);
1692 signal(SIGALRM, handle_sigint);
1694 if (net_mode == Net_client && net_setup_client())
1697 if (start_devices() != 0)
1700 atexit(stop_all_tracing);
1707 if (!is_trace_stopped()) {