pf2 0.12.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: ef67bdd8a362c1f5cd3dd37a47e13b64f5ee0663a204908d0f075d41acc94da3
4
- data.tar.gz: 535d1fe8efb9a408ce47218910d6ad3ddfe88c2a4e4f42ac42c01ab8dceceacd
3
+ metadata.gz: 117782be2a4d708cf477b5efb4cdf6cb7fbd7f58ef416d62b780c9d9ff4c7f16
4
+ data.tar.gz: 554efc38d2a316a84a9cd727afd9dac32815af50bc164409237bbb0fe38a9de9
5
5
  SHA512:
6
- metadata.gz: 15a65711f8c1a804b06d8b2195a9e620646972b7905a714fdab67561f9d796d7c92a1f332f9169ffa27c14b6b91d9f33ba254d84d47c281f66f7a069cc660d58
7
- data.tar.gz: 411bdf85ef4db94bed73840224132498621f389e4f6092a45e222bf5834dd5d1e6aa6940cafcb56cdc86148163d1b9bda4eee45501cc9c307ce729894954613d
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,35 @@
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
+
20
+ ## [0.13.0] - 2026-01-18
21
+
22
+ ### Added
23
+
24
+ - Pf2 should now have a dramatically lower memory footprint.
25
+ - Samples are now stored in a compact hashmap internally.
26
+ - See https://github.com/osyoyu/pf2/pull/85 for details.
27
+
28
+ ### Fixed
29
+
30
+ - `pf2 serve` command now properly works. (Thanks @hanazuki)
31
+
32
+
3
33
  ## [0.12.0] - 2026-01-09
4
34
 
5
35
  ### Added
data/README.md CHANGED
@@ -6,8 +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
- NOTE: This README contains some outdated information!
10
-
11
9
  Notable Capabilites
12
10
  --------
13
11
 
@@ -37,7 +35,29 @@ Pf2 can be installed as a standalone CLI tool as well.
37
35
  gem install pf2
38
36
  ```
39
37
 
40
- ### 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'
41
61
 
42
62
  Run your Ruby program through `pf2 serve`.
43
63
  Wait a while until Pf2 collects profiles (or until the target program exits), then open the displayed link for visualization.
@@ -50,39 +70,32 @@ $ pf2 serve -- ruby target.rb
50
70
  I'm the target program!
51
71
  ```
52
72
 
53
- ### 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.
54
74
 
55
- Pf2 will collect samples every 10 ms of wall time by default.
75
+ ### More precise control
76
+
77
+ More precise control may be acheived by using `Pf2.start` and `Pf2.stop`.
56
78
 
57
79
  ```ruby
58
- # Threads in `threads` will be tracked
59
- Pf2.start(threads: [Thread.current])
80
+ Pf2.start
60
81
 
61
82
  your_code_here
62
83
 
63
- # Stop profiling and save the profile for visualization
84
+ # Stop profiling
64
85
  profile = Pf2.stop
65
- File.write("my_program.pf2profile", profile)
66
- ```
67
-
68
- Alternatively, you may provide a code block to profile.
69
-
70
- ```ruby
71
- profile = Pf2.profile do
72
- your_code_here() # will be profiled
73
- Thread.new { threaded_code() } # will also be profiled
74
- end
75
86
 
76
- # Save the profile for visualization
77
- File.write("my_program.pf2profile", profile)
87
+ # Save file for later visualization using 'pf2 report'
88
+ File.binwrite("my_program.pf2prof", Marshal.dump(profile))
78
89
  ```
79
90
 
91
+ `Pf2.start` accepts `interval_ms` and `time_mode`.
92
+
80
93
  ### Reporting / Visualization
81
94
 
82
95
  Profiles can be visualized using the [Firefox Profiler](https://profiler.firefox.com/).
83
96
 
84
97
  ```console
85
- $ pf2 report -o report.json my_program.pf2profile
98
+ $ pf2 report -o report.json my_program.pf2prof
86
99
  ```
87
100
 
88
101
  Alternatively, `pf2 annotate` can be used to display hit counts side-by-side with source code.
@@ -91,25 +104,10 @@ Alternatively, `pf2 annotate` can be used to display hit counts side-by-side wit
91
104
  $ pf2 annotate my_program.pf2prof
92
105
  ```
93
106
 
94
- ### Configuration
95
-
96
- Pf2 accepts the following configuration keys:
97
-
98
- ```rb
99
- Pf2.start(
100
- interval_ms: 9, # Integer: The sampling interval in milliseconds (default: 9)
101
- time_mode: :cpu, # `:cpu` or `:wall`: The sampling timer's mode
102
- # (default: `:cpu` for SignalScheduler, `:wall` for TimerThreadScheduler)
103
- threads: [th1, th2], # `Array<Thread>` | `:all`: A list of Ruby Threads to be tracked.
104
- # When `:all` or unspecified, Pf2 will track all active Threads.
105
- )
106
- ```
107
-
108
-
109
107
  Overhead
110
108
  --------
111
109
 
112
- 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.
113
111
 
114
112
  (TBD)
115
113
 
@@ -132,45 +130,35 @@ Pf2 is a _sampling profiler_. This means that Pf2 collects _samples_ of program
132
130
 
133
131
  Pf2 uses the `rb_profile_thread_frames()` API for sampling. When to do so is controlled by _Schedulers_, described in the following section.
134
132
 
135
- ### Schedulers
133
+ ### Scheduling
136
134
 
137
- Schedulers determine when to execute sample collection, based on configuration (time mode and interval). Pf2 has two schedulers available.
135
+ Schedulers determine when to execute sample collection, based on configuration (time mode and interval).
138
136
 
139
- #### SignalScheduler (Linux-only)
137
+ #### Signal-based scheduling
140
138
 
141
- The first is the `SignalScheduler`, based on POSIX timers. Pf2 will use this scheduler when possible. SignalScheduler creates a POSIX timer for each Ruby Thread (the underlying pthread to be more accurate) using `timer_create(2)`. This leaves the actual time-keeping to the OS, which is capable of tracking accurate per-thread CPU time usage.
139
+ Pf2 schedules sample collection using POSIX timers. It creates a POSIX timer using `timer_create(3)` where available, or otherwise `setitimer(3)`. This leaves the actual time-keeping to the operating system kernel, which is capable of tracking accurate per-thread CPU time usage.
142
140
 
143
- When the specified interval has arrived (the timer has _expired_), the OS delivers us a SIGPROF signal. This is why the scheduler is named SignalScheduler.
141
+ When the specified interval has arrived (the timer has _expired_), the OS delivers us a SIGPROF signal.
144
142
 
145
143
  Signals are directed to Ruby Threads' underlying pthread, effectively "pausing" the Thread's activity. This routing is done using `SIGEV_THREAD_ID`, which is a Linux-only feature. Sample collection is done in the signal handler, which is expected to be more _accurate_, capturing the paused Thread's activity.
146
144
 
147
145
  This scheduler heavily relies on Ruby's 1:N Thread model (1 Ruby Threads is strongly tied to a native pthread). It will not work properly in MaNy (`RUBY_MN_THREADS=1`).
148
146
 
149
- #### TimerThreadScheduler
147
+ #### ~~Timer-thread based scheduling~~
148
+
149
+ Note: Timer thread-based scheduling has been removed in v0.10.0, when the profiling backend has been rewritten in C. This may come back in the future if needed.
150
150
 
151
151
  Another scheduler is the `TimerThreadScheduler`, which maintains a time-keeping thread by itself. A new native thread (pthread on Linux/macOS) will be created, and an infinite loop will be run inside. After `sleep(2)`-ing for the specified interval time, sampling will be queued using Ruby's Postponed Job API.
152
152
 
153
153
  This scheduler is wall-time only, and does not support CPU-time based profiling.
154
154
 
155
- #### macOS Support
156
-
157
- On platforms where `timer_create()` is not supported (namely macOS), Pf2 falls back to `setitimer()`.
158
-
159
-
160
- Wishlist
161
- --------
162
-
163
- - [Flame Scopes](https://www.brendangregg.com/flamescope.html)
164
- - More unit/e2e tests
165
- - more
166
-
167
155
  Development
168
156
  --------
169
157
 
170
158
  See [doc/development.md](doc/development.md).
171
159
 
172
-
173
160
  License
174
161
  --------
175
162
 
176
163
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
164
+ See [LICENSE.txt](/LICENSE.txt) and [THIRD_PARTY_LICENSES.txt](/THIRD_PARTY_LICENSES.txt) for details.
@@ -0,0 +1,75 @@
1
+ This project includes the following third-party software:
2
+
3
+ ## khashl
4
+
5
+ The MIT License
6
+
7
+ Copyright (c) 2019- by Attractive Chaos <attractor@live.co.uk>
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining
10
+ a copy of this software and associated documentation files (the
11
+ "Software"), to deal in the Software without restriction, including
12
+ without limitation the rights to use, copy, modify, merge, publish,
13
+ distribute, sublicense, and/or sell copies of the Software, and to
14
+ permit persons to whom the Software is furnished to do so, subject to
15
+ the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be
18
+ included in all copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+
29
+ ## libbacktrace
30
+
31
+ Copyright (C) 2012-2016 Free Software Foundation, Inc.
32
+
33
+ Redistribution and use in source and binary forms, with or without
34
+ modification, are permitted provided that the following conditions are
35
+ met:
36
+
37
+ (1) Redistributions of source code must retain the above copyright
38
+ notice, this list of conditions and the following disclaimer.
39
+
40
+ (2) Redistributions in binary form must reproduce the above copyright
41
+ notice, this list of conditions and the following disclaimer in
42
+ the documentation and/or other materials provided with the
43
+ distribution.
44
+
45
+ (3) The name of the author may not be used to
46
+ endorse or promote products derived from this software without
47
+ specific prior written permission.
48
+
49
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52
+ DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
53
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
57
+ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
58
+ IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
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/extconf.rb CHANGED
@@ -1,9 +1,28 @@
1
1
  require 'mkmf'
2
2
  require 'mini_portile2'
3
3
  require 'fileutils'
4
+ require 'optparse'
4
5
 
5
6
  gem_root = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
6
7
 
8
+ options = {
9
+ debug: ENV['PF2_DEBUG'] == '1',
10
+ }
11
+ option_parser = OptionParser.new do |opts|
12
+ opts.on('--debug[=BOOL]') do |debug|
13
+ options[:debug] =
14
+ case debug
15
+ when nil, "true"
16
+ true
17
+ when "false"
18
+ false
19
+ else
20
+ raise OptionParser::InvalidArgument, "Expected true or false for --debug"
21
+ end
22
+ end
23
+ end
24
+ option_parser.parse!(ARGV)
25
+
7
26
  libbacktrace = MiniPortile.new('libbacktrace', '1.0.0')
8
27
  libbacktrace.source_directory = File.join(gem_root, 'vendor', 'libbacktrace')
9
28
  libbacktrace.patch_files = Dir.glob(File.join(gem_root, 'ext', 'patches', 'libbacktrace', '*.patch'))
@@ -28,7 +47,7 @@ end
28
47
 
29
48
  append_ldflags('-lrt') # for timer_create
30
49
  append_cflags('-fvisibility=hidden')
31
- append_cflags('-DPF2_DEBUG') if ENV['PF2_DEBUG'] == '1'
50
+ append_cflags('-DPF2_DEBUG') if options[:debug]
32
51
 
33
52
  # Check for timer functions
34
53
  have_timer_create = have_func('timer_create')