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[] = \
439 "-d <dev> [ -r debugfs path ] [ -o <output> ] [-k ] [ -w time ]\n" \
440 "[ -a action ] [ -A action mask ] [ -I <devs file> ] [ -v ]\n\n" \
441 "\t-d Use specified device. May also be given last after options\n" \
442 "\t-r Path to mounted debugfs, defaults to /sys/kernel/debug\n" \
443 "\t-o File(s) to send output to\n" \
444 "\t-D Directory to prepend to output file names\n" \
445 "\t-k Kill a running trace\n" \
446 "\t-w Stop after defined time, in seconds\n" \
447 "\t-a Only trace specified actions. See documentation\n" \
448 "\t-A Give trace mask as a single value. See documentation\n" \
449 "\t-b Sub buffer size in KiB\n" \
450 "\t-n Number of sub buffers\n" \
451 "\t-l Run in network listen mode (blktrace server)\n" \
452 "\t-h Run in network client mode, connecting to the given host\n" \
453 "\t-p Network port to use (default 8462)\n" \
454 "\t-s Make the network client NOT use sendfile() to transfer data\n" \
455 "\t-I Add devices found in <devs file>\n" \
456 "\t-V Print program version info\n\n";
458 static void clear_events(struct pollfd *pfd)
464 static inline int net_client_use_sendfile(void)
466 return net_mode == Net_client && net_use_sendfile;
469 static inline int net_client_use_send(void)
471 return net_mode == Net_client && !net_use_sendfile;
474 static inline int use_tracer_devpaths(void)
476 return piped_output || net_client_use_send();
479 static inline int in_addr_eq(struct in_addr a, struct in_addr b)
481 return a.s_addr == b.s_addr;
484 static inline void pdc_dr_update(struct devpath *dpp, int cpu, int data_read)
486 dpp->stats[cpu].data_read += data_read;
489 static inline void pdc_nev_update(struct devpath *dpp, int cpu, int nevents)
491 dpp->stats[cpu].nevents += nevents;
494 static void show_usage(char *prog)
496 fprintf(stderr, "Usage: %s %s %s", prog, blktrace_version, usage_str);
500 * Create a timespec 'msec' milliseconds into the future
502 static inline void make_timespec(struct timespec *tsp, long delta_msec)
506 gettimeofday(&now, NULL);
507 tsp->tv_sec = now.tv_sec;
508 tsp->tv_nsec = 1000L * now.tv_usec;
510 tsp->tv_nsec += (delta_msec * 1000000L);
511 if (tsp->tv_nsec > 1000000000L) {
512 long secs = tsp->tv_nsec / 1000000000L;
515 tsp->tv_nsec -= (secs * 1000000000L);
520 * Add a timer to ensure wait ends
522 static void t_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
526 make_timespec(&ts, 50);
527 pthread_cond_timedwait(cond, mutex, &ts);
530 static void unblock_tracers(void)
532 pthread_mutex_lock(&mt_mutex);
534 pthread_cond_broadcast(&mt_cond);
535 pthread_mutex_unlock(&mt_mutex);
538 static void tracer_wait_unblock(struct tracer *tp)
540 pthread_mutex_lock(&mt_mutex);
541 while (!tp->is_done && !tracers_run)
542 pthread_cond_wait(&mt_cond, &mt_mutex);
543 pthread_mutex_unlock(&mt_mutex);
546 static void tracer_signal_ready(struct tracer *tp,
547 enum thread_status th_status,
550 pthread_mutex_lock(&mt_mutex);
553 if (th_status == Th_running)
555 else if (th_status == Th_error)
560 pthread_cond_signal(&mt_cond);
561 pthread_mutex_unlock(&mt_mutex);
564 static void wait_tracers_ready(int ncpus_started)
566 pthread_mutex_lock(&mt_mutex);
567 while ((nthreads_running + nthreads_error) < ncpus_started)
568 t_pthread_cond_wait(&mt_cond, &mt_mutex);
569 pthread_mutex_unlock(&mt_mutex);
572 static void wait_tracers_leaving(void)
574 pthread_mutex_lock(&mt_mutex);
575 while (nthreads_leaving < nthreads_running)
576 t_pthread_cond_wait(&mt_cond, &mt_mutex);
577 pthread_mutex_unlock(&mt_mutex);
580 static void init_mmap_info(struct mmap_info *mip)
582 mip->buf_size = buf_size;
583 mip->buf_nr = buf_nr;
584 mip->pagesize = pagesize;
587 static void net_close_connection(int *fd)
589 shutdown(*fd, SHUT_RDWR);
594 static void dpp_free(struct devpath *dpp)
603 free(dpp->buts_name);
607 static int lock_on_cpu(int cpu)
612 CPU_SET(cpu, &cpu_mask);
613 if (sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask) < 0)
619 static int increase_limit(int resource, rlim_t increase)
622 int save_errno = errno;
624 if (!getrlimit(resource, &rlim)) {
625 rlim.rlim_cur += increase;
626 if (rlim.rlim_cur >= rlim.rlim_max)
627 rlim.rlim_max = rlim.rlim_cur + increase;
629 if (!setrlimit(resource, &rlim))
637 static int handle_open_failure(void)
639 if (errno == ENFILE || errno == EMFILE)
640 return increase_limit(RLIMIT_NOFILE, 16);
644 static int handle_mem_failure(size_t length)
647 return handle_open_failure();
648 else if (errno == ENOMEM)
649 return increase_limit(RLIMIT_MEMLOCK, 2 * length);
653 static FILE *my_fopen(const char *path, const char *mode)
658 fp = fopen(path, mode);
659 } while (fp == NULL && handle_open_failure());
664 static int my_open(const char *path, int flags)
669 fd = open(path, flags);
670 } while (fd < 0 && handle_open_failure());
675 static int my_socket(int domain, int type, int protocol)
680 fd = socket(domain, type, protocol);
681 } while (fd < 0 && handle_open_failure());
686 static int my_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
691 fd = accept(sockfd, addr, addrlen);
692 } while (fd < 0 && handle_open_failure());
697 static void *my_mmap(void *addr, size_t length, int prot, int flags, int fd,
703 new = mmap(addr, length, prot, flags, fd, offset);
704 } while (new == MAP_FAILED && handle_mem_failure(length));
709 static int my_mlock(const void *addr, size_t len)
714 ret = mlock(addr, len);
715 } while (ret < 0 && handle_mem_failure(len));
720 static int setup_mmap(int fd, unsigned int maxlen, struct mmap_info *mip)
722 if (mip->fs_off + maxlen > mip->fs_buf_len) {
723 unsigned long nr = max(16, mip->buf_nr);
726 munlock(mip->fs_buf, mip->fs_buf_len);
727 munmap(mip->fs_buf, mip->fs_buf_len);
731 mip->fs_off = mip->fs_size & (mip->pagesize - 1);
732 mip->fs_buf_len = (nr * mip->buf_size) - mip->fs_off;
733 mip->fs_max_size += mip->fs_buf_len;
735 if (ftruncate(fd, mip->fs_max_size) < 0) {
736 perror("setup_mmap: ftruncate");
740 mip->fs_buf = my_mmap(NULL, mip->fs_buf_len, PROT_WRITE,
742 mip->fs_size - mip->fs_off);
743 if (mip->fs_buf == MAP_FAILED) {
744 perror("setup_mmap: mmap");
747 my_mlock(mip->fs_buf, mip->fs_buf_len);
753 static int __stop_trace(int fd)
756 * Should be stopped, don't complain if it isn't
758 ioctl(fd, BLKTRACESTOP);
759 return ioctl(fd, BLKTRACETEARDOWN);
762 static int write_data(char *buf, int len)
767 ret = fwrite(buf, len, 1, pfp);
768 if (ferror(pfp) || ret != 1) {
769 if (errno == EINTR) {
774 if (!piped_output || (errno != EPIPE && errno != EBADF)) {
775 fprintf(stderr, "write(%d) failed: %d/%s\n",
776 len, errno, strerror(errno));
790 * Returns the number of bytes read (successfully)
792 static int __net_recv_data(int fd, void *buf, unsigned int len)
794 unsigned int bytes_left = len;
796 while (bytes_left && !done) {
797 int ret = recv(fd, buf, bytes_left, MSG_WAITALL);
802 if (errno == EAGAIN) {
806 perror("server: net_recv_data: recv failed");
814 return len - bytes_left;
817 static int net_recv_data(int fd, void *buf, unsigned int len)
819 return __net_recv_data(fd, buf, len);
823 * Returns number of bytes written
825 static int net_send_data(int fd, void *buf, unsigned int buf_len)
828 unsigned int bytes_left = buf_len;
831 ret = send(fd, buf, bytes_left, 0);
841 return buf_len - bytes_left;
844 static int net_send_header(int fd, int cpu, char *buts_name, int len)
846 struct blktrace_net_hdr hdr;
848 memset(&hdr, 0, sizeof(hdr));
850 hdr.magic = BLK_IO_TRACE_MAGIC;
851 strncpy(hdr.buts_name, buts_name, sizeof(hdr.buts_name));
852 hdr.buts_name[sizeof(hdr.buts_name)-1] = '\0';
854 hdr.max_cpus = ncpus;
856 hdr.cl_id = getpid();
857 hdr.buf_size = buf_size;
859 hdr.page_size = pagesize;
861 return net_send_data(fd, &hdr, sizeof(hdr)) != sizeof(hdr);
864 static void net_send_open_close(int fd, int cpu, char *buts_name, int len)
866 struct blktrace_net_hdr ret_hdr;
868 net_send_header(fd, cpu, buts_name, len);
869 net_recv_data(fd, &ret_hdr, sizeof(ret_hdr));
872 static void net_send_open(int fd, int cpu, char *buts_name)
874 net_send_open_close(fd, cpu, buts_name, 0);
877 static void net_send_close(int fd, char *buts_name, int drops)
880 * Overload CPU w/ number of drops
882 * XXX: Need to clear/set done around call - done=1 (which
883 * is true here) stops reads from happening... :-(
886 net_send_open_close(fd, drops, buts_name, 1);
890 static void ack_open_close(int fd, char *buts_name)
892 net_send_header(fd, 0, buts_name, 2);
895 static void net_send_drops(int fd)
899 __list_for_each(p, &devpaths) {
900 struct devpath *dpp = list_entry(p, struct devpath, head);
902 net_send_close(fd, dpp->buts_name, dpp->drops);
912 static int net_get_header(struct cl_conn *nc, struct blktrace_net_hdr *bnh)
915 int fl = fcntl(nc->fd, F_GETFL);
917 fcntl(nc->fd, F_SETFL, fl | O_NONBLOCK);
918 bytes_read = __net_recv_data(nc->fd, bnh, sizeof(*bnh));
919 fcntl(nc->fd, F_SETFL, fl & ~O_NONBLOCK);
921 if (bytes_read == sizeof(*bnh))
923 else if (bytes_read == 0)
929 static int net_setup_addr(void)
931 struct sockaddr_in *addr = &hostname_addr;
933 memset(addr, 0, sizeof(*addr));
934 addr->sin_family = AF_INET;
935 addr->sin_port = htons(net_port);
937 if (inet_aton(hostname, &addr->sin_addr) != 1) {
938 struct hostent *hent;
940 hent = gethostbyname(hostname);
942 if (h_errno == TRY_AGAIN) {
945 } else if (h_errno == NO_RECOVERY) {
946 fprintf(stderr, "gethostbyname(%s)"
947 "non-recoverable error encountered\n",
951 * HOST_NOT_FOUND, NO_ADDRESS or NO_DATA
953 fprintf(stderr, "Host %s not found\n",
959 memcpy(&addr->sin_addr, hent->h_addr, 4);
960 strcpy(hostname, hent->h_name);
966 static int net_setup_client(void)
969 struct sockaddr_in *addr = &hostname_addr;
971 fd = my_socket(AF_INET, SOCK_STREAM, 0);
973 perror("client: socket");
977 if (connect(fd, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
978 if (errno == ECONNREFUSED)
980 "\nclient: Connection to %s refused, "
981 "perhaps the server is not started?\n\n",
984 perror("client: connect");
993 static int open_client_connections(void)
997 cl_fds = calloc(ncpus, sizeof(*cl_fds));
998 for (cpu = 0; cpu < ncpus; cpu++) {
999 cl_fds[cpu] = net_setup_client();
1000 if (cl_fds[cpu] < 0)
1007 close(cl_fds[cpu--]);
1012 static void close_client_connections(void)
1017 for (cpu = 0, fdp = cl_fds; cpu < ncpus; cpu++, fdp++) {
1019 net_send_drops(*fdp);
1020 net_close_connection(fdp);
1027 static void setup_buts(void)
1029 struct list_head *p;
1031 __list_for_each(p, &devpaths) {
1032 struct blk_user_trace_setup buts;
1033 struct devpath *dpp = list_entry(p, struct devpath, head);
1035 memset(&buts, 0, sizeof(buts));
1036 buts.buf_size = buf_size;
1037 buts.buf_nr = buf_nr;
1038 buts.act_mask = act_mask;
1040 if (ioctl(dpp->fd, BLKTRACESETUP, &buts) >= 0) {
1042 dpp->buts_name = strdup(buts.name);
1045 dpp->stats = calloc(dpp->ncpus, sizeof(*dpp->stats));
1046 memset(dpp->stats, 0, dpp->ncpus * sizeof(*dpp->stats));
1048 fprintf(stderr, "BLKTRACESETUP(2) %s failed: %d/%s\n",
1049 dpp->path, errno, strerror(errno));
1053 static void start_buts(void)
1055 struct list_head *p;
1057 __list_for_each(p, &devpaths) {
1058 struct devpath *dpp = list_entry(p, struct devpath, head);
1060 if (ioctl(dpp->fd, BLKTRACESTART) < 0) {
1061 fprintf(stderr, "BLKTRACESTART %s failed: %d/%s\n",
1062 dpp->path, errno, strerror(errno));
1067 static int get_drops(struct devpath *dpp)
1070 char fn[MAXPATHLEN + 64], tmp[256];
1072 snprintf(fn, sizeof(fn), "%s/block/%s/dropped", debugfs_path,
1075 fd = my_open(fn, O_RDONLY);
1078 * This may be ok: the kernel may not support
1081 if (errno != ENOENT)
1082 fprintf(stderr, "Could not open %s: %d/%s\n",
1083 fn, errno, strerror(errno));
1085 } else if (read(fd, tmp, sizeof(tmp)) < 0) {
1086 fprintf(stderr, "Could not read %s: %d/%s\n",
1087 fn, errno, strerror(errno));
1095 static void get_all_drops(void)
1097 struct list_head *p;
1099 __list_for_each(p, &devpaths) {
1100 struct devpath *dpp = list_entry(p, struct devpath, head);
1102 dpp->drops = get_drops(dpp);
1106 static inline struct trace_buf *alloc_trace_buf(int cpu, int bufsize)
1108 struct trace_buf *tbp;
1110 tbp = malloc(sizeof(*tbp) + bufsize);
1111 INIT_LIST_HEAD(&tbp->head);
1113 tbp->buf = (void *)(tbp + 1);
1115 tbp->dpp = NULL; /* Will be set when tbp is added */
1120 static void free_tracer_heads(struct devpath *dpp)
1123 struct tracer_devpath_head *hd;
1125 for (cpu = 0, hd = dpp->heads; cpu < ncpus; cpu++, hd++) {
1129 pthread_mutex_destroy(&hd->mutex);
1134 static int setup_tracer_devpaths(void)
1136 struct list_head *p;
1138 if (net_client_use_send())
1139 if (open_client_connections())
1142 __list_for_each(p, &devpaths) {
1144 struct tracer_devpath_head *hd;
1145 struct devpath *dpp = list_entry(p, struct devpath, head);
1147 dpp->heads = calloc(ncpus, sizeof(struct tracer_devpath_head));
1148 for (cpu = 0, hd = dpp->heads; cpu < ncpus; cpu++, hd++) {
1149 INIT_LIST_HEAD(&hd->head);
1150 pthread_mutex_init(&hd->mutex, NULL);
1158 static inline void add_trace_buf(struct devpath *dpp, int cpu,
1159 struct trace_buf **tbpp)
1161 struct trace_buf *tbp = *tbpp;
1162 struct tracer_devpath_head *hd = &dpp->heads[cpu];
1166 pthread_mutex_lock(&hd->mutex);
1167 list_add_tail(&tbp->head, &hd->head);
1168 pthread_mutex_unlock(&hd->mutex);
1170 *tbpp = alloc_trace_buf(cpu, buf_size);
1173 static inline void incr_entries(int entries_handled)
1175 pthread_mutex_lock(&dp_mutex);
1176 if (dp_entries == 0)
1177 pthread_cond_signal(&dp_cond);
1178 dp_entries += entries_handled;
1179 pthread_mutex_unlock(&dp_mutex);
1182 static void decr_entries(int handled)
1184 pthread_mutex_lock(&dp_mutex);
1185 dp_entries -= handled;
1186 pthread_mutex_unlock(&dp_mutex);
1189 static int wait_empty_entries(void)
1191 pthread_mutex_lock(&dp_mutex);
1192 while (!done && dp_entries == 0)
1193 t_pthread_cond_wait(&dp_cond, &dp_mutex);
1194 pthread_mutex_unlock(&dp_mutex);
1199 static int add_devpath(char *path)
1202 struct devpath *dpp;
1205 * Verify device is valid before going too far
1207 fd = my_open(path, O_RDONLY | O_NONBLOCK);
1209 fprintf(stderr, "Invalid path %s specified: %d/%s\n",
1210 path, errno, strerror(errno));
1214 dpp = malloc(sizeof(*dpp));
1215 memset(dpp, 0, sizeof(*dpp));
1216 dpp->path = strdup(path);
1219 list_add_tail(&dpp->head, &devpaths);
1224 static void rel_devpaths(void)
1226 struct list_head *p, *q;
1228 list_for_each_safe(p, q, &devpaths) {
1229 struct devpath *dpp = list_entry(p, struct devpath, head);
1231 list_del(&dpp->head);
1232 __stop_trace(dpp->fd);
1236 free_tracer_heads(dpp);
1243 static int flush_subbuf_net(struct trace_buf *tbp)
1245 int fd = cl_fds[tbp->cpu];
1246 struct devpath *dpp = tbp->dpp;
1248 if (net_send_header(fd, tbp->cpu, dpp->buts_name, tbp->len))
1250 else if (net_send_data(fd, tbp->buf, tbp->len) != tbp->len)
1257 handle_list_net(__attribute__((__unused__))struct tracer_devpath_head *hd,
1258 struct list_head *list)
1260 struct trace_buf *tbp;
1261 struct list_head *p, *q;
1262 int entries_handled = 0;
1264 list_for_each_safe(p, q, list) {
1265 tbp = list_entry(p, struct trace_buf, head);
1267 list_del(&tbp->head);
1270 if (cl_fds[tbp->cpu] >= 0) {
1271 if (flush_subbuf_net(tbp)) {
1272 close(cl_fds[tbp->cpu]);
1273 cl_fds[tbp->cpu] = -1;
1280 return entries_handled;
1284 * Tack 'tbp's buf onto the tail of 'prev's buf
1286 static struct trace_buf *tb_combine(struct trace_buf *prev,
1287 struct trace_buf *tbp)
1289 unsigned long tot_len;
1291 tot_len = prev->len + tbp->len;
1292 if (tot_len > buf_size) {
1294 * tbp->head isn't connected (it was 'prev'
1295 * so it had been taken off of the list
1296 * before). Therefore, we can realloc
1297 * the whole structures, as the other fields
1300 prev = realloc(prev->buf, sizeof(*prev) + tot_len);
1301 prev->buf = (void *)(prev + 1);
1304 memcpy(prev->buf + prev->len, tbp->buf, tbp->len);
1305 prev->len = tot_len;
1311 static int handle_list_file(struct tracer_devpath_head *hd,
1312 struct list_head *list)
1314 int off, t_len, nevents;
1315 struct blk_io_trace *t;
1316 struct list_head *p, *q;
1317 int entries_handled = 0;
1318 struct trace_buf *tbp, *prev;
1321 list_for_each_safe(p, q, list) {
1322 tbp = list_entry(p, struct trace_buf, head);
1323 list_del(&tbp->head);
1327 * If there was some leftover before, tack this new
1328 * entry onto the tail of the previous one.
1331 tbp = tb_combine(prev, tbp);
1334 * See how many whole traces there are - send them
1335 * all out in one go.
1339 while (off + (int)sizeof(*t) <= tbp->len) {
1340 t = (struct blk_io_trace *)(tbp->buf + off);
1341 t_len = sizeof(*t) + t->pdu_len;
1342 if (off + t_len > tbp->len)
1349 pdc_nev_update(tbp->dpp, tbp->cpu, nevents);
1352 * Write any full set of traces, any remaining data is kept
1353 * for the next pass.
1356 if (write_data(tbp->buf, off) || off == tbp->len) {
1362 * Move valid data to beginning of buffer
1365 memmove(tbp->buf, tbp->buf + off, tbp->len);
1373 return entries_handled;
1376 static void __process_trace_bufs(void)
1379 struct list_head *p;
1380 struct list_head list;
1383 __list_for_each(p, &devpaths) {
1384 struct devpath *dpp = list_entry(p, struct devpath, head);
1385 struct tracer_devpath_head *hd = dpp->heads;
1387 for (cpu = 0; cpu < ncpus; cpu++, hd++) {
1388 pthread_mutex_lock(&hd->mutex);
1389 if (list_empty(&hd->head)) {
1390 pthread_mutex_unlock(&hd->mutex);
1394 list_replace_init(&hd->head, &list);
1395 pthread_mutex_unlock(&hd->mutex);
1397 handled += handle_list(hd, &list);
1402 decr_entries(handled);
1405 static void process_trace_bufs(void)
1407 while (wait_empty_entries())
1408 __process_trace_bufs();
1411 static void clean_trace_bufs(void)
1414 * No mutex needed here: we're only reading from the lists,
1418 __process_trace_bufs();
1421 static inline void read_err(int cpu, char *ifn)
1423 if (errno != EAGAIN)
1424 fprintf(stderr, "Thread %d failed read of %s: %d/%s\n",
1425 cpu, ifn, errno, strerror(errno));
1428 static int net_sendfile(struct io_info *iop)
1432 ret = sendfile(iop->ofd, iop->ifd, NULL, iop->ready);
1436 } else if (ret < (int)iop->ready) {
1437 fprintf(stderr, "short sendfile send (%d of %d)\n",
1445 static inline int net_sendfile_data(struct tracer *tp, struct io_info *iop)
1447 struct devpath *dpp = iop->dpp;
1449 if (net_send_header(iop->ofd, tp->cpu, dpp->buts_name, iop->ready))
1451 return net_sendfile(iop);
1454 static int fill_ofname(struct io_info *iop, int cpu)
1458 char *dst = iop->ofn;
1461 len = snprintf(iop->ofn, sizeof(iop->ofn), "%s/", output_dir);
1463 len = snprintf(iop->ofn, sizeof(iop->ofn), "./");
1465 if (net_mode == Net_server) {
1466 struct cl_conn *nc = iop->nc;
1468 len += sprintf(dst + len, "%s-", nc->ch->hostname);
1469 len += strftime(dst + len, 64, "%F-%T/",
1470 gmtime(&iop->dpp->cl_connect_time));
1473 if (stat(iop->ofn, &sb) < 0) {
1474 if (errno != ENOENT) {
1476 "Destination dir %s stat failed: %d/%s\n",
1477 iop->ofn, errno, strerror(errno));
1481 * There is no synchronization between multiple threads
1482 * trying to create the directory at once. It's harmless
1483 * to let them try, so just detect the problem and move on.
1485 if (mkdir(iop->ofn, 0755) < 0 && errno != EEXIST) {
1487 "Destination dir %s can't be made: %d/%s\n",
1488 iop->ofn, errno, strerror(errno));
1494 snprintf(iop->ofn + len, sizeof(iop->ofn), "%s.blktrace.%d",
1497 snprintf(iop->ofn + len, sizeof(iop->ofn), "%s.blktrace.%d",
1498 iop->dpp->buts_name, cpu);
1503 static int set_vbuf(struct io_info *iop, int mode, size_t size)
1505 iop->obuf = malloc(size);
1506 if (setvbuf(iop->ofp, iop->obuf, mode, size) < 0) {
1507 fprintf(stderr, "setvbuf(%s, %d) failed: %d/%s\n",
1508 iop->dpp->path, (int)size, errno,
1517 static int iop_open(struct io_info *iop, int cpu)
1520 if (fill_ofname(iop, cpu))
1523 iop->ofp = my_fopen(iop->ofn, "w+");
1524 if (iop->ofp == NULL) {
1525 fprintf(stderr, "Open output file %s failed: %d/%s\n",
1526 iop->ofn, errno, strerror(errno));
1530 if (set_vbuf(iop, _IOLBF, FILE_VBUF_SIZE)) {
1531 fprintf(stderr, "set_vbuf for file %s failed: %d/%s\n",
1532 iop->ofn, errno, strerror(errno));
1537 iop->ofd = fileno(iop->ofp);
1541 static void close_iop(struct io_info *iop)
1543 struct mmap_info *mip = &iop->mmap_info;
1546 munmap(mip->fs_buf, mip->fs_buf_len);
1548 if (!piped_output) {
1549 if (ftruncate(fileno(iop->ofp), mip->fs_size) < 0) {
1551 "Ignoring err: ftruncate(%s): %d/%s\n",
1552 iop->ofn, errno, strerror(errno));
1562 static void close_ios(struct tracer *tp)
1564 while (tp->nios > 0) {
1565 struct io_info *iop = &tp->ios[--tp->nios];
1567 iop->dpp->drops = get_drops(iop->dpp);
1573 else if (iop->ofd >= 0) {
1574 struct devpath *dpp = iop->dpp;
1576 net_send_close(iop->ofd, dpp->buts_name, dpp->drops);
1577 net_close_connection(&iop->ofd);
1585 static int open_ios(struct tracer *tp)
1588 struct io_info *iop;
1589 struct list_head *p;
1591 tp->ios = calloc(ndevs, sizeof(struct io_info));
1592 memset(tp->ios, 0, ndevs * sizeof(struct io_info));
1594 tp->pfds = calloc(ndevs, sizeof(struct pollfd));
1595 memset(tp->pfds, 0, ndevs * sizeof(struct pollfd));
1600 __list_for_each(p, &devpaths) {
1601 struct devpath *dpp = list_entry(p, struct devpath, head);
1605 snprintf(iop->ifn, sizeof(iop->ifn), "%s/block/%s/trace%d",
1606 debugfs_path, dpp->buts_name, tp->cpu);
1608 iop->ifd = my_open(iop->ifn, O_RDONLY | O_NONBLOCK);
1610 fprintf(stderr, "Thread %d failed open %s: %d/%s\n",
1611 tp->cpu, iop->ifn, errno, strerror(errno));
1615 init_mmap_info(&iop->mmap_info);
1618 pfd->events = POLLIN;
1622 else if (net_client_use_sendfile()) {
1623 iop->ofd = net_setup_client();
1626 net_send_open(iop->ofd, tp->cpu, dpp->buts_name);
1627 } else if (net_mode == Net_none) {
1628 if (iop_open(iop, tp->cpu))
1632 * This ensures that the server knows about all
1633 * connections & devices before _any_ closes
1635 net_send_open(cl_fds[tp->cpu], tp->cpu, dpp->buts_name);
1646 close(iop->ifd); /* tp->nios _not_ bumped */
1651 static int handle_pfds_file(struct tracer *tp, int nevs, int force_read)
1653 struct mmap_info *mip;
1654 int i, ret, nentries = 0;
1655 struct pollfd *pfd = tp->pfds;
1656 struct io_info *iop = tp->ios;
1658 for (i = 0; nevs > 0 && i < ndevs; i++, pfd++, iop++) {
1659 if (pfd->revents & POLLIN || force_read) {
1660 mip = &iop->mmap_info;
1662 ret = setup_mmap(iop->ofd, buf_size, mip);
1668 ret = read(iop->ifd, mip->fs_buf + mip->fs_off,
1671 pdc_dr_update(iop->dpp, tp->cpu, ret);
1672 mip->fs_size += ret;
1675 } else if (ret == 0) {
1677 * Short reads after we're done stop us
1678 * from trying reads.
1683 read_err(tp->cpu, iop->ifn);
1684 if (errno != EAGAIN || tp->is_done)
1694 static int handle_pfds_netclient(struct tracer *tp, int nevs, int force_read)
1697 int i, nentries = 0;
1698 struct pdc_stats *sp;
1699 struct pollfd *pfd = tp->pfds;
1700 struct io_info *iop = tp->ios;
1702 for (i = 0; i < ndevs; i++, pfd++, iop++, sp++) {
1703 if (pfd->revents & POLLIN || force_read) {
1704 if (fstat(iop->ifd, &sb) < 0) {
1707 } else if (sb.st_size > (off_t)iop->data_queued) {
1708 iop->ready = sb.st_size - iop->data_queued;
1709 iop->data_queued = sb.st_size;
1711 if (!net_sendfile_data(tp, iop)) {
1712 pdc_dr_update(iop->dpp, tp->cpu,
1724 incr_entries(nentries);
1729 static int handle_pfds_entries(struct tracer *tp, int nevs, int force_read)
1731 int i, nentries = 0;
1732 struct trace_buf *tbp;
1733 struct pollfd *pfd = tp->pfds;
1734 struct io_info *iop = tp->ios;
1736 tbp = alloc_trace_buf(tp->cpu, buf_size);
1737 for (i = 0; i < ndevs; i++, pfd++, iop++) {
1738 if (pfd->revents & POLLIN || force_read) {
1739 tbp->len = read(iop->ifd, tbp->buf, buf_size);
1741 pdc_dr_update(iop->dpp, tp->cpu, tbp->len);
1742 add_trace_buf(iop->dpp, tp->cpu, &tbp);
1744 } else if (tbp->len == 0) {
1746 * Short reads after we're done stop us
1747 * from trying reads.
1752 read_err(tp->cpu, iop->ifn);
1753 if (errno != EAGAIN || tp->is_done)
1756 if (!piped_output && --nevs == 0)
1763 incr_entries(nentries);
1768 static void *thread_main(void *arg)
1770 int ret, ndone, to_val;
1771 struct tracer *tp = arg;
1773 ret = lock_on_cpu(tp->cpu);
1782 to_val = 50; /* Frequent partial handles */
1784 to_val = 500; /* 1/2 second intervals */
1787 tracer_signal_ready(tp, Th_running, 0);
1788 tracer_wait_unblock(tp);
1790 while (!tp->is_done) {
1791 ndone = poll(tp->pfds, ndevs, to_val);
1792 if (ndone || piped_output)
1793 (void)handle_pfds(tp, ndone, piped_output);
1794 else if (ndone < 0 && errno != EINTR)
1795 fprintf(stderr, "Thread %d poll failed: %d/%s\n",
1796 tp->cpu, errno, strerror(errno));
1800 * Trace is stopped, pull data until we get a short read
1802 while (handle_pfds(tp, ndevs, 1) > 0)
1806 tracer_signal_ready(tp, Th_leaving, 0);
1810 tracer_signal_ready(tp, Th_error, ret);
1814 static int start_tracer(int cpu)
1818 tp = malloc(sizeof(*tp));
1819 memset(tp, 0, sizeof(*tp));
1821 INIT_LIST_HEAD(&tp->head);
1825 if (pthread_create(&tp->thread, NULL, thread_main, tp)) {
1826 fprintf(stderr, "FAILED to start thread on CPU %d: %d/%s\n",
1827 cpu, errno, strerror(errno));
1832 list_add_tail(&tp->head, &tracers);
1836 static void start_tracers(void)
1839 struct list_head *p;
1841 for (cpu = 0; cpu < ncpus; cpu++)
1842 if (start_tracer(cpu))
1845 wait_tracers_ready(cpu);
1847 __list_for_each(p, &tracers) {
1848 struct tracer *tp = list_entry(p, struct tracer, head);
1851 "FAILED to start thread on CPU %d: %d/%s\n",
1852 tp->cpu, tp->status, strerror(tp->status));
1856 static void stop_tracers(void)
1858 struct list_head *p;
1861 * Stop the tracing - makes the tracer threads clean up quicker.
1863 __list_for_each(p, &devpaths) {
1864 struct devpath *dpp = list_entry(p, struct devpath, head);
1865 (void)ioctl(dpp->fd, BLKTRACESTOP);
1869 * Tell each tracer to quit
1871 __list_for_each(p, &tracers) {
1872 struct tracer *tp = list_entry(p, struct tracer, head);
1877 static void del_tracers(void)
1879 struct list_head *p, *q;
1881 list_for_each_safe(p, q, &tracers) {
1882 struct tracer *tp = list_entry(p, struct tracer, head);
1884 list_del(&tp->head);
1889 static void wait_tracers(void)
1891 struct list_head *p;
1893 if (use_tracer_devpaths())
1894 process_trace_bufs();
1896 wait_tracers_leaving();
1898 __list_for_each(p, &tracers) {
1900 struct tracer *tp = list_entry(p, struct tracer, head);
1902 ret = pthread_join(tp->thread, NULL);
1904 fprintf(stderr, "Thread join %d failed %d\n",
1908 if (use_tracer_devpaths())
1914 static void exit_tracing(void)
1916 signal(SIGINT, SIG_IGN);
1917 signal(SIGHUP, SIG_IGN);
1918 signal(SIGTERM, SIG_IGN);
1919 signal(SIGALRM, SIG_IGN);
1927 static void handle_sigint(__attribute__((__unused__)) int sig)
1933 static void show_stats(struct list_head *devpaths)
1936 struct list_head *p;
1937 unsigned long long nevents, data_read;
1938 unsigned long long total_drops = 0;
1939 unsigned long long total_events = 0;
1942 ofp = my_fopen("/dev/null", "w");
1946 __list_for_each(p, devpaths) {
1948 struct pdc_stats *sp;
1949 struct devpath *dpp = list_entry(p, struct devpath, head);
1951 if (net_mode == Net_server)
1952 printf("server: end of run for %s:%s\n",
1953 dpp->ch->hostname, dpp->buts_name);
1958 fprintf(ofp, "=== %s ===\n", dpp->buts_name);
1959 for (cpu = 0, sp = dpp->stats; cpu < dpp->ncpus; cpu++, sp++) {
1961 * Estimate events if not known...
1963 if (sp->nevents == 0) {
1964 sp->nevents = sp->data_read /
1965 sizeof(struct blk_io_trace);
1969 " CPU%3d: %20llu events, %8llu KiB data\n",
1970 cpu, sp->nevents, (sp->data_read + 1023) >> 10);
1972 data_read += sp->data_read;
1973 nevents += sp->nevents;
1976 fprintf(ofp, " Total: %20llu events (dropped %llu),"
1977 " %8llu KiB data\n", nevents,
1978 dpp->drops, (data_read + 1024) >> 10);
1980 total_drops += dpp->drops;
1981 total_events += (nevents + dpp->drops);
1989 double drops_ratio = 1.0;
1992 drops_ratio = (double)total_drops/(double)total_events;
1994 fprintf(stderr, "\nYou have %llu (%5.1lf%%) dropped events\n"
1995 "Consider using a larger buffer size (-b) "
1996 "and/or more buffers (-n)\n",
1997 total_drops, 100.0 * drops_ratio);
2001 static int handle_args(int argc, char *argv[])
2005 int act_mask_tmp = 0;
2007 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
2010 i = find_mask_map(optarg);
2012 fprintf(stderr, "Invalid action mask %s\n",
2020 if ((sscanf(optarg, "%x", &i) != 1) ||
2021 !valid_act_opt(i)) {
2023 "Invalid set action mask %s/0x%x\n",
2031 if (add_devpath(optarg) != 0)
2037 FILE *ifp = my_fopen(optarg, "r");
2041 "Invalid file for devices %s\n",
2046 while (fscanf(ifp, "%s\n", dev_line) == 1)
2047 if (add_devpath(dev_line) != 0)
2053 debugfs_path = optarg;
2057 output_name = optarg;
2060 kill_running_trace = 1;
2063 stop_watch = atoi(optarg);
2064 if (stop_watch <= 0) {
2066 "Invalid stopwatch value (%d secs)\n",
2073 printf("%s version %s\n", argv[0], blktrace_version);
2077 buf_size = strtoul(optarg, NULL, 10);
2078 if (buf_size <= 0 || buf_size > 16*1024) {
2079 fprintf(stderr, "Invalid buffer size (%lu)\n",
2086 buf_nr = strtoul(optarg, NULL, 10);
2089 "Invalid buffer nr (%lu)\n", buf_nr);
2094 output_dir = optarg;
2097 net_mode = Net_client;
2098 strcpy(hostname, optarg);
2101 net_mode = Net_server;
2104 net_port = atoi(optarg);
2107 net_use_sendfile = 0;
2110 show_usage(argv[0]);
2116 while (optind < argc)
2117 if (add_devpath(argv[optind++]) != 0)
2120 if (net_mode != Net_server && ndevs == 0) {
2121 show_usage(argv[0]);
2125 if (statfs(debugfs_path, &st) < 0 || st.f_type != (long)DEBUGFS_TYPE) {
2126 fprintf(stderr, "Invalid debug path %s: %d/%s\n",
2127 debugfs_path, errno, strerror(errno));
2131 if (act_mask_tmp != 0)
2132 act_mask = act_mask_tmp;
2134 if (net_mode == Net_client && net_setup_addr())
2138 * Set up for appropriate PFD handler based upon output name.
2140 if (net_client_use_sendfile())
2141 handle_pfds = handle_pfds_netclient;
2142 else if (net_client_use_send())
2143 handle_pfds = handle_pfds_entries;
2144 else if (output_name && (strcmp(output_name, "-") == 0)) {
2146 handle_pfds = handle_pfds_entries;
2148 setvbuf(pfp, NULL, _IONBF, 0);
2150 handle_pfds = handle_pfds_file;
2154 static void ch_add_connection(struct net_server_s *ns, struct cl_host *ch,
2159 nc = malloc(sizeof(*nc));
2160 memset(nc, 0, sizeof(*nc));
2162 time(&nc->connect_time);
2167 list_add_tail(&nc->ch_head, &ch->conn_list);
2170 list_add_tail(&nc->ns_head, &ns->conn_list);
2172 ns->pfds = realloc(ns->pfds, (ns->connects+1) * sizeof(struct pollfd));
2175 static void ch_rem_connection(struct net_server_s *ns, struct cl_host *ch,
2178 net_close_connection(&nc->fd);
2180 list_del(&nc->ch_head);
2183 list_del(&nc->ns_head);
2185 ns->pfds = realloc(ns->pfds, (ns->connects+1) * sizeof(struct pollfd));
2190 static struct cl_host *net_find_client_host(struct net_server_s *ns,
2191 struct in_addr cl_in_addr)
2193 struct list_head *p;
2195 __list_for_each(p, &ns->ch_list) {
2196 struct cl_host *ch = list_entry(p, struct cl_host, head);
2198 if (in_addr_eq(ch->cl_in_addr, cl_in_addr))
2205 static struct cl_host *net_add_client_host(struct net_server_s *ns,
2206 struct sockaddr_in *addr)
2210 ch = malloc(sizeof(*ch));
2211 memset(ch, 0, sizeof(*ch));
2214 ch->cl_in_addr = addr->sin_addr;
2215 list_add_tail(&ch->head, &ns->ch_list);
2218 ch->hostname = strdup(inet_ntoa(addr->sin_addr));
2219 printf("server: connection from %s\n", ch->hostname);
2221 INIT_LIST_HEAD(&ch->conn_list);
2222 INIT_LIST_HEAD(&ch->devpaths);
2227 static void device_done(struct devpath *dpp, int ncpus)
2230 struct io_info *iop;
2232 for (cpu = 0, iop = dpp->ios; cpu < ncpus; cpu++, iop++)
2235 list_del(&dpp->head);
2239 static void net_ch_remove(struct cl_host *ch, int ncpus)
2241 struct list_head *p, *q;
2242 struct net_server_s *ns = ch->ns;
2244 list_for_each_safe(p, q, &ch->devpaths) {
2245 struct devpath *dpp = list_entry(p, struct devpath, head);
2246 device_done(dpp, ncpus);
2249 list_for_each_safe(p, q, &ch->conn_list) {
2250 struct cl_conn *nc = list_entry(p, struct cl_conn, ch_head);
2252 ch_rem_connection(ns, ch, nc);
2255 list_del(&ch->head);
2263 static void net_add_connection(struct net_server_s *ns)
2267 socklen_t socklen = sizeof(ns->addr);
2269 fd = my_accept(ns->listen_fd, (struct sockaddr *)&ns->addr, &socklen);
2272 * This is OK: we just won't accept this connection,
2277 ch = net_find_client_host(ns, ns->addr.sin_addr);
2279 ch = net_add_client_host(ns, &ns->addr);
2281 ch_add_connection(ns, ch, fd);
2285 static struct devpath *nc_add_dpp(struct cl_conn *nc,
2286 struct blktrace_net_hdr *bnh,
2287 time_t connect_time)
2290 struct io_info *iop;
2291 struct devpath *dpp;
2293 dpp = malloc(sizeof(*dpp));
2294 memset(dpp, 0, sizeof(*dpp));
2296 dpp->buts_name = strdup(bnh->buts_name);
2297 dpp->path = strdup(bnh->buts_name);
2300 dpp->cl_id = bnh->cl_id;
2301 dpp->cl_connect_time = connect_time;
2302 dpp->ncpus = nc->ncpus;
2303 dpp->stats = calloc(dpp->ncpus, sizeof(*dpp->stats));
2304 memset(dpp->stats, 0, dpp->ncpus * sizeof(*dpp->stats));
2306 list_add_tail(&dpp->head, &nc->ch->devpaths);
2309 dpp->ios = calloc(nc->ncpus, sizeof(*iop));
2310 memset(dpp->ios, 0, ndevs * sizeof(*iop));
2312 for (cpu = 0, iop = dpp->ios; cpu < nc->ncpus; cpu++, iop++) {
2315 init_mmap_info(&iop->mmap_info);
2317 if (iop_open(iop, cpu))
2325 * Need to unravel what's been done...
2328 close_iop(&dpp->ios[cpu--]);
2334 static struct devpath *nc_find_dpp(struct cl_conn *nc,
2335 struct blktrace_net_hdr *bnh)
2337 struct list_head *p;
2338 time_t connect_time = nc->connect_time;
2340 __list_for_each(p, &nc->ch->devpaths) {
2341 struct devpath *dpp = list_entry(p, struct devpath, head);
2343 if (!strcmp(dpp->buts_name, bnh->buts_name))
2346 if (dpp->cl_id == bnh->cl_id)
2347 connect_time = dpp->cl_connect_time;
2350 return nc_add_dpp(nc, bnh, connect_time);
2353 static void net_client_read_data(struct cl_conn *nc, struct devpath *dpp,
2354 struct blktrace_net_hdr *bnh)
2357 struct io_info *iop = &dpp->ios[bnh->cpu];
2358 struct mmap_info *mip = &iop->mmap_info;
2360 if (setup_mmap(iop->ofd, bnh->len, &iop->mmap_info)) {
2361 fprintf(stderr, "ncd(%s:%d): mmap failed\n",
2362 nc->ch->hostname, nc->fd);
2366 ret = net_recv_data(nc->fd, mip->fs_buf + mip->fs_off, bnh->len);
2368 pdc_dr_update(dpp, bnh->cpu, ret);
2369 mip->fs_size += ret;
2376 * Returns 1 if we closed a host - invalidates other polling information
2377 * that may be present.
2379 static int net_client_data(struct cl_conn *nc)
2382 struct devpath *dpp;
2383 struct blktrace_net_hdr bnh;
2385 ret = net_get_header(nc, &bnh);
2390 fprintf(stderr, "ncd(%d): header read failed\n", nc->fd);
2394 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
2395 fprintf(stderr, "ncd(%d): received data is bad\n", nc->fd);
2399 if (!data_is_native) {
2400 bnh.magic = be32_to_cpu(bnh.magic);
2401 bnh.cpu = be32_to_cpu(bnh.cpu);
2402 bnh.max_cpus = be32_to_cpu(bnh.max_cpus);
2403 bnh.len = be32_to_cpu(bnh.len);
2404 bnh.cl_id = be32_to_cpu(bnh.cl_id);
2405 bnh.buf_size = be32_to_cpu(bnh.buf_size);
2406 bnh.buf_nr = be32_to_cpu(bnh.buf_nr);
2407 bnh.page_size = be32_to_cpu(bnh.page_size);
2410 if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2411 fprintf(stderr, "ncd(%s:%d): bad data magic\n",
2412 nc->ch->hostname, nc->fd);
2416 if (nc->ncpus == -1)
2417 nc->ncpus = bnh.max_cpus;
2420 * len == 0 means the other end is sending us a new connection/dpp
2421 * len == 1 means that the other end signalled end-of-run
2423 dpp = nc_find_dpp(nc, &bnh);
2426 * Just adding in the dpp above is enough
2428 ack_open_close(nc->fd, dpp->buts_name);
2430 } else if (bnh.len == 1) {
2432 * overload cpu count with dropped events
2434 dpp->drops = bnh.cpu;
2436 ack_open_close(nc->fd, dpp->buts_name);
2437 if (--nc->ch->cl_opens == 0) {
2438 show_stats(&nc->ch->devpaths);
2439 net_ch_remove(nc->ch, nc->ncpus);
2443 net_client_read_data(nc, dpp, &bnh);
2448 static void handle_client_data(struct net_server_s *ns, int events)
2452 struct list_head *p, *q;
2455 list_for_each_safe(p, q, &ns->conn_list) {
2456 if (pfd->revents & POLLIN) {
2457 nc = list_entry(p, struct cl_conn, ns_head);
2459 if (net_client_data(nc) || --events == 0)
2466 static void net_setup_pfds(struct net_server_s *ns)
2469 struct list_head *p;
2471 ns->pfds[0].fd = ns->listen_fd;
2472 ns->pfds[0].events = POLLIN;
2475 __list_for_each(p, &ns->conn_list) {
2476 struct cl_conn *nc = list_entry(p, struct cl_conn, ns_head);
2479 pfd->events = POLLIN;
2484 static int net_server_handle_connections(struct net_server_s *ns)
2488 printf("server: waiting for connections...\n");
2492 events = poll(ns->pfds, ns->connects + 1, -1);
2494 if (errno != EINTR) {
2495 perror("FATAL: poll error");
2498 } else if (events > 0) {
2499 if (ns->pfds[0].revents & POLLIN) {
2500 net_add_connection(ns);
2505 handle_client_data(ns, events);
2512 static int net_server(void)
2516 struct net_server_s net_server;
2517 struct net_server_s *ns = &net_server;
2519 memset(ns, 0, sizeof(*ns));
2520 INIT_LIST_HEAD(&ns->ch_list);
2521 INIT_LIST_HEAD(&ns->conn_list);
2522 ns->pfds = malloc(sizeof(struct pollfd));
2524 fd = my_socket(AF_INET, SOCK_STREAM, 0);
2526 perror("server: socket");
2531 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
2532 perror("setsockopt");
2536 memset(&ns->addr, 0, sizeof(ns->addr));
2537 ns->addr.sin_family = AF_INET;
2538 ns->addr.sin_addr.s_addr = htonl(INADDR_ANY);
2539 ns->addr.sin_port = htons(net_port);
2541 if (bind(fd, (struct sockaddr *) &ns->addr, sizeof(ns->addr)) < 0) {
2546 if (listen(fd, 1) < 0) {
2552 * The actual server looping is done here:
2555 ret = net_server_handle_connections(ns);
2558 * Clean up and return...
2565 static int run_tracers(void)
2567 atexit(exit_tracing);
2568 if (net_mode == Net_client)
2569 printf("blktrace: connecting to %s\n", hostname);
2573 if (use_tracer_devpaths()) {
2574 if (setup_tracer_devpaths())
2578 handle_list = handle_list_file;
2580 handle_list = handle_list_net;
2584 if (nthreads_running == ncpus) {
2587 if (net_mode == Net_client)
2588 printf("blktrace: connected!\n");
2595 if (nthreads_running == ncpus)
2596 show_stats(&devpaths);
2597 if (net_client_use_send())
2598 close_client_connections();
2604 int main(int argc, char *argv[])
2608 setlocale(LC_NUMERIC, "en_US");
2609 pagesize = getpagesize();
2610 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
2612 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed %d/%s\n",
2613 errno, strerror(errno));
2616 } else if (handle_args(argc, argv)) {
2621 signal(SIGINT, handle_sigint);
2622 signal(SIGHUP, handle_sigint);
2623 signal(SIGTERM, handle_sigint);
2624 signal(SIGALRM, handle_sigint);
2625 signal(SIGPIPE, SIG_IGN);
2627 if (kill_running_trace) {
2628 struct devpath *dpp;
2629 struct list_head *p;
2631 __list_for_each(p, &devpaths) {
2632 dpp = list_entry(p, struct devpath, head);
2633 if (__stop_trace(dpp->fd)) {
2635 "BLKTRACETEARDOWN %s failed: %d/%s\n",
2636 dpp->path, errno, strerror(errno));
2639 } else if (net_mode == Net_server) {
2641 fprintf(stderr, "-o ignored in server mode\n");
2646 ret = run_tracers();