-
Notifications
You must be signed in to change notification settings - Fork 5
/
blkparse.c
2900 lines (2417 loc) · 62.1 KB
/
blkparse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* block queue tracing parse application
*
* Copyright (C) 2005 Jens Axboe <[email protected]>
* Copyright (C) 2006 Jens Axboe <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <locale.h>
#include <libgen.h>
#include "blktrace.h"
#include "rbtree.h"
#include "jhash.h"
static char blkparse_version[] = "1.0.5";
struct skip_info {
unsigned long start, end;
struct skip_info *prev, *next;
};
struct per_dev_info {
dev_t dev;
char *name;
int backwards;
unsigned long long events;
unsigned long long first_reported_time;
unsigned long long last_reported_time;
unsigned long long last_read_time;
struct io_stats io_stats;
unsigned long skips;
unsigned long long seq_skips;
unsigned int max_depth[2];
unsigned int cur_depth[2];
struct rb_root rb_track;
int nfiles;
int ncpus;
unsigned long *cpu_map;
unsigned int cpu_map_max;
struct per_cpu_info *cpus;
};
/*
* some duplicated effort here, we can unify this hash and the ppi hash later
*/
struct process_pid_map {
pid_t pid;
char comm[16];
struct process_pid_map *hash_next, *list_next;
};
#define PPM_HASH_SHIFT (8)
#define PPM_HASH_SIZE (1 << PPM_HASH_SHIFT)
#define PPM_HASH_MASK (PPM_HASH_SIZE - 1)
static struct process_pid_map *ppm_hash_table[PPM_HASH_SIZE];
struct per_process_info {
struct process_pid_map *ppm;
struct io_stats io_stats;
struct per_process_info *hash_next, *list_next;
int more_than_one;
/*
* individual io stats
*/
unsigned long long longest_allocation_wait[2];
unsigned long long longest_dispatch_wait[2];
unsigned long long longest_completion_wait[2];
};
#define PPI_HASH_SHIFT (8)
#define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
#define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
static struct per_process_info *ppi_list;
static int ppi_list_entries;
static struct option l_opts[] = {
{
.name = "act-mask",
.has_arg = required_argument,
.flag = NULL,
.val = 'a'
},
{
.name = "set-mask",
.has_arg = required_argument,
.flag = NULL,
.val = 'A'
},
{
.name = "batch",
.has_arg = required_argument,
.flag = NULL,
.val = 'b'
},
{
.name = "input-directory",
.has_arg = required_argument,
.flag = NULL,
.val = 'D'
},
{
.name = "dump-binary",
.has_arg = required_argument,
.flag = NULL,
.val = 'd'
},
{
.name = "format",
.has_arg = required_argument,
.flag = NULL,
.val = 'f'
},
{
.name = "format-spec",
.has_arg = required_argument,
.flag = NULL,
.val = 'F'
},
{
.name = "hash-by-name",
.has_arg = no_argument,
.flag = NULL,
.val = 'h'
},
{
.name = "input",
.has_arg = required_argument,
.flag = NULL,
.val = 'i'
},
{
.name = "no-msgs",
.has_arg = no_argument,
.flag = NULL,
.val = 'M'
},
{
.name = "output",
.has_arg = required_argument,
.flag = NULL,
.val = 'o'
},
{
.name = "no-text-output",
.has_arg = no_argument,
.flag = NULL,
.val = 'O'
},
{
.name = "quiet",
.has_arg = no_argument,
.flag = NULL,
.val = 'q'
},
{
.name = "per-program-stats",
.has_arg = no_argument,
.flag = NULL,
.val = 's'
},
{
.name = "track-ios",
.has_arg = no_argument,
.flag = NULL,
.val = 't'
},
{
.name = "stopwatch",
.has_arg = required_argument,
.flag = NULL,
.val = 'w'
},
{
.name = "verbose",
.has_arg = no_argument,
.flag = NULL,
.val = 'v'
},
{
.name = "version",
.has_arg = no_argument,
.flag = NULL,
.val = 'V'
},
{
.name = NULL,
}
};
/*
* for sorting the displayed output
*/
struct trace {
struct blk_io_trace *bit;
struct rb_node rb_node;
struct trace *next;
unsigned long read_sequence;
};
static struct rb_root rb_sort_root;
static unsigned long rb_sort_entries;
static struct trace *trace_list;
/*
* allocation cache
*/
static struct blk_io_trace *bit_alloc_list;
static struct trace *t_alloc_list;
/*
* for tracking individual ios
*/
struct io_track {
struct rb_node rb_node;
struct process_pid_map *ppm;
__u64 sector;
unsigned long long allocation_time;
unsigned long long queue_time;
unsigned long long dispatch_time;
unsigned long long completion_time;
};
static int ndevices;
static struct per_dev_info *devices;
static char *get_dev_name(struct per_dev_info *, char *, int);
static int trace_rb_insert_last(struct per_dev_info *, struct trace *);
FILE *ofp = NULL;
static char *output_name;
static char *input_dir;
static unsigned long long genesis_time;
static unsigned long long last_allowed_time;
static unsigned long long stopwatch_start; /* start from zero by default */
static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
static unsigned long read_sequence;
static int per_process_stats;
static int per_device_and_cpu_stats = 1;
static int track_ios;
static int ppi_hash_by_pid = 1;
static int verbose;
static unsigned int act_mask = -1U;
static int stats_printed;
static int bin_output_msgs = 1;
int data_is_native = -1;
static FILE *dump_fp;
static char *dump_binary;
static unsigned int t_alloc_cache;
static unsigned int bit_alloc_cache;
#define RB_BATCH_DEFAULT (512)
static unsigned int rb_batch = RB_BATCH_DEFAULT;
static int pipeline;
static char *pipename;
static int text_output = 1;
#define is_done() (*(volatile int *)(&done))
static volatile int done;
struct timespec abs_start_time;
static unsigned long long start_timestamp;
static int have_drv_data = 0;
#define JHASH_RANDOM (0x3af5f2ee)
#define CPUS_PER_LONG (8 * sizeof(unsigned long))
#define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
#define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
static void output_binary(void *buf, int len)
{
if (dump_binary) {
size_t n = fwrite(buf, len, 1, dump_fp);
if (n != 1) {
perror(dump_binary);
fclose(dump_fp);
dump_binary = NULL;
}
}
}
static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
{
struct per_cpu_info *cpus = pdi->cpus;
int ncpus = pdi->ncpus;
int new_count = cpu + 1;
int new_space, size;
char *new_start;
size = new_count * sizeof(struct per_cpu_info);
cpus = realloc(cpus, size);
if (!cpus) {
char name[20];
fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
get_dev_name(pdi, name, sizeof(name)), size);
exit(1);
}
new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
memset(new_start, 0, new_space);
pdi->ncpus = new_count;
pdi->cpus = cpus;
for (new_count = 0; new_count < pdi->ncpus; new_count++) {
struct per_cpu_info *pci = &pdi->cpus[new_count];
if (!pci->fd) {
pci->fd = -1;
memset(&pci->rb_last, 0, sizeof(pci->rb_last));
pci->rb_last_entries = 0;
pci->last_sequence = -1;
}
}
}
static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
{
struct per_cpu_info *pci;
if (cpu >= pdi->ncpus)
resize_cpu_info(pdi, cpu);
pci = &pdi->cpus[cpu];
pci->cpu = cpu;
return pci;
}
static int resize_devices(char *name)
{
int size = (ndevices + 1) * sizeof(struct per_dev_info);
devices = realloc(devices, size);
if (!devices) {
fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
return 1;
}
memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
devices[ndevices].name = name;
ndevices++;
return 0;
}
static struct per_dev_info *get_dev_info(dev_t dev)
{
struct per_dev_info *pdi;
int i;
for (i = 0; i < ndevices; i++) {
if (!devices[i].dev)
devices[i].dev = dev;
if (devices[i].dev == dev)
return &devices[i];
}
if (resize_devices(NULL))
return NULL;
pdi = &devices[ndevices - 1];
pdi->dev = dev;
pdi->first_reported_time = 0;
pdi->last_read_time = 0;
return pdi;
}
static void insert_skip(struct per_cpu_info *pci, unsigned long start,
unsigned long end)
{
struct skip_info *sip;
for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
if (end == (sip->start - 1)) {
sip->start = start;
return;
} else if (start == (sip->end + 1)) {
sip->end = end;
return;
}
}
sip = malloc(sizeof(struct skip_info));
sip->start = start;
sip->end = end;
sip->prev = sip->next = NULL;
if (pci->skips_tail == NULL)
pci->skips_head = pci->skips_tail = sip;
else {
sip->prev = pci->skips_tail;
pci->skips_tail->next = sip;
pci->skips_tail = sip;
}
}
static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
{
if (sip->prev == NULL) {
if (sip->next == NULL)
pci->skips_head = pci->skips_tail = NULL;
else {
pci->skips_head = sip->next;
sip->next->prev = NULL;
}
} else if (sip->next == NULL) {
pci->skips_tail = sip->prev;
sip->prev->next = NULL;
} else {
sip->prev->next = sip->next;
sip->next->prev = sip->prev;
}
sip->prev = sip->next = NULL;
free(sip);
}
#define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
{
struct skip_info *sip;
for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
if (IN_SKIP(sip, seq)) {
if (sip->start == seq) {
if (sip->end == seq)
remove_sip(pci, sip);
else
sip->start += 1;
} else if (sip->end == seq)
sip->end -= 1;
else {
sip->end = seq - 1;
insert_skip(pci, seq + 1, sip->end);
}
return 1;
}
}
return 0;
}
static void collect_pdi_skips(struct per_dev_info *pdi)
{
struct skip_info *sip;
int cpu;
pdi->skips = 0;
pdi->seq_skips = 0;
for (cpu = 0; cpu < pdi->ncpus; cpu++) {
struct per_cpu_info *pci = &pdi->cpus[cpu];
for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
pdi->skips++;
pdi->seq_skips += (sip->end - sip->start + 1);
if (verbose)
fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
MAJOR(pdi->dev), MINOR(pdi->dev),
sip->start, sip->end);
}
}
}
static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
{
if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
unsigned long *map = malloc(new_max / sizeof(long));
memset(map, 0, new_max / sizeof(long));
if (pdi->cpu_map) {
memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
free(pdi->cpu_map);
}
pdi->cpu_map = map;
pdi->cpu_map_max = new_max;
}
pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
}
static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
{
pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
}
static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
{
return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
}
static inline int ppm_hash_pid(pid_t pid)
{
return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
}
static struct process_pid_map *find_ppm(pid_t pid)
{
const int hash_idx = ppm_hash_pid(pid);
struct process_pid_map *ppm;
ppm = ppm_hash_table[hash_idx];
while (ppm) {
if (ppm->pid == pid)
return ppm;
ppm = ppm->hash_next;
}
return NULL;
}
static struct process_pid_map *add_ppm_hash(pid_t pid, const char *name)
{
const int hash_idx = ppm_hash_pid(pid);
struct process_pid_map *ppm;
ppm = find_ppm(pid);
if (!ppm) {
ppm = malloc(sizeof(*ppm));
memset(ppm, 0, sizeof(*ppm));
ppm->pid = pid;
memset(ppm->comm, 0, sizeof(ppm->comm));
strncpy(ppm->comm, name, sizeof(ppm->comm));
ppm->comm[sizeof(ppm->comm) - 1] = '\0';
ppm->hash_next = ppm_hash_table[hash_idx];
ppm_hash_table[hash_idx] = ppm;
}
return ppm;
}
static void handle_notify(struct blk_io_trace *bit)
{
void *payload = (caddr_t) bit + sizeof(*bit);
__u32 two32[2];
switch (bit->action) {
case BLK_TN_PROCESS:
add_ppm_hash(bit->pid, payload);
break;
case BLK_TN_TIMESTAMP:
if (bit->pdu_len != sizeof(two32))
return;
memcpy(two32, payload, sizeof(two32));
if (!data_is_native) {
two32[0] = be32_to_cpu(two32[0]);
two32[1] = be32_to_cpu(two32[1]);
}
start_timestamp = bit->time;
abs_start_time.tv_sec = two32[0];
abs_start_time.tv_nsec = two32[1];
if (abs_start_time.tv_nsec < 0) {
abs_start_time.tv_sec--;
abs_start_time.tv_nsec += 1000000000;
}
break;
case BLK_TN_MESSAGE:
if (bit->pdu_len > 0) {
char msg[bit->pdu_len+1];
memcpy(msg, (char *)payload, bit->pdu_len);
msg[bit->pdu_len] = '\0';
fprintf(ofp,
"%3d,%-3d %2d %8s %5d.%09lu %5u %2s %3s %s\n",
MAJOR(bit->device), MINOR(bit->device),
bit->cpu, "0", (int) SECONDS(bit->time),
(unsigned long) NANO_SECONDS(bit->time),
0, "m", "N", msg);
}
break;
default:
/* Ignore unknown notify events */
;
}
}
char *find_process_name(pid_t pid)
{
struct process_pid_map *ppm = find_ppm(pid);
if (ppm)
return ppm->comm;
return NULL;
}
static inline int ppi_hash_pid(pid_t pid)
{
return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
}
static inline int ppi_hash_name(const char *name)
{
return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
}
static inline int ppi_hash(struct per_process_info *ppi)
{
struct process_pid_map *ppm = ppi->ppm;
if (ppi_hash_by_pid)
return ppi_hash_pid(ppm->pid);
return ppi_hash_name(ppm->comm);
}
static inline void add_ppi_to_hash(struct per_process_info *ppi)
{
const int hash_idx = ppi_hash(ppi);
ppi->hash_next = ppi_hash_table[hash_idx];
ppi_hash_table[hash_idx] = ppi;
}
static inline void add_ppi_to_list(struct per_process_info *ppi)
{
ppi->list_next = ppi_list;
ppi_list = ppi;
ppi_list_entries++;
}
static struct per_process_info *find_ppi_by_name(char *name)
{
const int hash_idx = ppi_hash_name(name);
struct per_process_info *ppi;
ppi = ppi_hash_table[hash_idx];
while (ppi) {
struct process_pid_map *ppm = ppi->ppm;
if (!strcmp(ppm->comm, name))
return ppi;
ppi = ppi->hash_next;
}
return NULL;
}
static struct per_process_info *find_ppi_by_pid(pid_t pid)
{
const int hash_idx = ppi_hash_pid(pid);
struct per_process_info *ppi;
ppi = ppi_hash_table[hash_idx];
while (ppi) {
struct process_pid_map *ppm = ppi->ppm;
if (ppm->pid == pid)
return ppi;
ppi = ppi->hash_next;
}
return NULL;
}
static struct per_process_info *find_ppi(pid_t pid)
{
struct per_process_info *ppi;
char *name;
if (ppi_hash_by_pid)
return find_ppi_by_pid(pid);
name = find_process_name(pid);
if (!name)
return NULL;
ppi = find_ppi_by_name(name);
if (ppi && ppi->ppm->pid != pid)
ppi->more_than_one = 1;
return ppi;
}
/*
* struct trace and blktrace allocation cache, we do potentially
* millions of mallocs for these structures while only using at most
* a few thousand at the time
*/
static inline void t_free(struct trace *t)
{
if (t_alloc_cache < 1024) {
t->next = t_alloc_list;
t_alloc_list = t;
t_alloc_cache++;
} else
free(t);
}
static inline struct trace *t_alloc(void)
{
struct trace *t = t_alloc_list;
if (t) {
t_alloc_list = t->next;
t_alloc_cache--;
return t;
}
return malloc(sizeof(*t));
}
static inline void bit_free(struct blk_io_trace *bit)
{
if (bit_alloc_cache < 1024 && !bit->pdu_len) {
/*
* abuse a 64-bit field for a next pointer for the free item
*/
bit->time = (__u64) (unsigned long) bit_alloc_list;
bit_alloc_list = (struct blk_io_trace *) bit;
bit_alloc_cache++;
} else
free(bit);
}
static inline struct blk_io_trace *bit_alloc(void)
{
struct blk_io_trace *bit = bit_alloc_list;
if (bit) {
bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
bit->time;
bit_alloc_cache--;
return bit;
}
return malloc(sizeof(*bit));
}
static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
{
struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
rb_erase(&t->rb_node, &pci->rb_last);
pci->rb_last_entries--;
bit_free(t->bit);
t_free(t);
}
static void put_trace(struct per_dev_info *pdi, struct trace *t)
{
rb_erase(&t->rb_node, &rb_sort_root);
rb_sort_entries--;
trace_rb_insert_last(pdi, t);
}
static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
{
struct rb_node **p = &root->rb_node;
struct rb_node *parent = NULL;
struct trace *__t;
while (*p) {
parent = *p;
__t = rb_entry(parent, struct trace, rb_node);
if (t->bit->time < __t->bit->time)
p = &(*p)->rb_left;
else if (t->bit->time > __t->bit->time)
p = &(*p)->rb_right;
else if (t->bit->device < __t->bit->device)
p = &(*p)->rb_left;
else if (t->bit->device > __t->bit->device)
p = &(*p)->rb_right;
else if (t->bit->sequence < __t->bit->sequence)
p = &(*p)->rb_left;
else /* >= sequence */
p = &(*p)->rb_right;
}
rb_link_node(&t->rb_node, parent, p);
rb_insert_color(&t->rb_node, root);
return 0;
}
static inline int trace_rb_insert_sort(struct trace *t)
{
if (!trace_rb_insert(t, &rb_sort_root)) {
rb_sort_entries++;
return 0;
}
return 1;
}
static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
{
struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
if (trace_rb_insert(t, &pci->rb_last))
return 1;
pci->rb_last_entries++;
if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
struct rb_node *n = rb_first(&pci->rb_last);
t = rb_entry(n, struct trace, rb_node);
__put_trace_last(pdi, t);
}
return 0;
}
static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
struct rb_root *root, int order)
{
struct rb_node *n = root->rb_node;
struct rb_node *prev = NULL;
struct trace *__t;
while (n) {
__t = rb_entry(n, struct trace, rb_node);
prev = n;
if (device < __t->bit->device)
n = n->rb_left;
else if (device > __t->bit->device)
n = n->rb_right;
else if (sequence < __t->bit->sequence)
n = n->rb_left;
else if (sequence > __t->bit->sequence)
n = n->rb_right;
else
return __t;
}
/*
* hack - the list may not be sequence ordered because some
* events don't have sequence and time matched. so we end up
* being a little off in the rb lookup here, because we don't
* know the time we are looking for. compensate by browsing
* a little ahead from the last entry to find the match
*/
if (order && prev) {
int max = 5;
while (((n = rb_next(prev)) != NULL) && max--) {
__t = rb_entry(n, struct trace, rb_node);
if (__t->bit->device == device &&
__t->bit->sequence == sequence)
return __t;
prev = n;
}
}
return NULL;
}
static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
struct per_cpu_info *pci,
unsigned long seq)
{
return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
}
static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
{
struct rb_node **p = &pdi->rb_track.rb_node;
struct rb_node *parent = NULL;
struct io_track *__iot;
while (*p) {
parent = *p;
__iot = rb_entry(parent, struct io_track, rb_node);
if (iot->sector < __iot->sector)
p = &(*p)->rb_left;
else if (iot->sector > __iot->sector)
p = &(*p)->rb_right;
else {
fprintf(stderr,
"sector alias (%Lu) on device %d,%d!\n",
(unsigned long long) iot->sector,
MAJOR(pdi->dev), MINOR(pdi->dev));
return 1;
}
}
rb_link_node(&iot->rb_node, parent, p);
rb_insert_color(&iot->rb_node, &pdi->rb_track);
return 0;
}
static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
{
struct rb_node *n = pdi->rb_track.rb_node;
struct io_track *__iot;
while (n) {
__iot = rb_entry(n, struct io_track, rb_node);
if (sector < __iot->sector)
n = n->rb_left;
else if (sector > __iot->sector)
n = n->rb_right;
else
return __iot;
}
return NULL;
}
static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
__u64 sector)
{
struct io_track *iot;
iot = __find_track(pdi, sector);
if (!iot) {
iot = malloc(sizeof(*iot));
iot->ppm = find_ppm(pid);
if (!iot->ppm)
iot->ppm = add_ppm_hash(pid, "unknown");
iot->sector = sector;
track_rb_insert(pdi, iot);
}
return iot;
}
static void log_track_frontmerge(struct per_dev_info *pdi,
struct blk_io_trace *t)
{
struct io_track *iot;
if (!track_ios)
return;
iot = __find_track(pdi, t->sector + t_sec(t));
if (!iot) {
if (verbose)
fprintf(stderr, "merge not found for (%d,%d): %llu\n",
MAJOR(pdi->dev), MINOR(pdi->dev),
(unsigned long long) t->sector + t_sec(t));
return;
}
rb_erase(&iot->rb_node, &pdi->rb_track);
iot->sector -= t_sec(t);
track_rb_insert(pdi, iot);
}
static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
{