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

.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

.enabled?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 39

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

.flush_current_thread_tcacheObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 87

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


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 121

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 100

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



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 59

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



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 73

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'ext/jemalloc_ctl/jemalloc_ctl.c', line 44

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