aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/process_memory_stats.h
blob: 000c0942905a831655be1a4289eee7c1b65bcb09 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/stllike/string.h>

namespace vespalib {

/*
 * Class for linux specific way to get memory stats for current process.
 */
class ProcessMemoryStats
{
    uint64_t _mapped_virt; // virtual size
    uint64_t _mapped_rss;  // resident size
    uint64_t _anonymous_virt; // virtual size
    uint64_t _anonymous_rss;  // resident size
    uint64_t _mappings_count; // number of mappings
                              // (limited by sysctl vm.max_map_count)

    static ProcessMemoryStats createStatsFromSmaps();

public:
    ProcessMemoryStats();
    /**
     * Sample memory stats for the current process based on reading the file /proc/self/smaps.
     *
     * Samples are taken until two consecutive memory stats are similar given the size epsilon.
     * This ensures a somewhat consistent memory stats snapshot.
     */
    static ProcessMemoryStats create(uint64_t sizeEpsilon = 1 * 1024 * 1024);
    uint64_t getMappedVirt() const { return _mapped_virt; }
    uint64_t getMappedRss() const { return _mapped_rss; }
    uint64_t getAnonymousVirt() const { return _anonymous_virt; }
    uint64_t getAnonymousRss() const { return _anonymous_rss; }
    uint64_t getMappingsCount() const { return _mappings_count; }
    bool similarTo(const ProcessMemoryStats &rhs, uint64_t sizeEpsilon) const;
    vespalib::string toString() const;
    bool operator < (const ProcessMemoryStats & rhs) const { return _anonymous_rss < rhs._anonymous_rss; }

    /** for unit tests only */
    ProcessMemoryStats(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
};

}