aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/madvise.cpp
blob: ababb94f3fb054b35dc3a779824f35b13a2de3dd (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "madvise.h"
#include "align.h"
#include "info.h"

#include <util/generic/yexception.h>

#if defined(_win_)
    #include <util/system/winint.h>
#else
    #include <sys/types.h>
    #include <sys/mman.h>
#endif

#ifndef MADV_DONTDUMP        /* This flag is defined in sys/mman.h since Linux 3.4, but currently old libc header is in use \
                                for capability with Ubuntu 12.04, so we need to define it here manually */
    #define MADV_DONTDUMP 16 /* Explicity exclude from the core dump, overrides the coredump filter bits */
#endif

#ifndef MADV_DODUMP        /* This flag is defined in sys/mman.h since Linux 3.4, but currently old libc header is in use \
                                for capability with Ubuntu 12.04, so we need to define it here manually */
    #define MADV_DODUMP 17 /* Undo the effect of an earlier MADV_DONTDUMP */
#endif

namespace {
    void Madvise(int flag, const void* cbegin, size_t size) {
        static const size_t pageSize = NSystemInfo::GetPageSize();
        void* begin = AlignDown(const_cast<void*>(cbegin), pageSize);
        size = AlignUp(size, pageSize);

#if defined(_win_)
        if (!VirtualFree((LPVOID)begin, size, flag)) {
            TString err(LastSystemErrorText());
            ythrow yexception() << "VirtualFree(" << begin << ", " << size << ", " << flag << ")"
                                << " returned error: " << err;
        }
#else
        if (-1 == madvise(begin, size, flag)) {
            TString err(LastSystemErrorText()); 
            ythrow yexception() << "madvise(" << begin << ", " << size << ", " << flag << ")"
                                << " returned error: " << err;
        }
#endif
    }
}

void MadviseSequentialAccess(const void* begin, size_t size) {
#if !defined(_win_)
    Madvise(MADV_SEQUENTIAL, begin, size);
#endif
}

void MadviseSequentialAccess(TArrayRef<const char> data) {
    MadviseSequentialAccess(data.data(), data.size());
}

void MadviseSequentialAccess(TArrayRef<const ui8> data) {
    MadviseSequentialAccess(data.data(), data.size());
}

void MadviseRandomAccess(const void* begin, size_t size) {
#if !defined(_win_)
    Madvise(MADV_RANDOM, begin, size);
#endif
}

void MadviseRandomAccess(TArrayRef<const char> data) {
    MadviseRandomAccess(data.data(), data.size());
}

void MadviseRandomAccess(TArrayRef<const ui8> data) {
    MadviseRandomAccess(data.data(), data.size());
}

void MadviseEvict(const void* begin, size_t size) {
#if defined(_win_)
    Madvise(MEM_DECOMMIT, begin, size);
#elif defined(_linux_) || defined(_cygwin_)
    Madvise(MADV_DONTNEED, begin, size);
#else // freebsd, osx
    Madvise(MADV_FREE, begin, size);
#endif
}

void MadviseEvict(TArrayRef<const char> data) {
    MadviseEvict(data.data(), data.size());
}

void MadviseEvict(TArrayRef<const ui8> data) {
    MadviseEvict(data.data(), data.size());
}

void MadviseExcludeFromCoreDump(const void* begin, size_t size) {
#if defined(_darwin_)
    // Don't try to call function with flag which doesn't work
    // https://st.yandex-team.ru/PASSP-31755#6050bbafc68f501f2c22caab
    Y_UNUSED(begin);
    Y_UNUSED(size);
#elif !defined(_win_)
    Madvise(MADV_DONTDUMP, begin, size);
#endif
}

void MadviseExcludeFromCoreDump(TArrayRef<const char> data) {
    MadviseExcludeFromCoreDump(data.data(), data.size());
}

void MadviseExcludeFromCoreDump(TArrayRef<const ui8> data) {
    MadviseExcludeFromCoreDump(data.data(), data.size());
}

void MadviseIncludeIntoCoreDump(const void* begin, size_t size) {
#if defined(_darwin_)
    Y_UNUSED(begin);
    Y_UNUSED(size);
#elif !defined(_win_)
    Madvise(MADV_DODUMP, begin, size);
#endif
}

void MadviseIncludeIntoCoreDump(TArrayRef<const char> data) {
    MadviseIncludeIntoCoreDump(data.data(), data.size());
}

void MadviseIncludeIntoCoreDump(TArrayRef<const ui8> data) {
    MadviseIncludeIntoCoreDump(data.data(), data.size());
}