aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/mlock/mlock_linux.cpp
blob: 8791869f951c8c9a043e4b3c0900c025955e5391 (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
#include "mlock.h"

#include <stdio.h>
#include <sys/mman.h>
#include <stdint.h>
#include <inttypes.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

void PopulateFile(void* ptr, size_t size)
{
    constexpr size_t PageSize = 4096;

    auto* begin = static_cast<volatile char*>(ptr);
    for (auto* current = begin; current < begin + size; current += PageSize) {
        *current;
    }
}

bool MlockFileMappings(bool populate)
{
    auto* file = ::fopen("/proc/self/maps", "r");
    if (!file) {
        return false;
    }

    // Each line of /proc/<pid>/smaps has the following format:
    // address           perms offset  dev   inode   path
    // E.g.
    // 08048000-08056000 r-xp 00000000 03:0c 64593   /usr/sbin/gpm

    bool failed = false;
    while (true) {
        char line[1024];
        if (!fgets(line, sizeof(line), file)) {
            break;
        }

        char addressStr[64];
        char permsStr[64];
        char offsetStr[64];
        char devStr[64];
        int inode;
        if (sscanf(line, "%s %s %s %s %d",
            addressStr,
            permsStr,
            offsetStr,
            devStr,
            &inode) != 5)
        {
            continue;
        }

        if (inode == 0) {
            continue;
        }

        if (permsStr[0] != 'r') {
            continue;
        }

        uintptr_t startAddress;
        uintptr_t endAddress;
        if (sscanf(addressStr, "%" PRIx64 "-%" PRIx64,
                &startAddress,
                &endAddress) != 2)
        {
            continue;
        }

        if (::mlock(reinterpret_cast<const void*>(startAddress), endAddress - startAddress) != 0) {
            failed = true;
            continue;
        }

        if (populate) {
            PopulateFile(reinterpret_cast<void*>(startAddress), endAddress - startAddress);
        }
    }

    ::fclose(file);
    return !failed;
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT