This PR attempts to support stackprof's object mode which tracks the number of allocated objects. This differs from the other modes (cpu and wall) by taking samples every time a Ruby object is allocated using Ruby's [`NEWOBJ` tracepoint](df24b85953/ext/stackprof/stackprof.c (L198-L199)).
When importing an object mode profile into speedscope today it still works but what you see is a profile using time units. The profile will only have samples for when an object was allocated which means even if time is reported, the profile is not really meaningful when looking at time.
To address this I've done three things when `mode` is `object`:
+ adjusted the total size of the `StackListProfileBuilder` to use the number of samples (since each sample is one allocation)
+ adjusted the weight of each sample to be `nSamples` (which I believe is always `1` but I'm not positive)
+ do not set the value formatter to a time formatter
Here's what it looks like before and after my changes (note the units and weight of samples):
wall (before) | object (before) | object (after)
-- | -- | --
<img width="1624" alt="Screen Shot 2022-05-11 at 4 51 31 PM" src="https://user-images.githubusercontent.com/898172/167945635-2401ca73-4de7-4559-b884-cf8947ca9738.png"> | <img width="1624" alt="Screen Shot 2022-05-11 at 4 51 34 PM" src="https://user-images.githubusercontent.com/898172/167945641-ef302a60-730b-4afd-8e44-5f02e54b3cb7.png"> | <img width="1624" alt="Screen Shot 2022-05-11 at 4 51 42 PM" src="https://user-images.githubusercontent.com/898172/167945643-5611b267-f8b2-4227-a2bf-7145c4030aa2.png">
<details>
<summary>Test code</summary>
```ruby
require 'stackprof'
require 'json'
def do_test
5.times do
make_a_word
end
end
def make_a_word
('a'..'z').to_a.shuffle.map(&:upcase).join
end
StackProf.start(mode: :object, interval: 1, raw: true)
do_test
StackProf.stop
File.write('tmp/object_profile.json', JSON.generate(StackProf.results))
StackProf.start(mode: :wall, interval: 1, raw: true)
do_test
StackProf.stop
File.write('tmp/wall_profile.json', JSON.generate(StackProf.results))
```
</details>
2 lines
12 KiB
JSON
2 lines
12 KiB
JSON
{"version":1.2,"mode":"object","interval":1,"samples":103,"gc_samples":0,"missed_samples":0,"metadata":{},"frames":{"4317464320":{"name":"block in <main>","file":"/Users/alex/src/github.com/alexcoco/speedscope/sample/programs/ruby/object.rb","line":21,"total_samples":103,"samples":1,"edges":{"4379033920":102},"lines":{"23":[1,1],"22":[102,0]}},"4379035920":{"name":"StackProf.run","file":"<cfunc>","line":null,"total_samples":103,"samples":0,"edges":{"4317464320":103}},"4376547240":{"name":"<main>","file":"/Users/alex/src/github.com/alexcoco/speedscope/sample/programs/ruby/object.rb","total_samples":103,"samples":0,"edges":{"4379035920":103},"lines":{"21":[103,0]}},"4317728280":{"name":"<main>","file":"../../alexcoco/speedscope/sample/programs/ruby/object.rb","total_samples":103,"samples":0,"edges":{"4376547240":103}},"4379033920":{"name":"Object#a","file":"/Users/alex/src/github.com/alexcoco/speedscope/sample/programs/ruby/object.rb","line":4,"total_samples":102,"samples":2,"edges":{"4317869400":102,"4379033880":5,"4379033840":95},"lines":{"6":[6,1],"5":[102,0],"8":[1,1],"7":[95,0]}},"4317869400":{"name":"Range#each","file":"<cfunc>","line":null,"total_samples":102,"samples":0,"edges":{"4379033920":102,"4379033840":95}},"4379033880":{"name":"Object#b","file":"/Users/alex/src/github.com/alexcoco/speedscope/sample/programs/ruby/object.rb","line":11,"total_samples":5,"samples":1,"edges":{"4318054440":4},"lines":{"12":[5,1]}},"4318054440":{"name":"Class#new","file":"<cfunc>","line":null,"total_samples":4,"samples":4},"4379033840":{"name":"Object#c","file":"/Users/alex/src/github.com/alexcoco/speedscope/sample/programs/ruby/object.rb","line":15,"total_samples":95,"samples":2,"edges":{"4317869400":95,"4317868920":37,"4317563560":20,"4317922760":36},"lines":{"17":[95,2],"16":[95,0]}},"4317868920":{"name":"Range#to_a","file":"<cfunc>","line":null,"total_samples":37,"samples":1,"edges":{"4379016640":36}},"4379016640":{"name":"Enumerable#to_a","file":"<cfunc>","line":null,"total_samples":36,"samples":36},"4317563560":{"name":"Array#sample","file":"<internal:array>","line":60,"total_samples":20,"samples":20,"lines":{"60":[1,1],"68":[19,19]}},"4317922760":{"name":"Array#sort","file":"<cfunc>","line":null,"total_samples":36,"samples":36}},"raw":[4,4317728280,4376547240,4379035920,4317464320,1,7,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,1,8,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033880,1,9,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033880,4318054440,2,7,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,1,10,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,1,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,10,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,3,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,9,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033880,4318054440,1,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,9,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033880,4318054440,1,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2,12,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317868920,4379016640,2,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317563560,1,11,4317728280,4376547240,4379035920,4317464320,4379033920,4317869400,4379033920,4379033840,4317869400,4379033840,4317922760,2],"raw_sample_timestamps":[3215395537054,3215395537057,3215395537058,3215395537059,3215395537060,3215395537060,3215395537061,3215395537063,3215395537064,3215395537064,3215395537066,3215395537068,3215395537082,3215395537090,3215395537091,3215395537091,3215395537092,3215395537093,3215395537094,3215395537094,3215395537094,3215395537095,3215395537095,3215395537096,3215395537096,3215395537096,3215395537097,3215395537097,3215395537097,3215395537098,3215395537098,3215395537099,3215395537099,3215395537099,3215395537100,3215395537100,3215395537100,3215395537101,3215395537101,3215395537102,3215395537102,3215395537102,3215395537103,3215395537103,3215395537104,3215395537104,3215395537104,3215395537105,3215395537105,3215395537106,3215395537106,3215395537107,3215395537107,3215395537107,3215395537108,3215395537108,3215395537108,3215395537109,3215395537109,3215395537110,3215395537110,3215395537110,3215395537111,3215395537111,3215395537112,3215395537112,3215395537112,3215395537113,3215395537113,3215395537113,3215395537114,3215395537114,3215395537114,3215395537115,3215395537115,3215395537116,3215395537116,3215395537116,3215395537117,3215395537117,3215395537117,3215395537118,3215395537118,3215395537118,3215395537119,3215395537119,3215395537119,3215395537120,3215395537120,3215395537120,3215395537121,3215395537121,3215395537122,3215395537122,3215395537122,3215395537123,3215395537123,3215395537123,3215395537124,3215395537124,3215395537125,3215395537125,3215395537126],"raw_timestamp_deltas":[4,2,0,0,1,0,0,1,1,0,1,2,14,8,0,0,0,0,1,0,0,1,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
|