2 * block queue tracing application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
7 * Rewrite to have a single thread per CPU (managing all devices on that CPU)
8 * Alan D. Brunelle <alan.brunelle@hp.com> - January 2009
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
44 #include <sys/param.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
51 #include <sys/sendfile.h>
57 * You may want to increase this even more, if you are logging at a high
58 * rate and see skipped/missed events
60 #define BUF_SIZE (512 * 1024)
63 #define FILE_VBUF_SIZE (128 * 1024)
65 #define DEBUGFS_TYPE (0x64626720)
66 #define TRACE_NET_PORT (8462)
81 * Generic stats collected: nevents can be _roughly_ estimated by data_read
82 * (discounting pdu...)
84 * These fields are updated w/ pdc_dr_update & pdc_nev_update below.
87 unsigned long long data_read;
88 unsigned long long nevents;
92 struct list_head head;
93 char *path; /* path to device special file */
94 char *buts_name; /* name returned from bt kernel code */
95 struct pdc_stats *stats;
97 unsigned long long drops;
100 * For piped output only:
102 * Each tracer will have a tracer_devpath_head that it will add new
103 * data onto. It's list is protected above (tracer_devpath_head.mutex)
104 * and it will signal the processing thread using the dp_cond,
105 * dp_mutex & dp_entries variables above.
107 struct tracer_devpath_head *heads;
110 * For network server mode only:
114 time_t cl_connect_time;
119 * For piped output to stdout we will have each tracer thread (one per dev)
120 * tack buffers read from the relay queues on a per-device list.
122 * The main thread will then collect trace buffers from each of lists in turn.
124 * We will use a mutex to guard each of the trace_buf list. The tracers
125 * can then signal the main thread using <dp_cond,dp_mutex> and
126 * dp_entries. (When dp_entries is 0, and a tracer adds an entry it will
127 * signal. When dp_entries is 0, the main thread will wait for that condition
130 * adb: It may be better just to have a large buffer per tracer per dev,
131 * and then use it as a ring-buffer. This would certainly cut down a lot
132 * of malloc/free thrashing, at the cost of more memory movements (potentially).
135 struct list_head head;
141 struct tracer_devpath_head {
142 pthread_mutex_t mutex;
143 struct list_head head;
144 struct trace_buf *prev;
148 * Used to handle the mmap() interfaces for output file (containing traces)
152 unsigned long long fs_size, fs_max_size, fs_off, fs_buf_len;
153 unsigned long buf_size, buf_nr;
158 * Each thread doing work on a (client) side of blktrace will have one
159 * of these. The ios array contains input/output information, pfds holds
160 * poll() data. The volatile's provide flags to/from the main executing
164 struct list_head head;
169 volatile int status, is_done;
173 * networking stuff follows. we include a magic number so we know whether
174 * to endianness convert or not.
176 * The len field is overloaded:
177 * 0 - Indicates an "open" - allowing the server to set up for a dev/cpu
178 * 1 - Indicates a "close" - Shut down connection orderly
180 * The cpu field is overloaded on close: it will contain the number of drops.
182 struct blktrace_net_hdr {
183 u32 magic; /* same as trace magic */
184 char buts_name[32]; /* trace name */
185 u32 cpu; /* for which cpu */
187 u32 len; /* length of following trace data */
188 u32 cl_id; /* id for set of client per-cpu connections */
189 u32 buf_size; /* client buf_size for this trace */
190 u32 buf_nr; /* client buf_nr for this trace */
191 u32 page_size; /* client page_size for this trace */
195 * Each host encountered has one of these. The head is used to link this
196 * on to the network server's ch_list. Connections associated with this
197 * host are linked on conn_list, and any devices traced on that host
198 * are connected on the devpaths list.
201 struct list_head head;
202 struct list_head conn_list;
203 struct list_head devpaths;
204 struct net_server_s *ns;
206 struct in_addr cl_in_addr;
207 int connects, ndevs, cl_opens;
211 * Each connection (client to server socket ('fd')) has one of these. A
212 * back reference to the host ('ch'), and lists headers (for the host
213 * list, and the network server conn_list) are also included.
216 struct list_head ch_head, ns_head;
223 * The network server requires some poll structures to be maintained -
224 * one per conection currently on conn_list. The nchs/ch_list values
225 * are for each host connected to this server. The addr field is used
226 * for scratch as new connections are established.
228 struct net_server_s {
229 struct list_head conn_list;
230 struct list_head ch_list;
232 int listen_fd, connects, nchs;
233 struct sockaddr_in addr;
237 * This structure is (generically) used to providide information
238 * for a read-to-write set of values.
240 * ifn & ifd represent input information
242 * ofn, ofd, ofp, obuf & mmap_info are used for output file (optionally).
248 struct cl_conn *nc; /* Server network connection */
251 * mmap controlled output files
253 struct mmap_info mmap_info;
256 * Client network fields
259 unsigned long long data_queued;
262 * Input/output file descriptors & names
265 char ifn[MAXPATHLEN + 64];
266 char ofn[MAXPATHLEN + 64];
269 static char blktrace_version[] = "2.0.0";
272 * Linkage to blktrace helper routines (trace conversions)
274 int data_is_native = -1;
279 static int act_mask = ~0U;
280 static int kill_running_trace;
281 static int stop_watch;
282 static int piped_output;
284 static char *debugfs_path = "/sys/kernel/debug";
285 static char *output_name;
286 static char *output_dir;
288 static unsigned long buf_size = BUF_SIZE;
289 static unsigned long buf_nr = BUF_NR;
293 static LIST_HEAD(devpaths);
294 static LIST_HEAD(tracers);
296 static volatile int done;
299 * tracer threads add entries, the main thread takes them off and processes
300 * them. These protect the dp_entries variable.
302 static pthread_cond_t dp_cond = PTHREAD_COND_INITIALIZER;
303 static pthread_mutex_t dp_mutex = PTHREAD_MUTEX_INITIALIZER;
304 static volatile int dp_entries;
307 * These synchronize master / thread interactions.
309 static pthread_cond_t mt_cond = PTHREAD_COND_INITIALIZER;
310 static pthread_mutex_t mt_mutex = PTHREAD_MUTEX_INITIALIZER;
311 static volatile int nthreads_running;
312 static volatile int nthreads_leaving;
313 static volatile int nthreads_error;
314 static volatile int tracers_run;
317 * network cmd line params
319 static struct sockaddr_in hostname_addr;
320 static char hostname[MAXHOSTNAMELEN];
321 static int net_port = TRACE_NET_PORT;
322 static int net_use_sendfile = 1;
326 static int (*handle_pfds)(struct tracer *, int, int);
327 static int (*handle_list)(struct tracer_devpath_head *, struct list_head *);
329 #define S_OPTS "d:a:A:r:o:kw:vVb:n:D:lh:p:sI:"
330 static struct option l_opts[] = {
333 .has_arg = required_argument,
338 .name = "input-devs",
339 .has_arg = required_argument,
345 .has_arg = required_argument,
351 .has_arg = required_argument,
357 .has_arg = required_argument,
363 .has_arg = required_argument,
369 .has_arg = no_argument,
375 .has_arg = required_argument,
381 .has_arg = no_argument,
387 .has_arg = no_argument,
392 .name = "buffer-size",
393 .has_arg = required_argument,
398 .name = "num-sub-buffers",
399 .has_arg = required_argument,
404 .name = "output-dir",
405 .has_arg = required_argument,
411 .has_arg = no_argument,
417 .has_arg = required_argument,
423 .has_arg = required_argument,
428 .name = "no-sendfile",
429 .has_arg = no_argument,
438 static char usage_str[] = "\n\n" \
439 "-d <dev> | --dev=<dev>\n" \
440 "[ -r <debugfs path> | --relay=<debugfs path> ]\n" \
441 "[ -o <file> | --output=<file>]\n" \
442 "[ -D <dir> | --output-dir=<dir>\n" \
443 "[ -w <time> | --stopwatch=<time>]\n" \
444 "[ -a <action field> | --act-mask=<action field>]\n" \
445 "[ -A <action mask> | --set-mask=<action mask>]\n" \
446 "[ -b <size> | --buffer-size]\n" \
447 "[ -n <number> | --num-sub-buffers=<number>]\n" \
448 "[ -l | --listen]\n" \
449 "[ -h <hostname> | --host=<hostname>]\n" \
450 "[ -p <port number> | --port=<port number>]\n" \
451 "[ -s | --no-sendfile]\n" \
452 "[ -I <devs file> | --input-devs=<devs file>]\n" \
453 "[ -v <version> | --version]\n" \
454 "[ -V <version> | --version]\n" \
456 "\t-d Use specified device. May also be given last after options\n" \
457 "\t-r Path to mounted debugfs, defaults to /sys/kernel/debug\n" \
458 "\t-o File(s) to send output to\n" \
459 "\t-D Directory to prepend to output file names\n" \
460 "\t-w Stop after defined time, in seconds\n" \
461 "\t-a Only trace specified actions. See documentation\n" \
462 "\t-A Give trace mask as a single value. See documentation\n" \
463 "\t-b Sub buffer size in KiB (default 512)\n" \
464 "\t-n Number of sub buffers (default 4)\n" \
465 "\t-l Run in network listen mode (blktrace server)\n" \
466 "\t-h Run in network client mode, connecting to the given host\n" \
467 "\t-p Network port to use (default 8462)\n" \
468 "\t-s Make the network client NOT use sendfile() to transfer data\n" \
469 "\t-I Add devices found in <devs file>\n" \
470 "\t-v Print program version info\n" \
471 "\t-V Print program version info\n\n";
473 static void clear_events(struct pollfd *pfd)
479 static inline int net_client_use_sendfile(void)
481 return net_mode == Net_client && net_use_sendfile;
484 static inline int net_client_use_send(void)
486 return net_mode == Net_client && !net_use_sendfile;
489 static inline int use_tracer_devpaths(void)
491 return piped_output || net_client_use_send();
494 static inline int in_addr_eq(struct in_addr a, struct in_addr b)
496 return a.s_addr == b.s_addr;
499 static inline void pdc_dr_update(struct devpath *dpp, int cpu, int data_read)
501 dpp->stats[cpu].data_read += data_read;
504 static inline void pdc_nev_update(struct devpath *dpp, int cpu, int nevents)
506 dpp->stats[cpu].nevents += nevents;
509 static void show_usage(char *prog)
511 fprintf(stderr, "Usage: %s %s", prog, usage_str);
515 * Create a timespec 'msec' milliseconds into the future
517 static inline void make_timespec(struct timespec *tsp, long delta_msec)
521 gettimeofday(&now, NULL);
522 tsp->tv_sec = now.tv_sec;
523 tsp->tv_nsec = 1000L * now.tv_usec;
525 tsp->tv_nsec += (delta_msec * 1000000L);
526 if (tsp->tv_nsec > 1000000000L) {
527 long secs = tsp->tv_nsec / 1000000000L;
530 tsp->tv_nsec -= (secs * 1000000000L);
535 * Add a timer to ensure wait ends
537 static void t_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
541 make_timespec(&ts, 50);
542 pthread_cond_timedwait(cond, mutex, &ts);
545 static void unblock_tracers(void)
547 pthread_mutex_lock(&mt_mutex);
549 pthread_cond_broadcast(&mt_cond);
550 pthread_mutex_unlock(&mt_mutex);
553 static void tracer_wait_unblock(struct tracer *tp)
555 pthread_mutex_lock(&mt_mutex);
556 while (!tp->is_done && !tracers_run)
557 pthread_cond_wait(&mt_cond, &mt_mutex);
558 pthread_mutex_unlock(&mt_mutex);
561 static void tracer_signal_ready(struct tracer *tp,
562 enum thread_status th_status,
565 pthread_mutex_lock(&mt_mutex);
568 if (th_status == Th_running)
570 else if (th_status == Th_error)
575 pthread_cond_signal(&mt_cond);
576 pthread_mutex_unlock(&mt_mutex);
579 static void wait_tracers_ready(int ncpus_started)
581 pthread_mutex_lock(&mt_mutex);
582 while ((nthreads_running + nthreads_error) < ncpus_started)
583 t_pthread_cond_wait(&mt_cond, &mt_mutex);
584 pthread_mutex_unlock(&mt_mutex);
587 static void wait_tracers_leaving(void)
589 pthread_mutex_lock(&mt_mutex);
590 while (nthreads_leaving < nthreads_running)
591 t_pthread_cond_wait(&mt_cond, &mt_mutex);
592 pthread_mutex_unlock(&mt_mutex);
595 static void init_mmap_info(struct mmap_info *mip)
597 mip->buf_size = buf_size;
598 mip->buf_nr = buf_nr;
599 mip->pagesize = pagesize;
602 static void net_close_connection(int *fd)
604 shutdown(*fd, SHUT_RDWR);
609 static void dpp_free(struct devpath *dpp)
618 free(dpp->buts_name);
622 static int lock_on_cpu(int cpu)
624 cpu_set_t * cpu_mask;
626 cpu_mask = CPU_ALLOC(ncpus);
627 size = CPU_ALLOC_SIZE(ncpus);
629 CPU_ZERO_S(size, cpu_mask);
630 CPU_SET_S(cpu, size, cpu_mask);
631 if (sched_setaffinity(0, size, cpu_mask) < 0) {
640 static int increase_limit(int resource, rlim_t increase)
643 int save_errno = errno;
645 if (!getrlimit(resource, &rlim)) {
646 rlim.rlim_cur += increase;
647 if (rlim.rlim_cur >= rlim.rlim_max)
648 rlim.rlim_max = rlim.rlim_cur + increase;
650 if (!setrlimit(resource, &rlim))
658 static int handle_open_failure(void)
660 if (errno == ENFILE || errno == EMFILE)
661 return increase_limit(RLIMIT_NOFILE, 16);
665 static int handle_mem_failure(size_t length)
668 return handle_open_failure();
669 else if (errno == ENOMEM)
670 return increase_limit(RLIMIT_MEMLOCK, 2 * length);
674 static FILE *my_fopen(const char *path, const char *mode)
679 fp = fopen(path, mode);
680 } while (fp == NULL && handle_open_failure());
685 static int my_open(const char *path, int flags)
690 fd = open(path, flags);
691 } while (fd < 0 && handle_open_failure());
696 static int my_socket(int domain, int type, int protocol)
701 fd = socket(domain, type, protocol);
702 } while (fd < 0 && handle_open_failure());
707 static int my_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
712 fd = accept(sockfd, addr, addrlen);
713 } while (fd < 0 && handle_open_failure());
718 static void *my_mmap(void *addr, size_t length, int prot, int flags, int fd,
724 new = mmap(addr, length, prot, flags, fd, offset);
725 } while (new == MAP_FAILED && handle_mem_failure(length));
730 static int my_mlock(struct tracer *tp,
731 const void *addr, size_t len)
736 ret = mlock(addr, len);
737 if ((retry >= 10) && tp && tp->is_done)
740 } while (ret < 0 && handle_mem_failure(len));
745 static int setup_mmap(int fd, unsigned int maxlen,
746 struct mmap_info *mip,
749 if (mip->fs_off + maxlen > mip->fs_buf_len) {
750 unsigned long nr = max(16, mip->buf_nr);
753 munlock(mip->fs_buf, mip->fs_buf_len);
754 munmap(mip->fs_buf, mip->fs_buf_len);
758 mip->fs_off = mip->fs_size & (mip->pagesize - 1);
759 mip->fs_buf_len = (nr * mip->buf_size) - mip->fs_off;
760 mip->fs_max_size += mip->fs_buf_len;
762 if (ftruncate(fd, mip->fs_max_size) < 0) {
763 perror("setup_mmap: ftruncate");
767 mip->fs_buf = my_mmap(NULL, mip->fs_buf_len, PROT_WRITE,
769 mip->fs_size - mip->fs_off);
770 if (mip->fs_buf == MAP_FAILED) {
771 perror("setup_mmap: mmap");
774 if (my_mlock(tp, mip->fs_buf, mip->fs_buf_len) < 0) {
775 perror("setup_mlock: mlock");
783 static int __stop_trace(int fd)
786 * Should be stopped, don't complain if it isn't
788 ioctl(fd, BLKTRACESTOP);
789 return ioctl(fd, BLKTRACETEARDOWN);
792 static int write_data(char *buf, int len)
797 ret = fwrite(buf, len, 1, pfp);
798 if (ferror(pfp) || ret != 1) {
799 if (errno == EINTR) {
804 if (!piped_output || (errno != EPIPE && errno != EBADF)) {
805 fprintf(stderr, "write(%d) failed: %d/%s\n",
806 len, errno, strerror(errno));
820 * Returns the number of bytes read (successfully)
822 static int __net_recv_data(int fd, void *buf, unsigned int len)
824 unsigned int bytes_left = len;
826 while (bytes_left && !done) {
827 int ret = recv(fd, buf, bytes_left, MSG_WAITALL);
832 if (errno == EAGAIN) {
836 perror("server: net_recv_data: recv failed");
844 return len - bytes_left;
847 static int net_recv_data(int fd, void *buf, unsigned int len)
849 return __net_recv_data(fd, buf, len);
853 * Returns number of bytes written
855 static int net_send_data(int fd, void *buf, unsigned int buf_len)
858 unsigned int bytes_left = buf_len;
861 ret = send(fd, buf, bytes_left, 0);
871 return buf_len - bytes_left;
874 static int net_send_header(int fd, int cpu, char *buts_name, int len)
876 struct blktrace_net_hdr hdr;
878 memset(&hdr, 0, sizeof(hdr));
880 hdr.magic = BLK_IO_TRACE_MAGIC;
881 memset(hdr.buts_name, 0, sizeof(hdr.buts_name));
882 strncpy(hdr.buts_name, buts_name, sizeof(hdr.buts_name));
883 hdr.buts_name[sizeof(hdr.buts_name) - 1] = '\0';
885 hdr.max_cpus = ncpus;
887 hdr.cl_id = getpid();
888 hdr.buf_size = buf_size;
890 hdr.page_size = pagesize;
892 return net_send_data(fd, &hdr, sizeof(hdr)) != sizeof(hdr);
895 static void net_send_open_close(int fd, int cpu, char *buts_name, int len)
897 struct blktrace_net_hdr ret_hdr;
899 net_send_header(fd, cpu, buts_name, len);
900 net_recv_data(fd, &ret_hdr, sizeof(ret_hdr));
903 static void net_send_open(int fd, int cpu, char *buts_name)
905 net_send_open_close(fd, cpu, buts_name, 0);
908 static void net_send_close(int fd, char *buts_name, int drops)
911 * Overload CPU w/ number of drops
913 * XXX: Need to clear/set done around call - done=1 (which
914 * is true here) stops reads from happening... :-(
917 net_send_open_close(fd, drops, buts_name, 1);
921 static void ack_open_close(int fd, char *buts_name)
923 net_send_header(fd, 0, buts_name, 2);
926 static void net_send_drops(int fd)
930 __list_for_each(p, &devpaths) {
931 struct devpath *dpp = list_entry(p, struct devpath, head);
933 net_send_close(fd, dpp->buts_name, dpp->drops);
943 static int net_get_header(struct cl_conn *nc, struct blktrace_net_hdr *bnh)
946 int fl = fcntl(nc->fd, F_GETFL);
948 fcntl(nc->fd, F_SETFL, fl | O_NONBLOCK);
949 bytes_read = __net_recv_data(nc->fd, bnh, sizeof(*bnh));
950 fcntl(nc->fd, F_SETFL, fl & ~O_NONBLOCK);
952 if (bytes_read == sizeof(*bnh))
954 else if (bytes_read == 0)
960 static int net_setup_addr(void)
962 struct sockaddr_in *addr = &hostname_addr;
964 memset(addr, 0, sizeof(*addr));
965 addr->sin_family = AF_INET;
966 addr->sin_port = htons(net_port);
968 if (inet_aton(hostname, &addr->sin_addr) != 1) {
969 struct hostent *hent;
971 hent = gethostbyname(hostname);
973 if (h_errno == TRY_AGAIN) {
976 } else if (h_errno == NO_RECOVERY) {
977 fprintf(stderr, "gethostbyname(%s)"
978 "non-recoverable error encountered\n",
982 * HOST_NOT_FOUND, NO_ADDRESS or NO_DATA
984 fprintf(stderr, "Host %s not found\n",
990 memcpy(&addr->sin_addr, hent->h_addr, 4);
991 memset(hostname, 0, sizeof(hostname));
992 strncpy(hostname, hent->h_name, sizeof(hostname));
993 hostname[sizeof(hostname) - 1] = '\0';
999 static int net_setup_client(void)
1002 struct sockaddr_in *addr = &hostname_addr;
1004 fd = my_socket(AF_INET, SOCK_STREAM, 0);
1006 perror("client: socket");
1010 if (connect(fd, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
1011 if (errno == ECONNREFUSED)
1013 "\nclient: Connection to %s refused, "
1014 "perhaps the server is not started?\n\n",
1017 perror("client: connect");
1026 static int open_client_connections(void)
1030 cl_fds = calloc(ncpus, sizeof(*cl_fds));
1031 for (cpu = 0; cpu < ncpus; cpu++) {
1032 cl_fds[cpu] = net_setup_client();
1033 if (cl_fds[cpu] < 0)
1040 close(cl_fds[cpu--]);
1045 static void close_client_connections(void)
1050 for (cpu = 0, fdp = cl_fds; cpu < ncpus; cpu++, fdp++) {
1052 net_send_drops(*fdp);
1053 net_close_connection(fdp);
1060 static void setup_buts(void)
1062 struct list_head *p;
1064 __list_for_each(p, &devpaths) {
1065 struct blk_user_trace_setup buts;
1066 struct devpath *dpp = list_entry(p, struct devpath, head);
1068 memset(&buts, 0, sizeof(buts));
1069 buts.buf_size = buf_size;
1070 buts.buf_nr = buf_nr;
1071 buts.act_mask = act_mask;
1073 if (ioctl(dpp->fd, BLKTRACESETUP, &buts) >= 0) {
1075 dpp->buts_name = strdup(buts.name);
1078 dpp->stats = calloc(dpp->ncpus, sizeof(*dpp->stats));
1079 memset(dpp->stats, 0, dpp->ncpus * sizeof(*dpp->stats));
1081 fprintf(stderr, "BLKTRACESETUP(2) %s failed: %d/%s\n",
1082 dpp->path, errno, strerror(errno));
1086 static void start_buts(void)
1088 struct list_head *p;
1090 __list_for_each(p, &devpaths) {
1091 struct devpath *dpp = list_entry(p, struct devpath, head);
1093 if (ioctl(dpp->fd, BLKTRACESTART) < 0) {
1094 fprintf(stderr, "BLKTRACESTART %s failed: %d/%s\n",
1095 dpp->path, errno, strerror(errno));
1100 static int get_drops(struct devpath *dpp)
1103 char fn[MAXPATHLEN + 64], tmp[256];
1105 snprintf(fn, sizeof(fn), "%s/block/%s/dropped", debugfs_path,
1108 fd = my_open(fn, O_RDONLY);
1111 * This may be ok: the kernel may not support
1114 if (errno != ENOENT)
1115 fprintf(stderr, "Could not open %s: %d/%s\n",
1116 fn, errno, strerror(errno));
1118 } else if (read(fd, tmp, sizeof(tmp)) < 0) {
1119 fprintf(stderr, "Could not read %s: %d/%s\n",
1120 fn, errno, strerror(errno));
1128 static void get_all_drops(void)
1130 struct list_head *p;
1132 __list_for_each(p, &devpaths) {
1133 struct devpath *dpp = list_entry(p, struct devpath, head);
1135 dpp->drops = get_drops(dpp);
1139 static inline struct trace_buf *alloc_trace_buf(int cpu, int bufsize)
1141 struct trace_buf *tbp;
1143 tbp = malloc(sizeof(*tbp) + bufsize);
1144 INIT_LIST_HEAD(&tbp->head);
1146 tbp->buf = (void *)(tbp + 1);
1148 tbp->dpp = NULL; /* Will be set when tbp is added */
1153 static void free_tracer_heads(struct devpath *dpp)
1156 struct tracer_devpath_head *hd;
1158 for (cpu = 0, hd = dpp->heads; cpu < ncpus; cpu++, hd++) {
1162 pthread_mutex_destroy(&hd->mutex);
1167 static int setup_tracer_devpaths(void)
1169 struct list_head *p;
1171 if (net_client_use_send())
1172 if (open_client_connections())
1175 __list_for_each(p, &devpaths) {
1177 struct tracer_devpath_head *hd;
1178 struct devpath *dpp = list_entry(p, struct devpath, head);
1180 dpp->heads = calloc(ncpus, sizeof(struct tracer_devpath_head));
1181 for (cpu = 0, hd = dpp->heads; cpu < ncpus; cpu++, hd++) {
1182 INIT_LIST_HEAD(&hd->head);
1183 pthread_mutex_init(&hd->mutex, NULL);
1191 static inline void add_trace_buf(struct devpath *dpp, int cpu,
1192 struct trace_buf **tbpp)
1194 struct trace_buf *tbp = *tbpp;
1195 struct tracer_devpath_head *hd = &dpp->heads[cpu];
1199 pthread_mutex_lock(&hd->mutex);
1200 list_add_tail(&tbp->head, &hd->head);
1201 pthread_mutex_unlock(&hd->mutex);
1203 *tbpp = alloc_trace_buf(cpu, buf_size);
1206 static inline void incr_entries(int entries_handled)
1208 pthread_mutex_lock(&dp_mutex);
1209 if (dp_entries == 0)
1210 pthread_cond_signal(&dp_cond);
1211 dp_entries += entries_handled;
1212 pthread_mutex_unlock(&dp_mutex);
1215 static void decr_entries(int handled)
1217 pthread_mutex_lock(&dp_mutex);
1218 dp_entries -= handled;
1219 pthread_mutex_unlock(&dp_mutex);
1222 static int wait_empty_entries(void)
1224 pthread_mutex_lock(&dp_mutex);
1225 while (!done && dp_entries == 0)
1226 t_pthread_cond_wait(&dp_cond, &dp_mutex);
1227 pthread_mutex_unlock(&dp_mutex);
1232 static int add_devpath(char *path)
1235 struct devpath *dpp;
1236 struct list_head *p;
1239 * Verify device is not duplicated
1241 __list_for_each(p, &devpaths) {
1242 struct devpath *tmp = list_entry(p, struct devpath, head);
1243 if (!strcmp(tmp->path, path))
1247 * Verify device is valid before going too far
1249 fd = my_open(path, O_RDONLY | O_NONBLOCK);
1251 fprintf(stderr, "Invalid path %s specified: %d/%s\n",
1252 path, errno, strerror(errno));
1256 dpp = malloc(sizeof(*dpp));
1257 memset(dpp, 0, sizeof(*dpp));
1258 dpp->path = strdup(path);
1261 list_add_tail(&dpp->head, &devpaths);
1266 static void rel_devpaths(void)
1268 struct list_head *p, *q;
1270 list_for_each_safe(p, q, &devpaths) {
1271 struct devpath *dpp = list_entry(p, struct devpath, head);
1273 list_del(&dpp->head);
1274 __stop_trace(dpp->fd);
1278 free_tracer_heads(dpp);
1285 static int flush_subbuf_net(struct trace_buf *tbp)
1287 int fd = cl_fds[tbp->cpu];
1288 struct devpath *dpp = tbp->dpp;
1290 if (net_send_header(fd, tbp->cpu, dpp->buts_name, tbp->len))
1292 else if (net_send_data(fd, tbp->buf, tbp->len) != tbp->len)
1299 handle_list_net(__attribute__((__unused__))struct tracer_devpath_head *hd,
1300 struct list_head *list)
1302 struct trace_buf *tbp;
1303 struct list_head *p, *q;
1304 int entries_handled = 0;
1306 list_for_each_safe(p, q, list) {
1307 tbp = list_entry(p, struct trace_buf, head);
1309 list_del(&tbp->head);
1312 if (cl_fds[tbp->cpu] >= 0) {
1313 if (flush_subbuf_net(tbp)) {
1314 close(cl_fds[tbp->cpu]);
1315 cl_fds[tbp->cpu] = -1;
1322 return entries_handled;
1326 * Tack 'tbp's buf onto the tail of 'prev's buf
1328 static struct trace_buf *tb_combine(struct trace_buf *prev,
1329 struct trace_buf *tbp)
1331 unsigned long tot_len;
1333 tot_len = prev->len + tbp->len;
1334 if (tot_len > buf_size) {
1336 * tbp->head isn't connected (it was 'prev'
1337 * so it had been taken off of the list
1338 * before). Therefore, we can realloc
1339 * the whole structures, as the other fields
1342 prev = realloc(prev, sizeof(*prev) + tot_len);
1343 prev->buf = (void *)(prev + 1);
1346 memcpy(prev->buf + prev->len, tbp->buf, tbp->len);
1347 prev->len = tot_len;
1353 static int handle_list_file(struct tracer_devpath_head *hd,
1354 struct list_head *list)
1356 int off, t_len, nevents;
1357 struct blk_io_trace *t;
1358 struct list_head *p, *q;
1359 int entries_handled = 0;
1360 struct trace_buf *tbp, *prev;
1363 list_for_each_safe(p, q, list) {
1364 tbp = list_entry(p, struct trace_buf, head);
1365 list_del(&tbp->head);
1369 * If there was some leftover before, tack this new
1370 * entry onto the tail of the previous one.
1373 tbp = tb_combine(prev, tbp);
1376 * See how many whole traces there are - send them
1377 * all out in one go.
1381 while (off + (int)sizeof(*t) <= tbp->len) {
1382 t = (struct blk_io_trace *)(tbp->buf + off);
1383 t_len = sizeof(*t) + t->pdu_len;
1384 if (off + t_len > tbp->len)
1391 pdc_nev_update(tbp->dpp, tbp->cpu, nevents);
1394 * Write any full set of traces, any remaining data is kept
1395 * for the next pass.
1398 if (write_data(tbp->buf, off) || off == tbp->len) {
1404 * Move valid data to beginning of buffer
1407 memmove(tbp->buf, tbp->buf + off, tbp->len);
1415 return entries_handled;
1418 static void __process_trace_bufs(void)
1421 struct list_head *p;
1422 struct list_head list;
1425 __list_for_each(p, &devpaths) {
1426 struct devpath *dpp = list_entry(p, struct devpath, head);
1427 struct tracer_devpath_head *hd = dpp->heads;
1429 for (cpu = 0; cpu < ncpus; cpu++, hd++) {
1430 pthread_mutex_lock(&hd->mutex);
1431 if (list_empty(&hd->head)) {
1432 pthread_mutex_unlock(&hd->mutex);
1436 list_replace_init(&hd->head, &list);
1437 pthread_mutex_unlock(&hd->mutex);
1439 handled += handle_list(hd, &list);
1444 decr_entries(handled);
1447 static void process_trace_bufs(void)
1449 while (wait_empty_entries())
1450 __process_trace_bufs();
1453 static void clean_trace_bufs(void)
1456 * No mutex needed here: we're only reading from the lists,
1460 __process_trace_bufs();
1463 static inline void read_err(int cpu, char *ifn)
1465 if (errno != EAGAIN)
1466 fprintf(stderr, "Thread %d failed read of %s: %d/%s\n",
1467 cpu, ifn, errno, strerror(errno));
1470 static int net_sendfile(struct io_info *iop)
1474 ret = sendfile(iop->ofd, iop->ifd, NULL, iop->ready);
1478 } else if (ret < (int)iop->ready) {
1479 fprintf(stderr, "short sendfile send (%d of %d)\n",
1487 static inline int net_sendfile_data(struct tracer *tp, struct io_info *iop)
1489 struct devpath *dpp = iop->dpp;
1491 if (net_send_header(iop->ofd, tp->cpu, dpp->buts_name, iop->ready))
1493 return net_sendfile(iop);
1496 static int fill_ofname(struct io_info *iop, int cpu)
1500 char *dst = iop->ofn;
1503 len = snprintf(iop->ofn, sizeof(iop->ofn), "%s/", output_dir);
1505 len = snprintf(iop->ofn, sizeof(iop->ofn), "./");
1507 if (net_mode == Net_server) {
1508 struct cl_conn *nc = iop->nc;
1510 len += sprintf(dst + len, "%s-", nc->ch->hostname);
1511 len += strftime(dst + len, 64, "%F-%T/",
1512 gmtime(&iop->dpp->cl_connect_time));
1515 if (stat(iop->ofn, &sb) < 0) {
1516 if (errno != ENOENT) {
1518 "Destination dir %s stat failed: %d/%s\n",
1519 iop->ofn, errno, strerror(errno));
1523 * There is no synchronization between multiple threads
1524 * trying to create the directory at once. It's harmless
1525 * to let them try, so just detect the problem and move on.
1527 if (mkdir(iop->ofn, 0755) < 0 && errno != EEXIST) {
1529 "Destination dir %s can't be made: %d/%s\n",
1530 iop->ofn, errno, strerror(errno));
1536 snprintf(iop->ofn + len, sizeof(iop->ofn), "%s.blktrace.%d",
1539 snprintf(iop->ofn + len, sizeof(iop->ofn), "%s.blktrace.%d",
1540 iop->dpp->buts_name, cpu);
1545 static int set_vbuf(struct io_info *iop, int mode, size_t size)
1547 iop->obuf = malloc(size);
1548 if (setvbuf(iop->ofp, iop->obuf, mode, size) < 0) {
1549 fprintf(stderr, "setvbuf(%s, %d) failed: %d/%s\n",
1550 iop->dpp->path, (int)size, errno,
1559 static int iop_open(struct io_info *iop, int cpu)
1562 if (fill_ofname(iop, cpu))
1565 iop->ofp = my_fopen(iop->ofn, "w+");
1566 if (iop->ofp == NULL) {
1567 fprintf(stderr, "Open output file %s failed: %d/%s\n",
1568 iop->ofn, errno, strerror(errno));
1572 if (set_vbuf(iop, _IOLBF, FILE_VBUF_SIZE)) {
1573 fprintf(stderr, "set_vbuf for file %s failed: %d/%s\n",
1574 iop->ofn, errno, strerror(errno));
1579 iop->ofd = fileno(iop->ofp);
1583 static void close_iop(struct io_info *iop)
1585 struct mmap_info *mip = &iop->mmap_info;
1588 munmap(mip->fs_buf, mip->fs_buf_len);
1590 if (!piped_output) {
1591 if (ftruncate(fileno(iop->ofp), mip->fs_size) < 0) {
1593 "Ignoring err: ftruncate(%s): %d/%s\n",
1594 iop->ofn, errno, strerror(errno));
1604 static void close_ios(struct tracer *tp)
1606 while (tp->nios > 0) {
1607 struct io_info *iop = &tp->ios[--tp->nios];
1609 iop->dpp->drops = get_drops(iop->dpp);
1615 else if (iop->ofd >= 0) {
1616 struct devpath *dpp = iop->dpp;
1618 net_send_close(iop->ofd, dpp->buts_name, dpp->drops);
1619 net_close_connection(&iop->ofd);
1627 static int open_ios(struct tracer *tp)
1630 struct io_info *iop;
1631 struct list_head *p;
1633 tp->ios = calloc(ndevs, sizeof(struct io_info));
1634 memset(tp->ios, 0, ndevs * sizeof(struct io_info));
1636 tp->pfds = calloc(ndevs, sizeof(struct pollfd));
1637 memset(tp->pfds, 0, ndevs * sizeof(struct pollfd));
1642 __list_for_each(p, &devpaths) {
1643 struct devpath *dpp = list_entry(p, struct devpath, head);
1647 snprintf(iop->ifn, sizeof(iop->ifn), "%s/block/%s/trace%d",
1648 debugfs_path, dpp->buts_name, tp->cpu);
1650 iop->ifd = my_open(iop->ifn, O_RDONLY | O_NONBLOCK);
1652 fprintf(stderr, "Thread %d failed open %s: %d/%s\n",
1653 tp->cpu, iop->ifn, errno, strerror(errno));
1657 init_mmap_info(&iop->mmap_info);
1660 pfd->events = POLLIN;
1664 else if (net_client_use_sendfile()) {
1665 iop->ofd = net_setup_client();
1668 net_send_open(iop->ofd, tp->cpu, dpp->buts_name);
1669 } else if (net_mode == Net_none) {
1670 if (iop_open(iop, tp->cpu))
1674 * This ensures that the server knows about all
1675 * connections & devices before _any_ closes
1677 net_send_open(cl_fds[tp->cpu], tp->cpu, dpp->buts_name);
1688 close(iop->ifd); /* tp->nios _not_ bumped */
1693 static int handle_pfds_file(struct tracer *tp, int nevs, int force_read)
1695 struct mmap_info *mip;
1696 int i, ret, nentries = 0;
1697 struct pollfd *pfd = tp->pfds;
1698 struct io_info *iop = tp->ios;
1700 for (i = 0; nevs > 0 && i < ndevs; i++, pfd++, iop++) {
1701 if (pfd->revents & POLLIN || force_read) {
1702 mip = &iop->mmap_info;
1704 ret = setup_mmap(iop->ofd, buf_size, mip, tp);
1710 ret = read(iop->ifd, mip->fs_buf + mip->fs_off,
1713 pdc_dr_update(iop->dpp, tp->cpu, ret);
1714 mip->fs_size += ret;
1717 } else if (ret == 0) {
1719 * Short reads after we're done stop us
1720 * from trying reads.
1725 read_err(tp->cpu, iop->ifn);
1726 if (errno != EAGAIN || tp->is_done)
1736 static int handle_pfds_netclient(struct tracer *tp, int nevs, int force_read)
1739 int i, nentries = 0;
1740 struct pollfd *pfd = tp->pfds;
1741 struct io_info *iop = tp->ios;
1743 for (i = 0; i < ndevs; i++, pfd++, iop++) {
1744 if (pfd->revents & POLLIN || force_read) {
1745 if (fstat(iop->ifd, &sb) < 0) {
1748 } else if (sb.st_size > (off_t)iop->data_queued) {
1749 iop->ready = sb.st_size - iop->data_queued;
1750 iop->data_queued = sb.st_size;
1752 if (!net_sendfile_data(tp, iop)) {
1753 pdc_dr_update(iop->dpp, tp->cpu,
1765 incr_entries(nentries);
1770 static int handle_pfds_entries(struct tracer *tp, int nevs, int force_read)
1772 int i, nentries = 0;
1773 struct trace_buf *tbp;
1774 struct pollfd *pfd = tp->pfds;
1775 struct io_info *iop = tp->ios;
1777 tbp = alloc_trace_buf(tp->cpu, buf_size);
1778 for (i = 0; i < ndevs; i++, pfd++, iop++) {
1779 if (pfd->revents & POLLIN || force_read) {
1780 tbp->len = read(iop->ifd, tbp->buf, buf_size);
1782 pdc_dr_update(iop->dpp, tp->cpu, tbp->len);
1783 add_trace_buf(iop->dpp, tp->cpu, &tbp);
1785 } else if (tbp->len == 0) {
1787 * Short reads after we're done stop us
1788 * from trying reads.
1793 read_err(tp->cpu, iop->ifn);
1794 if (errno != EAGAIN || tp->is_done)
1797 if (!piped_output && --nevs == 0)
1804 incr_entries(nentries);
1809 static void *thread_main(void *arg)
1811 int ret, ndone, to_val;
1812 struct tracer *tp = arg;
1814 ret = lock_on_cpu(tp->cpu);
1823 to_val = 50; /* Frequent partial handles */
1825 to_val = 500; /* 1/2 second intervals */
1828 tracer_signal_ready(tp, Th_running, 0);
1829 tracer_wait_unblock(tp);
1831 while (!tp->is_done) {
1832 ndone = poll(tp->pfds, ndevs, to_val);
1833 if (ndone || piped_output)
1834 (void)handle_pfds(tp, ndone, piped_output);
1835 else if (ndone < 0 && errno != EINTR)
1836 fprintf(stderr, "Thread %d poll failed: %d/%s\n",
1837 tp->cpu, errno, strerror(errno));
1841 * Trace is stopped, pull data until we get a short read
1843 while (handle_pfds(tp, ndevs, 1) > 0)
1847 tracer_signal_ready(tp, Th_leaving, 0);
1851 tracer_signal_ready(tp, Th_error, ret);
1855 static int start_tracer(int cpu)
1859 tp = malloc(sizeof(*tp));
1860 memset(tp, 0, sizeof(*tp));
1862 INIT_LIST_HEAD(&tp->head);
1866 if (pthread_create(&tp->thread, NULL, thread_main, tp)) {
1867 fprintf(stderr, "FAILED to start thread on CPU %d: %d/%s\n",
1868 cpu, errno, strerror(errno));
1873 list_add_tail(&tp->head, &tracers);
1877 static void start_tracers(void)
1880 struct list_head *p;
1882 for (cpu = 0; cpu < ncpus; cpu++)
1883 if (start_tracer(cpu))
1886 wait_tracers_ready(cpu);
1888 __list_for_each(p, &tracers) {
1889 struct tracer *tp = list_entry(p, struct tracer, head);
1892 "FAILED to start thread on CPU %d: %d/%s\n",
1893 tp->cpu, tp->status, strerror(tp->status));
1897 static void stop_tracers(void)
1899 struct list_head *p;
1902 * Stop the tracing - makes the tracer threads clean up quicker.
1904 __list_for_each(p, &devpaths) {
1905 struct devpath *dpp = list_entry(p, struct devpath, head);
1906 (void)ioctl(dpp->fd, BLKTRACESTOP);
1910 * Tell each tracer to quit
1912 __list_for_each(p, &tracers) {
1913 struct tracer *tp = list_entry(p, struct tracer, head);
1916 pthread_cond_broadcast(&mt_cond);
1919 static void del_tracers(void)
1921 struct list_head *p, *q;
1923 list_for_each_safe(p, q, &tracers) {
1924 struct tracer *tp = list_entry(p, struct tracer, head);
1926 list_del(&tp->head);
1931 static void wait_tracers(void)
1933 struct list_head *p;
1935 if (use_tracer_devpaths())
1936 process_trace_bufs();
1938 wait_tracers_leaving();
1940 __list_for_each(p, &tracers) {
1942 struct tracer *tp = list_entry(p, struct tracer, head);
1944 ret = pthread_join(tp->thread, NULL);
1946 fprintf(stderr, "Thread join %d failed %d\n",
1950 if (use_tracer_devpaths())
1956 static void exit_tracing(void)
1958 signal(SIGINT, SIG_IGN);
1959 signal(SIGHUP, SIG_IGN);
1960 signal(SIGTERM, SIG_IGN);
1961 signal(SIGALRM, SIG_IGN);
1969 static void handle_sigint(__attribute__((__unused__)) int sig)
1975 static void show_stats(struct list_head *devpaths)
1978 struct list_head *p;
1979 unsigned long long nevents, data_read;
1980 unsigned long long total_drops = 0;
1981 unsigned long long total_events = 0;
1984 ofp = my_fopen("/dev/null", "w");
1988 __list_for_each(p, devpaths) {
1990 struct pdc_stats *sp;
1991 struct devpath *dpp = list_entry(p, struct devpath, head);
1993 if (net_mode == Net_server)
1994 printf("server: end of run for %s:%s\n",
1995 dpp->ch->hostname, dpp->buts_name);
2000 fprintf(ofp, "=== %s ===\n", dpp->buts_name);
2001 for (cpu = 0, sp = dpp->stats; cpu < dpp->ncpus; cpu++, sp++) {
2003 * Estimate events if not known...
2005 if (sp->nevents == 0) {
2006 sp->nevents = sp->data_read /
2007 sizeof(struct blk_io_trace);
2011 " CPU%3d: %20llu events, %8llu KiB data\n",
2012 cpu, sp->nevents, (sp->data_read + 1023) >> 10);
2014 data_read += sp->data_read;
2015 nevents += sp->nevents;
2018 fprintf(ofp, " Total: %20llu events (dropped %llu),"
2019 " %8llu KiB data\n", nevents,
2020 dpp->drops, (data_read + 1024) >> 10);
2022 total_drops += dpp->drops;
2023 total_events += (nevents + dpp->drops);
2031 double drops_ratio = 1.0;
2034 drops_ratio = (double)total_drops/(double)total_events;
2036 fprintf(stderr, "\nYou have %llu (%5.1lf%%) dropped events\n"
2037 "Consider using a larger buffer size (-b) "
2038 "and/or more buffers (-n)\n",
2039 total_drops, 100.0 * drops_ratio);
2043 static int handle_args(int argc, char *argv[])
2047 int act_mask_tmp = 0;
2049 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
2052 i = find_mask_map(optarg);
2054 fprintf(stderr, "Invalid action mask %s\n",
2062 if ((sscanf(optarg, "%x", &i) != 1) ||
2063 !valid_act_opt(i)) {
2065 "Invalid set action mask %s/0x%x\n",
2073 if (add_devpath(optarg) != 0)
2079 FILE *ifp = my_fopen(optarg, "r");
2083 "Invalid file for devices %s\n",
2088 while (fscanf(ifp, "%s\n", dev_line) == 1) {
2089 if (add_devpath(dev_line) != 0) {
2099 debugfs_path = optarg;
2103 output_name = optarg;
2106 kill_running_trace = 1;
2109 stop_watch = atoi(optarg);
2110 if (stop_watch <= 0) {
2112 "Invalid stopwatch value (%d secs)\n",
2119 printf("%s version %s\n", argv[0], blktrace_version);
2123 buf_size = strtoul(optarg, NULL, 10);
2124 if (buf_size <= 0 || buf_size > 16*1024) {
2125 fprintf(stderr, "Invalid buffer size (%lu)\n",
2132 buf_nr = strtoul(optarg, NULL, 10);
2135 "Invalid buffer nr (%lu)\n", buf_nr);
2140 output_dir = optarg;
2143 net_mode = Net_client;
2144 memset(hostname, 0, sizeof(hostname));
2145 strncpy(hostname, optarg, sizeof(hostname));
2146 hostname[sizeof(hostname) - 1] = '\0';
2149 net_mode = Net_server;
2152 net_port = atoi(optarg);
2155 net_use_sendfile = 0;
2158 show_usage(argv[0]);
2164 while (optind < argc)
2165 if (add_devpath(argv[optind++]) != 0)
2168 if (net_mode != Net_server && ndevs == 0) {
2169 show_usage(argv[0]);
2173 if (statfs(debugfs_path, &st) < 0) {
2174 fprintf(stderr, "Invalid debug path %s: %d/%s\n",
2175 debugfs_path, errno, strerror(errno));
2179 if (st.f_type != (long)DEBUGFS_TYPE) {
2180 fprintf(stderr, "Debugfs is not mounted at %s\n", debugfs_path);
2184 if (act_mask_tmp != 0)
2185 act_mask = act_mask_tmp;
2187 if (net_mode == Net_client && net_setup_addr())
2191 * Set up for appropriate PFD handler based upon output name.
2193 if (net_client_use_sendfile())
2194 handle_pfds = handle_pfds_netclient;
2195 else if (net_client_use_send())
2196 handle_pfds = handle_pfds_entries;
2197 else if (output_name && (strcmp(output_name, "-") == 0)) {
2199 handle_pfds = handle_pfds_entries;
2201 if (setvbuf(pfp, NULL, _IONBF, 0)) {
2202 perror("setvbuf stdout");
2206 handle_pfds = handle_pfds_file;
2210 static void ch_add_connection(struct net_server_s *ns, struct cl_host *ch,
2215 nc = malloc(sizeof(*nc));
2216 memset(nc, 0, sizeof(*nc));
2218 time(&nc->connect_time);
2223 list_add_tail(&nc->ch_head, &ch->conn_list);
2226 list_add_tail(&nc->ns_head, &ns->conn_list);
2228 ns->pfds = realloc(ns->pfds, (ns->connects+1) * sizeof(struct pollfd));
2231 static void ch_rem_connection(struct net_server_s *ns, struct cl_host *ch,
2234 net_close_connection(&nc->fd);
2236 list_del(&nc->ch_head);
2239 list_del(&nc->ns_head);
2241 ns->pfds = realloc(ns->pfds, (ns->connects+1) * sizeof(struct pollfd));
2246 static struct cl_host *net_find_client_host(struct net_server_s *ns,
2247 struct in_addr cl_in_addr)
2249 struct list_head *p;
2251 __list_for_each(p, &ns->ch_list) {
2252 struct cl_host *ch = list_entry(p, struct cl_host, head);
2254 if (in_addr_eq(ch->cl_in_addr, cl_in_addr))
2261 static struct cl_host *net_add_client_host(struct net_server_s *ns,
2262 struct sockaddr_in *addr)
2266 ch = malloc(sizeof(*ch));
2267 memset(ch, 0, sizeof(*ch));
2270 ch->cl_in_addr = addr->sin_addr;
2271 list_add_tail(&ch->head, &ns->ch_list);
2274 ch->hostname = strdup(inet_ntoa(addr->sin_addr));
2275 printf("server: connection from %s\n", ch->hostname);
2277 INIT_LIST_HEAD(&ch->conn_list);
2278 INIT_LIST_HEAD(&ch->devpaths);
2283 static void device_done(struct devpath *dpp, int ncpus)
2286 struct io_info *iop;
2288 for (cpu = 0, iop = dpp->ios; cpu < ncpus; cpu++, iop++)
2291 list_del(&dpp->head);
2295 static void net_ch_remove(struct cl_host *ch, int ncpus)
2297 struct list_head *p, *q;
2298 struct net_server_s *ns = ch->ns;
2300 list_for_each_safe(p, q, &ch->devpaths) {
2301 struct devpath *dpp = list_entry(p, struct devpath, head);
2302 device_done(dpp, ncpus);
2305 list_for_each_safe(p, q, &ch->conn_list) {
2306 struct cl_conn *nc = list_entry(p, struct cl_conn, ch_head);
2308 ch_rem_connection(ns, ch, nc);
2311 list_del(&ch->head);
2319 static void net_add_connection(struct net_server_s *ns)
2323 socklen_t socklen = sizeof(ns->addr);
2325 fd = my_accept(ns->listen_fd, (struct sockaddr *)&ns->addr, &socklen);
2328 * This is OK: we just won't accept this connection,
2333 ch = net_find_client_host(ns, ns->addr.sin_addr);
2335 ch = net_add_client_host(ns, &ns->addr);
2337 ch_add_connection(ns, ch, fd);
2341 static struct devpath *nc_add_dpp(struct cl_conn *nc,
2342 struct blktrace_net_hdr *bnh,
2343 time_t connect_time)
2346 struct io_info *iop;
2347 struct devpath *dpp;
2349 dpp = malloc(sizeof(*dpp));
2350 memset(dpp, 0, sizeof(*dpp));
2352 dpp->buts_name = strdup(bnh->buts_name);
2353 dpp->path = strdup(bnh->buts_name);
2356 dpp->cl_id = bnh->cl_id;
2357 dpp->cl_connect_time = connect_time;
2358 dpp->ncpus = nc->ncpus;
2359 dpp->stats = calloc(dpp->ncpus, sizeof(*dpp->stats));
2360 memset(dpp->stats, 0, dpp->ncpus * sizeof(*dpp->stats));
2362 list_add_tail(&dpp->head, &nc->ch->devpaths);
2365 dpp->ios = calloc(nc->ncpus, sizeof(*iop));
2366 memset(dpp->ios, 0, ndevs * sizeof(*iop));
2368 for (cpu = 0, iop = dpp->ios; cpu < nc->ncpus; cpu++, iop++) {
2371 init_mmap_info(&iop->mmap_info);
2373 if (iop_open(iop, cpu))
2381 * Need to unravel what's been done...
2384 close_iop(&dpp->ios[cpu--]);
2390 static struct devpath *nc_find_dpp(struct cl_conn *nc,
2391 struct blktrace_net_hdr *bnh)
2393 struct list_head *p;
2394 time_t connect_time = nc->connect_time;
2396 __list_for_each(p, &nc->ch->devpaths) {
2397 struct devpath *dpp = list_entry(p, struct devpath, head);
2399 if (!strcmp(dpp->buts_name, bnh->buts_name))
2402 if (dpp->cl_id == bnh->cl_id)
2403 connect_time = dpp->cl_connect_time;
2406 return nc_add_dpp(nc, bnh, connect_time);
2409 static void net_client_read_data(struct cl_conn *nc, struct devpath *dpp,
2410 struct blktrace_net_hdr *bnh)
2413 struct io_info *iop = &dpp->ios[bnh->cpu];
2414 struct mmap_info *mip = &iop->mmap_info;
2416 if (setup_mmap(iop->ofd, bnh->len, &iop->mmap_info, NULL)) {
2417 fprintf(stderr, "ncd(%s:%d): mmap failed\n",
2418 nc->ch->hostname, nc->fd);
2422 ret = net_recv_data(nc->fd, mip->fs_buf + mip->fs_off, bnh->len);
2424 pdc_dr_update(dpp, bnh->cpu, ret);
2425 mip->fs_size += ret;
2432 * Returns 1 if we closed a host - invalidates other polling information
2433 * that may be present.
2435 static int net_client_data(struct cl_conn *nc)
2438 struct devpath *dpp;
2439 struct blktrace_net_hdr bnh;
2441 ret = net_get_header(nc, &bnh);
2446 fprintf(stderr, "ncd(%d): header read failed\n", nc->fd);
2450 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
2451 fprintf(stderr, "ncd(%d): received data is bad\n", nc->fd);
2455 if (!data_is_native) {
2456 bnh.magic = be32_to_cpu(bnh.magic);
2457 bnh.cpu = be32_to_cpu(bnh.cpu);
2458 bnh.max_cpus = be32_to_cpu(bnh.max_cpus);
2459 bnh.len = be32_to_cpu(bnh.len);
2460 bnh.cl_id = be32_to_cpu(bnh.cl_id);
2461 bnh.buf_size = be32_to_cpu(bnh.buf_size);
2462 bnh.buf_nr = be32_to_cpu(bnh.buf_nr);
2463 bnh.page_size = be32_to_cpu(bnh.page_size);
2466 if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2467 fprintf(stderr, "ncd(%s:%d): bad data magic\n",
2468 nc->ch->hostname, nc->fd);
2472 if (nc->ncpus == -1)
2473 nc->ncpus = bnh.max_cpus;
2476 * len == 0 means the other end is sending us a new connection/dpp
2477 * len == 1 means that the other end signalled end-of-run
2479 dpp = nc_find_dpp(nc, &bnh);
2482 * Just adding in the dpp above is enough
2484 ack_open_close(nc->fd, dpp->buts_name);
2486 } else if (bnh.len == 1) {
2488 * overload cpu count with dropped events
2490 dpp->drops = bnh.cpu;
2492 ack_open_close(nc->fd, dpp->buts_name);
2493 if (--nc->ch->cl_opens == 0) {
2494 show_stats(&nc->ch->devpaths);
2495 net_ch_remove(nc->ch, nc->ncpus);
2499 net_client_read_data(nc, dpp, &bnh);
2504 static void handle_client_data(struct net_server_s *ns, int events)
2508 struct list_head *p, *q;
2511 list_for_each_safe(p, q, &ns->conn_list) {
2512 if (pfd->revents & POLLIN) {
2513 nc = list_entry(p, struct cl_conn, ns_head);
2515 if (net_client_data(nc) || --events == 0)
2522 static void net_setup_pfds(struct net_server_s *ns)
2525 struct list_head *p;
2527 ns->pfds[0].fd = ns->listen_fd;
2528 ns->pfds[0].events = POLLIN;
2531 __list_for_each(p, &ns->conn_list) {
2532 struct cl_conn *nc = list_entry(p, struct cl_conn, ns_head);
2535 pfd->events = POLLIN;
2540 static int net_server_handle_connections(struct net_server_s *ns)
2544 printf("server: waiting for connections...\n");
2548 events = poll(ns->pfds, ns->connects + 1, -1);
2550 if (errno != EINTR) {
2551 perror("FATAL: poll error");
2554 } else if (events > 0) {
2555 if (ns->pfds[0].revents & POLLIN) {
2556 net_add_connection(ns);
2561 handle_client_data(ns, events);
2568 static int net_server(void)
2572 struct net_server_s net_server;
2573 struct net_server_s *ns = &net_server;
2575 memset(ns, 0, sizeof(*ns));
2576 INIT_LIST_HEAD(&ns->ch_list);
2577 INIT_LIST_HEAD(&ns->conn_list);
2578 ns->pfds = malloc(sizeof(struct pollfd));
2580 fd = my_socket(AF_INET, SOCK_STREAM, 0);
2582 perror("server: socket");
2587 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
2588 perror("setsockopt");
2592 memset(&ns->addr, 0, sizeof(ns->addr));
2593 ns->addr.sin_family = AF_INET;
2594 ns->addr.sin_addr.s_addr = htonl(INADDR_ANY);
2595 ns->addr.sin_port = htons(net_port);
2597 if (bind(fd, (struct sockaddr *) &ns->addr, sizeof(ns->addr)) < 0) {
2602 if (listen(fd, 1) < 0) {
2608 * The actual server looping is done here:
2611 ret = net_server_handle_connections(ns);
2614 * Clean up and return...
2621 static int run_tracers(void)
2623 atexit(exit_tracing);
2624 if (net_mode == Net_client)
2625 printf("blktrace: connecting to %s\n", hostname);
2629 if (use_tracer_devpaths()) {
2630 if (setup_tracer_devpaths())
2634 handle_list = handle_list_file;
2636 handle_list = handle_list_net;
2640 if (nthreads_running == ncpus) {
2643 if (net_mode == Net_client)
2644 printf("blktrace: connected!\n");
2651 if (nthreads_running == ncpus)
2652 show_stats(&devpaths);
2653 if (net_client_use_send())
2654 close_client_connections();
2660 int main(int argc, char *argv[])
2664 setlocale(LC_NUMERIC, "en_US");
2665 pagesize = getpagesize();
2666 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
2668 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed %d/%s\n",
2669 errno, strerror(errno));
2672 } else if (handle_args(argc, argv)) {
2677 if (ndevs > 1 && output_name && strcmp(output_name, "-") != 0) {
2678 fprintf(stderr, "-o not supported with multiple devices\n");
2683 signal(SIGINT, handle_sigint);
2684 signal(SIGHUP, handle_sigint);
2685 signal(SIGTERM, handle_sigint);
2686 signal(SIGALRM, handle_sigint);
2687 signal(SIGPIPE, SIG_IGN);
2689 if (kill_running_trace) {
2690 struct devpath *dpp;
2691 struct list_head *p;
2693 __list_for_each(p, &devpaths) {
2694 dpp = list_entry(p, struct devpath, head);
2695 if (__stop_trace(dpp->fd)) {
2697 "BLKTRACETEARDOWN %s failed: %d/%s\n",
2698 dpp->path, errno, strerror(errno));
2701 } else if (net_mode == Net_server) {
2703 fprintf(stderr, "-o ignored in server mode\n");
2708 ret = run_tracers();