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



130
131
132
133
134
135
136
137
138
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 130

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 113

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)


43
44
45
46
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 43

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

.flush_current_thread_tcacheObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 91

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;
}


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

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 140

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



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 63

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



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 77

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;
}

.versionObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 48

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);
}