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;
166 #define FIFO_SIZE (1024) /* should be plenty big! */
167 #define CL_SIZE (128) /* cache line, any bigger? */
169 struct tip_subbuf_fifo {
170 int tail __attribute__((aligned(CL_SIZE)));
171 int head __attribute__((aligned(CL_SIZE)));
172 struct tip_subbuf *q[FIFO_SIZE];
175 struct thread_information {
181 char fn[MAXPATHLEN + 64];
186 struct in_addr cl_in_addr;
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);
198 unsigned long events_processed;
199 unsigned long long data_read;
200 struct device_information *device;
207 struct tip_subbuf_fifo fifo;
208 struct tip_subbuf *leftover_ts;
211 * mmap controlled output files
213 unsigned long long fs_size;
214 unsigned long long fs_max_size;
215 unsigned long fs_off;
217 unsigned long fs_buf_len;
220 struct device_information {
224 volatile int trace_started;
225 unsigned long drop_count;
226 struct thread_information *threads;
230 static struct thread_information *thread_information;
232 static struct device_information *device_information;
234 /* command line option globals */
235 static char *relay_path;
236 static char *output_name;
237 static char *output_dir;
238 static int act_mask = ~0U;
239 static int kill_running_trace;
240 static unsigned long buf_size = BUF_SIZE;
241 static unsigned long buf_nr = BUF_NR;
242 static unsigned int page_size;
244 #define is_done() (*(volatile int *)(&done))
245 static volatile int done;
247 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
248 static volatile int trace_stopped;
250 #define is_stat_shown() (*(volatile int *)(&stat_shown))
251 static volatile int stat_shown;
253 int data_is_native = -1;
255 static void exit_trace(int status);
257 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
258 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
260 #define __for_each_dip(__d, __i, __e) \
261 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
263 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
264 #define for_each_tip(__d, __t, __j) \
265 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
268 * networking stuff follows. we include a magic number so we know whether
269 * to endianness convert or not
271 struct blktrace_net_hdr {
272 u32 magic; /* same as trace magic */
273 char buts_name[32]; /* trace name */
274 u32 cpu; /* for which cpu */
276 u32 len; /* length of following trace data */
279 #define TRACE_NET_PORT (8462)
288 * network cmd line params
290 static char hostname[MAXHOSTNAMELEN];
291 static int net_port = TRACE_NET_PORT;
292 static int net_mode = 0;
293 static int net_sendfile;
295 static int net_in_fd = -1;
296 static int net_out_fd = -1;
298 static void handle_sigint(__attribute__((__unused__)) int sig)
300 struct device_information *dip;
304 * stop trace so we can reap currently produced data
306 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 size_t get_subbuf_padding(struct thread_information *tip,
348 size_t padding_size = buf_nr * sizeof(size_t);
351 if (read(tip->pfd, tip->pfd_buf, padding_size) < 0) {
352 perror("tip pad read");
355 ret = tip->pfd_buf[subbuf];
360 static int start_trace(struct device_information *dip)
362 struct blk_user_trace_setup buts;
364 memset(&buts, 0, sizeof(buts));
365 buts.buf_size = buf_size;
366 buts.buf_nr = buf_nr;
367 buts.act_mask = act_mask;
369 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
370 perror("BLKTRACESETUP");
374 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
375 perror("BLKTRACESTART");
379 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
380 dip_set_tracing(dip, 1);
384 static void stop_trace(struct device_information *dip)
386 if (dip_tracing(dip) || kill_running_trace) {
387 dip_set_tracing(dip, 0);
390 * should be stopped, just don't complain if it isn't
392 ioctl(dip->fd, BLKTRACESTOP);
394 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
395 perror("BLKTRACETEARDOWN");
402 static void stop_all_traces(void)
404 struct device_information *dip;
407 for_each_dip(dip, i) {
408 dip->drop_count = get_dropped_count(dip->buts_name);
413 static void wait_for_data(struct thread_information *tip)
415 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
419 if (pfd.revents & POLLIN)
421 if (tip->ofile_stdout)
423 } while (!is_done());
426 static int read_data_file(struct thread_information *tip, void *buf,
434 ret = read(tip->fd, buf, len);
440 if (errno != EAGAIN) {
442 fprintf(stderr,"Thread %d failed read of %s\n",
448 } while (!is_done());
454 static int read_data_net(struct thread_information *tip, void *buf,
457 unsigned int bytes_left = len;
461 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
466 if (errno != EAGAIN) {
468 fprintf(stderr, "server: failed read\n");
476 } while (!is_done() && bytes_left);
478 return len - bytes_left;
481 static int read_data(struct thread_information *tip, void *buf,
484 return tip->read_data(tip, buf, len);
487 static inline struct tip_subbuf *
488 subbuf_fifo_dequeue(struct thread_information *tip)
490 const int head = tip->fifo.head;
491 const int next = (head + 1) & (FIFO_SIZE - 1);
493 if (head != tip->fifo.tail) {
494 struct tip_subbuf *ts = tip->fifo.q[head];
497 tip->fifo.head = next;
504 static inline int subbuf_fifo_queue(struct thread_information *tip,
505 struct tip_subbuf *ts)
507 const int tail = tip->fifo.tail;
508 const int next = (tail + 1) & (FIFO_SIZE - 1);
510 if (next != tip->fifo.head) {
511 tip->fifo.q[tail] = ts;
513 tip->fifo.tail = next;
517 fprintf(stderr, "fifo too small!\n");
522 * For file output, truncate and mmap the file appropriately
524 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
526 int ofd = fileno(tip->ofile);
530 * extend file, if we have to. use chunks of 16 subbuffers.
532 if (tip->fs_off + buf_size > tip->fs_buf_len) {
534 munlock(tip->fs_buf, tip->fs_buf_len);
535 munmap(tip->fs_buf, tip->fs_buf_len);
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;
543 if (ftruncate(ofd, tip->fs_max_size) < 0) {
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) {
554 mlock(tip->fs_buf, tip->fs_buf_len);
557 ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
559 tip->data_read += ret;
569 * Use the copy approach for pipes and network
571 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
573 struct tip_subbuf *ts = malloc(sizeof(*ts));
576 ts->buf = malloc(buf_size);
577 ts->max_len = maxlen;
579 ret = read_data(tip, ts->buf, ts->max_len);
582 tip->data_read += ret;
583 if (subbuf_fifo_queue(tip, ts))
590 static int get_subbuf_sendfile(struct thread_information *tip,
593 struct tip_subbuf *ts;
595 unsigned int ready, this_size, total;
600 * hack to get last data out, we can't use sendfile for that
603 return get_subbuf(tip, maxlen);
605 if (fstat(tip->fd, &sb) < 0) {
606 perror("trace stat");
610 ready = sb.st_size - tip->ofile_offset;
613 * delay a little, since poll() will return data available
614 * until sendfile() is run
620 this_size = buf_size;
623 if (this_size > ready)
626 ts = malloc(sizeof(*ts));
632 ts->offset = tip->ofile_offset;
633 tip->ofile_offset += ts->len;
635 if (subbuf_fifo_queue(tip, ts))
644 static void close_thread(struct thread_information *tip)
652 if (tip->ofile_buffer)
653 free(tip->ofile_buffer);
662 tip->ofile_buffer = NULL;
666 static void tip_ftrunc_final(struct thread_information *tip)
669 * truncate to right size and cleanup mmap
671 if (tip->ofile_mmap) {
672 int ofd = fileno(tip->ofile);
675 munmap(tip->fs_buf, tip->fs_buf_len);
677 ftruncate(ofd, tip->fs_size);
681 static void *thread_main(void *arg)
683 struct thread_information *tip = arg;
684 pid_t pid = getpid();
688 CPU_SET((tip->cpu), &cpu_mask);
690 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
691 perror("sched_setaffinity");
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);
700 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
705 if (net_mode == Net_client && net_sendfile) {
706 char tmp[MAXPATHLEN + 64];
708 snprintf(tmp, sizeof(tmp), "%s/block/%s/trace%d.padding",
709 relay_path, tip->device->buts_name, tip->cpu);
711 tip->pfd = open(tmp, O_RDONLY);
713 fprintf(stderr, "Couldn't open padding file %s\n", tmp);
717 tip->pfd_buf = malloc(buf_nr * sizeof(size_t));
721 if (tip->get_subbuf(tip, buf_size) < 0)
726 * trace is stopped, pull data until we get a short read
728 while (tip->get_subbuf(tip, buf_size) > 0)
731 tip_ftrunc_final(tip);
736 static int write_data_net(int fd, void *buf, unsigned int buf_len)
738 unsigned int bytes_left = buf_len;
742 ret = send(fd, buf, bytes_left, 0);
755 static int net_send_header(struct thread_information *tip, unsigned int len)
757 struct blktrace_net_hdr hdr;
759 hdr.magic = BLK_IO_TRACE_MAGIC;
760 strcpy(hdr.buts_name, tip->device->buts_name);
762 hdr.max_cpus = ncpus;
765 return write_data_net(net_out_fd, &hdr, sizeof(hdr));
769 * send header with 0 length to signal end-of-run
771 static void net_client_send_close(void)
773 struct blktrace_net_hdr hdr;
775 hdr.magic = BLK_IO_TRACE_MAGIC;
777 hdr.max_cpus = ncpus;
780 write_data_net(net_out_fd, &hdr, sizeof(hdr));
783 static int flush_subbuf_net(struct thread_information *tip,
784 struct tip_subbuf *ts)
786 if (net_send_header(tip, ts->len))
788 if (write_data_net(net_out_fd, ts->buf, ts->len))
796 static int flush_subbuf_sendfile(struct thread_information *tip,
797 struct tip_subbuf *ts)
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
809 return flush_subbuf_net(tip, ts);
811 subbuf = (ts->offset / buf_size) % buf_nr;
812 padding = get_subbuf_padding(tip, subbuf);
813 len = ts->len - padding;
815 if (net_send_header(tip, len))
817 if (sendfile(net_out_fd, tip->fd, &ts->offset, len) < 0) {
822 tip->data_read += len;
827 static int write_data(struct thread_information *tip, void *buf,
828 unsigned int buf_len)
836 ret = fwrite(buf, buf_len, 1, tip->ofile);
846 if (tip->ofile_stdout)
852 static int flush_subbuf_file(struct thread_information *tip,
853 struct tip_subbuf *ts)
855 unsigned int offset = 0;
856 struct blk_io_trace *t;
857 int pdu_len, events = 0;
860 * surplus from last run
862 if (tip->leftover_ts) {
863 struct tip_subbuf *prev_ts = tip->leftover_ts;
865 if (prev_ts->len + ts->len > prev_ts->max_len) {
866 prev_ts->max_len += ts->len;
867 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
870 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
871 prev_ts->len += ts->len;
877 tip->leftover_ts = NULL;
880 while (offset + sizeof(*t) <= ts->len) {
881 t = ts->buf + offset;
883 if (verify_trace(t)) {
884 write_data(tip, ts->buf, offset);
888 pdu_len = t->pdu_len;
890 if (offset + sizeof(*t) + pdu_len > ts->len)
893 offset += sizeof(*t) + pdu_len;
894 tip->events_processed++;
895 tip->data_read += sizeof(*t) + pdu_len;
899 if (write_data(tip, ts->buf, offset))
903 * leftover bytes, save them for next time
905 if (offset != ts->len) {
906 tip->leftover_ts = ts;
908 memmove(ts->buf, ts->buf + offset, ts->len);
917 static int write_tip_events(struct thread_information *tip)
919 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
922 return tip->flush_subbuf(tip, ts);
928 * scans the tips we know and writes out the subbuffers we accumulate
930 static void get_and_write_events(void)
932 struct device_information *dip;
933 struct thread_information *tip;
934 int i, j, events, ret, tips_running;
939 for_each_dip(dip, i) {
940 for_each_tip(dip, tip, j) {
941 ret = write_tip_events(tip);
957 for_each_dip(dip, i) {
958 for_each_tip(dip, tip, j) {
959 ret = write_tip_events(tip);
962 tips_running += !tip->exited;
966 } while (events || tips_running);
969 static void wait_for_threads(void)
972 * for piped or network output, poll and fetch data for writeout.
973 * for files, we just wait around for trace threads to exit
975 if ((output_name && !strcmp(output_name, "-")) ||
976 net_mode == Net_client)
977 get_and_write_events();
979 struct device_information *dip;
980 struct thread_information *tip;
981 int i, j, tips_running;
988 for_each_tip(dip, tip, j)
989 tips_running += !tip->exited;
990 } while (tips_running);
993 if (net_mode == Net_client)
994 net_client_send_close();
997 static int fill_ofname(struct thread_information *tip, char *dst,
1005 len = sprintf(dst, "%s/", output_dir);
1007 if (net_mode == Net_server) {
1008 len += sprintf(dst + len, "%s-", inet_ntoa(tip->cl_in_addr));
1010 len += strftime(dst + len, 64, "%F-%T/", gmtime(&t));
1013 if (stat(dst, &sb) < 0) {
1014 if (errno != ENOENT) {
1018 if (mkdir(dst, 0755) < 0) {
1020 fprintf(stderr, "Can't make output dir\n");
1026 sprintf(dst + len, "%s.blktrace.%d", output_name, tip->cpu);
1028 sprintf(dst + len, "%s.blktrace.%d", buts_name, tip->cpu);
1033 static void fill_ops(struct thread_information *tip)
1038 if (net_mode == Net_client) {
1040 tip->get_subbuf = get_subbuf_sendfile;
1041 tip->flush_subbuf = flush_subbuf_sendfile;
1043 tip->get_subbuf = get_subbuf;
1044 tip->flush_subbuf = flush_subbuf_net;
1047 if (tip->ofile_mmap)
1048 tip->get_subbuf = mmap_subbuf;
1050 tip->get_subbuf = get_subbuf;
1052 tip->flush_subbuf = flush_subbuf_file;
1055 if (net_mode == Net_server)
1056 tip->read_data = read_data_net;
1058 tip->read_data = read_data_file;
1061 static int tip_open_output(struct device_information *dip,
1062 struct thread_information *tip)
1064 int pipeline = output_name && !strcmp(output_name, "-");
1065 int mode, vbuf_size;
1068 if (net_mode == Net_client) {
1070 tip->ofile_stdout = 0;
1071 tip->ofile_mmap = 0;
1073 mode = 0; /* gcc 4.x issues a bogus warning */
1074 } else if (pipeline) {
1075 tip->ofile = fdopen(STDOUT_FILENO, "w");
1076 tip->ofile_stdout = 1;
1077 tip->ofile_mmap = 0;
1081 if (fill_ofname(tip, op, dip->buts_name))
1083 tip->ofile = fopen(op, "w+");
1084 tip->ofile_stdout = 0;
1085 tip->ofile_mmap = 1;
1087 vbuf_size = OFILE_BUF;
1090 if (net_mode != Net_client && tip->ofile == NULL) {
1096 tip->ofile_buffer = malloc(vbuf_size);
1097 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
1108 static int start_threads(struct device_information *dip)
1110 struct thread_information *tip;
1113 for_each_tip(dip, tip, j) {
1116 tip->events_processed = 0;
1119 memset(&tip->fifo, 0, sizeof(tip->fifo));
1120 tip->leftover_ts = NULL;
1122 if (tip_open_output(dip, tip))
1125 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
1126 perror("pthread_create");
1135 static void stop_threads(struct device_information *dip)
1137 struct thread_information *tip;
1141 for_each_tip(dip, tip, i) {
1142 (void) pthread_join(tip->thread, (void *) &ret);
1147 static void stop_all_threads(void)
1149 struct device_information *dip;
1152 for_each_dip(dip, i)
1156 static void stop_all_tracing(void)
1158 struct device_information *dip;
1161 for_each_dip(dip, i)
1165 static void exit_trace(int status)
1167 if (!is_trace_stopped()) {
1176 static int resize_devices(char *path)
1178 int size = (ndevs + 1) * sizeof(struct device_information);
1180 device_information = realloc(device_information, size);
1181 if (!device_information) {
1182 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1185 device_information[ndevs].path = path;
1190 static int open_devices(void)
1192 struct device_information *dip;
1195 for_each_dip(dip, i) {
1196 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1206 static int start_devices(void)
1208 struct device_information *dip;
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);
1218 for_each_dip(dip, i) {
1219 if (start_trace(dip)) {
1221 fprintf(stderr, "Failed to start trace on %s\n",
1228 __for_each_dip(dip, j, i)
1234 for_each_dip(dip, i) {
1235 dip->threads = thread_information + (i * ncpus);
1236 if (start_threads(dip)) {
1237 fprintf(stderr, "Failed to start worker threads\n");
1243 __for_each_dip(dip, j, i)
1245 for_each_dip(dip, i)
1254 static void show_stats(void)
1256 struct device_information *dip;
1257 struct thread_information *tip;
1258 unsigned long long events_processed, data_read;
1259 unsigned long total_drops;
1260 int i, j, no_stdout = 0;
1262 if (is_stat_shown())
1265 if (output_name && !strcmp(output_name, "-"))
1271 for_each_dip(dip, i) {
1273 printf("Device: %s\n", dip->path);
1274 events_processed = 0;
1276 for_each_tip(dip, tip, j) {
1278 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1279 tip->cpu, tip->events_processed,
1280 (tip->data_read + 1023) >> 10);
1281 events_processed += tip->events_processed;
1282 data_read += tip->data_read;
1284 total_drops += dip->drop_count;
1286 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1287 events_processed, dip->drop_count,
1288 (data_read + 1023) >> 10);
1292 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1295 static struct device_information *net_get_dip(char *buts_name,
1296 struct in_addr *cl_in_addr)
1298 struct device_information *dip;
1301 for (i = 0; i < ndevs; i++) {
1302 dip = &device_information[i];
1304 if (!strcmp(dip->buts_name, buts_name))
1308 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1309 dip = &device_information[ndevs];
1310 strcpy(dip->buts_name, buts_name);
1311 strcpy(dip->path, buts_name);
1313 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1314 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1319 for (i = 0; i < ncpus; i++) {
1320 struct thread_information *tip = &dip->threads[i];
1326 tip->cl_in_addr = *cl_in_addr;
1328 if (tip_open_output(dip, tip))
1335 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh,
1336 struct in_addr *cl_in_addr)
1338 struct device_information *dip;
1340 ncpus = bnh->max_cpus;
1341 dip = net_get_dip(bnh->buts_name, cl_in_addr);
1342 return &dip->threads[bnh->cpu];
1345 static int net_get_header(struct blktrace_net_hdr *bnh)
1347 int fl = fcntl(net_in_fd, F_GETFL);
1348 int bytes_left, ret;
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);
1356 if (errno != EAGAIN) {
1357 perror("recv header");
1370 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1374 static int net_server_loop(struct in_addr *cl_in_addr)
1376 struct thread_information *tip;
1377 struct blktrace_net_hdr bnh;
1379 if (net_get_header(&bnh))
1382 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1383 fprintf(stderr, "server: received data is bad\n");
1387 if (!data_is_native) {
1388 bnh.cpu = be32_to_cpu(bnh.cpu);
1389 bnh.len = be32_to_cpu(bnh.len);
1393 * len == 0 means that the other end signalled end-of-run
1396 fprintf(stderr, "server: end of run\n");
1400 tip = net_get_tip(&bnh, cl_in_addr);
1404 if (mmap_subbuf(tip, bnh.len))
1411 * Start here when we are in server mode - just fetch data from the network
1414 static int net_server(void)
1416 struct device_information *dip;
1417 struct thread_information *tip;
1418 struct sockaddr_in addr;
1422 fd = socket(AF_INET, SOCK_STREAM, 0);
1424 perror("server: socket");
1429 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1430 perror("setsockopt");
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);
1439 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1444 if (listen(fd, 1) < 0) {
1450 signal(SIGINT, NULL);
1451 signal(SIGHUP, NULL);
1452 signal(SIGTERM, NULL);
1453 signal(SIGALRM, NULL);
1455 printf("blktrace: waiting for incoming connection...\n");
1457 socklen = sizeof(addr);
1458 net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen);
1459 if (net_in_fd < 0) {
1464 signal(SIGINT, handle_sigint);
1465 signal(SIGHUP, handle_sigint);
1466 signal(SIGTERM, handle_sigint);
1467 signal(SIGALRM, handle_sigint);
1469 printf("blktrace: connection from %s\n", inet_ntoa(addr.sin_addr));
1471 while (!is_done()) {
1472 if (net_server_loop(&addr.sin_addr))
1476 for_each_dip(dip, i)
1477 for_each_tip(dip, tip, j)
1478 tip_ftrunc_final(tip);
1486 * cleanup for next run
1488 for_each_dip(dip, i) {
1489 for_each_tip(dip, tip, j)
1495 free(device_information);
1496 device_information = NULL;
1506 * Setup outgoing network connection where we will transmit data
1508 static int net_setup_client(void)
1510 struct sockaddr_in addr;
1513 fd = socket(AF_INET, SOCK_STREAM, 0);
1515 perror("client: socket");
1519 memset(&addr, 0, sizeof(addr));
1520 addr.sin_family = AF_INET;
1521 addr.sin_port = htons(net_port);
1523 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1524 struct hostent *hent = gethostbyname(hostname);
1526 perror("gethostbyname");
1530 memcpy(&addr.sin_addr, hent->h_addr, 4);
1531 strcpy(hostname, hent->h_name);
1534 printf("blktrace: connecting to %s\n", hostname);
1536 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1537 perror("client: connect");
1541 printf("blktrace: connected!\n");
1546 static 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" \
1552 "\t-D Directory to prepend to output file names\n" \
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" \
1557 "\t-b Sub buffer size in KiB\n" \
1558 "\t-n Number of sub buffers\n" \
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";
1565 static void show_usage(char *program)
1567 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1570 int main(int argc, char *argv[])
1572 static char default_relay_path[] = "/relay";
1576 int act_mask_tmp = 0;
1578 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1581 i = find_mask_map(optarg);
1583 fprintf(stderr,"Invalid action mask %s\n",
1591 if ((sscanf(optarg, "%x", &i) != 1) ||
1592 !valid_act_opt(i)) {
1594 "Invalid set action mask %s/0x%x\n",
1602 if (resize_devices(optarg) != 0)
1607 relay_path = optarg;
1611 output_name = optarg;
1614 kill_running_trace = 1;
1617 stop_watch = atoi(optarg);
1618 if (stop_watch <= 0) {
1620 "Invalid stopwatch value (%d secs)\n",
1626 printf("%s version %s\n", argv[0], blktrace_version);
1629 buf_size = strtoul(optarg, NULL, 10);
1630 if (buf_size <= 0 || buf_size > 16*1024) {
1632 "Invalid buffer size (%lu)\n",buf_size);
1638 buf_nr = strtoul(optarg, NULL, 10);
1641 "Invalid buffer nr (%lu)\n", buf_nr);
1646 output_dir = optarg;
1649 net_mode = Net_client;
1650 strcpy(hostname, optarg);
1653 net_mode = Net_server;
1656 net_port = atoi(optarg);
1662 show_usage(argv[0]);
1667 setlocale(LC_NUMERIC, "en_US");
1669 page_size = getpagesize();
1671 if (net_mode == Net_server)
1672 return net_server();
1674 while (optind < argc) {
1675 if (resize_devices(argv[optind++]) != 0)
1680 show_usage(argv[0]);
1685 relay_path = default_relay_path;
1687 if (act_mask_tmp != 0)
1688 act_mask = act_mask_tmp;
1690 if (statfs(relay_path, &st) < 0) {
1692 fprintf(stderr,"%s does not appear to be a valid path\n",
1695 } else if (st.f_type != (long) RELAYFS_TYPE) {
1696 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1701 if (open_devices() != 0)
1704 if (kill_running_trace) {
1709 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1711 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1715 signal(SIGINT, handle_sigint);
1716 signal(SIGHUP, handle_sigint);
1717 signal(SIGTERM, handle_sigint);
1718 signal(SIGALRM, handle_sigint);
1720 if (net_mode == Net_client && net_setup_client())
1723 if (start_devices() != 0)
1726 atexit(stop_all_tracing);
1733 if (!is_trace_stopped()) {