• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/components/performance_counters/memory_counters/src/mem_counter_linux.cpp
1
// Copyright (c) 2012 Vinay C Amatya
2
// Copyright (c) Bryce Adelstein-Lelbach
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7

8
#include <hpx/config.hpp>
9

10
#if defined(__linux) || defined(linux) || defined(linux__) || defined(__linux__)
11

12
#include <sys/mman.h>
13
#include <sys/param.h>
14
#include <sys/types.h>
15
#include <unistd.h>
16

17
// Fix for musl. Use linux/param.h for EXEC_PAGESIZE
18
#ifdef __linux__
19
#include <linux/param.h>
20
#endif
21

22
#include <hpx/modules/errors.hpp>
23
#include <hpx/modules/format.hpp>
24

25
#include <boost/fusion/include/define_struct.hpp>
26
#include <boost/fusion/include/io.hpp>
27
#include <boost/spirit/home/support/iterators/istream_iterator.hpp>
28
#include <boost/spirit/home/x3/char.hpp>
29
#include <boost/spirit/home/x3/core.hpp>
30
#include <boost/spirit/home/x3/numeric.hpp>
31
#include <boost/spirit/home/x3/operator.hpp>
32

33
#include <algorithm>
34
#include <cstdint>
35
#include <fstream>
36
#include <iterator>
37
#include <string>
38
#include <vector>
39

40
// clang-format off
41
BOOST_FUSION_DEFINE_STRUCT((hpx)(performance_counters)(memory), proc_statm,
×
42
    (std::uint64_t, size)
43
    (std::uint64_t, resident)
44
    (std::uint64_t, share)
45
    (std::uint64_t, text)
46
    (std::uint64_t, lib)
47
    (std::uint64_t, data)
48
    (std::uint64_t, dt)
49
)
50
// clang-format on
51

52
namespace hpx { namespace performance_counters { namespace memory {
53
    namespace x3 = boost::spirit::x3;
54
    namespace ascii = boost::spirit::x3::ascii;
55

56
    auto const proc_statm_grammar = x3::uint64 >> x3::uint64 >> x3::uint64 >>
57
        x3::uint64 >> x3::uint64 >> x3::uint64 >> x3::uint64;
58

59
    struct ifstream_raii
60
    {
61
        ifstream_raii(char const* file, std::ios_base::openmode mode)
62
          : stm(file, mode)
×
63
        {
64
        }
×
65

66
        ~ifstream_raii()
×
67
        {
68
            if (stm.is_open())
×
69
                stm.close();
×
70
        }
×
71

72
        std::ifstream& get()
73
        {
74
            return stm;
75
        }
76

77
    private:
78
        std::ifstream stm;
79
    };
80

81
    bool read_proc_statm(proc_statm& ps, std::int32_t pid)
×
82
    {
83
        std::string filename = hpx::util::format("/proc/{1}/statm", pid);
×
84

85
        ifstream_raii in(filename.c_str(), std::ios_base::in);
86

87
        if (!in.get())
×
88
            return false;
89

90
        in.get().unsetf(std::ios::skipws);    // No white space skipping!
91

92
        typedef boost::spirit::basic_istream_iterator<char> iterator;
93

94
        iterator it(in.get()), end;
95

96
        return x3::phrase_parse(it, end, proc_statm_grammar, ascii::space, ps);
×
97
    }
×
98

99
    ///////////////////////////////////////////////////////////////////////////
100
    // Returns virtual memory usage.
101
    std::uint64_t read_psm_virtual(bool)
×
102
    {
103
        proc_statm ps;
104

105
        if (!read_proc_statm(ps, getpid()))
×
106
        {
107
            HPX_THROW_EXCEPTION(hpx::error::invalid_data,
×
108
                "hpx::performance_counters::memory::read_psm_virtual",
109
                "failed to parse '/proc/{1}/statm'", getpid());
110
            return std::uint64_t(-1);
111
        }
112

113
        // ps.size is in pages, but we need to return the number of bytes.
114
        return ps.size * EXEC_PAGESIZE;
×
115
    }
116

117
    // Returns resident memory usage.
118
    std::uint64_t read_psm_resident(bool)
×
119
    {
120
        proc_statm ps;
121

122
        if (!read_proc_statm(ps, getpid()))
×
123
        {
124
            HPX_THROW_EXCEPTION(hpx::error::invalid_data,
×
125
                "hpx::performance_counters::memory::read_psm_resident",
126
                "failed to parse '/proc/{1}/statm'", getpid());
127
            return std::uint64_t(-1);
128
        }
129

130
        // ps.resident is in pages, but we need to return the number of bytes.
131
        return ps.resident * EXEC_PAGESIZE;
×
132
    }
133

134
    // Returns total available memory
135
    std::uint64_t read_total_mem_avail(bool)
×
136
    {
137
        std::string file = "/proc/meminfo";
×
138
        std::ifstream in;
×
139

140
        char buffer[1024];
141
        in.open(file.c_str());
×
142

143
        //Available Memory is on 3rd line
144
        for (int k = 0; k < 3; k++)
×
145
        {
146
            in.getline(buffer, 1024);
×
147
        }
148
        in.close();
×
149
        std::string tbuf = buffer;
×
150
        tbuf.copy(buffer, 11, 13);
×
151
        return atol(buffer);
×
152
    }
×
153

154
}}}    // namespace hpx::performance_counters::memory
155

156
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc