summaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/mem_limit.cpp
blob: 6f03f796c82edc6b7c802cf945ce4f028e6fde55 (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
#ifdef __unix__
    #include <sys/resource.h>
#endif
#include <util/generic/yexception.h>

#include "mem_limit.h"

namespace NYql {

void SetAddressSpaceLimit(ui64 memLimit) {
    if (memLimit) {
#ifdef __unix__
        auto memLimitBytes = memLimit * 1024 * 1024;

        struct rlimit rl;
        if (getrlimit(RLIMIT_AS, &rl)) {
            throw TSystemError() << "Cannot getrlimit(RLIMIT_AS)";
        }

        rl.rlim_cur = memLimitBytes;
        if (setrlimit(RLIMIT_AS, &rl)) {
            throw TSystemError() << "Cannot setrlimit(RLIMIT_AS) to " << memLimitBytes << " bytes";
        }
#else
        throw yexception() << "Memory limit can not be set on this platfrom";
#endif
    }
}

} // namespace NYql