This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathioprof.pl
executable file
·2211 lines (1961 loc) · 83.9 KB
/
ioprof.pl
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
#!/usr/bin/perl -w
# I/O Profiler for Linux
# Copyright (c) 2017, Intel Corporation.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
### I/O profing tool
my $VERSION = '2.0.5';
print "$0 ($VERSION)\n";
my $VERBOSE = 0;
my $DEBUG = 0;
my $FASTFILE = 1;
my $SINGLE_THREADED = 0;
my $READAHEAD = 1;
my $THREAD_POOL = 0;
use strict;
use POSIX ":sys_wait_h";
use Getopt::Std;
use threads;
use threads::shared;
use Thread::Semaphore;
use Thread::Queue;
$| = 1; # Force flush after every write/print
### Shared Variables: Locking requird when multi-threaded
my $io_total :shared; # Number of total I/O's
my $read_total :shared; # Number of buckets read (1 I/O can touch many buckets)
my $write_total :shared; # Number of buckets written (1 I/O can touch many buckets)
my @reads :shared; # Array of read hits by bucket ID
my @writes :shared; # Array of write hits by bucket ID
my %r_totals :shared; # Hash of read I/O's with I/O size as key
my %w_totals :shared; # Hash of write I/O's with I/O size as key
my $bucket_hits_total :shared; # Total number of bucket hits (not the total buckets)
my $total_blocks :shared; # Total number of LBA's accessed during profiling
my %files_to_lbas :shared; # Files and the lba ranges associated with them
my $max_bucket_hits :shared; # The hottest bucket
my @bucket_to_files :shared; # List of files that reside on each bucket
my $TERM :shared = 0; # Thread pool done with work
my $trace_files :shared = 0; # Map filesystem files to block LBAs
### Semaphores: These are the locks for the shared variables
my $read_semaphore; # Lock for the global read hit array
my $write_semaphore; # Lock for the global write hit array
my $read_totals_semaphore; # Lock for the global read totals
my $write_totals_semaphore; # Lock for the global write totals
my $total_semaphore; # Lock for the global I/O totals
my $total_blocks_semaphore; # Lock for the global total LBA's accessed
my $files_to_lbas_semaphore; # Lock for the global file->lba mapping hash
my $max_bucket_hits_semaphore; # Lock for the global maximum hits per bucket
my $bucket_to_files_semaphore; # Lock for the global bucket_to_files;
my $term_semaphore; # Lock for the global TERM;
my $trace_files_semaphore; # Lock for the global trace_files;
### Thread Queue
my $IDLE_QUEUE = Thread::Queue->new();
### Thread-local Variables: We use these to avoid locking constantly
my $thread_io_total=0; # Thread-local total I/O count (I/O ops)
my (%thread_r_totals, %thread_w_totals); # Thread-local I/O size counts (ops)
my $thread_bucket_hits_total=0; # Thread-local total bucket hits (buckets)
my $thread_read_total=0; # Thread-local total read count (I/O ops)
my $thread_write_total=0; # Thread-local total write count (I/O ops)
my %thread_reads; # Thread-local read count hash (buckets)
my %thread_writes; # Thread-local write count hash (buckets)
my $thread_total_blocks=0; # Thread-local total blocks accessed (lbas)
my $thread_max_bucket_hits=0; # Thread-local maximum bucket hits (bucket hits)
# Globals (Single-threaded, so no locking required)
my %file_hit_count; # Count of I/O's to each file
my @files_to_cleanup; # Files to delete after running this script
my $CMD; # Command to run
my $total_lbas; # Total logical blocks, regardless of sector size
my $tar_file; # .tar file outputted from 'trace' mode
my $fdisk_file; # File capture of fdisk tool output
my @pidlist; # Multi-process PID array, to keep track
my $top_files = ""; # Top files list
my $dev = ""; # Device (e.g. /dev/sda1)
my $dev_str = ""; # Device string (e.g. sda1 for /dev/sda1)
### Unit Scales
my $KiB = 1024;
my $MiB = 1048576;
my $GiB = 1073741824;
### Config Settings
my $bucket_size = 1 * $MiB; # Size of the bucket for totaling I/O counts (e.g. 1MB buckets)
my $num_buckets = 1; # Number of total buckets for this device
my $timeout = 3; # Seconds between each print
my $runtime = 0; # Runtime for 'live' and 'trace' modes
my $live_iterations = 0; # How many iterations for live mode. Each iteration is $timeout seconds long
my $sector_size = 512; # Sector size (usually obtained with fdisk)
my $live = 0; # Live mode 0=disabled, 1=enabled (command line arg)
my $percent = 0.020; # Histogram threshold for each level (e.g. 0.02% of total drive size)
my $total_capacity_GiB = 0; # Total drive capacity
my $mode = "unknown"; # Processing mode (live, trace, post)
my $pdf_report = 0; # Generate a PDF report instead of a text report
my $top_count_limit = 10; # How many files to list in Top Files list (e.g. Top 10 files)
my $thread_count = 0; # Thread count
my $cpu_affinity = 0; # Tie each thread to a CPU for load balancing
my $thread_max = 128; # Maximum thread count
my $buffer_size = 1024; # blktrace buffer size
my $buffer_count = 8; # blktrace buffer count
### Gnuplot Settings
my $xwidth = 800; # gnuplot x-width
my $yheight = 600; # gnuplot y-height
### Analysis strings
my $analysis_histogram_iops = "Histogram analysis coming soon.\n";
my $analysis_heatmap = "Heatmap analysis coming soon.\n";
my $analysis_stats_iops = "IOPS analysis coming soon.\n";
my $analysis_stats_bw = "Bandwidth analysis coming soon.\n";
### Heatmap Globals
my $SCALEX=5; # Scale heatmap to term width - $SCALEX chars
my $SCALEY=20; # Scale heatmap to term height - $SCALEY chars
my $min_x=5; # Minium terminal width in chars
my $min_y=5; # Minimum terminal height in chars
### ANSI COLORS
my $black = "\e[40m";
my $red = "\e[41m";
my $green = "\e[42m";
my $yellow = "\e[43m";
my $blue = "\e[44m";
my $magenta = "\e[45m";
my $cyan = "\e[46m";
my $white = "\e[47m";
my $none = "\e[0m";
### Heatmap Key
my @colors=($white,$blue,$cyan,$green,$yellow,$magenta,$red);
### Heatmap Globals
my $color_index; # Index in heatmap array
my $choices=scalar(@colors); # Number of color choices
my $vpc; # VPC=Values Per Choice. IOPS Range for each color
my $cap=0; # Maximum IOPS per heatmap block
my $rate; # How densely we pack buckets into heatmap blocks
my @block_map; # Heatmap to print
### File Mapping Globals
my $fibmap = 1; # FIBMAP ioctl number
my $extents; # EXT extents
my $mounttype; # Filesystem type (e.g. ext4)
my $mountpoint; # Mountpoint
### Print usage
sub usage
{
print "Invalid command\n\n";
print @ARGV;
print "Usage:\n";
print "$0 -m trace -d <dev> -r <runtime> [-v] [-f] # run trace for post-processing later\n";
print "$0 -m post -t <dev.tar file> [-v] [-p] # post-process mode\n";
print "$0 -m live -d <dev> -r <runtime> [-v] # live mode\n";
print "\nCommand Line Arguments:\n";
print "-d <dev> : The device to trace (e.g. /dev/sdb). You can run traces to multiple devices (e.g. /dev/sda and /dev/sdb)\n";
print " at the same time, but please only run 1 trace to a single device (e.g. /dev/sdb) at a time\n";
print "-r <runtime> : Runtime (seconds) for tracing\n";
print "-t <dev.tar file> : A .tar file is created during the 'trace' phase. Please use this file for the 'post' phase\n";
print " You can offload this file and run the 'post' phase on another system.\n";
print "-v : (OPTIONAL) Print verbose messages.\n";
print "-f : (OPTIONAL) Map all files on the device specified by -d <dev> during 'trace' phase to their LBA ranges.\n";
print " This is useful for determining the most fequently accessed files, but may take a while on really large filesystems\n";
print "-p : (OPTIONAL) Generate a .pdf output file in addition to STDOUT. This requires 'pdflatex', 'gnuplot' and 'terminal png'\n";
print " to be installed.\n";
exit;
} # sub usage
### Check arguments
sub check_args
{
my %opt;
getopts('m:d:t:fr:vpx', \%opt) or usage();
print "check args\n" if ($VERBOSE);
if(defined($opt{'v'}))
{
$VERBOSE=1; # Enable verbose messaging, may slow things down
print "VERBOSE enabled\n";
}
# Hidden DEBUG switch
if(defined($opt{'x'}))
{
$VERBOSE=1; # Enable verbose messaging, may slow things down
$DEBUG=1; # Enable debug messaging, will slow things down
print "VERBOSE and DEBUG enabled\n";
}
if(!$opt{'m'}) { usage(); }
$mode = $opt{'m'};
print "Mode: $mode\n" if ($VERBOSE);
if($mode eq 'live')
{
# Check for invalid args
if(!$opt{'d'} || !$opt{'r'}) { usage(); }
$dev = $opt{'d'};
$runtime = $opt{'r'};
print "Dev: $dev Runtime: $runtime\n" if ($DEBUG);
$live=1;
if ($dev =~ /\/dev\/(\S+)/) { $dev_str = $1; }
$dev_str =~ s/\//_/g;
print "str: $dev_str\n" if($DEBUG);
die("ERROR: dev_str=$dev_str invalid") if ($dev_str eq "");
# Check if $dev is not a block special file
die("ERROR: Cannot find $dev or $dev is not a block special file") if (! -b $dev);
}
elsif($mode eq 'post')
{
# Check Args
if (!$opt{'t'}) { usage(); }
$tar_file = $opt{'t'};
if($tar_file =~ /(\S+).tar/)
{
$dev_str = $1;
}
die("ERROR: dev_str=$dev_str invalid") if ($dev_str eq "");
if ($opt{'p'})
{
check_pdf_prereqs();
$pdf_report = 1;
}
$fdisk_file = "fdisk.$dev_str";
print "fdisk_file: $fdisk_file\n" if ($DEBUG);
print "Option -t $tar_file\n" if($DEBUG);
push(@files_to_cleanup, $fdisk_file);
die("ERROR: Cannot find $tar_file or $tar_file is not a plain file") if (! -f $tar_file);
}
elsif($mode eq 'trace')
{
# Check for invalid args
if(!$opt{'d'} || !$opt{'r'}) { usage(); }
if($opt{'f'}) { $trace_files = 1 ;}
print "Trace files = $trace_files\n" if($DEBUG);
$dev = $opt{'d'};
$runtime = $opt{'r'};
if ($runtime < 3)
{
$runtime = 3; # Min Runtime
}
print "Runtime: $runtime\n" if ($DEBUG);
if ($dev =~ /\/dev\/(\S+)/) { $dev_str = $1; }
$dev_str =~ s/\//_/g;
print "Option -d $dev\n" if($DEBUG);
print "str: $dev_str\n" if($DEBUG);
die("ERROR: dev_str=$dev_str invalid") if ($dev_str eq "");
# Check if $dev is not a block special file
die("ERROR: Cannot find $dev or $dev is not a block special file") if (! -b $dev);
}
else
{
usage();
}
return;
}
### Check prereqs for gnuplot and latex
sub check_pdf_prereqs
{
`which gnuplot`;
if ($? != 0) { die("ERROR: gnuplot not installed. Please offload the trace file for processing."); }
`which pdflatex`;
if ($? != 0) { die("ERROR: pdflatex not installed. Please offload the trace file for processing."); }
`echo 'set terminal png' > pngtest.txt; gnuplot pngtest.txt >/dev/null 2>&1`;
if ($? != 0) { `rm -f pngtest.txt`; die("ERROR: gnuplot PNG terminal not installed. Please offload the trace file for processing."); }
`rm -f pngtest.txt`;
}
### Check prereqs for blktrace
sub check_trace_prereqs
{
`which blktrace`;
if ($? != 0) { die("ERROR: blktrace not installed. Please install blktrace"); }
`which blkparse`;
if($? != 0) { die("ERROR: blkparse not installed. Please install blkparse"); }
}
### Check if debugfs is mounted
sub mount_debugfs
{
`mount | grep debugfs >/dev/null`;
if($?)
{
print "Need to mount debugfs\n" if ($VERBOSE);
`mount -t debugfs debugfs /sys/kernel/debug`;
if($? !=0 ) { die("ERROR: Failed to mount debugfs"); }
print "mounted debugfs successfully\n" if ($VERBOSE);
}
return;
}
sub lba_to_bucket
{
my $lba = shift;
return int($lba * $sector_size / $bucket_size);
}
sub bucket_to_lba
{
my $bucket = shift;
return int($bucket * $bucket_size / $sector_size);
}
# debufs method
# This method can only be used on ext2/ext3/ext4 filesystems
# I don't plan on using this method right now
# In testing the debugfs method, I found it to be approximately 30% slower than the ioctl method
sub debugfs_method
{
my $file = shift;
$extents = "";
$file =~ s/$mountpoint//;
print "file: $file\n" if ($DEBUG);
my @extent_out = `debugfs -R "dump_extents $file" $dev 2>/dev/null`;
# Pattern
#0/ 0 1/ 1 0 - 3701 7440384 - 7444085 3702
foreach my $line (@extent_out)
{
print $line if ($DEBUG);
if($line =~ /\s+\d+\/\s+\d+\s+\d+\/\s+\d+\s+\d+\s+-\s+\d+\s+(\d+)\s+-\s+(\d+)/)
{
$extents .= "$1:$2 ";
print "$extents\n" if ($DEBUG);
}
}
}
# Filesystem cluter (io_block_size) to LBA
sub fs_cluster_to_lba
{
my $fs_cluster_size = shift;
my $sector_size = shift;
my $io_cluster = shift;
return $io_cluster * ($fs_cluster_size / $sector_size);
}
# ioctl method
# This method "should" be usable regardless of filesystem
# There is some risk that FIBMAP!=1. Need to address this later
# I plan to use the ioctl method because it is 30% faster than the debugfs method
sub ioctl_method
{
my $file = shift;
my $FH;
my $output = "";
my $start=-1;
my $end=-1;
my $prev=-1;
my $cluster_id;
#my $contig = "Contig";
print "#$file#\n" if ($DEBUG);
#my $fs_cluster_size = `stat '$file' | grep "IO Block"| awk '{ print \$7 }'`;
#my $file_blocks = `stat '$file' | grep Blocks | awk '{ print \$4 }'`;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
my $fs_cluster_size = $blksize;
my $file_blocks = $blocks;
my $file_size = -s $file;
my $io_blocks = ($file_blocks * $sector_size) / $fs_cluster_size;
if (!defined($fs_cluster_size) || $fs_cluster_size == 0) { return; }
my $file_clusters = ($file_blocks * $sector_size) / $fs_cluster_size;
open($FH, "<", $file) or die("ERORR: Failed to open file $file");
my $log_id_a = 0;
my $log_id_b = int($file_clusters);
my $arg_a = pack "I", $log_id_a;
my $arg_b = pack "I", $log_id_b;
ioctl($FH, $fibmap, $arg_a) or die("ERROR: ioctl failed $!");
ioctl($FH, $fibmap, $arg_b) or die("ERROR: ioctl failed $!");
my $cluster_id_a = unpack "I", $arg_a;
my $cluster_id_b = unpack "I", $arg_b;
if($file_clusters == ($cluster_id_b - $cluster_id_a))
{
close($FH);
my $start_lba = fs_cluster_to_lba($fs_cluster_size, $sector_size, $cluster_id_a);
my $end_lba = fs_cluster_to_lba($fs_cluster_size, $sector_size, $cluster_id_b);
$output = "$start_lba:$end_lba";
return;
}
if ($READAHEAD)
{
my $log_id = 0;
my $skip = 1;
my $prev_log_id = -1;
while ($log_id <= int($file_clusters))
{
my $arg = pack "I", $log_id;
ioctl($FH, $fibmap, $arg) or die("ERROR: ioctl failed $!");
if($? != 0) { next; }
$cluster_id = unpack "I", $arg;
print "$file : log=$log_id prev=$prev_log_id : cid=$cluster_id previd=$prev: skip=$skip\n" if ($DEBUG);
if ($start == -1)
{
$start = fs_cluster_to_lba($fs_cluster_size, $sector_size, $cluster_id);
$output .= "$start:";
}
if ($cluster_id == ($prev + $skip))
{
print "Contig!\n" if ($DEBUG);
$prev_log_id = $log_id;
$prev = $cluster_id;
# Keep doubling readahead value (aka skip)
$skip = ($skip == (1<<16)) ? 32 : $skip << 1;
$log_id += $skip;
next;
}
elsif(($skip != 1) && ($prev != -1))
{
print "Not Contig, skip miss, skip=$skip\n" if ($DEBUG);
$log_id = $prev_log_id;
$skip = 1;
}
elsif($prev != -1)
{
my $prev_lba = fs_cluster_to_lba($fs_cluster_size, $sector_size, $prev);
my $curr_lba = fs_cluster_to_lba($fs_cluster_size, $sector_size, $cluster_id);
if($prev_lba != 0)
{
$output .= "$prev_lba ";
}
if($cluster_id != 0)
{
$output .= "$curr_lba:";
}
print "Not Contig, skip=$skip\n" if ($DEBUG);
#$contig = "Not Contig";
}
$prev = $cluster_id;
$log_id += $skip;
}
$end = fs_cluster_to_lba($fs_cluster_size, $sector_size, $cluster_id);
if ($end != 0)
{
$output .= "$end";
}
}
else
{
for (0 .. int($file_clusters))
{
my $log_id = $_;
my $arg = pack "I", $log_id;
ioctl($FH, $fibmap, $arg) or die("ERROR: ioctl failed $!");
if($? != 0) { next; }
$cluster_id = unpack "I", $arg;
print "$file : $log_id : $cluster_id\n" if ($DEBUG);
if ($start == -1)
{
$start = fs_cluster_to_lba($fs_cluster_size, $sector_size, $cluster_id);
$output .= "$start:";
}
if ($cluster_id == ($prev + 1))
{
print "Contig!\n" if ($DEBUG);
}
elsif($prev != -1)
{
my $prev_lba = fs_cluster_to_lba($fs_cluster_size, $sector_size, $prev);
my $curr_lba = fs_cluster_to_lba($fs_cluster_size, $sector_size, $cluster_id);
if($prev_lba != 0)
{
$output .= "$prev_lba ";
}
if($cluster_id != 0)
{
$output .= "$curr_lba:";
}
print "Not Contig\n" if ($DEBUG);
#$contig = "Not Contig";
}
$prev = $cluster_id;
}
$end = fs_cluster_to_lba($fs_cluster_size, $sector_size, $cluster_id);
if ($end != 0)
{
$output .= "$end";
}
}
close($FH);
print "$output\n" if ($DEBUG);
$extents = $output;
}
### Print filetrace files
sub printout
{
my $file = shift;
my $OUT;
print "printout: $file\n" if($DEBUG);
open ($OUT,">>", "filetrace.$dev_str.$cpu_affinity.txt") || die("ERROR: Failed to open filetrace.$dev_str.$cpu_affinity.txt");
print $OUT "$file :: $extents\n";
close($OUT);
}
### Find block ranges of each file
sub block_ranges
{
my $file = shift;
print "block_ranges: $file\n" if($DEBUG);
if ($file =~ /^\/proc/) { print "/proc file: $file\n" if($DEBUG); return; }
if ($file =~ /^\/sys/) { print "/sys file: $file\n" if ($DEBUG); return; }
if ((-l $file) || (!-f $file) || (-z $file)) { print "Disqualified file: $file\n" if ($DEBUG); return; }
if ($mounttype eq "ext4")
{
#debugfs_method($file);
ioctl_method($file);
}
elsif ($mounttype eq "ext3")
{
ioctl_method($file);
}
else
{
die("ERROR: $mounttype is not supported yet");
}
printout($file);
}
### Find all files on device
sub find_all_files
{
my @FILES;
`rm -f filetrace.*`;
my $cpu_count = `cat /proc/cpuinfo | grep processor | wc -l`;
$mountpoint = `mount | grep $dev | awk '{ print \$3 }'`;
if ($? != 0 )
{
print "$dev not mounted\n";
return;
}
chomp($mountpoint);
print "\nmountpoint: $mountpoint\n" if ($VERBOSE);
$mounttype = `mount | grep $dev | awk '{ print \$5 }'`;
chomp($mounttype);
print "mounttype: $mounttype\n" if ($VERBOSE);
if($FASTFILE)
{
my $FH;
`find $mountpoint -xdev -name "*" > ioprof_files.$dev_str.txt`;
open($FH, "<", "ioprof_files.$dev_str.txt") or die("ERROR: Failed to open file ioprof_files.$dev_str.txt");
while (<$FH>)
{
push(@FILES, $_);
}
close($FH);
}
else
{
@FILES = `find $mountpoint -mount -name "*"`;
}
my $file_count = scalar(@FILES);
print "filecount=$file_count\n" if($VERBOSE);
print @FILES if($DEBUG);
my $set_count = int($file_count / $cpu_count) + 1;
print "set_count=$set_count\n" if ($DEBUG);
if($SINGLE_THREADED)
{
my $k = 0;
for (0 .. $file_count)
{
if ($k > 100)
{
my $progress = $_ / $file_count * 100;
printf "\r%05.2f%% COMPLETE", $progress;
$k =0;
}
$k++;
my $file = $FILES[$_];
if (defined $file)
{
chomp($file);
print "$file\n" if($DEBUG);
block_ranges($file);
}
}
}
else
{
for(0 .. ($cpu_count-1))
{
$cpu_affinity = $_ % $cpu_count;
print "$cpu_affinity\n" if ($DEBUG);
my $pid = fork();
if($pid < 0)
{
# Failed to fork
die("ERROR: Failed to fork process\n");
}
elsif($pid == 0)
{
# Child process
my $start = 0 + ($set_count * $cpu_affinity);
if($start > $file_count) {exit; }
my $end = ($set_count * ($cpu_affinity + 1)) - 1;
$end = ($end >= $file_count) ? $file_count : $end;
print "$cpu_affinity: start=$start end=$end\n" if ($VERBOSE);
my $range = $end - $start;
#if ($range == 0) { exit; }
my $k = 0;
for ($start .. $end)
{
if ($range >0)
{
my $progress = ($_ - $start) / $range * 100;
if ($k > 100)
{
printf "\r%05.2f%% COMPLETE", $progress;
$k =0;
}
$k++;
}
my $file = $FILES[$_];
if (defined $file)
{
chomp($file);
print "$cpu_affinity: $file\n" if($DEBUG);
block_ranges($file);
}
}
exit;
}
else
{
# Parent process
print "pid=$pid CPU=$cpu_affinity\n" if ($DEBUG);
push(@pidlist, $pid);
my $hexcpu = sprintf("0x%08x", (1 << $cpu_affinity));
print "hexcpu=$hexcpu\n" if ($DEBUG);
`taskset -p $hexcpu $pid`;
print "running $cpu_affinity\n" if ($DEBUG);
}
}
print "\n\nWaiting on threads to finish\n\n";
foreach my $pid (@pidlist)
{
do
{
print("\rWaiting on $pid ");
} while (!waitpid($pid, 0));
}
print "\nDONE!\n";
print "Compressing files\n";
}
`gzip --fast filetrace.*`;
}
### Translate a bucket ID to a list of files
sub bucket_to_file_list
{
my $bucket_id = shift;
my @list;
if(defined($bucket_to_files[$bucket_id]))
{
@list = split(" ", $bucket_to_files[$bucket_id]);
}
if (scalar(@list) == 1 && $list[0] eq ' ')
{
undef(@list);
}
return @list;
}
### Translate a file to a list of buckets
sub file_to_buckets
{
my $k=0;
my $size = scalar(keys %files_to_lbas);
foreach my $file (keys %files_to_lbas)
{
$k++;
if($k % 100 == 0) { printf( "\rfile_to_buckets: %d %% (%d of %d)", ($k*100 / $size), $k, $size); }
$file_hit_count{$file}=0; # Initialize file hit count
foreach my $range (split(" ", $files_to_lbas{$file}))
{
my ($start, $finish) = split(':', $range);
print "$file start=$start, finish=$finish\n" if ($DEBUG);
next if ($start eq '' || $finish eq '');
my $start_bucket = lba_to_bucket($start);
my $finish_bucket = lba_to_bucket($finish);
print "$file s_lba=$start f_lba=$finish s_buc=$start_bucket f_buc=$finish_bucket\n" if ($DEBUG);
for(my $i=$start_bucket; $i<=$finish_bucket; $i++)
{
if (defined($bucket_to_files[$i]))
{
# Wrap $file in \Q \E to quote meta chars, trust me
if(!$bucket_to_files[$i] =~ /\Q$file\E/)
{
$bucket_to_files[$i] .= "$file ";
}
}
else
{
$bucket_to_files[$i] .= "$file ";
}
}
}
}
print "\rDone correlating files to buckets. Now time to count bucket hits.\n";
# Print all files
if($trace_files && $DEBUG)
{
print "--------------------------------------\n";
print "Buckets to file list:\n";
for(my $i=0; $i<$num_buckets; $i++)
{
my @list = bucket_to_file_list($i);
my $length = @list;
if ($length > 0)
{
print "$i($length): ";
print @list;
print "\n";
}
}
print "--------------------------------------\n";
}
return;
}
## Add up I/O hits to each file touched by a bucket
sub add_file_hits
{
my $bucket_id = shift;
my $io_count = shift;
my @list = bucket_to_file_list($bucket_id);
if(scalar(@list) == 0 && $io_count != 0) { print "No file hit. bucket=$bucket_id, io_cnt=$io_count\n" if ($DEBUG);}
foreach my $file (@list)
{
$file_hit_count{$file} += $io_count;
}
}
### Get logrithmic theta for Zipfian distribution
sub theta_log
{
my $base = shift;
my $value = shift;
print "base=$base, value=$value\n" if($DEBUG);
return log($value)/log($base);
}
### Print results
sub print_results
{
my $num = defined($1) ? $1 : 0;
my $sum;
my $k=0;
my ($DATA, $DATA2, $HEADER, $STATS);
my $buffer = "";
my $i;
my ($read_sum, $write_sum);
my %counts;
$read_sum = $write_sum = 0;
my ($row, $column);
$row=$column=0;
my $bw_total=0;
my @histogram_iops;
my @histogram_bw;
print "num_buckets=$num_buckets bucket_size=$bucket_size\n" if($VERBOSE);
print `date` if ($DEBUG);
if($pdf_report)
{
open($DATA, ">data.$dev_str.$num") || die ("ERROR: Could not open: data");
}
my $x=0;
my $threshold = $num_buckets / 100;
for($i=0; $i<$num_buckets; $i++)
{
$x++;
if ($x > $threshold)
{
printf( "\rBucket Percent: %d %%", ($i / $num_buckets * 100));
$x=0;
}
if ($i!=0 && ($i % $xwidth) == 0)
{
if($pdf_report)
{
print $DATA "$buffer\n";
}
$buffer="";
$row++;
$column=0;
}
my $r = defined($reads[$i]) ? $reads[$i] : 0;
my $w = defined($writes[$i]) ? $writes[$i] : 0;
my $bucket_total = $r + $w;
$bw_total += $bucket_total * $bucket_size;
print "$i: bt=$bucket_total\n" if ($DEBUG);
add_file_hits($i, $bucket_total) if ($trace_files);
$counts{$bucket_total}++;
$read_sum += $r;
$write_sum += $w;
$buffer .= sprintf("%d ", $bucket_total);
#push(@map,$bucket_total);
#$reads[$i]=0;
#$writes[$i]=0;
#undef($reads[$i]);
#undef($writes[$i]);
$column++;
}
print "\r \n";
while (($i % $xwidth) != 0) { $i++; $buffer .= "0 "; }
if($pdf_report)
{
print $DATA "$buffer\n";
close($DATA);
push(@files_to_cleanup, "data.$dev_str.$num");
}
print "num_buckets=$num_buckets pfgp iot=$io_total bucket_hits_total=$bucket_hits_total r_sum=$read_sum w_sum=$write_sum yheight=$yheight\n" if($VERBOSE);
print `date` if ($DEBUG);
my $t=0;
my $j=0;
my $section_count=0;
my $b_count=0;
my $GB_tot=0;
my $bw_tot=0;
my $bw_count=0;
my $io_sum=0;
my $tot=0;
if($pdf_report)
{
open($DATA, ">histogram_iops.$dev_str.$num") || die("ERROR: Could not open: histogram_iops.$dev_str.$num");
open($DATA2, ">histogram_bw.$dev_str.$num") || die("ERROR: Could not open: histogram_bw.$dev_str.$num");
}
# %counts is a hash
# each key "bucket_total" represents a particular I/O count for a bucket
# each value represents the number of buckets that had this I/O count
# This allows me to quickly tally up a histogram and is pretty
# space efficient since most buckets tend to have zero I/O that
# key tends to have the largest number of buckets
#
# Iterate through each key in decending order
my $max_set=0;
my $max=0;
my $theta_count=1;
my $theta_total=0;
my $min=1;
my $max_theta=0;
my $min_theta=999;
for my $total ( sort {$b<=>$a} keys %counts)
{
if ($total)
{
print "$total: $counts{$total}\n" if ($DEBUG);
$tot += $total * $counts{$total};
print "tot=$tot\n" if($DEBUG);
if (!$max_set)
{
$max_set = 1;
$max = $total;
}
else
{
$theta_count++;
$min = $total;
my $cur_theta = theta_log($theta_count, $max) - theta_log($theta_count, $total);
$max_theta = ($cur_theta > $max_theta) ? $cur_theta : $max_theta;
$min_theta = ($cur_theta < $min_theta) ? $cur_theta : $min_theta;
print "cur_theta: $cur_theta\n" if ($DEBUG);
$theta_total += $cur_theta;
}
for(my $i=0; $i< $counts{$total}; $i++)
{
$section_count += $total;
$b_count++;
$bw_count += $total * $bucket_size;
if(($b_count*$bucket_size/$GiB) > ($percent * $total_capacity_GiB))
{
print "b_count: $b_count\n" if ($DEBUG);
$bw_tot += $bw_count;
$GB_tot += ($b_count * $bucket_size);
$io_sum += $section_count;
my $GB = sprintf("%.1f", $GB_tot / $GiB);
my $io_perc = sprintf("%.1f", ($section_count / $bucket_hits_total)*100);
my $bw_perc = sprintf("%.1f", ($bw_count / $bw_total)*100);
my $io_sum_perc = sprintf("%.1f", ($io_sum/($bucket_hits_total))*100);
if($pdf_report)
{
print $DATA "\"$GB GB\" $io_perc $section_count $io_sum_perc $io_sum\n";
print $DATA2 "\"$GB GB\" $bw_perc $bw_count\n";
}
push(@histogram_iops, "$GB GB $io_perc% ($io_sum_perc% cumulative)");
push(@histogram_bw, "$GB GB $bw_perc%");
$b_count=0;
$section_count=0;
$bw_count=0;
}
}
}
}
if ($b_count)
{
print "b_count: $b_count\n" if ($DEBUG);
$bw_tot += $bw_count;
$GB_tot += ($b_count * $bucket_size);
$io_sum += $section_count;
my $GB = sprintf("%.1f", $GB_tot / $GiB);
my $io_perc = sprintf("%.1f", ($section_count / $bucket_hits_total)*100);
my $bw_perc = sprintf("%.1f", ($bw_count / $bw_total)*100);
my $io_sum_perc = sprintf("%.1f", ($io_sum/($bucket_hits_total))*100);
if($pdf_report)
{
print $DATA "\"$GB GB\" $io_perc $section_count $io_sum_perc $io_sum\n";
print $DATA2 "\"$GB GB\" $bw_perc $bw_tot\n";
}
push(@histogram_iops, "$GB GB $io_perc% ($io_sum_perc% cumulative)");
push(@histogram_bw, "$GB GB $bw_perc%");
$b_count=0;