File: display-profiler-output

package info (click to toggle)
qtwebkit-opensource-src 5.7.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 291,692 kB
  • ctags: 268,122
  • sloc: cpp: 1,360,420; python: 70,286; ansic: 42,986; perl: 35,476; ruby: 12,236; objc: 9,465; xml: 8,396; asm: 3,873; yacc: 2,397; sh: 1,647; makefile: 650; lex: 644; java: 110
file content (938 lines) | stat: -rwxr-xr-x 29,870 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env ruby

# Copyright (C) 2012, 2013 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.

require 'rubygems'

require 'readline'

begin
    require 'json'
    require 'highline'
rescue LoadError
    $stderr.puts "Error: some required gems are not installed!"
    $stderr.puts
    $stderr.puts "Try running:"
    $stderr.puts
    $stderr.puts "sudo gem install json"
    $stderr.puts "sudo gem install highline"
    exit 1
end

class Bytecode
    attr_accessor :bytecodes, :bytecodeIndex, :opcode, :description, :topCounts, :bottomCounts, :machineInlinees, :osrExits
    
    def initialize(bytecodes, bytecodeIndex, opcode, description)
        @bytecodes = bytecodes
        @bytecodeIndex = bytecodeIndex
        @opcode = opcode
        @description = description
        @topCounts = [] # "source" counts
        @bottomCounts = {} # "machine" counts, maps compilations to counts
        @machineInlinees = {} # maps my compilation to a set of inlinees
        @osrExits = []
    end
    
    def shouldHaveCounts?
        @opcode != "op_call_put_result"
    end
    
    def addTopCount(count)
        @topCounts << count
    end
    
    def addBottomCountForCompilation(count, compilation)
        @bottomCounts[compilation] = [] unless @bottomCounts[compilation]
        @bottomCounts[compilation] << count
    end
    
    def addMachineInlinee(compilation, inlinee)
        @machineInlinees[compilation] = {} unless @machineInlinees[compilation]
        @machineInlinees[compilation][inlinee] = true
    end
    
    def totalTopExecutionCount
        sum = 0
        @topCounts.each {
            | value |
            sum += value.count
        }
        sum
    end
    
    def topExecutionCount(engine)
        sum = 0
        @topCounts.each {
            | value |
            if value.engine == engine
                sum += value.count
            end
        }
        sum
    end
    
    def totalBottomExecutionCount
        sum = 0
        @bottomCounts.each_value {
            | counts |
            max = 0
            counts.each {
                | value |
                max = [max, value.count].max
            }
            sum += max
        }
        sum
    end
    
    def bottomExecutionCount(engine)
        sum = 0
        @bottomCounts.each_pair {
            | compilation, counts |
            if compilation.engine == engine
                max = 0
                counts.each {
                    | value |
                    max = [max, value.count].max
                }
                sum += max
            end
        }
        sum
    end
    
    def totalExitCount
        sum = 0
        @osrExits.each {
            | exit |
            sum += exit.count
        }
        sum
    end
end

class Bytecodes
    attr_accessor :codeHash, :inferredName, :source, :instructionCount, :machineInlineSites, :compilations
    
    def initialize(json)
        @codeHash = json["hash"].to_s
        @inferredName = json["inferredName"].to_s
        @source = json["sourceCode"].to_s
        @instructionCount = json["instructionCount"].to_i
        @bytecode = {}
        json["bytecode"].each {
            | subJson |
            index = subJson["bytecodeIndex"].to_i
            @bytecode[index] = Bytecode.new(self, index, subJson["opcode"].to_s, subJson["description"].to_s)
        }
        @machineInlineSites = {} # maps compilation to a set of origins
        @compilations = []
    end
    
    def name(limit)
        if to_s.size > limit
            "\##{@codeHash}"
        else
            to_s
        end
    end
    
    def to_s
        "#{@inferredName}\##{@codeHash}"
    end
    
    def matches(pattern)
        if pattern =~ /^#/
            $~.post_match == @codeHash
        elsif pattern =~ /#/
            pattern == to_s
        else
            pattern == @inferredName or pattern == @codeHash
        end
    end
    
    def each
        @bytecode.values.sort{|a, b| a.bytecodeIndex <=> b.bytecodeIndex}.each {
            | value |
            yield value
        }
    end
    
    def bytecode(bytecodeIndex)
        @bytecode[bytecodeIndex]
    end
    
    def addMachineInlineSite(compilation, origin)
        @machineInlineSites[compilation] = {} unless @machineInlineSites[compilation]
        @machineInlineSites[compilation][origin] = true
    end
    
    def totalMachineInlineSites
        sum = 0
        @machineInlineSites.each_value {
            | set |
            sum += set.size
        }
        sum
    end
    
    def sourceMachineInlineSites
        set = {}
        @machineInlineSites.each_value {
            | mySet |
            set.merge!(mySet)
        }
        set.size
    end
    
    def totalMaxTopExecutionCount
        max = 0
        @bytecode.each_value {
            | bytecode |
            max = [max, bytecode.totalTopExecutionCount].max
        }
        max
    end
    
    def maxTopExecutionCount(engine)
        max = 0
        @bytecode.each_value {
            | bytecode |
            max = [max, bytecode.topExecutionCount(engine)].max
        }
        max
    end
    
    def totalMaxBottomExecutionCount
        max = 0
        @bytecode.each_value {
            | bytecode |
            max = [max, bytecode.totalBottomExecutionCount].max
        }
        max
    end
    
    def maxBottomExecutionCount(engine)
        max = 0
        @bytecode.each_value {
            | bytecode |
            max = [max, bytecode.bottomExecutionCount(engine)].max
        }
        max
    end
    
    def totalExitCount
        sum = 0
        each {
            | bytecode |
            sum += bytecode.totalExitCount
        }
        sum
    end
end

class ProfiledBytecode
    attr_reader :bytecodeIndex, :description
    
    def initialize(json)
        @bytecodeIndex = json["bytecodeIndex"].to_i
        @description = json["description"].to_s
    end
end

class ProfiledBytecodes
    attr_reader :header, :bytecodes
    
    def initialize(json)
        @header = json["header"]
        @bytecodes = $bytecodes[json["bytecodesID"].to_i]
        @sequence = json["bytecode"].map {
            | subJson |
            ProfiledBytecode.new(subJson)
        }
    end
    
    def each
        @sequence.each {
            | description |
            yield description
        }
    end
end

def originStackFromJSON(json)
    json.map {
        | subJson |
        $bytecodes[subJson["bytecodesID"].to_i].bytecode(subJson["bytecodeIndex"].to_i)
    }
end

class CompiledBytecode
    attr_accessor :origin, :description
    
    def initialize(json)
        @origin = originStackFromJSON(json["origin"])
        @description = json["description"].to_s
    end
end

class ExecutionCounter
    attr_accessor :origin, :engine, :count
    
    def initialize(origin, engine, count)
        @origin = origin
        @engine = engine
        @count = count
    end
end

class OSRExit
    attr_reader :compilation, :origin, :codeAddresses, :exitKind, :isWatchpoint, :count
    
    def initialize(compilation, origin, codeAddresses, exitKind, isWatchpoint, count)
        @compilation = compilation
        @origin = origin
        @codeAddresses = codeAddresses
        @exitKind = exitKind
        @isWatchpoint = isWatchpoint
        @count = count
    end
    
    def dumpForDisplay(prefix)
        puts(prefix + "EXIT: due to #{@exitKind}, #{@count} times")
    end
end

class Compilation
    attr_accessor :bytecode, :engine, :descriptions, :counters, :compilationIndex
    attr_accessor :osrExits, :profiledBytecodes, :numInlinedGetByIds, :numInlinedPutByIds
    attr_accessor :numInlinedCalls
    
    def initialize(json)
        @bytecode = $bytecodes[json["bytecodesID"].to_i]
        @bytecode.compilations << self
        @compilationIndex = @bytecode.compilations.size
        @engine = json["compilationKind"]
        @descriptions = json["descriptions"].map {
            | subJson |
            CompiledBytecode.new(subJson)
        }
        @descriptions.each {
            | description |
            next if description.origin.empty?
            description.origin[1..-1].each_with_index {
                | inlinee, index |
                description.origin[0].addMachineInlinee(self, inlinee.bytecodes)
                inlinee.bytecodes.addMachineInlineSite(self, description.origin[0...index])
            }
        }
        @counters = {}
        json["counters"].each {
            | subJson |
            origin = originStackFromJSON(subJson["origin"])
            counter = ExecutionCounter.new(origin, @engine, subJson["executionCount"].to_i)
            @counters[origin] = counter
            origin[-1].addTopCount(counter)
            origin[0].addBottomCountForCompilation(counter, self)
        }
        @osrExits = {}
        json["osrExits"].each {
            | subJson |
            osrExit = OSRExit.new(self, originStackFromJSON(subJson["origin"]),
                                  json["osrExitSites"][subJson["id"]].map {
                                      | value |
                                      value.hex
                                  }, subJson["exitKind"], subJson["isWatchpoint"],
                                  subJson["count"])
            osrExit.codeAddresses.each {
                | codeAddress |
                osrExits[codeAddress] = [] unless osrExits[codeAddress]
                osrExits[codeAddress] << osrExit
            }
            osrExit.origin[-1].osrExits << osrExit
        }
        @profiledBytecodes = []
        json["profiledBytecodes"].each {
            | subJson |
            @profiledBytecodes << ProfiledBytecodes.new(subJson)
        }
        @numInlinedGetByIds = json["numInlinedGetByIds"]
        @numInlinedPutByIds = json["numInlinedPutByIds"]
        @numInlinedCalls = json["numInlinedCalls"]
    end
    
    def counter(origin)
        @counters[origin]
    end
    
    def to_s
        "#{bytecode}-#{compilationIndex}-#{engine}"
    end
end

class DescriptionLine
    attr_reader :actualCountsString, :sourceCountsString, :disassembly, :shouldShow
    
    def initialize(actualCountsString, sourceCountsString, disassembly, shouldShow)
        @actualCountsString = actualCountsString
        @sourceCountsString = sourceCountsString
        @disassembly = disassembly
        @shouldShow = shouldShow
    end
    
    def codeAddress
        if @disassembly =~ /^\s*(0x[0-9a-fA-F]+):/
            $1.hex
        else
            nil
        end
    end
end
            
if ARGV.length != 1
    $stderr.puts "Usage: display-profiler-output <path to profiler output file>"
    $stderr.puts
    $stderr.puts "The typical usage pattern for the profiler currently looks something like:"
    $stderr.puts
    $stderr.puts "Path/To/jsc -p profile.json myprogram.js"
    $stderr.puts "display-profiler-output profile.json"
    exit 1
end

$json = JSON::parse(IO::read(ARGV[0]))
$bytecodes = $json["bytecodes"].map {
    | subJson |
    Bytecodes.new(subJson)
}
$compilations = $json["compilations"].map {
    | subJson |
    Compilation.new(subJson)
}
$engines = ["Baseline", "DFG"]

def lpad(str,chars)
  if str.length>chars
    str
  else
    "%#{chars}s"%(str)
  end
end

def rpad(str, chars)
    while str.length < chars
        str += " "
    end
    str
end

def center(str, chars)
    while str.length < chars
        str += " "
        if str.length < chars
            str = " " + str
        end
    end
    str
end

def mayBeHash(hash)
    hash =~ /#/ or hash.size == 6
end

def sourceOnOneLine(source, limit)
    source.gsub(/\s+/, ' ')[0...limit]
end

def screenWidth
    if $stdin.tty?
        HighLine::SystemExtensions.terminal_size[0]
    else
        200
    end
end

def summary(mode)
    remaining = screenWidth
    
    # Figure out how many columns we need for the code block names, and for counts
    maxCount = 0
    maxName = 0
    $bytecodes.each {
        | bytecodes |
        maxCount = ([maxCount] + $engines.map {
                        | engine |
                        bytecodes.maxTopExecutionCount(engine)
                    } + $engines.map {
                        | engine |
                        bytecodes.maxBottomExecutionCount(engine)
                    }).max
        maxName = [bytecodes.to_s.size, maxName].max
    }
    maxCountDigits = maxCount.to_s.size
    
    hashCols = [[maxName, 30].min, "CodeBlock".size].max
    remaining -= hashCols + 1
    
    countCols = [maxCountDigits * $engines.size, "Source Counts".size].max
    remaining -= countCols + 1
    
    if mode == :full
        instructionCountCols = 6
        remaining -= instructionCountCols + 1
        
        machineCountCols = [maxCountDigits * $engines.size, "Machine Counts".size].max
        remaining -= machineCountCols + 1
        
        compilationsCols = 7
        remaining -= compilationsCols + 1
        
        inlinesCols = 9
        remaining -= inlinesCols + 1
        
        exitCountCols = 7
        remaining -= exitCountCols + 1
        
        recentOptsCols = 12
        remaining -= recentOptsCols + 1
    end
    
    if remaining > 0
        sourceCols = remaining
    else
        sourceCols = nil
    end
    
    print(center("CodeBlock", hashCols))
    if mode == :full
        print(" " + center("#Instr", instructionCountCols))
    end
    print(" " + center("Source Counts", countCols))
    if mode == :full
        print(" " + center("Machine Counts", machineCountCols))
        print(" " + center("#Compil", compilationsCols))
        print(" " + center("Inlines", inlinesCols))
        print(" " + center("#Exits", exitCountCols))
        print(" " + center("Last Opts", recentOptsCols))
    end
    if sourceCols
        print(" " + center("Source", sourceCols))
    end
    puts
    
    print(center("", hashCols))
    if mode == :full
        print(" " + (" " * instructionCountCols))
    end
    print(" " + center("Base/DFG", countCols))
    if mode == :full
        print(" " + center("Base/DFG", machineCountCols))
        print(" " + (" " * compilationsCols))
        print(" " + center("Src/Total", inlinesCols))
        print(" " + (" " * exitCountCols))
        print(" " + center("Get/Put/Call", recentOptsCols))
    end
    puts
    $bytecodes.sort {
        | a, b |
        b.totalMaxTopExecutionCount <=> a.totalMaxTopExecutionCount
    }.each {
        | bytecode |
        print(center(bytecode.name(hashCols), hashCols))
        if mode == :full
            print(" " + center(bytecode.instructionCount.to_s, instructionCountCols))
        end
        print(" " +
              center($engines.map {
                         | engine |
                         bytecode.maxTopExecutionCount(engine).to_s
                     }.join("/"), countCols))
        if mode == :full
            print(" " + center($engines.map {
                                   | engine |
                                   bytecode.maxBottomExecutionCount(engine).to_s
                               }.join("/"), machineCountCols))
            print(" " + center(bytecode.compilations.size.to_s, compilationsCols))
            print(" " + center(bytecode.sourceMachineInlineSites.to_s + "/" + bytecode.totalMachineInlineSites.to_s, inlinesCols))
            print(" " + center(bytecode.totalExitCount.to_s, exitCountCols))
            lastCompilation = bytecode.compilations[-1]
            if lastCompilation
                optData = [lastCompilation.numInlinedGetByIds,
                           lastCompilation.numInlinedPutByIds,
                           lastCompilation.numInlinedCalls]
            else
                optData = ["N/A"]
            end
            print(" " + center(optData.join('/'), recentOptsCols))
        end
        if sourceCols
            print(" " + sourceOnOneLine(bytecode.source, sourceCols))
        end
        puts
    }
end

def executeCommand(*commandArray)
    command = commandArray[0]
    args = commandArray[1..-1]
    case command
    when "help", "h", "?"
        puts "summary (s)     Print a summary of code block execution rates."
        puts "full (f)        Same as summary, but prints more information."
        puts "source          Show the source for a code block."
        puts "bytecode (b)    Show the bytecode for a code block, with counts."
        puts "profiling (p)   Show the (internal) profiling data for a code block."
        puts "display (d)     Display details for a code block."
        puts "inlines         Show all inlining stacks that the code block was on."
        puts "help (h)        Print this message."
        puts "quit (q)        Quit."
    when "quit", "q", "exit"
        exit 0
    when "summary", "s"
        summary(:summary)
    when "full", "f"
        summary(:full)
    when "source"
        if args.length != 1
            puts "Usage: source <code block hash>"
            return
        end
        $bytecodes.each {
            | bytecode |
            if bytecode.matches(args[0])
                puts bytecode.source
            end
        }
    when "bytecode", "b"
        if args.length != 1
            puts "Usage: source <code block hash>"
            return
        end
        
        hash = args[0]
        
        countCols = 10 * $engines.size
        machineCols = 10 * $engines.size
        pad = 1
        while (countCols + 1 + machineCols + pad) % 8 != 0
            pad += 1
        end
        
        $bytecodes.each {
            | bytecodes |
            next unless bytecodes.matches(hash)
            puts(center("Source Counts", countCols) + " " + center("Machine Counts", machineCols) +
                 (" " * pad) + center("Bytecode for #{bytecodes}", screenWidth - pad - countCols - 1 - machineCols))
            puts(center("Base/DFG", countCols) + " " + center("Base/DFG", countCols))
            bytecodes.each {
                | bytecode |
                if bytecode.shouldHaveCounts?
                    countsString = $engines.map {
                        | myEngine |
                        bytecode.topExecutionCount(myEngine)
                    }.join("/")
                    machineString = $engines.map {
                        | myEngine |
                        bytecode.bottomExecutionCount(myEngine)
                    }.join("/")
                else
                    countsString = ""
                    machineString = ""
                end
                puts(center(countsString, countCols) + " " + center(machineString, machineCols) + (" " * pad) + bytecode.description.chomp)
                bytecode.osrExits.each {
                    | exit |
                    puts(center("!!!!!", countCols) + " " + center("!!!!!", machineCols) + (" " * (pad + 10)) +
                         "EXIT: in #{exit.compilation} due to #{exit.exitKind}, #{exit.count} times")
                }
            }
        }
    when "profiling", "p"
        if args.length != 1
            puts "Usage: profiling <code block hash>"
            return
        end
        
        hash = args[0]
        
        first = true
        $compilations.each {
            | compilation |
            
            compilation.profiledBytecodes.each {
                | profiledBytecodes |
                if profiledBytecodes.bytecodes.matches(hash)
                    if first
                        first = false
                    else
                        puts
                    end
                    
                    puts "Compilation #{compilation}:"
                    profiledBytecodes.header.each {
                        | header |
                        puts(" " * 6 + header)
                    }
                    profiledBytecodes.each {
                        | bytecode |
                        puts(" " * 8 + bytecode.description)
                        profiledBytecodes.bytecodes.bytecode(bytecode.bytecodeIndex).osrExits.each {
                            | exit |
                            if exit.compilation == compilation
                                puts(" !!!!!           EXIT: due to #{exit.exitKind}, #{exit.count} times")
                            end
                        }
                    }
                end
            }
        }
    when "inlines"
        if args.length != 1
            puts "Usage: inlines <code block hash>"
            return
        end
        
        hash = args[0]
        
        $bytecodes.each {
            | bytecodes |
            next unless bytecodes.matches(hash)
            
            # FIXME: print something useful to say more about which code block this is.
            
            $compilations.each {
                | compilation |
                myOrigins = []
                compilation.descriptions.each {
                    | description |
                    if description.origin.index {
                            | myBytecode |
                            bytecodes == myBytecode.bytecodes
                        }
                        myOrigins << description.origin
                    end
                }
                myOrigins.uniq!
                myOrigins.sort! {
                    | a, b |
                    result = 0
                    [a.size, b.size].min.times {
                        | index |
                        result = a[index].bytecodeIndex <=> b[index].bytecodeIndex
                        break if result != 0
                    }
                    result
                }
                
                next if myOrigins.empty?

                printArray = []
                lastPrintStack = []
                
                def originToPrintStack(origin)
                    (0...(origin.size - 1)).map {
                        | index |
                        "bc\##{origin[index].bytecodeIndex} --> #{origin[index + 1].bytecodes}"
                    }
                end
                
                def printStack(printArray, stack, lastStack)
                    stillCommon = true
                    stack.each_with_index {
                        | entry, index |
                        next if stillCommon and entry == lastStack[index]
                        printArray << ("    " * (index + 1) + entry)
                        stillCommon = false
                    }
                end
                
                myOrigins.each {
                    | origin |
                    currentPrintStack = originToPrintStack(origin)
                    printStack(printArray, currentPrintStack, lastPrintStack)
                    lastPrintStack = currentPrintStack
                }

                next if printArray.empty?
                
                puts "Compilation #{compilation}:"
                printArray.each {
                    | entry |
                    puts entry
                }
            }
        }
    when "display", "d"
        compilationIndex = nil
        
        case args.length
        when 1
            if args[0] == "*"
                hash = nil
            else
                hash = args[0]
            end
            engine = nil
        when 2
            if mayBeHash(args[0])
                hash = args[0]
                engine = args[1]
            else
                engine = args[0]
                hash = args[1]
            end
        else
            puts "Usage: summary <code block hash> <engine>"
            return
        end
        
        if hash and hash =~ /-([0-9]+)-/
            hash = $~.pre_match
            engine = $~.post_match
            compilationIndex = $1.to_i
        end
        
        if engine and not $engines.index(engine)
            pattern = Regexp.new(Regexp.escape(engine), "i")
            trueEngine = nil
            $engines.each {
                | myEngine |
                if myEngine =~ pattern
                    trueEngine = myEngine
                    break
                end
            }
            unless trueEngine
                puts "#{engine} is not a valid engine, try #{$engines.join(' or ')}."
                return
            end
            engine = trueEngine
        end
        
        actualCountCols = 13
        sourceCountCols = 10 * $engines.size
        
        first = true
        $compilations.each {
            | compilation |
            next if hash and not compilation.bytecode.matches(hash)
            next if engine and compilation.engine != engine
            next if compilationIndex and compilation.compilationIndex != compilationIndex
            
            if first
                first = false
            else
                puts
            end
            
            puts("Compilation #{compilation}:")
            puts("    Num inlined: GetByIds: #{compilation.numInlinedGetByIds}  PutByIds: #{compilation.numInlinedPutByIds}  Calls: #{compilation.numInlinedCalls}")
            puts(center("Actual Counts", actualCountCols) + " " + center("Source Counts", sourceCountCols) + " " + center("Disassembly in #{compilation.engine}", screenWidth - 1 - sourceCountCols - 1 - actualCountCols))
            puts((" " * actualCountCols) + " " + center("Base/DFG", sourceCountCols))

            lines = []

            compilation.descriptions.each {
                | description |
                # FIXME: We should have a better way of detecting things like CountExecution nodes
                # and slow path entries in the baseline JIT.
                if description.description =~ /CountExecution\(/ and compilation.engine == "DFG"
                    shouldShow = false
                else
                    shouldShow = true
                end
                if description.origin.empty? or not description.origin[-1].shouldHaveCounts? or (compilation.engine == "Baseline" and description.description =~ /^\s*\(S\)/)
                    actualCountsString = ""
                    sourceCountsString = ""
                else
                    actualCountsString = compilation.counter(description.origin).count.to_s
                    sourceCountsString = $engines.map {
                        | myEngine |
                        description.origin[-1].topExecutionCount(myEngine)
                    }.join("/")
                end
                description.description.split("\n").each {
                    | line |
                    lines << DescriptionLine.new(actualCountsString, sourceCountsString, line.chomp, shouldShow)
                }
            }
            
            exitPrefix = center("!!!!!", actualCountCols) + " " + center("!!!!!", sourceCountCols) + (" " * 25)
            
            lines.each_with_index {
                | line, index |
                codeAddress = line.codeAddress
                if codeAddress
                    list = compilation.osrExits[codeAddress]
                    if list
                        list.each {
                            | exit |
                            if exit.isWatchpoint
                                exit.dumpForDisplay(exitPrefix)
                            end
                        }
                    end
                end
                if line.shouldShow
                    puts(center(line.actualCountsString, actualCountCols) + " " + center(line.sourceCountsString, sourceCountCols) + " " + line.disassembly)
                end
                if codeAddress
                    # Find the next disassembly address.
                    endIndex = index + 1
                    endAddress = nil
                    while endIndex < lines.size
                        myAddress = lines[endIndex].codeAddress
                        if myAddress
                            endAddress = myAddress
                            break
                        end
                        endIndex += 1
                    end
                    
                    if endAddress
                        list = compilation.osrExits[endAddress]
                        if list
                            list.each {
                                | exit |
                                unless exit.isWatchpoint
                                    exit.dumpForDisplay(exitPrefix)
                                end
                            }
                        end
                    end
                end
            }
        }
    else
        puts "Invalid command: #{command}"
    end
end

if $stdin.tty?
    executeCommand("full")
end

while commandLine = Readline.readline("> ", true)
    executeCommand(*commandLine.split)
end