aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/malloc/jemalloc/malloc-info.cpp
blob: 2643ca47668cbf89b58de4da7e84b3ece7d2abf8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <library/cpp/malloc/api/malloc.h>

using namespace NMalloc;

#if defined(_MSC_VER)
TMallocInfo NMalloc::MallocInfo() {
    TMallocInfo r;
    r.Name = "jemalloc";
    return r;
}
#else
#include <strings.h>
#include <stdlib.h>
#include <inttypes.h>

#include <contrib/libs/jemalloc/include/jemalloc/jemalloc.h>

namespace {
    bool JESetParam(const char* param, const char*) {
        if (param) {
            if (strcmp(param, "j:reset_epoch") == 0) {
                uint64_t epoch = 1;
                size_t sz = sizeof(epoch);

                mallctl("epoch", &epoch, &sz, &epoch, sz);

                return true;
            }

            return false;
        }

        return false;
    }

    const char* JEGetParam(const char* param) {
        if (param) {
            if (strcmp(param, "allocated") == 0) {
                JESetParam("j:reset_epoch", nullptr);

                size_t allocated = 0;
                size_t sz = sizeof(allocated);

                mallctl("stats.allocated", &allocated, &sz, nullptr, 0);

                static_assert(sizeof(size_t) == sizeof(void*), "fix me");

                return (const char*)(void*)allocated;
            }

            return nullptr;
        }

        return nullptr;
    }
}

TMallocInfo NMalloc::MallocInfo() {
    TMallocInfo r;
    r.Name = "jemalloc";
    r.SetParam = JESetParam;
    r.GetParam = JEGetParam;
    return r;
}
#endif