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 | |
13d928f0 | 49 | static char blktrace_version[] = "0.99"; |
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 | ||
e3e74029 NS |
60 | #define RELAYFS_TYPE 0xF0B4A981 |
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 JA |
148 | { |
149 | .name = "sendfile", | |
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; | |
32f18c48 | 163 | off_t offset; |
9db17354 JA |
164 | }; |
165 | ||
21f55651 JA |
166 | #define FIFO_SIZE (1024) /* should be plenty big! */ |
167 | #define CL_SIZE (128) /* cache line, any bigger? */ | |
168 | ||
169 | struct tip_subbuf_fifo { | |
170 | int tail __attribute__((aligned(CL_SIZE))); | |
171 | int head __attribute__((aligned(CL_SIZE))); | |
172 | struct tip_subbuf *q[FIFO_SIZE]; | |
173 | }; | |
174 | ||
d0ca268b JA |
175 | struct thread_information { |
176 | int cpu; | |
177 | pthread_t thread; | |
b9d4294e JA |
178 | |
179 | int fd; | |
a3e4d330 | 180 | void *fd_buf; |
b9d4294e JA |
181 | char fn[MAXPATHLEN + 64]; |
182 | ||
11eedd9b JA |
183 | int pfd; |
184 | size_t *pfd_buf; | |
185 | ||
007c233c JA |
186 | FILE *ofile; |
187 | char *ofile_buffer; | |
32f18c48 | 188 | off_t ofile_offset; |
9db17354 | 189 | int ofile_stdout; |
8e86c98a | 190 | int ofile_mmap; |
007c233c | 191 | |
0cc7d25e JA |
192 | int (*get_subbuf)(struct thread_information *, unsigned int); |
193 | int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *); | |
194 | int (*read_data)(struct thread_information *, void *, unsigned int); | |
195 | ||
d0ca268b | 196 | unsigned long events_processed; |
b7106311 | 197 | unsigned long long data_read; |
e7c9f3ff | 198 | struct device_information *device; |
9db17354 JA |
199 | |
200 | int exited; | |
201 | ||
b7106311 JA |
202 | /* |
203 | * piped fifo buffers | |
204 | */ | |
21f55651 | 205 | struct tip_subbuf_fifo fifo; |
7de86b12 | 206 | struct tip_subbuf *leftover_ts; |
b7106311 JA |
207 | |
208 | /* | |
209 | * mmap controlled output files | |
210 | */ | |
211 | unsigned long long fs_size; | |
212 | unsigned long long fs_max_size; | |
213 | unsigned long fs_off; | |
214 | void *fs_buf; | |
215 | unsigned long fs_buf_len; | |
d0ca268b JA |
216 | }; |
217 | ||
e7c9f3ff NS |
218 | struct device_information { |
219 | int fd; | |
220 | char *path; | |
221 | char buts_name[32]; | |
99c1f5ab | 222 | volatile int trace_started; |
eb3c8108 | 223 | unsigned long drop_count; |
e7c9f3ff NS |
224 | struct thread_information *threads; |
225 | }; | |
d0ca268b | 226 | |
e7c9f3ff | 227 | static int ncpus; |
d0ca268b | 228 | static struct thread_information *thread_information; |
e7c9f3ff NS |
229 | static int ndevs; |
230 | static struct device_information *device_information; | |
231 | ||
232 | /* command line option globals */ | |
233 | static char *relay_path; | |
d5396421 | 234 | static char *output_name; |
d1d7f15f | 235 | static char *output_dir; |
5c86134e | 236 | static int act_mask = ~0U; |
bc39777c | 237 | static int kill_running_trace; |
eb3c8108 JA |
238 | static unsigned long buf_size = BUF_SIZE; |
239 | static unsigned long buf_nr = BUF_NR; | |
b7106311 | 240 | static unsigned int page_size; |
d39c04ca | 241 | |
e7c9f3ff NS |
242 | #define is_done() (*(volatile int *)(&done)) |
243 | static volatile int done; | |
244 | ||
eb3c8108 JA |
245 | #define is_trace_stopped() (*(volatile int *)(&trace_stopped)) |
246 | static volatile int trace_stopped; | |
247 | ||
248 | #define is_stat_shown() (*(volatile int *)(&stat_shown)) | |
249 | static volatile int stat_shown; | |
a3e4d330 | 250 | |
8e86c98a JA |
251 | int data_is_native = -1; |
252 | ||
72ca8801 NS |
253 | static void exit_trace(int status); |
254 | ||
99c1f5ab JA |
255 | #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started)) |
256 | #define dip_set_tracing(dip, v) ((dip)->trace_started = (v)) | |
257 | ||
258 | #define __for_each_dip(__d, __i, __e) \ | |
259 | for (__i = 0, __d = device_information; __i < __e; __i++, __d++) | |
260 | ||
261 | #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs) | |
9db17354 JA |
262 | #define for_each_tip(__d, __t, __j) \ |
263 | for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++) | |
99c1f5ab | 264 | |
8e86c98a JA |
265 | /* |
266 | * networking stuff follows. we include a magic number so we know whether | |
267 | * to endianness convert or not | |
268 | */ | |
269 | struct blktrace_net_hdr { | |
270 | u32 magic; /* same as trace magic */ | |
22cd0c02 | 271 | char buts_name[32]; /* trace name */ |
8e86c98a | 272 | u32 cpu; /* for which cpu */ |
22cd0c02 | 273 | u32 max_cpus; |
8e86c98a JA |
274 | u32 len; /* length of following trace data */ |
275 | }; | |
276 | ||
277 | #define TRACE_NET_PORT (8462) | |
278 | ||
279 | enum { | |
280 | Net_none = 0, | |
281 | Net_server, | |
282 | Net_client, | |
283 | }; | |
284 | ||
285 | /* | |
286 | * network cmd line params | |
287 | */ | |
288 | static char hostname[MAXHOSTNAMELEN]; | |
289 | static int net_port = TRACE_NET_PORT; | |
290 | static int net_mode = 0; | |
32f18c48 | 291 | static int net_sendfile; |
8e86c98a JA |
292 | |
293 | static int net_in_fd = -1; | |
294 | static int net_out_fd = -1; | |
295 | ||
296 | static void handle_sigint(__attribute__((__unused__)) int sig) | |
297 | { | |
298 | done = 1; | |
299 | } | |
300 | ||
eb3c8108 JA |
301 | static int get_dropped_count(const char *buts_name) |
302 | { | |
303 | int fd; | |
304 | char tmp[MAXPATHLEN + 64]; | |
305 | ||
306 | snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped", | |
307 | relay_path, buts_name); | |
308 | ||
309 | fd = open(tmp, O_RDONLY); | |
310 | if (fd < 0) { | |
311 | /* | |
312 | * this may be ok, if the kernel doesn't support dropped counts | |
313 | */ | |
314 | if (errno == ENOENT) | |
315 | return 0; | |
316 | ||
317 | fprintf(stderr, "Couldn't open dropped file %s\n", tmp); | |
318 | return -1; | |
319 | } | |
320 | ||
321 | if (read(fd, tmp, sizeof(tmp)) < 0) { | |
322 | perror(tmp); | |
323 | close(fd); | |
324 | return -1; | |
325 | } | |
326 | ||
327 | close(fd); | |
328 | ||
329 | return atoi(tmp); | |
330 | } | |
331 | ||
11eedd9b JA |
332 | static size_t get_subbuf_padding(struct thread_information *tip, |
333 | unsigned subbuf) | |
334 | { | |
335 | size_t padding_size = buf_nr * sizeof(size_t); | |
336 | size_t ret; | |
337 | ||
338 | if (read(tip->pfd, tip->pfd_buf, padding_size) < 0) { | |
339 | perror("tip pad read"); | |
340 | ret = -1; | |
341 | } else | |
342 | ret = tip->pfd_buf[subbuf]; | |
343 | ||
344 | return ret; | |
345 | } | |
346 | ||
e7c9f3ff | 347 | static int start_trace(struct device_information *dip) |
d0ca268b JA |
348 | { |
349 | struct blk_user_trace_setup buts; | |
350 | ||
1f79c4a0 | 351 | memset(&buts, 0, sizeof(buts)); |
129aa440 JA |
352 | buts.buf_size = buf_size; |
353 | buts.buf_nr = buf_nr; | |
d39c04ca | 354 | buts.act_mask = act_mask; |
d0ca268b | 355 | |
ed71a31e JA |
356 | if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) { |
357 | perror("BLKTRACESETUP"); | |
358 | return 1; | |
359 | } | |
360 | ||
361 | if (ioctl(dip->fd, BLKTRACESTART) < 0) { | |
362 | perror("BLKTRACESTART"); | |
d0ca268b JA |
363 | return 1; |
364 | } | |
365 | ||
e7c9f3ff | 366 | memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name)); |
99c1f5ab | 367 | dip_set_tracing(dip, 1); |
d0ca268b JA |
368 | return 0; |
369 | } | |
370 | ||
e7c9f3ff | 371 | static void stop_trace(struct device_information *dip) |
d0ca268b | 372 | { |
99c1f5ab JA |
373 | if (dip_tracing(dip) || kill_running_trace) { |
374 | dip_set_tracing(dip, 0); | |
cf9208ea | 375 | |
ed71a31e JA |
376 | if (ioctl(dip->fd, BLKTRACESTOP) < 0) |
377 | perror("BLKTRACESTOP"); | |
378 | if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0) | |
379 | perror("BLKTRACETEARDOWN"); | |
cf9208ea | 380 | |
e7c9f3ff | 381 | close(dip->fd); |
cf9208ea | 382 | dip->fd = -1; |
707b0914 | 383 | } |
d0ca268b JA |
384 | } |
385 | ||
e7c9f3ff NS |
386 | static void stop_all_traces(void) |
387 | { | |
388 | struct device_information *dip; | |
389 | int i; | |
390 | ||
eb3c8108 JA |
391 | for_each_dip(dip, i) { |
392 | dip->drop_count = get_dropped_count(dip->buts_name); | |
e7c9f3ff | 393 | stop_trace(dip); |
eb3c8108 | 394 | } |
e7c9f3ff NS |
395 | } |
396 | ||
eb3c8108 JA |
397 | static void wait_for_data(struct thread_information *tip) |
398 | { | |
399 | struct pollfd pfd = { .fd = tip->fd, .events = POLLIN }; | |
400 | ||
9db17354 | 401 | do { |
b4aabcb3 | 402 | poll(&pfd, 1, 100); |
9db17354 JA |
403 | if (pfd.revents & POLLIN) |
404 | break; | |
405 | if (tip->ofile_stdout) | |
406 | break; | |
407 | } while (!is_done()); | |
eb3c8108 JA |
408 | } |
409 | ||
0cc7d25e JA |
410 | static int read_data_file(struct thread_information *tip, void *buf, |
411 | unsigned int len) | |
d0ca268b | 412 | { |
ae9f71b3 | 413 | int ret = 0; |
bbabf03a | 414 | |
9db17354 JA |
415 | do { |
416 | wait_for_data(tip); | |
ae9f71b3 | 417 | |
9db17354 JA |
418 | ret = read(tip->fd, buf, len); |
419 | if (!ret) | |
420 | continue; | |
421 | else if (ret > 0) | |
422 | return ret; | |
423 | else { | |
bbabf03a | 424 | if (errno != EAGAIN) { |
a3e4d330 JA |
425 | perror(tip->fn); |
426 | fprintf(stderr,"Thread %d failed read of %s\n", | |
427 | tip->cpu, tip->fn); | |
428 | break; | |
429 | } | |
9db17354 | 430 | continue; |
bbabf03a | 431 | } |
9db17354 | 432 | } while (!is_done()); |
8a43bac5 | 433 | |
bbabf03a | 434 | return ret; |
8e86c98a | 435 | |
8a43bac5 JA |
436 | } |
437 | ||
0cc7d25e JA |
438 | static int read_data_net(struct thread_information *tip, void *buf, |
439 | unsigned int len) | |
8e86c98a JA |
440 | { |
441 | unsigned int bytes_left = len; | |
442 | int ret = 0; | |
443 | ||
444 | do { | |
445 | ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL); | |
446 | ||
447 | if (!ret) | |
448 | continue; | |
449 | else if (ret < 0) { | |
450 | if (errno != EAGAIN) { | |
451 | perror(tip->fn); | |
452 | fprintf(stderr, "server: failed read\n"); | |
453 | return 0; | |
454 | } | |
455 | continue; | |
456 | } else { | |
457 | buf += ret; | |
458 | bytes_left -= ret; | |
459 | } | |
460 | } while (!is_done() && bytes_left); | |
461 | ||
410d7c62 | 462 | return len - bytes_left; |
8e86c98a JA |
463 | } |
464 | ||
0cc7d25e JA |
465 | static int read_data(struct thread_information *tip, void *buf, |
466 | unsigned int len) | |
8e86c98a | 467 | { |
663962f7 | 468 | return tip->read_data(tip, buf, len); |
8e86c98a JA |
469 | } |
470 | ||
471 | static inline struct tip_subbuf * | |
472 | subbuf_fifo_dequeue(struct thread_information *tip) | |
a3e4d330 | 473 | { |
21f55651 JA |
474 | const int head = tip->fifo.head; |
475 | const int next = (head + 1) & (FIFO_SIZE - 1); | |
476 | ||
477 | if (head != tip->fifo.tail) { | |
478 | struct tip_subbuf *ts = tip->fifo.q[head]; | |
479 | ||
480 | store_barrier(); | |
481 | tip->fifo.head = next; | |
482 | return ts; | |
483 | } | |
484 | ||
485 | return NULL; | |
9db17354 | 486 | } |
eb3c8108 | 487 | |
21f55651 JA |
488 | static inline int subbuf_fifo_queue(struct thread_information *tip, |
489 | struct tip_subbuf *ts) | |
9db17354 | 490 | { |
21f55651 JA |
491 | const int tail = tip->fifo.tail; |
492 | const int next = (tail + 1) & (FIFO_SIZE - 1); | |
493 | ||
494 | if (next != tip->fifo.head) { | |
495 | tip->fifo.q[tail] = ts; | |
496 | store_barrier(); | |
497 | tip->fifo.tail = next; | |
498 | return 0; | |
499 | } | |
500 | ||
501 | fprintf(stderr, "fifo too small!\n"); | |
502 | return 1; | |
a3e4d330 JA |
503 | } |
504 | ||
b7106311 JA |
505 | /* |
506 | * For file output, truncate and mmap the file appropriately | |
507 | */ | |
8e86c98a | 508 | static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen) |
b7106311 JA |
509 | { |
510 | int ofd = fileno(tip->ofile); | |
511 | int ret; | |
512 | ||
513 | /* | |
514 | * extend file, if we have to. use chunks of 16 subbuffers. | |
515 | */ | |
516 | if (tip->fs_off + buf_size > tip->fs_buf_len) { | |
517 | if (tip->fs_buf) { | |
5975d309 | 518 | munlock(tip->fs_buf, tip->fs_buf_len); |
b7106311 JA |
519 | munmap(tip->fs_buf, tip->fs_buf_len); |
520 | tip->fs_buf = NULL; | |
521 | } | |
522 | ||
523 | tip->fs_off = tip->fs_size & (page_size - 1); | |
524 | tip->fs_buf_len = (16 * buf_size) - tip->fs_off; | |
525 | tip->fs_max_size += tip->fs_buf_len; | |
526 | ||
527 | if (ftruncate(ofd, tip->fs_max_size) < 0) { | |
528 | perror("ftruncate"); | |
529 | return -1; | |
530 | } | |
531 | ||
532 | tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE, | |
533 | MAP_SHARED, ofd, tip->fs_size - tip->fs_off); | |
534 | if (tip->fs_buf == MAP_FAILED) { | |
535 | perror("mmap"); | |
536 | return -1; | |
537 | } | |
5975d309 | 538 | mlock(tip->fs_buf, tip->fs_buf_len); |
b7106311 JA |
539 | } |
540 | ||
8e86c98a | 541 | ret = read_data(tip, tip->fs_buf + tip->fs_off, maxlen); |
b7106311 | 542 | if (ret >= 0) { |
dbfbd6db | 543 | tip->data_read += ret; |
b7106311 JA |
544 | tip->fs_size += ret; |
545 | tip->fs_off += ret; | |
546 | return 0; | |
547 | } | |
548 | ||
549 | return -1; | |
550 | } | |
551 | ||
18eed2a7 JA |
552 | /* |
553 | * Use the copy approach for pipes and network | |
554 | */ | |
555 | static int get_subbuf(struct thread_information *tip, unsigned int maxlen) | |
556 | { | |
557 | struct tip_subbuf *ts = malloc(sizeof(*ts)); | |
558 | int ret; | |
559 | ||
560 | ts->buf = malloc(buf_size); | |
561 | ts->max_len = maxlen; | |
562 | ||
563 | ret = read_data(tip, ts->buf, ts->max_len); | |
564 | if (ret > 0) { | |
565 | ts->len = ret; | |
dbfbd6db | 566 | tip->data_read += ret; |
18eed2a7 JA |
567 | return subbuf_fifo_queue(tip, ts); |
568 | } | |
569 | ||
570 | return ret; | |
571 | } | |
572 | ||
32f18c48 JA |
573 | static int get_subbuf_sendfile(struct thread_information *tip, |
574 | unsigned int maxlen) | |
575 | { | |
11eedd9b | 576 | struct tip_subbuf *ts; |
32f18c48 | 577 | struct stat sb; |
1be42f3d | 578 | unsigned int ready, this_size; |
11eedd9b | 579 | int err; |
32f18c48 | 580 | |
18eed2a7 JA |
581 | wait_for_data(tip); |
582 | ||
583 | /* | |
584 | * hack to get last data out, we can't use sendfile for that | |
585 | */ | |
586 | if (is_done()) | |
587 | return get_subbuf(tip, maxlen); | |
588 | ||
32f18c48 JA |
589 | if (fstat(tip->fd, &sb) < 0) { |
590 | perror("trace stat"); | |
591 | return 1; | |
592 | } | |
593 | ||
11eedd9b | 594 | ready = sb.st_size - tip->ofile_offset; |
663962f7 JA |
595 | if (!ready) { |
596 | /* | |
597 | * delay a little, since we poll() will return data available | |
598 | * until sendfile() is run | |
599 | */ | |
600 | usleep(100); | |
11eedd9b | 601 | return 0; |
663962f7 | 602 | } |
11eedd9b | 603 | |
1be42f3d | 604 | this_size = buf_size; |
11eedd9b | 605 | while (ready) { |
1be42f3d JA |
606 | if (this_size > ready) |
607 | this_size = ready; | |
608 | ||
11eedd9b JA |
609 | ts = malloc(sizeof(*ts)); |
610 | ||
611 | ts->max_len = maxlen; | |
612 | ts->buf = NULL; | |
613 | ||
1be42f3d | 614 | ts->len = this_size; |
11eedd9b JA |
615 | ts->max_len = ts->len; |
616 | ts->offset = tip->ofile_offset; | |
617 | tip->ofile_offset += ts->len; | |
618 | ||
619 | err = subbuf_fifo_queue(tip, ts); | |
620 | if (err) | |
621 | return err; | |
622 | ||
1be42f3d | 623 | ready -= this_size; |
11eedd9b JA |
624 | } |
625 | ||
626 | return 0; | |
32f18c48 JA |
627 | } |
628 | ||
9db17354 | 629 | static void close_thread(struct thread_information *tip) |
a3e4d330 | 630 | { |
9db17354 JA |
631 | if (tip->fd != -1) |
632 | close(tip->fd); | |
11eedd9b JA |
633 | if (tip->pfd != -1) |
634 | close(tip->pfd); | |
9db17354 JA |
635 | if (tip->ofile) |
636 | fclose(tip->ofile); | |
637 | if (tip->ofile_buffer) | |
638 | free(tip->ofile_buffer); | |
639 | if (tip->fd_buf) | |
640 | free(tip->fd_buf); | |
11eedd9b JA |
641 | if (tip->pfd_buf) |
642 | free(tip->pfd_buf); | |
1c99bc21 | 643 | |
9db17354 | 644 | tip->fd = -1; |
11eedd9b | 645 | tip->pfd = -1; |
9db17354 JA |
646 | tip->ofile = NULL; |
647 | tip->ofile_buffer = NULL; | |
648 | tip->fd_buf = NULL; | |
a3e4d330 JA |
649 | } |
650 | ||
8e86c98a JA |
651 | static void tip_ftrunc_final(struct thread_information *tip) |
652 | { | |
653 | /* | |
654 | * truncate to right size and cleanup mmap | |
655 | */ | |
656 | if (tip->ofile_mmap) { | |
657 | int ofd = fileno(tip->ofile); | |
658 | ||
659 | if (tip->fs_buf) | |
660 | munmap(tip->fs_buf, tip->fs_buf_len); | |
661 | ||
662 | ftruncate(ofd, tip->fs_size); | |
663 | } | |
664 | } | |
665 | ||
9db17354 | 666 | static void *thread_main(void *arg) |
a3e4d330 | 667 | { |
9db17354 JA |
668 | struct thread_information *tip = arg; |
669 | pid_t pid = getpid(); | |
670 | cpu_set_t cpu_mask; | |
a3e4d330 | 671 | |
9db17354 JA |
672 | CPU_ZERO(&cpu_mask); |
673 | CPU_SET((tip->cpu), &cpu_mask); | |
a3e4d330 | 674 | |
9db17354 JA |
675 | if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) { |
676 | perror("sched_setaffinity"); | |
677 | exit_trace(1); | |
678 | } | |
a3e4d330 | 679 | |
9db17354 JA |
680 | snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d", |
681 | relay_path, tip->device->buts_name, tip->cpu); | |
682 | tip->fd = open(tip->fn, O_RDONLY); | |
683 | if (tip->fd < 0) { | |
684 | perror(tip->fn); | |
685 | fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu, | |
686 | tip->fn); | |
687 | exit_trace(1); | |
a3e4d330 JA |
688 | } |
689 | ||
11eedd9b JA |
690 | if (net_mode == Net_client && net_sendfile) { |
691 | char tmp[MAXPATHLEN + 64]; | |
692 | ||
693 | snprintf(tmp, sizeof(tmp), "%s/block/%s/trace%d.padding", | |
694 | relay_path, tip->device->buts_name, tip->cpu); | |
695 | ||
696 | tip->pfd = open(tmp, O_RDONLY); | |
697 | if (tip->pfd < 0) { | |
698 | fprintf(stderr, "Couldn't open padding file %s\n", tmp); | |
699 | exit_trace(1); | |
700 | } | |
701 | ||
702 | tip->pfd_buf = malloc(buf_nr * sizeof(size_t)); | |
703 | } | |
704 | ||
b7106311 | 705 | while (!is_done()) { |
0cc7d25e JA |
706 | if (tip->get_subbuf(tip, buf_size)) |
707 | break; | |
b7106311 JA |
708 | } |
709 | ||
8e86c98a JA |
710 | tip_ftrunc_final(tip); |
711 | tip->exited = 1; | |
712 | return NULL; | |
713 | } | |
b7106311 | 714 | |
8e86c98a JA |
715 | static int write_data_net(int fd, void *buf, unsigned int buf_len) |
716 | { | |
717 | unsigned int bytes_left = buf_len; | |
718 | int ret; | |
b7106311 | 719 | |
8e86c98a JA |
720 | while (bytes_left) { |
721 | ret = send(fd, buf, bytes_left, 0); | |
722 | if (ret < 0) { | |
723 | perror("send"); | |
724 | return 1; | |
725 | } | |
726 | ||
727 | buf += ret; | |
728 | bytes_left -= ret; | |
9db17354 | 729 | } |
a3e4d330 | 730 | |
8e86c98a | 731 | return 0; |
a3e4d330 JA |
732 | } |
733 | ||
32f18c48 | 734 | static int net_send_header(struct thread_information *tip, unsigned int len) |
8e86c98a JA |
735 | { |
736 | struct blktrace_net_hdr hdr; | |
8e86c98a JA |
737 | |
738 | hdr.magic = BLK_IO_TRACE_MAGIC; | |
22cd0c02 | 739 | strcpy(hdr.buts_name, tip->device->buts_name); |
8e86c98a | 740 | hdr.cpu = tip->cpu; |
22cd0c02 | 741 | hdr.max_cpus = ncpus; |
32f18c48 | 742 | hdr.len = len; |
8e86c98a | 743 | |
32f18c48 JA |
744 | return write_data_net(net_out_fd, &hdr, sizeof(hdr)); |
745 | } | |
8e86c98a | 746 | |
6a752c90 JA |
747 | /* |
748 | * send header with 0 length to signal end-of-run | |
749 | */ | |
750 | static void net_client_send_close(void) | |
751 | { | |
752 | struct blktrace_net_hdr hdr; | |
753 | ||
754 | hdr.magic = BLK_IO_TRACE_MAGIC; | |
755 | hdr.cpu = 0; | |
756 | hdr.max_cpus = ncpus; | |
757 | hdr.len = 0; | |
758 | ||
759 | write_data_net(net_out_fd, &hdr, sizeof(hdr)); | |
760 | } | |
761 | ||
32f18c48 JA |
762 | static int flush_subbuf_net(struct thread_information *tip, |
763 | struct tip_subbuf *ts) | |
764 | { | |
765 | if (net_send_header(tip, ts->len)) | |
766 | return 1; | |
22cd0c02 JA |
767 | if (write_data_net(net_out_fd, ts->buf, ts->len)) |
768 | return 1; | |
8e86c98a | 769 | |
f0597a7e | 770 | free(ts->buf); |
8e86c98a JA |
771 | free(ts); |
772 | return 0; | |
773 | } | |
774 | ||
32f18c48 JA |
775 | static int flush_subbuf_sendfile(struct thread_information *tip, |
776 | struct tip_subbuf *ts) | |
777 | { | |
11eedd9b JA |
778 | size_t padding; |
779 | unsigned subbuf; | |
780 | unsigned len; | |
18eed2a7 JA |
781 | |
782 | /* | |
783 | * currently we cannot use sendfile() on the last bytes read, as they | |
784 | * may not be a full subbuffer. get_subbuf_sendfile() falls back to | |
785 | * the read approach for those, so use send() to ship them out | |
786 | */ | |
787 | if (ts->buf) | |
788 | return flush_subbuf_net(tip, ts); | |
11eedd9b JA |
789 | |
790 | subbuf = (ts->offset / buf_size) % buf_nr; | |
791 | padding = get_subbuf_padding(tip, subbuf); | |
792 | len = ts->len - padding; | |
793 | ||
794 | if (net_send_header(tip, len)) | |
32f18c48 | 795 | return 1; |
11eedd9b | 796 | if (sendfile(net_out_fd, tip->fd, &ts->offset, len) < 0) { |
32f18c48 JA |
797 | perror("sendfile"); |
798 | return 1; | |
799 | } | |
800 | ||
112a166e | 801 | tip->data_read += len; |
32f18c48 JA |
802 | free(ts); |
803 | return 0; | |
804 | } | |
805 | ||
8e86c98a JA |
806 | static int write_data(struct thread_information *tip, void *buf, |
807 | unsigned int buf_len) | |
8a43bac5 | 808 | { |
7126171a | 809 | int ret; |
8a43bac5 | 810 | |
6480258a JA |
811 | if (!buf_len) |
812 | return 0; | |
813 | ||
7126171a JA |
814 | while (1) { |
815 | ret = fwrite(buf, buf_len, 1, tip->ofile); | |
007c233c | 816 | if (ret == 1) |
8a43bac5 JA |
817 | break; |
818 | ||
db6fe5bc JA |
819 | if (ret < 0) { |
820 | perror("write"); | |
821 | return 1; | |
8a43bac5 | 822 | } |
d0ca268b JA |
823 | } |
824 | ||
9db17354 | 825 | if (tip->ofile_stdout) |
7126171a JA |
826 | fflush(tip->ofile); |
827 | ||
8a43bac5 JA |
828 | return 0; |
829 | } | |
830 | ||
8e86c98a JA |
831 | static int flush_subbuf_file(struct thread_information *tip, |
832 | struct tip_subbuf *ts) | |
8a43bac5 | 833 | { |
9db17354 JA |
834 | unsigned int offset = 0; |
835 | struct blk_io_trace *t; | |
836 | int pdu_len, events = 0; | |
8a43bac5 | 837 | |
9db17354 | 838 | /* |
7de86b12 | 839 | * surplus from last run |
9db17354 | 840 | */ |
7de86b12 AB |
841 | if (tip->leftover_ts) { |
842 | struct tip_subbuf *prev_ts = tip->leftover_ts; | |
843 | ||
9e8b753c | 844 | if (prev_ts->len + ts->len > prev_ts->max_len) { |
7de86b12 AB |
845 | prev_ts->max_len += ts->len; |
846 | prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len); | |
847 | } | |
848 | ||
9e8b753c | 849 | memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len); |
7de86b12 AB |
850 | prev_ts->len += ts->len; |
851 | ||
852 | free(ts->buf); | |
853 | free(ts); | |
854 | ||
855 | ts = prev_ts; | |
856 | tip->leftover_ts = NULL; | |
9db17354 | 857 | } |
d0ca268b | 858 | |
9db17354 JA |
859 | while (offset + sizeof(*t) <= ts->len) { |
860 | t = ts->buf + offset; | |
3a9d6c13 | 861 | |
9cfa6c2b AB |
862 | if (verify_trace(t)) { |
863 | write_data(tip, ts->buf, offset); | |
9db17354 | 864 | return -1; |
9cfa6c2b | 865 | } |
3a9d6c13 | 866 | |
9db17354 | 867 | pdu_len = t->pdu_len; |
3a9d6c13 | 868 | |
9db17354 | 869 | if (offset + sizeof(*t) + pdu_len > ts->len) |
3a9d6c13 | 870 | break; |
4b5db44a | 871 | |
9db17354 JA |
872 | offset += sizeof(*t) + pdu_len; |
873 | tip->events_processed++; | |
b7106311 | 874 | tip->data_read += sizeof(*t) + pdu_len; |
9db17354 | 875 | events++; |
3a9d6c13 JA |
876 | } |
877 | ||
9cfa6c2b AB |
878 | if (write_data(tip, ts->buf, offset)) |
879 | return -1; | |
880 | ||
3a9d6c13 | 881 | /* |
9db17354 | 882 | * leftover bytes, save them for next time |
3a9d6c13 | 883 | */ |
9db17354 | 884 | if (offset != ts->len) { |
7de86b12 | 885 | tip->leftover_ts = ts; |
9e8b753c JA |
886 | ts->len -= offset; |
887 | memmove(ts->buf, ts->buf + offset, ts->len); | |
7de86b12 AB |
888 | } else { |
889 | free(ts->buf); | |
890 | free(ts); | |
9db17354 | 891 | } |
4b5db44a | 892 | |
9db17354 | 893 | return events; |
4b5db44a JA |
894 | } |
895 | ||
9db17354 | 896 | static int write_tip_events(struct thread_information *tip) |
d5396421 | 897 | { |
21f55651 | 898 | struct tip_subbuf *ts = subbuf_fifo_dequeue(tip); |
d5396421 | 899 | |
0cc7d25e JA |
900 | if (ts) |
901 | return tip->flush_subbuf(tip, ts); | |
91816d54 | 902 | |
9db17354 | 903 | return 0; |
91816d54 JA |
904 | } |
905 | ||
9db17354 JA |
906 | /* |
907 | * scans the tips we know and writes out the subbuffers we accumulate | |
908 | */ | |
909 | static void get_and_write_events(void) | |
d0ca268b | 910 | { |
9db17354 JA |
911 | struct device_information *dip; |
912 | struct thread_information *tip; | |
27223f19 | 913 | int i, j, events, ret, tips_running; |
d0ca268b | 914 | |
9db17354 JA |
915 | while (!is_done()) { |
916 | events = 0; | |
d0ca268b | 917 | |
9db17354 JA |
918 | for_each_dip(dip, i) { |
919 | for_each_tip(dip, tip, j) { | |
920 | ret = write_tip_events(tip); | |
921 | if (ret > 0) | |
922 | events += ret; | |
923 | } | |
924 | } | |
d0ca268b | 925 | |
9db17354 JA |
926 | if (!events) |
927 | usleep(10); | |
d0ca268b JA |
928 | } |
929 | ||
a3e4d330 | 930 | /* |
9db17354 | 931 | * reap stored events |
a3e4d330 | 932 | */ |
9db17354 JA |
933 | do { |
934 | events = 0; | |
27223f19 | 935 | tips_running = 0; |
9db17354 JA |
936 | for_each_dip(dip, i) { |
937 | for_each_tip(dip, tip, j) { | |
938 | ret = write_tip_events(tip); | |
939 | if (ret > 0) | |
940 | events += ret; | |
27223f19 | 941 | tips_running += !tip->exited; |
9db17354 | 942 | } |
69e65a9e | 943 | } |
9db17354 | 944 | usleep(10); |
27223f19 | 945 | } while (events || tips_running); |
d0ca268b JA |
946 | } |
947 | ||
b7106311 JA |
948 | static void wait_for_threads(void) |
949 | { | |
950 | /* | |
8e86c98a JA |
951 | * for piped or network output, poll and fetch data for writeout. |
952 | * for files, we just wait around for trace threads to exit | |
b7106311 | 953 | */ |
8e86c98a JA |
954 | if ((output_name && !strcmp(output_name, "-")) || |
955 | net_mode == Net_client) | |
b7106311 JA |
956 | get_and_write_events(); |
957 | else { | |
958 | struct device_information *dip; | |
959 | struct thread_information *tip; | |
960 | int i, j, tips_running; | |
961 | ||
962 | do { | |
963 | tips_running = 0; | |
964 | usleep(1000); | |
965 | ||
966 | for_each_dip(dip, i) | |
967 | for_each_tip(dip, tip, j) | |
968 | tips_running += !tip->exited; | |
969 | } while (tips_running); | |
970 | } | |
6a752c90 JA |
971 | |
972 | if (net_mode == Net_client) | |
973 | net_client_send_close(); | |
b7106311 JA |
974 | } |
975 | ||
8e86c98a JA |
976 | static void fill_ofname(char *dst, char *buts_name, int cpu) |
977 | { | |
978 | int len = 0; | |
979 | ||
980 | if (output_dir) | |
981 | len = sprintf(dst, "%s/", output_dir); | |
982 | ||
983 | if (output_name) | |
984 | sprintf(dst + len, "%s.blktrace.%d", output_name, cpu); | |
985 | else | |
986 | sprintf(dst + len, "%s.blktrace.%d", buts_name, cpu); | |
987 | } | |
988 | ||
0cc7d25e JA |
989 | static void fill_ops(struct thread_information *tip) |
990 | { | |
991 | /* | |
992 | * setup ops | |
993 | */ | |
32f18c48 JA |
994 | if (net_mode == Net_client) { |
995 | if (net_sendfile) { | |
996 | tip->get_subbuf = get_subbuf_sendfile; | |
997 | tip->flush_subbuf = flush_subbuf_sendfile; | |
998 | } else { | |
999 | tip->get_subbuf = get_subbuf; | |
1000 | tip->flush_subbuf = flush_subbuf_net; | |
1001 | } | |
1002 | } else { | |
1003 | if (tip->ofile_mmap) | |
1004 | tip->get_subbuf = mmap_subbuf; | |
1005 | else | |
1006 | tip->get_subbuf = get_subbuf; | |
0cc7d25e | 1007 | |
0cc7d25e | 1008 | tip->flush_subbuf = flush_subbuf_file; |
32f18c48 JA |
1009 | } |
1010 | ||
0cc7d25e JA |
1011 | if (net_mode == Net_server) |
1012 | tip->read_data = read_data_net; | |
1013 | else | |
1014 | tip->read_data = read_data_file; | |
1015 | } | |
1016 | ||
ddf22842 JA |
1017 | static int tip_open_output(struct device_information *dip, |
1018 | struct thread_information *tip) | |
d0ca268b | 1019 | { |
ddf22842 | 1020 | int pipeline = output_name && !strcmp(output_name, "-"); |
8e86c98a | 1021 | int mode, vbuf_size; |
22cd0c02 | 1022 | char op[64]; |
d0ca268b | 1023 | |
ddf22842 JA |
1024 | if (net_mode == Net_client) { |
1025 | tip->ofile = NULL; | |
1026 | tip->ofile_stdout = 0; | |
1027 | tip->ofile_mmap = 0; | |
1028 | vbuf_size = 0; | |
1be42f3d | 1029 | mode = 0; /* gcc 4.x issues a bogus warning */ |
ddf22842 JA |
1030 | } else if (pipeline) { |
1031 | tip->ofile = fdopen(STDOUT_FILENO, "w"); | |
1032 | tip->ofile_stdout = 1; | |
1033 | tip->ofile_mmap = 0; | |
1034 | mode = _IOLBF; | |
1035 | vbuf_size = 512; | |
1036 | } else { | |
1037 | fill_ofname(op, dip->buts_name, tip->cpu); | |
1038 | tip->ofile = fopen(op, "w+"); | |
1039 | tip->ofile_stdout = 0; | |
1040 | tip->ofile_mmap = 1; | |
1041 | mode = _IOFBF; | |
1042 | vbuf_size = OFILE_BUF; | |
1043 | } | |
d5396421 | 1044 | |
ddf22842 JA |
1045 | if (net_mode != Net_client && tip->ofile == NULL) { |
1046 | perror(op); | |
1047 | return 1; | |
1048 | } | |
d5396421 | 1049 | |
ddf22842 | 1050 | if (vbuf_size) { |
57e8a2ad TZ |
1051 | tip->ofile_buffer = malloc(vbuf_size); |
1052 | if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) { | |
007c233c JA |
1053 | perror("setvbuf"); |
1054 | close_thread(tip); | |
1055 | return 1; | |
1056 | } | |
ddf22842 JA |
1057 | } |
1058 | ||
1059 | fill_ops(tip); | |
1060 | return 0; | |
1061 | } | |
007c233c | 1062 | |
ddf22842 JA |
1063 | static int start_threads(struct device_information *dip) |
1064 | { | |
1065 | struct thread_information *tip; | |
1066 | int j; | |
1067 | ||
1068 | for_each_tip(dip, tip, j) { | |
1069 | tip->cpu = j; | |
1070 | tip->device = dip; | |
1071 | tip->events_processed = 0; | |
11eedd9b JA |
1072 | tip->fd = -1; |
1073 | tip->pfd = -1; | |
ddf22842 JA |
1074 | memset(&tip->fifo, 0, sizeof(tip->fifo)); |
1075 | tip->leftover_ts = NULL; | |
1076 | ||
1077 | if (tip_open_output(dip, tip)) | |
1078 | return 1; | |
0cc7d25e | 1079 | |
9db17354 | 1080 | if (pthread_create(&tip->thread, NULL, thread_main, tip)) { |
e7c9f3ff | 1081 | perror("pthread_create"); |
007c233c | 1082 | close_thread(tip); |
e7c9f3ff | 1083 | return 1; |
d0ca268b JA |
1084 | } |
1085 | } | |
1086 | ||
e7c9f3ff | 1087 | return 0; |
d0ca268b JA |
1088 | } |
1089 | ||
e7c9f3ff | 1090 | static void stop_threads(struct device_information *dip) |
3aabcd89 | 1091 | { |
e7c9f3ff | 1092 | struct thread_information *tip; |
91816d54 | 1093 | unsigned long ret; |
007c233c JA |
1094 | int i; |
1095 | ||
9db17354 | 1096 | for_each_tip(dip, tip, i) { |
91816d54 | 1097 | (void) pthread_join(tip->thread, (void *) &ret); |
9db17354 JA |
1098 | close_thread(tip); |
1099 | } | |
3aabcd89 JA |
1100 | } |
1101 | ||
e7c9f3ff | 1102 | static void stop_all_threads(void) |
72ca8801 | 1103 | { |
e7c9f3ff | 1104 | struct device_information *dip; |
72ca8801 NS |
1105 | int i; |
1106 | ||
99c1f5ab | 1107 | for_each_dip(dip, i) |
e7c9f3ff NS |
1108 | stop_threads(dip); |
1109 | } | |
1110 | ||
1111 | static void stop_all_tracing(void) | |
1112 | { | |
1113 | struct device_information *dip; | |
91816d54 | 1114 | int i; |
007c233c | 1115 | |
91816d54 | 1116 | for_each_dip(dip, i) |
e7c9f3ff | 1117 | stop_trace(dip); |
72ca8801 NS |
1118 | } |
1119 | ||
1120 | static void exit_trace(int status) | |
1121 | { | |
eb3c8108 JA |
1122 | if (!is_trace_stopped()) { |
1123 | trace_stopped = 1; | |
1124 | stop_all_threads(); | |
1125 | stop_all_tracing(); | |
1126 | } | |
1127 | ||
72ca8801 NS |
1128 | exit(status); |
1129 | } | |
1130 | ||
e7c9f3ff NS |
1131 | static int resize_devices(char *path) |
1132 | { | |
1133 | int size = (ndevs + 1) * sizeof(struct device_information); | |
1134 | ||
1135 | device_information = realloc(device_information, size); | |
1136 | if (!device_information) { | |
1137 | fprintf(stderr, "Out of memory, device %s (%d)\n", path, size); | |
1138 | return 1; | |
1139 | } | |
1140 | device_information[ndevs].path = path; | |
1141 | ndevs++; | |
1142 | return 0; | |
1143 | } | |
1144 | ||
1145 | static int open_devices(void) | |
d0ca268b | 1146 | { |
e7c9f3ff | 1147 | struct device_information *dip; |
d0ca268b | 1148 | int i; |
d0ca268b | 1149 | |
99c1f5ab | 1150 | for_each_dip(dip, i) { |
cf9208ea | 1151 | dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK); |
e7c9f3ff NS |
1152 | if (dip->fd < 0) { |
1153 | perror(dip->path); | |
1154 | return 1; | |
1155 | } | |
1156 | } | |
99c1f5ab | 1157 | |
e7c9f3ff NS |
1158 | return 0; |
1159 | } | |
1160 | ||
1161 | static int start_devices(void) | |
1162 | { | |
1163 | struct device_information *dip; | |
1164 | int i, j, size; | |
1165 | ||
1166 | size = ncpus * sizeof(struct thread_information); | |
1167 | thread_information = malloc(size * ndevs); | |
1168 | if (!thread_information) { | |
1169 | fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs); | |
1170 | return 1; | |
1171 | } | |
d5396421 | 1172 | |
99c1f5ab | 1173 | for_each_dip(dip, i) { |
e7c9f3ff NS |
1174 | if (start_trace(dip)) { |
1175 | close(dip->fd); | |
1176 | fprintf(stderr, "Failed to start trace on %s\n", | |
1177 | dip->path); | |
1178 | break; | |
1179 | } | |
1180 | } | |
99c1f5ab | 1181 | |
e7c9f3ff | 1182 | if (i != ndevs) { |
99c1f5ab | 1183 | __for_each_dip(dip, j, i) |
e7c9f3ff | 1184 | stop_trace(dip); |
99c1f5ab | 1185 | |
e7c9f3ff NS |
1186 | return 1; |
1187 | } | |
1188 | ||
99c1f5ab | 1189 | for_each_dip(dip, i) { |
e7c9f3ff NS |
1190 | dip->threads = thread_information + (i * ncpus); |
1191 | if (start_threads(dip)) { | |
1192 | fprintf(stderr, "Failed to start worker threads\n"); | |
1193 | break; | |
1194 | } | |
1195 | } | |
99c1f5ab | 1196 | |
e7c9f3ff | 1197 | if (i != ndevs) { |
99c1f5ab | 1198 | __for_each_dip(dip, j, i) |
e7c9f3ff | 1199 | stop_threads(dip); |
99c1f5ab | 1200 | for_each_dip(dip, i) |
e7c9f3ff | 1201 | stop_trace(dip); |
99c1f5ab | 1202 | |
e7c9f3ff | 1203 | return 1; |
d0ca268b JA |
1204 | } |
1205 | ||
e7c9f3ff | 1206 | return 0; |
d0ca268b JA |
1207 | } |
1208 | ||
e7c9f3ff NS |
1209 | static void show_stats(void) |
1210 | { | |
e7c9f3ff NS |
1211 | struct device_information *dip; |
1212 | struct thread_information *tip; | |
b7106311 | 1213 | unsigned long long events_processed, data_read; |
eb3c8108 | 1214 | unsigned long total_drops; |
2f903295 | 1215 | int i, j, no_stdout = 0; |
eb3c8108 JA |
1216 | |
1217 | if (is_stat_shown()) | |
1218 | return; | |
1219 | ||
2f903295 JA |
1220 | if (output_name && !strcmp(output_name, "-")) |
1221 | no_stdout = 1; | |
1222 | ||
eb3c8108 | 1223 | stat_shown = 1; |
428683db | 1224 | |
56070ea4 | 1225 | total_drops = 0; |
99c1f5ab | 1226 | for_each_dip(dip, i) { |
2f903295 | 1227 | if (!no_stdout) |
56070ea4 | 1228 | printf("Device: %s\n", dip->path); |
e7c9f3ff | 1229 | events_processed = 0; |
b7106311 | 1230 | data_read = 0; |
99c1f5ab | 1231 | for_each_tip(dip, tip, j) { |
2f903295 | 1232 | if (!no_stdout) |
b7106311 JA |
1233 | printf(" CPU%3d: %20lu events, %8llu KiB data\n", |
1234 | tip->cpu, tip->events_processed, | |
54824c20 | 1235 | (tip->data_read + 1023) >> 10); |
e7c9f3ff | 1236 | events_processed += tip->events_processed; |
b7106311 | 1237 | data_read += tip->data_read; |
e7c9f3ff | 1238 | } |
eb3c8108 | 1239 | total_drops += dip->drop_count; |
2f903295 | 1240 | if (!no_stdout) |
b7106311 JA |
1241 | printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n", |
1242 | events_processed, dip->drop_count, | |
18d8437d | 1243 | (data_read + 1023) >> 10); |
e7c9f3ff | 1244 | } |
56070ea4 JA |
1245 | |
1246 | if (total_drops) | |
1247 | fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n"); | |
e7c9f3ff | 1248 | } |
52724a0e | 1249 | |
22cd0c02 | 1250 | static struct device_information *net_get_dip(char *buts_name) |
8e86c98a | 1251 | { |
22cd0c02 | 1252 | struct device_information *dip; |
8e86c98a JA |
1253 | int i; |
1254 | ||
22cd0c02 JA |
1255 | for (i = 0; i < ndevs; i++) { |
1256 | dip = &device_information[i]; | |
8e86c98a | 1257 | |
22cd0c02 JA |
1258 | if (!strcmp(dip->buts_name, buts_name)) |
1259 | return dip; | |
8e86c98a JA |
1260 | } |
1261 | ||
22cd0c02 JA |
1262 | device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip)); |
1263 | dip = &device_information[ndevs]; | |
1264 | strcpy(dip->buts_name, buts_name); | |
410d7c62 | 1265 | strcpy(dip->path, buts_name); |
22cd0c02 JA |
1266 | ndevs++; |
1267 | dip->threads = malloc(ncpus * sizeof(struct thread_information)); | |
1268 | memset(dip->threads, 0, ncpus * sizeof(struct thread_information)); | |
1269 | ||
1270 | /* | |
1271 | * open all files | |
1272 | */ | |
1273 | for (i = 0; i < ncpus; i++) { | |
1274 | struct thread_information *tip = &dip->threads[i]; | |
8e86c98a | 1275 | |
22cd0c02 | 1276 | tip->cpu = i; |
22cd0c02 | 1277 | tip->device = dip; |
8e86c98a | 1278 | |
ddf22842 | 1279 | if (tip_open_output(dip, tip)) |
22cd0c02 | 1280 | return NULL; |
8e86c98a JA |
1281 | } |
1282 | ||
22cd0c02 JA |
1283 | return dip; |
1284 | } | |
1285 | ||
1286 | static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh) | |
1287 | { | |
1288 | struct device_information *dip; | |
1289 | ||
1290 | ncpus = bnh->max_cpus; | |
1291 | dip = net_get_dip(bnh->buts_name); | |
1292 | return &dip->threads[bnh->cpu]; | |
8e86c98a JA |
1293 | } |
1294 | ||
1295 | static int net_get_header(struct blktrace_net_hdr *bnh) | |
1296 | { | |
1297 | int fl = fcntl(net_in_fd, F_GETFL); | |
1298 | int bytes_left, ret; | |
1299 | void *p = bnh; | |
1300 | ||
1301 | fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK); | |
1302 | bytes_left = sizeof(*bnh); | |
1303 | while (bytes_left && !is_done()) { | |
1304 | ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL); | |
1305 | if (ret < 0) { | |
1306 | if (errno != EAGAIN) { | |
1307 | perror("recv header"); | |
1308 | return 1; | |
1309 | } | |
1310 | usleep(100); | |
1311 | continue; | |
1312 | } else if (!ret) { | |
1313 | usleep(100); | |
1314 | continue; | |
1315 | } else { | |
1316 | p += ret; | |
1317 | bytes_left -= ret; | |
1318 | } | |
1319 | } | |
1320 | fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK); | |
1321 | return 0; | |
1322 | } | |
1323 | ||
1324 | static int net_server_loop(void) | |
1325 | { | |
1326 | struct thread_information *tip; | |
1327 | struct blktrace_net_hdr bnh; | |
1328 | ||
1329 | if (net_get_header(&bnh)) | |
1330 | return 1; | |
1331 | ||
1332 | if (data_is_native == -1 && check_data_endianness(bnh.magic)) { | |
1333 | fprintf(stderr, "server: received data is bad\n"); | |
1334 | return 1; | |
1335 | } | |
1336 | ||
1337 | if (!data_is_native) { | |
1338 | bnh.cpu = be32_to_cpu(bnh.cpu); | |
1339 | bnh.len = be32_to_cpu(bnh.len); | |
1340 | } | |
1341 | ||
6a752c90 JA |
1342 | /* |
1343 | * len == 0 means that the other end signalled end-of-run | |
1344 | */ | |
1345 | if (!bnh.len) { | |
1346 | fprintf(stderr, "server: end of run\n"); | |
1347 | return 1; | |
1348 | } | |
1349 | ||
8e86c98a JA |
1350 | tip = net_get_tip(&bnh); |
1351 | if (!tip) | |
1352 | return 1; | |
1353 | ||
1354 | if (mmap_subbuf(tip, bnh.len)) | |
1355 | return 1; | |
1356 | ||
1357 | return 0; | |
1358 | } | |
1359 | ||
1360 | /* | |
1361 | * Start here when we are in server mode - just fetch data from the network | |
1362 | * and dump to files | |
1363 | */ | |
1364 | static int net_server(void) | |
1365 | { | |
898bbd3b JA |
1366 | struct device_information *dip; |
1367 | struct thread_information *tip; | |
8e86c98a JA |
1368 | struct sockaddr_in addr; |
1369 | socklen_t socklen; | |
22cd0c02 | 1370 | int fd, opt, i, j; |
8e86c98a JA |
1371 | |
1372 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
1373 | if (fd < 0) { | |
1374 | perror("server: socket"); | |
1375 | return 1; | |
1376 | } | |
1377 | ||
1378 | opt = 1; | |
1379 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { | |
1380 | perror("setsockopt"); | |
1381 | return 1; | |
1382 | } | |
1383 | ||
1384 | memset(&addr, 0, sizeof(addr)); | |
1385 | addr.sin_family = AF_INET; | |
1386 | addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
1387 | addr.sin_port = htons(net_port); | |
1388 | ||
1389 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
1390 | perror("bind"); | |
1391 | return 1; | |
1392 | } | |
1393 | ||
1394 | if (listen(fd, 1) < 0) { | |
1395 | perror("listen"); | |
1396 | return 1; | |
1397 | } | |
1398 | ||
6a752c90 | 1399 | repeat: |
bbb1c18a JA |
1400 | signal(SIGINT, NULL); |
1401 | signal(SIGHUP, NULL); | |
1402 | signal(SIGTERM, NULL); | |
1403 | signal(SIGALRM, NULL); | |
1404 | ||
8e86c98a JA |
1405 | printf("blktrace: waiting for incoming connection...\n"); |
1406 | ||
1407 | socklen = sizeof(addr); | |
1408 | net_in_fd = accept(fd, (struct sockaddr *) &addr, &socklen); | |
1409 | if (net_in_fd < 0) { | |
1410 | perror("accept"); | |
1411 | return 1; | |
1412 | } | |
1413 | ||
1414 | signal(SIGINT, handle_sigint); | |
1415 | signal(SIGHUP, handle_sigint); | |
1416 | signal(SIGTERM, handle_sigint); | |
1417 | signal(SIGALRM, handle_sigint); | |
1418 | ||
e85ee0f2 | 1419 | printf("blktrace: connection from %s\n", inet_ntoa(addr.sin_addr)); |
8e86c98a JA |
1420 | |
1421 | while (!is_done()) { | |
1422 | if (net_server_loop()) | |
1423 | break; | |
1424 | } | |
1425 | ||
898bbd3b JA |
1426 | for_each_dip(dip, i) |
1427 | for_each_tip(dip, tip, j) | |
1428 | tip_ftrunc_final(tip); | |
8e86c98a | 1429 | |
410d7c62 | 1430 | show_stats(); |
6a752c90 JA |
1431 | |
1432 | if (is_done()) | |
1433 | return 0; | |
1434 | ||
898bbd3b JA |
1435 | /* |
1436 | * cleanup for next run | |
1437 | */ | |
1438 | for_each_dip(dip, i) { | |
1439 | for_each_tip(dip, tip, j) | |
1440 | fclose(tip->ofile); | |
1441 | ||
1442 | free(dip->threads); | |
1443 | } | |
1444 | ||
1445 | free(device_information); | |
1446 | device_information = NULL; | |
1447 | ncpus = ndevs = 0; | |
b46a0342 JA |
1448 | |
1449 | close(net_in_fd); | |
1450 | net_in_fd = -1; | |
dbfbd6db | 1451 | stat_shown = 0; |
6a752c90 | 1452 | goto repeat; |
8e86c98a JA |
1453 | } |
1454 | ||
1455 | /* | |
1456 | * Setup outgoing network connection where we will transmit data | |
1457 | */ | |
1458 | static int net_setup_client(void) | |
1459 | { | |
1460 | struct sockaddr_in addr; | |
1461 | int fd; | |
1462 | ||
1463 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
1464 | if (fd < 0) { | |
1465 | perror("client: socket"); | |
1466 | return 1; | |
1467 | } | |
1468 | ||
1469 | memset(&addr, 0, sizeof(addr)); | |
1470 | addr.sin_family = AF_INET; | |
1471 | addr.sin_port = htons(net_port); | |
1472 | ||
1473 | if (inet_aton(hostname, &addr.sin_addr) != 1) { | |
1474 | struct hostent *hent = gethostbyname(hostname); | |
1475 | if (!hent) { | |
1476 | perror("gethostbyname"); | |
1477 | return 1; | |
1478 | } | |
1479 | ||
1480 | memcpy(&addr.sin_addr, hent->h_addr, 4); | |
1481 | strcpy(hostname, hent->h_name); | |
1482 | } | |
1483 | ||
1484 | printf("blktrace: connecting to %s\n", hostname); | |
1485 | ||
1486 | if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
1487 | perror("client: connect"); | |
1488 | return 1; | |
1489 | } | |
1490 | ||
1491 | printf("blktrace: connected!\n"); | |
1492 | net_out_fd = fd; | |
1493 | return 0; | |
1494 | } | |
1495 | ||
52724a0e JA |
1496 | static char usage_str[] = \ |
1497 | "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \ | |
1498 | "[ -a action ] [ -A action mask ] [ -v ]\n\n" \ | |
1499 | "\t-d Use specified device. May also be given last after options\n" \ | |
1500 | "\t-r Path to mounted relayfs, defaults to /relay\n" \ | |
1501 | "\t-o File(s) to send output to\n" \ | |
d1d7f15f | 1502 | "\t-D Directory to prepend to output file names\n" \ |
52724a0e JA |
1503 | "\t-k Kill a running trace\n" \ |
1504 | "\t-w Stop after defined time, in seconds\n" \ | |
1505 | "\t-a Only trace specified actions. See documentation\n" \ | |
1506 | "\t-A Give trace mask as a single value. See documentation\n" \ | |
129aa440 JA |
1507 | "\t-b Sub buffer size in KiB\n" \ |
1508 | "\t-n Number of sub buffers\n" \ | |
f531b94d JA |
1509 | "\t-l Run in network listen mode (blktrace server)\n" \ |
1510 | "\t-h Run in network client mode, connecting to the given host\n" \ | |
1511 | "\t-p Network port to use (default 8462)\n" \ | |
1512 | "\t-s Make the network client use sendfile() to transfer data\n" \ | |
1513 | "\t-V Print program version info\n\n"; | |
52724a0e | 1514 | |
ee1f4158 NS |
1515 | static void show_usage(char *program) |
1516 | { | |
52724a0e | 1517 | fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str); |
ee1f4158 | 1518 | } |
d0ca268b JA |
1519 | |
1520 | int main(int argc, char *argv[]) | |
1521 | { | |
5270dddd | 1522 | static char default_relay_path[] = "/relay"; |
e3e74029 | 1523 | struct statfs st; |
d39c04ca | 1524 | int i, c; |
ece238a6 | 1525 | int stop_watch = 0; |
d39c04ca AB |
1526 | int act_mask_tmp = 0; |
1527 | ||
1528 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) { | |
1529 | switch (c) { | |
1530 | case 'a': | |
1531 | i = find_mask_map(optarg); | |
1532 | if (i < 0) { | |
ab197ca7 | 1533 | fprintf(stderr,"Invalid action mask %s\n", |
d39c04ca | 1534 | optarg); |
7425d456 | 1535 | return 1; |
d39c04ca AB |
1536 | } |
1537 | act_mask_tmp |= i; | |
1538 | break; | |
1539 | ||
1540 | case 'A': | |
98f8386b AB |
1541 | if ((sscanf(optarg, "%x", &i) != 1) || |
1542 | !valid_act_opt(i)) { | |
d39c04ca | 1543 | fprintf(stderr, |
ab197ca7 | 1544 | "Invalid set action mask %s/0x%x\n", |
d39c04ca | 1545 | optarg, i); |
7425d456 | 1546 | return 1; |
d39c04ca AB |
1547 | } |
1548 | act_mask_tmp = i; | |
1549 | break; | |
d0ca268b | 1550 | |
d39c04ca | 1551 | case 'd': |
e7c9f3ff NS |
1552 | if (resize_devices(optarg) != 0) |
1553 | return 1; | |
d39c04ca AB |
1554 | break; |
1555 | ||
5270dddd JA |
1556 | case 'r': |
1557 | relay_path = optarg; | |
1558 | break; | |
1559 | ||
d5396421 | 1560 | case 'o': |
66efebf8 | 1561 | output_name = optarg; |
d5396421 | 1562 | break; |
bc39777c JA |
1563 | case 'k': |
1564 | kill_running_trace = 1; | |
1565 | break; | |
ece238a6 NS |
1566 | case 'w': |
1567 | stop_watch = atoi(optarg); | |
1568 | if (stop_watch <= 0) { | |
1569 | fprintf(stderr, | |
1570 | "Invalid stopwatch value (%d secs)\n", | |
1571 | stop_watch); | |
1572 | return 1; | |
1573 | } | |
1574 | break; | |
57ea8602 | 1575 | case 'V': |
52724a0e JA |
1576 | printf("%s version %s\n", argv[0], blktrace_version); |
1577 | return 0; | |
129aa440 | 1578 | case 'b': |
eb3c8108 | 1579 | buf_size = strtoul(optarg, NULL, 10); |
183a0855 | 1580 | if (buf_size <= 0 || buf_size > 16*1024) { |
129aa440 | 1581 | fprintf(stderr, |
eb3c8108 | 1582 | "Invalid buffer size (%lu)\n",buf_size); |
129aa440 JA |
1583 | return 1; |
1584 | } | |
1585 | buf_size <<= 10; | |
1586 | break; | |
1587 | case 'n': | |
eb3c8108 | 1588 | buf_nr = strtoul(optarg, NULL, 10); |
129aa440 JA |
1589 | if (buf_nr <= 0) { |
1590 | fprintf(stderr, | |
eb3c8108 | 1591 | "Invalid buffer nr (%lu)\n", buf_nr); |
129aa440 JA |
1592 | return 1; |
1593 | } | |
1594 | break; | |
d1d7f15f JA |
1595 | case 'D': |
1596 | output_dir = optarg; | |
1597 | break; | |
8e86c98a JA |
1598 | case 'h': |
1599 | net_mode = Net_client; | |
1600 | strcpy(hostname, optarg); | |
1601 | break; | |
1602 | case 'l': | |
1603 | net_mode = Net_server; | |
1604 | break; | |
1605 | case 'p': | |
1606 | net_port = atoi(optarg); | |
1607 | break; | |
32f18c48 JA |
1608 | case 's': |
1609 | net_sendfile = 1; | |
1610 | break; | |
d39c04ca | 1611 | default: |
ee1f4158 | 1612 | show_usage(argv[0]); |
7425d456 | 1613 | return 1; |
d39c04ca AB |
1614 | } |
1615 | } | |
1616 | ||
8e86c98a JA |
1617 | setlocale(LC_NUMERIC, "en_US"); |
1618 | ||
1619 | page_size = getpagesize(); | |
1620 | ||
1621 | if (net_mode == Net_server) | |
1622 | return net_server(); | |
1623 | ||
22cd0c02 JA |
1624 | while (optind < argc) { |
1625 | if (resize_devices(argv[optind++]) != 0) | |
1626 | return 1; | |
1627 | } | |
1628 | ||
e7c9f3ff | 1629 | if (ndevs == 0) { |
ee1f4158 | 1630 | show_usage(argv[0]); |
7425d456 | 1631 | return 1; |
d39c04ca AB |
1632 | } |
1633 | ||
5270dddd JA |
1634 | if (!relay_path) |
1635 | relay_path = default_relay_path; | |
1636 | ||
d5396421 | 1637 | if (act_mask_tmp != 0) |
d39c04ca | 1638 | act_mask = act_mask_tmp; |
d0ca268b | 1639 | |
e3e74029 NS |
1640 | if (statfs(relay_path, &st) < 0) { |
1641 | perror("statfs"); | |
1642 | fprintf(stderr,"%s does not appear to be a valid path\n", | |
1643 | relay_path); | |
1644 | return 1; | |
64acacae | 1645 | } else if (st.f_type != (long) RELAYFS_TYPE) { |
e3e74029 | 1646 | fprintf(stderr,"%s does not appear to be a relay filesystem\n", |
d0ca268b | 1647 | relay_path); |
7425d456 | 1648 | return 1; |
d0ca268b JA |
1649 | } |
1650 | ||
e7c9f3ff | 1651 | if (open_devices() != 0) |
7425d456 | 1652 | return 1; |
bc39777c JA |
1653 | |
1654 | if (kill_running_trace) { | |
e7c9f3ff | 1655 | stop_all_traces(); |
7425d456 | 1656 | return 0; |
bc39777c JA |
1657 | } |
1658 | ||
e7c9f3ff NS |
1659 | ncpus = sysconf(_SC_NPROCESSORS_ONLN); |
1660 | if (ncpus < 0) { | |
1661 | fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n"); | |
7425d456 | 1662 | return 1; |
d0ca268b JA |
1663 | } |
1664 | ||
d0ca268b JA |
1665 | signal(SIGINT, handle_sigint); |
1666 | signal(SIGHUP, handle_sigint); | |
1667 | signal(SIGTERM, handle_sigint); | |
ece238a6 | 1668 | signal(SIGALRM, handle_sigint); |
d0ca268b | 1669 | |
8e86c98a JA |
1670 | if (net_mode == Net_client && net_setup_client()) |
1671 | return 1; | |
1672 | ||
1673 | if (start_devices() != 0) | |
1674 | return 1; | |
1675 | ||
e7c9f3ff | 1676 | atexit(stop_all_tracing); |
830fd65c | 1677 | |
ece238a6 NS |
1678 | if (stop_watch) |
1679 | alarm(stop_watch); | |
1680 | ||
b7106311 | 1681 | wait_for_threads(); |
d0ca268b | 1682 | |
eb3c8108 JA |
1683 | if (!is_trace_stopped()) { |
1684 | trace_stopped = 1; | |
91816d54 JA |
1685 | stop_all_threads(); |
1686 | stop_all_traces(); | |
91816d54 | 1687 | } |
d0ca268b | 1688 | |
eb3c8108 JA |
1689 | show_stats(); |
1690 | ||
d0ca268b JA |
1691 | return 0; |
1692 | } | |
1693 |