summaryrefslogtreecommitdiff
path: root/external/bsd/kyua-cli/dist/utils/memory.cpp
blob: d580384b1c329e14c2f77ef65863257382512afa (plain)
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
// Copyright 2012 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "utils/memory.hpp"

#if defined(HAVE_CONFIG_H)
#   include "config.h"
#endif

extern "C" {
#if defined(HAVE_SYS_TYPES_H)
#   include <sys/types.h>
#endif
#if defined(HAVE_SYS_PARAM_H)
#   include <sys/param.h>
#endif
#if defined(HAVE_SYS_SYSCTL_H)
#   include <sys/sysctl.h>
#endif
}

#include <cerrno>
#include <cstddef>
#include <cstring>
#include <stdexcept>

#include "utils/defs.hpp"
#include "utils/format/macros.hpp"
#include "utils/logging/macros.hpp"
#include "utils/units.hpp"
#include "utils/sanity.hpp"

namespace units = utils::units;


namespace {


/// Name of the method to query the available memory as detected by configure.
static const char* query_type = MEMORY_QUERY_TYPE;


/// Value of query_type when we do not know how to query the memory.
static const char* query_type_unknown = "unknown";


/// Value of query_type when we have to use sysctlbyname(3).
static const char* query_type_sysctlbyname = "sysctlbyname";


/// Name of the sysctl MIB with the physical memory as detected by configure.
///
/// This should only be used if memory_query_type is 'sysctl'.
static const char* query_sysctl_mib = MEMORY_QUERY_SYSCTL_MIB;


#if !defined(HAVE_SYSCTLBYNAME)
/// Stub for sysctlbyname(3) for systems that don't have it.
///
/// The whole purpose of this fake function is to allow the caller code to be
/// compiled on any machine regardless of the presence of sysctlbyname(3).  This
/// will prevent the code from breaking when it is compiled on a machine without
/// this function.  It also prevents "unused variable" warnings in the caller
/// code.
///
/// \param unused_name Unused.
/// \param unused_oldp Unused.
/// \param unused_oldlenp Unused.
/// \param unused_newp Unused.
/// \param unused_newlen Unused.
///
/// \return Nothing; this always crashes.
static int
sysctlbyname(const char* UTILS_UNUSED_PARAM(name),
             void* UTILS_UNUSED_PARAM(oldp),
             std::size_t* UTILS_UNUSED_PARAM(oldlenp),
             const void* UTILS_UNUSED_PARAM(newp),
             std::size_t UTILS_UNUSED_PARAM(newlen))
{
    UNREACHABLE;
}
#endif


}  // anonymous namespace


/// Gets the value of an integral sysctl MIB.
///
/// \pre The system supports the sysctlbyname(3) function.
///
/// \param mib The name of the sysctl MIB to query.
///
/// \return The value of the MIB, if found.
///
/// \throw std::runtime_error If the sysctlbyname(3) call fails.  This might be
///     a bit drastic.  If it turns out that this causes problems, we could just
///     change the code to log the error instead of raising an exception.
static int64_t
query_sysctl(const char* mib)
{
    // This must be explicitly initialized to 0.  If the sysctl query returned a
    // value smaller in size than value_length, we would get garbage otherwise.
    int64_t value = 0;
    std::size_t value_length = sizeof(value);
    if (::sysctlbyname(mib, &value, &value_length, NULL, 0) == -1) {
        const int original_errno = errno;
        throw std::runtime_error(F("Failed to get sysctl(%s) value: %s") %
                                 mib % std::strerror(original_errno));
    }
    return value;
}


/// Queries the total amount of physical memory.
///
/// The real query is run only once and the result is cached.  Further calls to
/// this function will always return the same value.
///
/// \return The amount of physical memory, in bytes.  If the code does not know
/// how to query the memory, this logs a warning and returns 0.
units::bytes
utils::physical_memory(void)
{
    static int64_t amount = -1;
    if (amount == -1) {
        if (std::strcmp(query_type, query_type_unknown) == 0) {
            LW("Don't know how to query the physical memory");
            amount = 0;
        } else if (std::strcmp(query_type, query_type_sysctlbyname) == 0) {
            amount = query_sysctl(query_sysctl_mib);
        } else
            UNREACHABLE_MSG("Unimplemented memory query type");
        LI(F("Physical memory as returned by query type '%s': %s") %
           query_type % amount);
    }
    POST(amount > -1);
    return units::bytes(amount);
}