2 * block queue tracing parse application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
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.
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.
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
21 #include <sys/types.h>
38 static char blkparse_version[] = "0.99";
41 unsigned long start, end;
42 struct skip_info *prev, *next;
50 unsigned long long events;
51 unsigned long long first_reported_time;
52 unsigned long long last_reported_time;
53 unsigned long long last_read_time;
54 struct io_stats io_stats;
55 unsigned long skips, nskips;
56 unsigned long long seq_skips, seq_nskips;
57 unsigned int max_depth[2];
58 unsigned int cur_depth[2];
60 struct rb_root rb_track;
65 unsigned long *cpu_map;
66 unsigned int cpu_map_max;
68 struct per_cpu_info *cpus;
71 struct per_process_info {
74 struct io_stats io_stats;
75 struct per_process_info *hash_next, *list_next;
81 unsigned long long longest_allocation_wait[2];
82 unsigned long long longest_dispatch_wait[2];
83 unsigned long long longest_completion_wait[2];
86 #define PPI_HASH_SHIFT (8)
87 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
88 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
89 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
90 static struct per_process_info *ppi_list;
91 static int ppi_list_entries;
93 #define S_OPTS "a:A:i:o:b:stqw:f:F:vVhD:"
94 static struct option l_opts[] = {
97 .has_arg = required_argument,
103 .has_arg = required_argument,
109 .has_arg = required_argument,
115 .has_arg = required_argument,
121 .has_arg = required_argument,
126 .name = "per-program-stats",
127 .has_arg = no_argument,
133 .has_arg = no_argument,
139 .has_arg = no_argument,
145 .has_arg = required_argument,
151 .has_arg = required_argument,
156 .name = "format-spec",
157 .has_arg = required_argument,
162 .name = "hash-by-name",
163 .has_arg = no_argument,
169 .has_arg = no_argument,
175 .has_arg = no_argument,
180 .name = "input-directory",
181 .has_arg = required_argument,
191 * for sorting the displayed output
194 struct blk_io_trace *bit;
195 struct rb_node rb_node;
197 unsigned long read_sequence;
200 static struct rb_root rb_sort_root;
201 static unsigned long rb_sort_entries;
203 static struct trace *trace_list;
208 static struct blk_io_trace *bit_alloc_list;
209 static struct trace *t_alloc_list;
212 * for tracking individual ios
215 struct rb_node rb_node;
220 unsigned long long allocation_time;
221 unsigned long long queue_time;
222 unsigned long long dispatch_time;
223 unsigned long long completion_time;
227 static struct per_dev_info *devices;
228 static char *get_dev_name(struct per_dev_info *, char *, int);
229 static int trace_rb_insert_last(struct per_dev_info *, struct trace *);
232 static char *output_name;
233 static char *input_dir;
235 static unsigned long long genesis_time;
236 static unsigned long long last_allowed_time;
237 static unsigned long long stopwatch_start; /* start from zero by default */
238 static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
239 static unsigned long read_sequence;
241 static int per_process_stats;
242 static int per_device_and_cpu_stats = 1;
243 static int track_ios;
244 static int ppi_hash_by_pid = 1;
246 static unsigned int act_mask = -1U;
247 static int stats_printed;
249 static unsigned int t_alloc_cache;
250 static unsigned int bit_alloc_cache;
252 #define RB_BATCH_DEFAULT (512)
253 static unsigned int rb_batch = RB_BATCH_DEFAULT;
257 #define is_done() (*(volatile int *)(&done))
258 static volatile int done;
260 #define JHASH_RANDOM (0x3af5f2ee)
262 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
263 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
264 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
266 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
268 struct per_cpu_info *cpus = pdi->cpus;
269 int ncpus = pdi->ncpus;
270 int new_count = cpu + 1;
274 size = new_count * sizeof(struct per_cpu_info);
275 cpus = realloc(cpus, size);
278 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
279 get_dev_name(pdi, name, sizeof(name)), size);
283 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
284 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
285 memset(new_start, 0, new_space);
287 pdi->ncpus = new_count;
290 for (new_count = 0; new_count < pdi->ncpus; new_count++) {
291 struct per_cpu_info *pci = &pdi->cpus[new_count];
295 memset(&pci->rb_last, 0, sizeof(pci->rb_last));
296 pci->rb_last_entries = 0;
297 pci->last_sequence = -1;
302 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
304 struct per_cpu_info *pci;
306 if (cpu >= pdi->ncpus)
307 resize_cpu_info(pdi, cpu);
309 pci = &pdi->cpus[cpu];
315 static int resize_devices(char *name)
317 int size = (ndevices + 1) * sizeof(struct per_dev_info);
319 devices = realloc(devices, size);
321 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
324 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
325 devices[ndevices].name = name;
330 static struct per_dev_info *get_dev_info(dev_t dev)
332 struct per_dev_info *pdi;
335 for (i = 0; i < ndevices; i++) {
337 devices[i].dev = dev;
338 if (devices[i].dev == dev)
342 if (resize_devices(NULL))
345 pdi = &devices[ndevices - 1];
347 pdi->first_reported_time = 0;
348 pdi->last_read_time = 0;
353 static void insert_skip(struct per_cpu_info *pci, unsigned long start,
356 struct skip_info *sip;
358 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
359 if (end == (sip->start - 1)) {
362 } else if (start == (sip->end + 1)) {
368 sip = malloc(sizeof(struct skip_info));
371 sip->prev = sip->next = NULL;
372 if (pci->skips_tail == NULL)
373 pci->skips_head = pci->skips_tail = sip;
375 sip->prev = pci->skips_tail;
376 pci->skips_tail->next = sip;
377 pci->skips_tail = sip;
381 static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
383 if (sip->prev == NULL) {
384 if (sip->next == NULL)
385 pci->skips_head = pci->skips_tail = NULL;
387 pci->skips_head = sip->next;
388 sip->next->prev = NULL;
390 } else if (sip->next == NULL) {
391 pci->skips_tail = sip->prev;
392 sip->prev->next = NULL;
394 sip->prev->next = sip->next;
395 sip->next->prev = sip->prev;
398 sip->prev = sip->next = NULL;
402 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
403 static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
405 struct skip_info *sip;
407 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
408 if (IN_SKIP(sip, seq)) {
409 if (sip->start == seq) {
411 remove_sip(pci, sip);
414 } else if (sip->end == seq)
418 insert_skip(pci, seq + 1, sip->end);
427 static void collect_pdi_skips(struct per_dev_info *pdi)
429 struct skip_info *sip;
435 for (cpu = 0; cpu < pdi->ncpus; cpu++) {
436 struct per_cpu_info *pci = &pdi->cpus[cpu];
438 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
440 pdi->seq_skips += (sip->end - sip->start + 1);
442 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
443 MAJOR(pdi->dev), MINOR(pdi->dev),
444 sip->start, sip->end);
449 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
451 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
452 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
453 unsigned long *map = malloc(new_max / sizeof(long));
455 memset(map, 0, new_max / sizeof(long));
458 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
463 pdi->cpu_map_max = new_max;
466 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
469 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
471 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
474 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
476 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
479 static inline int ppi_hash_pid(__u32 pid)
481 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
484 static inline int ppi_hash_name(const char *name)
486 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
489 static inline int ppi_hash(struct per_process_info *ppi)
492 return ppi_hash_pid(ppi->pid);
494 return ppi_hash_name(ppi->name);
497 static inline void add_process_to_hash(struct per_process_info *ppi)
499 const int hash_idx = ppi_hash(ppi);
501 ppi->hash_next = ppi_hash_table[hash_idx];
502 ppi_hash_table[hash_idx] = ppi;
505 static inline void add_process_to_list(struct per_process_info *ppi)
507 ppi->list_next = ppi_list;
512 static struct per_process_info *find_process_by_name(char *name)
514 const int hash_idx = ppi_hash_name(name);
515 struct per_process_info *ppi;
517 ppi = ppi_hash_table[hash_idx];
519 if (!strcmp(ppi->name, name))
522 ppi = ppi->hash_next;
528 static struct per_process_info *find_process_by_pid(__u32 pid)
530 const int hash_idx = ppi_hash_pid(pid);
531 struct per_process_info *ppi;
533 ppi = ppi_hash_table[hash_idx];
538 ppi = ppi->hash_next;
544 static struct per_process_info *find_process(__u32 pid, char *name)
546 struct per_process_info *ppi;
549 return find_process_by_pid(pid);
551 ppi = find_process_by_name(name);
552 if (ppi && ppi->pid != pid)
553 ppi->more_than_one = 1;
559 * struct trace and blktrace allocation cache, we do potentially
560 * millions of mallocs for these structures while only using at most
561 * a few thousand at the time
563 static inline void t_free(struct trace *t)
565 if (t_alloc_cache < 1024) {
566 t->next = t_alloc_list;
573 static inline struct trace *t_alloc(void)
575 struct trace *t = t_alloc_list;
578 t_alloc_list = t->next;
583 return malloc(sizeof(*t));
586 static inline void bit_free(struct blk_io_trace *bit)
588 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
590 * abuse a 64-bit field for a next pointer for the free item
592 bit->time = (__u64) (unsigned long) bit_alloc_list;
593 bit_alloc_list = (struct blk_io_trace *) bit;
599 static inline struct blk_io_trace *bit_alloc(void)
601 struct blk_io_trace *bit = bit_alloc_list;
604 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
610 return malloc(sizeof(*bit));
613 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
615 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
617 rb_erase(&t->rb_node, &pci->rb_last);
618 pci->rb_last_entries--;
624 static void put_trace(struct per_dev_info *pdi, struct trace *t)
626 rb_erase(&t->rb_node, &rb_sort_root);
629 trace_rb_insert_last(pdi, t);
632 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
634 struct rb_node **p = &root->rb_node;
635 struct rb_node *parent = NULL;
641 __t = rb_entry(parent, struct trace, rb_node);
643 if (t->bit->time < __t->bit->time)
645 else if (t->bit->time > __t->bit->time)
647 else if (t->bit->device < __t->bit->device)
649 else if (t->bit->device > __t->bit->device)
651 else if (t->bit->sequence < __t->bit->sequence)
653 else /* >= sequence */
657 rb_link_node(&t->rb_node, parent, p);
658 rb_insert_color(&t->rb_node, root);
662 static inline int trace_rb_insert_sort(struct trace *t)
664 if (!trace_rb_insert(t, &rb_sort_root)) {
672 static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
674 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
676 if (trace_rb_insert(t, &pci->rb_last))
679 pci->rb_last_entries++;
681 if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
682 struct rb_node *n = rb_first(&pci->rb_last);
684 t = rb_entry(n, struct trace, rb_node);
685 __put_trace_last(pdi, t);
691 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
692 struct rb_root *root, int order)
694 struct rb_node *n = root->rb_node;
695 struct rb_node *prev = NULL;
699 __t = rb_entry(n, struct trace, rb_node);
702 if (device < __t->bit->device)
704 else if (device > __t->bit->device)
706 else if (sequence < __t->bit->sequence)
708 else if (sequence > __t->bit->sequence)
715 * hack - the list may not be sequence ordered because some
716 * events don't have sequence and time matched. so we end up
717 * being a little off in the rb lookup here, because we don't
718 * know the time we are looking for. compensate by browsing
719 * a little ahead from the last entry to find the match
724 while (((n = rb_next(prev)) != NULL) && max--) {
725 __t = rb_entry(n, struct trace, rb_node);
727 if (__t->bit->device == device &&
728 __t->bit->sequence == sequence)
738 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
739 struct per_cpu_info *pci,
742 return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
745 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
747 struct rb_node **p = &pdi->rb_track.rb_node;
748 struct rb_node *parent = NULL;
749 struct io_track *__iot;
753 __iot = rb_entry(parent, struct io_track, rb_node);
755 if (iot->sector < __iot->sector)
757 else if (iot->sector > __iot->sector)
761 "sector alias (%Lu) on device %d,%d!\n",
762 (unsigned long long) iot->sector,
763 MAJOR(pdi->dev), MINOR(pdi->dev));
768 rb_link_node(&iot->rb_node, parent, p);
769 rb_insert_color(&iot->rb_node, &pdi->rb_track);
773 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
775 struct rb_node *n = pdi->rb_track.rb_node;
776 struct io_track *__iot;
779 __iot = rb_entry(n, struct io_track, rb_node);
781 if (sector < __iot->sector)
783 else if (sector > __iot->sector)
792 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
793 char *comm, __u64 sector)
795 struct io_track *iot;
797 iot = __find_track(pdi, sector);
799 iot = malloc(sizeof(*iot));
801 memcpy(iot->comm, comm, sizeof(iot->comm));
802 iot->sector = sector;
803 track_rb_insert(pdi, iot);
809 static void log_track_frontmerge(struct per_dev_info *pdi,
810 struct blk_io_trace *t)
812 struct io_track *iot;
817 iot = __find_track(pdi, t->sector + t_sec(t));
820 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
821 MAJOR(pdi->dev), MINOR(pdi->dev),
822 (unsigned long long) t->sector + t_sec(t));
826 rb_erase(&iot->rb_node, &pdi->rb_track);
827 iot->sector -= t_sec(t);
828 track_rb_insert(pdi, iot);
831 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
833 struct io_track *iot;
838 iot = find_track(pdi, t->pid, t->comm, t->sector);
839 iot->allocation_time = t->time;
843 * return time between rq allocation and insertion
845 static unsigned long long log_track_insert(struct per_dev_info *pdi,
846 struct blk_io_trace *t)
848 unsigned long long elapsed;
849 struct io_track *iot;
854 iot = find_track(pdi, t->pid, t->comm, t->sector);
855 iot->queue_time = t->time;
857 if (!iot->allocation_time)
860 elapsed = iot->queue_time - iot->allocation_time;
862 if (per_process_stats) {
863 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
864 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
866 if (ppi && elapsed > ppi->longest_allocation_wait[w])
867 ppi->longest_allocation_wait[w] = elapsed;
874 * return time between queue and issue
876 static unsigned long long log_track_issue(struct per_dev_info *pdi,
877 struct blk_io_trace *t)
879 unsigned long long elapsed;
880 struct io_track *iot;
884 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
887 iot = __find_track(pdi, t->sector);
890 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
891 MAJOR(pdi->dev), MINOR(pdi->dev),
892 (unsigned long long) t->sector);
896 iot->dispatch_time = t->time;
897 elapsed = iot->dispatch_time - iot->queue_time;
899 if (per_process_stats) {
900 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
901 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
903 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
904 ppi->longest_dispatch_wait[w] = elapsed;
911 * return time between dispatch and complete
913 static unsigned long long log_track_complete(struct per_dev_info *pdi,
914 struct blk_io_trace *t)
916 unsigned long long elapsed;
917 struct io_track *iot;
921 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
924 iot = __find_track(pdi, t->sector);
927 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
928 MAJOR(pdi->dev), MINOR(pdi->dev),
929 (unsigned long long) t->sector);
933 iot->completion_time = t->time;
934 elapsed = iot->completion_time - iot->dispatch_time;
936 if (per_process_stats) {
937 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
938 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
940 if (ppi && elapsed > ppi->longest_completion_wait[w])
941 ppi->longest_completion_wait[w] = elapsed;
945 * kill the trace, we don't need it after completion
947 rb_erase(&iot->rb_node, &pdi->rb_track);
954 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
956 struct per_process_info *ppi = find_process(pid, name);
959 ppi = malloc(sizeof(*ppi));
960 memset(ppi, 0, sizeof(*ppi));
961 memcpy(ppi->name, name, 16);
963 add_process_to_hash(ppi);
964 add_process_to_list(ppi);
967 return &ppi->io_stats;
970 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
973 snprintf(buffer, size, "%s", pdi->name);
975 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
979 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
981 unsigned long long this = bit->time;
982 unsigned long long last = pdi->last_reported_time;
984 pdi->backwards = (this < last) ? 'B' : ' ';
985 pdi->last_reported_time = this;
988 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
993 ios->qwrite_kb += t_kb(t);
996 ios->qread_kb += t_kb(t);
1000 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1003 __account_m(&pci->io_stats, t, rw);
1005 if (per_process_stats) {
1006 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
1008 __account_m(ios, t, rw);
1012 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1017 ios->qwrite_kb += t_kb(t);
1020 ios->qread_kb += t_kb(t);
1024 static inline void account_queue(struct blk_io_trace *t,
1025 struct per_cpu_info *pci, int rw)
1027 __account_queue(&pci->io_stats, t, rw);
1029 if (per_process_stats) {
1030 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
1032 __account_queue(ios, t, rw);
1036 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
1040 ios->cwrite_kb += bytes >> 10;
1043 ios->cread_kb += bytes >> 10;
1047 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1050 __account_c(&pci->io_stats, rw, bytes);
1052 if (per_process_stats) {
1053 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
1055 __account_c(ios, rw, bytes);
1059 static inline void __account_issue(struct io_stats *ios, int rw,
1064 ios->iwrite_kb += bytes >> 10;
1067 ios->iread_kb += bytes >> 10;
1071 static inline void account_issue(struct blk_io_trace *t,
1072 struct per_cpu_info *pci, int rw)
1074 __account_issue(&pci->io_stats, rw, t->bytes);
1076 if (per_process_stats) {
1077 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
1079 __account_issue(ios, rw, t->bytes);
1083 static inline void __account_unplug(struct io_stats *ios, int timer)
1086 ios->timer_unplugs++;
1091 static inline void account_unplug(struct blk_io_trace *t,
1092 struct per_cpu_info *pci, int timer)
1094 __account_unplug(&pci->io_stats, timer);
1096 if (per_process_stats) {
1097 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
1099 __account_unplug(ios, timer);
1103 static inline void __account_requeue(struct io_stats *ios,
1104 struct blk_io_trace *t, int rw)
1108 ios->iwrite_kb -= t_kb(t);
1111 ios->iread_kb -= t_kb(t);
1115 static inline void account_requeue(struct blk_io_trace *t,
1116 struct per_cpu_info *pci, int rw)
1118 __account_requeue(&pci->io_stats, t, rw);
1120 if (per_process_stats) {
1121 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
1123 __account_requeue(ios, t, rw);
1127 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1128 struct blk_io_trace *t, char *act)
1130 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1133 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1134 struct blk_io_trace *t, char *act)
1136 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1139 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1142 process_fmt(act, pci, t, -1, 0, NULL);
1145 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1146 struct blk_io_trace *t, char *act)
1148 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1151 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1152 struct blk_io_trace *t, char *act)
1155 log_track_frontmerge(pdi, t);
1157 process_fmt(act, pci, t, -1ULL, 0, NULL);
1160 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1163 process_fmt(act, pci, t, -1ULL, 0, NULL);
1166 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1169 process_fmt(act, pci, t, -1ULL, 0, NULL);
1172 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1175 process_fmt(act, pci, t, -1ULL, 0, NULL);
1178 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1181 process_fmt(act, pci, t, -1ULL, 0, NULL);
1184 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1186 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1188 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1191 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1193 int act = t->action & 0xffff;
1196 case __BLK_TA_QUEUE:
1197 log_generic(pci, t, "Q");
1199 case __BLK_TA_GETRQ:
1200 log_generic(pci, t, "G");
1202 case __BLK_TA_SLEEPRQ:
1203 log_generic(pci, t, "S");
1205 case __BLK_TA_REQUEUE:
1206 log_generic(pci, t, "R");
1208 case __BLK_TA_ISSUE:
1209 log_pc(pci, t, "D");
1211 case __BLK_TA_COMPLETE:
1212 log_pc(pci, t, "C");
1214 case __BLK_TA_INSERT:
1215 log_pc(pci, t, "I");
1218 fprintf(stderr, "Bad pc action %x\n", act);
1223 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1224 struct per_cpu_info *pci)
1226 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1227 int act = t->action & 0xffff;
1230 case __BLK_TA_QUEUE:
1231 account_queue(t, pci, w);
1232 log_queue(pci, t, "Q");
1234 case __BLK_TA_INSERT:
1235 log_insert(pdi, pci, t, "I");
1237 case __BLK_TA_BACKMERGE:
1238 account_m(t, pci, w);
1239 log_merge(pdi, pci, t, "M");
1241 case __BLK_TA_FRONTMERGE:
1242 account_m(t, pci, w);
1243 log_merge(pdi, pci, t, "F");
1245 case __BLK_TA_GETRQ:
1246 log_track_getrq(pdi, t);
1247 log_generic(pci, t, "G");
1249 case __BLK_TA_SLEEPRQ:
1250 log_generic(pci, t, "S");
1252 case __BLK_TA_REQUEUE:
1254 * can happen if we miss traces, don't let it go
1257 if (pdi->cur_depth[w])
1258 pdi->cur_depth[w]--;
1259 account_requeue(t, pci, w);
1260 log_queue(pci, t, "R");
1262 case __BLK_TA_ISSUE:
1263 account_issue(t, pci, w);
1264 pdi->cur_depth[w]++;
1265 if (pdi->cur_depth[w] > pdi->max_depth[w])
1266 pdi->max_depth[w] = pdi->cur_depth[w];
1267 log_issue(pdi, pci, t, "D");
1269 case __BLK_TA_COMPLETE:
1270 if (pdi->cur_depth[w])
1271 pdi->cur_depth[w]--;
1272 account_c(t, pci, w, t->bytes);
1273 log_complete(pdi, pci, t, "C");
1276 log_action(pci, t, "P");
1278 case __BLK_TA_UNPLUG_IO:
1279 account_unplug(t, pci, 0);
1280 log_unplug(pci, t, "U");
1282 case __BLK_TA_UNPLUG_TIMER:
1283 account_unplug(t, pci, 1);
1284 log_unplug(pci, t, "UT");
1286 case __BLK_TA_SPLIT:
1287 log_split(pci, t, "X");
1289 case __BLK_TA_BOUNCE:
1290 log_generic(pci, t, "B");
1292 case __BLK_TA_REMAP:
1293 log_generic(pci, t, "A");
1296 fprintf(stderr, "Bad fs action %x\n", t->action);
1301 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1302 struct per_dev_info *pdi)
1304 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1305 dump_trace_pc(t, pci);
1307 dump_trace_fs(t, pdi, pci);
1310 pdi->first_reported_time = t->time;
1316 * print in a proper way, not too small and not too big. if more than
1317 * 1000,000K, turn into M and so on
1319 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1321 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1327 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1332 sprintf(dst, "%'8Lu%c", num, suff[i]);
1336 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1339 static char x[256], y[256];
1341 fprintf(ofp, "%s\n", msg);
1343 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1344 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1346 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1347 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1348 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1349 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
1350 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1351 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1352 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1353 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1355 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1356 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1358 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1359 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1362 static void dump_wait_stats(struct per_process_info *ppi)
1364 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1365 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1366 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1367 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1368 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1369 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1371 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1372 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1373 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1374 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1375 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1376 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1379 static int ppi_name_compare(const void *p1, const void *p2)
1381 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1382 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1385 res = strverscmp(ppi1->name, ppi2->name);
1387 res = ppi1->pid > ppi2->pid;
1392 static void sort_process_list(void)
1394 struct per_process_info **ppis;
1395 struct per_process_info *ppi;
1398 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1403 ppi = ppi->list_next;
1406 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1408 i = ppi_list_entries - 1;
1413 ppi->list_next = ppi_list;
1421 static void show_process_stats(void)
1423 struct per_process_info *ppi;
1425 sort_process_list();
1431 if (ppi->more_than_one)
1432 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1434 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1436 dump_io_stats(NULL, &ppi->io_stats, name);
1437 dump_wait_stats(ppi);
1438 ppi = ppi->list_next;
1444 static void show_device_and_cpu_stats(void)
1446 struct per_dev_info *pdi;
1447 struct per_cpu_info *pci;
1448 struct io_stats total, *ios;
1449 unsigned long long rrate, wrate, msec;
1450 int i, j, pci_events;
1451 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1454 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1456 memset(&total, 0, sizeof(total));
1462 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1466 ios = &pci->io_stats;
1467 total.qreads += ios->qreads;
1468 total.qwrites += ios->qwrites;
1469 total.creads += ios->creads;
1470 total.cwrites += ios->cwrites;
1471 total.mreads += ios->mreads;
1472 total.mwrites += ios->mwrites;
1473 total.ireads += ios->ireads;
1474 total.iwrites += ios->iwrites;
1475 total.rrqueue += ios->rrqueue;
1476 total.wrqueue += ios->wrqueue;
1477 total.qread_kb += ios->qread_kb;
1478 total.qwrite_kb += ios->qwrite_kb;
1479 total.cread_kb += ios->cread_kb;
1480 total.cwrite_kb += ios->cwrite_kb;
1481 total.iread_kb += ios->iread_kb;
1482 total.iwrite_kb += ios->iwrite_kb;
1483 total.timer_unplugs += ios->timer_unplugs;
1484 total.io_unplugs += ios->io_unplugs;
1486 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1487 j, get_dev_name(pdi, name, sizeof(name)));
1488 dump_io_stats(pdi, ios, line);
1492 if (pci_events > 1) {
1494 snprintf(line, sizeof(line) - 1, "Total (%s):",
1495 get_dev_name(pdi, name, sizeof(name)));
1496 dump_io_stats(NULL, &total, line);
1500 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1502 rrate = 1000 * total.cread_kb / msec;
1503 wrate = 1000 * total.cwrite_kb / msec;
1506 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1508 fprintf(ofp, "Events (%s): %'Lu entries\n",
1509 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1511 collect_pdi_skips(pdi);
1512 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
1513 pdi->skips,pdi->seq_skips,
1514 100.0 * ((double)pdi->seq_skips /
1515 (double)(pdi->events + pdi->seq_skips)));
1519 static void find_genesis(void)
1521 struct trace *t = trace_list;
1523 genesis_time = -1ULL;
1525 if (t->bit->time < genesis_time)
1526 genesis_time = t->bit->time;
1532 static inline int check_stopwatch(struct blk_io_trace *bit)
1534 if (bit->time < stopwatch_end &&
1535 bit->time >= stopwatch_start)
1542 * return youngest entry read
1544 static int sort_entries(unsigned long long *youngest)
1546 struct per_dev_info *pdi = NULL;
1547 struct per_cpu_info *pci = NULL;
1554 while ((t = trace_list) != NULL) {
1555 struct blk_io_trace *bit = t->bit;
1557 trace_list = t->next;
1559 bit->time -= genesis_time;
1561 if (bit->time < *youngest || !*youngest)
1562 *youngest = bit->time;
1564 if (!pdi || pdi->dev != bit->device) {
1565 pdi = get_dev_info(bit->device);
1569 if (!pci || pci->cpu != bit->cpu)
1570 pci = get_cpu_info(pdi, bit->cpu);
1572 if (bit->sequence < pci->smallest_seq_read)
1573 pci->smallest_seq_read = bit->sequence;
1575 if (check_stopwatch(bit)) {
1581 if (trace_rb_insert_sort(t))
1589 * to continue, we must have traces from all online cpus in the tree
1591 static int check_cpu_map(struct per_dev_info *pdi)
1593 unsigned long *cpu_map;
1600 * create a map of the cpus we have traces for
1602 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1603 n = rb_first(&rb_sort_root);
1605 __t = rb_entry(n, struct trace, rb_node);
1606 cpu = __t->bit->cpu;
1608 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1613 * we can't continue if pdi->cpu_map has entries set that we don't
1614 * have in the sort rbtree. the opposite is not a problem, though
1617 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1618 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1628 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1630 struct blk_io_trace *bit = t->bit;
1631 unsigned long expected_sequence;
1632 struct per_cpu_info *pci;
1635 pci = get_cpu_info(pdi, bit->cpu);
1636 expected_sequence = pci->last_sequence + 1;
1638 if (!expected_sequence) {
1640 * 1 should be the first entry, just allow it
1642 if (bit->sequence == 1)
1644 if (bit->sequence == pci->smallest_seq_read)
1647 return check_cpu_map(pdi);
1650 if (bit->sequence == expected_sequence)
1654 * we may not have seen that sequence yet. if we are not doing
1655 * the final run, break and wait for more entries.
1657 if (expected_sequence < pci->smallest_seq_read) {
1658 __t = trace_rb_find_last(pdi, pci, expected_sequence);
1662 __put_trace_last(pdi, __t);
1664 } else if (!force) {
1668 if (check_current_skips(pci, bit->sequence))
1671 if (expected_sequence < bit->sequence)
1672 insert_skip(pci, expected_sequence, bit->sequence - 1);
1677 static void show_entries_rb(int force)
1679 struct per_dev_info *pdi = NULL;
1680 struct per_cpu_info *pci = NULL;
1681 struct blk_io_trace *bit;
1685 while ((n = rb_first(&rb_sort_root)) != NULL) {
1686 if (is_done() && !force && !pipeline)
1689 t = rb_entry(n, struct trace, rb_node);
1692 if (read_sequence - t->read_sequence < 1 && !force)
1695 if (!pdi || pdi->dev != bit->device) {
1696 pdi = get_dev_info(bit->device);
1701 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1702 MAJOR(bit->device), MINOR(bit->device));
1706 if (check_sequence(pdi, t, force))
1709 if (!force && bit->time > last_allowed_time)
1712 check_time(pdi, bit);
1714 if (!pci || pci->cpu != bit->cpu)
1715 pci = get_cpu_info(pdi, bit->cpu);
1717 pci->last_sequence = bit->sequence;
1721 if (bit->action & (act_mask << BLK_TC_SHIFT))
1722 dump_trace(bit, pci, pdi);
1728 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
1730 int ret, bytes_left, fl;
1733 if (block != *fdblock) {
1734 fl = fcntl(fd, F_GETFL);
1738 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1741 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1747 while (bytes_left > 0) {
1748 ret = read(fd, p, bytes_left);
1752 if (errno != EAGAIN) {
1758 * never do partial reads. we can return if we
1759 * didn't read anything and we should not block,
1760 * otherwise wait for data
1762 if ((bytes_left == bytes) && !block)
1776 static int read_events(int fd, int always_block, int *fdblock)
1778 struct per_dev_info *pdi = NULL;
1779 unsigned int events = 0;
1781 while (!is_done() && events < rb_batch) {
1782 struct blk_io_trace *bit;
1784 int pdu_len, should_block, ret;
1789 should_block = !events || always_block;
1791 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
1794 if (!events && ret < 0)
1799 magic = be32_to_cpu(bit->magic);
1800 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1801 fprintf(stderr, "Bad magic %x\n", magic);
1805 pdu_len = be16_to_cpu(bit->pdu_len);
1807 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1809 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
1819 if (verify_trace(bit)) {
1825 memset(t, 0, sizeof(*t));
1827 t->read_sequence = read_sequence;
1829 t->next = trace_list;
1832 if (!pdi || pdi->dev != bit->device)
1833 pdi = get_dev_info(bit->device);
1835 if (bit->time > pdi->last_read_time)
1836 pdi->last_read_time = bit->time;
1844 static int do_file(void)
1846 struct per_cpu_info *pci;
1847 struct per_dev_info *pdi;
1848 int i, j, events, events_added;
1851 * first prepare all files for reading
1853 for (i = 0; i < ndevices; i++) {
1862 pci = get_cpu_info(pdi, j);
1867 p = strdup(pdi->name);
1869 if (strcmp(dname, ".")) {
1871 p = strdup(pdi->name);
1872 strcpy(pdi->name, basename(p));
1877 len = sprintf(pci->fname, "%s/", input_dir);
1879 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
1880 "%s.blktrace.%d", pdi->name, pci->cpu);
1881 if (stat(pci->fname, &st) < 0)
1884 pci->fd = open(pci->fname, O_RDONLY);
1891 printf("Input file %s added\n", pci->fname);
1893 cpu_mark_online(pdi, pci->cpu);
1898 * now loop over the files reading in the data
1901 unsigned long long youngest;
1904 last_allowed_time = -1ULL;
1907 for (i = 0; i < ndevices; i++) {
1909 pdi->last_read_time = -1ULL;
1911 for (j = 0; j < pdi->nfiles; j++) {
1913 pci = get_cpu_info(pdi, j);
1918 pci->smallest_seq_read = -1;
1920 events = read_events(pci->fd, 1, &pci->fdblock);
1922 cpu_mark_offline(pdi, pci->cpu);
1928 if (pdi->last_read_time < last_allowed_time)
1929 last_allowed_time = pdi->last_read_time;
1931 events_added += events;
1935 if (sort_entries(&youngest))
1938 if (youngest > stopwatch_end)
1943 } while (events_added);
1945 if (rb_sort_entries)
1951 static int do_stdin(void)
1953 unsigned long long youngest;
1954 int fd, events, fdblock;
1956 last_allowed_time = -1ULL;
1957 fd = dup(STDIN_FILENO);
1959 perror("dup stdin");
1964 while ((events = read_events(fd, 0, &fdblock)) > 0) {
1968 smallest_seq_read = -1U;
1971 if (sort_entries(&youngest))
1974 if (youngest > stopwatch_end)
1980 if (rb_sort_entries)
1987 static void show_stats(void)
1996 if (per_process_stats)
1997 show_process_stats();
1999 if (per_device_and_cpu_stats)
2000 show_device_and_cpu_stats();
2005 static void handle_sigint(__attribute__((__unused__)) int sig)
2011 * Extract start and duration times from a string, allowing
2012 * us to specify a time interval of interest within a trace.
2013 * Format: "duration" (start is zero) or "start:duration".
2015 static int find_stopwatch_interval(char *string)
2020 value = strtod(string, &sp);
2022 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2026 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2028 value = strtod(string, &sp);
2029 if (sp == string || *sp != '\0') {
2030 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2034 } else if (*sp != '\0') {
2035 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2038 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2039 if (stopwatch_end <= stopwatch_start) {
2040 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2041 stopwatch_start, stopwatch_end);
2048 static char usage_str[] = \
2049 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
2050 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
2051 "\t-i Input file containing trace data, or '-' for stdin\n" \
2052 "\t-D Directory to prepend to input file names\n" \
2053 "\t-o Output file. If not given, output is stdout\n" \
2054 "\t-b stdin read batching\n" \
2055 "\t-s Show per-program io statistics\n" \
2056 "\t-h Hash processes by name, not pid\n" \
2057 "\t-t Track individual ios. Will tell you the time a request took\n" \
2058 "\t to get queued, to get dispatched, and to get completed\n" \
2059 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2060 "\t-w Only parse data between the given time interval in seconds.\n" \
2061 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
2062 "\t-f Output format. Customize the output format. The format field\n" \
2063 "\t identifies can be found in the documentation\n" \
2064 "\t-F Format specification. Can be found in the documentation\n" \
2065 "\t-v More verbose for marginal errors\n" \
2066 "\t-V Print program version info\n\n";
2068 static void usage(char *prog)
2070 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2073 int main(int argc, char *argv[])
2076 int i, c, ret, mode;
2077 int act_mask_tmp = 0;
2079 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2082 i = find_mask_map(optarg);
2084 fprintf(stderr,"Invalid action mask %s\n",
2092 if ((sscanf(optarg, "%x", &i) != 1) ||
2093 !valid_act_opt(i)) {
2095 "Invalid set action mask %s/0x%x\n",
2102 if (!strcmp(optarg, "-") && !pipeline)
2104 else if (resize_devices(optarg) != 0)
2111 output_name = optarg;
2114 rb_batch = atoi(optarg);
2116 rb_batch = RB_BATCH_DEFAULT;
2119 per_process_stats = 1;
2125 per_device_and_cpu_stats = 0;
2128 if (find_stopwatch_interval(optarg) != 0)
2132 set_all_format_specs(optarg);
2135 if (add_format_spec(optarg) != 0)
2139 ppi_hash_by_pid = 0;
2145 printf("%s version %s\n", argv[0], blkparse_version);
2153 while (optind < argc) {
2154 if (!strcmp(argv[optind], "-") && !pipeline)
2156 else if (resize_devices(argv[optind]) != 0)
2161 if (!pipeline && !ndevices) {
2166 if (act_mask_tmp != 0)
2167 act_mask = act_mask_tmp;
2169 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2171 signal(SIGINT, handle_sigint);
2172 signal(SIGHUP, handle_sigint);
2173 signal(SIGTERM, handle_sigint);
2175 setlocale(LC_NUMERIC, "en_US");
2178 ofp = fdopen(STDOUT_FILENO, "w");
2183 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2184 ofp = fopen(ofname, "w");
2193 ofp_buffer = malloc(4096);
2194 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {