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