[PATCH] Read and sort events in chunks from stdin
[blktrace.git] / blktrace.c
CommitLineData
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>
d0ca268b
JA
31#include <stdio.h>
32#include <stdlib.h>
33#include <sched.h>
d39c04ca
AB
34#include <ctype.h>
35#include <getopt.h>
d0ca268b
JA
36
37#include "blktrace.h"
38
39#define BUF_SIZE (128 *1024)
40#define BUF_NR (4)
41
d39c04ca
AB
42#define DECLARE_MASK_MAP(mask) { BLK_TC_##mask, #mask, "BLK_TC_"#mask }
43#define COMPARE_MASK_MAP(mmp, str) \
75da3c6a
NS
44 (!strcasecmp((mmp)->short_form, (str)) || \
45 !strcasecmp((mmp)->long_form, (str)))
d39c04ca
AB
46
47#define VALID_SET(x) ((1 <= (x)) && ((x) < (1 << BLK_TC_SHIFT)))
48
49struct mask_map {
50 int mask;
51 char *short_form;
52 char *long_form;
53};
54
55struct mask_map mask_maps[] = {
5c86134e
JA
56 DECLARE_MASK_MAP(READ),
57 DECLARE_MASK_MAP(WRITE),
58 DECLARE_MASK_MAP(BARRIER),
59 DECLARE_MASK_MAP(SYNC),
60 DECLARE_MASK_MAP(QUEUE),
61 DECLARE_MASK_MAP(REQUEUE),
62 DECLARE_MASK_MAP(ISSUE),
63 DECLARE_MASK_MAP(COMPLETE),
64 DECLARE_MASK_MAP(FS),
65 DECLARE_MASK_MAP(PC),
d39c04ca
AB
66};
67
d5396421
JA
68#define S_OPTS "d:a:A:r:o:"
69static struct option l_opts[] = {
5c86134e 70 {
d39c04ca
AB
71 .name = "dev",
72 .has_arg = 1,
73 .flag = NULL,
74 .val = 'd'
75 },
5c86134e 76 {
d39c04ca
AB
77 .name = "act-mask",
78 .has_arg = 1,
79 .flag = NULL,
80 .val = 'a'
81 },
5c86134e 82 {
d39c04ca
AB
83 .name = "set-mask",
84 .has_arg = 1,
85 .flag = NULL,
86 .val = 'A'
87 },
5c86134e 88 {
5270dddd
JA
89 .name = "relay",
90 .has_arg = 1,
91 .flag = NULL,
92 .val = 'r'
93 },
d5396421
JA
94 {
95 .name = "output",
96 .has_arg = 1,
97 .flag = NULL,
98 .val = 'o'
99 },
d39c04ca
AB
100 {
101 .name = NULL,
102 .has_arg = 0,
103 .flag = NULL,
104 .val = 0
105 }
106};
107
d0ca268b
JA
108struct thread_information {
109 int cpu;
110 pthread_t thread;
b9d4294e
JA
111
112 int fd;
113 char fn[MAXPATHLEN + 64];
114
d5396421
JA
115 pthread_mutex_t *fd_lock;
116 int ofd;
117
d0ca268b
JA
118 unsigned long events_processed;
119};
120
5270dddd 121static char *relay_path;
d0ca268b
JA
122
123#define is_done() (*(volatile int *)(&done))
124static volatile int done;
125
126static int devfd, ncpus;
127static struct thread_information *thread_information;
128static char *buts_name_p;
d39c04ca 129static char *dev;
d5396421 130static char *output_name;
5c86134e 131static int act_mask = ~0U;
830fd65c 132static int trace_started;
d39c04ca 133
d5396421
JA
134static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
135
d39c04ca
AB
136int find_mask_map(char *string)
137{
5c86134e
JA
138 int i;
139
140 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
75da3c6a 141 if (COMPARE_MASK_MAP(&mask_maps[i], string))
5c86134e 142 return mask_maps[i].mask;
d39c04ca 143
d39c04ca
AB
144 return -1;
145}
d0ca268b 146
3aabcd89 147static int start_trace(char *dev)
d0ca268b
JA
148{
149 struct blk_user_trace_setup buts;
150
151 devfd = open(dev, O_RDONLY);
152 if (devfd < 0) {
153 perror(dev);
154 return 1;
155 }
156
157 memset(&buts, sizeof(buts), 0);
158 buts.buf_size = BUF_SIZE;
159 buts.buf_nr = BUF_NR;
d39c04ca 160 buts.act_mask = act_mask;
d0ca268b 161
d0ca268b
JA
162 if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
163 perror("BLKSTARTTRACE");
164 return 1;
165 }
166
830fd65c 167 trace_started = 1;
d0ca268b
JA
168 buts_name_p = strdup(buts.name);
169 return 0;
170}
171
3aabcd89 172static void stop_trace(void)
d0ca268b 173{
707b0914
JA
174 if (trace_started) {
175 if (ioctl(devfd, BLKSTOPTRACE) < 0)
176 perror("BLKSTOPTRACE");
d0ca268b 177
707b0914
JA
178 close(devfd);
179 trace_started = 0;
180 }
d0ca268b
JA
181}
182
d5396421 183static void extract_data(struct thread_information *tip, char *ofn, int nb)
d0ca268b
JA
184{
185 int ret, bytes_left;
87b72777 186 unsigned char *buf, *p;
d0ca268b 187
87b72777 188 buf = malloc(nb);
d0ca268b
JA
189 p = buf;
190 bytes_left = nb;
191 while (bytes_left > 0) {
b9d4294e 192 ret = read(tip->fd, p, bytes_left);
3aabcd89
JA
193 if (!ret)
194 usleep(1000);
195 else if (ret < 0) {
b9d4294e 196 perror(tip->fn);
d0ca268b 197 fprintf(stderr, "Thread %d extract_data %s failed\n",
b9d4294e 198 tip->cpu, tip->fn);
87b72777 199 free(buf);
d0ca268b 200 exit(1);
3aabcd89 201 } else {
d0ca268b
JA
202 p += ret;
203 bytes_left -= ret;
204 }
205 }
206
d5396421 207 ret = write(tip->ofd, buf, nb);
d0ca268b
JA
208 if (ret != nb) {
209 perror(ofn);
b9d4294e 210 fprintf(stderr,"Thread %d extract_data %s failed\n", tip->cpu, ofn);
87b72777 211 free(buf);
d0ca268b
JA
212 exit(1);
213 }
87b72777
JA
214
215 free(buf);
d0ca268b
JA
216}
217
d5396421
JA
218static inline void tip_fd_unlock(struct thread_information *tip)
219{
220 if (tip->fd_lock)
221 pthread_mutex_unlock(tip->fd_lock);
222}
223
224static inline void tip_fd_lock(struct thread_information *tip)
225{
226 if (tip->fd_lock)
227 pthread_mutex_lock(tip->fd_lock);
228}
229
3aabcd89 230static void *extract(void *arg)
d0ca268b
JA
231{
232 struct thread_information *tip = arg;
d5396421
JA
233 int ret, pdu_len;
234 char dp[64];
d0ca268b
JA
235 struct blk_io_trace t;
236 pid_t pid = getpid();
237 cpu_set_t cpu_mask;
238
239 CPU_ZERO(&cpu_mask);
b9d4294e 240 CPU_SET((tip->cpu), &cpu_mask);
d0ca268b
JA
241
242 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
243 perror("sched_setaffinity");
244 exit(1);
245 }
246
b9d4294e
JA
247 snprintf(tip->fn, sizeof(tip->fn),
248 "%s/block/%s/trace%d", relay_path, buts_name_p, tip->cpu);
249 tip->fd = open(tip->fn, O_RDONLY);
250 if (tip->fd < 0) {
251 perror(tip->fn);
5c86134e
JA
252 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
253 tip->fn);
d0ca268b
JA
254 exit(1);
255 }
256
257 while (!is_done()) {
b9d4294e 258 ret = read(tip->fd, &t, sizeof(t));
d0ca268b
JA
259 if (ret != sizeof(t)) {
260 if (ret < 0) {
b9d4294e 261 perror(tip->fn);
d0ca268b 262 fprintf(stderr,"Thread %d failed read of %s\n",
b9d4294e 263 tip->cpu, tip->fn);
d0ca268b
JA
264 exit(1);
265 } else if (ret > 0) {
8fc0abbc 266 fprintf(stderr,"Thread %d misread %s %d,%d\n",
b9d4294e 267 tip->cpu, tip->fn, ret, (int)sizeof(t));
d0ca268b
JA
268 exit(1);
269 } else {
270 usleep(10000);
271 continue;
272 }
273 }
274
275 if (verify_trace(&t))
276 exit(1);
277
18ada3d4
JA
278 pdu_len = t.pdu_len;
279
6fe4709e
JA
280 trace_to_be(&t);
281
d5396421
JA
282 tip_fd_lock(tip);
283
284 ret = write(tip->ofd, &t, sizeof(t));
d0ca268b 285 if (ret < 0) {
d5396421
JA
286 fprintf(stderr,"Thread %d failed write\n", tip->cpu);
287 tip_fd_unlock(tip);
d0ca268b
JA
288 exit(1);
289 }
290
18ada3d4 291 if (pdu_len)
d5396421
JA
292 extract_data(tip, dp, pdu_len);
293
294 tip_fd_unlock(tip);
87b72777 295
d0ca268b
JA
296 tip->events_processed++;
297 }
298
299 return NULL;
300}
301
3aabcd89 302static int start_threads(void)
d0ca268b
JA
303{
304 struct thread_information *tip;
d5396421 305 char op[64];
d0ca268b
JA
306 int i;
307
308 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
309 if (ncpus < 0) {
310 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
311 return 1;
312 }
d0ca268b
JA
313
314 thread_information = malloc(ncpus * sizeof(struct thread_information));
315 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
d5396421 316 tip->fd_lock = NULL;
d0ca268b
JA
317 tip->cpu = i;
318 tip->events_processed = 0;
319
d5396421
JA
320 if (!strcmp(output_name, "-")) {
321 tip->ofd = dup(1);
322 tip->fd_lock = &stdout_mutex;
323 } else {
324 sprintf(op, "%s_out.%d", output_name, tip->cpu);
325 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
326 }
327
328 if (tip->ofd < 0) {
329 perror(op);
330 return 1;
331 }
332
d0ca268b
JA
333 if (pthread_create(&tip->thread, NULL, extract, tip)) {
334 perror( "pthread_create");
335 return 0;
336 }
337 }
338
339 return ncpus;
340}
341
3aabcd89
JA
342static void stop_threads(void)
343{
344 struct thread_information *tip = thread_information;
345 int i;
346
347 for (i = 0; i < ncpus; i++, tip++) {
348 int ret;
349
350 if (pthread_join(tip->thread, (void *) &ret))
351 perror("thread_join");
d5396421 352
b9d4294e 353 close(tip->fd);
d5396421 354 close(tip->ofd);
3aabcd89
JA
355 }
356}
357
d0ca268b
JA
358void show_stats(void)
359{
360 int i;
361 struct thread_information *tip;
362 unsigned long events_processed = 0;
363
d5396421
JA
364 if (!strcmp(output_name, "-"))
365 return;
366
d0ca268b
JA
367 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
368 printf("CPU%3d: %20ld events\n",
369 tip->cpu, tip->events_processed);
370 events_processed += tip->events_processed;
371 }
372
373 printf("Total: %20ld events\n", events_processed);
374}
375
376void handle_sigint(int sig)
377{
d0ca268b
JA
378 done = 1;
379}
380
381int main(int argc, char *argv[])
382{
5270dddd 383 static char default_relay_path[] = "/relay";
d0ca268b 384 struct stat st;
d39c04ca
AB
385 int i, c;
386 int act_mask_tmp = 0;
387
388 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
389 switch (c) {
390 case 'a':
391 i = find_mask_map(optarg);
392 if (i < 0) {
393 fprintf(stderr,"Invalid action mask %s\n",
394 optarg);
395 return 4;
396 }
397 act_mask_tmp |= i;
398 break;
399
400 case 'A':
401 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
402 fprintf(stderr,
403 "Invalid set action mask %s/0x%x\n",
404 optarg, i);
405 return 4;
406 }
407 act_mask_tmp = i;
408 break;
d0ca268b 409
d39c04ca
AB
410 case 'd':
411 dev = strdup(optarg);
412 break;
413
5270dddd
JA
414 case 'r':
415 relay_path = optarg;
416 break;
417
d5396421
JA
418 case 'o':
419 output_name = strdup(optarg);
420 break;
421
d39c04ca
AB
422 default:
423 fprintf(stderr,"Usage: %s -d <dev> "
424 "[-a <trace> [-a <trace>]]\n", argv[0]);
425 return 4;
426 }
427 }
428
429 if ((dev == NULL) || (optind < argc)) {
430 fprintf(stderr,"Usage: %s -d <dev> "
431 "[-a <trace> [-a <trace>]]\n", argv[0]);
432 return 4;
433 }
434
5270dddd
JA
435 if (!relay_path)
436 relay_path = default_relay_path;
437
d5396421 438 if (act_mask_tmp != 0)
d39c04ca 439 act_mask = act_mask_tmp;
d0ca268b
JA
440
441 if (stat(relay_path, &st) < 0) {
d39c04ca 442 fprintf(stderr,"%s does not appear to be mounted\n",
d0ca268b
JA
443 relay_path);
444 return 2;
445 }
446
d39c04ca 447 if (start_trace(dev)) {
5270dddd 448 close(devfd);
d39c04ca 449 fprintf(stderr, "Failed to start trace on %s\n", dev);
d0ca268b
JA
450 return 3;
451 }
452
453 setlocale(LC_NUMERIC, "en_US");
454
d5396421
JA
455 if (!output_name)
456 output_name = strdup(buts_name_p);
457
d0ca268b
JA
458 i = start_threads();
459 if (!i) {
460 fprintf(stderr, "Failed to start worker threads\n");
461 stop_trace();
462 return 4;
463 }
464
d0ca268b
JA
465 signal(SIGINT, handle_sigint);
466 signal(SIGHUP, handle_sigint);
467 signal(SIGTERM, handle_sigint);
468
707b0914 469 atexit(stop_trace);
830fd65c 470
d0ca268b
JA
471 while (!is_done())
472 sleep(1);
473
b9d4294e 474 stop_threads();
3a9995b9 475 stop_trace();
d0ca268b
JA
476 show_stats();
477
478 return 0;
479}
480