aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/process_memory_stats.cpp
blob: 41f1e282c4be9d08f71d35163af801661918a383 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "process_memory_stats.h"
#include <vespa/vespalib/stllike/asciistream.h>
#include <algorithm>
#include <vector>
#include <cinttypes>

#include <vespa/log/log.h>

LOG_SETUP(".vespalib.util.process_memory_stats");

namespace vespalib {

namespace {

#ifdef __linux__
/*
 * Check if line specifies an address range.
 *
 * address           perms offset  dev   inode   pathname
 *
 * 00400000-00420000 r-xp 00000000 fd:04 16545041                           /usr/bin/less
 */

bool
isRange(vespalib::stringref line) {
    for (char c : line) {
        if (c == ' ') {
            return true;
        }
        if (c == ':') {
            return false;
        }
    }
    return false;
}


/*
 * Check if address range is anonymous, e.g. not mapped from file.
 * inode number is 0 in that case.
 *
 * address           perms offset  dev   inode   pathname
 *
 * 00400000-00420000 r-xp 00000000 fd:04 16545041                           /usr/bin/less
 * 00625000-00628000 rw-p 00000000 00:00 0
 *
 * The range starting at 00400000 is not anonymous.
 * The range starting at 00625000 is anonymous.
 */

bool
isAnonymous(vespalib::stringref line) {
    int delims = 0;
    for (char c : line) {
        if (delims >= 4) {
            return (c == '0');
        }
        if (c == ' ') {
            ++delims;
        }
    }
    return true;
}


/*
 * Lines not containing an address range contains a header and a
 * value, e.g.
 *
 * Size:                128 kB
 * Rss:                  96 kB
 * Anonymous:             0 kB
 *
 * The lines with header Anonymous are ignored, thus anonymous pages
 * caused by mmap() of a file with MAP_PRIVATE flags are counted as
 * mapped pages.
 */

vespalib::stringref
getLineHeader(vespalib::stringref line)
{
    return line.substr(0, line.find(':'));
}
#endif

}

ProcessMemoryStats
ProcessMemoryStats::createStatsFromSmaps()
{
    ProcessMemoryStats ret;
#ifdef __linux__
    asciistream smaps = asciistream::createFromDevice("/proc/self/smaps");
    bool anonymous = true;
    uint64_t lineVal = 0;
    while (!smaps.eof()) {
        string backedLine = smaps.getline();
        stringref line(backedLine);
        if (isRange(line)) {
            ret._mappings_count += 1;
            anonymous = isAnonymous(line);
        } else if (!line.empty()) {
            stringref lineHeader = getLineHeader(line);
            if (lineHeader == "Size") {
                asciistream is(line.substr(lineHeader.size() + 1));
                is >> lineVal;
                if (anonymous) {
                    ret._anonymous_virt += lineVal * 1024;
                } else {
                    ret._mapped_virt += lineVal * 1024;
                }
            } else if (lineHeader == "Rss") {
                asciistream is(line.substr(lineHeader.size() + 1));
                is >> lineVal;
                if (anonymous) {
                    ret._anonymous_rss += lineVal * 1024;
                } else {
                    ret._mapped_rss += lineVal * 1024;
                }
            }
        }
    }
#endif
    return ret;
}


ProcessMemoryStats::ProcessMemoryStats()
    : _mapped_virt(0),
      _mapped_rss(0),
      _anonymous_virt(0),
      _anonymous_rss(0),
      _mappings_count(0)
{
}

ProcessMemoryStats::ProcessMemoryStats(uint64_t mapped_virt,
                                       uint64_t mapped_rss,
                                       uint64_t anonymous_virt,
                                       uint64_t anonymous_rss,
                                       uint64_t mappings_cnt)
    : _mapped_virt(mapped_virt),
      _mapped_rss(mapped_rss),
      _anonymous_virt(anonymous_virt),
      _anonymous_rss(anonymous_rss),
      _mappings_count(mappings_cnt)
{
}

namespace {

bool
similar(uint64_t lhs, uint64_t rhs, uint64_t epsilon)
{
    return (lhs < rhs) ? ((rhs - lhs) <= epsilon) : ((lhs - rhs) <= epsilon);
}

}

bool
ProcessMemoryStats::similarTo(const ProcessMemoryStats &rhs, uint64_t sizeEpsilon) const
{
    return similar(_mapped_virt, rhs._mapped_virt, sizeEpsilon) &&
            similar(_mapped_rss, rhs._mapped_rss, sizeEpsilon) &&
            similar(_anonymous_virt, rhs._anonymous_virt, sizeEpsilon) &&
            similar(_anonymous_rss, rhs._anonymous_rss, sizeEpsilon) &&
            (_mappings_count == rhs._mappings_count);
}

vespalib::string
ProcessMemoryStats::toString() const
{
    vespalib::asciistream stream;
    stream << "_mapped_virt=" << _mapped_virt << ", "
           << "_mapped_rss=" << _mapped_rss << ", "
           << "_anonymous_virt=" << _anonymous_virt << ", "
           << "_anonymous_rss=" << _anonymous_rss << ", "
           << "_mappings_count=" << _mappings_count;
    return stream.str();
}

ProcessMemoryStats
ProcessMemoryStats::create(uint64_t sizeEpsilon)
{
    constexpr size_t NUM_TRIES = 3;
    std::vector<ProcessMemoryStats> samples;
    samples.reserve(NUM_TRIES);
    samples.push_back(createStatsFromSmaps());
    for (size_t i = 0; i < NUM_TRIES; ++i) {
        samples.push_back(createStatsFromSmaps());
        if (samples.back().similarTo(*(samples.rbegin()+1), sizeEpsilon)) {
            return samples.back();
        }
        LOG(debug, "create(): Memory stats have changed, trying to read smaps file again: i=%zu, prevStats={%s}, currStats={%s}",
            i, (samples.rbegin()+1)->toString().c_str(), samples.back().toString().c_str());
    }
    std::sort(samples.begin(), samples.end());
    LOG(debug, "We failed to find 2 consecutive samples that where similar with epsilon of %" PRIu64 ".\nSmallest is '%s',\n median is '%s',\n largest is '%s'",
                 sizeEpsilon, samples.front().toString().c_str(), samples[samples.size()/2].toString().c_str(), samples.back().toString().c_str());
    return samples[samples.size()/2];
}

}