jemalloc_ctl 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: 9ab59d11b2bfb3adb1e40476ed0902532eae1436edf9b0e31f64be2dd4f3d8da
4
+ data.tar.gz: d771262a02cf483a43a85c3d0d643a499e5c5f1cde09bf4be70a5678995010bd
5
+ SHA512:
6
+ metadata.gz: 86e68a6ed91e73633fb5cbaa416152d374f266de91ff564e49145e62aa4cd766478b3d626640a4c53a55f5e30d8e47e73acc06d77af641e2fe205d32d5b0e418
7
+ data.tar.gz: 865191340c23cfb157aa6f74800cf78ee25a21c611d3999813eaec291419cc6ccb9f4de9f6ff52b8d9bffc44ccc0c6aab4e5d71f91174a674d4493a68c32c860
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Luke Gruber
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README ADDED
@@ -0,0 +1 @@
1
+ Configure jemalloc at runtime from Ruby.
@@ -0,0 +1,6 @@
1
+ require "mkmf"
2
+
3
+ have_header("dlfcn.h")
4
+ have_library("dl", "dlsym")
5
+
6
+ create_makefile("jemalloc_ctl/jemalloc_ctl")
@@ -0,0 +1,116 @@
1
+ #include <ruby.h>
2
+ #include <dlfcn.h>
3
+ #include <stdbool.h>
4
+
5
+ typedef int (*mallctl_fn)(const char *, void *, size_t *, void *, size_t);
6
+
7
+ static mallctl_fn my_mallctl = NULL;
8
+
9
+ #define JEMALLOC_CTL_DLOPEN_FALLBACK 0
10
+
11
+ #if JEMALLOC_CTL_DLOPEN_FALLBACK > 0
12
+ // used for testing
13
+ static const char *jemalloc_lib_names[] = {
14
+ #ifdef __APPLE__
15
+ "libjemalloc.2.dylib",
16
+ "libjemalloc.dylib",
17
+ "/usr/local/lib/libjemalloc.2.dylib",
18
+ "/usr/local/lib/libjemalloc.dylib",
19
+ "/opt/homebrew/lib/libjemalloc.2.dylib",
20
+ "/opt/homebrew/lib/libjemalloc.dylib",
21
+ #else
22
+ "libjemalloc.so.2",
23
+ "libjemalloc.so",
24
+ #endif
25
+ NULL
26
+ };
27
+ #endif
28
+
29
+ static VALUE rb_mJemallocCtl;
30
+
31
+ static VALUE rb_jemalloc_ctl_enabled_p(VALUE self)
32
+ {
33
+ return my_mallctl != NULL ? Qtrue : Qfalse;
34
+ }
35
+
36
+ static VALUE rb_jemalloc_ctl_version(VALUE self)
37
+ {
38
+ if (my_mallctl == NULL)
39
+ rb_raise(rb_eRuntimeError, "jemalloc is not available");
40
+
41
+ const char *version;
42
+ size_t sz = sizeof(version);
43
+ int err = my_mallctl("version", &version, &sz, NULL, 0);
44
+
45
+ if (err != 0)
46
+ rb_raise(rb_eRuntimeError, "mallctl(\"version\") failed with error %d", err);
47
+
48
+ return rb_str_new_cstr(version);
49
+ }
50
+
51
+ static VALUE rb_jemalloc_ctl_enable_background_thread(VALUE self)
52
+ {
53
+ if (my_mallctl == NULL)
54
+ rb_raise(rb_eRuntimeError, "jemalloc is not available");
55
+
56
+ bool val = true;
57
+ int err = my_mallctl("background_thread", NULL, NULL, &val, sizeof(bool));
58
+
59
+ if (err != 0)
60
+ rb_raise(rb_eRuntimeError, "mallctl(\"background_thread\") failed with error %d", err);
61
+
62
+ return Qtrue;
63
+ }
64
+
65
+ static VALUE rb_jemalloc_ctl_disable_background_thread(VALUE self)
66
+ {
67
+ if (my_mallctl == NULL)
68
+ rb_raise(rb_eRuntimeError, "jemalloc is not available");
69
+
70
+ bool val = false;
71
+ int err = my_mallctl("background_thread", NULL, NULL, &val, sizeof(bool));
72
+
73
+ if (err != 0)
74
+ rb_raise(rb_eRuntimeError, "mallctl(\"background_thread\") failed with error %d", err);
75
+
76
+ return Qfalse;
77
+ }
78
+
79
+ static mallctl_fn find_mallctl_in(void *handle)
80
+ {
81
+ // Try unprefixed (jemalloc as default allocator) then je_-prefixed
82
+ mallctl_fn fn = (mallctl_fn)dlsym(handle, "mallctl");
83
+ if (fn == NULL)
84
+ fn = (mallctl_fn)dlsym(handle, "je_mallctl");
85
+ return fn;
86
+ }
87
+
88
+ void Init_jemalloc_ctl(void)
89
+ {
90
+ // First: check if mallctl is already in the process
91
+ // (jemalloc compiled into Ruby or LD_PRELOADed)
92
+ my_mallctl = find_mallctl_in(RTLD_DEFAULT);
93
+
94
+ #if JEMALLOC_CTL_DLOPEN_FALLBACK > 0
95
+ // Fallback: try to dlopen jemalloc from common paths
96
+ if (my_mallctl == NULL) {
97
+ void *jemalloc_handle;
98
+ for (const char **name = jemalloc_lib_names; *name != NULL; name++) {
99
+ jemalloc_handle = dlopen(*name, RTLD_LAZY);
100
+ if (jemalloc_handle != NULL) {
101
+ my_mallctl = find_mallctl_in(jemalloc_handle);
102
+ if (my_mallctl != NULL)
103
+ break;
104
+ dlclose(jemalloc_handle);
105
+ jemalloc_handle = NULL;
106
+ }
107
+ }
108
+ }
109
+ #endif
110
+
111
+ rb_mJemallocCtl = rb_define_module("JemallocCtl");
112
+ rb_define_singleton_method(rb_mJemallocCtl, "enabled?", rb_jemalloc_ctl_enabled_p, 0);
113
+ rb_define_singleton_method(rb_mJemallocCtl, "version", rb_jemalloc_ctl_version, 0);
114
+ rb_define_singleton_method(rb_mJemallocCtl, "enable_background_thread", rb_jemalloc_ctl_enable_background_thread, 0);
115
+ rb_define_singleton_method(rb_mJemallocCtl, "disable_background_thread", rb_jemalloc_ctl_disable_background_thread, 0);
116
+ }
@@ -0,0 +1,3 @@
1
+ require "jemalloc_ctl/jemalloc_ctl"
2
+ module JemallocCtl
3
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jemalloc_ctl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luke Gruber
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Ruby interface to jemalloc's mallctl, detected at runtime via dlsym.
13
+ Works with jemalloc compiled into Ruby or loaded via LD_PRELOAD.
14
+ executables: []
15
+ extensions:
16
+ - ext/jemalloc_ctl/extconf.rb
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README
21
+ - ext/jemalloc_ctl/extconf.rb
22
+ - ext/jemalloc_ctl/jemalloc_ctl.c
23
+ - lib/jemalloc_ctl.rb
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.7.0
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 4.0.6
42
+ specification_version: 4
43
+ summary: Configure jemalloc via mallctl
44
+ test_files: []