perb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4d147f0306e6a9c9c89cce4dcc30ff7ecdf85fb1c08f4942f234f1b84bff5d58
4
+ data.tar.gz: 7d4e5dbb2011da49b6cd67cd1284afef481f5fe0459bca730393ff1d2fd635e3
5
+ SHA512:
6
+ metadata.gz: 6426988554b37d64140c11697a61e9b72ee19c34aeda5932800489216bda776e8b449a04905bd86bbb9a8aaab325f980a3f08fcb06fda6dd58f89359d051776b
7
+ data.tar.gz: 16ee173e6f09fb4c877271f35b74a8a6f2c982e9ac321607779a87dd1f5f5232f4a1e1f64993dbda97376329bc2e7846cd7d786ea7467c5e34daa932971e5a9c
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Metrics/AbcSize:
17
+ Enabled: false
@@ -0,0 +1,14 @@
1
+ {
2
+ // See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
3
+ // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
4
+
5
+ // List of extensions which should be recommended for users of this workspace.
6
+ "recommendations": [
7
+ "rust-lang.rust-analyzer",
8
+ "shopify.ruby-extensions-pack"
9
+ ],
10
+ // List of extensions recommended by VS Code that should not be recommended for users of this workspace.
11
+ "unwantedRecommendations": [
12
+
13
+ ]
14
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "rust-analyzer.linkedProjects": [
3
+ "ext/perb/Cargo.toml"
4
+ ]
5
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-19
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at marcoc.r@outlook.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in perb.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ group :development do
15
+ gem "ruby-lsp", require: false
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ perb (0.1.0)
5
+ parser (~> 3.1)
6
+ rb_sys (~> 0.9.39)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ json (2.6.3)
13
+ language_server-protocol (3.17.0.2)
14
+ minitest (5.16.3)
15
+ parallel (1.22.1)
16
+ parser (3.1.3.0)
17
+ ast (~> 2.4.1)
18
+ prettier_print (1.1.0)
19
+ rainbow (3.1.1)
20
+ rake (13.0.6)
21
+ rake-compiler (1.2.1)
22
+ rake
23
+ rb_sys (0.9.52)
24
+ regexp_parser (2.6.1)
25
+ rexml (3.2.5)
26
+ rubocop (1.40.0)
27
+ json (~> 2.3)
28
+ parallel (~> 1.10)
29
+ parser (>= 3.1.2.1)
30
+ rainbow (>= 2.2.2, < 4.0)
31
+ regexp_parser (>= 1.8, < 3.0)
32
+ rexml (>= 3.2.5, < 4.0)
33
+ rubocop-ast (>= 1.23.0, < 2.0)
34
+ ruby-progressbar (~> 1.7)
35
+ unicode-display_width (>= 1.4.0, < 3.0)
36
+ rubocop-ast (1.24.0)
37
+ parser (>= 3.1.1.0)
38
+ ruby-lsp (0.3.7)
39
+ language_server-protocol (~> 3.17.0)
40
+ sorbet-runtime
41
+ syntax_tree (>= 4.0.2, < 5.0.0)
42
+ ruby-progressbar (1.11.0)
43
+ sorbet-runtime (0.5.10595)
44
+ syntax_tree (4.3.0)
45
+ prettier_print (>= 1.0.2)
46
+ unicode-display_width (2.3.0)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ minitest (~> 5.0)
53
+ perb!
54
+ rake (~> 13.0)
55
+ rake-compiler (~> 1.2.0)
56
+ rubocop (~> 1.21)
57
+ ruby-lsp
58
+
59
+ BUNDLED WITH
60
+ 2.3.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Marco Concetto Rudilosso
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Perb
2
+
3
+ Perb instruments your methods such that they show up when a ruby executable gets profiled with perf. it works by
4
+ wrapping all methods with:
5
+
6
+ ```ruby
7
+ Perb::wrapper(<some_id>) do
8
+ ... (your method code here)
9
+ end
10
+ ```
11
+
12
+ For each method it JITs a small assembly function which just yields back to your ruby method. The assembly looks like
13
+ this:
14
+
15
+ ```assembly
16
+ push rbp
17
+ mov rbp, rsp
18
+ xor rdi, rdi
19
+ call rb_sys::rb_yield
20
+ leave
21
+ ret
22
+ ```
23
+
24
+ Then, we produce a perf map file (as described [here](https://github.com/torvalds/linux/blob/0513e464f9007b70b96740271a948ca5ab6e7dd7/tools/perf/Documentation/jit-interface.txt)) which maps the address of the JITed assembly to the ruby function we are wrapping. This way perf
25
+ can show ruby function names (and their location in the file system) in its output.
26
+
27
+ Inspired by the work done to do something similar for Python, which is explained [here](https://docs.python.org/zh-cn/dev/howto/perf_profiling.html)
28
+
29
+ ## Installation
30
+
31
+ Install the gem and add to the application's Gemfile by executing:
32
+
33
+ $ bundle add perb
34
+
35
+ If bundler is not being used to manage dependencies, install the gem by executing:
36
+
37
+ $ gem install perb
38
+
39
+ ## Usage
40
+
41
+ In order for this to work, you need ruby to be compiled with `CFLAGS="-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer"`
42
+ and also all your native extensions to do so as well. For rust you want: `RUSTFLAGS="-C force-frame-pointers=yes"`
43
+
44
+ Without this you will see incorrect perf results, as perf won't be able to profile correctly your program.
45
+
46
+ Having done this, you can setup `perb` with `require "perb/setup"` which should be required before all the code that you
47
+ want to be able to profile. See `bin/perf_test` as an example. Then you can just:
48
+
49
+ ```shell
50
+ perf record -g <your-ruby-executable>
51
+ ```
52
+
53
+ and you should be able to profile native and ruby code with one profile.
54
+
55
+ ## Caveats
56
+
57
+ I've noticed that perf drops stack frames when the stack gets really deep, which results in weird results. Given that
58
+ the call graphs that we are recording are fairly convoluted (they also include all the internals of the Ruby VM) this
59
+ can happen quite quickly. You should be able to increase such limit with:
60
+
61
+ ```
62
+ sudo sysctl kernel.perf_event_max_stack=<stack-size>
63
+ ```
64
+
65
+ This currently will break anything that patches `load_iseq` like `bootsnap`.
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/perb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/perb/blob/master/CODE_OF_CONDUCT.md).
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
80
+
81
+ ## Code of Conduct
82
+
83
+ Everyone interacting in the Perb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/perb/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rake/testtask"
4
+ require "rake/extensiontask"
5
+ require "bundler/gem_tasks"
6
+ require "rubocop/rake_task"
7
+
8
+ Rake::ExtensionTask.new("perb") do |c|
9
+ c.lib_dir = "lib/perb"
10
+ end
11
+
12
+ task :dev do
13
+ ENV["RB_SYS_CARGO_PROFILE"] = "dev"
14
+ end
15
+
16
+ ENV["RUSTFLAGS"] = "-C force-frame-pointers=yes"
17
+
18
+ Rake::TestTask.new(:test) do |t|
19
+ t.deps << :dev << :compile
20
+ t.libs << "test"
21
+ t.libs << "lib"
22
+ t.test_files = FileList["test/**/test_*.rb"]
23
+ end
24
+
25
+ RuboCop::RakeTask.new
26
+
27
+ task default: %i[test rubocop]
@@ -0,0 +1 @@
1
+ target/
@@ -0,0 +1,352 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 3
4
+
5
+ [[package]]
6
+ name = "aho-corasick"
7
+ version = "0.7.20"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
10
+ dependencies = [
11
+ "memchr",
12
+ ]
13
+
14
+ [[package]]
15
+ name = "bindgen"
16
+ version = "0.60.1"
17
+ source = "registry+https://github.com/rust-lang/crates.io-index"
18
+ checksum = "062dddbc1ba4aca46de6338e2bf87771414c335f7b2f2036e8f3e9befebf88e6"
19
+ dependencies = [
20
+ "bitflags",
21
+ "cexpr",
22
+ "clang-sys",
23
+ "lazy_static",
24
+ "lazycell",
25
+ "peeking_take_while",
26
+ "proc-macro2",
27
+ "quote",
28
+ "regex",
29
+ "rustc-hash",
30
+ "shlex",
31
+ ]
32
+
33
+ [[package]]
34
+ name = "bitflags"
35
+ version = "1.3.2"
36
+ source = "registry+https://github.com/rust-lang/crates.io-index"
37
+ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
38
+
39
+ [[package]]
40
+ name = "byteorder"
41
+ version = "1.4.3"
42
+ source = "registry+https://github.com/rust-lang/crates.io-index"
43
+ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
44
+
45
+ [[package]]
46
+ name = "cexpr"
47
+ version = "0.6.0"
48
+ source = "registry+https://github.com/rust-lang/crates.io-index"
49
+ checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
50
+ dependencies = [
51
+ "nom",
52
+ ]
53
+
54
+ [[package]]
55
+ name = "cfg-if"
56
+ version = "1.0.0"
57
+ source = "registry+https://github.com/rust-lang/crates.io-index"
58
+ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
59
+
60
+ [[package]]
61
+ name = "clang-sys"
62
+ version = "1.4.0"
63
+ source = "registry+https://github.com/rust-lang/crates.io-index"
64
+ checksum = "fa2e27ae6ab525c3d369ded447057bca5438d86dc3a68f6faafb8269ba82ebf3"
65
+ dependencies = [
66
+ "glob",
67
+ "libc",
68
+ "libloading",
69
+ ]
70
+
71
+ [[package]]
72
+ name = "dynasm"
73
+ version = "1.2.3"
74
+ source = "registry+https://github.com/rust-lang/crates.io-index"
75
+ checksum = "add9a102807b524ec050363f09e06f1504214b0e1c7797f64261c891022dce8b"
76
+ dependencies = [
77
+ "bitflags",
78
+ "byteorder",
79
+ "lazy_static",
80
+ "proc-macro-error",
81
+ "proc-macro2",
82
+ "quote",
83
+ "syn",
84
+ ]
85
+
86
+ [[package]]
87
+ name = "dynasmrt"
88
+ version = "1.2.3"
89
+ source = "registry+https://github.com/rust-lang/crates.io-index"
90
+ checksum = "64fba5a42bd76a17cad4bfa00de168ee1cbfa06a5e8ce992ae880218c05641a9"
91
+ dependencies = [
92
+ "byteorder",
93
+ "dynasm",
94
+ "memmap2",
95
+ ]
96
+
97
+ [[package]]
98
+ name = "glob"
99
+ version = "0.3.0"
100
+ source = "registry+https://github.com/rust-lang/crates.io-index"
101
+ checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
102
+
103
+ [[package]]
104
+ name = "lazy_static"
105
+ version = "1.4.0"
106
+ source = "registry+https://github.com/rust-lang/crates.io-index"
107
+ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
108
+
109
+ [[package]]
110
+ name = "lazycell"
111
+ version = "1.3.0"
112
+ source = "registry+https://github.com/rust-lang/crates.io-index"
113
+ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
114
+
115
+ [[package]]
116
+ name = "libc"
117
+ version = "0.2.138"
118
+ source = "registry+https://github.com/rust-lang/crates.io-index"
119
+ checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
120
+
121
+ [[package]]
122
+ name = "libloading"
123
+ version = "0.7.4"
124
+ source = "registry+https://github.com/rust-lang/crates.io-index"
125
+ checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f"
126
+ dependencies = [
127
+ "cfg-if",
128
+ "winapi",
129
+ ]
130
+
131
+ [[package]]
132
+ name = "magnus"
133
+ version = "0.4.3"
134
+ source = "registry+https://github.com/rust-lang/crates.io-index"
135
+ checksum = "c7d904f55da4e2b0ac7f133257af80580ff4ca17bcca67f51ea06be46aa634ac"
136
+ dependencies = [
137
+ "magnus-macros",
138
+ "rb-sys",
139
+ "rb-sys-env",
140
+ ]
141
+
142
+ [[package]]
143
+ name = "magnus-macros"
144
+ version = "0.2.0"
145
+ source = "registry+https://github.com/rust-lang/crates.io-index"
146
+ checksum = "acc8ba6908cb0f67a4e75cb48fc81a1f0e6a6dd1501936e0c9e2c7c8f9f18e05"
147
+ dependencies = [
148
+ "proc-macro2",
149
+ "quote",
150
+ "syn",
151
+ ]
152
+
153
+ [[package]]
154
+ name = "memchr"
155
+ version = "2.5.0"
156
+ source = "registry+https://github.com/rust-lang/crates.io-index"
157
+ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
158
+
159
+ [[package]]
160
+ name = "memmap2"
161
+ version = "0.5.8"
162
+ source = "registry+https://github.com/rust-lang/crates.io-index"
163
+ checksum = "4b182332558b18d807c4ce1ca8ca983b34c3ee32765e47b3f0f69b90355cc1dc"
164
+ dependencies = [
165
+ "libc",
166
+ ]
167
+
168
+ [[package]]
169
+ name = "minimal-lexical"
170
+ version = "0.2.1"
171
+ source = "registry+https://github.com/rust-lang/crates.io-index"
172
+ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
173
+
174
+ [[package]]
175
+ name = "nom"
176
+ version = "7.1.1"
177
+ source = "registry+https://github.com/rust-lang/crates.io-index"
178
+ checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36"
179
+ dependencies = [
180
+ "memchr",
181
+ "minimal-lexical",
182
+ ]
183
+
184
+ [[package]]
185
+ name = "once_cell"
186
+ version = "1.16.0"
187
+ source = "registry+https://github.com/rust-lang/crates.io-index"
188
+ checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
189
+
190
+ [[package]]
191
+ name = "peeking_take_while"
192
+ version = "0.1.2"
193
+ source = "registry+https://github.com/rust-lang/crates.io-index"
194
+ checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
195
+
196
+ [[package]]
197
+ name = "perb"
198
+ version = "0.1.0"
199
+ dependencies = [
200
+ "dynasmrt",
201
+ "magnus",
202
+ "once_cell",
203
+ "rb-sys",
204
+ ]
205
+
206
+ [[package]]
207
+ name = "proc-macro-error"
208
+ version = "1.0.4"
209
+ source = "registry+https://github.com/rust-lang/crates.io-index"
210
+ checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
211
+ dependencies = [
212
+ "proc-macro-error-attr",
213
+ "proc-macro2",
214
+ "quote",
215
+ "syn",
216
+ "version_check",
217
+ ]
218
+
219
+ [[package]]
220
+ name = "proc-macro-error-attr"
221
+ version = "1.0.4"
222
+ source = "registry+https://github.com/rust-lang/crates.io-index"
223
+ checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
224
+ dependencies = [
225
+ "proc-macro2",
226
+ "quote",
227
+ "version_check",
228
+ ]
229
+
230
+ [[package]]
231
+ name = "proc-macro2"
232
+ version = "1.0.49"
233
+ source = "registry+https://github.com/rust-lang/crates.io-index"
234
+ checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5"
235
+ dependencies = [
236
+ "unicode-ident",
237
+ ]
238
+
239
+ [[package]]
240
+ name = "quote"
241
+ version = "1.0.23"
242
+ source = "registry+https://github.com/rust-lang/crates.io-index"
243
+ checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
244
+ dependencies = [
245
+ "proc-macro2",
246
+ ]
247
+
248
+ [[package]]
249
+ name = "rb-sys"
250
+ version = "0.9.52"
251
+ source = "registry+https://github.com/rust-lang/crates.io-index"
252
+ checksum = "02fffdb0162fc4cc1b6509b2d9b32a75f7e7ed392f58bde639e0c33b23e74b97"
253
+ dependencies = [
254
+ "rb-sys-build",
255
+ ]
256
+
257
+ [[package]]
258
+ name = "rb-sys-build"
259
+ version = "0.9.52"
260
+ source = "registry+https://github.com/rust-lang/crates.io-index"
261
+ checksum = "85d1a236755f879fc155d16d9ba4cd3b1975fd52ef6a28113702ae3881b73c03"
262
+ dependencies = [
263
+ "bindgen",
264
+ "regex",
265
+ "shell-words",
266
+ ]
267
+
268
+ [[package]]
269
+ name = "rb-sys-env"
270
+ version = "0.1.1"
271
+ source = "registry+https://github.com/rust-lang/crates.io-index"
272
+ checksum = "74c38752410925faeb82c400c06ba2fd9ee6aa8f719dd33994c9e53f5242d25f"
273
+
274
+ [[package]]
275
+ name = "regex"
276
+ version = "1.7.0"
277
+ source = "registry+https://github.com/rust-lang/crates.io-index"
278
+ checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
279
+ dependencies = [
280
+ "aho-corasick",
281
+ "memchr",
282
+ "regex-syntax",
283
+ ]
284
+
285
+ [[package]]
286
+ name = "regex-syntax"
287
+ version = "0.6.28"
288
+ source = "registry+https://github.com/rust-lang/crates.io-index"
289
+ checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
290
+
291
+ [[package]]
292
+ name = "rustc-hash"
293
+ version = "1.1.0"
294
+ source = "registry+https://github.com/rust-lang/crates.io-index"
295
+ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
296
+
297
+ [[package]]
298
+ name = "shell-words"
299
+ version = "1.1.0"
300
+ source = "registry+https://github.com/rust-lang/crates.io-index"
301
+ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
302
+
303
+ [[package]]
304
+ name = "shlex"
305
+ version = "1.1.0"
306
+ source = "registry+https://github.com/rust-lang/crates.io-index"
307
+ checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
308
+
309
+ [[package]]
310
+ name = "syn"
311
+ version = "1.0.107"
312
+ source = "registry+https://github.com/rust-lang/crates.io-index"
313
+ checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
314
+ dependencies = [
315
+ "proc-macro2",
316
+ "quote",
317
+ "unicode-ident",
318
+ ]
319
+
320
+ [[package]]
321
+ name = "unicode-ident"
322
+ version = "1.0.6"
323
+ source = "registry+https://github.com/rust-lang/crates.io-index"
324
+ checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
325
+
326
+ [[package]]
327
+ name = "version_check"
328
+ version = "0.9.4"
329
+ source = "registry+https://github.com/rust-lang/crates.io-index"
330
+ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
331
+
332
+ [[package]]
333
+ name = "winapi"
334
+ version = "0.3.9"
335
+ source = "registry+https://github.com/rust-lang/crates.io-index"
336
+ checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
337
+ dependencies = [
338
+ "winapi-i686-pc-windows-gnu",
339
+ "winapi-x86_64-pc-windows-gnu",
340
+ ]
341
+
342
+ [[package]]
343
+ name = "winapi-i686-pc-windows-gnu"
344
+ version = "0.4.0"
345
+ source = "registry+https://github.com/rust-lang/crates.io-index"
346
+ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
347
+
348
+ [[package]]
349
+ name = "winapi-x86_64-pc-windows-gnu"
350
+ version = "0.4.0"
351
+ source = "registry+https://github.com/rust-lang/crates.io-index"
352
+ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
@@ -0,0 +1,18 @@
1
+ [package]
2
+ name = "perb"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+
6
+ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
+
8
+ [lib]
9
+ crate-type = ["cdylib"]
10
+
11
+ [dependencies]
12
+ magnus = { version = "0.4", features = ["rb-sys-interop"] }
13
+ rb-sys = "0.9.52"
14
+ dynasmrt = "1"
15
+ once_cell = "1"
16
+
17
+ [profile.release]
18
+ debug = true
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+ require "rb_sys/mkmf"
5
+
6
+ create_rust_makefile("perb/perb")
@@ -0,0 +1,70 @@
1
+ use std::{io::Write, mem, sync::RwLock};
2
+
3
+ use dynasmrt::{dynasm, AssemblyOffset, DynasmApi};
4
+ use magnus::{define_module, function, rb_sys::FromRawValue};
5
+ use once_cell::sync::Lazy;
6
+
7
+ static ASSEMBLER: Lazy<RwLock<dynasmrt::x64::Assembler>> =
8
+ Lazy::new(|| RwLock::new(dynasmrt::x64::Assembler::new().unwrap()));
9
+
10
+ fn build_wrapper(fn_name: String) -> usize {
11
+ let mut ops = ASSEMBLER.write().unwrap();
12
+ let asm_wrapper = ops.offset();
13
+
14
+ dynasm!(ops
15
+ ; push rbp
16
+ ; mov rbp, rsp
17
+ ; xor rdi, rdi
18
+ ; mov rax, QWORD rb_sys::rb_yield as _
19
+ ; call rax
20
+ ; leave
21
+ ; ret
22
+ );
23
+
24
+ let end_offset = AssemblyOffset(ops.offset().0 - 1);
25
+
26
+ ops.commit().unwrap();
27
+
28
+ let executor = ops.reader();
29
+ let buf = executor.lock();
30
+
31
+ let file_name = format!("/tmp/perf-{}.map", std::process::id());
32
+ let file = std::fs::OpenOptions::new()
33
+ .append(true)
34
+ .create(true)
35
+ .open(file_name)
36
+ .unwrap();
37
+
38
+ let mut line_writer = std::io::LineWriter::new(file);
39
+ let len = unsafe { buf.ptr(end_offset).offset_from(buf.ptr(asm_wrapper)) };
40
+ let perf_map = format!(
41
+ "{:x} {:x} {}\n",
42
+ buf.ptr(asm_wrapper) as usize,
43
+ len,
44
+ fn_name
45
+ );
46
+ line_writer.write_all(perf_map.as_bytes()).unwrap();
47
+
48
+ asm_wrapper.0
49
+ }
50
+
51
+ fn wrapper(fn_pointer: usize) -> magnus::Value {
52
+ let fn_pointer = ASSEMBLER
53
+ .read()
54
+ .unwrap()
55
+ .reader()
56
+ .lock()
57
+ .ptr(AssemblyOffset(fn_pointer));
58
+ unsafe {
59
+ let asm_wrapper_fn: extern "C" fn() -> rb_sys::Value = mem::transmute(fn_pointer);
60
+ magnus::Value::from_raw(asm_wrapper_fn())
61
+ }
62
+ }
63
+
64
+ #[magnus::init]
65
+ fn init() -> Result<(), magnus::Error> {
66
+ let module = define_module("Perb")?;
67
+ module.define_module_function("wrapper", function!(wrapper, 1))?;
68
+ module.define_module_function("build_wrapper", function!(build_wrapper, 1))?;
69
+ Ok(())
70
+ }
data/lib/perb/setup.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../perb"
4
+
5
+ class << RubyVM::InstructionSequence
6
+ prepend(Perb::InstructionSequenceExt)
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Perb
4
+ VERSION = "0.1.0"
5
+ end
data/lib/perb.rb ADDED
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "perb/version"
4
+ require_relative "perb/perb"
5
+ require "parser/current"
6
+
7
+ module Perb
8
+ # This is a tree rewriter that will insert the wrapper code around
9
+ # every method definition.
10
+ class Rewriter < Parser::TreeRewriter
11
+ def on_def(node)
12
+ line = node.location.line || "line?"
13
+ column = node.location.column || "column?"
14
+ method_name = node.children[0] || "*unknown*"
15
+ file_name = @source_rewriter.source_buffer.name
16
+ method_info = "#{method_name} @ #{file_name}:#{line}:#{column}"
17
+
18
+ block = node.children[2]
19
+ return if block.nil?
20
+
21
+ insert_before(block.loc.expression, "Perb::wrapper(#{Perb.build_wrapper(method_info)}) do\n")
22
+ insert_after(block.loc.expression, "\nend")
23
+ end
24
+ end
25
+ end
26
+
27
+ module Perb
28
+ # Extension module for RubyVM::InstructionSequence which patches
29
+ # the load_iseq method to use the Perb rewriter.
30
+ module InstructionSequenceExt
31
+ def load_iseq(path)
32
+ source = Parser::Source::Buffer.new(path).read
33
+ parser = Parser::CurrentRuby.new
34
+ rewriter = Perb::Rewriter.new
35
+
36
+ ast = parser.parse(source)
37
+ source = rewriter.rewrite(source, ast)
38
+ RubyVM::InstructionSequence.compile(source)
39
+ end
40
+ end
41
+ end
42
+
43
+ module Perb
44
+ class Error < StandardError; end
45
+ end
data/sig/perb.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Perb
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: perb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marco Concetto Rudilosso
8
+ - Giovanni Borgh
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2022-12-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: parser
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.1'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.1'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rb_sys
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.9.39
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.9.39
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake-compiler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 1.2.0
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 1.2.0
56
+ description: perb allows perf to profile ruby
57
+ email:
58
+ - marcoc.r@outlook.com
59
+ executables: []
60
+ extensions:
61
+ - ext/perb/extconf.rb
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".rubocop.yml"
65
+ - ".vscode/extensions.json"
66
+ - ".vscode/settings.json"
67
+ - CHANGELOG.md
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - ext/perb/.gitignore
75
+ - ext/perb/Cargo.lock
76
+ - ext/perb/Cargo.toml
77
+ - ext/perb/extconf.rb
78
+ - ext/perb/src/lib.rs
79
+ - lib/perb.rb
80
+ - lib/perb/setup.rb
81
+ - lib/perb/version.rb
82
+ - sig/perb.rbs
83
+ homepage: https://github.com/Maaarcocr/perb
84
+ licenses:
85
+ - MIT
86
+ metadata:
87
+ homepage_uri: https://github.com/Maaarcocr/perb
88
+ source_code_uri: https://github.com/Maaarcocr/perb
89
+ changelog_uri: https://github.com/Maaarcocr/perb/blob/main/CHANGELOG.md
90
+ rubygems_mfa_required: 'true'
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 2.6.0
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.3.26
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: perb allows perf to profile ruby
110
+ test_files: []