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->buf = malloc(buf_size);
545 ts->max_len = maxlen;
548 if (fstat(tip->fd, &sb) < 0) {
549 perror("trace stat");
553 ts->len = sb.st_size - tip->ofile_offset;
554 ts->max_len = ts->len;
555 ts->offset = tip->ofile_offset;
556 tip->ofile_offset += ts->len;
557 return subbuf_fifo_queue(tip, ts);
561 * Use the copy approach for pipes and network
563 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
565 struct tip_subbuf *ts = malloc(sizeof(*ts));
568 ts->buf = malloc(buf_size);
569 ts->max_len = maxlen;
571 ret = read_data(tip, ts->buf, ts->max_len);
574 return subbuf_fifo_queue(tip, ts);
580 static void close_thread(struct thread_information *tip)
586 if (tip->ofile_buffer)
587 free(tip->ofile_buffer);
593 tip->ofile_buffer = NULL;
597 static void tip_ftrunc_final(struct thread_information *tip)
600 * truncate to right size and cleanup mmap
602 if (tip->ofile_mmap) {
603 int ofd = fileno(tip->ofile);
606 munmap(tip->fs_buf, tip->fs_buf_len);
608 ftruncate(ofd, tip->fs_size);
612 static void *thread_main(void *arg)
614 struct thread_information *tip = arg;
615 pid_t pid = getpid();
619 CPU_SET((tip->cpu), &cpu_mask);
621 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
622 perror("sched_setaffinity");
626 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
627 relay_path, tip->device->buts_name, tip->cpu);
628 tip->fd = open(tip->fn, O_RDONLY);
631 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
637 if (tip->get_subbuf(tip, buf_size))
641 tip_ftrunc_final(tip);
646 static int write_data_net(int fd, void *buf, unsigned int buf_len)
648 unsigned int bytes_left = buf_len;
652 ret = send(fd, buf, bytes_left, 0);
665 static int net_send_header(struct thread_information *tip, unsigned int len)
667 struct blktrace_net_hdr hdr;
669 hdr.magic = BLK_IO_TRACE_MAGIC;
670 strcpy(hdr.buts_name, tip->device->buts_name);
672 hdr.max_cpus = ncpus;
675 return write_data_net(net_out_fd, &hdr, sizeof(hdr));
679 * send header with 0 length to signal end-of-run
681 static void net_client_send_close(void)
683 struct blktrace_net_hdr hdr;
685 hdr.magic = BLK_IO_TRACE_MAGIC;
687 hdr.max_cpus = ncpus;
690 write_data_net(net_out_fd, &hdr, sizeof(hdr));
693 static int flush_subbuf_net(struct thread_information *tip,
694 struct tip_subbuf *ts)
696 if (net_send_header(tip, ts->len))
698 if (write_data_net(net_out_fd, ts->buf, ts->len))
706 static int flush_subbuf_sendfile(struct thread_information *tip,
707 struct tip_subbuf *ts)
709 if (net_send_header(tip, ts->len))
711 if (sendfile(net_out_fd, tip->fd, &ts->offset, ts->len) < 0) {
720 static int write_data(struct thread_information *tip, void *buf,
721 unsigned int buf_len)
729 ret = fwrite(buf, buf_len, 1, tip->ofile);
739 if (tip->ofile_stdout)
745 static int flush_subbuf_file(struct thread_information *tip,
746 struct tip_subbuf *ts)
748 unsigned int offset = 0;
749 struct blk_io_trace *t;
750 int pdu_len, events = 0;
753 * surplus from last run
755 if (tip->leftover_ts) {
756 struct tip_subbuf *prev_ts = tip->leftover_ts;
758 if (prev_ts->len + ts->len > prev_ts->max_len) {
759 prev_ts->max_len += ts->len;
760 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
763 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
764 prev_ts->len += ts->len;
770 tip->leftover_ts = NULL;
773 while (offset + sizeof(*t) <= ts->len) {
774 t = ts->buf + offset;
776 if (verify_trace(t)) {
777 write_data(tip, ts->buf, offset);
781 pdu_len = t->pdu_len;
783 if (offset + sizeof(*t) + pdu_len > ts->len)
786 offset += sizeof(*t) + pdu_len;
787 tip->events_processed++;
788 tip->data_read += sizeof(*t) + pdu_len;
792 if (write_data(tip, ts->buf, offset))
796 * leftover bytes, save them for next time
798 if (offset != ts->len) {
799 tip->leftover_ts = ts;
801 memmove(ts->buf, ts->buf + offset, ts->len);
810 static int write_tip_events(struct thread_information *tip)
812 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
815 return tip->flush_subbuf(tip, ts);
821 * scans the tips we know and writes out the subbuffers we accumulate
823 static void get_and_write_events(void)
825 struct device_information *dip;
826 struct thread_information *tip;
827 int i, j, events, ret, tips_running;
832 for_each_dip(dip, i) {
833 for_each_tip(dip, tip, j) {
834 ret = write_tip_events(tip);
850 for_each_dip(dip, i) {
851 for_each_tip(dip, tip, j) {
852 ret = write_tip_events(tip);
855 tips_running += !tip->exited;
859 } while (events || tips_running);
862 static void wait_for_threads(void)
865 * for piped or network output, poll and fetch data for writeout.
866 * for files, we just wait around for trace threads to exit
868 if ((output_name && !strcmp(output_name, "-")) ||
869 net_mode == Net_client)
870 get_and_write_events();
872 struct device_information *dip;
873 struct thread_information *tip;
874 int i, j, tips_running;
881 for_each_tip(dip, tip, j)
882 tips_running += !tip->exited;
883 } while (tips_running);
886 if (net_mode == Net_client)
887 net_client_send_close();
890 static void fill_ofname(char *dst, char *buts_name, int cpu)
895 len = sprintf(dst, "%s/", output_dir);
898 sprintf(dst + len, "%s.blktrace.%d", output_name, cpu);
900 sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu);
903 static void fill_ops(struct thread_information *tip)
908 if (net_mode == Net_client) {
910 tip->get_subbuf = get_subbuf_sendfile;
911 tip->flush_subbuf = flush_subbuf_sendfile;
913 tip->get_subbuf = get_subbuf;
914 tip->flush_subbuf = flush_subbuf_net;
918 tip->get_subbuf = mmap_subbuf;
920 tip->get_subbuf = get_subbuf;
922 tip->flush_subbuf = flush_subbuf_file;
925 if (net_mode == Net_server)
926 tip->read_data = read_data_net;
928 tip->read_data = read_data_file;
931 static int start_threads(struct device_information *dip)
933 struct thread_information *tip;
934 int j, pipeline = output_name && !strcmp(output_name, "-");
938 for_each_tip(dip, tip, j) {
941 tip->events_processed = 0;
942 memset(&tip->fifo, 0, sizeof(tip->fifo));
943 tip->leftover_ts = NULL;
946 tip->ofile = fdopen(STDOUT_FILENO, "w");
947 tip->ofile_stdout = 1;
952 fill_ofname(op, dip->buts_name, tip->cpu);
953 tip->ofile = fopen(op, "w+");
954 tip->ofile_stdout = 0;
957 vbuf_size = OFILE_BUF;
960 if (tip->ofile == NULL) {
965 tip->ofile_buffer = malloc(vbuf_size);
966 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
974 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
975 perror("pthread_create");
984 static void stop_threads(struct device_information *dip)
986 struct thread_information *tip;
990 for_each_tip(dip, tip, i) {
991 (void) pthread_join(tip->thread, (void *) &ret);
996 static void stop_all_threads(void)
998 struct device_information *dip;
1001 for_each_dip(dip, i)
1005 static void stop_all_tracing(void)
1007 struct device_information *dip;
1010 for_each_dip(dip, i)
1014 static void exit_trace(int status)
1016 if (!is_trace_stopped()) {
1025 static int resize_devices(char *path)
1027 int size = (ndevs + 1) * sizeof(struct device_information);
1029 device_information = realloc(device_information, size);
1030 if (!device_information) {
1031 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1034 device_information[ndevs].path = path;
1039 static int open_devices(void)
1041 struct device_information *dip;
1044 for_each_dip(dip, i) {
1045 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1055 static int start_devices(void)
1057 struct device_information *dip;
1060 size = ncpus * sizeof(struct thread_information);
1061 thread_information = malloc(size * ndevs);
1062 if (!thread_information) {
1063 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1067 for_each_dip(dip, i) {
1068 if (start_trace(dip)) {
1070 fprintf(stderr, "Failed to start trace on %s\n",
1077 __for_each_dip(dip, j, i)
1083 for_each_dip(dip, i) {
1084 dip->threads = thread_information + (i * ncpus);
1085 if (start_threads(dip)) {
1086 fprintf(stderr, "Failed to start worker threads\n");
1092 __for_each_dip(dip, j, i)
1094 for_each_dip(dip, i)
1103 static void show_stats(void)
1105 struct device_information *dip;
1106 struct thread_information *tip;
1107 unsigned long long events_processed, data_read;
1108 unsigned long total_drops;
1109 int i, j, no_stdout = 0;
1111 if (is_stat_shown())
1114 if (output_name && !strcmp(output_name, "-"))
1120 for_each_dip(dip, i) {
1122 printf("Device: %s\n", dip->path);
1123 events_processed = 0;
1125 for_each_tip(dip, tip, j) {
1127 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1128 tip->cpu, tip->events_processed,
1129 tip->data_read >> 10);
1130 events_processed += tip->events_processed;
1131 data_read += tip->data_read;
1133 total_drops += dip->drop_count;
1135 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1136 events_processed, dip->drop_count,
1141 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1144 static struct device_information *net_get_dip(char *buts_name)
1146 struct device_information *dip;
1149 for (i = 0; i < ndevs; i++) {
1150 dip = &device_information[i];
1152 if (!strcmp(dip->buts_name, buts_name))
1156 device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1157 dip = &device_information[ndevs];
1158 strcpy(dip->buts_name, buts_name);
1159 strcpy(dip->path, buts_name);
1161 dip->threads = malloc(ncpus * sizeof(struct thread_information));
1162 memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1167 for (i = 0; i < ncpus; i++) {
1168 struct thread_information *tip = &dip->threads[i];
1172 tip->ofile_stdout = 0;
1173 tip->ofile_mmap = 1;
1178 fill_ofname(op, dip->buts_name, tip->cpu);
1180 tip->ofile = fopen(op, "w+");
1190 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh)
1192 struct device_information *dip;
1194 ncpus = bnh->max_cpus;
1195 dip = net_get_dip(bnh->buts_name);
1196 return &dip->threads[bnh->cpu];
1199 static int net_get_header(struct blktrace_net_hdr *bnh)
1201 int fl = fcntl(net_in_fd, F_GETFL);
1202 int bytes_left, ret;
1205 fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1206 bytes_left = sizeof(*bnh);
1207 while (bytes_left && !is_done()) {
1208 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1210 if (errno != EAGAIN) {
1211 perror("recv header");
1224 fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1228 static int net_server_loop(void)
1230 struct thread_information *tip;
1231 struct blktrace_net_hdr bnh;
1233 if (net_get_header(&bnh))
1236 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1237 fprintf(stderr, "server: received data is bad\n");
1241 if (!data_is_native) {
1242 bnh.cpu = be32_to_cpu(bnh.cpu);
1243 bnh.len = be32_to_cpu(bnh.len);
1247 * len == 0 means that the other end signalled end-of-run
1250 fprintf(stderr, "server: end of run\n");
1254 tip = net_get_tip(&bnh);
1258 if (mmap_subbuf(tip, bnh.len))
1265 * Start here when we are in server mode - just fetch data from the network
1268 static int net_server(void)
1270 struct device_information *dip;
1271 struct thread_information *tip;
1272 struct sockaddr_in addr;
1276 fd = socket(AF_INET, SOCK_STREAM, 0);
1278 perror("server: socket");
1283 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1284 perror("setsockopt");
1288 memset(&addr, 0, sizeof(addr));
1289 addr.sin_family = AF_INET;
1290 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1291 addr.sin_port = htons(net_port);
1293 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1298 if (listen(fd, 1) < 0) {
1304 printf("blktrace: waiting for incoming connection...\n");
1306 socklen = sizeof(addr);
1307 net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen);
1308 if (net_in_fd < 0) {
1313 signal(SIGINT, handle_sigint);
1314 signal(SIGHUP, handle_sigint);
1315 signal(SIGTERM, handle_sigint);
1316 signal(SIGALRM, handle_sigint);
1318 printf("blktrace: connected!\n");
1320 while (!is_done()) {
1321 if (net_server_loop())
1325 for_each_dip(dip, i)
1326 for_each_tip(dip, tip, j)
1327 tip_ftrunc_final(tip);
1335 * cleanup for next run
1337 for_each_dip(dip, i) {
1338 for_each_tip(dip, tip, j)
1344 free(device_information);
1345 device_information = NULL;
1351 * Setup outgoing network connection where we will transmit data
1353 static int net_setup_client(void)
1355 struct sockaddr_in addr;
1358 fd = socket(AF_INET, SOCK_STREAM, 0);
1360 perror("client: socket");
1364 memset(&addr, 0, sizeof(addr));
1365 addr.sin_family = AF_INET;
1366 addr.sin_port = htons(net_port);
1368 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1369 struct hostent *hent = gethostbyname(hostname);
1371 perror("gethostbyname");
1375 memcpy(&addr.sin_addr, hent->h_addr, 4);
1376 strcpy(hostname, hent->h_name);
1379 printf("blktrace: connecting to %s\n", hostname);
1381 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1382 perror("client: connect");
1386 printf("blktrace: connected!\n");
1391 static char usage_str[] = \
1392 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1393 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1394 "\t-d Use specified device. May also be given last after options\n" \
1395 "\t-r Path to mounted relayfs, defaults to /relay\n" \
1396 "\t-o File(s) to send output to\n" \
1397 "\t-D Directory to prepend to output file names\n" \
1398 "\t-k Kill a running trace\n" \
1399 "\t-w Stop after defined time, in seconds\n" \
1400 "\t-a Only trace specified actions. See documentation\n" \
1401 "\t-A Give trace mask as a single value. See documentation\n" \
1402 "\t-b Sub buffer size in KiB\n" \
1403 "\t-n Number of sub buffers\n" \
1404 "\t-l Run in network listen mode (blktrace server)\n" \
1405 "\t-h Run in network client mode, connecting to the given host\n" \
1406 "\t-p Network port to use (default 8462)\n" \
1407 "\t-s Make the network client use sendfile() to transfer data\n" \
1408 "\t-V Print program version info\n\n";
1410 static void show_usage(char *program)
1412 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1415 int main(int argc, char *argv[])
1417 static char default_relay_path[] = "/relay";
1421 int act_mask_tmp = 0;
1423 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1426 i = find_mask_map(optarg);
1428 fprintf(stderr,"Invalid action mask %s\n",
1436 if ((sscanf(optarg, "%x", &i) != 1) ||
1437 !valid_act_opt(i)) {
1439 "Invalid set action mask %s/0x%x\n",
1447 if (resize_devices(optarg) != 0)
1452 relay_path = optarg;
1456 output_name = optarg;
1459 kill_running_trace = 1;
1462 stop_watch = atoi(optarg);
1463 if (stop_watch <= 0) {
1465 "Invalid stopwatch value (%d secs)\n",
1471 printf("%s version %s\n", argv[0], blktrace_version);
1474 buf_size = strtoul(optarg, NULL, 10);
1475 if (buf_size <= 0 || buf_size > 16*1024) {
1477 "Invalid buffer size (%lu)\n",buf_size);
1483 buf_nr = strtoul(optarg, NULL, 10);
1486 "Invalid buffer nr (%lu)\n", buf_nr);
1491 output_dir = optarg;
1494 net_mode = Net_client;
1495 strcpy(hostname, optarg);
1498 net_mode = Net_server;
1501 net_port = atoi(optarg);
1507 show_usage(argv[0]);
1512 setlocale(LC_NUMERIC, "en_US");
1514 page_size = getpagesize();
1516 if (net_mode == Net_server)
1517 return net_server();
1519 while (optind < argc) {
1520 if (resize_devices(argv[optind++]) != 0)
1525 show_usage(argv[0]);
1530 relay_path = default_relay_path;
1532 if (act_mask_tmp != 0)
1533 act_mask = act_mask_tmp;
1535 if (statfs(relay_path, &st) < 0) {
1537 fprintf(stderr,"%s does not appear to be a valid path\n",
1540 } else if (st.f_type != (long) RELAYFS_TYPE) {
1541 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1546 if (open_devices() != 0)
1549 if (kill_running_trace) {
1554 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1556 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1560 signal(SIGINT, handle_sigint);
1561 signal(SIGHUP, handle_sigint);
1562 signal(SIGTERM, handle_sigint);
1563 signal(SIGALRM, handle_sigint);
1565 if (net_mode == Net_client && net_setup_client())
1568 if (start_devices() != 0)
1571 atexit(stop_all_tracing);
1578 if (!is_trace_stopped()) {