Skip to content

Commit

Permalink
Make top output work on parsed and profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Dec 13, 2024
1 parent 8c0b72a commit cfe7234
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/vernier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative "vernier/version"
require_relative "vernier/collector"
require_relative "vernier/stack_table"
require_relative "vernier/parsed_profile"
require_relative "vernier/result"
require_relative "vernier/hooks"
require_relative "vernier/vernier"
Expand Down
16 changes: 13 additions & 3 deletions lib/vernier/output/top.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ def initialize(profile)
end

def output
thread = @profile.main_thread
stack_table =
if thread.respond_to?(:stack_table)
thread.stack_table
else
@profile._stack_table
end

stack_weights = Hash.new(0)
@profile.samples.zip(@profile.weights) do |stack_idx, weight|
thread[:samples].zip(thread[:weights]) do |stack_idx, weight|
stack_weights[stack_idx] += weight
end

top_by_self = Hash.new(0)
stack_weights.each do |stack_idx, weight|
stack = @profile.stack(stack_idx)
top_by_self[stack.leaf_frame.name] += weight
frame_idx = stack_table.stack_frame_idx(stack_idx)
func_idx = stack_table.frame_func_idx(frame_idx)
name = stack_table.func_name(func_idx)
top_by_self[name] += weight
end

s = +""
Expand Down
13 changes: 13 additions & 0 deletions lib/vernier/parsed_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ def stack_table
def main_thread?
@data["isMainThread"]
end

def samples
@data["samples"]["stack"]
end

def weights
@data["samples"]["weight"]
end

# Emulate hash
def [](name)
send(name)
end
end

attr_reader :data
Expand Down
12 changes: 11 additions & 1 deletion test/output/test_top.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ def test_complex_profile
assert_match(/^\d+\tGVLTest\.sleep_without_gvl$/, output)
assert_match(/^\d+\tGVLTest\.sleep_holding_gvl$/, output)
assert_match(/^\d+\tKernel#sleep$/, output)
assert_match(/^\d+\tProcess.clock_gettime$/, output)
assert_match(/^\d+\tProcess\.clock_gettime$/, output)
end

def test_parsed_profile
profile = Vernier::ParsedProfile.read_file(fixture_path("gvl_sleep.vernier.json"))
output = Vernier::Output::Top.new(profile).output
assert_match(/^2010\tGVLTest\.sleep_without_gvl$/, output)
assert_match(/^2013\tGVLTest\.sleep_holding_gvl$/, output)
assert_match(/^2010\tKernel#sleep$/, output)
assert_match(/^1989\tObject#ruby_sleep_gvl$/, output)
assert_match(/^10\tProcess\.clock_gettime$/, output)
end
end
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
class Minitest::Test
make_my_diffs_pretty!

def fixture_path(filename)
File.expand_path("fixtures/#{filename}", __dir__)
end

def assert_valid_result(result)
assert_equal result.samples.length, result.weights.length

Expand Down

0 comments on commit cfe7234

Please sign in to comment.