Module: JemallocCtl

Defined in:
lib/jemalloc_ctl.rb,
ext/jemalloc_ctl/jemalloc_ctl.c

Defined Under Namespace

Classes: Error, JemallocNotFound

Class Method Summary collapse

Class Method Details

.disable_background_threadObject



3
4
5
# File 'lib/jemalloc_ctl.rb', line 3

def self.disable_background_thread
  toggle_background_thread(false)
end

.disable_current_thread_tcacheObject



10
11
12
# File 'lib/jemalloc_ctl.rb', line 10

def self.disable_current_thread_tcache
  toggle_current_thread_tcache(false)
end

.disable_gc_exit_tcache_flush!Object



134
135
136
137
138
139
140
141
142
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 134

static VALUE rb_jemalloc_ctl_disable_gc_exit_tcache_flush(VALUE self)
{
    if (!gc_exit_hook_installed)
        return Qfalse;

    rb_remove_event_hook(gc_exit_tcache_flush_hook);
    gc_exit_hook_installed = 0;
    return Qtrue;
}

.enable_background_threadObject



6
7
8
# File 'lib/jemalloc_ctl.rb', line 6

def self.enable_background_thread
  toggle_background_thread(true)
end

.enable_current_thread_tcacheObject



13
14
15
# File 'lib/jemalloc_ctl.rb', line 13

def self.enable_current_thread_tcache
  toggle_current_thread_tcache(true)
end

.enable_gc_exit_tcache_flush!Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 117

static VALUE rb_jemalloc_ctl_enable_gc_exit_tcache_flush(VALUE self)
{
    if (my_mallctl == NULL)
        rb_raise(rb_eJemallocCtlJemallocNotFound, "jemalloc is not available");

    if (gc_exit_hook_installed)
        return Qfalse;

    if (main_thread_val == Qnil) {
        main_thread_val = rb_thread_main();
        rb_gc_register_address(&main_thread_val);
    }
    rb_add_event_hook(gc_exit_tcache_flush_hook, RUBY_INTERNAL_EVENT_GC_EXIT, Qnil);
    gc_exit_hook_installed = 1;
    return Qtrue;
}

.enabled?Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 47

static VALUE rb_jemalloc_ctl_enabled_p(VALUE self)
{
    return my_mallctl != NULL ? Qtrue : Qfalse;
}

.flush_current_thread_tcacheObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 95

static VALUE rb_jemalloc_ctl_flush_current_thread_tcache(VALUE self)
{
    if (my_mallctl == NULL)
        rb_raise(rb_eJemallocCtlJemallocNotFound, "jemalloc is not available");

    int err = my_mallctl("thread.tcache.flush", NULL, NULL, NULL, 0);

    if (err != 0)
        rb_raise(rb_eJemallocCtlError, "mallctl(\"thread.tcache.flush\") failed with error %d", err);

    return Qtrue;
}


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 199

static VALUE rb_jemalloc_ctl_print_stats(VALUE self, VALUE filename)
{
    if (my_malloc_stats_print == NULL)
        rb_raise(rb_eJemallocCtlError, "malloc_stats_print is not available");

    const char *path = StringValueCStr(filename);
    FILE *f = fopen(path, "w");
    if (f == NULL)
        rb_sys_fail(path);

    my_malloc_stats_print(write_stats_cb, f, NULL);

    fclose(f);
    return Qtrue;
}

.purge_arenasObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 178

static VALUE rb_jemalloc_ctl_purge_arenas(VALUE self)
{
    if (my_mallctl == NULL)
        rb_raise(rb_eJemallocCtlJemallocNotFound, "jemalloc is not available");

    char ctl[32];
    snprintf(ctl, sizeof(ctl), "arena.%d.purge", MALLCTL_ARENAS_ALL);

    int err = my_mallctl(ctl, NULL, NULL, NULL, 0);

    if (err != 0)
        rb_raise(rb_eJemallocCtlError, "mallctl(\"%s\") failed with error %d", ctl, err);

    return Qtrue;
}

.toggle_background_thread(onoff) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 67

static VALUE rb_jemalloc_ctl_toggle_background_thread(VALUE self, VALUE onoff)
{
    if (my_mallctl == NULL)
        rb_raise(rb_eJemallocCtlJemallocNotFound, "jemalloc is not available");

    bool val = RTEST(onoff);
    int err = my_mallctl("background_thread", NULL, NULL, &val, sizeof(bool));

    if (err != 0)
        rb_raise(rb_eJemallocCtlError, "mallctl(\"background_thread\") failed with error %d", err);

    return Qtrue;
}

.toggle_current_thread_tcache(onoff) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 81

static VALUE rb_jemalloc_ctl_toggle_current_thread_tcache(VALUE self, VALUE onoff)
{
    if (my_mallctl == NULL)
        rb_raise(rb_eJemallocCtlJemallocNotFound, "jemalloc is not available");

    bool enabled = RTEST(onoff);
    int err = my_mallctl("thread.tcache.enabled", NULL, NULL, &enabled, sizeof(enabled));

    if (err != 0)
        rb_raise(rb_eJemallocCtlError, "mallctl(\"thread.tcache.enabled\") failed with error %d", err);

    return Qtrue;
}

.toggle_major_gc_arena_purge!(onoff) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 159

static VALUE rb_jemalloc_ctl_toggle_major_gc_arena_purge(VALUE self, VALUE onoff)
{
    if (my_mallctl == NULL)
        rb_raise(rb_eJemallocCtlJemallocNotFound, "jemalloc is not available");

    bool enable = RTEST(onoff);
    if (enable && !major_gc_purge_hook_installed) {
        if (major_gc_count_sym == 0)
            major_gc_count_sym = ID2SYM(rb_intern("major_gc_count"));
        last_major_gc_count = rb_gc_stat(major_gc_count_sym);
        rb_add_event_hook(major_gc_arena_purge_hook, RUBY_INTERNAL_EVENT_GC_EXIT, Qnil);
        major_gc_purge_hook_installed = 1;
    } else if (!enable && major_gc_purge_hook_installed) {
        rb_remove_event_hook(major_gc_arena_purge_hook);
        major_gc_purge_hook_installed = 0;
    }
    return Qtrue;
}

.versionObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 52

static VALUE rb_jemalloc_ctl_version(VALUE self)
{
    if (my_mallctl == NULL)
        rb_raise(rb_eJemallocCtlJemallocNotFound, "jemalloc is not available");

    const char *version;
    size_t sz = sizeof(version);
    int err = my_mallctl("version", &version, &sz, NULL, 0);

    if (err != 0)
        rb_raise(rb_eJemallocCtlError, "mallctl(\"version\") failed with error %d", err);

    return rb_str_new_cstr(version);
}