Commit | Line | Data |
---|---|---|
d0ca268b JA |
1 | /* |
2 | * block queue tracing application | |
3 | * | |
d956a2cd JA |
4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
d0ca268b JA |
20 | */ |
21 | #include <pthread.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
24 | #include <unistd.h> | |
25 | #include <locale.h> | |
26 | #include <signal.h> | |
27 | #include <fcntl.h> | |
28 | #include <string.h> | |
29 | #include <sys/ioctl.h> | |
b9d4294e | 30 | #include <sys/param.h> |
e3e74029 | 31 | #include <sys/statfs.h> |
eb3c8108 | 32 | #include <sys/poll.h> |
b7106311 | 33 | #include <sys/mman.h> |
8e86c98a | 34 | #include <sys/socket.h> |
d0ca268b JA |
35 | #include <stdio.h> |
36 | #include <stdlib.h> | |
37 | #include <sched.h> | |
d39c04ca AB |
38 | #include <ctype.h> |
39 | #include <getopt.h> | |
da39451f | 40 | #include <errno.h> |
8e86c98a JA |
41 | #include <netinet/in.h> |
42 | #include <arpa/inet.h> | |
43 | #include <netdb.h> | |
32f18c48 | 44 | #include <sys/sendfile.h> |
d0ca268b JA |
45 | |
46 | #include "blktrace.h" | |
21f55651 | 47 | #include "barrier.h" |
d0ca268b | 48 | |
a3225fed | 49 | static char blktrace_version[] = "0.99.1"; |
52724a0e | 50 | |
8f551a39 JA |
51 | /* |
52 | * You may want to increase this even more, if you are logging at a high | |
53 | * rate and see skipped/missed events | |
54 | */ | |
007c233c | 55 | #define BUF_SIZE (512 * 1024) |
d0ca268b JA |
56 | #define BUF_NR (4) |
57 | ||
007c233c JA |
58 | #define OFILE_BUF (128 * 1024) |
59 | ||
3d06efea | 60 | #define DEBUGFS_TYPE 0x64626720 |
e3e74029 | 61 | |
32f18c48 | 62 | #define S_OPTS "d:a:A:r:o:kw:Vb:n:D:lh:p:s" |
d5396421 | 63 | static struct option l_opts[] = { |
5c86134e | 64 | { |
d39c04ca | 65 | .name = "dev", |
428683db | 66 | .has_arg = required_argument, |
d39c04ca AB |
67 | .flag = NULL, |
68 | .val = 'd' | |
69 | }, | |
5c86134e | 70 | { |
d39c04ca | 71 | .name = "act-mask", |
428683db | 72 | .has_arg = required_argument, |
d39c04ca AB |
73 | .flag = NULL, |
74 | .val = 'a' | |
75 | }, | |
5c86134e | 76 | { |
d39c04ca | 77 | .name = "set-mask", |
428683db | 78 | .has_arg = required_argument, |
d39c04ca AB |
79 | .flag = NULL, |
80 | .val = 'A' | |
81 | }, | |
5c86134e | 82 | { |
5270dddd | 83 | .name = "relay", |
428683db | 84 | .has_arg = required_argument, |
5270dddd JA |
85 | .flag = NULL, |
86 | .val = 'r' | |
87 | }, | |
d5396421 JA |
88 | { |
89 | .name = "output", | |
428683db | 90 | .has_arg = required_argument, |
d5396421 JA |
91 | .flag = NULL, |
92 | .val = 'o' | |
93 | }, | |
bc39777c JA |
94 | { |
95 | .name = "kill", | |
428683db | 96 | .has_arg = no_argument, |
bc39777c JA |
97 | .flag = NULL, |
98 | .val = 'k' | |
99 | }, | |
ece238a6 NS |
100 | { |
101 | .name = "stopwatch", | |
428683db | 102 | .has_arg = required_argument, |
ece238a6 NS |
103 | .flag = NULL, |
104 | .val = 'w' | |
105 | }, | |
52724a0e JA |
106 | { |
107 | .name = "version", | |
108 | .has_arg = no_argument, | |
109 | .flag = NULL, | |
57ea8602 | 110 | .val = 'V' |
52724a0e | 111 | }, |
129aa440 | 112 | { |
3f65c585 | 113 | .name = "buffer-size", |
129aa440 JA |
114 | .has_arg = required_argument, |
115 | .flag = NULL, | |
116 | .val = 'b' | |
117 | }, | |
118 | { | |
3f65c585 | 119 | .name = "num-sub-buffers", |
129aa440 JA |
120 | .has_arg = required_argument, |
121 | .flag = NULL, | |
122 | .val = 'n' | |
123 | }, | |
d1d7f15f | 124 | { |
3f65c585 | 125 | .name = "output-dir", |
d1d7f15f JA |
126 | .has_arg = required_argument, |
127 | .flag = NULL, | |
128 | .val = 'D' | |
129 | }, | |
8e86c98a JA |
130 | { |
131 | .name = "listen", | |
132 | .has_arg = no_argument, | |
133 | .flag = NULL, | |
134 | .val = 'l' | |
135 | }, | |
136 | { | |
137 | .name = "host", | |
138 | .has_arg = required_argument, | |
139 | .flag = NULL, | |
140 | .val = 'h' | |
141 | }, | |
142 | { | |
143 | .name = "port", | |
144 | .has_arg = required_argument, | |
145 | .flag = NULL, | |
146 | .val = 'p' | |
147 | }, | |
32f18c48 | 148 | { |
79971f43 | 149 | .name = "no-sendfile", |
32f18c48 JA |
150 | .has_arg = no_argument, |
151 | .flag = NULL, | |
152 | .val = 's' | |
153 | }, | |
71ef8b7c JA |
154 | { |
155 | .name = NULL, | |
156 | } | |
d39c04ca AB |
157 | }; |
158 | ||
9db17354 | 159 | struct tip_subbuf { |
9db17354 | 160 | void *buf; |
5be4bdaf JA |
161 | unsigned int len; |
162 | unsigned int max_len; | |
9db17354 JA |
163 | }; |
164 | ||
21f55651 JA |
165 | #define FIFO_SIZE (1024) /* should be plenty big! */ |
166 | #define CL_SIZE (128) /* cache line, any bigger? */ | |
167 | ||
168 | struct tip_subbuf_fifo { | |
169 | int tail __attribute__((aligned(CL_SIZE))); | |
170 | int head __attribute__((aligned(CL_SIZE))); | |
171 | struct tip_subbuf *q[FIFO_SIZE]; | |
172 | }; | |
173 | ||
d0ca268b JA |
174 | struct thread_information { |
175 | int cpu; | |
176 | pthread_t thread; | |
b9d4294e JA |
177 | |
178 | int fd; | |
a3e4d330 | 179 | void *fd_buf; |
b9d4294e JA |
180 | char fn[MAXPATHLEN + 64]; |
181 | ||
007c233c JA |
182 | FILE *ofile; |
183 | char *ofile_buffer; | |
32f18c48 | 184 | off_t ofile_offset; |
9db17354 | 185 | int ofile_stdout; |
8e86c98a | 186 | int ofile_mmap; |
007c233c | 187 | |
0cc7d25e JA |
188 | int (*get_subbuf)(struct thread_information *, unsigned int); |
189 | int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *); | |
190 | int (*read_data)(struct thread_information *, void *, unsigned int); | |
191 | ||
d0ca268b | 192 | unsigned long events_processed; |
b7106311 | 193 | unsigned long long data_read; |
bcbeb60f | 194 | unsigned long long data_queued; |
e7c9f3ff | 195 | struct device_information *device; |
9db17354 JA |
196 | |
197 | int exited; | |
198 | ||
b7106311 JA |
199 | /* |
200 | * piped fifo buffers | |
201 | */ | |
21f55651 | 202 | struct tip_subbuf_fifo fifo; |
7de86b12 | 203 | struct tip_subbuf *leftover_ts; |
b7106311 JA |
204 | |
205 | /* | |
206 | * mmap controlled output files | |
207 | */ | |
208 | unsigned long long fs_size; | |
209 | unsigned long long fs_max_size; | |
210 | unsigned long fs_off; | |
211 | void *fs_buf; | |
212 | unsigned long fs_buf_len; | |
ff11d54c TZ |
213 | |
214 | struct net_connection *nc; | |
d0ca268b JA |
215 | }; |
216 | ||
e7c9f3ff NS |
217 | struct device_information { |
218 | int fd; | |
219 | char *path; | |
220 | char buts_name[32]; | |
99c1f5ab | 221 | volatile int trace_started; |
eb3c8108 | 222 | unsigned long drop_count; |
e7c9f3ff | 223 | struct thread_information *threads; |
ff11d54c TZ |
224 | |
225 | struct cl_host *ch; | |
226 | u32 cl_id; | |
227 | time_t cl_connect_time; | |
e7c9f3ff | 228 | }; |
d0ca268b | 229 | |
e7c9f3ff | 230 | static int ncpus; |
d0ca268b | 231 | static struct thread_information *thread_information; |
e7c9f3ff NS |
232 | static int ndevs; |
233 | static struct device_information *device_information; | |
234 | ||
235 | /* command line option globals */ | |
3d06efea | 236 | static char *debugfs_path; |
d5396421 | 237 | static char *output_name; |
d1d7f15f | 238 | static char *output_dir; |
5c86134e | 239 | static int act_mask = ~0U; |
bc39777c | 240 | static int kill_running_trace; |
eb3c8108 JA |
241 | static unsigned long buf_size = BUF_SIZE; |
242 | static unsigned long buf_nr = BUF_NR; | |
b7106311 | 243 | static unsigned int page_size; |
d39c04ca | 244 | |
e7c9f3ff NS |
245 | #define is_done() (*(volatile int *)(&done)) |
246 | static volatile int done; | |
247 | ||
eb3c8108 JA |
248 | #define is_trace_stopped() (*(volatile int *)(&trace_stopped)) |
249 | static volatile int trace_stopped; | |
250 | ||
251 | #define is_stat_shown() (*(volatile int *)(&stat_shown)) | |
252 | static volatile int stat_shown; | |
a3e4d330 | 253 | |
8e86c98a JA |
254 | int data_is_native = -1; |
255 | ||
72ca8801 NS |
256 | static void exit_trace(int status); |
257 | ||
99c1f5ab JA |
258 | #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started)) |
259 | #define dip_set_tracing(dip, v) ((dip)->trace_started = (v)) | |
260 | ||
ce020676 | 261 | #define __for_each_dip(__d, __di, __e, __i) \ |
e0a1988b JA |
262 | for (__i = 0, __d = __di; __i < __e; __i++, __d++) |
263 | ||
264 | #define for_each_dip(__d, __i) \ | |
ce020676 | 265 | __for_each_dip(__d, device_information, ndevs, __i) |
e0a1988b | 266 | #define for_each_nc_dip(__nc, __d, __i) \ |
ff11d54c | 267 | __for_each_dip(__d, (__nc)->ch->device_information, (__nc)->ch->ndevs, __i) |
99c1f5ab | 268 | |
ce020676 JA |
269 | #define __for_each_tip(__d, __t, __ncpus, __j) \ |
270 | for (__j = 0, __t = (__d)->threads; __j < __ncpus; __j++, __t++) | |
9db17354 | 271 | #define for_each_tip(__d, __t, __j) \ |
ce020676 | 272 | __for_each_tip(__d, __t, ncpus, __j) |
ff11d54c TZ |
273 | #define for_each_cl_host(__c) \ |
274 | for (__c = cl_host_list; __c; __c = __c->list_next) | |
99c1f5ab | 275 | |
8e86c98a JA |
276 | /* |
277 | * networking stuff follows. we include a magic number so we know whether | |
278 | * to endianness convert or not | |
279 | */ | |
280 | struct blktrace_net_hdr { | |
281 | u32 magic; /* same as trace magic */ | |
22cd0c02 | 282 | char buts_name[32]; /* trace name */ |
8e86c98a | 283 | u32 cpu; /* for which cpu */ |
22cd0c02 | 284 | u32 max_cpus; |
8e86c98a | 285 | u32 len; /* length of following trace data */ |
ff11d54c | 286 | u32 cl_id; /* id for set of client per-cpu connections */ |
8e86c98a JA |
287 | }; |
288 | ||
289 | #define TRACE_NET_PORT (8462) | |
290 | ||
291 | enum { | |
292 | Net_none = 0, | |
293 | Net_server, | |
294 | Net_client, | |
295 | }; | |
296 | ||
297 | /* | |
298 | * network cmd line params | |
299 | */ | |
300 | static char hostname[MAXHOSTNAMELEN]; | |
301 | static int net_port = TRACE_NET_PORT; | |
302 | static int net_mode = 0; | |
79971f43 | 303 | static int net_use_sendfile = 1; |
8e86c98a | 304 | |
ff11d54c TZ |
305 | struct cl_host { |
306 | struct cl_host *list_next; | |
e0a1988b | 307 | struct in_addr cl_in_addr; |
ff11d54c TZ |
308 | struct net_connection *net_connections; |
309 | int nconn; | |
e0a1988b JA |
310 | struct device_information *device_information; |
311 | int ndevs; | |
312 | int ncpus; | |
ff11d54c | 313 | int ndevs_done; |
e0a1988b JA |
314 | }; |
315 | ||
ff11d54c TZ |
316 | struct net_connection { |
317 | int in_fd; | |
318 | struct pollfd pfd; | |
319 | time_t connect_time; | |
320 | struct cl_host *ch; | |
321 | int ncpus; | |
322 | }; | |
323 | ||
324 | #define NET_MAX_CL_HOSTS (1024) | |
325 | static struct cl_host *cl_host_list; | |
326 | static int cl_hosts; | |
e0a1988b | 327 | static int net_connects; |
ff11d54c TZ |
328 | |
329 | static int *net_out_fd; | |
8e86c98a JA |
330 | |
331 | static void handle_sigint(__attribute__((__unused__)) int sig) | |
332 | { | |
7035d92d JA |
333 | struct device_information *dip; |
334 | int i; | |
335 | ||
336 | /* | |
337 | * stop trace so we can reap currently produced data | |
338 | */ | |
339 | for_each_dip(dip, i) { | |
921b05fe JA |
340 | if (dip->fd == -1) |
341 | continue; | |
7035d92d JA |
342 | if (ioctl(dip->fd, BLKTRACESTOP) < 0) |
343 | perror("BLKTRACESTOP"); | |
344 | } | |
345 | ||
8e86c98a JA |
346 | done = 1; |
347 | } | |
348 | ||
eb3c8108 JA |
349 | static int get_dropped_count(const char *buts_name) |
350 | { | |
351 | int fd; | |
352 | char tmp[MAXPATHLEN + 64]; | |
353 | ||
354 | snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped", | |
3d06efea | 355 | debugfs_path, buts_name); |
eb3c8108 JA |
356 | |
357 | fd = open(tmp, O_RDONLY); | |
358 | if (fd < 0) { | |
359 | /* | |
360 | * this may be ok, if the kernel doesn't support dropped counts | |
361 | */ | |
362 | if (errno == ENOENT) | |
363 | return 0; | |
364 | ||
365 | fprintf(stderr, "Couldn't open dropped file %s\n", tmp); | |
366 | return -1; | |
367 | } | |
368 | ||
369 | if (read(fd, tmp, sizeof(tmp)) < 0) { | |
370 | perror(tmp); | |
371 | close(fd); | |
372 | return -1; | |
373 | } | |
374 | ||
375 | close(fd); | |
376 | ||
377 | return atoi(tmp); | |
378 | } | |
379 | ||
e7c9f3ff | 380 | static int start_trace(struct device_information *dip) |
d0ca268b JA |
381 | { |
382 | struct blk_user_trace_setup buts; | |
383 | ||
1f79c4a0 | 384 | memset(&buts, 0, sizeof(buts)); |
129aa440 JA |
385 | buts.buf_size = buf_size; |
386 | buts.buf_nr = buf_nr; | |
d39c04ca | 387 | buts.act_mask = act_mask; |
d0ca268b | 388 | |
ed71a31e JA |
389 | if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) { |
390 | perror("BLKTRACESETUP"); | |
391 | return 1; | |
392 | } | |
393 | ||
394 | if (ioctl(dip->fd, BLKTRACESTART) < 0) { | |
395 | perror("BLKTRACESTART"); | |
d0ca268b JA |
396 | return 1; |
397 | } | |
398 | ||
e7c9f3ff | 399 | memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name)); |
99c1f5ab | 400 | dip_set_tracing(dip, 1); |
d0ca268b JA |
401 | return 0; |
402 | } | |
403 | ||
e7c9f3ff | 404 | static void stop_trace(struct device_information *dip) |
d0ca268b | 405 | { |
99c1f5ab JA |
406 | if (dip_tracing(dip) || kill_running_trace) { |
407 | dip_set_tracing(dip, 0); | |
cf9208ea | 408 | |
7035d92d JA |
409 | /* |
410 | * should be stopped, just don't complain if it isn't | |
411 | */ | |
412 | ioctl(dip->fd, BLKTRACESTOP); | |
413 | ||
ed71a31e JA |
414 | if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0) |
415 | perror("BLKTRACETEARDOWN"); | |
cf9208ea | 416 | |
e7c9f3ff | 417 | close(dip->fd); |
cf9208ea | 418 | dip->fd = -1; |
707b0914 | 419 | } |
d0ca268b JA |
420 | } |
421 | ||
e7c9f3ff NS |
422 | static void stop_all_traces(void) |
423 | { | |
424 | struct device_information *dip; | |
425 | int i; | |
426 | ||
eb3c8108 JA |
427 | for_each_dip(dip, i) { |
428 | dip->drop_count = get_dropped_count(dip->buts_name); | |
e7c9f3ff | 429 | stop_trace(dip); |
eb3c8108 | 430 | } |
e7c9f3ff NS |
431 | } |
432 | ||
ff11d54c | 433 | static void wait_for_data(struct thread_information *tip, int timeout) |
eb3c8108 | 434 | { |
ff11d54c | 435 | struct pollfd pfd = { .fd = tip->fd, .events = POLLIN }; |
eb3c8108 | 436 | |
ff11d54c TZ |
437 | while (!is_done()) { |
438 | if (poll(&pfd, 1, timeout) < 0) { | |
7934e668 JA |
439 | perror("poll"); |
440 | break; | |
441 | } | |
ff11d54c | 442 | if (pfd.revents & POLLIN) |
9db17354 JA |
443 | break; |
444 | if (tip->ofile_stdout) | |
445 | break; | |
ff11d54c | 446 | } |
eb3c8108 JA |
447 | } |
448 | ||
0cc7d25e JA |
449 | static int read_data_file(struct thread_information *tip, void *buf, |
450 | unsigned int len) | |
d0ca268b | 451 | { |
ae9f71b3 | 452 | int ret = 0; |
bbabf03a | 453 | |
9db17354 | 454 | do { |
ff11d54c | 455 | wait_for_data(tip, 100); |
ae9f71b3 | 456 | |
9db17354 JA |
457 | ret = read(tip->fd, buf, len); |
458 | if (!ret) | |
459 | continue; | |
460 | else if (ret > 0) | |
461 | return ret; | |
462 | else { | |
bbabf03a | 463 | if (errno != EAGAIN) { |
a3e4d330 JA |
464 | perror(tip->fn); |
465 | fprintf(stderr,"Thread %d failed read of %s\n", | |
466 | tip->cpu, tip->fn); | |
467 | break; | |
468 | } | |
9db17354 | 469 | continue; |
bbabf03a | 470 | } |
9db17354 | 471 | } while (!is_done()); |
8a43bac5 | 472 | |
bbabf03a | 473 | return ret; |
8e86c98a | 474 | |
8a43bac5 JA |
475 | } |
476 | ||
0cc7d25e JA |
477 | static int read_data_net(struct thread_information *tip, void *buf, |
478 | unsigned int len) | |
8e86c98a | 479 | { |
ff11d54c | 480 | struct net_connection *nc = tip->nc; |
8e86c98a JA |
481 | unsigned int bytes_left = len; |
482 | int ret = 0; | |
483 | ||
484 | do { | |
e0a1988b | 485 | ret = recv(nc->in_fd, buf, bytes_left, MSG_WAITALL); |
8e86c98a JA |
486 | |
487 | if (!ret) | |
488 | continue; | |
489 | else if (ret < 0) { | |
490 | if (errno != EAGAIN) { | |
491 | perror(tip->fn); | |
492 | fprintf(stderr, "server: failed read\n"); | |
493 | return 0; | |
494 | } | |
495 | continue; | |
496 | } else { | |
497 | buf += ret; | |
498 | bytes_left -= ret; | |
499 | } | |
500 | } while (!is_done() && bytes_left); | |
501 | ||
410d7c62 | 502 | return len - bytes_left; |
8e86c98a JA |
503 | } |
504 | ||
8e86c98a JA |
505 | static inline struct tip_subbuf * |
506 | subbuf_fifo_dequeue(struct thread_information *tip) | |
a3e4d330 | 507 | { |
21f55651 JA |
508 | const int head = tip->fifo.head; |
509 | const int next = (head + 1) & (FIFO_SIZE - 1); | |
510 | ||
511 | if (head != tip->fifo.tail) { | |
512 | struct tip_subbuf *ts = tip->fifo.q[head]; | |
513 | ||
514 | store_barrier(); | |
515 | tip->fifo.head = next; | |
516 | return ts; | |
517 | } | |
518 | ||
519 | return NULL; | |
9db17354 | 520 | } |
eb3c8108 | 521 | |
21f55651 JA |
522 | static inline int subbuf_fifo_queue(struct thread_information *tip, |
523 | struct tip_subbuf *ts) | |
9db17354 | 524 | { |
21f55651 JA |
525 | const int tail = tip->fifo.tail; |
526 | const int next = (tail + 1) & (FIFO_SIZE - 1); | |
527 | ||
528 | if (next != tip->fifo.head) { | |
529 | tip->fifo.q[tail] = ts; | |
530 | store_barrier(); | |
531 | tip->fifo.tail = next; | |
532 | return 0; | |
533 | } | |
534 | ||
535 | fprintf(stderr, "fifo too small!\n"); | |
536 | return 1; | |
a3e4d330 JA |
537 | } |
538 | ||
b7106311 JA |
539 | /* |
540 | * For file output, truncate and mmap the file appropriately | |
541 | */ | |
8e86c98a | 542 | static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen) |
b7106311 JA |
543 | { |
544 | int ofd = fileno(tip->ofile); | |
545 | int ret; | |
546 | ||
547 | /* | |
548 | * extend file, if we have to. use chunks of 16 subbuffers. | |
549 | */ | |
550 | if (tip->fs_off + buf_size > tip->fs_buf_len) { | |
551 | if (tip->fs_buf) { | |
5975d309 | 552 | munlock(tip->fs_buf, tip->fs_buf_len); |
b7106311 JA |
553 | munmap(tip->fs_buf, tip->fs_buf_len); |
554 | tip->fs_buf = NULL; | |
555 | } | |
556 | ||
557 | tip->fs_off = tip->fs_size & (page_size - 1); | |
558 | tip->fs_buf_len = (16 * buf_size) - tip->fs_off; | |
559 | tip->fs_max_size += tip->fs_buf_len; | |
560 | ||
561 | if (ftruncate(ofd, tip->fs_max_size) < 0) { | |
562 | perror("ftruncate"); | |
563 | return -1; | |
564 | } | |
565 | ||
566 | tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE, | |
567 | MAP_SHARED, ofd, tip->fs_size - tip->fs_off); | |
568 | if (tip->fs_buf == MAP_FAILED) { | |
569 | perror("mmap"); | |
570 | return -1; | |
571 | } | |
5975d309 | 572 | mlock(tip->fs_buf, tip->fs_buf_len); |
b7106311 JA |
573 | } |
574 | ||
7934e668 | 575 | ret = tip->read_data(tip, tip->fs_buf + tip->fs_off, maxlen); |
b7106311 | 576 | if (ret >= 0) { |
dbfbd6db | 577 | tip->data_read += ret; |
b7106311 JA |
578 | tip->fs_size += ret; |
579 | tip->fs_off += ret; | |
580 | return 0; | |
581 | } | |
582 | ||
583 | return -1; | |
584 | } | |
585 | ||
18eed2a7 JA |
586 | /* |
587 | * Use the copy approach for pipes and network | |
588 | */ | |
589 | static int get_subbuf(struct thread_information *tip, unsigned int maxlen) | |
590 | { | |
591 | struct tip_subbuf *ts = malloc(sizeof(*ts)); | |
592 | int ret; | |
593 | ||
594 | ts->buf = malloc(buf_size); | |
595 | ts->max_len = maxlen; | |
596 | ||
7934e668 | 597 | ret = tip->read_data(tip, ts->buf, ts->max_len); |
18eed2a7 JA |
598 | if (ret > 0) { |
599 | ts->len = ret; | |
dbfbd6db | 600 | tip->data_read += ret; |
7035d92d JA |
601 | if (subbuf_fifo_queue(tip, ts)) |
602 | return -1; | |
18eed2a7 JA |
603 | } |
604 | ||
605 | return ret; | |
606 | } | |
607 | ||
9db17354 | 608 | static void close_thread(struct thread_information *tip) |
a3e4d330 | 609 | { |
9db17354 JA |
610 | if (tip->fd != -1) |
611 | close(tip->fd); | |
612 | if (tip->ofile) | |
613 | fclose(tip->ofile); | |
614 | if (tip->ofile_buffer) | |
615 | free(tip->ofile_buffer); | |
616 | if (tip->fd_buf) | |
617 | free(tip->fd_buf); | |
1c99bc21 | 618 | |
9db17354 JA |
619 | tip->fd = -1; |
620 | tip->ofile = NULL; | |
621 | tip->ofile_buffer = NULL; | |
622 | tip->fd_buf = NULL; | |
a3e4d330 JA |
623 | } |
624 | ||
8e86c98a JA |
625 | static void tip_ftrunc_final(struct thread_information *tip) |
626 | { | |
627 | /* | |
628 | * truncate to right size and cleanup mmap | |
629 | */ | |
c196b5f2 | 630 | if (tip->ofile_mmap && tip->ofile) { |
8e86c98a JA |
631 | int ofd = fileno(tip->ofile); |
632 | ||
633 | if (tip->fs_buf) | |
634 | munmap(tip->fs_buf, tip->fs_buf_len); | |
635 | ||
636 | ftruncate(ofd, tip->fs_size); | |
637 | } | |
638 | } | |
639 | ||
9db17354 | 640 | static void *thread_main(void *arg) |
a3e4d330 | 641 | { |
9db17354 JA |
642 | struct thread_information *tip = arg; |
643 | pid_t pid = getpid(); | |
644 | cpu_set_t cpu_mask; | |
a3e4d330 | 645 | |
9db17354 JA |
646 | CPU_ZERO(&cpu_mask); |
647 | CPU_SET((tip->cpu), &cpu_mask); | |
a3e4d330 | 648 | |
9db17354 JA |
649 | if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) { |
650 | perror("sched_setaffinity"); | |
651 | exit_trace(1); | |
652 | } | |
a3e4d330 | 653 | |
9db17354 | 654 | snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d", |
3d06efea | 655 | debugfs_path, tip->device->buts_name, tip->cpu); |
9db17354 JA |
656 | tip->fd = open(tip->fn, O_RDONLY); |
657 | if (tip->fd < 0) { | |
658 | perror(tip->fn); | |
659 | fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu, | |
660 | tip->fn); | |
661 | exit_trace(1); | |
a3e4d330 JA |
662 | } |
663 | ||
b7106311 | 664 | while (!is_done()) { |
7035d92d | 665 | if (tip->get_subbuf(tip, buf_size) < 0) |
0cc7d25e | 666 | break; |
b7106311 JA |
667 | } |
668 | ||
7035d92d JA |
669 | /* |
670 | * trace is stopped, pull data until we get a short read | |
671 | */ | |
672 | while (tip->get_subbuf(tip, buf_size) > 0) | |
673 | ; | |
674 | ||
8e86c98a JA |
675 | tip_ftrunc_final(tip); |
676 | tip->exited = 1; | |
677 | return NULL; | |
678 | } | |
b7106311 | 679 | |
8e86c98a JA |
680 | static int write_data_net(int fd, void *buf, unsigned int buf_len) |
681 | { | |
682 | unsigned int bytes_left = buf_len; | |
683 | int ret; | |
b7106311 | 684 | |
8e86c98a JA |
685 | while (bytes_left) { |
686 | ret = send(fd, buf, bytes_left, 0); | |
687 | if (ret < 0) { | |
688 | perror("send"); | |
689 | return 1; | |
690 | } | |
691 | ||
692 | buf += ret; | |
693 | bytes_left -= ret; | |
9db17354 | 694 | } |
a3e4d330 | 695 | |
8e86c98a | 696 | return 0; |
a3e4d330 JA |
697 | } |
698 | ||
32f18c48 | 699 | static int net_send_header(struct thread_information *tip, unsigned int len) |
8e86c98a JA |
700 | { |
701 | struct blktrace_net_hdr hdr; | |
8e86c98a JA |
702 | |
703 | hdr.magic = BLK_IO_TRACE_MAGIC; | |
22cd0c02 | 704 | strcpy(hdr.buts_name, tip->device->buts_name); |
8e86c98a | 705 | hdr.cpu = tip->cpu; |
22cd0c02 | 706 | hdr.max_cpus = ncpus; |
32f18c48 | 707 | hdr.len = len; |
ff11d54c | 708 | hdr.cl_id = getpid(); |
8e86c98a | 709 | |
ff11d54c | 710 | return write_data_net(net_out_fd[tip->cpu], &hdr, sizeof(hdr)); |
32f18c48 | 711 | } |
8e86c98a | 712 | |
6a752c90 JA |
713 | /* |
714 | * send header with 0 length to signal end-of-run | |
715 | */ | |
716 | static void net_client_send_close(void) | |
717 | { | |
7934e668 | 718 | struct device_information *dip; |
6a752c90 | 719 | struct blktrace_net_hdr hdr; |
7934e668 | 720 | int i; |
6a752c90 | 721 | |
7934e668 | 722 | for_each_dip(dip, i) { |
7ab2f837 JA |
723 | hdr.magic = BLK_IO_TRACE_MAGIC; |
724 | hdr.max_cpus = ncpus; | |
725 | hdr.len = 0; | |
7934e668 | 726 | strcpy(hdr.buts_name, dip->buts_name); |
7ab2f837 | 727 | hdr.cpu = get_dropped_count(dip->buts_name); |
ff11d54c | 728 | hdr.cl_id = getpid(); |
7ab2f837 | 729 | |
ff11d54c | 730 | write_data_net(net_out_fd[0], &hdr, sizeof(hdr)); |
7934e668 JA |
731 | } |
732 | ||
6a752c90 JA |
733 | } |
734 | ||
32f18c48 JA |
735 | static int flush_subbuf_net(struct thread_information *tip, |
736 | struct tip_subbuf *ts) | |
737 | { | |
738 | if (net_send_header(tip, ts->len)) | |
7934e668 | 739 | return -1; |
ff11d54c | 740 | if (write_data_net(net_out_fd[tip->cpu], ts->buf, ts->len)) |
7934e668 | 741 | return -1; |
8e86c98a | 742 | |
f0597a7e | 743 | free(ts->buf); |
8e86c98a | 744 | free(ts); |
7934e668 | 745 | return 1; |
8e86c98a JA |
746 | } |
747 | ||
f6fead25 JA |
748 | static int net_sendfile(struct thread_information *tip, struct tip_subbuf *ts) |
749 | { | |
ff11d54c | 750 | int ret = sendfile(net_out_fd[tip->cpu], tip->fd, NULL, ts->len); |
11629347 JA |
751 | |
752 | if (ret < 0) { | |
753 | perror("sendfile"); | |
754 | return 1; | |
755 | } else if (ret < (int) ts->len) { | |
756 | fprintf(stderr, "short sendfile send (%d of %d)\n", ret, ts->len); | |
757 | return 1; | |
758 | } | |
759 | ||
760 | return 0; | |
761 | } | |
762 | ||
32f18c48 JA |
763 | static int flush_subbuf_sendfile(struct thread_information *tip, |
764 | struct tip_subbuf *ts) | |
765 | { | |
7934e668 | 766 | int ret = -1; |
18eed2a7 | 767 | |
f6fead25 | 768 | if (net_send_header(tip, ts->len)) |
11629347 | 769 | goto err; |
f6fead25 | 770 | if (net_sendfile(tip, ts)) |
11629347 | 771 | goto err; |
32f18c48 | 772 | |
f6fead25 | 773 | tip->data_read += ts->len; |
e076d33b | 774 | tip->ofile_offset += buf_size; |
7934e668 | 775 | ret = 1; |
11629347 | 776 | err: |
32f18c48 | 777 | free(ts); |
11629347 | 778 | return ret; |
32f18c48 JA |
779 | } |
780 | ||
ff11d54c | 781 | static int get_subbuf_sendfile(struct thread_information *tip, |
d042efdf | 782 | __attribute__((__unused__)) unsigned int maxlen) |
ff11d54c TZ |
783 | { |
784 | struct tip_subbuf *ts; | |
785 | struct stat sb; | |
786 | unsigned int ready; | |
ff11d54c TZ |
787 | |
788 | wait_for_data(tip, -1); | |
789 | ||
790 | if (fstat(tip->fd, &sb) < 0) { | |
791 | perror("trace stat"); | |
792 | return -1; | |
793 | } | |
794 | ready = sb.st_size - tip->data_queued; | |
795 | if (!ready) { | |
796 | usleep(1000); | |
797 | return 0; | |
798 | } | |
799 | ||
800 | ts = malloc(sizeof(*ts)); | |
801 | ts->buf = NULL; | |
802 | ts->max_len = 0; | |
803 | ts->len = ready; | |
804 | tip->data_queued += ready; | |
805 | ||
d042efdf JA |
806 | if (flush_subbuf_sendfile(tip, ts) < 0) |
807 | return -1; | |
ff11d54c TZ |
808 | |
809 | return ready; | |
810 | } | |
811 | ||
8e86c98a JA |
812 | static int write_data(struct thread_information *tip, void *buf, |
813 | unsigned int buf_len) | |
8a43bac5 | 814 | { |
7126171a | 815 | int ret; |
8a43bac5 | 816 | |
6480258a JA |
817 | if (!buf_len) |
818 | return 0; | |
819 | ||
7126171a JA |
820 | while (1) { |
821 | ret = fwrite(buf, buf_len, 1, tip->ofile); | |
007c233c | 822 | if (ret == 1) |
8a43bac5 JA |
823 | break; |
824 | ||
db6fe5bc JA |
825 | if (ret < 0) { |
826 | perror("write"); | |
827 | return 1; | |
8a43bac5 | 828 | } |
d0ca268b JA |
829 | } |
830 | ||
9db17354 | 831 | if (tip->ofile_stdout) |
7126171a JA |
832 | fflush(tip->ofile); |
833 | ||
8a43bac5 JA |
834 | return 0; |
835 | } | |
836 | ||
8e86c98a JA |
837 | static int flush_subbuf_file(struct thread_information *tip, |
838 | struct tip_subbuf *ts) | |
8a43bac5 | 839 | { |
9db17354 JA |
840 | unsigned int offset = 0; |
841 | struct blk_io_trace *t; | |
842 | int pdu_len, events = 0; | |
8a43bac5 | 843 | |
9db17354 | 844 | /* |
7de86b12 | 845 | * surplus from last run |
9db17354 | 846 | */ |
7de86b12 AB |
847 | if (tip->leftover_ts) { |
848 | struct tip_subbuf *prev_ts = tip->leftover_ts; | |
849 | ||
9e8b753c | 850 | if (prev_ts->len + ts->len > prev_ts->max_len) { |
7de86b12 AB |
851 | prev_ts->max_len += ts->len; |
852 | prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len); | |
853 | } | |
854 | ||
9e8b753c | 855 | memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len); |
7de86b12 AB |
856 | prev_ts->len += ts->len; |
857 | ||
858 | free(ts->buf); | |
859 | free(ts); | |
860 | ||
861 | ts = prev_ts; | |
862 | tip->leftover_ts = NULL; | |
9db17354 | 863 | } |
d0ca268b | 864 | |
9db17354 JA |
865 | while (offset + sizeof(*t) <= ts->len) { |
866 | t = ts->buf + offset; | |
3a9d6c13 | 867 | |
9cfa6c2b AB |
868 | if (verify_trace(t)) { |
869 | write_data(tip, ts->buf, offset); | |
9db17354 | 870 | return -1; |
9cfa6c2b | 871 | } |
3a9d6c13 | 872 | |
9db17354 | 873 | pdu_len = t->pdu_len; |
3a9d6c13 | 874 | |
9db17354 | 875 | if (offset + sizeof(*t) + pdu_len > ts->len) |
3a9d6c13 | 876 | break; |
4b5db44a | 877 | |
9db17354 JA |
878 | offset += sizeof(*t) + pdu_len; |
879 | tip->events_processed++; | |
b7106311 | 880 | tip->data_read += sizeof(*t) + pdu_len; |
9db17354 | 881 | events++; |
3a9d6c13 JA |
882 | } |
883 | ||
9cfa6c2b AB |
884 | if (write_data(tip, ts->buf, offset)) |
885 | return -1; | |
886 | ||
3a9d6c13 | 887 | /* |
9db17354 | 888 | * leftover bytes, save them for next time |
3a9d6c13 | 889 | */ |
9db17354 | 890 | if (offset != ts->len) { |
7de86b12 | 891 | tip->leftover_ts = ts; |
9e8b753c JA |
892 | ts->len -= offset; |
893 | memmove(ts->buf, ts->buf + offset, ts->len); | |
7de86b12 AB |
894 | } else { |
895 | free(ts->buf); | |
896 | free(ts); | |
9db17354 | 897 | } |
4b5db44a | 898 | |
9db17354 | 899 | return events; |
4b5db44a JA |
900 | } |
901 | ||
9db17354 | 902 | static int write_tip_events(struct thread_information *tip) |
d5396421 | 903 | { |
21f55651 | 904 | struct tip_subbuf *ts = subbuf_fifo_dequeue(tip); |
d5396421 | 905 | |
0cc7d25e JA |
906 | if (ts) |
907 | return tip->flush_subbuf(tip, ts); | |
91816d54 | 908 | |
9db17354 | 909 | return 0; |
91816d54 JA |
910 | } |
911 | ||
9db17354 JA |
912 | /* |
913 | * scans the tips we know and writes out the subbuffers we accumulate | |
914 | */ | |
915 | static void get_and_write_events(void) | |
d0ca268b | 916 | { |
9db17354 JA |
917 | struct device_information *dip; |
918 | struct thread_information *tip; | |
27223f19 | 919 | int i, j, events, ret, tips_running; |
d0ca268b | 920 | |
9db17354 JA |
921 | while (!is_done()) { |
922 | events = 0; | |
d0ca268b | 923 | |
9db17354 JA |
924 | for_each_dip(dip, i) { |
925 | for_each_tip(dip, tip, j) { | |
926 | ret = write_tip_events(tip); | |
927 | if (ret > 0) | |
928 | events += ret; | |
929 | } | |
930 | } | |
d0ca268b | 931 | |
9db17354 | 932 | if (!events) |
7934e668 | 933 | usleep(100000); |
d0ca268b JA |
934 | } |
935 | ||
a3e4d330 | 936 | /* |
9db17354 | 937 | * reap stored events |
a3e4d330 | 938 | */ |
9db17354 JA |
939 | do { |
940 | events = 0; | |
27223f19 | 941 | tips_running = 0; |
9db17354 JA |
942 | for_each_dip(dip, i) { |
943 | for_each_tip(dip, tip, j) { | |
944 | ret = write_tip_events(tip); | |
945 | if (ret > 0) | |
946 | events += ret; | |
27223f19 | 947 | tips_running += !tip->exited; |
9db17354 | 948 | } |
69e65a9e | 949 | } |
9db17354 | 950 | usleep(10); |
27223f19 | 951 | } while (events || tips_running); |
d0ca268b JA |
952 | } |
953 | ||
b7106311 JA |
954 | static void wait_for_threads(void) |
955 | { | |
956 | /* | |
8e86c98a JA |
957 | * for piped or network output, poll and fetch data for writeout. |
958 | * for files, we just wait around for trace threads to exit | |
b7106311 | 959 | */ |
8e86c98a | 960 | if ((output_name && !strcmp(output_name, "-")) || |
ff11d54c | 961 | ((net_mode == Net_client) && !net_use_sendfile)) |
b7106311 JA |
962 | get_and_write_events(); |
963 | else { | |
964 | struct device_information *dip; | |
965 | struct thread_information *tip; | |
966 | int i, j, tips_running; | |
967 | ||
968 | do { | |
969 | tips_running = 0; | |
7934e668 | 970 | usleep(100000); |
b7106311 JA |
971 | |
972 | for_each_dip(dip, i) | |
973 | for_each_tip(dip, tip, j) | |
974 | tips_running += !tip->exited; | |
975 | } while (tips_running); | |
976 | } | |
6a752c90 JA |
977 | |
978 | if (net_mode == Net_client) | |
979 | net_client_send_close(); | |
b7106311 JA |
980 | } |
981 | ||
97159c02 JA |
982 | static int fill_ofname(struct device_information *dip, |
983 | struct thread_information *tip, char *dst, | |
e3bf54d8 | 984 | char *buts_name) |
8e86c98a | 985 | { |
e3bf54d8 | 986 | struct stat sb; |
8e86c98a JA |
987 | int len = 0; |
988 | ||
989 | if (output_dir) | |
990 | len = sprintf(dst, "%s/", output_dir); | |
dd870ef6 AB |
991 | else |
992 | len = sprintf(dst, "./"); | |
8e86c98a | 993 | |
e3bf54d8 | 994 | if (net_mode == Net_server) { |
ff11d54c | 995 | struct net_connection *nc = tip->nc; |
e0a1988b | 996 | |
ff11d54c TZ |
997 | len += sprintf(dst + len, "%s-", inet_ntoa(nc->ch->cl_in_addr)); |
998 | len += strftime(dst + len, 64, "%F-%T/", gmtime(&dip->cl_connect_time)); | |
e3bf54d8 JA |
999 | } |
1000 | ||
1001 | if (stat(dst, &sb) < 0) { | |
1002 | if (errno != ENOENT) { | |
1003 | perror("stat"); | |
1004 | return 1; | |
1005 | } | |
1006 | if (mkdir(dst, 0755) < 0) { | |
1007 | perror(dst); | |
1008 | fprintf(stderr, "Can't make output dir\n"); | |
1009 | return 1; | |
1010 | } | |
1011 | } | |
1012 | ||
8e86c98a | 1013 | if (output_name) |
e3bf54d8 | 1014 | sprintf(dst + len, "%s.blktrace.%d", output_name, tip->cpu); |
8e86c98a | 1015 | else |
e3bf54d8 JA |
1016 | sprintf(dst + len, "%s.blktrace.%d", buts_name, tip->cpu); |
1017 | ||
1018 | return 0; | |
8e86c98a JA |
1019 | } |
1020 | ||
0cc7d25e JA |
1021 | static void fill_ops(struct thread_information *tip) |
1022 | { | |
1023 | /* | |
1024 | * setup ops | |
1025 | */ | |
32f18c48 | 1026 | if (net_mode == Net_client) { |
36808255 | 1027 | if (net_use_sendfile) { |
32f18c48 | 1028 | tip->get_subbuf = get_subbuf_sendfile; |
ff11d54c | 1029 | tip->flush_subbuf = NULL; |
32f18c48 JA |
1030 | } else { |
1031 | tip->get_subbuf = get_subbuf; | |
1032 | tip->flush_subbuf = flush_subbuf_net; | |
1033 | } | |
1034 | } else { | |
1035 | if (tip->ofile_mmap) | |
1036 | tip->get_subbuf = mmap_subbuf; | |
1037 | else | |
1038 | tip->get_subbuf = get_subbuf; | |
0cc7d25e | 1039 | |
0cc7d25e | 1040 | tip->flush_subbuf = flush_subbuf_file; |
32f18c48 JA |
1041 | } |
1042 | ||
0cc7d25e JA |
1043 | if (net_mode == Net_server) |
1044 | tip->read_data = read_data_net; | |
1045 | else | |
1046 | tip->read_data = read_data_file; | |
1047 | } | |
1048 | ||
ddf22842 JA |
1049 | static int tip_open_output(struct device_information *dip, |
1050 | struct thread_information *tip) | |
d0ca268b | 1051 | { |
ddf22842 | 1052 | int pipeline = output_name && !strcmp(output_name, "-"); |
8e86c98a | 1053 | int mode, vbuf_size; |
e3bf54d8 | 1054 | char op[128]; |
d0ca268b | 1055 | |
ddf22842 JA |
1056 | if (net_mode == Net_client) { |
1057 | tip->ofile = NULL; | |
1058 | tip->ofile_stdout = 0; | |
1059 | tip->ofile_mmap = 0; | |
0c0b75b4 | 1060 | goto done; |
ddf22842 JA |
1061 | } else if (pipeline) { |
1062 | tip->ofile = fdopen(STDOUT_FILENO, "w"); | |
1063 | tip->ofile_stdout = 1; | |
1064 | tip->ofile_mmap = 0; | |
1065 | mode = _IOLBF; | |
1066 | vbuf_size = 512; | |
1067 | } else { | |
97159c02 | 1068 | if (fill_ofname(dip, tip, op, dip->buts_name)) |
e3bf54d8 | 1069 | return 1; |
ddf22842 JA |
1070 | tip->ofile = fopen(op, "w+"); |
1071 | tip->ofile_stdout = 0; | |
1072 | tip->ofile_mmap = 1; | |
1073 | mode = _IOFBF; | |
1074 | vbuf_size = OFILE_BUF; | |
1075 | } | |
d5396421 | 1076 | |
0c0b75b4 | 1077 | if (tip->ofile == NULL) { |
ddf22842 JA |
1078 | perror(op); |
1079 | return 1; | |
1080 | } | |
d5396421 | 1081 | |
0c0b75b4 JA |
1082 | tip->ofile_buffer = malloc(vbuf_size); |
1083 | if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) { | |
1084 | perror("setvbuf"); | |
1085 | close_thread(tip); | |
1086 | return 1; | |
ddf22842 JA |
1087 | } |
1088 | ||
0c0b75b4 | 1089 | done: |
ddf22842 JA |
1090 | fill_ops(tip); |
1091 | return 0; | |
1092 | } | |
007c233c | 1093 | |
ddf22842 JA |
1094 | static int start_threads(struct device_information *dip) |
1095 | { | |
1096 | struct thread_information *tip; | |
1097 | int j; | |
1098 | ||
1099 | for_each_tip(dip, tip, j) { | |
1100 | tip->cpu = j; | |
1101 | tip->device = dip; | |
1102 | tip->events_processed = 0; | |
11eedd9b | 1103 | tip->fd = -1; |
ddf22842 JA |
1104 | memset(&tip->fifo, 0, sizeof(tip->fifo)); |
1105 | tip->leftover_ts = NULL; | |
1106 | ||
1107 | if (tip_open_output(dip, tip)) | |
1108 | return 1; | |
0cc7d25e | 1109 | |
9db17354 | 1110 | if (pthread_create(&tip->thread, NULL, thread_main, tip)) { |
e7c9f3ff | 1111 | perror("pthread_create"); |
007c233c | 1112 | close_thread(tip); |
e7c9f3ff | 1113 | return 1; |
d0ca268b JA |
1114 | } |
1115 | } | |
1116 | ||
e7c9f3ff | 1117 | return 0; |
d0ca268b JA |
1118 | } |
1119 | ||
e7c9f3ff | 1120 | static void stop_threads(struct device_information *dip) |
3aabcd89 | 1121 | { |
e7c9f3ff | 1122 | struct thread_information *tip; |
91816d54 | 1123 | unsigned long ret; |
007c233c JA |
1124 | int i; |
1125 | ||
9db17354 | 1126 | for_each_tip(dip, tip, i) { |
91816d54 | 1127 | (void) pthread_join(tip->thread, (void *) &ret); |
9db17354 JA |
1128 | close_thread(tip); |
1129 | } | |
3aabcd89 JA |
1130 | } |
1131 | ||
e7c9f3ff | 1132 | static void stop_all_threads(void) |
72ca8801 | 1133 | { |
e7c9f3ff | 1134 | struct device_information *dip; |
72ca8801 NS |
1135 | int i; |
1136 | ||
99c1f5ab | 1137 | for_each_dip(dip, i) |
e7c9f3ff NS |
1138 | stop_threads(dip); |
1139 | } | |
1140 | ||
1141 | static void stop_all_tracing(void) | |
1142 | { | |
1143 | struct device_information *dip; | |
91816d54 | 1144 | int i; |
007c233c | 1145 | |
91816d54 | 1146 | for_each_dip(dip, i) |
e7c9f3ff | 1147 | stop_trace(dip); |
72ca8801 NS |
1148 | } |
1149 | ||
1150 | static void exit_trace(int status) | |
1151 | { | |
eb3c8108 JA |
1152 | if (!is_trace_stopped()) { |
1153 | trace_stopped = 1; | |
1154 | stop_all_threads(); | |
1155 | stop_all_tracing(); | |
1156 | } | |
1157 | ||
72ca8801 NS |
1158 | exit(status); |
1159 | } | |
1160 | ||
e7c9f3ff NS |
1161 | static int resize_devices(char *path) |
1162 | { | |
1163 | int size = (ndevs + 1) * sizeof(struct device_information); | |
1164 | ||
1165 | device_information = realloc(device_information, size); | |
1166 | if (!device_information) { | |
1167 | fprintf(stderr, "Out of memory, device %s (%d)\n", path, size); | |
1168 | return 1; | |
1169 | } | |
1170 | device_information[ndevs].path = path; | |
1171 | ndevs++; | |
1172 | return 0; | |
1173 | } | |
1174 | ||
1175 | static int open_devices(void) | |
d0ca268b | 1176 | { |
e7c9f3ff | 1177 | struct device_information *dip; |
d0ca268b | 1178 | int i; |
d0ca268b | 1179 | |
99c1f5ab | 1180 | for_each_dip(dip, i) { |
cf9208ea | 1181 | dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK); |
e7c9f3ff NS |
1182 | if (dip->fd < 0) { |
1183 | perror(dip->path); | |
1184 | return 1; | |
1185 | } | |
1186 | } | |
99c1f5ab | 1187 | |
e7c9f3ff NS |
1188 | return 0; |
1189 | } | |
1190 | ||
1191 | static int start_devices(void) | |
1192 | { | |
1193 | struct device_information *dip; | |
1194 | int i, j, size; | |
1195 | ||
1196 | size = ncpus * sizeof(struct thread_information); | |
1197 | thread_information = malloc(size * ndevs); | |
1198 | if (!thread_information) { | |
1199 | fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs); | |
1200 | return 1; | |
1201 | } | |
d5396421 | 1202 | |
99c1f5ab | 1203 | for_each_dip(dip, i) { |
e7c9f3ff NS |
1204 | if (start_trace(dip)) { |
1205 | close(dip->fd); | |
1206 | fprintf(stderr, "Failed to start trace on %s\n", | |
1207 | dip->path); | |
1208 | break; | |
1209 | } | |
1210 | } | |
99c1f5ab | 1211 | |
e7c9f3ff | 1212 | if (i != ndevs) { |
ce020676 | 1213 | __for_each_dip(dip, device_information, i, j) |
e7c9f3ff | 1214 | stop_trace(dip); |
99c1f5ab | 1215 | |
e7c9f3ff NS |
1216 | return 1; |
1217 | } | |
1218 | ||
99c1f5ab | 1219 | for_each_dip(dip, i) { |
e7c9f3ff NS |
1220 | dip->threads = thread_information + (i * ncpus); |
1221 | if (start_threads(dip)) { | |
1222 | fprintf(stderr, "Failed to start worker threads\n"); | |
1223 | break; | |
1224 | } | |
1225 | } | |
99c1f5ab | 1226 | |
e7c9f3ff | 1227 | if (i != ndevs) { |
ce020676 | 1228 | __for_each_dip(dip, device_information, i, j) |
e7c9f3ff | 1229 | stop_threads(dip); |
99c1f5ab | 1230 | for_each_dip(dip, i) |
e7c9f3ff | 1231 | stop_trace(dip); |
99c1f5ab | 1232 | |
e7c9f3ff | 1233 | return 1; |
d0ca268b JA |
1234 | } |
1235 | ||
e7c9f3ff | 1236 | return 0; |
d0ca268b JA |
1237 | } |
1238 | ||
e0a1988b | 1239 | static void show_stats(struct device_information *dips, int ndips, int cpus) |
e7c9f3ff | 1240 | { |
e7c9f3ff NS |
1241 | struct device_information *dip; |
1242 | struct thread_information *tip; | |
b7106311 | 1243 | unsigned long long events_processed, data_read; |
eb3c8108 | 1244 | unsigned long total_drops; |
2f903295 | 1245 | int i, j, no_stdout = 0; |
eb3c8108 JA |
1246 | |
1247 | if (is_stat_shown()) | |
1248 | return; | |
1249 | ||
2f903295 JA |
1250 | if (output_name && !strcmp(output_name, "-")) |
1251 | no_stdout = 1; | |
1252 | ||
eb3c8108 | 1253 | stat_shown = 1; |
428683db | 1254 | |
56070ea4 | 1255 | total_drops = 0; |
ce020676 | 1256 | __for_each_dip(dip, dips, ndips, i) { |
2f903295 | 1257 | if (!no_stdout) |
56070ea4 | 1258 | printf("Device: %s\n", dip->path); |
e7c9f3ff | 1259 | events_processed = 0; |
b7106311 | 1260 | data_read = 0; |
ce020676 | 1261 | __for_each_tip(dip, tip, cpus, j) { |
2f903295 | 1262 | if (!no_stdout) |
b7106311 JA |
1263 | printf(" CPU%3d: %20lu events, %8llu KiB data\n", |
1264 | tip->cpu, tip->events_processed, | |
54824c20 | 1265 | (tip->data_read + 1023) >> 10); |
e7c9f3ff | 1266 | events_processed += tip->events_processed; |
b7106311 | 1267 | data_read += tip->data_read; |
e7c9f3ff | 1268 | } |
eb3c8108 | 1269 | total_drops += dip->drop_count; |
2f903295 | 1270 | if (!no_stdout) |
b7106311 JA |
1271 | printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n", |
1272 | events_processed, dip->drop_count, | |
18d8437d | 1273 | (data_read + 1023) >> 10); |
e7c9f3ff | 1274 | } |
56070ea4 JA |
1275 | |
1276 | if (total_drops) | |
1277 | fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n"); | |
e7c9f3ff | 1278 | } |
52724a0e | 1279 | |
e0a1988b | 1280 | static struct device_information *net_get_dip(struct net_connection *nc, |
ff11d54c | 1281 | char *buts_name, u32 cl_id) |
8e86c98a | 1282 | { |
ff11d54c TZ |
1283 | struct device_information *dip, *cl_dip = NULL; |
1284 | struct cl_host *ch = nc->ch; | |
8e86c98a JA |
1285 | int i; |
1286 | ||
ff11d54c TZ |
1287 | for (i = 0; i < ch->ndevs; i++) { |
1288 | dip = &ch->device_information[i]; | |
8e86c98a | 1289 | |
22cd0c02 JA |
1290 | if (!strcmp(dip->buts_name, buts_name)) |
1291 | return dip; | |
ff11d54c TZ |
1292 | |
1293 | if (dip->cl_id == cl_id) | |
1294 | cl_dip = dip; | |
8e86c98a JA |
1295 | } |
1296 | ||
ff11d54c TZ |
1297 | ch->device_information = realloc(ch->device_information, (ch->ndevs + 1) * sizeof(*dip)); |
1298 | dip = &ch->device_information[ch->ndevs]; | |
921b05fe JA |
1299 | memset(dip, 0, sizeof(*dip)); |
1300 | dip->fd = -1; | |
ff11d54c TZ |
1301 | dip->ch = ch; |
1302 | dip->cl_id = cl_id; | |
1303 | if (cl_dip) | |
1304 | dip->cl_connect_time = cl_dip->cl_connect_time; | |
1305 | else | |
1306 | dip->cl_connect_time = nc->connect_time; | |
22cd0c02 | 1307 | strcpy(dip->buts_name, buts_name); |
921b05fe | 1308 | dip->path = strdup(buts_name); |
7ab2f837 | 1309 | dip->trace_started = 1; |
ff11d54c | 1310 | ch->ndevs++; |
e0a1988b JA |
1311 | dip->threads = malloc(nc->ncpus * sizeof(struct thread_information)); |
1312 | memset(dip->threads, 0, nc->ncpus * sizeof(struct thread_information)); | |
22cd0c02 JA |
1313 | |
1314 | /* | |
1315 | * open all files | |
1316 | */ | |
e0a1988b | 1317 | for (i = 0; i < nc->ncpus; i++) { |
22cd0c02 | 1318 | struct thread_information *tip = &dip->threads[i]; |
8e86c98a | 1319 | |
22cd0c02 | 1320 | tip->cpu = i; |
22cd0c02 | 1321 | tip->device = dip; |
1366e53a | 1322 | tip->fd = -1; |
ff11d54c TZ |
1323 | tip->nc = nc; |
1324 | ||
ddf22842 | 1325 | if (tip_open_output(dip, tip)) |
22cd0c02 | 1326 | return NULL; |
ff11d54c TZ |
1327 | |
1328 | tip->nc = NULL; | |
8e86c98a JA |
1329 | } |
1330 | ||
22cd0c02 JA |
1331 | return dip; |
1332 | } | |
1333 | ||
e0a1988b JA |
1334 | static struct thread_information *net_get_tip(struct net_connection *nc, |
1335 | struct blktrace_net_hdr *bnh) | |
22cd0c02 JA |
1336 | { |
1337 | struct device_information *dip; | |
ff11d54c | 1338 | struct thread_information *tip; |
22cd0c02 | 1339 | |
e0a1988b | 1340 | nc->ncpus = bnh->max_cpus; |
ff11d54c | 1341 | dip = net_get_dip(nc, bnh->buts_name, bnh->cl_id); |
7ab2f837 JA |
1342 | if (!dip->trace_started) { |
1343 | fprintf(stderr, "Events for closed devices %s\n", dip->buts_name); | |
1344 | return NULL; | |
1345 | } | |
1346 | ||
ff11d54c TZ |
1347 | tip = &dip->threads[bnh->cpu]; |
1348 | if (!tip->nc) | |
1349 | tip->nc = nc; | |
1350 | ||
1351 | return tip; | |
8e86c98a JA |
1352 | } |
1353 | ||
e0a1988b JA |
1354 | static int net_get_header(struct net_connection *nc, |
1355 | struct blktrace_net_hdr *bnh) | |
8e86c98a | 1356 | { |
e0a1988b | 1357 | int fl = fcntl(nc->in_fd, F_GETFL); |
8e86c98a JA |
1358 | int bytes_left, ret; |
1359 | void *p = bnh; | |
1360 | ||
e0a1988b | 1361 | fcntl(nc->in_fd, F_SETFL, fl | O_NONBLOCK); |
8e86c98a JA |
1362 | bytes_left = sizeof(*bnh); |
1363 | while (bytes_left && !is_done()) { | |
e0a1988b | 1364 | ret = recv(nc->in_fd, p, bytes_left, MSG_WAITALL); |
8e86c98a JA |
1365 | if (ret < 0) { |
1366 | if (errno != EAGAIN) { | |
1367 | perror("recv header"); | |
1368 | return 1; | |
1369 | } | |
7934e668 | 1370 | usleep(1000); |
8e86c98a JA |
1371 | continue; |
1372 | } else if (!ret) { | |
7934e668 | 1373 | usleep(1000); |
8e86c98a JA |
1374 | continue; |
1375 | } else { | |
1376 | p += ret; | |
1377 | bytes_left -= ret; | |
1378 | } | |
1379 | } | |
e0a1988b | 1380 | fcntl(nc->in_fd, F_SETFL, fl & ~O_NONBLOCK); |
227f89ff | 1381 | return bytes_left; |
8e86c98a JA |
1382 | } |
1383 | ||
e0a1988b JA |
1384 | /* |
1385 | * finalize a net client: truncate files, show stats, cleanup, etc | |
1386 | */ | |
ff11d54c | 1387 | static void device_done(struct net_connection *nc, struct device_information *dip) |
e0a1988b | 1388 | { |
e0a1988b | 1389 | struct thread_information *tip; |
ff11d54c | 1390 | int i; |
e0a1988b | 1391 | |
ff11d54c TZ |
1392 | __for_each_tip(dip, tip, nc->ncpus, i) |
1393 | tip_ftrunc_final(tip); | |
e0a1988b | 1394 | |
ff11d54c | 1395 | show_stats(dip, 1, nc->ncpus); |
e0a1988b JA |
1396 | |
1397 | /* | |
1398 | * cleanup for next run | |
1399 | */ | |
ff11d54c TZ |
1400 | __for_each_tip(dip, tip, nc->ncpus, i) { |
1401 | if (tip->ofile) | |
1402 | fclose(tip->ofile); | |
e0a1988b JA |
1403 | } |
1404 | ||
ff11d54c TZ |
1405 | free(dip->threads); |
1406 | free(dip->path); | |
e0a1988b JA |
1407 | |
1408 | close(nc->in_fd); | |
1409 | nc->in_fd = -1; | |
1410 | ||
ff11d54c TZ |
1411 | stat_shown = 0; |
1412 | } | |
e0a1988b | 1413 | |
ff11d54c TZ |
1414 | static inline int in_addr_eq(struct in_addr a, struct in_addr b) |
1415 | { | |
1416 | return a.s_addr == b.s_addr; | |
1417 | } | |
1418 | ||
1419 | static void net_add_client_host(struct cl_host *ch) | |
1420 | { | |
1421 | ch->list_next = cl_host_list; | |
1422 | cl_host_list = ch; | |
1423 | cl_hosts++; | |
1424 | } | |
1425 | ||
1426 | static void net_remove_client_host(struct cl_host *ch) | |
1427 | { | |
1428 | struct cl_host *p, *c; | |
1429 | ||
1430 | for (p = c = cl_host_list; c; c = c->list_next) { | |
1431 | if (c == ch) { | |
1432 | if (p == c) | |
1433 | cl_host_list = c->list_next; | |
1434 | else | |
1435 | p->list_next = c->list_next; | |
1436 | cl_hosts--; | |
1437 | return; | |
1438 | } | |
1439 | p = c; | |
e0a1988b | 1440 | } |
ff11d54c | 1441 | } |
e0a1988b | 1442 | |
ff11d54c TZ |
1443 | static struct cl_host *net_find_client_host(struct in_addr cl_in_addr) |
1444 | { | |
1445 | struct cl_host *ch = cl_host_list; | |
1446 | ||
1447 | while (ch) { | |
1448 | if (in_addr_eq(ch->cl_in_addr, cl_in_addr)) | |
1449 | return ch; | |
1450 | ch = ch->list_next; | |
1451 | } | |
1452 | ||
1453 | return NULL; | |
1454 | } | |
1455 | ||
1456 | /* | |
1457 | * finalize a net client: truncate files, show stats, cleanup, etc | |
1458 | */ | |
1459 | static void net_client_host_done(struct cl_host *ch) | |
1460 | { | |
1461 | free(ch->device_information); | |
1462 | free(ch->net_connections); | |
1463 | net_connects -= ch->nconn; | |
1464 | net_remove_client_host(ch); | |
1465 | free(ch); | |
e0a1988b JA |
1466 | } |
1467 | ||
1468 | /* | |
1469 | * handle incoming events from a net client | |
1470 | */ | |
1471 | static int net_client_data(struct net_connection *nc) | |
8e86c98a JA |
1472 | { |
1473 | struct thread_information *tip; | |
1474 | struct blktrace_net_hdr bnh; | |
1475 | ||
e0a1988b | 1476 | if (net_get_header(nc, &bnh)) |
8e86c98a JA |
1477 | return 1; |
1478 | ||
1479 | if (data_is_native == -1 && check_data_endianness(bnh.magic)) { | |
1480 | fprintf(stderr, "server: received data is bad\n"); | |
1481 | return 1; | |
1482 | } | |
1483 | ||
1484 | if (!data_is_native) { | |
227f89ff | 1485 | bnh.magic = be32_to_cpu(bnh.magic); |
8e86c98a JA |
1486 | bnh.cpu = be32_to_cpu(bnh.cpu); |
1487 | bnh.len = be32_to_cpu(bnh.len); | |
ff11d54c | 1488 | bnh.cl_id = be32_to_cpu(bnh.cl_id); |
8e86c98a JA |
1489 | } |
1490 | ||
227f89ff JA |
1491 | if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { |
1492 | fprintf(stderr, "server: bad data magic\n"); | |
1493 | return 1; | |
1494 | } | |
1495 | ||
6a752c90 JA |
1496 | /* |
1497 | * len == 0 means that the other end signalled end-of-run | |
1498 | */ | |
1499 | if (!bnh.len) { | |
7934e668 JA |
1500 | /* |
1501 | * overload cpu count with dropped events | |
1502 | */ | |
1503 | struct device_information *dip; | |
1504 | ||
ff11d54c | 1505 | dip = net_get_dip(nc, bnh.buts_name, bnh.cl_id); |
7934e668 | 1506 | dip->drop_count = bnh.cpu; |
7ab2f837 | 1507 | dip->trace_started = 0; |
7934e668 | 1508 | |
e0a1988b | 1509 | printf("server: end of run for %s\n", dip->buts_name); |
ff11d54c TZ |
1510 | device_done(nc, dip); |
1511 | ||
1512 | if (++nc->ch->ndevs_done == nc->ch->ndevs) | |
1513 | net_client_host_done(nc->ch); | |
e0a1988b | 1514 | return 0; |
6a752c90 JA |
1515 | } |
1516 | ||
e0a1988b | 1517 | tip = net_get_tip(nc, &bnh); |
8e86c98a JA |
1518 | if (!tip) |
1519 | return 1; | |
1520 | ||
1521 | if (mmap_subbuf(tip, bnh.len)) | |
1522 | return 1; | |
1523 | ||
1524 | return 0; | |
1525 | } | |
1526 | ||
e0a1988b | 1527 | static void net_add_connection(int listen_fd, struct sockaddr_in *addr) |
659bcc3f | 1528 | { |
e0a1988b JA |
1529 | socklen_t socklen = sizeof(*addr); |
1530 | struct net_connection *nc; | |
ff11d54c TZ |
1531 | struct cl_host *ch; |
1532 | int in_fd; | |
659bcc3f | 1533 | |
ff11d54c TZ |
1534 | in_fd = accept(listen_fd, (struct sockaddr *) addr, &socklen); |
1535 | if (in_fd < 0) { | |
1536 | perror("accept"); | |
e0a1988b | 1537 | return; |
659bcc3f | 1538 | } |
659bcc3f | 1539 | |
ff11d54c TZ |
1540 | ch = net_find_client_host(addr->sin_addr); |
1541 | if (!ch) { | |
1542 | if (cl_hosts == NET_MAX_CL_HOSTS) { | |
1543 | fprintf(stderr, "server: no more clients allowed\n"); | |
1544 | return; | |
1545 | } | |
1546 | ch = malloc(sizeof(struct cl_host)); | |
1547 | memset(ch, 0, sizeof(*ch)); | |
1548 | ch->cl_in_addr = addr->sin_addr; | |
1549 | net_add_client_host(ch); | |
659bcc3f JA |
1550 | } |
1551 | ||
ff11d54c TZ |
1552 | ch->net_connections = realloc(ch->net_connections, (ch->nconn + 1) * sizeof(*nc)); |
1553 | nc = &ch->net_connections[ch->nconn++]; | |
1554 | memset(nc, 0, sizeof(*nc)); | |
1555 | ||
e0a1988b JA |
1556 | printf("server: connection from %s\n", inet_ntoa(addr->sin_addr)); |
1557 | time(&nc->connect_time); | |
ff11d54c TZ |
1558 | nc->ch = ch; |
1559 | nc->in_fd = in_fd; | |
e0a1988b JA |
1560 | net_connects++; |
1561 | } | |
1562 | ||
1563 | /* | |
1564 | * event driven loop, handle new incoming connections and data from | |
1565 | * existing connections | |
1566 | */ | |
1567 | static void net_server_handle_connections(int listen_fd, | |
1568 | struct sockaddr_in *addr) | |
1569 | { | |
ff11d54c TZ |
1570 | struct pollfd *pfds = NULL; |
1571 | struct net_connection **ncs = NULL; | |
1572 | int max_connects = 0; | |
1573 | int i, nconns, events; | |
1574 | struct cl_host *ch; | |
1575 | struct net_connection *nc; | |
1576 | ||
e0a1988b JA |
1577 | printf("server: waiting for connections...\n"); |
1578 | ||
1579 | while (!is_done()) { | |
ff11d54c TZ |
1580 | if (net_connects >= max_connects) { |
1581 | pfds = realloc(pfds, (net_connects + 1) * sizeof(*pfds)); | |
1582 | ncs = realloc(ncs, (net_connects + 1) * sizeof(*ncs)); | |
1583 | max_connects = net_connects + 1; | |
1584 | } | |
e0a1988b JA |
1585 | /* |
1586 | * the zero entry is for incoming connections, remaining | |
1587 | * entries for clients | |
1588 | */ | |
1589 | pfds[0].fd = listen_fd; | |
1590 | pfds[0].events = POLLIN; | |
ff11d54c TZ |
1591 | nconns = 0; |
1592 | for_each_cl_host(ch) { | |
1593 | for (i = 0; i < ch->nconn; i++) { | |
1594 | nc = &ch->net_connections[i]; | |
1595 | pfds[nconns + 1].fd = nc->in_fd; | |
1596 | pfds[nconns + 1].events = POLLIN; | |
1597 | ncs[nconns++] = nc; | |
1598 | } | |
e0a1988b JA |
1599 | } |
1600 | ||
ff11d54c | 1601 | events = poll(pfds, 1 + nconns, -1); |
e0a1988b JA |
1602 | if (events < 0) { |
1603 | if (errno == EINTR) | |
1604 | continue; | |
1605 | ||
1606 | perror("poll"); | |
1607 | break; | |
1608 | } else if (!events) | |
1609 | continue; | |
1610 | ||
1611 | if (pfds[0].revents & POLLIN) { | |
1612 | net_add_connection(listen_fd, addr); | |
1613 | events--; | |
1614 | } | |
1615 | ||
ff11d54c | 1616 | for (i = 0; events && i < nconns; i++) { |
e0a1988b | 1617 | if (pfds[i + 1].revents & POLLIN) { |
ff11d54c | 1618 | net_client_data(ncs[i]); |
e0a1988b JA |
1619 | events--; |
1620 | } | |
1621 | } | |
1622 | } | |
659bcc3f JA |
1623 | } |
1624 | ||
8e86c98a JA |
1625 | /* |
1626 | * Start here when we are in server mode - just fetch data from the network | |
1627 | * and dump to files | |
1628 | */ | |
1629 | static int net_server(void) | |
1630 | { | |
1631 | struct sockaddr_in addr; | |
e0a1988b | 1632 | int fd, opt; |
8e86c98a JA |
1633 | |
1634 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
1635 | if (fd < 0) { | |
1636 | perror("server: socket"); | |
1637 | return 1; | |
1638 | } | |
1639 | ||
1640 | opt = 1; | |
1641 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { | |
1642 | perror("setsockopt"); | |
1643 | return 1; | |
1644 | } | |
1645 | ||
1646 | memset(&addr, 0, sizeof(addr)); | |
1647 | addr.sin_family = AF_INET; | |
1648 | addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
1649 | addr.sin_port = htons(net_port); | |
1650 | ||
1651 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
1652 | perror("bind"); | |
1653 | return 1; | |
1654 | } | |
1655 | ||
1656 | if (listen(fd, 1) < 0) { | |
1657 | perror("listen"); | |
1658 | return 1; | |
1659 | } | |
1660 | ||
e0a1988b JA |
1661 | net_server_handle_connections(fd, &addr); |
1662 | return 0; | |
8e86c98a JA |
1663 | } |
1664 | ||
1665 | /* | |
1666 | * Setup outgoing network connection where we will transmit data | |
1667 | */ | |
ff11d54c | 1668 | static int net_setup_client_cpu(int i, struct sockaddr_in *addr) |
8e86c98a | 1669 | { |
8e86c98a JA |
1670 | int fd; |
1671 | ||
1672 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
1673 | if (fd < 0) { | |
1674 | perror("client: socket"); | |
1675 | return 1; | |
1676 | } | |
1677 | ||
ff11d54c TZ |
1678 | if (connect(fd, (struct sockaddr *) addr, sizeof(*addr)) < 0) { |
1679 | perror("client: connect"); | |
1680 | return 1; | |
1681 | } | |
1682 | ||
1683 | net_out_fd[i] = fd; | |
1684 | return 0; | |
1685 | } | |
1686 | ||
1687 | static int net_setup_client(void) | |
1688 | { | |
1689 | struct sockaddr_in addr; | |
1690 | int i; | |
1691 | ||
8e86c98a JA |
1692 | memset(&addr, 0, sizeof(addr)); |
1693 | addr.sin_family = AF_INET; | |
1694 | addr.sin_port = htons(net_port); | |
1695 | ||
1696 | if (inet_aton(hostname, &addr.sin_addr) != 1) { | |
1697 | struct hostent *hent = gethostbyname(hostname); | |
1698 | if (!hent) { | |
1699 | perror("gethostbyname"); | |
1700 | return 1; | |
1701 | } | |
1702 | ||
1703 | memcpy(&addr.sin_addr, hent->h_addr, 4); | |
1704 | strcpy(hostname, hent->h_name); | |
1705 | } | |
1706 | ||
1707 | printf("blktrace: connecting to %s\n", hostname); | |
1708 | ||
ff11d54c TZ |
1709 | net_out_fd = malloc(ncpus * sizeof(*net_out_fd)); |
1710 | for (i = 0; i < ncpus; i++) { | |
1711 | if (net_setup_client_cpu(i, &addr)) | |
1712 | return 1; | |
8e86c98a JA |
1713 | } |
1714 | ||
1715 | printf("blktrace: connected!\n"); | |
ff11d54c | 1716 | |
8e86c98a JA |
1717 | return 0; |
1718 | } | |
1719 | ||
52724a0e | 1720 | static char usage_str[] = \ |
3d06efea | 1721 | "-d <dev> [ -r debugfs path ] [ -o <output> ] [-k ] [ -w time ]\n" \ |
52724a0e JA |
1722 | "[ -a action ] [ -A action mask ] [ -v ]\n\n" \ |
1723 | "\t-d Use specified device. May also be given last after options\n" \ | |
3d06efea | 1724 | "\t-r Path to mounted debugfs, defaults to /debug\n" \ |
52724a0e | 1725 | "\t-o File(s) to send output to\n" \ |
d1d7f15f | 1726 | "\t-D Directory to prepend to output file names\n" \ |
52724a0e JA |
1727 | "\t-k Kill a running trace\n" \ |
1728 | "\t-w Stop after defined time, in seconds\n" \ | |
1729 | "\t-a Only trace specified actions. See documentation\n" \ | |
1730 | "\t-A Give trace mask as a single value. See documentation\n" \ | |
129aa440 JA |
1731 | "\t-b Sub buffer size in KiB\n" \ |
1732 | "\t-n Number of sub buffers\n" \ | |
f531b94d JA |
1733 | "\t-l Run in network listen mode (blktrace server)\n" \ |
1734 | "\t-h Run in network client mode, connecting to the given host\n" \ | |
1735 | "\t-p Network port to use (default 8462)\n" \ | |
ff11d54c | 1736 | "\t-s Make the network client NOT use sendfile() to transfer data\n" \ |
f531b94d | 1737 | "\t-V Print program version info\n\n"; |
52724a0e | 1738 | |
ee1f4158 NS |
1739 | static void show_usage(char *program) |
1740 | { | |
52724a0e | 1741 | fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str); |
ee1f4158 | 1742 | } |
d0ca268b JA |
1743 | |
1744 | int main(int argc, char *argv[]) | |
1745 | { | |
3d06efea | 1746 | static char default_debugfs_path[] = "/debug"; |
e3e74029 | 1747 | struct statfs st; |
d39c04ca | 1748 | int i, c; |
ece238a6 | 1749 | int stop_watch = 0; |
d39c04ca AB |
1750 | int act_mask_tmp = 0; |
1751 | ||
1752 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) { | |
1753 | switch (c) { | |
1754 | case 'a': | |
1755 | i = find_mask_map(optarg); | |
1756 | if (i < 0) { | |
ab197ca7 | 1757 | fprintf(stderr,"Invalid action mask %s\n", |
d39c04ca | 1758 | optarg); |
7425d456 | 1759 | return 1; |
d39c04ca AB |
1760 | } |
1761 | act_mask_tmp |= i; | |
1762 | break; | |
1763 | ||
1764 | case 'A': | |
98f8386b AB |
1765 | if ((sscanf(optarg, "%x", &i) != 1) || |
1766 | !valid_act_opt(i)) { | |
d39c04ca | 1767 | fprintf(stderr, |
ab197ca7 | 1768 | "Invalid set action mask %s/0x%x\n", |
d39c04ca | 1769 | optarg, i); |
7425d456 | 1770 | return 1; |
d39c04ca AB |
1771 | } |
1772 | act_mask_tmp = i; | |
1773 | break; | |
d0ca268b | 1774 | |
d39c04ca | 1775 | case 'd': |
e7c9f3ff NS |
1776 | if (resize_devices(optarg) != 0) |
1777 | return 1; | |
d39c04ca AB |
1778 | break; |
1779 | ||
5270dddd | 1780 | case 'r': |
3d06efea | 1781 | debugfs_path = optarg; |
5270dddd JA |
1782 | break; |
1783 | ||
d5396421 | 1784 | case 'o': |
66efebf8 | 1785 | output_name = optarg; |
d5396421 | 1786 | break; |
bc39777c JA |
1787 | case 'k': |
1788 | kill_running_trace = 1; | |
1789 | break; | |
ece238a6 NS |
1790 | case 'w': |
1791 | stop_watch = atoi(optarg); | |
1792 | if (stop_watch <= 0) { | |
1793 | fprintf(stderr, | |
1794 | "Invalid stopwatch value (%d secs)\n", | |
1795 | stop_watch); | |
1796 | return 1; | |
1797 | } | |
1798 | break; | |
57ea8602 | 1799 | case 'V': |
52724a0e JA |
1800 | printf("%s version %s\n", argv[0], blktrace_version); |
1801 | return 0; | |
129aa440 | 1802 | case 'b': |
eb3c8108 | 1803 | buf_size = strtoul(optarg, NULL, 10); |
183a0855 | 1804 | if (buf_size <= 0 || buf_size > 16*1024) { |
129aa440 | 1805 | fprintf(stderr, |
eb3c8108 | 1806 | "Invalid buffer size (%lu)\n",buf_size); |
129aa440 JA |
1807 | return 1; |
1808 | } | |
1809 | buf_size <<= 10; | |
1810 | break; | |
1811 | case 'n': | |
eb3c8108 | 1812 | buf_nr = strtoul(optarg, NULL, 10); |
129aa440 JA |
1813 | if (buf_nr <= 0) { |
1814 | fprintf(stderr, | |
eb3c8108 | 1815 | "Invalid buffer nr (%lu)\n", buf_nr); |
129aa440 JA |
1816 | return 1; |
1817 | } | |
1818 | break; | |
d1d7f15f JA |
1819 | case 'D': |
1820 | output_dir = optarg; | |
1821 | break; | |
8e86c98a JA |
1822 | case 'h': |
1823 | net_mode = Net_client; | |
1824 | strcpy(hostname, optarg); | |
1825 | break; | |
1826 | case 'l': | |
1827 | net_mode = Net_server; | |
1828 | break; | |
1829 | case 'p': | |
1830 | net_port = atoi(optarg); | |
1831 | break; | |
32f18c48 | 1832 | case 's': |
79971f43 | 1833 | net_use_sendfile = 0; |
32f18c48 | 1834 | break; |
d39c04ca | 1835 | default: |
ee1f4158 | 1836 | show_usage(argv[0]); |
7425d456 | 1837 | return 1; |
d39c04ca AB |
1838 | } |
1839 | } | |
1840 | ||
8e86c98a JA |
1841 | setlocale(LC_NUMERIC, "en_US"); |
1842 | ||
1843 | page_size = getpagesize(); | |
1844 | ||
1845 | if (net_mode == Net_server) | |
1846 | return net_server(); | |
1847 | ||
22cd0c02 JA |
1848 | while (optind < argc) { |
1849 | if (resize_devices(argv[optind++]) != 0) | |
1850 | return 1; | |
1851 | } | |
1852 | ||
e7c9f3ff | 1853 | if (ndevs == 0) { |
ee1f4158 | 1854 | show_usage(argv[0]); |
7425d456 | 1855 | return 1; |
d39c04ca AB |
1856 | } |
1857 | ||
d5396421 | 1858 | if (act_mask_tmp != 0) |
d39c04ca | 1859 | act_mask = act_mask_tmp; |
d0ca268b | 1860 | |
3d06efea JA |
1861 | if (!debugfs_path) |
1862 | debugfs_path = default_debugfs_path; | |
1863 | ||
1864 | if (statfs(debugfs_path, &st) < 0) { | |
e3e74029 NS |
1865 | perror("statfs"); |
1866 | fprintf(stderr,"%s does not appear to be a valid path\n", | |
3d06efea | 1867 | debugfs_path); |
e3e74029 | 1868 | return 1; |
3d06efea JA |
1869 | } else if (st.f_type != (long) DEBUGFS_TYPE) { |
1870 | fprintf(stderr,"%s does not appear to be a debug filesystem\n", | |
1871 | debugfs_path); | |
7425d456 | 1872 | return 1; |
d0ca268b JA |
1873 | } |
1874 | ||
e7c9f3ff | 1875 | if (open_devices() != 0) |
7425d456 | 1876 | return 1; |
bc39777c JA |
1877 | |
1878 | if (kill_running_trace) { | |
e7c9f3ff | 1879 | stop_all_traces(); |
7425d456 | 1880 | return 0; |
bc39777c JA |
1881 | } |
1882 | ||
e7c9f3ff NS |
1883 | ncpus = sysconf(_SC_NPROCESSORS_ONLN); |
1884 | if (ncpus < 0) { | |
1885 | fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n"); | |
7425d456 | 1886 | return 1; |
d0ca268b JA |
1887 | } |
1888 | ||
d0ca268b JA |
1889 | signal(SIGINT, handle_sigint); |
1890 | signal(SIGHUP, handle_sigint); | |
1891 | signal(SIGTERM, handle_sigint); | |
ece238a6 | 1892 | signal(SIGALRM, handle_sigint); |
d0ca268b | 1893 | |
8e86c98a JA |
1894 | if (net_mode == Net_client && net_setup_client()) |
1895 | return 1; | |
1896 | ||
1897 | if (start_devices() != 0) | |
1898 | return 1; | |
1899 | ||
e7c9f3ff | 1900 | atexit(stop_all_tracing); |
830fd65c | 1901 | |
ece238a6 NS |
1902 | if (stop_watch) |
1903 | alarm(stop_watch); | |
1904 | ||
b7106311 | 1905 | wait_for_threads(); |
d0ca268b | 1906 | |
eb3c8108 JA |
1907 | if (!is_trace_stopped()) { |
1908 | trace_stopped = 1; | |
91816d54 JA |
1909 | stop_all_threads(); |
1910 | stop_all_traces(); | |
91816d54 | 1911 | } |
d0ca268b | 1912 | |
e0a1988b | 1913 | show_stats(device_information, ndevs, ncpus); |
eb3c8108 | 1914 | |
d0ca268b JA |
1915 | return 0; |
1916 | } | |
1917 |