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