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.1";
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 DEBUGFS_TYPE 0x64626720
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,
149 .name = "no-sendfile",
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];
188 int (*get_subbuf)(struct thread_information *, unsigned int);
189 int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
190 int (*read_data)(struct thread_information *, void *, unsigned int);
192 unsigned long events_processed;
193 unsigned long long data_read;
194 unsigned long long data_queued;
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;
214 struct net_connection *nc;
217 struct device_information {
221 volatile int trace_started;
222 unsigned long drop_count;
223 struct thread_information *threads;
227 time_t cl_connect_time;
231 static struct thread_information *thread_information;
233 static struct device_information *device_information;
235 /* command line option globals */
236 static char *debugfs_path;
237 static char *output_name;
238 static char *output_dir;
239 static int act_mask = ~0U;
240 static int kill_running_trace;
241 static unsigned long buf_size = BUF_SIZE;
242 static unsigned long buf_nr = BUF_NR;
243 static unsigned int page_size;
245 #define is_done() (*(volatile int *)(&done))
246 static volatile int done;
248 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
249 static volatile int trace_stopped;
251 #define is_stat_shown() (*(volatile int *)(&stat_shown))
252 static volatile int stat_shown;
254 int data_is_native = -1;
256 static void exit_trace(int status);
258 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
259 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
261 #define __for_each_dip(__d, __di, __e, __i) \
262 for (__i = 0, __d = __di; __i < __e; __i++, __d++)
264 #define for_each_dip(__d, __i) \
265 __for_each_dip(__d, device_information, ndevs, __i)
266 #define for_each_nc_dip(__nc, __d, __i) \
267 __for_each_dip(__d, (__nc)->ch->device_information, (__nc)->ch->ndevs, __i)
269 #define __for_each_tip(__d, __t, __ncpus, __j) \
270 for (__j = 0, __t = (__d)->threads; __j < __ncpus; __j++, __t++)
271 #define for_each_tip(__d, __t, __j) \
272 __for_each_tip(__d, __t, ncpus, __j)
273 #define for_each_cl_host(__c) \
274 for (__c = cl_host_list; __c; __c = __c->list_next)
277 * networking stuff follows. we include a magic number so we know whether
278 * to endianness convert or not
280 struct blktrace_net_hdr {
281 u32 magic; /* same as trace magic */
282 char buts_name[32]; /* trace name */
283 u32 cpu; /* for which cpu */
285 u32 len; /* length of following trace data */
286 u32 cl_id; /* id for set of client per-cpu connections */
289 #define TRACE_NET_PORT (8462)
298 * network cmd line params
300 static char hostname[MAXHOSTNAMELEN];
301 static int net_port = TRACE_NET_PORT;
302 static int net_mode = 0;
303 static int net_use_sendfile = 1;
306 struct cl_host *list_next;
307 struct in_addr cl_in_addr;
308 struct net_connection *net_connections;
310 struct device_information *device_information;
316 struct net_connection {
324 #define NET_MAX_CL_HOSTS (1024)
325 static struct cl_host *cl_host_list;
327 static int net_connects;
329 static int *net_out_fd;
331 static void handle_sigint(__attribute__((__unused__)) int sig)
333 struct device_information *dip;
337 * stop trace so we can reap currently produced data
339 for_each_dip(dip, i) {
342 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
343 perror("BLKTRACESTOP");
349 static int get_dropped_count(const char *buts_name)
352 char tmp[MAXPATHLEN + 64];
354 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
355 debugfs_path, buts_name);
357 fd = open(tmp, O_RDONLY);
360 * this may be ok, if the kernel doesn't support dropped counts
365 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
369 if (read(fd, tmp, sizeof(tmp)) < 0) {
380 static int start_trace(struct device_information *dip)
382 struct blk_user_trace_setup buts;
384 memset(&buts, 0, sizeof(buts));
385 buts.buf_size = buf_size;
386 buts.buf_nr = buf_nr;
387 buts.act_mask = act_mask;
389 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
390 perror("BLKTRACESETUP");
394 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
395 perror("BLKTRACESTART");
399 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
400 dip_set_tracing(dip, 1);
404 static void stop_trace(struct device_information *dip)
406 if (dip_tracing(dip) || kill_running_trace) {
407 dip_set_tracing(dip, 0);
410 * should be stopped, just don't complain if it isn't
412 ioctl(dip->fd, BLKTRACESTOP);
414 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
415 perror("BLKTRACETEARDOWN");
422 static void stop_all_traces(void)
424 struct device_information *dip;
427 for_each_dip(dip, i) {
428 dip->drop_count = get_dropped_count(dip->buts_name);
433 static void wait_for_data(struct thread_information *tip, int timeout)
435 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
438 if (poll(&pfd, 1, timeout) < 0) {
442 if (pfd.revents & POLLIN)
444 if (tip->ofile_stdout)
449 static int read_data_file(struct thread_information *tip, void *buf,
455 wait_for_data(tip, 100);
457 ret = read(tip->fd, buf, len);
463 if (errno != EAGAIN) {
465 fprintf(stderr,"Thread %d failed read of %s\n",
471 } while (!is_done());
477 static int read_data_net(struct thread_information *tip, void *buf,
480 struct net_connection *nc = tip->nc;
481 unsigned int bytes_left = len;
485 ret = recv(nc->in_fd, buf, bytes_left, MSG_WAITALL);
490 if (errno != EAGAIN) {
492 fprintf(stderr, "server: failed read\n");
500 } while (!is_done() && bytes_left);
502 return len - bytes_left;
505 static inline struct tip_subbuf *
506 subbuf_fifo_dequeue(struct thread_information *tip)
508 const int head = tip->fifo.head;
509 const int next = (head + 1) & (FIFO_SIZE - 1);
511 if (head != tip->fifo.tail) {
512 struct tip_subbuf *ts = tip->fifo.q[head];
515 tip->fifo.head = next;
522 static inline int subbuf_fifo_queue(struct thread_information *tip,
523 struct tip_subbuf *ts)
525 const int tail = tip->fifo.tail;
526 const int next = (tail + 1) & (FIFO_SIZE - 1);
528 if (next != tip->fifo.head) {
529 tip->fifo.q[tail] = ts;
531 tip->fifo.tail = next;
535 fprintf(stderr, "fifo too small!\n");
540 * For file output, truncate and mmap the file appropriately
542 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
544 int ofd = fileno(tip->ofile);
548 * extend file, if we have to. use chunks of 16 subbuffers.
550 if (tip->fs_off + buf_size > tip->fs_buf_len) {
552 munlock(tip->fs_buf, tip->fs_buf_len);
553 munmap(tip->fs_buf, tip->fs_buf_len);
557 tip->fs_off = tip->fs_size & (page_size - 1);
558 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
559 tip->fs_max_size += tip->fs_buf_len;
561 if (ftruncate(ofd, tip->fs_max_size) < 0) {
566 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
567 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
568 if (tip->fs_buf == MAP_FAILED) {
572 mlock(tip->fs_buf, tip->fs_buf_len);
575 ret = tip->read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
577 tip->data_read += ret;
587 * Use the copy approach for pipes and network
589 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
591 struct tip_subbuf *ts = malloc(sizeof(*ts));
594 ts->buf = malloc(buf_size);
595 ts->max_len = maxlen;
597 ret = tip->read_data(tip, ts->buf, ts->max_len);
600 tip->data_read += ret;
601 if (subbuf_fifo_queue(tip, ts))
608 static void close_thread(struct thread_information *tip)
614 if (tip->ofile_buffer)
615 free(tip->ofile_buffer);
621 tip->ofile_buffer = NULL;
625 static void tip_ftrunc_final(struct thread_information *tip)
628 * truncate to right size and cleanup mmap
630 if (tip->ofile_mmap && tip->ofile) {
631 int ofd = fileno(tip->ofile);
634 munmap(tip->fs_buf, tip->fs_buf_len);
636 ftruncate(ofd, tip->fs_size);
640 static void *thread_main(void *arg)
642 struct thread_information *tip = arg;
643 pid_t pid = getpid();
647 CPU_SET((tip->cpu), &cpu_mask);
649 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
650 perror("sched_setaffinity");
654 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
655 debugfs_path, tip->device->buts_name, tip->cpu);
656 tip->fd = open(tip->fn, O_RDONLY);
659 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
665 if (tip->get_subbuf(tip, buf_size) < 0)
670 * trace is stopped, pull data until we get a short read
672 while (tip->get_subbuf(tip, buf_size) > 0)
675 tip_ftrunc_final(tip);
680 static int write_data_net(int fd, void *buf, unsigned int buf_len)
682 unsigned int bytes_left = buf_len;
686 ret = send(fd, buf, bytes_left, 0);
699 static int net_send_header(struct thread_information *tip, unsigned int len)
701 struct blktrace_net_hdr hdr;
703 hdr.magic = BLK_IO_TRACE_MAGIC;
704 strcpy(hdr.buts_name, tip->device->buts_name);
706 hdr.max_cpus = ncpus;
708 hdr.cl_id = getpid();
710 return write_data_net(net_out_fd[tip->cpu], &hdr, sizeof(hdr));
714 * send header with 0 length to signal end-of-run
716 static void net_client_send_close(void)
718 struct device_information *dip;
719 struct blktrace_net_hdr hdr;
722 for_each_dip(dip, i) {
723 hdr.magic = BLK_IO_TRACE_MAGIC;
724 hdr.max_cpus = ncpus;
726 strcpy(hdr.buts_name, dip->buts_name);
727 hdr.cpu = get_dropped_count(dip->buts_name);
728 hdr.cl_id = getpid();
730 write_data_net(net_out_fd[0], &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[tip->cpu], 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->cpu], 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)
768 if (net_send_header(tip, ts->len))
770 if (net_sendfile(tip, ts))
773 tip->data_read += ts->len;
774 tip->ofile_offset += buf_size;
781 static int get_subbuf_sendfile(struct thread_information *tip,
782 __attribute__((__unused__)) unsigned int maxlen)
784 struct tip_subbuf *ts;
788 wait_for_data(tip, -1);
790 if (fstat(tip->fd, &sb) < 0) {
791 perror("trace stat");
794 ready = sb.st_size - tip->data_queued;
800 ts = malloc(sizeof(*ts));
804 tip->data_queued += ready;
806 if (flush_subbuf_sendfile(tip, ts) < 0)
812 static int write_data(struct thread_information *tip, void *buf,
813 unsigned int buf_len)
821 ret = fwrite(buf, buf_len, 1, tip->ofile);
831 if (tip->ofile_stdout)
837 static int flush_subbuf_file(struct thread_information *tip,
838 struct tip_subbuf *ts)
840 unsigned int offset = 0;
841 struct blk_io_trace *t;
842 int pdu_len, events = 0;
845 * surplus from last run
847 if (tip->leftover_ts) {
848 struct tip_subbuf *prev_ts = tip->leftover_ts;
850 if (prev_ts->len + ts->len > prev_ts->max_len) {
851 prev_ts->max_len += ts->len;
852 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
855 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
856 prev_ts->len += ts->len;
862 tip->leftover_ts = NULL;
865 while (offset + sizeof(*t) <= ts->len) {
866 t = ts->buf + offset;
868 if (verify_trace(t)) {
869 write_data(tip, ts->buf, offset);
873 pdu_len = t->pdu_len;
875 if (offset + sizeof(*t) + pdu_len > ts->len)
878 offset += sizeof(*t) + pdu_len;
879 tip->events_processed++;
880 tip->data_read += sizeof(*t) + pdu_len;
884 if (write_data(tip, ts->buf, offset))
888 * leftover bytes, save them for next time
890 if (offset != ts->len) {
891 tip->leftover_ts = ts;
893 memmove(ts->buf, ts->buf + offset, ts->len);
902 static int write_tip_events(struct thread_information *tip)
904 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
907 return tip->flush_subbuf(tip, ts);
913 * scans the tips we know and writes out the subbuffers we accumulate
915 static void get_and_write_events(void)
917 struct device_information *dip;
918 struct thread_information *tip;
919 int i, j, events, ret, tips_running;
924 for_each_dip(dip, i) {
925 for_each_tip(dip, tip, j) {
926 ret = write_tip_events(tip);
942 for_each_dip(dip, i) {
943 for_each_tip(dip, tip, j) {
944 ret = write_tip_events(tip);
947 tips_running += !tip->exited;
951 } while (events || tips_running);
954 static void wait_for_threads(void)
957 * for piped or network output, poll and fetch data for writeout.
958 * for files, we just wait around for trace threads to exit
960 if ((output_name && !strcmp(output_name, "-")) ||
961 ((net_mode == Net_client) && !net_use_sendfile))
962 get_and_write_events();
964 struct device_information *dip;
965 struct thread_information *tip;
966 int i, j, tips_running;
973 for_each_tip(dip, tip, j)
974 tips_running += !tip->exited;
975 } while (tips_running);
978 if (net_mode == Net_client)
979 net_client_send_close();
982 static int fill_ofname(struct device_information *dip,
983 struct thread_information *tip, char *dst,
990 len = sprintf(dst, "%s/", output_dir);
992 len = sprintf(dst, "./");
994 if (net_mode == Net_server) {
995 struct net_connection *nc = tip->nc;
997 len += sprintf(dst + len, "%s-", inet_ntoa(nc->ch->cl_in_addr));
998 len += strftime(dst + len, 64, "%F-%T/", gmtime(&dip->cl_connect_time));
1001 if (stat(dst, &sb) < 0) {
1002 if (errno != ENOENT) {
1006 if (mkdir(dst, 0755) < 0) {
1008 fprintf(stderr, "Can't make output dir\n");
1014 sprintf(dst + len, "%s.blktrace.%d", output_name, tip->cpu);
1016 sprintf(dst + len, "%s.blktrace.%d", buts_name, tip->cpu);
1021 static void fill_ops(struct thread_information *tip)
1026 if (net_mode == Net_client) {
1027 if (net_use_sendfile) {
1028 tip->get_subbuf = get_subbuf_sendfile;
1029 tip->flush_subbuf = NULL;
1031 tip->get_subbuf = get_subbuf;
1032 tip->flush_subbuf = flush_subbuf_net;
1035 if (tip->ofile_mmap)
1036 tip->get_subbuf = mmap_subbuf;
1038 tip->get_subbuf = get_subbuf;
1040 tip->flush_subbuf = flush_subbuf_file;
1043 if (net_mode == Net_server)
1044 tip->read_data = read_data_net;
1046 tip->read_data = read_data_file;
1049 static int tip_open_output(struct device_information *dip,
1050 struct thread_information *tip)
1052 int pipeline = output_name && !strcmp(output_name, "-");
1053 int mode, vbuf_size;
1056 if (net_mode == Net_client) {
1058 tip->ofile_stdout = 0;
1059 tip->ofile_mmap = 0;
1061 } else if (pipeline) {
1062 tip->ofile = fdopen(STDOUT_FILENO, "w");
1063 tip->ofile_stdout = 1;
1064 tip->ofile_mmap = 0;
1068 if (fill_ofname(dip, tip, op, dip->buts_name))
1070 tip->ofile = fopen(op, "w+");
1071 tip->ofile_stdout = 0;
1072 tip->ofile_mmap = 1;
1074 vbuf_size = OFILE_BUF;
1077 if (tip->ofile == NULL) {
1082 tip->ofile_buffer = malloc(vbuf_size);
1083 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
1094 static int start_threads(struct device_information *dip)
1096 struct thread_information *tip;
1099 for_each_tip(dip, tip, j) {
1102 tip->events_processed = 0;
1104 memset(&tip->fifo, 0, sizeof(tip->fifo));
1105 tip->leftover_ts = NULL;
1107 if (tip_open_output(dip, tip))
1110 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
1111 perror("pthread_create");
1120 static void stop_threads(struct device_information *dip)
1122 struct thread_information *tip;
1126 for_each_tip(dip, tip, i) {
1127 (void) pthread_join(tip->thread, (void *) &ret);
1132 static void stop_all_threads(void)
1134 struct device_information *dip;
1137 for_each_dip(dip, i)
1141 static void stop_all_tracing(void)
1143 struct device_information *dip;
1146 for_each_dip(dip, i)
1150 static void exit_trace(int status)
1152 if (!is_trace_stopped()) {
1161 static int resize_devices(char *path)
1163 int size = (ndevs + 1) * sizeof(struct device_information);
1165 device_information = realloc(device_information, size);
1166 if (!device_information) {
1167 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1170 device_information[ndevs].path = path;
1175 static int open_devices(void)
1177 struct device_information *dip;
1180 for_each_dip(dip, i) {
1181 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1191 static int start_devices(void)
1193 struct device_information *dip;
1196 size = ncpus * sizeof(struct thread_information);
1197 thread_information = malloc(size * ndevs);
1198 if (!thread_information) {
1199 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1203 for_each_dip(dip, i) {
1204 if (start_trace(dip)) {
1206 fprintf(stderr, "Failed to start trace on %s\n",
1213 __for_each_dip(dip, device_information, i, j)
1219 for_each_dip(dip, i) {
1220 dip->threads = thread_information + (i * ncpus);
1221 if (start_threads(dip)) {
1222 fprintf(stderr, "Failed to start worker threads\n");
1228 __for_each_dip(dip, device_information, i, j)
1230 for_each_dip(dip, i)
1239 static void show_stats(struct device_information *dips, int ndips, int cpus)
1241 struct device_information *dip;
1242 struct thread_information *tip;
1243 unsigned long long events_processed, data_read;
1244 unsigned long total_drops;
1245 int i, j, no_stdout = 0;
1247 if (is_stat_shown())
1250 if (output_name && !strcmp(output_name, "-"))
1256 __for_each_dip(dip, dips, ndips, i) {
1258 printf("Device: %s\n", dip->path);
1259 events_processed = 0;
1261 __for_each_tip(dip, tip, cpus, j) {
1263 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
1264 tip->cpu, tip->events_processed,
1265 (tip->data_read + 1023) >> 10);
1266 events_processed += tip->events_processed;
1267 data_read += tip->data_read;
1269 total_drops += dip->drop_count;
1271 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
1272 events_processed, dip->drop_count,
1273 (data_read + 1023) >> 10);
1277 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1280 static struct device_information *net_get_dip(struct net_connection *nc,
1281 char *buts_name, u32 cl_id)
1283 struct device_information *dip, *cl_dip = NULL;
1284 struct cl_host *ch = nc->ch;
1287 for (i = 0; i < ch->ndevs; i++) {
1288 dip = &ch->device_information[i];
1290 if (!strcmp(dip->buts_name, buts_name))
1293 if (dip->cl_id == cl_id)
1297 ch->device_information = realloc(ch->device_information, (ch->ndevs + 1) * sizeof(*dip));
1298 dip = &ch->device_information[ch->ndevs];
1299 memset(dip, 0, sizeof(*dip));
1304 dip->cl_connect_time = cl_dip->cl_connect_time;
1306 dip->cl_connect_time = nc->connect_time;
1307 strcpy(dip->buts_name, buts_name);
1308 dip->path = strdup(buts_name);
1309 dip->trace_started = 1;
1311 dip->threads = malloc(nc->ncpus * sizeof(struct thread_information));
1312 memset(dip->threads, 0, nc->ncpus * sizeof(struct thread_information));
1317 for (i = 0; i < nc->ncpus; i++) {
1318 struct thread_information *tip = &dip->threads[i];
1325 if (tip_open_output(dip, tip))
1334 static struct thread_information *net_get_tip(struct net_connection *nc,
1335 struct blktrace_net_hdr *bnh)
1337 struct device_information *dip;
1338 struct thread_information *tip;
1340 nc->ncpus = bnh->max_cpus;
1341 dip = net_get_dip(nc, bnh->buts_name, bnh->cl_id);
1342 if (!dip->trace_started) {
1343 fprintf(stderr, "Events for closed devices %s\n", dip->buts_name);
1347 tip = &dip->threads[bnh->cpu];
1354 static int net_get_header(struct net_connection *nc,
1355 struct blktrace_net_hdr *bnh)
1357 int fl = fcntl(nc->in_fd, F_GETFL);
1358 int bytes_left, ret;
1361 fcntl(nc->in_fd, F_SETFL, fl | O_NONBLOCK);
1362 bytes_left = sizeof(*bnh);
1363 while (bytes_left && !is_done()) {
1364 ret = recv(nc->in_fd, p, bytes_left, MSG_WAITALL);
1366 if (errno != EAGAIN) {
1367 perror("recv header");
1380 fcntl(nc->in_fd, F_SETFL, fl & ~O_NONBLOCK);
1385 * finalize a net client: truncate files, show stats, cleanup, etc
1387 static void device_done(struct net_connection *nc, struct device_information *dip)
1389 struct thread_information *tip;
1392 __for_each_tip(dip, tip, nc->ncpus, i)
1393 tip_ftrunc_final(tip);
1395 show_stats(dip, 1, nc->ncpus);
1398 * cleanup for next run
1400 __for_each_tip(dip, tip, nc->ncpus, i) {
1414 static inline int in_addr_eq(struct in_addr a, struct in_addr b)
1416 return a.s_addr == b.s_addr;
1419 static void net_add_client_host(struct cl_host *ch)
1421 ch->list_next = cl_host_list;
1426 static void net_remove_client_host(struct cl_host *ch)
1428 struct cl_host *p, *c;
1430 for (p = c = cl_host_list; c; c = c->list_next) {
1433 cl_host_list = c->list_next;
1435 p->list_next = c->list_next;
1443 static struct cl_host *net_find_client_host(struct in_addr cl_in_addr)
1445 struct cl_host *ch = cl_host_list;
1448 if (in_addr_eq(ch->cl_in_addr, cl_in_addr))
1457 * finalize a net client: truncate files, show stats, cleanup, etc
1459 static void net_client_host_done(struct cl_host *ch)
1461 free(ch->device_information);
1462 free(ch->net_connections);
1463 net_connects -= ch->nconn;
1464 net_remove_client_host(ch);
1469 * handle incoming events from a net client
1471 static int net_client_data(struct net_connection *nc)
1473 struct thread_information *tip;
1474 struct blktrace_net_hdr bnh;
1476 if (net_get_header(nc, &bnh))
1479 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1480 fprintf(stderr, "server: received data is bad\n");
1484 if (!data_is_native) {
1485 bnh.magic = be32_to_cpu(bnh.magic);
1486 bnh.cpu = be32_to_cpu(bnh.cpu);
1487 bnh.len = be32_to_cpu(bnh.len);
1488 bnh.cl_id = be32_to_cpu(bnh.cl_id);
1491 if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1492 fprintf(stderr, "server: bad data magic\n");
1497 * len == 0 means that the other end signalled end-of-run
1501 * overload cpu count with dropped events
1503 struct device_information *dip;
1505 dip = net_get_dip(nc, bnh.buts_name, bnh.cl_id);
1506 dip->drop_count = bnh.cpu;
1507 dip->trace_started = 0;
1509 printf("server: end of run for %s\n", dip->buts_name);
1510 device_done(nc, dip);
1512 if (++nc->ch->ndevs_done == nc->ch->ndevs)
1513 net_client_host_done(nc->ch);
1517 tip = net_get_tip(nc, &bnh);
1521 if (mmap_subbuf(tip, bnh.len))
1527 static void net_add_connection(int listen_fd, struct sockaddr_in *addr)
1529 socklen_t socklen = sizeof(*addr);
1530 struct net_connection *nc;
1534 in_fd = accept(listen_fd, (struct sockaddr *) addr, &socklen);
1540 ch = net_find_client_host(addr->sin_addr);
1542 if (cl_hosts == NET_MAX_CL_HOSTS) {
1543 fprintf(stderr, "server: no more clients allowed\n");
1546 ch = malloc(sizeof(struct cl_host));
1547 memset(ch, 0, sizeof(*ch));
1548 ch->cl_in_addr = addr->sin_addr;
1549 net_add_client_host(ch);
1552 ch->net_connections = realloc(ch->net_connections, (ch->nconn + 1) * sizeof(*nc));
1553 nc = &ch->net_connections[ch->nconn++];
1554 memset(nc, 0, sizeof(*nc));
1556 printf("server: connection from %s\n", inet_ntoa(addr->sin_addr));
1557 time(&nc->connect_time);
1564 * event driven loop, handle new incoming connections and data from
1565 * existing connections
1567 static void net_server_handle_connections(int listen_fd,
1568 struct sockaddr_in *addr)
1570 struct pollfd *pfds = NULL;
1571 struct net_connection **ncs = NULL;
1572 int max_connects = 0;
1573 int i, nconns, events;
1575 struct net_connection *nc;
1577 printf("server: waiting for connections...\n");
1579 while (!is_done()) {
1580 if (net_connects >= max_connects) {
1581 pfds = realloc(pfds, (net_connects + 1) * sizeof(*pfds));
1582 ncs = realloc(ncs, (net_connects + 1) * sizeof(*ncs));
1583 max_connects = net_connects + 1;
1586 * the zero entry is for incoming connections, remaining
1587 * entries for clients
1589 pfds[0].fd = listen_fd;
1590 pfds[0].events = POLLIN;
1592 for_each_cl_host(ch) {
1593 for (i = 0; i < ch->nconn; i++) {
1594 nc = &ch->net_connections[i];
1595 pfds[nconns + 1].fd = nc->in_fd;
1596 pfds[nconns + 1].events = POLLIN;
1601 events = poll(pfds, 1 + nconns, -1);
1611 if (pfds[0].revents & POLLIN) {
1612 net_add_connection(listen_fd, addr);
1616 for (i = 0; events && i < nconns; i++) {
1617 if (pfds[i + 1].revents & POLLIN) {
1618 net_client_data(ncs[i]);
1626 * Start here when we are in server mode - just fetch data from the network
1629 static int net_server(void)
1631 struct sockaddr_in addr;
1634 fd = socket(AF_INET, SOCK_STREAM, 0);
1636 perror("server: socket");
1641 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1642 perror("setsockopt");
1646 memset(&addr, 0, sizeof(addr));
1647 addr.sin_family = AF_INET;
1648 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1649 addr.sin_port = htons(net_port);
1651 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1656 if (listen(fd, 1) < 0) {
1661 net_server_handle_connections(fd, &addr);
1666 * Setup outgoing network connection where we will transmit data
1668 static int net_setup_client_cpu(int i, struct sockaddr_in *addr)
1672 fd = socket(AF_INET, SOCK_STREAM, 0);
1674 perror("client: socket");
1678 if (connect(fd, (struct sockaddr *) addr, sizeof(*addr)) < 0) {
1679 perror("client: connect");
1687 static int net_setup_client(void)
1689 struct sockaddr_in addr;
1692 memset(&addr, 0, sizeof(addr));
1693 addr.sin_family = AF_INET;
1694 addr.sin_port = htons(net_port);
1696 if (inet_aton(hostname, &addr.sin_addr) != 1) {
1697 struct hostent *hent = gethostbyname(hostname);
1699 perror("gethostbyname");
1703 memcpy(&addr.sin_addr, hent->h_addr, 4);
1704 strcpy(hostname, hent->h_name);
1707 printf("blktrace: connecting to %s\n", hostname);
1709 net_out_fd = malloc(ncpus * sizeof(*net_out_fd));
1710 for (i = 0; i < ncpus; i++) {
1711 if (net_setup_client_cpu(i, &addr))
1715 printf("blktrace: connected!\n");
1720 static char usage_str[] = \
1721 "-d <dev> [ -r debugfs path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1722 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1723 "\t-d Use specified device. May also be given last after options\n" \
1724 "\t-r Path to mounted debugfs, defaults to /debug\n" \
1725 "\t-o File(s) to send output to\n" \
1726 "\t-D Directory to prepend to output file names\n" \
1727 "\t-k Kill a running trace\n" \
1728 "\t-w Stop after defined time, in seconds\n" \
1729 "\t-a Only trace specified actions. See documentation\n" \
1730 "\t-A Give trace mask as a single value. See documentation\n" \
1731 "\t-b Sub buffer size in KiB\n" \
1732 "\t-n Number of sub buffers\n" \
1733 "\t-l Run in network listen mode (blktrace server)\n" \
1734 "\t-h Run in network client mode, connecting to the given host\n" \
1735 "\t-p Network port to use (default 8462)\n" \
1736 "\t-s Make the network client NOT use sendfile() to transfer data\n" \
1737 "\t-V Print program version info\n\n";
1739 static void show_usage(char *program)
1741 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1744 int main(int argc, char *argv[])
1746 static char default_debugfs_path[] = "/debug";
1750 int act_mask_tmp = 0;
1752 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1755 i = find_mask_map(optarg);
1757 fprintf(stderr,"Invalid action mask %s\n",
1765 if ((sscanf(optarg, "%x", &i) != 1) ||
1766 !valid_act_opt(i)) {
1768 "Invalid set action mask %s/0x%x\n",
1776 if (resize_devices(optarg) != 0)
1781 debugfs_path = optarg;
1785 output_name = optarg;
1788 kill_running_trace = 1;
1791 stop_watch = atoi(optarg);
1792 if (stop_watch <= 0) {
1794 "Invalid stopwatch value (%d secs)\n",
1800 printf("%s version %s\n", argv[0], blktrace_version);
1803 buf_size = strtoul(optarg, NULL, 10);
1804 if (buf_size <= 0 || buf_size > 16*1024) {
1806 "Invalid buffer size (%lu)\n",buf_size);
1812 buf_nr = strtoul(optarg, NULL, 10);
1815 "Invalid buffer nr (%lu)\n", buf_nr);
1820 output_dir = optarg;
1823 net_mode = Net_client;
1824 strcpy(hostname, optarg);
1827 net_mode = Net_server;
1830 net_port = atoi(optarg);
1833 net_use_sendfile = 0;
1836 show_usage(argv[0]);
1841 setlocale(LC_NUMERIC, "en_US");
1843 page_size = getpagesize();
1845 if (net_mode == Net_server)
1846 return net_server();
1848 while (optind < argc) {
1849 if (resize_devices(argv[optind++]) != 0)
1854 show_usage(argv[0]);
1858 if (act_mask_tmp != 0)
1859 act_mask = act_mask_tmp;
1862 debugfs_path = default_debugfs_path;
1864 if (statfs(debugfs_path, &st) < 0) {
1866 fprintf(stderr,"%s does not appear to be a valid path\n",
1869 } else if (st.f_type != (long) DEBUGFS_TYPE) {
1870 fprintf(stderr,"%s does not appear to be a debug filesystem\n",
1875 if (open_devices() != 0)
1878 if (kill_running_trace) {
1883 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1885 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1889 signal(SIGINT, handle_sigint);
1890 signal(SIGHUP, handle_sigint);
1891 signal(SIGTERM, handle_sigint);
1892 signal(SIGALRM, handle_sigint);
1894 if (net_mode == Net_client && net_setup_client())
1897 if (start_devices() != 0)
1900 atexit(stop_all_tracing);
1907 if (!is_trace_stopped()) {
1913 show_stats(device_information, ndevs, ncpus);