jemalloc_ctl 0.1.3 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ee6bbbd66f548328f75198782d1981fb0b2b06772b6d19cb81496157e3efcf4
4
- data.tar.gz: fab1affaa3c8c1d94c4b681d1eebb35262acbbfef6faa37ba6dc302a9223bef8
3
+ metadata.gz: d7be6090c8c75247efea4b0c3448e68d688dd449d13ad7e53a82850b54b5c83c
4
+ data.tar.gz: '08906468b2623758dbcdc3804df5ad24074783f83cb589274e4c5f4d819f952c'
5
5
  SHA512:
6
- metadata.gz: c64a56894264596d6679879cac23f9ace7b17dbcfb6e0f7bd60198e0f5dd8326baa651757d2f465aea80803b735161e2886563805add92d0e1be0c81ae5af52c
7
- data.tar.gz: ba4c2cb7e37d667485cb58c431ba3479ea5a8394928278279d9271b8479e2a60f3fce6aca477635158fbf59a1276f28c637a93e52ea767a248b28d2577da60cc
6
+ metadata.gz: 445148d11c6a400b26fb814ebb0698858a50e7ecceea3a7fce29d3e37efc7f66c75445337179b77c0b3db0d9a80b7a0657d5a09f01c78df0166cf1aa5e6fb844
7
+ data.tar.gz: 8168e928e33be6f7a3234c3a457d99fcae86b65dcd13d4ff4763de992dc9d5fd2b0dd5666e60c654060c252d0d51b1f65934d1dd8bf2458b5872018cf2916636
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 Luke Gruber
3
+ Copyright (c) Shopify Inc.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,5 @@
1
1
  #include <ruby.h>
2
+ #include <ruby/debug.h>
2
3
  #include <dlfcn.h>
3
4
  #include <stdbool.h>
4
5
 
@@ -36,6 +37,13 @@ static VALUE rb_mJemallocCtl;
36
37
  static VALUE rb_eJemallocCtlError;
37
38
  static VALUE rb_eJemallocCtlJemallocNotFound;
38
39
 
40
+ static VALUE main_thread_val = Qnil;
41
+ static int gc_exit_hook_installed = 0;
42
+
43
+ static int major_gc_purge_hook_installed = 0;
44
+ static size_t last_major_gc_count = 0;
45
+ static VALUE major_gc_count_sym = 0;
46
+
39
47
  static VALUE rb_jemalloc_ctl_enabled_p(VALUE self)
40
48
  {
41
49
  return my_mallctl != NULL ? Qtrue : Qfalse;
@@ -97,6 +105,76 @@ static VALUE rb_jemalloc_ctl_flush_current_thread_tcache(VALUE self)
97
105
  return Qtrue;
98
106
  }
99
107
 
108
+ static void gc_exit_tcache_flush_hook(rb_event_flag_t flag, VALUE data,
109
+ VALUE self, ID mid, VALUE klass)
110
+ {
111
+ // Runs inside GC: must not allocate Ruby objects or raise.
112
+ if (my_mallctl == NULL) return;
113
+ if (rb_thread_current() == main_thread_val) return;
114
+ my_mallctl("thread.tcache.flush", NULL, NULL, NULL, 0);
115
+ }
116
+
117
+ static VALUE rb_jemalloc_ctl_enable_gc_exit_tcache_flush(VALUE self)
118
+ {
119
+ if (my_mallctl == NULL)
120
+ rb_raise(rb_eJemallocCtlJemallocNotFound, "jemalloc is not available");
121
+
122
+ if (gc_exit_hook_installed)
123
+ return Qfalse;
124
+
125
+ if (main_thread_val == Qnil) {
126
+ main_thread_val = rb_thread_main();
127
+ rb_gc_register_address(&main_thread_val);
128
+ }
129
+ rb_add_event_hook(gc_exit_tcache_flush_hook, RUBY_INTERNAL_EVENT_GC_EXIT, Qnil);
130
+ gc_exit_hook_installed = 1;
131
+ return Qtrue;
132
+ }
133
+
134
+ static VALUE rb_jemalloc_ctl_disable_gc_exit_tcache_flush(VALUE self)
135
+ {
136
+ if (!gc_exit_hook_installed)
137
+ return Qfalse;
138
+
139
+ rb_remove_event_hook(gc_exit_tcache_flush_hook);
140
+ gc_exit_hook_installed = 0;
141
+ return Qtrue;
142
+ }
143
+
144
+ static void major_gc_arena_purge_hook(rb_event_flag_t flag, VALUE data,
145
+ VALUE self, ID mid, VALUE klass)
146
+ {
147
+ // Runs inside GC: must not allocate Ruby objects or raise.
148
+ if (my_mallctl == NULL) return;
149
+
150
+ size_t current = rb_gc_stat(major_gc_count_sym); // safe
151
+ if (current == last_major_gc_count) return;
152
+ last_major_gc_count = current;
153
+
154
+ char ctl[32];
155
+ snprintf(ctl, sizeof(ctl), "arena.%d.purge", MALLCTL_ARENAS_ALL);
156
+ my_mallctl(ctl, NULL, NULL, NULL, 0);
157
+ }
158
+
159
+ static VALUE rb_jemalloc_ctl_toggle_major_gc_arena_purge(VALUE self, VALUE onoff)
160
+ {
161
+ if (my_mallctl == NULL)
162
+ rb_raise(rb_eJemallocCtlJemallocNotFound, "jemalloc is not available");
163
+
164
+ bool enable = RTEST(onoff);
165
+ if (enable && !major_gc_purge_hook_installed) {
166
+ if (major_gc_count_sym == 0)
167
+ major_gc_count_sym = ID2SYM(rb_intern("major_gc_count"));
168
+ last_major_gc_count = rb_gc_stat(major_gc_count_sym);
169
+ rb_add_event_hook(major_gc_arena_purge_hook, RUBY_INTERNAL_EVENT_GC_EXIT, Qnil);
170
+ major_gc_purge_hook_installed = 1;
171
+ } else if (!enable && major_gc_purge_hook_installed) {
172
+ rb_remove_event_hook(major_gc_arena_purge_hook);
173
+ major_gc_purge_hook_installed = 0;
174
+ }
175
+ return Qtrue;
176
+ }
177
+
100
178
  static VALUE rb_jemalloc_ctl_purge_arenas(VALUE self)
101
179
  {
102
180
  if (my_mallctl == NULL)
@@ -184,4 +262,7 @@ void Init_jemalloc_ctl(void)
184
262
  rb_define_singleton_method(rb_mJemallocCtl, "purge_arenas", rb_jemalloc_ctl_purge_arenas, 0);
185
263
  rb_define_singleton_method(rb_mJemallocCtl, "flush_current_thread_tcache", rb_jemalloc_ctl_flush_current_thread_tcache, 0);
186
264
  rb_define_singleton_method(rb_mJemallocCtl, "toggle_current_thread_tcache", rb_jemalloc_ctl_toggle_current_thread_tcache, 1);
265
+ rb_define_singleton_method(rb_mJemallocCtl, "enable_gc_exit_tcache_flush!", rb_jemalloc_ctl_enable_gc_exit_tcache_flush, 0);
266
+ rb_define_singleton_method(rb_mJemallocCtl, "disable_gc_exit_tcache_flush!", rb_jemalloc_ctl_disable_gc_exit_tcache_flush, 0);
267
+ rb_define_singleton_method(rb_mJemallocCtl, "toggle_major_gc_arena_purge!", rb_jemalloc_ctl_toggle_major_gc_arena_purge, 1);
187
268
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jemalloc_ctl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Gruber