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;
115 int setup_done; /* ioctl BLKTRACESETUP done */
120 * For piped output to stdout we will have each tracer thread (one per dev)
121 * tack buffers read from the relay queues on a per-device list.
123 * The main thread will then collect trace buffers from each of lists in turn.
125 * We will use a mutex to guard each of the trace_buf list. The tracers
126 * can then signal the main thread using <dp_cond,dp_mutex> and
127 * dp_entries. (When dp_entries is 0, and a tracer adds an entry it will
128 * signal. When dp_entries is 0, the main thread will wait for that condition
131 * adb: It may be better just to have a large buffer per tracer per dev,
132 * and then use it as a ring-buffer. This would certainly cut down a lot
133 * of malloc/free thrashing, at the cost of more memory movements (potentially).
136 struct list_head head;
142 struct tracer_devpath_head {
143 pthread_mutex_t mutex;
144 struct list_head head;
145 struct trace_buf *prev;
149 * Used to handle the mmap() interfaces for output file (containing traces)
153 unsigned long long fs_size, fs_max_size, fs_off, fs_buf_len;
154 unsigned long buf_size, buf_nr;
159 * Each thread doing work on a (client) side of blktrace will have one
160 * of these. The ios array contains input/output information, pfds holds
161 * poll() data. The volatile's provide flags to/from the main executing
165 struct list_head head;
170 volatile int status, is_done;
174 * networking stuff follows. we include a magic number so we know whether
175 * to endianness convert or not.
177 * The len field is overloaded:
178 * 0 - Indicates an "open" - allowing the server to set up for a dev/cpu
179 * 1 - Indicates a "close" - Shut down connection orderly
181 * The cpu field is overloaded on close: it will contain the number of drops.
183 struct blktrace_net_hdr {
184 u32 magic; /* same as trace magic */
185 char buts_name[32]; /* trace name */
186 u32 cpu; /* for which cpu */
188 u32 len; /* length of following trace data */
189 u32 cl_id; /* id for set of client per-cpu connections */
190 u32 buf_size; /* client buf_size for this trace */
191 u32 buf_nr; /* client buf_nr for this trace */
192 u32 page_size; /* client page_size for this trace */
196 * Each host encountered has one of these. The head is used to link this
197 * on to the network server's ch_list. Connections associated with this
198 * host are linked on conn_list, and any devices traced on that host
199 * are connected on the devpaths list.
202 struct list_head head;
203 struct list_head conn_list;
204 struct list_head devpaths;
205 struct net_server_s *ns;
207 struct in_addr cl_in_addr;
208 int connects, ndevs, cl_opens;
212 * Each connection (client to server socket ('fd')) has one of these. A
213 * back reference to the host ('ch'), and lists headers (for the host
214 * list, and the network server conn_list) are also included.
217 struct list_head ch_head, ns_head;
224 * The network server requires some poll structures to be maintained -
225 * one per conection currently on conn_list. The nchs/ch_list values
226 * are for each host connected to this server. The addr field is used
227 * for scratch as new connections are established.
229 struct net_server_s {
230 struct list_head conn_list;
231 struct list_head ch_list;
233 int listen_fd, connects, nchs;
234 struct sockaddr_in addr;
238 * This structure is (generically) used to providide information
239 * for a read-to-write set of values.
241 * ifn & ifd represent input information
243 * ofn, ofd, ofp, obuf & mmap_info are used for output file (optionally).
249 struct cl_conn *nc; /* Server network connection */
252 * mmap controlled output files
254 struct mmap_info mmap_info;
257 * Client network fields
260 unsigned long long data_queued;
263 * Input/output file descriptors & names
266 char ifn[MAXPATHLEN + 64];
267 char ofn[MAXPATHLEN + 64];
270 static char blktrace_version[] = "2.0.0";
273 * Linkage to blktrace helper routines (trace conversions)
275 int data_is_native = -1;
280 static cpu_set_t *online_cpus;
282 static int act_mask = ~0U;
283 static int kill_running_trace;
284 static int stop_watch;
285 static int piped_output;
287 static char *debugfs_path = "/sys/kernel/debug";
288 static char *output_name;
289 static char *output_dir;
291 static unsigned long buf_size = BUF_SIZE;
292 static unsigned long buf_nr = BUF_NR;
296 static LIST_HEAD(devpaths);
297 static LIST_HEAD(tracers);
299 static volatile int done;
302 * tracer threads add entries, the main thread takes them off and processes
303 * them. These protect the dp_entries variable.
305 static pthread_cond_t dp_cond = PTHREAD_COND_INITIALIZER;
306 static pthread_mutex_t dp_mutex = PTHREAD_MUTEX_INITIALIZER;
307 static volatile int dp_entries;
310 * These synchronize master / thread interactions.
312 static pthread_cond_t mt_cond = PTHREAD_COND_INITIALIZER;
313 static pthread_mutex_t mt_mutex = PTHREAD_MUTEX_INITIALIZER;
314 static volatile int nthreads_running;
315 static volatile int nthreads_leaving;
316 static volatile int nthreads_error;
317 static volatile int tracers_run;
320 * network cmd line params
322 static struct sockaddr_in hostname_addr;
323 static char hostname[MAXHOSTNAMELEN];
324 static int net_port = TRACE_NET_PORT;
325 static int net_use_sendfile = 1;
329 static int (*handle_pfds)(struct tracer *, int, int);
330 static int (*handle_list)(struct tracer_devpath_head *, struct list_head *);
332 #define S_OPTS "d:a:A:r:o:kw:vVb:n:D:lh:p:sI:"
333 static struct option l_opts[] = {
336 .has_arg = required_argument,
341 .name = "input-devs",
342 .has_arg = required_argument,
348 .has_arg = required_argument,
354 .has_arg = required_argument,
360 .has_arg = required_argument,
366 .has_arg = required_argument,
372 .has_arg = no_argument,
378 .has_arg = required_argument,
384 .has_arg = no_argument,
390 .has_arg = no_argument,
395 .name = "buffer-size",
396 .has_arg = required_argument,
401 .name = "num-sub-buffers",
402 .has_arg = required_argument,
407 .name = "output-dir",
408 .has_arg = required_argument,
414 .has_arg = no_argument,
420 .has_arg = required_argument,
426 .has_arg = required_argument,
431 .name = "no-sendfile",
432 .has_arg = no_argument,
441 static char usage_str[] = "\n\n" \
442 "-d <dev> | --dev=<dev>\n" \
443 "[ -r <debugfs path> | --relay=<debugfs path> ]\n" \
444 "[ -o <file> | --output=<file>]\n" \
445 "[ -D <dir> | --output-dir=<dir>\n" \
446 "[ -w <time> | --stopwatch=<time>]\n" \
447 "[ -a <action field> | --act-mask=<action field>]\n" \
448 "[ -A <action mask> | --set-mask=<action mask>]\n" \
449 "[ -b <size> | --buffer-size]\n" \
450 "[ -n <number> | --num-sub-buffers=<number>]\n" \
451 "[ -l | --listen]\n" \
452 "[ -h <hostname> | --host=<hostname>]\n" \
453 "[ -p <port number> | --port=<port number>]\n" \
454 "[ -s | --no-sendfile]\n" \
455 "[ -I <devs file> | --input-devs=<devs file>]\n" \
456 "[ -v <version> | --version]\n" \
457 "[ -V <version> | --version]\n" \
459 "\t-d Use specified device. May also be given last after options\n" \
460 "\t-r Path to mounted debugfs, defaults to /sys/kernel/debug\n" \
461 "\t-o File(s) to send output to\n" \
462 "\t-D Directory to prepend to output file names\n" \
463 "\t-w Stop after defined time, in seconds\n" \
464 "\t-a Only trace specified actions. See documentation\n" \
465 "\t-A Give trace mask as a single value. See documentation\n" \
466 "\t-b Sub buffer size in KiB (default 512)\n" \
467 "\t-n Number of sub buffers (default 4)\n" \
468 "\t-l Run in network listen mode (blktrace server)\n" \
469 "\t-h Run in network client mode, connecting to the given host\n" \
470 "\t-p Network port to use (default 8462)\n" \
471 "\t-s Make the network client NOT use sendfile() to transfer data\n" \
472 "\t-I Add devices found in <devs file>\n" \
473 "\t-v Print program version info\n" \
474 "\t-V Print program version info\n\n";
476 static void clear_events(struct pollfd *pfd)
482 static inline int net_client_use_sendfile(void)
484 return net_mode == Net_client && net_use_sendfile;
487 static inline int net_client_use_send(void)
489 return net_mode == Net_client && !net_use_sendfile;
492 static inline int use_tracer_devpaths(void)
494 return piped_output || net_client_use_send();
497 static inline int in_addr_eq(struct in_addr a, struct in_addr b)
499 return a.s_addr == b.s_addr;
502 static inline void pdc_dr_update(struct devpath *dpp, int cpu, int data_read)
504 dpp->stats[cpu].data_read += data_read;
507 static inline void pdc_nev_update(struct devpath *dpp, int cpu, int nevents)
509 dpp->stats[cpu].nevents += nevents;
512 static void show_usage(char *prog)
514 fprintf(stderr, "Usage: %s %s", prog, usage_str);
518 * Create a timespec 'msec' milliseconds into the future
520 static inline void make_timespec(struct timespec *tsp, long delta_msec)
524 gettimeofday(&now, NULL);
525 tsp->tv_sec = now.tv_sec;
526 tsp->tv_nsec = 1000L * now.tv_usec;
528 tsp->tv_nsec += (delta_msec * 1000000L);
529 if (tsp->tv_nsec > 1000000000L) {
530 long secs = tsp->tv_nsec / 1000000000L;
533 tsp->tv_nsec -= (secs * 1000000000L);
538 * Add a timer to ensure wait ends
540 static void t_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
544 make_timespec(&ts, 50);
545 pthread_cond_timedwait(cond, mutex, &ts);
548 static void unblock_tracers(void)
550 pthread_mutex_lock(&mt_mutex);
552 pthread_cond_broadcast(&mt_cond);
553 pthread_mutex_unlock(&mt_mutex);
556 static void tracer_wait_unblock(struct tracer *tp)
558 pthread_mutex_lock(&mt_mutex);
559 while (!tp->is_done && !tracers_run)
560 pthread_cond_wait(&mt_cond, &mt_mutex);
561 pthread_mutex_unlock(&mt_mutex);
564 static void tracer_signal_ready(struct tracer *tp,
565 enum thread_status th_status,
568 pthread_mutex_lock(&mt_mutex);
571 if (th_status == Th_running)
573 else if (th_status == Th_error)
578 pthread_cond_signal(&mt_cond);
579 pthread_mutex_unlock(&mt_mutex);
582 static void wait_tracers_ready(int ncpus_started)
584 pthread_mutex_lock(&mt_mutex);
585 while ((nthreads_running + nthreads_error) < ncpus_started)
586 t_pthread_cond_wait(&mt_cond, &mt_mutex);
587 pthread_mutex_unlock(&mt_mutex);
590 static void wait_tracers_leaving(void)
592 pthread_mutex_lock(&mt_mutex);
593 while (nthreads_leaving < nthreads_running)
594 t_pthread_cond_wait(&mt_cond, &mt_mutex);
595 pthread_mutex_unlock(&mt_mutex);
598 static void init_mmap_info(struct mmap_info *mip)
600 mip->buf_size = buf_size;
601 mip->buf_nr = buf_nr;
602 mip->pagesize = pagesize;
605 static void net_close_connection(int *fd)
607 shutdown(*fd, SHUT_RDWR);
612 static void dpp_free(struct devpath *dpp)
621 free(dpp->buts_name);
625 static int lock_on_cpu(int cpu)
627 cpu_set_t * cpu_mask;
630 cpu_mask = CPU_ALLOC(max_cpus);
631 size = CPU_ALLOC_SIZE(max_cpus);
633 CPU_ZERO_S(size, cpu_mask);
634 CPU_SET_S(cpu, size, cpu_mask);
635 if (sched_setaffinity(0, size, cpu_mask) < 0) {
644 static int increase_limit(int resource, rlim_t increase)
647 int save_errno = errno;
649 if (!getrlimit(resource, &rlim)) {
650 rlim.rlim_cur += increase;
651 if (rlim.rlim_cur >= rlim.rlim_max)
652 rlim.rlim_max = rlim.rlim_cur + increase;
654 if (!setrlimit(resource, &rlim))
662 static int handle_open_failure(void)
664 if (errno == ENFILE || errno == EMFILE)
665 return increase_limit(RLIMIT_NOFILE, 16);
669 static int handle_mem_failure(size_t length)
672 return handle_open_failure();
673 else if (errno == ENOMEM)
674 return increase_limit(RLIMIT_MEMLOCK, 2 * length);
678 static FILE *my_fopen(const char *path, const char *mode)
683 fp = fopen(path, mode);
684 } while (fp == NULL && handle_open_failure());
689 static int my_open(const char *path, int flags)
694 fd = open(path, flags);
695 } while (fd < 0 && handle_open_failure());
700 static int my_socket(int domain, int type, int protocol)
705 fd = socket(domain, type, protocol);
706 } while (fd < 0 && handle_open_failure());
711 static int my_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
716 fd = accept(sockfd, addr, addrlen);
717 } while (fd < 0 && handle_open_failure());
722 static void *my_mmap(void *addr, size_t length, int prot, int flags, int fd,
728 new = mmap(addr, length, prot, flags, fd, offset);
729 } while (new == MAP_FAILED && handle_mem_failure(length));
734 static int my_mlock(struct tracer *tp,
735 const void *addr, size_t len)
740 ret = mlock(addr, len);
741 if ((retry >= 10) && tp && tp->is_done)
744 } while (ret < 0 && handle_mem_failure(len));
749 static int setup_mmap(int fd, unsigned int maxlen,
750 struct mmap_info *mip,
753 if (mip->fs_off + maxlen > mip->fs_buf_len) {
754 unsigned long nr = max(16, mip->buf_nr);
757 munlock(mip->fs_buf, mip->fs_buf_len);
758 munmap(mip->fs_buf, mip->fs_buf_len);
762 mip->fs_off = mip->fs_size & (mip->pagesize - 1);
763 mip->fs_buf_len = (nr * mip->buf_size) - mip->fs_off;
764 mip->fs_max_size += mip->fs_buf_len;
766 if (ftruncate(fd, mip->fs_max_size) < 0) {
767 perror("setup_mmap: ftruncate");
771 mip->fs_buf = my_mmap(NULL, mip->fs_buf_len, PROT_WRITE,
773 mip->fs_size - mip->fs_off);
774 if (mip->fs_buf == MAP_FAILED) {
775 perror("setup_mmap: mmap");
778 if (my_mlock(tp, mip->fs_buf, mip->fs_buf_len) < 0) {
779 perror("setup_mlock: mlock");
787 static int __stop_trace(int fd)
790 * Should be stopped, don't complain if it isn't
792 ioctl(fd, BLKTRACESTOP);
793 return ioctl(fd, BLKTRACETEARDOWN);
796 static int write_data(char *buf, int len)
801 ret = fwrite(buf, len, 1, pfp);
802 if (ferror(pfp) || ret != 1) {
803 if (errno == EINTR) {
808 if (!piped_output || (errno != EPIPE && errno != EBADF)) {
809 fprintf(stderr, "write(%d) failed: %d/%s\n",
810 len, errno, strerror(errno));
824 * Returns the number of bytes read (successfully)
826 static int __net_recv_data(int fd, void *buf, unsigned int len)
828 unsigned int bytes_left = len;
830 while (bytes_left && !done) {
831 int ret = recv(fd, buf, bytes_left, MSG_WAITALL);
836 if (errno == EAGAIN) {
840 perror("server: net_recv_data: recv failed");
848 return len - bytes_left;
851 static int net_recv_data(int fd, void *buf, unsigned int len)
853 return __net_recv_data(fd, buf, len);
857 * Returns number of bytes written
859 static int net_send_data(int fd, void *buf, unsigned int buf_len)
862 unsigned int bytes_left = buf_len;
865 ret = send(fd, buf, bytes_left, 0);
875 return buf_len - bytes_left;
878 static int net_send_header(int fd, int cpu, char *buts_name, int len)
880 struct blktrace_net_hdr hdr;
882 memset(&hdr, 0, sizeof(hdr));
884 hdr.magic = BLK_IO_TRACE_MAGIC;
885 memset(hdr.buts_name, 0, sizeof(hdr.buts_name));
886 strncpy(hdr.buts_name, buts_name, sizeof(hdr.buts_name));
887 hdr.buts_name[sizeof(hdr.buts_name) - 1] = '\0';
889 hdr.max_cpus = max_cpus;
891 hdr.cl_id = getpid();
892 hdr.buf_size = buf_size;
894 hdr.page_size = pagesize;
896 return net_send_data(fd, &hdr, sizeof(hdr)) != sizeof(hdr);
899 static void net_send_open_close(int fd, int cpu, char *buts_name, int len)
901 struct blktrace_net_hdr ret_hdr;
903 net_send_header(fd, cpu, buts_name, len);
904 net_recv_data(fd, &ret_hdr, sizeof(ret_hdr));
907 static void net_send_open(int fd, int cpu, char *buts_name)
909 net_send_open_close(fd, cpu, buts_name, 0);
912 static void net_send_close(int fd, char *buts_name, int drops)
915 * Overload CPU w/ number of drops
917 * XXX: Need to clear/set done around call - done=1 (which
918 * is true here) stops reads from happening... :-(
921 net_send_open_close(fd, drops, buts_name, 1);
925 static void ack_open_close(int fd, char *buts_name)
927 net_send_header(fd, 0, buts_name, 2);
930 static void net_send_drops(int fd)
934 __list_for_each(p, &devpaths) {
935 struct devpath *dpp = list_entry(p, struct devpath, head);
937 net_send_close(fd, dpp->buts_name, dpp->drops);
947 static int net_get_header(struct cl_conn *nc, struct blktrace_net_hdr *bnh)
950 int fl = fcntl(nc->fd, F_GETFL);
952 fcntl(nc->fd, F_SETFL, fl | O_NONBLOCK);
953 bytes_read = __net_recv_data(nc->fd, bnh, sizeof(*bnh));
954 fcntl(nc->fd, F_SETFL, fl & ~O_NONBLOCK);
956 if (bytes_read == sizeof(*bnh))
958 else if (bytes_read == 0)
964 static int net_setup_addr(void)
966 struct sockaddr_in *addr = &hostname_addr;
968 memset(addr, 0, sizeof(*addr));
969 addr->sin_family = AF_INET;
970 addr->sin_port = htons(net_port);
972 if (inet_aton(hostname, &addr->sin_addr) != 1) {
973 struct hostent *hent;
975 hent = gethostbyname(hostname);
977 if (h_errno == TRY_AGAIN) {
980 } else if (h_errno == NO_RECOVERY) {
981 fprintf(stderr, "gethostbyname(%s)"
982 "non-recoverable error encountered\n",
986 * HOST_NOT_FOUND, NO_ADDRESS or NO_DATA
988 fprintf(stderr, "Host %s not found\n",
994 memcpy(&addr->sin_addr, hent->h_addr, 4);
995 memset(hostname, 0, sizeof(hostname));
996 strncpy(hostname, hent->h_name, sizeof(hostname));
997 hostname[sizeof(hostname) - 1] = '\0';
1003 static int net_setup_client(void)
1006 struct sockaddr_in *addr = &hostname_addr;
1008 fd = my_socket(AF_INET, SOCK_STREAM, 0);
1010 perror("client: socket");
1014 if (connect(fd, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
1015 if (errno == ECONNREFUSED)
1017 "\nclient: Connection to %s refused, "
1018 "perhaps the server is not started?\n\n",
1021 perror("client: connect");
1030 static int open_client_connections(void)
1033 size_t alloc_size = CPU_ALLOC_SIZE(max_cpus);
1035 cl_fds = calloc(ncpus, sizeof(*cl_fds));
1036 for (cpu = 0; cpu < max_cpus; cpu++) {
1037 if (!CPU_ISSET_S(cpu, alloc_size, online_cpus))
1039 cl_fds[cpu] = net_setup_client();
1040 if (cl_fds[cpu] < 0)
1047 close(cl_fds[cpu--]);
1052 static void close_client_connections(void)
1056 size_t alloc_size = CPU_ALLOC_SIZE(max_cpus);
1058 for (cpu = 0, fdp = cl_fds; cpu < max_cpus; cpu++, fdp++) {
1059 if (!CPU_ISSET_S(cpu, alloc_size, online_cpus))
1062 net_send_drops(*fdp);
1063 net_close_connection(fdp);
1070 static int setup_buts(void)
1072 struct list_head *p;
1075 __list_for_each(p, &devpaths) {
1076 struct blk_user_trace_setup buts;
1077 struct devpath *dpp = list_entry(p, struct devpath, head);
1079 memset(&buts, 0, sizeof(buts));
1080 buts.buf_size = buf_size;
1081 buts.buf_nr = buf_nr;
1082 buts.act_mask = act_mask;
1084 if (ioctl(dpp->fd, BLKTRACESETUP, &buts) >= 0) {
1085 dpp->ncpus = max_cpus;
1086 dpp->buts_name = strdup(buts.name);
1087 dpp->setup_done = 1;
1090 dpp->stats = calloc(dpp->ncpus, sizeof(*dpp->stats));
1091 memset(dpp->stats, 0, dpp->ncpus * sizeof(*dpp->stats));
1093 fprintf(stderr, "BLKTRACESETUP(2) %s failed: %d/%s\n",
1094 dpp->path, errno, strerror(errno));
1102 static void start_buts(void)
1104 struct list_head *p;
1106 __list_for_each(p, &devpaths) {
1107 struct devpath *dpp = list_entry(p, struct devpath, head);
1109 if (ioctl(dpp->fd, BLKTRACESTART) < 0) {
1110 fprintf(stderr, "BLKTRACESTART %s failed: %d/%s\n",
1111 dpp->path, errno, strerror(errno));
1116 static int get_drops(struct devpath *dpp)
1119 char fn[MAXPATHLEN + 64], tmp[256];
1121 snprintf(fn, sizeof(fn), "%s/block/%s/dropped", debugfs_path,
1124 fd = my_open(fn, O_RDONLY);
1127 * This may be ok: the kernel may not support
1130 if (errno != ENOENT)
1131 fprintf(stderr, "Could not open %s: %d/%s\n",
1132 fn, errno, strerror(errno));
1134 } else if (read(fd, tmp, sizeof(tmp)) < 0) {
1135 fprintf(stderr, "Could not read %s: %d/%s\n",
1136 fn, errno, strerror(errno));
1144 static void get_all_drops(void)
1146 struct list_head *p;
1148 __list_for_each(p, &devpaths) {
1149 struct devpath *dpp = list_entry(p, struct devpath, head);
1151 dpp->drops = get_drops(dpp);
1155 static inline struct trace_buf *alloc_trace_buf(int cpu, int bufsize)
1157 struct trace_buf *tbp;
1159 tbp = malloc(sizeof(*tbp) + bufsize);
1160 INIT_LIST_HEAD(&tbp->head);
1162 tbp->buf = (void *)(tbp + 1);
1164 tbp->dpp = NULL; /* Will be set when tbp is added */
1169 static void free_tracer_heads(struct devpath *dpp)
1172 struct tracer_devpath_head *hd;
1174 for (cpu = 0, hd = dpp->heads; cpu < max_cpus; cpu++, hd++) {
1178 pthread_mutex_destroy(&hd->mutex);
1183 static int setup_tracer_devpaths(void)
1185 struct list_head *p;
1187 if (net_client_use_send())
1188 if (open_client_connections())
1191 __list_for_each(p, &devpaths) {
1193 struct tracer_devpath_head *hd;
1194 struct devpath *dpp = list_entry(p, struct devpath, head);
1196 dpp->heads = calloc(max_cpus, sizeof(struct tracer_devpath_head));
1197 for (cpu = 0, hd = dpp->heads; cpu < max_cpus; cpu++, hd++) {
1198 INIT_LIST_HEAD(&hd->head);
1199 pthread_mutex_init(&hd->mutex, NULL);
1207 static inline void add_trace_buf(struct devpath *dpp, int cpu,
1208 struct trace_buf **tbpp)
1210 struct trace_buf *tbp = *tbpp;
1211 struct tracer_devpath_head *hd = &dpp->heads[cpu];
1215 pthread_mutex_lock(&hd->mutex);
1216 list_add_tail(&tbp->head, &hd->head);
1217 pthread_mutex_unlock(&hd->mutex);
1219 *tbpp = alloc_trace_buf(cpu, buf_size);
1222 static inline void incr_entries(int entries_handled)
1224 pthread_mutex_lock(&dp_mutex);
1225 if (dp_entries == 0)
1226 pthread_cond_signal(&dp_cond);
1227 dp_entries += entries_handled;
1228 pthread_mutex_unlock(&dp_mutex);
1231 static void decr_entries(int handled)
1233 pthread_mutex_lock(&dp_mutex);
1234 dp_entries -= handled;
1235 pthread_mutex_unlock(&dp_mutex);
1238 static int wait_empty_entries(void)
1240 pthread_mutex_lock(&dp_mutex);
1241 while (!done && dp_entries == 0)
1242 t_pthread_cond_wait(&dp_cond, &dp_mutex);
1243 pthread_mutex_unlock(&dp_mutex);
1248 static int add_devpath(char *path)
1251 struct devpath *dpp;
1252 struct list_head *p;
1255 * Verify device is not duplicated
1257 __list_for_each(p, &devpaths) {
1258 struct devpath *tmp = list_entry(p, struct devpath, head);
1259 if (!strcmp(tmp->path, path))
1263 * Verify device is valid before going too far
1265 fd = my_open(path, O_RDONLY | O_NONBLOCK);
1267 fprintf(stderr, "Invalid path %s specified: %d/%s\n",
1268 path, errno, strerror(errno));
1272 dpp = malloc(sizeof(*dpp));
1273 memset(dpp, 0, sizeof(*dpp));
1274 dpp->path = strdup(path);
1277 list_add_tail(&dpp->head, &devpaths);
1282 static void rel_devpaths(void)
1284 struct list_head *p, *q;
1286 list_for_each_safe(p, q, &devpaths) {
1287 struct devpath *dpp = list_entry(p, struct devpath, head);
1289 list_del(&dpp->head);
1290 if (dpp->setup_done)
1291 __stop_trace(dpp->fd);
1295 free_tracer_heads(dpp);
1302 static int flush_subbuf_net(struct trace_buf *tbp)
1304 int fd = cl_fds[tbp->cpu];
1305 struct devpath *dpp = tbp->dpp;
1307 if (net_send_header(fd, tbp->cpu, dpp->buts_name, tbp->len))
1309 else if (net_send_data(fd, tbp->buf, tbp->len) != tbp->len)
1316 handle_list_net(__attribute__((__unused__))struct tracer_devpath_head *hd,
1317 struct list_head *list)
1319 struct trace_buf *tbp;
1320 struct list_head *p, *q;
1321 int entries_handled = 0;
1323 list_for_each_safe(p, q, list) {
1324 tbp = list_entry(p, struct trace_buf, head);
1326 list_del(&tbp->head);
1329 if (cl_fds[tbp->cpu] >= 0) {
1330 if (flush_subbuf_net(tbp)) {
1331 close(cl_fds[tbp->cpu]);
1332 cl_fds[tbp->cpu] = -1;
1339 return entries_handled;
1343 * Tack 'tbp's buf onto the tail of 'prev's buf
1345 static struct trace_buf *tb_combine(struct trace_buf *prev,
1346 struct trace_buf *tbp)
1348 unsigned long tot_len;
1350 tot_len = prev->len + tbp->len;
1351 if (tot_len > buf_size) {
1353 * tbp->head isn't connected (it was 'prev'
1354 * so it had been taken off of the list
1355 * before). Therefore, we can realloc
1356 * the whole structures, as the other fields
1359 prev = realloc(prev, sizeof(*prev) + tot_len);
1360 prev->buf = (void *)(prev + 1);
1363 memcpy(prev->buf + prev->len, tbp->buf, tbp->len);
1364 prev->len = tot_len;
1370 static int handle_list_file(struct tracer_devpath_head *hd,
1371 struct list_head *list)
1373 int off, t_len, nevents;
1374 struct blk_io_trace *t;
1375 struct list_head *p, *q;
1376 int entries_handled = 0;
1377 struct trace_buf *tbp, *prev;
1380 list_for_each_safe(p, q, list) {
1381 tbp = list_entry(p, struct trace_buf, head);
1382 list_del(&tbp->head);
1386 * If there was some leftover before, tack this new
1387 * entry onto the tail of the previous one.
1390 tbp = tb_combine(prev, tbp);
1393 * See how many whole traces there are - send them
1394 * all out in one go.
1398 while (off + (int)sizeof(*t) <= tbp->len) {
1399 t = (struct blk_io_trace *)(tbp->buf + off);
1400 t_len = sizeof(*t) + t->pdu_len;
1401 if (off + t_len > tbp->len)
1408 pdc_nev_update(tbp->dpp, tbp->cpu, nevents);
1411 * Write any full set of traces, any remaining data is kept
1412 * for the next pass.
1415 if (write_data(tbp->buf, off) || off == tbp->len) {
1421 * Move valid data to beginning of buffer
1424 memmove(tbp->buf, tbp->buf + off, tbp->len);
1432 return entries_handled;
1435 static void __process_trace_bufs(void)
1438 struct list_head *p;
1439 struct list_head list;
1442 __list_for_each(p, &devpaths) {
1443 struct devpath *dpp = list_entry(p, struct devpath, head);
1444 struct tracer_devpath_head *hd = dpp->heads;
1446 for (cpu = 0; cpu < max_cpus; cpu++, hd++) {
1447 pthread_mutex_lock(&hd->mutex);
1448 if (list_empty(&hd->head)) {
1449 pthread_mutex_unlock(&hd->mutex);
1453 list_replace_init(&hd->head, &list);
1454 pthread_mutex_unlock(&hd->mutex);
1456 handled += handle_list(hd, &list);
1461 decr_entries(handled);
1464 static void process_trace_bufs(void)
1466 while (wait_empty_entries())
1467 __process_trace_bufs();
1470 static void clean_trace_bufs(void)
1473 * No mutex needed here: we're only reading from the lists,
1477 __process_trace_bufs();
1480 static inline void read_err(int cpu, char *ifn)
1482 if (errno != EAGAIN)
1483 fprintf(stderr, "Thread %d failed read of %s: %d/%s\n",
1484 cpu, ifn, errno, strerror(errno));
1487 static int net_sendfile(struct io_info *iop)
1491 ret = sendfile(iop->ofd, iop->ifd, NULL, iop->ready);
1495 } else if (ret < (int)iop->ready) {
1496 fprintf(stderr, "short sendfile send (%d of %d)\n",
1504 static inline int net_sendfile_data(struct tracer *tp, struct io_info *iop)
1506 struct devpath *dpp = iop->dpp;
1508 if (net_send_header(iop->ofd, tp->cpu, dpp->buts_name, iop->ready))
1510 return net_sendfile(iop);
1513 static int fill_ofname(char *dst, int dstlen, char *subdir, char *buts_name,
1520 len = snprintf(dst, dstlen, "%s/", output_dir);
1522 len = snprintf(dst, dstlen, "./");
1525 len += snprintf(dst + len, dstlen - len, "%s", subdir);
1527 if (stat(dst, &sb) < 0) {
1528 if (errno != ENOENT) {
1530 "Destination dir %s stat failed: %d/%s\n",
1531 dst, errno, strerror(errno));
1535 * There is no synchronization between multiple threads
1536 * trying to create the directory at once. It's harmless
1537 * to let them try, so just detect the problem and move on.
1539 if (mkdir(dst, 0755) < 0 && errno != EEXIST) {
1541 "Destination dir %s can't be made: %d/%s\n",
1542 dst, errno, strerror(errno));
1548 snprintf(dst + len, dstlen - len, "%s.blktrace.%d",
1551 snprintf(dst + len, dstlen - len, "%s.blktrace.%d",
1557 static int set_vbuf(struct io_info *iop, int mode, size_t size)
1559 iop->obuf = malloc(size);
1560 if (setvbuf(iop->ofp, iop->obuf, mode, size) < 0) {
1561 fprintf(stderr, "setvbuf(%s, %d) failed: %d/%s\n",
1562 iop->dpp->path, (int)size, errno,
1571 static int iop_open(struct io_info *iop, int cpu)
1573 char hostdir[MAXPATHLEN + 64];
1576 if (net_mode == Net_server) {
1577 struct cl_conn *nc = iop->nc;
1580 len = snprintf(hostdir, sizeof(hostdir), "%s-",
1582 len += strftime(hostdir + len, sizeof(hostdir) - len, "%F-%T/",
1583 gmtime(&iop->dpp->cl_connect_time));
1588 if (fill_ofname(iop->ofn, sizeof(iop->ofn), hostdir,
1589 iop->dpp->buts_name, cpu))
1592 iop->ofp = my_fopen(iop->ofn, "w+");
1593 if (iop->ofp == NULL) {
1594 fprintf(stderr, "Open output file %s failed: %d/%s\n",
1595 iop->ofn, errno, strerror(errno));
1599 if (set_vbuf(iop, _IOLBF, FILE_VBUF_SIZE)) {
1600 fprintf(stderr, "set_vbuf for file %s failed: %d/%s\n",
1601 iop->ofn, errno, strerror(errno));
1606 iop->ofd = fileno(iop->ofp);
1610 static void close_iop(struct io_info *iop)
1612 struct mmap_info *mip = &iop->mmap_info;
1615 munmap(mip->fs_buf, mip->fs_buf_len);
1617 if (!piped_output) {
1618 if (ftruncate(fileno(iop->ofp), mip->fs_size) < 0) {
1620 "Ignoring err: ftruncate(%s): %d/%s\n",
1621 iop->ofn, errno, strerror(errno));
1631 static void close_ios(struct tracer *tp)
1633 while (tp->nios > 0) {
1634 struct io_info *iop = &tp->ios[--tp->nios];
1636 iop->dpp->drops = get_drops(iop->dpp);
1642 else if (iop->ofd >= 0) {
1643 struct devpath *dpp = iop->dpp;
1645 net_send_close(iop->ofd, dpp->buts_name, dpp->drops);
1646 net_close_connection(&iop->ofd);
1654 static int open_ios(struct tracer *tp)
1657 struct io_info *iop;
1658 struct list_head *p;
1660 tp->ios = calloc(ndevs, sizeof(struct io_info));
1661 memset(tp->ios, 0, ndevs * sizeof(struct io_info));
1663 tp->pfds = calloc(ndevs, sizeof(struct pollfd));
1664 memset(tp->pfds, 0, ndevs * sizeof(struct pollfd));
1669 __list_for_each(p, &devpaths) {
1670 struct devpath *dpp = list_entry(p, struct devpath, head);
1674 snprintf(iop->ifn, sizeof(iop->ifn), "%s/block/%s/trace%d",
1675 debugfs_path, dpp->buts_name, tp->cpu);
1677 iop->ifd = my_open(iop->ifn, O_RDONLY | O_NONBLOCK);
1679 fprintf(stderr, "Thread %d failed open %s: %d/%s\n",
1680 tp->cpu, iop->ifn, errno, strerror(errno));
1684 init_mmap_info(&iop->mmap_info);
1687 pfd->events = POLLIN;
1691 else if (net_client_use_sendfile()) {
1692 iop->ofd = net_setup_client();
1695 net_send_open(iop->ofd, tp->cpu, dpp->buts_name);
1696 } else if (net_mode == Net_none) {
1697 if (iop_open(iop, tp->cpu))
1701 * This ensures that the server knows about all
1702 * connections & devices before _any_ closes
1704 net_send_open(cl_fds[tp->cpu], tp->cpu, dpp->buts_name);
1715 close(iop->ifd); /* tp->nios _not_ bumped */
1720 static int handle_pfds_file(struct tracer *tp, int nevs, int force_read)
1722 struct mmap_info *mip;
1723 int i, ret, nentries = 0;
1724 struct pollfd *pfd = tp->pfds;
1725 struct io_info *iop = tp->ios;
1727 for (i = 0; nevs > 0 && i < ndevs; i++, pfd++, iop++) {
1728 if (pfd->revents & POLLIN || force_read) {
1729 mip = &iop->mmap_info;
1731 ret = setup_mmap(iop->ofd, buf_size, mip, tp);
1737 ret = read(iop->ifd, mip->fs_buf + mip->fs_off,
1740 pdc_dr_update(iop->dpp, tp->cpu, ret);
1741 mip->fs_size += ret;
1744 } else if (ret == 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)
1763 static int handle_pfds_netclient(struct tracer *tp, int nevs, int force_read)
1766 int i, nentries = 0;
1767 struct pollfd *pfd = tp->pfds;
1768 struct io_info *iop = tp->ios;
1770 for (i = 0; i < ndevs; i++, pfd++, iop++) {
1771 if (pfd->revents & POLLIN || force_read) {
1772 if (fstat(iop->ifd, &sb) < 0) {
1775 } else if (sb.st_size > (off_t)iop->data_queued) {
1776 iop->ready = sb.st_size - iop->data_queued;
1777 iop->data_queued = sb.st_size;
1779 if (!net_sendfile_data(tp, iop)) {
1780 pdc_dr_update(iop->dpp, tp->cpu,
1792 incr_entries(nentries);
1797 static int handle_pfds_entries(struct tracer *tp, int nevs, int force_read)
1799 int i, nentries = 0;
1800 struct trace_buf *tbp;
1801 struct pollfd *pfd = tp->pfds;
1802 struct io_info *iop = tp->ios;
1804 tbp = alloc_trace_buf(tp->cpu, buf_size);
1805 for (i = 0; i < ndevs; i++, pfd++, iop++) {
1806 if (pfd->revents & POLLIN || force_read) {
1807 tbp->len = read(iop->ifd, tbp->buf, buf_size);
1809 pdc_dr_update(iop->dpp, tp->cpu, tbp->len);
1810 add_trace_buf(iop->dpp, tp->cpu, &tbp);
1812 } else if (tbp->len == 0) {
1814 * Short reads after we're done stop us
1815 * from trying reads.
1820 read_err(tp->cpu, iop->ifn);
1821 if (errno != EAGAIN || tp->is_done)
1824 if (!piped_output && --nevs == 0)
1831 incr_entries(nentries);
1836 static void *thread_main(void *arg)
1838 int ret, ndone, to_val;
1839 struct tracer *tp = arg;
1841 ret = lock_on_cpu(tp->cpu);
1850 to_val = 50; /* Frequent partial handles */
1852 to_val = 500; /* 1/2 second intervals */
1855 tracer_signal_ready(tp, Th_running, 0);
1856 tracer_wait_unblock(tp);
1858 while (!tp->is_done) {
1859 ndone = poll(tp->pfds, ndevs, to_val);
1860 if (ndone || piped_output)
1861 (void)handle_pfds(tp, ndone, piped_output);
1862 else if (ndone < 0 && errno != EINTR)
1863 fprintf(stderr, "Thread %d poll failed: %d/%s\n",
1864 tp->cpu, errno, strerror(errno));
1868 * Trace is stopped, pull data until we get a short read
1870 while (handle_pfds(tp, ndevs, 1) > 0)
1874 tracer_signal_ready(tp, Th_leaving, 0);
1878 tracer_signal_ready(tp, Th_error, ret);
1882 static int start_tracer(int cpu)
1886 tp = malloc(sizeof(*tp));
1887 memset(tp, 0, sizeof(*tp));
1889 INIT_LIST_HEAD(&tp->head);
1893 if (pthread_create(&tp->thread, NULL, thread_main, tp)) {
1894 fprintf(stderr, "FAILED to start thread on CPU %d: %d/%s\n",
1895 cpu, errno, strerror(errno));
1900 list_add_tail(&tp->head, &tracers);
1904 static int create_output_files(int cpu)
1906 char fname[MAXPATHLEN + 64];
1907 struct list_head *p;
1910 __list_for_each(p, &devpaths) {
1911 struct devpath *dpp = list_entry(p, struct devpath, head);
1913 if (fill_ofname(fname, sizeof(fname), NULL, dpp->buts_name,
1916 f = my_fopen(fname, "w+");
1924 static void start_tracers(void)
1926 int cpu, started = 0;
1927 struct list_head *p;
1928 size_t alloc_size = CPU_ALLOC_SIZE(max_cpus);
1930 for (cpu = 0; cpu < max_cpus; cpu++) {
1931 if (!CPU_ISSET_S(cpu, alloc_size, online_cpus)) {
1933 * Create fake empty output files so that other tools
1934 * like blkparse don't have to bother with sparse CPU
1937 if (create_output_files(cpu))
1941 if (start_tracer(cpu))
1946 wait_tracers_ready(started);
1948 __list_for_each(p, &tracers) {
1949 struct tracer *tp = list_entry(p, struct tracer, head);
1952 "FAILED to start thread on CPU %d: %d/%s\n",
1953 tp->cpu, tp->status, strerror(tp->status));
1957 static void stop_tracers(void)
1959 struct list_head *p;
1962 * Stop the tracing - makes the tracer threads clean up quicker.
1964 __list_for_each(p, &devpaths) {
1965 struct devpath *dpp = list_entry(p, struct devpath, head);
1966 (void)ioctl(dpp->fd, BLKTRACESTOP);
1970 * Tell each tracer to quit
1972 __list_for_each(p, &tracers) {
1973 struct tracer *tp = list_entry(p, struct tracer, head);
1976 pthread_cond_broadcast(&mt_cond);
1979 static void del_tracers(void)
1981 struct list_head *p, *q;
1983 list_for_each_safe(p, q, &tracers) {
1984 struct tracer *tp = list_entry(p, struct tracer, head);
1986 list_del(&tp->head);
1991 static void wait_tracers(void)
1993 struct list_head *p;
1995 if (use_tracer_devpaths())
1996 process_trace_bufs();
1998 wait_tracers_leaving();
2000 __list_for_each(p, &tracers) {
2002 struct tracer *tp = list_entry(p, struct tracer, head);
2004 ret = pthread_join(tp->thread, NULL);
2006 fprintf(stderr, "Thread join %d failed %d\n",
2010 if (use_tracer_devpaths())
2016 static void exit_tracing(void)
2018 signal(SIGINT, SIG_IGN);
2019 signal(SIGHUP, SIG_IGN);
2020 signal(SIGTERM, SIG_IGN);
2021 signal(SIGALRM, SIG_IGN);
2029 static void handle_sigint(__attribute__((__unused__)) int sig)
2035 static void show_stats(struct list_head *devpaths)
2038 struct list_head *p;
2039 unsigned long long nevents, data_read;
2040 unsigned long long total_drops = 0;
2041 unsigned long long total_events = 0;
2044 ofp = my_fopen("/dev/null", "w");
2048 __list_for_each(p, devpaths) {
2050 struct pdc_stats *sp;
2051 struct devpath *dpp = list_entry(p, struct devpath, head);
2053 if (net_mode == Net_server)
2054 printf("server: end of run for %s:%s\n",
2055 dpp->ch->hostname, dpp->buts_name);
2060 fprintf(ofp, "=== %s ===\n", dpp->buts_name);
2061 for (cpu = 0, sp = dpp->stats; cpu < dpp->ncpus; cpu++, sp++) {
2063 * Estimate events if not known...
2065 if (sp->nevents == 0) {
2066 sp->nevents = sp->data_read /
2067 sizeof(struct blk_io_trace);
2071 " CPU%3d: %20llu events, %8llu KiB data\n",
2072 cpu, sp->nevents, (sp->data_read + 1023) >> 10);
2074 data_read += sp->data_read;
2075 nevents += sp->nevents;
2078 fprintf(ofp, " Total: %20llu events (dropped %llu),"
2079 " %8llu KiB data\n", nevents,
2080 dpp->drops, (data_read + 1024) >> 10);
2082 total_drops += dpp->drops;
2083 total_events += (nevents + dpp->drops);
2091 double drops_ratio = 1.0;
2094 drops_ratio = (double)total_drops/(double)total_events;
2096 fprintf(stderr, "\nYou have %llu (%5.1lf%%) dropped events\n"
2097 "Consider using a larger buffer size (-b) "
2098 "and/or more buffers (-n)\n",
2099 total_drops, 100.0 * drops_ratio);
2103 static int handle_args(int argc, char *argv[])
2107 int act_mask_tmp = 0;
2109 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
2112 i = find_mask_map(optarg);
2114 fprintf(stderr, "Invalid action mask %s\n",
2122 if ((sscanf(optarg, "%x", &i) != 1) ||
2123 !valid_act_opt(i)) {
2125 "Invalid set action mask %s/0x%x\n",
2133 if (add_devpath(optarg) != 0)
2139 FILE *ifp = my_fopen(optarg, "r");
2143 "Invalid file for devices %s\n",
2148 while (fscanf(ifp, "%s\n", dev_line) == 1) {
2149 if (add_devpath(dev_line) != 0) {
2159 debugfs_path = optarg;
2163 output_name = optarg;
2166 kill_running_trace = 1;
2169 stop_watch = atoi(optarg);
2170 if (stop_watch <= 0) {
2172 "Invalid stopwatch value (%d secs)\n",
2179 printf("%s version %s\n", argv[0], blktrace_version);
2183 buf_size = strtoul(optarg, NULL, 10);
2184 if (buf_size <= 0 || buf_size > 16*1024) {
2185 fprintf(stderr, "Invalid buffer size (%lu)\n",
2192 buf_nr = strtoul(optarg, NULL, 10);
2195 "Invalid buffer nr (%lu)\n", buf_nr);
2200 output_dir = optarg;
2203 net_mode = Net_client;
2204 memset(hostname, 0, sizeof(hostname));
2205 strncpy(hostname, optarg, sizeof(hostname));
2206 hostname[sizeof(hostname) - 1] = '\0';
2209 net_mode = Net_server;
2212 net_port = atoi(optarg);
2215 net_use_sendfile = 0;
2218 show_usage(argv[0]);
2224 while (optind < argc)
2225 if (add_devpath(argv[optind++]) != 0)
2228 if (net_mode != Net_server && ndevs == 0) {
2229 show_usage(argv[0]);
2233 if (statfs(debugfs_path, &st) < 0) {
2234 fprintf(stderr, "Invalid debug path %s: %d/%s\n",
2235 debugfs_path, errno, strerror(errno));
2239 if (st.f_type != (long)DEBUGFS_TYPE) {
2240 fprintf(stderr, "Debugfs is not mounted at %s\n", debugfs_path);
2244 if (act_mask_tmp != 0)
2245 act_mask = act_mask_tmp;
2247 if (net_mode == Net_client && net_setup_addr())
2251 * Set up for appropriate PFD handler based upon output name.
2253 if (net_client_use_sendfile())
2254 handle_pfds = handle_pfds_netclient;
2255 else if (net_client_use_send())
2256 handle_pfds = handle_pfds_entries;
2257 else if (output_name && (strcmp(output_name, "-") == 0)) {
2259 handle_pfds = handle_pfds_entries;
2261 if (setvbuf(pfp, NULL, _IONBF, 0)) {
2262 perror("setvbuf stdout");
2266 handle_pfds = handle_pfds_file;
2270 static void ch_add_connection(struct net_server_s *ns, struct cl_host *ch,
2275 nc = malloc(sizeof(*nc));
2276 memset(nc, 0, sizeof(*nc));
2278 time(&nc->connect_time);
2283 list_add_tail(&nc->ch_head, &ch->conn_list);
2286 list_add_tail(&nc->ns_head, &ns->conn_list);
2288 ns->pfds = realloc(ns->pfds, (ns->connects+1) * sizeof(struct pollfd));
2291 static void ch_rem_connection(struct net_server_s *ns, struct cl_host *ch,
2294 net_close_connection(&nc->fd);
2296 list_del(&nc->ch_head);
2299 list_del(&nc->ns_head);
2301 ns->pfds = realloc(ns->pfds, (ns->connects+1) * sizeof(struct pollfd));
2306 static struct cl_host *net_find_client_host(struct net_server_s *ns,
2307 struct in_addr cl_in_addr)
2309 struct list_head *p;
2311 __list_for_each(p, &ns->ch_list) {
2312 struct cl_host *ch = list_entry(p, struct cl_host, head);
2314 if (in_addr_eq(ch->cl_in_addr, cl_in_addr))
2321 static struct cl_host *net_add_client_host(struct net_server_s *ns,
2322 struct sockaddr_in *addr)
2326 ch = malloc(sizeof(*ch));
2327 memset(ch, 0, sizeof(*ch));
2330 ch->cl_in_addr = addr->sin_addr;
2331 list_add_tail(&ch->head, &ns->ch_list);
2334 ch->hostname = strdup(inet_ntoa(addr->sin_addr));
2335 printf("server: connection from %s\n", ch->hostname);
2337 INIT_LIST_HEAD(&ch->conn_list);
2338 INIT_LIST_HEAD(&ch->devpaths);
2343 static void device_done(struct devpath *dpp, int ncpus)
2346 struct io_info *iop;
2348 for (cpu = 0, iop = dpp->ios; cpu < ncpus; cpu++, iop++)
2351 list_del(&dpp->head);
2355 static void net_ch_remove(struct cl_host *ch, int ncpus)
2357 struct list_head *p, *q;
2358 struct net_server_s *ns = ch->ns;
2360 list_for_each_safe(p, q, &ch->devpaths) {
2361 struct devpath *dpp = list_entry(p, struct devpath, head);
2362 device_done(dpp, ncpus);
2365 list_for_each_safe(p, q, &ch->conn_list) {
2366 struct cl_conn *nc = list_entry(p, struct cl_conn, ch_head);
2368 ch_rem_connection(ns, ch, nc);
2371 list_del(&ch->head);
2379 static void net_add_connection(struct net_server_s *ns)
2383 socklen_t socklen = sizeof(ns->addr);
2385 fd = my_accept(ns->listen_fd, (struct sockaddr *)&ns->addr, &socklen);
2388 * This is OK: we just won't accept this connection,
2393 ch = net_find_client_host(ns, ns->addr.sin_addr);
2395 ch = net_add_client_host(ns, &ns->addr);
2397 ch_add_connection(ns, ch, fd);
2401 static struct devpath *nc_add_dpp(struct cl_conn *nc,
2402 struct blktrace_net_hdr *bnh,
2403 time_t connect_time)
2406 struct io_info *iop;
2407 struct devpath *dpp;
2409 dpp = malloc(sizeof(*dpp));
2410 memset(dpp, 0, sizeof(*dpp));
2412 dpp->buts_name = strdup(bnh->buts_name);
2413 dpp->path = strdup(bnh->buts_name);
2416 dpp->cl_id = bnh->cl_id;
2417 dpp->cl_connect_time = connect_time;
2418 dpp->ncpus = nc->ncpus;
2419 dpp->stats = calloc(dpp->ncpus, sizeof(*dpp->stats));
2420 memset(dpp->stats, 0, dpp->ncpus * sizeof(*dpp->stats));
2422 list_add_tail(&dpp->head, &nc->ch->devpaths);
2425 dpp->ios = calloc(nc->ncpus, sizeof(*iop));
2426 memset(dpp->ios, 0, ndevs * sizeof(*iop));
2428 for (cpu = 0, iop = dpp->ios; cpu < nc->ncpus; cpu++, iop++) {
2431 init_mmap_info(&iop->mmap_info);
2433 if (iop_open(iop, cpu))
2441 * Need to unravel what's been done...
2444 close_iop(&dpp->ios[cpu--]);
2450 static struct devpath *nc_find_dpp(struct cl_conn *nc,
2451 struct blktrace_net_hdr *bnh)
2453 struct list_head *p;
2454 time_t connect_time = nc->connect_time;
2456 __list_for_each(p, &nc->ch->devpaths) {
2457 struct devpath *dpp = list_entry(p, struct devpath, head);
2459 if (!strcmp(dpp->buts_name, bnh->buts_name))
2462 if (dpp->cl_id == bnh->cl_id)
2463 connect_time = dpp->cl_connect_time;
2466 return nc_add_dpp(nc, bnh, connect_time);
2469 static void net_client_read_data(struct cl_conn *nc, struct devpath *dpp,
2470 struct blktrace_net_hdr *bnh)
2473 struct io_info *iop = &dpp->ios[bnh->cpu];
2474 struct mmap_info *mip = &iop->mmap_info;
2476 if (setup_mmap(iop->ofd, bnh->len, &iop->mmap_info, NULL)) {
2477 fprintf(stderr, "ncd(%s:%d): mmap failed\n",
2478 nc->ch->hostname, nc->fd);
2482 ret = net_recv_data(nc->fd, mip->fs_buf + mip->fs_off, bnh->len);
2484 pdc_dr_update(dpp, bnh->cpu, ret);
2485 mip->fs_size += ret;
2492 * Returns 1 if we closed a host - invalidates other polling information
2493 * that may be present.
2495 static int net_client_data(struct cl_conn *nc)
2498 struct devpath *dpp;
2499 struct blktrace_net_hdr bnh;
2501 ret = net_get_header(nc, &bnh);
2506 fprintf(stderr, "ncd(%d): header read failed\n", nc->fd);
2510 if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
2511 fprintf(stderr, "ncd(%d): received data is bad\n", nc->fd);
2515 if (!data_is_native) {
2516 bnh.magic = be32_to_cpu(bnh.magic);
2517 bnh.cpu = be32_to_cpu(bnh.cpu);
2518 bnh.max_cpus = be32_to_cpu(bnh.max_cpus);
2519 bnh.len = be32_to_cpu(bnh.len);
2520 bnh.cl_id = be32_to_cpu(bnh.cl_id);
2521 bnh.buf_size = be32_to_cpu(bnh.buf_size);
2522 bnh.buf_nr = be32_to_cpu(bnh.buf_nr);
2523 bnh.page_size = be32_to_cpu(bnh.page_size);
2526 if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2527 fprintf(stderr, "ncd(%s:%d): bad data magic\n",
2528 nc->ch->hostname, nc->fd);
2532 if (nc->ncpus == -1)
2533 nc->ncpus = bnh.max_cpus;
2536 * len == 0 means the other end is sending us a new connection/dpp
2537 * len == 1 means that the other end signalled end-of-run
2539 dpp = nc_find_dpp(nc, &bnh);
2542 * Just adding in the dpp above is enough
2544 ack_open_close(nc->fd, dpp->buts_name);
2546 } else if (bnh.len == 1) {
2548 * overload cpu count with dropped events
2550 dpp->drops = bnh.cpu;
2552 ack_open_close(nc->fd, dpp->buts_name);
2553 if (--nc->ch->cl_opens == 0) {
2554 show_stats(&nc->ch->devpaths);
2555 net_ch_remove(nc->ch, nc->ncpus);
2559 net_client_read_data(nc, dpp, &bnh);
2564 static void handle_client_data(struct net_server_s *ns, int events)
2568 struct list_head *p, *q;
2571 list_for_each_safe(p, q, &ns->conn_list) {
2572 if (pfd->revents & POLLIN) {
2573 nc = list_entry(p, struct cl_conn, ns_head);
2575 if (net_client_data(nc) || --events == 0)
2582 static void net_setup_pfds(struct net_server_s *ns)
2585 struct list_head *p;
2587 ns->pfds[0].fd = ns->listen_fd;
2588 ns->pfds[0].events = POLLIN;
2591 __list_for_each(p, &ns->conn_list) {
2592 struct cl_conn *nc = list_entry(p, struct cl_conn, ns_head);
2595 pfd->events = POLLIN;
2600 static int net_server_handle_connections(struct net_server_s *ns)
2604 printf("server: waiting for connections...\n");
2608 events = poll(ns->pfds, ns->connects + 1, -1);
2610 if (errno != EINTR) {
2611 perror("FATAL: poll error");
2614 } else if (events > 0) {
2615 if (ns->pfds[0].revents & POLLIN) {
2616 net_add_connection(ns);
2621 handle_client_data(ns, events);
2628 static int net_server(void)
2632 struct net_server_s net_server;
2633 struct net_server_s *ns = &net_server;
2635 memset(ns, 0, sizeof(*ns));
2636 INIT_LIST_HEAD(&ns->ch_list);
2637 INIT_LIST_HEAD(&ns->conn_list);
2638 ns->pfds = malloc(sizeof(struct pollfd));
2640 fd = my_socket(AF_INET, SOCK_STREAM, 0);
2642 perror("server: socket");
2647 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
2648 perror("setsockopt");
2652 memset(&ns->addr, 0, sizeof(ns->addr));
2653 ns->addr.sin_family = AF_INET;
2654 ns->addr.sin_addr.s_addr = htonl(INADDR_ANY);
2655 ns->addr.sin_port = htons(net_port);
2657 if (bind(fd, (struct sockaddr *) &ns->addr, sizeof(ns->addr)) < 0) {
2662 if (listen(fd, 1) < 0) {
2668 * The actual server looping is done here:
2671 ret = net_server_handle_connections(ns);
2674 * Clean up and return...
2681 static int run_tracers(void)
2683 atexit(exit_tracing);
2684 if (net_mode == Net_client)
2685 printf("blktrace: connecting to %s\n", hostname);
2690 if (use_tracer_devpaths()) {
2691 if (setup_tracer_devpaths())
2695 handle_list = handle_list_file;
2697 handle_list = handle_list_net;
2701 if (nthreads_running == ncpus) {
2704 if (net_mode == Net_client)
2705 printf("blktrace: connected!\n");
2712 if (nthreads_running == ncpus)
2713 show_stats(&devpaths);
2714 if (net_client_use_send())
2715 close_client_connections();
2721 static cpu_set_t *get_online_cpus(void)
2726 int cpuid, prevcpuid = -1;
2728 int n, ncpu, curcpu = 0;
2731 ncpu = sysconf(_SC_NPROCESSORS_CONF);
2735 cpu_nums = malloc(sizeof(int)*ncpu);
2742 * There is no way to easily get maximum CPU number. So we have to
2743 * parse the file first to find it out and then create appropriate
2746 cpus = my_fopen("/sys/devices/system/cpu/online", "r");
2748 n = fscanf(cpus, "%d%c", &cpuid, &nextch);
2751 if (n == 2 && nextch == '-') {
2755 if (prevcpuid == -1)
2757 while (prevcpuid <= cpuid) {
2758 /* More CPUs listed than configured? */
2759 if (curcpu >= ncpu) {
2763 cpu_nums[curcpu++] = prevcpuid++;
2770 max_cpus = cpu_nums[ncpu - 1] + 1;
2772 /* Now that we have maximum cpu number, create a cpuset */
2773 set = CPU_ALLOC(max_cpus);
2778 alloc_size = CPU_ALLOC_SIZE(max_cpus);
2779 CPU_ZERO_S(alloc_size, set);
2781 for (curcpu = 0; curcpu < ncpu; curcpu++)
2782 CPU_SET_S(cpu_nums[curcpu], alloc_size, set);
2789 int main(int argc, char *argv[])
2793 setlocale(LC_NUMERIC, "en_US");
2794 pagesize = getpagesize();
2795 online_cpus = get_online_cpus();
2797 fprintf(stderr, "cannot get online cpus %d/%s\n",
2798 errno, strerror(errno));
2801 } else if (handle_args(argc, argv)) {
2806 ncpus = CPU_COUNT_S(CPU_ALLOC_SIZE(max_cpus), online_cpus);
2807 if (ndevs > 1 && output_name && strcmp(output_name, "-") != 0) {
2808 fprintf(stderr, "-o not supported with multiple devices\n");
2813 signal(SIGINT, handle_sigint);
2814 signal(SIGHUP, handle_sigint);
2815 signal(SIGTERM, handle_sigint);
2816 signal(SIGALRM, handle_sigint);
2817 signal(SIGPIPE, SIG_IGN);
2819 if (kill_running_trace) {
2820 struct devpath *dpp;
2821 struct list_head *p;
2823 __list_for_each(p, &devpaths) {
2824 dpp = list_entry(p, struct devpath, head);
2825 if (__stop_trace(dpp->fd)) {
2827 "BLKTRACETEARDOWN %s failed: %d/%s\n",
2828 dpp->path, errno, strerror(errno));
2831 } else if (net_mode == Net_server) {
2833 fprintf(stderr, "-o ignored in server mode\n");
2838 ret = run_tracers();