pf2 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 84ea10eb9b861a910a1190ea017ad276ad593bc460c01687aabce8128392e35a
4
- data.tar.gz: abd41b0b0ca74ebbcfeb1a202b75a4eec52f7c48b5b87e80d6e3052516304415
3
+ metadata.gz: 117782be2a4d708cf477b5efb4cdf6cb7fbd7f58ef416d62b780c9d9ff4c7f16
4
+ data.tar.gz: 554efc38d2a316a84a9cd727afd9dac32815af50bc164409237bbb0fe38a9de9
5
5
  SHA512:
6
- metadata.gz: 7f17ba3e89269c97398559f97b87033a598e590b4c20efe341b76ce4841a47080a75c6324cce0881f83b6d68a336892ccd7ca593b953f5963097cf99b0a065ab
7
- data.tar.gz: d09630c20fbddb0e5b072c20cf97ad0af0f541db7fa4265090427a0a64af0bf4b565923fe9e988438a623e060c902ba92243b75d6b0f257df57b11674d8d4d1b
6
+ metadata.gz: 48601b3a739539cd028d3d72cbbb2b2838c2c3ca285a438b65be8fb3e14b6a7f4b1d67438f0f15f855d7d28a5d9dc815874a69d0acafc05d524adfb1ca87d1e4
7
+ data.tar.gz: 11759b313c52670f56f1fb05ed09565806049526aa649d740479d4363e636506a5eb745dcd9ddfaba6ea245dfd2b6547370bfe56df5b6166a740850ae36fa621
data/.rdoc_options CHANGED
@@ -2,5 +2,8 @@ title: Pf2
2
2
  main_page: README.md
3
3
  encoding: UTF-8
4
4
 
5
+ exclude:
6
+ - ext/patches
7
+
5
8
  autolink_excluded_words:
6
9
  - Pf2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  ## [Unreleased]
2
2
 
3
+
4
+ ## [0.14.0] - 2026-01-25
5
+
6
+ ### Added
7
+
8
+ - `Pf2.profile` can now directly write a Firefox Profiler-compatible profile into a file or an IO-ish object. Check out `Pf2.profile`'s `out:` and `format:` option.
9
+ - An _experimental_ pprof-compatible recorder has been added. Profiles generated by this reporter can be passed to tools such as `go tool pprof`, or services such as Grafana Pyroscope or Google Cloud's Cloud Profiler.
10
+
11
+ ### Changed
12
+
13
+ - `Pf2::Reporter::FirefoxProfilerSer2` now emits a JSON string, not a Hash. Parse the JSON to obtain the original representaion.
14
+
15
+ ### Fixed
16
+
17
+ - Fixed a crash when a delayed SIGPROF is received after `Pf2.stop` is called.
18
+
19
+
3
20
  ## [0.13.0] - 2026-01-18
4
21
 
5
22
  ### Added
data/README.md CHANGED
@@ -6,7 +6,6 @@ A experimental sampling-based profiler for Ruby 3.3+.
6
6
  - GitHub: https://github.com/osyoyu/pf2
7
7
  - Documentation: https://osyoyu.github.io/pf2/
8
8
 
9
-
10
9
  Notable Capabilites
11
10
  --------
12
11
 
@@ -36,7 +35,29 @@ Pf2 can be installed as a standalone CLI tool as well.
36
35
  gem install pf2
37
36
  ```
38
37
 
39
- ### Quickstart
38
+ ### Profiling
39
+
40
+ Surround and run your code with `Pf2.profile { }`.
41
+
42
+ ```ruby
43
+ profile = Pf2.profile(out: 'profile.json') do
44
+ your_code_here() # will be profiled
45
+ Thread.new { threaded_code() } # will also be profiled
46
+ end
47
+ ```
48
+
49
+ Then drop 'profile.json' into https://profiler.firefox.com/ for nice visualization.
50
+
51
+ Options for `Pf2.profile`:
52
+
53
+ | Option | Type | Description |
54
+ | ----------- | ----------------- | ---------------------------------------------- |
55
+ | interval_ms | Integer | Sampling interval in milliseconds (default: 9) |
56
+ | time_mode | `:cpu` or `:wall` | Sampling timer mode (default: `:cpu`) |
57
+ | out | String or IO-like | Location to write collected profile data |
58
+ | format | `:firefox` or `:pf2prof` | Profile format to be saved |
59
+
60
+ ### Direct visualization using 'pf2 serve'
40
61
 
41
62
  Run your Ruby program through `pf2 serve`.
42
63
  Wait a while until Pf2 collects profiles (or until the target program exits), then open the displayed link for visualization.
@@ -49,39 +70,32 @@ $ pf2 serve -- ruby target.rb
49
70
  I'm the target program!
50
71
  ```
51
72
 
52
- ### Profiling
73
+ No profile data is sent to Mozilla's servers. https://profiler.firefox.com operates inside your browser, unless you explicitly upload your profile for sharing.
74
+
75
+ ### More precise control
53
76
 
54
- Pf2 will collect samples every 10 ms of wall time by default.
77
+ More precise control may be acheived by using `Pf2.start` and `Pf2.stop`.
55
78
 
56
79
  ```ruby
57
- # Threads in `threads` will be tracked
58
- Pf2.start(threads: [Thread.current])
80
+ Pf2.start
59
81
 
60
82
  your_code_here
61
83
 
62
- # Stop profiling and save the profile for visualization
84
+ # Stop profiling
63
85
  profile = Pf2.stop
64
- File.write("my_program.pf2profile", profile)
65
- ```
66
-
67
- Alternatively, you may provide a code block to profile.
68
86
 
69
- ```ruby
70
- profile = Pf2.profile do
71
- your_code_here() # will be profiled
72
- Thread.new { threaded_code() } # will also be profiled
73
- end
74
-
75
- # Save the profile for visualization
76
- File.write("my_program.pf2profile", profile)
87
+ # Save file for later visualization using 'pf2 report'
88
+ File.binwrite("my_program.pf2prof", Marshal.dump(profile))
77
89
  ```
78
90
 
91
+ `Pf2.start` accepts `interval_ms` and `time_mode`.
92
+
79
93
  ### Reporting / Visualization
80
94
 
81
95
  Profiles can be visualized using the [Firefox Profiler](https://profiler.firefox.com/).
82
96
 
83
97
  ```console
84
- $ pf2 report -o report.json my_program.pf2profile
98
+ $ pf2 report -o report.json my_program.pf2prof
85
99
  ```
86
100
 
87
101
  Alternatively, `pf2 annotate` can be used to display hit counts side-by-side with source code.
@@ -90,22 +104,10 @@ Alternatively, `pf2 annotate` can be used to display hit counts side-by-side wit
90
104
  $ pf2 annotate my_program.pf2prof
91
105
  ```
92
106
 
93
- ### Configuration
94
-
95
- Pf2 accepts the following configuration keys:
96
-
97
- ```rb
98
- Pf2.start(
99
- interval_ms: 9, # Integer: The sampling interval in milliseconds (default: 9)
100
- time_mode: :cpu, # `:cpu` or `:wall`: The sampling timer's mode
101
- )
102
- ```
103
-
104
-
105
107
  Overhead
106
108
  --------
107
109
 
108
- While Pf2 aims to be non-disturbulent as much as possible, a small overhead still is incured.
110
+ While Pf2 aims to be non-disturbulent as much as possible, a small overhead is still incured.
109
111
 
110
112
  (TBD)
111
113
 
@@ -150,19 +152,11 @@ Another scheduler is the `TimerThreadScheduler`, which maintains a time-keeping
150
152
 
151
153
  This scheduler is wall-time only, and does not support CPU-time based profiling.
152
154
 
153
-
154
- Wishlist
155
- --------
156
-
157
- - [Flame Scopes](https://www.brendangregg.com/flamescope.html)
158
- - more
159
-
160
155
  Development
161
156
  --------
162
157
 
163
158
  See [doc/development.md](doc/development.md).
164
159
 
165
-
166
160
  License
167
161
  --------
168
162
 
@@ -57,3 +57,19 @@ This project includes the following third-party software:
57
57
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
58
58
  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59
59
  POSSIBILITY OF SUCH DAMAGE.
60
+
61
+ ## profile.proto
62
+
63
+ Copyright 2016 Google Inc. All Rights Reserved.
64
+
65
+ Licensed under the Apache License, Version 2.0 (the "License");
66
+ you may not use this file except in compliance with the License.
67
+ You may obtain a copy of the License at
68
+
69
+ http://www.apache.org/licenses/LICENSE-2.0
70
+
71
+ Unless required by applicable law or agreed to in writing, software
72
+ distributed under the License is distributed on an "AS IS" BASIS,
73
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74
+ See the License for the specific language governing permissions and
75
+ limitations under the License.
data/doc/development.md CHANGED
@@ -15,3 +15,9 @@ Releasing
15
15
  - Run `bundle install` to update Gemfile.lock
16
16
  - Commit changes
17
17
  - Run `bundle exec rake release`
18
+
19
+ Hidden options for testing
20
+ --------
21
+
22
+ - Pf2.start
23
+ - _test_no_install_timer: When true, no timer is installed. Manually send a SIGPROF to trigger sampling.
data/ext/pf2/session.c CHANGED
@@ -195,6 +195,12 @@ sigprof_handler(int sig, siginfo_t *info, void *ucontext)
195
195
 
196
196
  struct pf2_session *session = global_current_session;
197
197
 
198
+ // Pending signals may be delivered even after the session is stopped.
199
+ // Simply ignore those.
200
+ if (session == NULL || session->is_running == false) {
201
+ return;
202
+ }
203
+
198
204
  // If garbage collection is in progress, don't collect samples.
199
205
  if (atomic_load_explicit(&session->is_marking, memory_order_acquire)) {
200
206
  PF2_DEBUG_LOG("Dropping sample: Garbage collection is in progress\n");
@@ -388,8 +394,8 @@ pf2_session_stop(struct pf2_session *session)
388
394
  if (setitimer(which_timer, &zero_timer, NULL) == -1) {
389
395
  rb_raise(rb_eRuntimeError, "Failed to stop timer");
390
396
  }
391
- global_current_session = NULL;
392
397
  #endif
398
+ global_current_session = NULL;
393
399
 
394
400
  // Terminate the collector thread
395
401
  session->is_running = false;
data/lib/pf2/cli.rb CHANGED
@@ -60,7 +60,6 @@ module Pf2
60
60
 
61
61
  profile = Marshal.load(File.read(argv[0]))
62
62
  report = Pf2::Reporter::FirefoxProfilerSer2.new(profile).emit
63
- report = JSON.generate(report)
64
63
 
65
64
  if options[:output_file]
66
65
  File.write(options[:output_file], report)
@@ -61,7 +61,7 @@ module Pf2
61
61
  counters: [],
62
62
  threads: thread_reports,
63
63
  }
64
- FirefoxProfilerSer2.deep_camelize_keys(report)
64
+ JSON.generate(FirefoxProfilerSer2.deep_camelize_keys(report))
65
65
  end
66
66
 
67
67
  class ThreadReport
@@ -0,0 +1,349 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+ require 'zlib'
5
+
6
+ module Pf2
7
+ module Reporter
8
+ # A minimal Protobuf encoder.
9
+ class Protobuf
10
+ def initialize
11
+ @data = +"".b
12
+ end
13
+
14
+ def to_bytes
15
+ @data
16
+ end
17
+
18
+ # Wire types
19
+ # Not implemented: I64, SGROUP, EGROUP, I32
20
+
21
+ WIRETYPE_VARINT = 0
22
+ WIRETYPE_LEN = 2
23
+
24
+ def put_byte(b)
25
+ @data << (b & 0xFF).chr(Encoding::BINARY)
26
+ end
27
+
28
+ def put_varint(val)
29
+ # Emit 7-bit chunks
30
+ # msb=1 for continuation
31
+ while val > 0x7F
32
+ put_byte((val & 0x7F) | 0x80)
33
+ val >>= 7
34
+ end
35
+ put_byte(val & 0xFF)
36
+ end
37
+
38
+ def wire_varint(field, val)
39
+ put_varint((field << 3) | WIRETYPE_VARINT) # tag
40
+ put_varint(val)
41
+ end
42
+
43
+ def wire_len(field, bytes)
44
+ put_varint((field << 3) | WIRETYPE_LEN) # tag
45
+ put_varint(bytes.bytesize)
46
+ @data << bytes.b
47
+ end
48
+
49
+ # ---
50
+
51
+ # Scalar value types
52
+ # Not implemented:
53
+ # double, float, uint32, sint32, sint64,
54
+ # fixed32, fixed64, sfixed32, sfixed64, bytes
55
+
56
+ def int32(field, val)
57
+ twos_comp = val & 0xFFFF_FFFF
58
+ wire_varint(field, twos_comp)
59
+ end
60
+
61
+ def int64(field, val)
62
+ twos_comp = val & 0xFFFF_FFFF_FFFF_FFFF
63
+ wire_varint(field, twos_comp)
64
+ end
65
+
66
+ def uint64(field, val)
67
+ wire_varint(field, val)
68
+ end
69
+
70
+ def bool(field, val)
71
+ wire_varint(field, val ? 1 : 0)
72
+ end
73
+
74
+ def string(field, val)
75
+ wire_len(field, val)
76
+ end
77
+
78
+ # ---
79
+
80
+ def submessage(field, submsg)
81
+ wire_len(field, submsg)
82
+ end
83
+ end
84
+
85
+ PROTO_TAGS = {
86
+ profile: {
87
+ sample_type: 1,
88
+ sample: 2,
89
+ mapping: 3,
90
+ location: 4,
91
+ function: 5,
92
+ string_table: 6,
93
+ drop_frames: 7,
94
+ keep_frames: 8,
95
+ time_nanos: 9,
96
+ duration_nanos: 10,
97
+ period_type: 11,
98
+ period: 12,
99
+ comment: 13,
100
+ default_sample_type: 14,
101
+ doc_url: 15,
102
+ },
103
+ value_type: {
104
+ type: 1,
105
+ unit: 2,
106
+ },
107
+ sample: {
108
+ location_id: 1,
109
+ value: 2,
110
+ label: 3,
111
+ },
112
+ label: {
113
+ key: 1,
114
+ str: 2,
115
+ num: 3,
116
+ num_unit: 4,
117
+ },
118
+ mapping: {
119
+ id: 1,
120
+ memory_start: 2,
121
+ memory_limit: 3,
122
+ file_offset: 4,
123
+ filename: 5,
124
+ build_id: 6,
125
+ has_functions: 7,
126
+ has_filenames: 8,
127
+ has_line_numbers: 9,
128
+ has_inline_frames: 10,
129
+ },
130
+ location: {
131
+ id: 1,
132
+ mapping_id: 2,
133
+ address: 3,
134
+ line: 4,
135
+ is_folded: 5,
136
+ },
137
+ line: {
138
+ function_id: 1,
139
+ line: 2,
140
+ column: 3,
141
+ },
142
+ function: {
143
+ id: 1,
144
+ name: 2,
145
+ system_name: 3,
146
+ filename: 4,
147
+ start_line: 5,
148
+ },
149
+ }
150
+
151
+ class Pprof
152
+ def initialize(profile)
153
+ @profile = profile
154
+ @stack_weaver = StackWeaver.new(profile)
155
+
156
+ @strings = []
157
+ @string_id_table = {}
158
+ @function_id_map = {}
159
+ @location_id_map = {}
160
+
161
+ string_index("") # index 0 is empty string
162
+ end
163
+
164
+ def emit
165
+ pb = Protobuf.new
166
+
167
+ pb.int64(PROTO_TAGS[:profile][:time_nanos], @profile[:start_timestamp_ns] || 0)
168
+ pb.int64(PROTO_TAGS[:profile][:duration_nanos], @profile[:duration_ns] || 0)
169
+
170
+ # Sample types
171
+ pb.submessage(PROTO_TAGS[:profile][:sample_type], pb_value_type("samples", "count"))
172
+ pb.submessage(PROTO_TAGS[:profile][:sample_type], pb_value_type("cpu", "nanoseconds"))
173
+
174
+ # Period
175
+ pb.submessage(PROTO_TAGS[:profile][:period_type], pb_value_type("cpu", "nanoseconds"))
176
+ pb.int64(PROTO_TAGS[:profile][:period], inferred_period_ns)
177
+
178
+ build_functions(pb)
179
+ build_locations(pb)
180
+ build_samples(pb)
181
+
182
+ # String table
183
+ @strings.each do |s|
184
+ pb.string(PROTO_TAGS[:profile][:string_table], s)
185
+ end
186
+
187
+ buffer = StringIO.new
188
+ Zlib::GzipWriter.wrap(buffer) { |gz| gz.write(pb.to_bytes) }
189
+ buffer.string
190
+ end
191
+
192
+ private
193
+
194
+ def string_index(str)
195
+ if @string_id_table.key?(str)
196
+ return @string_id_table[str]
197
+ end
198
+
199
+ id = @strings.length
200
+ @strings << str
201
+ @string_id_table[str] = id
202
+ id
203
+ end
204
+
205
+ def pb_value_type(type, unit)
206
+ pb = Protobuf.new
207
+ pb.int64(PROTO_TAGS[:value_type][:type], string_index(type))
208
+ pb.int64(PROTO_TAGS[:value_type][:unit], string_index(unit))
209
+ pb.to_bytes
210
+ end
211
+
212
+ def pb_sample(location_ids, cpu_value_ns)
213
+ pb = Protobuf.new
214
+ # Equalivalent to:
215
+ # location_ids.each do |loc_id|
216
+ # pb.uint64(PROTO_TAGS[:sample][:location_id], loc_id)
217
+ # end
218
+ # pb.int64(PROTO_TAGS[:sample][:value], 1) # samples
219
+ # pb.int64(PROTO_TAGS[:sample][:value], cpu_value_ns) # cpu nanoseconds
220
+ pack_repeated_uint64(pb, PROTO_TAGS[:sample][:location_id], location_ids)
221
+ pack_repeated_int64(pb, PROTO_TAGS[:sample][:value], [1, cpu_value_ns])
222
+ pb.to_bytes
223
+ end
224
+
225
+ def pack_repeated_uint64(pb, field_no, values)
226
+ packed = Protobuf.new
227
+ values.each { |v| packed.put_varint(v) }
228
+ pb.wire_len(field_no, packed.to_bytes)
229
+ end
230
+
231
+ def pack_repeated_int64(pb, field_no, values)
232
+ packed = Protobuf.new
233
+ values.each { |v| packed.put_varint(v & 0xFFFF_FFFF_FFFF_FFFF) }
234
+ pb.wire_len(field_no, packed.to_bytes)
235
+ end
236
+
237
+ # def pb_mapping(id, memory_start, memory_limit, file_offset, filename)
238
+ # pb = Protobuf.new
239
+ # pb.uint64(PROTO_TAGS[:mapping][:id], id)
240
+ # pb.uint64(PROTO_TAGS[:mapping][:memory_start], memory_start)
241
+ # pb.uint64(PROTO_TAGS[:mapping][:memory_limit], memory_limit)
242
+ # pb.uint64(PROTO_TAGS[:mapping][:file_offset], file_offset)
243
+ # pb.int64(PROTO_TAGS[:mapping][:filename], string_index(filename))
244
+ # pb.int64(PROTO_TAGS[:mapping][:build_id], 123456)
245
+ # pb.bool(PROTO_TAGS[:mapping][:has_functions], true)
246
+ # pb.bool(PROTO_TAGS[:mapping][:has_filenames], true)
247
+ # pb.bool(PROTO_TAGS[:mapping][:has_line_numbers], true)
248
+ # pb.bool(PROTO_TAGS[:mapping][:has_inline_frames], false)
249
+ # pb.to_bytes
250
+ # end
251
+
252
+ def pb_location(id, mapping_id, address, lines)
253
+ pb = Protobuf.new
254
+ pb.uint64(PROTO_TAGS[:location][:id], id)
255
+ pb.uint64(PROTO_TAGS[:location][:mapping_id], mapping_id)
256
+ pb.uint64(PROTO_TAGS[:location][:address], address)
257
+ lines.each do |fn_id, lineno, column|
258
+ pb.submessage(PROTO_TAGS[:location][:line], pb_line(fn_id, lineno, column || 0))
259
+ end
260
+ pb
261
+ end
262
+
263
+ def pb_line(function_id, line, column = 0)
264
+ pb = Protobuf.new
265
+ pb.uint64(PROTO_TAGS[:line][:function_id], function_id)
266
+ pb.int32(PROTO_TAGS[:line][:line], line)
267
+ pb.int32(PROTO_TAGS[:line][:column], column) if column != 0
268
+ pb.to_bytes
269
+ end
270
+
271
+ def pb_function(id, name, system_name, filename, start_line)
272
+ pb = Protobuf.new
273
+ pb.uint64(PROTO_TAGS[:function][:id], id)
274
+ pb.int64(PROTO_TAGS[:function][:name], string_index(name))
275
+ pb.int64(PROTO_TAGS[:function][:system_name], string_index(system_name))
276
+ pb.int64(PROTO_TAGS[:function][:filename], string_index(filename))
277
+ pb.int64(PROTO_TAGS[:function][:start_line], start_line)
278
+ pb.to_bytes
279
+ end
280
+
281
+ def build_functions(pb)
282
+ @profile[:functions].each.with_index do |fn, idx|
283
+ id = idx + 1
284
+ @function_id_map[idx] = id
285
+ pb.submessage(
286
+ PROTO_TAGS[:profile][:function],
287
+ pb_function(
288
+ id,
289
+ fn[:name] || "<unknown>",
290
+ "",
291
+ fn[:filename] || "<unknown>",
292
+ fn[:start_lineno] || 0,
293
+ )
294
+ )
295
+ end
296
+ end
297
+
298
+ def build_locations(pb)
299
+ @profile[:locations].each.with_index do |loc, idx|
300
+ id = idx + 1
301
+ @location_id_map[idx] = id
302
+ function_id = @function_id_map[loc[:function_index]] || 0
303
+ pb.submessage(
304
+ PROTO_TAGS[:profile][:location],
305
+ pb_location(
306
+ id,
307
+ 0, # mapping_id
308
+ loc[:address] || 0,
309
+ [[function_id, loc[:lineno] || 0]],
310
+ ).to_bytes
311
+ )
312
+ end
313
+ end
314
+
315
+ def build_samples(pb)
316
+ cpu_value_ns = inferred_sample_value_ns
317
+
318
+ @profile[:samples].each do |sample|
319
+ woven_stack = @stack_weaver.weave(sample[:stack], sample[:native_stack] || [])
320
+ woven_stack.reverse! # leaf -> root for pprof
321
+ location_ids = woven_stack.map { |idx| @location_id_map[idx] }.compact
322
+ next if location_ids.empty?
323
+
324
+ pb.submessage(
325
+ PROTO_TAGS[:profile][:sample],
326
+ pb_sample(location_ids, cpu_value_ns)
327
+ )
328
+ end
329
+ end
330
+
331
+ def inferred_period_ns
332
+ value = inferred_sample_value_ns
333
+ return value if value > 0
334
+
335
+ 9_000_000 # default 9 ms
336
+ end
337
+
338
+ def inferred_sample_value_ns
339
+ samples = @profile[:samples]
340
+ return 0 if samples.empty?
341
+
342
+ duration = @profile[:duration_ns] || 0
343
+ return 0 if duration == 0
344
+
345
+ (duration.to_f / samples.length).round
346
+ end
347
+ end
348
+ end
349
+ end
data/lib/pf2/reporter.rb CHANGED
@@ -3,3 +3,4 @@
3
3
  require_relative './reporter/annotate'
4
4
  require_relative './reporter/stack_weaver'
5
5
  require_relative './reporter/firefox_profiler_ser2'
6
+ require_relative './reporter/pprof'
data/lib/pf2/serve.rb CHANGED
@@ -29,7 +29,7 @@ module Pf2
29
29
  profile = Pf2.stop
30
30
  res.header['Content-Type'] = 'application/json'
31
31
  res.header['Access-Control-Allow-Origin'] = '*'
32
- res.body = JSON.generate(Pf2::Reporter::FirefoxProfilerSer2.new(profile).emit)
32
+ res.body = Pf2::Reporter::FirefoxProfilerSer2.new(profile).emit
33
33
  Pf2.start
34
34
  end
35
35
 
data/lib/pf2/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pf2
4
- VERSION = '0.13.0'
4
+ VERSION = '0.14.0'
5
5
  end
data/lib/pf2.rb CHANGED
@@ -6,29 +6,74 @@ require_relative 'pf2/version'
6
6
  module Pf2
7
7
  class Error < StandardError; end
8
8
 
9
+ KNOWN_FORMATS = [:pf2prof, :firefox, :pprof] # :nodoc:
10
+ private_constant :KNOWN_FORMATS
11
+
12
+ # Start a profiling session.
13
+ #
14
+ # Parameters:
15
+ # interval_ms - Sampling interval in milliseconds (default: 9)
16
+ # time_mode - :cpu or :wall (default: :cpu)
9
17
  def self.start(...)
10
18
  @@session = Session.new(...)
11
19
  @@session.start
12
20
  end
13
21
 
22
+ # Stop the current profiling session and return the profile data.
14
23
  def self.stop
15
24
  @@session.stop
16
25
  end
17
26
 
18
27
  # Profiles the given block of code.
19
28
  #
29
+ # Parameters:
30
+ # interval_ms - Sampling interval in milliseconds (default: 9)
31
+ # time_mode - :cpu or :wall (default: :cpu)
32
+ # out - String or IO-like object specifying where to write profile data.
33
+ # - nil (default): do not write to file
34
+ # - String: file path to write the profile data
35
+ # - IO-like object: an object responding to #write
36
+ # format - Output format. Possible values are:
37
+ # - :pf2prof: Raw profile dump loadable by 'pf2 report'
38
+ # - :firefox (default): JSON for profiler.firefox.com
39
+ # - :pprof: .pb.gz compatible with Golang's pprof tool
40
+ #
20
41
  # Example:
21
42
  #
22
43
  # profile = Pf2.profile(interval_ms: 42) do
23
44
  # your_code_here
24
45
  # end
25
46
  #
26
- def self.profile(**kwargs, &block)
47
+ def self.profile(interval_ms: 9, time_mode: :cpu, out: nil, format: :firefox, &block)
27
48
  raise ArgumentError, "block required" unless block_given?
28
- start(**kwargs)
49
+ raise ArgumentError, "Unknown format: #{format}" unless KNOWN_FORMATS.include?(format)
50
+ if !(out.nil? || out.is_a?(String) || (out.respond_to?(:write) && out.respond_to?(:close)))
51
+ raise ArgumentError, "'out' must be an IO-like object"
52
+ end
53
+
54
+ start(interval_ms:, time_mode:)
29
55
  yield
30
- result = stop
56
+ result = stop()
31
57
  @@session = nil # let GC clean up the session
58
+
59
+ if out
60
+ is_path_passed = out.is_a?(String)
61
+ io = is_path_passed ? File.open(out, "wb") : out
62
+ case format
63
+ in :pf2prof
64
+ io.write(Marshal.dump(result))
65
+ in :firefox
66
+ require 'pf2/reporter'
67
+ reporter = Reporter::FirefoxProfilerSer2.new(result)
68
+ io.write(reporter.emit)
69
+ in :pprof
70
+ require 'pf2/reporter'
71
+ reporter = Reporter::Pprof.new(result)
72
+ io.write(reporter.emit)
73
+ end
74
+ io.close if is_path_passed
75
+ end
76
+
32
77
  result
33
78
  ensure
34
79
  if defined?(@@session) && @@session != nil
@@ -0,0 +1,233 @@
1
+ // Copyright 2016 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ // Profile is a common stacktrace profile format.
16
+ //
17
+ // Measurements represented with this format should follow the
18
+ // following conventions:
19
+ //
20
+ // - Consumers should treat unset optional fields as if they had been
21
+ // set with their default value.
22
+ //
23
+ // - When possible, measurements should be stored in "unsampled" form
24
+ // that is most useful to humans. There should be enough
25
+ // information present to determine the original sampled values.
26
+ //
27
+ // - On-disk, the serialized proto must be gzip-compressed.
28
+ //
29
+ // - The profile is represented as a set of samples, where each sample
30
+ // references a sequence of locations, and where each location belongs
31
+ // to a mapping.
32
+ // - There is a N->1 relationship from sample.location_id entries to
33
+ // locations. For every sample.location_id entry there must be a
34
+ // unique Location with that id.
35
+ // - There is an optional N->1 relationship from locations to
36
+ // mappings. For every nonzero Location.mapping_id there must be a
37
+ // unique Mapping with that id.
38
+
39
+ syntax = "proto3";
40
+
41
+ package perftools.profiles;
42
+
43
+ option java_package = "com.google.perftools.profiles";
44
+ option java_outer_classname = "ProfileProto";
45
+
46
+ message Profile {
47
+ // A description of the samples associated with each Sample.value.
48
+ // For a cpu profile this might be:
49
+ // [["cpu","nanoseconds"]] or [["wall","seconds"]] or [["syscall","count"]]
50
+ // For a heap profile, this might be:
51
+ // [["allocations","count"], ["space","bytes"]],
52
+ // If one of the values represents the number of events represented
53
+ // by the sample, by convention it should be at index 0 and use
54
+ // sample_type.unit == "count".
55
+ repeated ValueType sample_type = 1;
56
+ // The set of samples recorded in this profile.
57
+ repeated Sample sample = 2;
58
+ // Mapping from address ranges to the image/binary/library mapped
59
+ // into that address range. mapping[0] will be the main binary.
60
+ repeated Mapping mapping = 3;
61
+ // Locations referenced by samples.
62
+ repeated Location location = 4;
63
+ // Functions referenced by locations.
64
+ repeated Function function = 5;
65
+ // A common table for strings referenced by various messages.
66
+ // string_table[0] must always be "".
67
+ repeated string string_table = 6;
68
+ // frames with Function.function_name fully matching the following
69
+ // regexp will be dropped from the samples, along with their successors.
70
+ int64 drop_frames = 7; // Index into string table.
71
+ // frames with Function.function_name fully matching the following
72
+ // regexp will be kept, even if it matches drop_frames.
73
+ int64 keep_frames = 8; // Index into string table.
74
+
75
+ // The following fields are informational, do not affect
76
+ // interpretation of results.
77
+
78
+ // Time of collection (UTC) represented as nanoseconds past the epoch.
79
+ int64 time_nanos = 9;
80
+ // Duration of the profile, if a duration makes sense.
81
+ int64 duration_nanos = 10;
82
+ // The kind of events between sampled occurrences.
83
+ // e.g [ "cpu","cycles" ] or [ "heap","bytes" ]
84
+ ValueType period_type = 11;
85
+ // The number of events between sampled occurrences.
86
+ int64 period = 12;
87
+ // Free-form text associated with the profile. The text is displayed as is
88
+ // to the user by the tools that read profiles (e.g. by pprof). This field
89
+ // should not be used to store any machine-readable information, it is only
90
+ // for human-friendly content. The profile must stay functional if this field
91
+ // is cleaned.
92
+ repeated int64 comment = 13; // Indices into string table.
93
+ // Index into the string table of the type of the preferred sample
94
+ // value. If unset, clients should default to the last sample value.
95
+ int64 default_sample_type = 14;
96
+ // Documentation link for this profile type. The URL must be absolute,
97
+ // e.g., http://pprof.example.com/cpu-profile.html
98
+ //
99
+ // The URL may be missing if the profile was generated by code that did not
100
+ // supply a link.
101
+ int64 doc_url = 15; // Index into string table.
102
+ }
103
+
104
+ // ValueType describes the semantics and measurement units of a value.
105
+ message ValueType {
106
+ int64 type = 1; // Index into string table.
107
+ int64 unit = 2; // Index into string table.
108
+ }
109
+
110
+ // Each Sample records values encountered in some program
111
+ // context. The program context is typically a stack trace, perhaps
112
+ // augmented with auxiliary information like the thread-id, some
113
+ // indicator of a higher level request being handled etc.
114
+ message Sample {
115
+ // The ids recorded here correspond to a Profile.location.id.
116
+ // The leaf is at location_id[0].
117
+ repeated uint64 location_id = 1;
118
+ // The type and unit of each value is defined by the corresponding
119
+ // entry in Profile.sample_type. All samples must have the same
120
+ // number of values, the same as the length of Profile.sample_type.
121
+ // When aggregating multiple samples into a single sample, the
122
+ // result has a list of values that is the element-wise sum of the
123
+ // lists of the originals.
124
+ repeated int64 value = 2;
125
+ // label includes additional context for this sample. It can include
126
+ // things like a thread id, allocation size, etc.
127
+ //
128
+ // NOTE: While possible, having multiple values for the same label key is
129
+ // strongly discouraged and should never be used. Most tools (e.g. pprof) do
130
+ // not have good (or any) support for multi-value labels. And an even more
131
+ // discouraged case is having a string label and a numeric label of the same
132
+ // name on a sample. Again, possible to express, but should not be used.
133
+ repeated Label label = 3;
134
+ }
135
+
136
+ message Label {
137
+ // Index into string table. An annotation for a sample (e.g.
138
+ // "allocation_size") with an associated value.
139
+ // Keys with "pprof::" prefix are reserved for internal use by pprof.
140
+ int64 key = 1;
141
+
142
+ // At most one of the following must be present
143
+ int64 str = 2; // Index into string table
144
+ int64 num = 3;
145
+
146
+ // Should only be present when num is present.
147
+ // Specifies the units of num.
148
+ // Use arbitrary string (for example, "requests") as a custom count unit.
149
+ // If no unit is specified, consumer may apply heuristic to deduce the unit.
150
+ // Consumers may also interpret units like "bytes" and "kilobytes" as memory
151
+ // units and units like "seconds" and "nanoseconds" as time units,
152
+ // and apply appropriate unit conversions to these.
153
+ int64 num_unit = 4; // Index into string table
154
+ }
155
+
156
+ message Mapping {
157
+ // Unique nonzero id for the mapping.
158
+ uint64 id = 1;
159
+ // Address at which the binary (or DLL) is loaded into memory.
160
+ uint64 memory_start = 2;
161
+ // The limit of the address range occupied by this mapping.
162
+ uint64 memory_limit = 3;
163
+ // Offset in the binary that corresponds to the first mapped address.
164
+ uint64 file_offset = 4;
165
+ // The object this entry is loaded from. This can be a filename on
166
+ // disk for the main binary and shared libraries, or virtual
167
+ // abstractions like "[vdso]".
168
+ int64 filename = 5; // Index into string table
169
+ // A string that uniquely identifies a particular program version
170
+ // with high probability. E.g., for binaries generated by GNU tools,
171
+ // it could be the contents of the .note.gnu.build-id field.
172
+ int64 build_id = 6; // Index into string table
173
+
174
+ // The following fields indicate the resolution of symbolic info.
175
+ bool has_functions = 7;
176
+ bool has_filenames = 8;
177
+ bool has_line_numbers = 9;
178
+ bool has_inline_frames = 10;
179
+ }
180
+
181
+ // Describes function and line table debug information.
182
+ message Location {
183
+ // Unique nonzero id for the location. A profile could use
184
+ // instruction addresses or any integer sequence as ids.
185
+ uint64 id = 1;
186
+ // The id of the corresponding profile.Mapping for this location.
187
+ // It can be unset if the mapping is unknown or not applicable for
188
+ // this profile type.
189
+ uint64 mapping_id = 2;
190
+ // The instruction address for this location, if available. It
191
+ // should be within [Mapping.memory_start...Mapping.memory_limit]
192
+ // for the corresponding mapping. A non-leaf address may be in the
193
+ // middle of a call instruction. It is up to display tools to find
194
+ // the beginning of the instruction if necessary.
195
+ uint64 address = 3;
196
+ // Multiple line indicates this location has inlined functions,
197
+ // where the last entry represents the caller into which the
198
+ // preceding entries were inlined.
199
+ //
200
+ // E.g., if memcpy() is inlined into printf:
201
+ // line[0].function_name == "memcpy"
202
+ // line[1].function_name == "printf"
203
+ repeated Line line = 4;
204
+ // Provides an indication that multiple symbols map to this location's
205
+ // address, for example due to identical code folding by the linker. In that
206
+ // case the line information above represents one of the multiple
207
+ // symbols. This field must be recomputed when the symbolization state of the
208
+ // profile changes.
209
+ bool is_folded = 5;
210
+ }
211
+
212
+ message Line {
213
+ // The id of the corresponding profile.Function for this line.
214
+ uint64 function_id = 1;
215
+ // Line number in source code.
216
+ int64 line = 2;
217
+ // Column number in source code.
218
+ int64 column = 3;
219
+ }
220
+
221
+ message Function {
222
+ // Unique nonzero id for the function.
223
+ uint64 id = 1;
224
+ // Name of the function, in human-readable form if available.
225
+ int64 name = 2; // Index into string table
226
+ // Name of the function, as identified by the system.
227
+ // For instance, it can be a C++ mangled name.
228
+ int64 system_name = 3; // Index into string table
229
+ // Source file containing the function.
230
+ int64 filename = 4; // Index into string table
231
+ // Line number in source file.
232
+ int64 start_line = 5;
233
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pf2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daisuke Aritomo
@@ -149,6 +149,7 @@ files:
149
149
  - lib/pf2/reporter.rb
150
150
  - lib/pf2/reporter/annotate.rb
151
151
  - lib/pf2/reporter/firefox_profiler_ser2.rb
152
+ - lib/pf2/reporter/pprof.rb
152
153
  - lib/pf2/reporter/stack_weaver.rb
153
154
  - lib/pf2/serve.rb
154
155
  - lib/pf2/version.rb
@@ -222,6 +223,7 @@ files:
222
223
  - vendor/libbacktrace/xztest.c
223
224
  - vendor/libbacktrace/zstdtest.c
224
225
  - vendor/libbacktrace/ztest.c
226
+ - vendor/profile.proto
225
227
  homepage: https://github.com/osyoyu/pf2
226
228
  licenses:
227
229
  - MIT