pid_cache 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +12 -0
- data/lib/pid_cache/version.rb +5 -0
- data/lib/pid_cache.rb +33 -0
- data/pid_cache.gemspec +32 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4e6f4e7428e8b05e3e495a03e7f33d61630aec11f70f91da83500b417fa9d5fe
|
4
|
+
data.tar.gz: 03bed860fc75b2d20bbfd5363fbaf2ef93d592238c7a1efa8c582a25f2ea0758
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9319cc3774946d7bed55faf94055a8e9903fd8a29aa7355a94de1e38982e60841f6e9b2d106349a3adca63244d9d3c32641297bea8f1bdf66ba54b619433bb44
|
7
|
+
data.tar.gz: a1f4edf40740a435315f756336998f89f4993b7bb27e7dc12d94688bc7f52a9fbb05ecca48aa12fc02a57f74a94a319682643e5e07246487b9276752523e8672
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pid_cache (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.17.0)
|
10
|
+
rake (13.0.6)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
arm64-darwin-21
|
14
|
+
arm64-darwin-22
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
minitest (~> 5.0)
|
18
|
+
pid_cache!
|
19
|
+
rake (~> 13.0)
|
20
|
+
|
21
|
+
BUNDLED WITH
|
22
|
+
2.4.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Shopify
|
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,51 @@
|
|
1
|
+
# PIDCache
|
2
|
+
|
3
|
+
Cache calls to `Process.pid` to avoid lots of useless system calls on modern Linux.
|
4
|
+
|
5
|
+
Ruby's `Process.pid` calls the standard `libc` `getpid(2)`, historically pretty much all
|
6
|
+
implementations of the `libc` have been caching this function to avoid doing a syscall everytime.
|
7
|
+
|
8
|
+
However `glibc 2.25` released in 2017 removed that cache, causing a performance regression for application
|
9
|
+
frequently monitoring the PID.
|
10
|
+
|
11
|
+
The reason they removed it, is that some low-level libraries would `fork` by calling the syscall themselves,
|
12
|
+
bypassing the code responsible for clearing the cache.
|
13
|
+
|
14
|
+
But doing this in Ruby is impossible, so we can safely cache the PID and save some performance.
|
15
|
+
|
16
|
+
This gem is a backport of [a proposed feature for Ruby 3.3](https://bugs.ruby-lang.org/issues/19443), hopefully
|
17
|
+
as of Ruby 3.3 this gem may be useless.
|
18
|
+
|
19
|
+
## Requirements
|
20
|
+
|
21
|
+
This gem only work on Ruby 3.1+, on older rubies it has no effect.
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Install the gem and add to the application's Gemfile by executing:
|
26
|
+
|
27
|
+
$ bundle add pid_cache
|
28
|
+
|
29
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
30
|
+
|
31
|
+
$ gem install pid_cache
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
That's it, the cache is applied immediately uppon requiring the gem, and is automatically flushed upon fork.
|
36
|
+
|
37
|
+
Note that the caching only applies to `Process.pid`, the `$$` magic variable is unimpacted.
|
38
|
+
|
39
|
+
## Development
|
40
|
+
|
41
|
+
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.
|
42
|
+
|
43
|
+
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).
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/pid_cache.
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/pid_cache.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "pid_cache/version"
|
4
|
+
|
5
|
+
module PIDCache
|
6
|
+
if Process.respond_to?(:_fork) # Ruby 3.1+
|
7
|
+
ORIGINAL_METHOD = Process.method(:pid)
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def pid
|
11
|
+
@pid ||= ORIGINAL_METHOD.call
|
12
|
+
end
|
13
|
+
|
14
|
+
def reset
|
15
|
+
@pid = nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module CoreExt
|
20
|
+
def _fork
|
21
|
+
child_pid = super
|
22
|
+
::PIDCache.reset if child_pid == 0
|
23
|
+
child_pid
|
24
|
+
end
|
25
|
+
|
26
|
+
def pid
|
27
|
+
::PIDCache.pid
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Process.singleton_class.prepend(PIDCache::CoreExt)
|
32
|
+
end
|
33
|
+
end
|
data/pid_cache.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/pid_cache/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "pid_cache"
|
7
|
+
spec.version = PIDCache::VERSION
|
8
|
+
spec.authors = ["Jean Boussier"]
|
9
|
+
spec.email = ["jean.boussier@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Cache calls to Process.pid"
|
12
|
+
spec.description = "On platforms using glibc 1.25+ Process.pid always issue a syscall which is wasteful. pid_cache prevents that."
|
13
|
+
spec.homepage = "https://gtihub.com/Shopify/pid_cache"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
20
|
+
spec.metadata["changelog_uri"] = File.join(spec.homepage, "blob/master/CHANGELOG.md")
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(__dir__) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pid_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jean Boussier
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-02-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: On platforms using glibc 1.25+ Process.pid always issue a syscall which
|
14
|
+
is wasteful. pid_cache prevents that.
|
15
|
+
email:
|
16
|
+
- jean.boussier@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/pid_cache.rb
|
29
|
+
- lib/pid_cache/version.rb
|
30
|
+
- pid_cache.gemspec
|
31
|
+
homepage: https://gtihub.com/Shopify/pid_cache
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
metadata:
|
35
|
+
allowed_push_host: https://rubygems.org
|
36
|
+
homepage_uri: https://gtihub.com/Shopify/pid_cache
|
37
|
+
source_code_uri: https://gtihub.com/Shopify/pid_cache
|
38
|
+
changelog_uri: https://gtihub.com/Shopify/pid_cache/blob/master/CHANGELOG.md
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.4.4
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Cache calls to Process.pid
|
58
|
+
test_files: []
|