yabeda-jemalloc 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 34cf4df8c852318316b430e722a61c47428bd9356e3babb436b1e46991484847
4
+ data.tar.gz: 6b98e646b0dcb22aa0041123765e8540b867b500219ec06cbbbede103a29df8b
5
+ SHA512:
6
+ metadata.gz: 542b28ac7dc2b44c6c072cdf55e9fe4c7e9c11eb3ac3e3789c1946c62d2c75ac18af07a05b528e81e9d644adcbb4278dda33df525dbca7fa824893bd216e5db4
7
+ data.tar.gz: fa49fd16af6857405c0d80f6aa7cf35ba4a4a9bcf23ee93e112d02244388e3d9446dc17a67370d8b208f0629173bdf79fadef6aaf9b0088263fbaf65a9d450c4
@@ -0,0 +1,35 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ "main" ]
6
+ pull_request:
7
+ branches: [ "main" ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Publish
12
+ runs-on: ubuntu-latest
13
+ permissions:
14
+ contents: read
15
+ packages: write
16
+
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - name: Set up Ruby 2.6
20
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
21
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
22
+ uses: ruby/setup-ruby@v1
23
+ with:
24
+ ruby-version: 3.2.2
25
+
26
+ - name: Publish to RubyGems
27
+ run: |
28
+ mkdir -p $HOME/.gem
29
+ touch $HOME/.gem/credentials
30
+ chmod 0600 $HOME/.gem/credentials
31
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
32
+ gem build *.gemspec
33
+ gem push *.gem
34
+ env:
35
+ GEM_HOST_API_KEY: "${{secrets.GEM_HOST_API_KEY}}"
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in yabeda-jemalloc.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yabeda-jemalloc (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.17)
16
+ rake (~> 10.0)
17
+ yabeda-jemalloc!
18
+
19
+ BUNDLED WITH
20
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Jon David Schober
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,193 @@
1
+ # Yabeda::Jemalloc
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/yabeda-jemalloc.svg)](https://badge.fury.io/rb/yabeda-jemalloc)
4
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.txt)
5
+
6
+ A [Yabeda](https://github.com/yabeda-rb/yabeda) plugin that exposes [jemalloc](http://jemalloc.net/) memory allocation statistics for Ruby applications. This gem uses FFI to interface with jemalloc's `mallctl` API and exports key memory metrics to Prometheus or other monitoring systems.
7
+
8
+ ## Why Use This Gem?
9
+
10
+ Jemalloc is a high-performance memory allocator that provides detailed memory statistics. When running Ruby applications with jemalloc, you can gain valuable insights into:
11
+
12
+ - **Memory fragmentation**: Understand the difference between allocated vs. resident memory
13
+ - **Virtual memory usage**: Track total mapped memory by jemalloc
14
+ - **Active allocations**: Monitor memory backing live allocations
15
+
16
+ This is especially useful for production Ruby applications where memory management and leak detection are critical.
17
+
18
+ ## Requirements
19
+
20
+ - Ruby 2.6 or higher
21
+ - [jemalloc](http://jemalloc.net/) installed and preloaded via `LD_PRELOAD`
22
+ - [Yabeda](https://github.com/yabeda-rb/yabeda) gem
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'yabeda-jemalloc'
30
+ gem 'yabeda-prometheus' # or another exporter
31
+ ```
32
+
33
+ And then execute:
34
+
35
+ ```bash
36
+ $ bundle install
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### 1. Install jemalloc
42
+
43
+ First, ensure jemalloc is installed on your system:
44
+
45
+ **Ubuntu/Debian:**
46
+ ```bash
47
+ sudo apt-get install libjemalloc2
48
+ ```
49
+
50
+ **macOS:**
51
+ ```bash
52
+ brew install jemalloc
53
+ ```
54
+
55
+ **Alpine Linux:**
56
+ ```bash
57
+ apk add jemalloc
58
+ ```
59
+
60
+ ### 2. Configure Your Application
61
+
62
+ The gem automatically registers metrics when jemalloc is detected. Simply require it in your application:
63
+
64
+ ```ruby
65
+ require 'yabeda/jemalloc'
66
+ ```
67
+
68
+ For Rails applications, add to your `config/initializers/yabeda.rb`:
69
+
70
+ ```ruby
71
+ require 'yabeda/prometheus'
72
+ require 'yabeda/jemalloc'
73
+ ```
74
+
75
+ ### 3. Run with jemalloc Preloaded
76
+
77
+ You need to preload jemalloc when starting your Ruby application:
78
+
79
+ ```bash
80
+ LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 bundle exec rails server
81
+ ```
82
+
83
+ Or for other Ruby applications:
84
+
85
+ ```bash
86
+ LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 ruby your_app.rb
87
+ ```
88
+
89
+ **Docker Example:**
90
+
91
+ ```dockerfile
92
+ FROM ruby:3.3
93
+ RUN apt-get update && apt-get install -y libjemalloc2
94
+ ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2
95
+ # ... rest of your Dockerfile
96
+ ```
97
+
98
+ ### 4. Access Metrics
99
+
100
+ If using `yabeda-prometheus`, metrics will be available at your configured metrics endpoint (typically `/metrics`):
101
+
102
+ ```
103
+ # HELP jemalloc_mapped_bytes Total virtual bytes mapped by jemalloc
104
+ # TYPE jemalloc_mapped_bytes gauge
105
+ jemalloc_mapped_bytes 2147483648
106
+
107
+ # HELP jemalloc_resident_bytes Resident bytes (RSS) that belong to jemalloc
108
+ # TYPE jemalloc_resident_bytes gauge
109
+ jemalloc_resident_bytes 1073741824
110
+
111
+ # HELP jemalloc_active_bytes Bytes backing live allocations (user + internal)
112
+ # TYPE jemalloc_active_bytes gauge
113
+ jemalloc_active_bytes 536870912
114
+
115
+ # HELP jemalloc_allocated_bytes Bytes actually allocated by the app
116
+ # TYPE jemalloc_allocated_bytes gauge
117
+ jemalloc_allocated_bytes 268435456
118
+ ```
119
+
120
+ ## Exposed Metrics
121
+
122
+ | Metric Name | Type | Description |
123
+ |-------------|------|-------------|
124
+ | `jemalloc_mapped_bytes` | gauge | Total number of bytes in active extents mapped by the allocator |
125
+ | `jemalloc_resident_bytes` | gauge | Maximum number of bytes in physically resident data pages mapped by the allocator |
126
+ | `jemalloc_active_bytes` | gauge | Total number of bytes in active pages allocated by the application |
127
+ | `jemalloc_allocated_bytes` | gauge | Total number of bytes allocated by the application |
128
+
129
+ ### Understanding the Metrics
130
+
131
+ - **Mapped**: Virtual memory allocated by jemalloc (highest number)
132
+ - **Resident**: Physical memory (RAM) actually used (RSS)
133
+ - **Active**: Memory pages with at least one allocation
134
+ - **Allocated**: Memory requested by your application (lowest number)
135
+
136
+ The difference between these metrics helps identify memory fragmentation and over-allocation issues.
137
+
138
+ ## Configuration
139
+
140
+ The gem automatically configures itself when jemalloc is detected via `LD_PRELOAD`. No additional configuration is required.
141
+
142
+ If jemalloc is not preloaded, the gem will silently skip metric registration, allowing you to safely include it in environments where jemalloc may not be available.
143
+
144
+ ## Troubleshooting
145
+
146
+ ### Metrics not appearing?
147
+
148
+ 1. Verify jemalloc is preloaded:
149
+ ```bash
150
+ echo $LD_PRELOAD # should include libjemalloc.so.2
151
+ ```
152
+
153
+ 2. Check if jemalloc is actually being used:
154
+ ```bash
155
+ ldd $(which ruby) | grep jemalloc
156
+ ```
157
+
158
+ 3. Ensure Yabeda is properly configured with an exporter (e.g., `yabeda-prometheus`)
159
+
160
+ ### Wrong jemalloc path?
161
+
162
+ The path to `libjemalloc.so.2` varies by system. Find it with:
163
+
164
+ ```bash
165
+ find /usr -name "libjemalloc.so*" 2>/dev/null
166
+ ```
167
+
168
+ Then use the full path in your `LD_PRELOAD`.
169
+
170
+ ## Development
171
+
172
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
173
+
174
+ To install this gem onto your local machine, run `bundle exec rake install`.
175
+
176
+ ## Contributing
177
+
178
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jondavidschober/yabeda-jemalloc.
179
+
180
+ ## License
181
+
182
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
183
+
184
+ ## Credits
185
+
186
+ Created by [Jon David Schober](https://github.com/jondavidschober) at [Datavine](https://getdatanamic.com).
187
+
188
+ ## See Also
189
+
190
+ - [Yabeda](https://github.com/yabeda-rb/yabeda) - Extensible framework for collecting metrics
191
+ - [jemalloc](http://jemalloc.net/) - General purpose malloc implementation
192
+ - [yabeda-prometheus](https://github.com/yabeda-rb/yabeda-prometheus) - Prometheus exporter for Yabeda
193
+ - [Observability is the Eyes and Ears of a Service](dev.datavine.com/observability-is-the-eyes-and-ears-of-a-service)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yabeda/jemalloc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+ module Yabeda::Jemalloc::JemallocFFI
5
+ if ENV['LD_PRELOAD']&.include?('libjemalloc.so.2')
6
+ extend FFI::Library
7
+ ffi_lib 'libjemalloc.so.2' # uses the already-preloaded jemalloc.so
8
+
9
+ attach_function :mallctl, %i[string pointer pointer pointer size_t], :int
10
+
11
+ SIZE_T = FFI::Type::ULONG_LONG.size # works on 64-bit only
12
+
13
+ def self.refresh!
14
+ epoch = FFI::MemoryPointer.new(:ulong_long)
15
+ sz = FFI::MemoryPointer.new(:size_t).write_ulong_long(SIZE_T)
16
+ mallctl('epoch', epoch, sz, epoch, SIZE_T) # epoch++ refreshes stats
17
+ end
18
+
19
+ def self.uint64(name)
20
+ buf = FFI::MemoryPointer.new(:ulong_long)
21
+ sz = FFI::MemoryPointer.new(:size_t).write_ulong_long(SIZE_T)
22
+ mallctl(name, buf, sz, nil, 0)
23
+ buf.read_ulong_long
24
+ end
25
+
26
+ def self.dump(path)
27
+ path_ptr = FFI::MemoryPointer.from_string(path)
28
+ path_arg = FFI::MemoryPointer.new(:pointer)
29
+ path_arg.write_pointer(path_ptr)
30
+ ret = Jemalloc.mallctl('prof.dump', nil, nil, path_arg, FFI::Type::POINTER.size)
31
+
32
+ raise "mallctl failed with error code #{ret}" if ret != 0
33
+
34
+ puts "Heap profile dumped to #{path || 'default path'}"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Yabeda
2
+ module Jemalloc
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yabeda/jemalloc/version'
4
+
5
+ module Yabeda
6
+ module Jemalloc
7
+ Yabeda.configure do
8
+ if ENV['LD_PRELOAD']&.include?('libjemalloc.so.2')
9
+ gauge :jemalloc_mapped_bytes,
10
+ comment: 'Total virtual bytes mapped by jemalloc'
11
+ gauge :jemalloc_resident_bytes,
12
+ comment: 'Resident bytes (RSS) that belong to jemalloc'
13
+ gauge :jemalloc_active_bytes,
14
+ comment: 'Bytes backing live allocations (user + internal)'
15
+ gauge :jemalloc_allocated_bytes,
16
+ comment: 'Bytes actually allocated by the app'
17
+ collect do
18
+ JemallocFFI.refresh!
19
+ jemalloc_mapped_bytes.set({}, JemallocFFI.uint64('stats.mapped'))
20
+ jemalloc_resident_bytes.set({}, JemallocFFI.uint64('stats.resident'))
21
+ jemalloc_active_bytes.set({}, JemallocFFI.uint64('stats.active'))
22
+ jemalloc_allocated_bytes.set({}, JemallocFFI.uint64('stats.allocated'))
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "yabeda/jemalloc/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yabeda-jemalloc"
8
+ spec.version = Yabeda::Jemalloc::VERSION
9
+ spec.authors = ["Jon David Schober"]
10
+ spec.email = ["jon@getdatanamic.com"]
11
+
12
+ spec.summary = %q{Yabeda plugin for monitoring jemalloc stats}
13
+ spec.description = %q{Yabeda plugin that exposes the jemalloc stats extracted from an FFI to prometheus}
14
+ spec.homepage = "https://github.com/jondavidschober/yabeda-jemalloc"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/jondavidschober/yabeda-jemalloc"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.17"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_dependency "yabeda", "~> 0.6"
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yabeda-jemalloc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon David Schober
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yabeda
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
55
+ description: Yabeda plugin that exposes the jemalloc stats extracted from an FFI to
56
+ prometheus
57
+ email:
58
+ - jon@getdatanamic.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".github/workflows/gem-push.yml"
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/yabeda/jemalloc.rb
73
+ - lib/yabeda/jemalloc/ffi.rb
74
+ - lib/yabeda/jemalloc/version.rb
75
+ - yabeda-jemalloc.gemspec
76
+ homepage: https://github.com/jondavidschober/yabeda-jemalloc
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/jondavidschober/yabeda-jemalloc
81
+ source_code_uri: https://github.com/jondavidschober/yabeda-jemalloc
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.4.10
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Yabeda plugin for monitoring jemalloc stats
101
+ test_files: []