|
| 1 | +# Corkscrew |
| 2 | + |
| 3 | +Corkscrew is a Ruby causal-profiling toolkit for bottleneck experiments. It |
| 4 | +records progress points, line samples, latency spans, Ruby-level wait targets, |
| 5 | +and validation benchmark results, then reports virtual speedup curves for likely |
| 6 | +bottlenecks. |
| 7 | + |
| 8 | +The gem includes a portable Ruby engine and a native CRuby extension for thread |
| 9 | +hooks, GC hooks, delay accounting, sampling handoff, stamp inheritance, native |
| 10 | +monitor ticks, native sample ring flushes, thread-state gauges, adaptive |
| 11 | +line/wait experiment rounds, and runtime snapshots. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +Install the gem and add it to the application's Gemfile by executing: |
| 16 | + |
| 17 | +```sh |
| 18 | +bundle add corkscrew |
| 19 | +``` |
| 20 | + |
| 21 | +If Bundler is not being used to manage dependencies, install the gem by |
| 22 | +executing: |
| 23 | + |
| 24 | +```sh |
| 25 | +gem install corkscrew |
| 26 | +``` |
| 27 | + |
| 28 | +Corkscrew requires CRuby 3.3 or newer. The native extension is compiled during |
| 29 | +installation when the required CRuby APIs are available; otherwise the Ruby |
| 30 | +engine remains the portable fallback. |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +Profile an existing Ruby command with the CLI: |
| 35 | + |
| 36 | +```sh |
| 37 | +corkscrew run --repeat 3 --targets both --output run.cork.ndjson -- ruby app.rb |
| 38 | +``` |
| 39 | + |
| 40 | +Generate reports from the recorded NDJSON file: |
| 41 | + |
| 42 | +```sh |
| 43 | +corkscrew report --html report.html --firefox profile.json run.cork.ndjson |
| 44 | +``` |
| 45 | + |
| 46 | +`--firefox` writes an aggregate Firefox Profiler JSON with thread, sample, |
| 47 | +stack, frame, function, marker, and string tables. It includes target summary |
| 48 | +markers and round timeline markers derived from Corkscrew's NDJSON records. |
| 49 | + |
| 50 | +### Progress Points |
| 51 | + |
| 52 | +Application code can provide throughput and latency points: |
| 53 | + |
| 54 | +```ruby |
| 55 | +require "corkscrew" |
| 56 | + |
| 57 | +Corkscrew.latency_begin(:request) |
| 58 | +do_work |
| 59 | +Corkscrew.progress(:request) |
| 60 | +Corkscrew.latency_end(:request) |
| 61 | +``` |
| 62 | + |
| 63 | +`--targets lines` records line targets. `--targets waits` or `--targets both` |
| 64 | +also enables Ruby-level wrappers for `Mutex`, `Thread::Queue`, |
| 65 | +`Thread::SizedQueue`, `Thread::ConditionVariable`, `Kernel.sleep`, and `IO`. |
| 66 | + |
| 67 | +### Native Signal Source |
| 68 | + |
| 69 | +The default signal source is Ruby's `setitimer` path. The native monitor can |
| 70 | +deliver `SIGPROF` to the registered profiling thread by setting |
| 71 | +`CORKSCREW_NATIVE_SIGNALS=1`; this path is recorded in native counters and kept |
| 72 | +opt-in because direct pthread signal delivery can conflict with platform signal |
| 73 | +handling in embedded Ruby processes. |
| 74 | + |
| 75 | +## Validation |
| 76 | + |
| 77 | +Run the bundled validation harness: |
| 78 | + |
| 79 | +```sh |
| 80 | +corkscrew validate --quick |
| 81 | +corkscrew validate |
| 82 | +``` |
| 83 | + |
| 84 | +The validation harness includes benchmark manifests for serial CPU hotspots, |
| 85 | +false hotspots under concurrency, I/O wait, lock contention, GVL contention, GC |
| 86 | +pressure, queue pipeline behavior, and native-call attribution smoke coverage. |
| 87 | + |
| 88 | +## Development |
| 89 | + |
| 90 | +After checking out the repo, install dependencies: |
| 91 | + |
| 92 | +```sh |
| 93 | +bundle install |
| 94 | +``` |
| 95 | + |
| 96 | +Compile the native extension and run the test suite: |
| 97 | + |
| 98 | +```sh |
| 99 | +bundle exec rake compile |
| 100 | +bundle exec rake |
| 101 | +``` |
| 102 | + |
| 103 | +Install the gem locally from the checkout: |
| 104 | + |
| 105 | +```sh |
| 106 | +bundle exec rake install |
| 107 | +``` |
| 108 | + |
| 109 | +Run validation locally: |
| 110 | + |
| 111 | +```sh |
| 112 | +ruby -Ilib exe/corkscrew validate --quick |
| 113 | +ruby -Ilib exe/corkscrew validate |
| 114 | +python3 validate/harness/stats_check.py |
| 115 | +``` |
| 116 | + |
| 117 | +For local CLI testing without installing the gem, use: |
| 118 | + |
| 119 | +```sh |
| 120 | +ruby -Ilib exe/corkscrew run --repeat 1 --output run.cork.ndjson -- ruby app.rb |
| 121 | +``` |
| 122 | + |
| 123 | +## Contributing |
| 124 | + |
| 125 | +Bug reports and pull requests are welcome. Please include a focused reproduction |
| 126 | +or validation benchmark when changing profiling behavior. |
| 127 | + |
| 128 | +## License |
| 129 | + |
| 130 | +The gem is available as open source under the terms of the MIT License. |
0 commit comments