Skip to content

Commit

Permalink
YJIT: Adjust the padding size of counts automatically (#9912)
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Feb 12, 2024
1 parent b9f25b1 commit c04782c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions yjit.rb
Expand Up @@ -239,7 +239,7 @@ class << self
private

# Print stats and dump exit locations
def print_and_dump_stats
def print_and_dump_stats # :nodoc:
if Primitive.rb_yjit_print_stats_p
_print_stats
end
Expand Down Expand Up @@ -396,18 +396,18 @@ def print_sorted_method_calls(calls, num_calls, out:, type:, how_many: 20, left_

# Sort calls by decreasing frequency and keep the top N
pairs = calls.map { |k,v| [k, v] }
pairs.sort_by! {|pair| pair[1] }
pairs.reverse!
pairs.sort_by! {|pair| -pair[1] }
pairs = pairs[0...how_many]

top_n_total = pairs.sum { |name, count| count }
top_n_pct = 100.0 * top_n_total / num_calls

out.puts "Top-#{pairs.size} most frequent #{type} calls (#{"%.1f" % top_n_pct}% of #{type} calls):"

count_width = format_number(0, pairs[0][1]).length
pairs.each do |name, count|
padded_count = format_number_pct(10, count, num_calls)
out.puts("#{padded_count}: #{name}")
padded_count = format_number_pct(count_width, count, num_calls)
out.puts(" #{padded_count}: #{name}")
end
end

Expand All @@ -429,9 +429,10 @@ def print_sorted_exit_counts(stats, out:, prefix:, how_many: 20, left_pad: 4) #

out.puts "Top-#{exits.size} most frequent exit ops (#{"%.1f" % top_n_exit_pct}% of exits):"

count_width = format_number(0, exits[0][1]).length
exits.each do |name, count|
padded_count = format_number_pct(10, count, total_exits)
out.puts("#{padded_count}: #{name}")
padded_count = format_number_pct(count_width, count, total_exits)
out.puts(" #{padded_count}: #{name}")
end
else
out.puts "total_exits: " + format_number(10, total_exits)
Expand Down Expand Up @@ -474,15 +475,15 @@ def print_counters(counters, out:, prefix:, prompt:, optional: false) # :nodoc:
end

# Format large numbers with comma separators for readability
def format_number(pad, number)
def format_number(pad, number) # :nodoc:
s = number.to_s
i = s.index('.') || s.size
s.insert(i -= 3, ',') while i > 3
s.rjust(pad, ' ')
end

# Format a number along with a percentage over a total value
def format_number_pct(pad, number, total)
def format_number_pct(pad, number, total) # :nodoc:
padded_count = format_number(pad, number)
percentage = number.fdiv(total) * 100
formatted_pct = "%4.1f%%" % percentage
Expand Down

0 comments on commit c04782c

Please sign in to comment.