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];
189 int (*get_subbuf)(struct thread_information *, unsigned int);
190 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
191 int (*read_data)(struct thread_information *, void *, unsigned int);
193 unsigned long events_processed;
194 unsigned long long data_read;
195 struct device_information *device;
202 struct tip_subbuf_fifo fifo;
203 struct tip_subbuf *leftover_ts;
206 * mmap controlled output files
208 unsigned long long fs_size;
209 unsigned long long fs_max_size;
210 unsigned long fs_off;
212 unsigned long fs_buf_len;
215 struct device_information {
219 volatile int trace_started;
220 unsigned long drop_count;
221 struct thread_information *threads;
225 static struct thread_information *thread_information;
227 static struct device_information *device_information;
229 /* command line option globals */
230 static char *relay_path;
231 static char *output_name;
232 static char *output_dir;
233 static int act_mask = ~0U;
234 static int kill_running_trace;
235 static unsigned long buf_size = BUF_SIZE;
236 static unsigned long buf_nr = BUF_NR;
237 static unsigned int page_size;
239 #define is_done() (*(volatile int *)(&done))
240 static volatile int done;
242 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
243 static volatile int trace_stopped;
245 #define is_stat_shown() (*(volatile int *)(&stat_shown))
246 static volatile int stat_shown;
248 int data_is_native = -1;
250 static void exit_trace(int status);
252 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
253 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
255 #define __for_each_dip(__d, __i, __e) \
256 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
258 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
259 #define for_each_tip(__d, __t, __j) \
260 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
263 * networking stuff follows. we include a magic number so we know whether
264 * to endianness convert or not
266 struct blktrace_net_hdr {
267 u32 magic; /* same as trace magic */
268 char buts_name[32]; /* trace name */
269 u32 cpu; /* for which cpu */
271 u32 len; /* length of following trace data */
274 #define TRACE_NET_PORT (8462)
283 * network cmd line params
285 static char hostname[MAXHOSTNAMELEN];
286 static int net_port = TRACE_NET_PORT;
287 static int net_mode = 0;
288 static int net_sendfile;
290 static int net_in_fd = -1;
291 static int net_out_fd = -1;
293 static void handle_sigint(__attribute__((__unused__)) int sig)
298 static int get_dropped_count(const char *buts_name)
301 char tmp[MAXPATHLEN + 64];
303 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
304 relay_path, buts_name);
306 fd = open(tmp, O_RDONLY);
309 * this may be ok, if the kernel doesn't support dropped counts
314 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
318 if (read(fd, tmp, sizeof(tmp)) < 0) {
329 static int start_trace(struct device_information *dip)
331 struct blk_user_trace_setup buts;
333 memset(&buts, 0, sizeof(buts));
334 buts.buf_size = buf_size;
335 buts.buf_nr = buf_nr;
336 buts.act_mask = act_mask;
338 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
339 perror("BLKTRACESETUP");
343 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
344 perror("BLKTRACESTART");
348 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
349 dip_set_tracing(dip, 1);
353 static void stop_trace(struct device_information *dip)
355 if (dip_tracing(dip) || kill_running_trace) {
356 dip_set_tracing(dip, 0);
358 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
359 perror("BLKTRACESTOP");
360 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
361 perror("BLKTRACETEARDOWN");
368 static void stop_all_traces(void)
370 struct device_information *dip;
373 for_each_dip(dip, i) {
374 dip->drop_count = get_dropped_count(dip->buts_name);
379 static void wait_for_data(struct thread_information *tip)
381 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
385 if (pfd.revents & POLLIN)
387 if (tip->ofile_stdout)
389 } while (!is_done());
392 static int read_data_file(struct thread_information *tip, void *buf,
400 ret = read(tip->fd, buf, len);
406 if (errno != EAGAIN) {
408 fprintf(stderr,"Thread %d failed read of %s\n",
414 } while (!is_done());
420 static int read_data_net(struct thread_information *tip, void *buf,
423 unsigned int bytes_left = len;
427 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
432 if (errno != EAGAIN) {
434 fprintf(stderr, "server: failed read\n");
442 } while (!is_done() && bytes_left);
444 return len - bytes_left;
447 static int read_data(struct thread_information *tip, void *buf,
450 int ret = tip->read_data(tip, buf, len);
453 tip->data_read += ret;
458 static inline struct tip_subbuf *
459 subbuf_fifo_dequeue(struct thread_information *tip)
461 const int head = tip->fifo.head;
462 const int next = (head + 1) & (FIFO_SIZE - 1);
464 if (head != tip->fifo.tail) {
465 struct tip_subbuf *ts = tip->fifo.q[head];
468 tip->fifo.head = next;
475 static inline int subbuf_fifo_queue(struct thread_information *tip,
476 struct tip_subbuf *ts)
478 const int tail = tip->fifo.tail;
479 const int next = (tail + 1) & (FIFO_SIZE - 1);
481 if (next != tip->fifo.head) {
482 tip->fifo.q[tail] = ts;
484 tip->fifo.tail = next;
488 fprintf(stderr, "fifo too small!\n");
493 * For file output, truncate and mmap the file appropriately
495 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
497 int ofd = fileno(tip->ofile);
501 * extend file, if we have to. use chunks of 16 subbuffers.
503 if (tip->fs_off + buf_size > tip->fs_buf_len) {
505 munlock(tip->fs_buf, tip->fs_buf_len);
506 munmap(tip->fs_buf, tip->fs_buf_len);
510 tip->fs_off = tip->fs_size & (page_size - 1);
511 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
512 tip->fs_max_size += tip->fs_buf_len;
514 if (ftruncate(ofd, tip->fs_max_size) < 0) {
519 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
520 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
521 if (tip->fs_buf == MAP_FAILED) {
525 mlock(tip->fs_buf, tip->fs_buf_len);
528 ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
538 static int get_subbuf_sendfile(struct thread_information *tip,
541 struct tip_subbuf *ts = malloc(sizeof(*ts));
544 ts->max_len = maxlen;
547 if (fstat(tip->fd, &sb) < 0) {
548 perror("trace stat");
552 ts->len = sb.st_size - tip->ofile_offset;
553 ts->max_len = ts->len;
554 ts->offset = tip->ofile_offset;
555 tip->ofile_offset += ts->len;
556 return subbuf_fifo_queue(tip, ts);
560 * Use the copy approach for pipes and network
562 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
564 struct tip_subbuf *ts = malloc(sizeof(*ts));
567 ts->buf = malloc(buf_size);
568 ts->max_len = maxlen;
570 ret = read_data(tip, ts->buf, ts->max_len);
573 return subbuf_fifo_queue(tip, ts);
579 static void close_thread(struct thread_information *tip)
585 if (tip->ofile_buffer)
586 free(tip->ofile_buffer);
592 tip->ofile_buffer = NULL;
596 static void tip_ftrunc_final(struct thread_information *tip)
599 * truncate to right size and cleanup mmap
601 if (tip->ofile_mmap) {
602 int ofd = fileno(tip->ofile);
605 munmap(tip->fs_buf, tip->fs_buf_len);
607 ftruncate(ofd, tip->fs_size);
611 static void *thread_main(void *arg)
613 struct thread_information *tip = arg;
614 pid_t pid = getpid();
618 CPU_SET((tip->cpu), &cpu_mask);
620 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
621 perror("sched_setaffinity");
625 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
626 relay_path, tip->device->buts_name, tip->cpu);
627 tip->fd = open(tip->fn, O_RDONLY);
630 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
636 if (tip->get_subbuf(tip, buf_size))
640 tip_ftrunc_final(tip);
645 static int write_data_net(int fd, void *buf, unsigned int buf_len)
647 unsigned int bytes_left = buf_len;
651 ret = send(fd, buf, bytes_left, 0);
664 static int net_send_header(struct thread_information *tip, unsigned int len)
666 struct blktrace_net_hdr hdr;
668 hdr.magic = BLK_IO_TRACE_MAGIC;
669 strcpy(hdr.buts_name, tip->device->buts_name);
671 hdr.max_cpus = ncpus;
674 return write_data_net(net_out_fd, &hdr, sizeof(hdr));
678 * send header with 0 length to signal end-of-run
680 static void net_client_send_close(void)
682 struct blktrace_net_hdr hdr;
684 hdr.magic = BLK_IO_TRACE_MAGIC;
686 hdr.max_cpus = ncpus;
689 write_data_net(net_out_fd, &hdr, sizeof(hdr));
692 static int flush_subbuf_net(struct thread_information *tip,
693 struct tip_subbuf *ts)
695 if (net_send_header(tip, ts->len))
697 if (write_data_net(net_out_fd, ts->buf, ts->len))
705 static int flush_subbuf_sendfile(struct thread_information *tip,
706 struct tip_subbuf *ts)
708 if (net_send_header(tip, ts->len))
710 if (sendfile(net_out_fd, tip->fd, &ts->offset, ts->len) < 0) {
719 static int write_data(struct thread_information *tip, void *buf,
720 unsigned int buf_len)
728 ret = fwrite(buf, buf_len, 1, tip->ofile);
738 if (tip->ofile_stdout)
744 static int flush_subbuf_file(struct thread_information *tip,
745 struct tip_subbuf *ts)
747 unsigned int offset = 0;
748 struct blk_io_trace *t;
749 int pdu_len, events = 0;
752 * surplus from last run
754 if (tip->leftover_ts) {
755 struct tip_subbuf *prev_ts = tip->leftover_ts;
757 if (prev_ts->len + ts->len > prev_ts->max_len) {
758 prev_ts->max_len += ts->len;
759 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
762 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
763 prev_ts->len += ts->len;
769 tip->leftover_ts = NULL;
772 while (offset + sizeof(*t) <= ts->len) {
773 t = ts->buf + offset;
775 if (verify_trace(t)) {
776 write_data(tip, ts->buf, offset);
780 pdu_len = t->pdu_len;
782 if (offset + sizeof(*t) + pdu_len > ts->len)
785 offset += sizeof(*t) + pdu_len;
786 tip->events_processed++;
787 tip->data_read += sizeof(*t) + pdu_len;
791 if (write_data(tip, ts->buf, offset))
795 * leftover bytes, save them for next time
797 if (offset != ts->len) {
798 tip->leftover_ts = ts;
800 memmove(ts->buf, ts->buf + offset, ts->len);
809 static int write_tip_events(struct thread_information *tip)
811 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
814 return tip->flush_subbuf(tip, ts);
820 * scans the tips we know and writes out the subbuffers we accumulate
822 static void get_and_write_events(void)
824 struct device_information *dip;
825 struct thread_information *tip;
826 int i, j, events, ret, tips_running;
831 for_each_dip(dip, i) {
832 for_each_tip(dip, tip, j) {
833 ret = write_tip_events(tip);
849 for_each_dip(dip, i) {
850 for_each_tip(dip, tip, j) {
851 ret = write_tip_events(tip);
854 tips_running += !tip->exited;
858 } while (events || tips_running);
861 static void wait_for_threads(void)
864 * for piped or network output, poll and fetch data for writeout.
865 * for files, we just wait around for trace threads to exit
867 if ((output_name && !strcmp(output_name, "-")) ||
868 net_mode == Net_client)
869 get_and_write_events();
871 struct device_information *dip;
872 struct thread_information *tip;
873 int i, j, tips_running;
880 for_each_tip(dip, tip, j)
881 tips_running += !tip->exited;
882 } while (tips_running);
885 if (net_mode == Net_client)
886 net_client_send_close();
889 static void fill_ofname(char *dst, char *buts_name, int cpu)
894 len = sprintf(dst, "%s/", output_dir);
897 sprintf(dst + len, "%s.blktrace.%d", output_name, cpu);
899 sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu);
902 static void fill_ops(struct thread_information *tip)
907 if (net_mode == Net_client) {
909 tip->get_subbuf = get_subbuf_sendfile;
910 tip->flush_subbuf = flush_subbuf_sendfile;
912 tip->get_subbuf = get_subbuf;
913 tip->flush_subbuf = flush_subbuf_net;
917 tip->get_subbuf = mmap_subbuf;
919 tip->get_subbuf = get_subbuf;
921 tip->flush_subbuf = flush_subbuf_file;
924 if (net_mode == Net_server)
925 tip->read_data = read_data_net;
927 tip->read_data = read_data_file;
930 static int tip_open_output(struct device_information *dip,
931 struct thread_information *tip)
933 int pipeline = output_name && !strcmp(output_name, "-");
937 if (net_mode == Net_client) {
939 tip->ofile_stdout = 0;
942 } else if (pipeline) {
943 tip->ofile = fdopen(STDOUT_FILENO, "w");
944 tip->ofile_stdout = 1;
949 fill_ofname(op, dip->buts_name, tip->cpu);
950 tip->ofile = fopen(op, "w+");
951 tip->ofile_stdout = 0;
954 vbuf_size = OFILE_BUF;
957 if (net_mode != Net_client && tip->ofile == NULL) {
963 tip->ofile_buffer = malloc(vbuf_size);
964 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
975 static int start_threads(struct device_information *dip)
977 struct thread_information *tip;
980 for_each_tip(dip, tip, j) {
983 tip->events_processed = 0;
984 memset(&tip->fifo, 0, sizeof(tip->fifo));
985 tip->leftover_ts = NULL;
987 if (tip_open_output(dip, tip))
990 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
991 perror("pthread_create");
1000 static void stop_threads(struct device_information *dip)
1002 struct thread_information *tip;
1006 for_each_tip(dip, tip, i) {
1007 (void) pthread_join(tip->thread, (void *) &ret);
1012 static void stop_all_threads(void)
1014 struct device_information *dip;
1017 for_each_dip(dip, i)
1021 static void stop_all_tracing(void)
1023 struct device_information *dip;
1026 for_each_dip(dip, i)
1030 static void exit_trace(int status)
1032 if (!is_trace_stopped()) {
1041 static int resize_devices(char *path)
1043 int size = (ndevs + 1) * sizeof(struct device_information);
1045 device_information = realloc(device_information, size);
1046 if (!device_information) {
1047 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1050 device_information[ndevs].path = path;
1055 static int open_devices(void)
1057 struct device_information *dip;
1060 for_each_dip(dip, i) {
1061 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1071 static int start_devices(void)
1073 struct device_information *dip;
1076 size = ncpus * sizeof(struct thread_information);
1077 thread_information = malloc(size * ndevs);
1078 if (!thread_information) {
1079 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1083 for_each_dip(dip, i) {
1084 if (start_trace(dip)) {
1086 fprintf(stderr, "Failed to start trace on %s\n",
1093 __for_each_dip(dip, j, i)
1099 for_each_dip(dip, i) {
1100 dip->threads = thread_information + (i * ncpus);
1101 if (start_threads(dip)) {
1102 fprintf(stderr, "Failed to start worker threads\n");
1108 __for_each_dip(dip, j, i)
1110 for_each_dip(dip, i)
1119 static void show_stats(void)
1121 struct device_information *dip;
1122 struct thread_information *tip;
1123 unsigned long long events_processed, data_read;
1124 unsigned long total_drops;
1125 int i, j, no_stdout = 0;
1127 if (is_stat_shown())
1130 if (output_name && !strcmp(output_name, "-"))
1136 for_each_dip(dip, i) {
1138 printf("Device: %s\n", dip->path);
1139 events_processed = 0;
1141 for_each_tip(dip, tip, j) {
1143 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1144 tip->cpu, tip->events_processed,
1145 (tip->data_read + 1023) >> 10);
1146 events_processed += tip->events_processed;
1147 data_read += tip->data_read;
1149 total_drops += dip->drop_count;
1151 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1152 events_processed, dip->drop_count,
1153 (data_read + 1023) >> 10);
1157 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1160 static struct device_information *net_get_dip(char *buts_name)
1162 struct device_information *dip;
1165 for (i = 0; i < ndevs; i++) {
1166 dip = &device_information[i];
1168 if (!strcmp(dip->buts_name, buts_name))
1172 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1173 dip = &device_information[ndevs];
1174 strcpy(dip->buts_name, buts_name);
1175 strcpy(dip->path, buts_name);
1177 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1178 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1183 for (i = 0; i < ncpus; i++) {
1184 struct thread_information *tip = &dip->threads[i];
1189 if (tip_open_output(dip, tip))
1196 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh)
1198 struct device_information *dip;
1200 ncpus = bnh->max_cpus;
1201 dip = net_get_dip(bnh->buts_name);
1202 return &dip->threads[bnh->cpu];
1205 static int net_get_header(struct blktrace_net_hdr *bnh)
1207 int fl = fcntl(net_in_fd, F_GETFL);
1208 int bytes_left, ret;
1211 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1212 bytes_left = sizeof(*bnh);
1213 while (bytes_left && !is_done()) {
1214 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1216 if (errno != EAGAIN) {
1217 perror("recv header");
1230 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1234 static int net_server_loop(void)
1236 struct thread_information *tip;
1237 struct blktrace_net_hdr bnh;
1239 if (net_get_header(&bnh))
1242 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1243 fprintf(stderr, "server: received data is bad\n");
1247 if (!data_is_native) {
1248 bnh.cpu = be32_to_cpu(bnh.cpu);
1249 bnh.len = be32_to_cpu(bnh.len);
1253 * len == 0 means that the other end signalled end-of-run
1256 fprintf(stderr, "server: end of run\n");
1260 tip = net_get_tip(&bnh);
1264 if (mmap_subbuf(tip, bnh.len))
1271 * Start here when we are in server mode - just fetch data from the network
1274 static int net_server(void)
1276 struct device_information *dip;
1277 struct thread_information *tip;
1278 struct sockaddr_in addr;
1282 fd = socket(AF_INET, SOCK_STREAM, 0);
1284 perror("server: socket");
1289 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1290 perror("setsockopt");
1294 memset(&addr, 0, sizeof(addr));
1295 addr.sin_family = AF_INET;
1296 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1297 addr.sin_port = htons(net_port);
1299 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1304 if (listen(fd, 1) < 0) {
1310 signal(SIGINT, NULL);
1311 signal(SIGHUP, NULL);
1312 signal(SIGTERM, NULL);
1313 signal(SIGALRM, NULL);
1315 printf("blktrace: waiting for incoming connection...\n");
1317 socklen = sizeof(addr);
1318 net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen);
1319 if (net_in_fd < 0) {
1324 signal(SIGINT, handle_sigint);
1325 signal(SIGHUP, handle_sigint);
1326 signal(SIGTERM, handle_sigint);
1327 signal(SIGALRM, handle_sigint);
1329 printf("blktrace: connection from %s\n", inet_ntoa(addr.sin_addr));
1331 while (!is_done()) {
1332 if (net_server_loop())
1336 for_each_dip(dip, i)
1337 for_each_tip(dip, tip, j)
1338 tip_ftrunc_final(tip);
1346 * cleanup for next run
1348 for_each_dip(dip, i) {
1349 for_each_tip(dip, tip, j)
1355 free(device_information);
1356 device_information = NULL;
1365 * Setup outgoing network connection where we will transmit data
1367 static int net_setup_client(void)
1369 struct sockaddr_in addr;
1372 fd = socket(AF_INET, SOCK_STREAM, 0);
1374 perror("client: socket");
1378 memset(&addr, 0, sizeof(addr));
1379 addr.sin_family = AF_INET;
1380 addr.sin_port = htons(net_port);
1382 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1383 struct hostent *hent = gethostbyname(hostname);
1385 perror("gethostbyname");
1389 memcpy(&addr.sin_addr, hent->h_addr, 4);
1390 strcpy(hostname, hent->h_name);
1393 printf("blktrace: connecting to %s\n", hostname);
1395 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1396 perror("client: connect");
1400 printf("blktrace: connected!\n");
1405 static char usage_str[] = \
1406 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1407 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1408 "\t-d Use specified device. May also be given last after options\n" \
1409 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1410 "\t-o File(s) to send output to\n" \
1411 "\t-D Directory to prepend to output file names\n" \
1412 "\t-k Kill a running trace\n" \
1413 "\t-w Stop after defined time, in seconds\n" \
1414 "\t-a Only trace specified actions. See documentation\n" \
1415 "\t-A Give trace mask as a single value. See documentation\n" \
1416 "\t-b Sub buffer size in KiB\n" \
1417 "\t-n Number of sub buffers\n" \
1418 "\t-l Run in network listen mode (blktrace server)\n" \
1419 "\t-h Run in network client mode, connecting to the given host\n" \
1420 "\t-p Network port to use (default 8462)\n" \
1421 "\t-s Make the network client use sendfile() to transfer data\n" \
1422 "\t-V Print program version info\n\n";
1424 static void show_usage(char *program)
1426 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1429 int main(int argc, char *argv[])
1431 static char default_relay_path[] = "/relay";
1435 int act_mask_tmp = 0;
1437 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1440 i = find_mask_map(optarg);
1442 fprintf(stderr,"Invalid action mask %s\n",
1450 if ((sscanf(optarg, "%x", &i) != 1) ||
1451 !valid_act_opt(i)) {
1453 "Invalid set action mask %s/0x%x\n",
1461 if (resize_devices(optarg) != 0)
1466 relay_path = optarg;
1470 output_name = optarg;
1473 kill_running_trace = 1;
1476 stop_watch = atoi(optarg);
1477 if (stop_watch <= 0) {
1479 "Invalid stopwatch value (%d secs)\n",
1485 printf("%s version %s\n", argv[0], blktrace_version);
1488 buf_size = strtoul(optarg, NULL, 10);
1489 if (buf_size <= 0 || buf_size > 16*1024) {
1491 "Invalid buffer size (%lu)\n",buf_size);
1497 buf_nr = strtoul(optarg, NULL, 10);
1500 "Invalid buffer nr (%lu)\n", buf_nr);
1505 output_dir = optarg;
1508 net_mode = Net_client;
1509 strcpy(hostname, optarg);
1512 net_mode = Net_server;
1515 net_port = atoi(optarg);
1521 show_usage(argv[0]);
1526 setlocale(LC_NUMERIC, "en_US");
1528 page_size = getpagesize();
1530 if (net_mode == Net_server)
1531 return net_server();
1533 while (optind < argc) {
1534 if (resize_devices(argv[optind++]) != 0)
1539 show_usage(argv[0]);
1544 relay_path = default_relay_path;
1546 if (act_mask_tmp != 0)
1547 act_mask = act_mask_tmp;
1549 if (statfs(relay_path, &st) < 0) {
1551 fprintf(stderr,"%s does not appear to be a valid path\n",
1554 } else if (st.f_type != (long) RELAYFS_TYPE) {
1555 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1560 if (open_devices() != 0)
1563 if (kill_running_trace) {
1568 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1570 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1574 signal(SIGINT, handle_sigint);
1575 signal(SIGHUP, handle_sigint);
1576 signal(SIGTERM, handle_sigint);
1577 signal(SIGALRM, handle_sigint);
1579 if (net_mode == Net_client && net_setup_client())
1582 if (start_devices() != 0)
1585 atexit(stop_all_tracing);
1592 if (!is_trace_stopped()) {