xref: /freebsd/contrib/kyua/utils/memory.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis // Copyright 2012 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis #include "utils/memory.hpp"
30*b0d29bc4SBrooks Davis 
31*b0d29bc4SBrooks Davis #if defined(HAVE_CONFIG_H)
32*b0d29bc4SBrooks Davis #   include "config.h"
33*b0d29bc4SBrooks Davis #endif
34*b0d29bc4SBrooks Davis 
35*b0d29bc4SBrooks Davis extern "C" {
36*b0d29bc4SBrooks Davis #if defined(HAVE_SYS_TYPES_H)
37*b0d29bc4SBrooks Davis #   include <sys/types.h>
38*b0d29bc4SBrooks Davis #endif
39*b0d29bc4SBrooks Davis #if defined(HAVE_SYS_PARAM_H)
40*b0d29bc4SBrooks Davis #   include <sys/param.h>
41*b0d29bc4SBrooks Davis #endif
42*b0d29bc4SBrooks Davis #if defined(HAVE_SYS_SYSCTL_H)
43*b0d29bc4SBrooks Davis #   include <sys/sysctl.h>
44*b0d29bc4SBrooks Davis #endif
45*b0d29bc4SBrooks Davis }
46*b0d29bc4SBrooks Davis 
47*b0d29bc4SBrooks Davis #include <cerrno>
48*b0d29bc4SBrooks Davis #include <cstddef>
49*b0d29bc4SBrooks Davis #include <cstring>
50*b0d29bc4SBrooks Davis #include <stdexcept>
51*b0d29bc4SBrooks Davis 
52*b0d29bc4SBrooks Davis #include "utils/defs.hpp"
53*b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
54*b0d29bc4SBrooks Davis #include "utils/logging/macros.hpp"
55*b0d29bc4SBrooks Davis #include "utils/units.hpp"
56*b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
57*b0d29bc4SBrooks Davis 
58*b0d29bc4SBrooks Davis namespace units = utils::units;
59*b0d29bc4SBrooks Davis 
60*b0d29bc4SBrooks Davis 
61*b0d29bc4SBrooks Davis namespace {
62*b0d29bc4SBrooks Davis 
63*b0d29bc4SBrooks Davis 
64*b0d29bc4SBrooks Davis /// Name of the method to query the available memory as detected by configure.
65*b0d29bc4SBrooks Davis static const char* query_type = MEMORY_QUERY_TYPE;
66*b0d29bc4SBrooks Davis 
67*b0d29bc4SBrooks Davis 
68*b0d29bc4SBrooks Davis /// Value of query_type when we do not know how to query the memory.
69*b0d29bc4SBrooks Davis static const char* query_type_unknown = "unknown";
70*b0d29bc4SBrooks Davis 
71*b0d29bc4SBrooks Davis 
72*b0d29bc4SBrooks Davis /// Value of query_type when we have to use sysctlbyname(3).
73*b0d29bc4SBrooks Davis static const char* query_type_sysctlbyname = "sysctlbyname";
74*b0d29bc4SBrooks Davis 
75*b0d29bc4SBrooks Davis 
76*b0d29bc4SBrooks Davis /// Name of the sysctl MIB with the physical memory as detected by configure.
77*b0d29bc4SBrooks Davis ///
78*b0d29bc4SBrooks Davis /// This should only be used if memory_query_type is 'sysctl'.
79*b0d29bc4SBrooks Davis static const char* query_sysctl_mib = MEMORY_QUERY_SYSCTL_MIB;
80*b0d29bc4SBrooks Davis 
81*b0d29bc4SBrooks Davis 
82*b0d29bc4SBrooks Davis #if !defined(HAVE_SYSCTLBYNAME)
83*b0d29bc4SBrooks Davis /// Stub for sysctlbyname(3) for systems that don't have it.
84*b0d29bc4SBrooks Davis ///
85*b0d29bc4SBrooks Davis /// The whole purpose of this fake function is to allow the caller code to be
86*b0d29bc4SBrooks Davis /// compiled on any machine regardless of the presence of sysctlbyname(3).  This
87*b0d29bc4SBrooks Davis /// will prevent the code from breaking when it is compiled on a machine without
88*b0d29bc4SBrooks Davis /// this function.  It also prevents "unused variable" warnings in the caller
89*b0d29bc4SBrooks Davis /// code.
90*b0d29bc4SBrooks Davis ///
91*b0d29bc4SBrooks Davis /// \return Nothing; this always crashes.
92*b0d29bc4SBrooks Davis static int
sysctlbyname(const char *,void *,std::size_t *,const void *,std::size_t)93*b0d29bc4SBrooks Davis sysctlbyname(const char* /* name */,
94*b0d29bc4SBrooks Davis              void* /* oldp */,
95*b0d29bc4SBrooks Davis              std::size_t* /* oldlenp */,
96*b0d29bc4SBrooks Davis              const void* /* newp */,
97*b0d29bc4SBrooks Davis              std::size_t /* newlen */)
98*b0d29bc4SBrooks Davis {
99*b0d29bc4SBrooks Davis     UNREACHABLE;
100*b0d29bc4SBrooks Davis }
101*b0d29bc4SBrooks Davis #endif
102*b0d29bc4SBrooks Davis 
103*b0d29bc4SBrooks Davis 
104*b0d29bc4SBrooks Davis }  // anonymous namespace
105*b0d29bc4SBrooks Davis 
106*b0d29bc4SBrooks Davis 
107*b0d29bc4SBrooks Davis /// Gets the value of an integral sysctl MIB.
108*b0d29bc4SBrooks Davis ///
109*b0d29bc4SBrooks Davis /// \pre The system supports the sysctlbyname(3) function.
110*b0d29bc4SBrooks Davis ///
111*b0d29bc4SBrooks Davis /// \param mib The name of the sysctl MIB to query.
112*b0d29bc4SBrooks Davis ///
113*b0d29bc4SBrooks Davis /// \return The value of the MIB, if found.
114*b0d29bc4SBrooks Davis ///
115*b0d29bc4SBrooks Davis /// \throw std::runtime_error If the sysctlbyname(3) call fails.  This might be
116*b0d29bc4SBrooks Davis ///     a bit drastic.  If it turns out that this causes problems, we could just
117*b0d29bc4SBrooks Davis ///     change the code to log the error instead of raising an exception.
118*b0d29bc4SBrooks Davis static int64_t
query_sysctl(const char * mib)119*b0d29bc4SBrooks Davis query_sysctl(const char* mib)
120*b0d29bc4SBrooks Davis {
121*b0d29bc4SBrooks Davis     // This must be explicitly initialized to 0.  If the sysctl query returned a
122*b0d29bc4SBrooks Davis     // value smaller in size than value_length, we would get garbage otherwise.
123*b0d29bc4SBrooks Davis     int64_t value = 0;
124*b0d29bc4SBrooks Davis     std::size_t value_length = sizeof(value);
125*b0d29bc4SBrooks Davis     if (::sysctlbyname(mib, &value, &value_length, NULL, 0) == -1) {
126*b0d29bc4SBrooks Davis         const int original_errno = errno;
127*b0d29bc4SBrooks Davis         throw std::runtime_error(F("Failed to get sysctl(%s) value: %s") %
128*b0d29bc4SBrooks Davis                                  mib % std::strerror(original_errno));
129*b0d29bc4SBrooks Davis     }
130*b0d29bc4SBrooks Davis     return value;
131*b0d29bc4SBrooks Davis }
132*b0d29bc4SBrooks Davis 
133*b0d29bc4SBrooks Davis 
134*b0d29bc4SBrooks Davis /// Queries the total amount of physical memory.
135*b0d29bc4SBrooks Davis ///
136*b0d29bc4SBrooks Davis /// The real query is run only once and the result is cached.  Further calls to
137*b0d29bc4SBrooks Davis /// this function will always return the same value.
138*b0d29bc4SBrooks Davis ///
139*b0d29bc4SBrooks Davis /// \return The amount of physical memory, in bytes.  If the code does not know
140*b0d29bc4SBrooks Davis /// how to query the memory, this logs a warning and returns 0.
141*b0d29bc4SBrooks Davis units::bytes
physical_memory(void)142*b0d29bc4SBrooks Davis utils::physical_memory(void)
143*b0d29bc4SBrooks Davis {
144*b0d29bc4SBrooks Davis     static int64_t amount = -1;
145*b0d29bc4SBrooks Davis     if (amount == -1) {
146*b0d29bc4SBrooks Davis         if (std::strcmp(query_type, query_type_unknown) == 0) {
147*b0d29bc4SBrooks Davis             LW("Don't know how to query the physical memory");
148*b0d29bc4SBrooks Davis             amount = 0;
149*b0d29bc4SBrooks Davis         } else if (std::strcmp(query_type, query_type_sysctlbyname) == 0) {
150*b0d29bc4SBrooks Davis             amount = query_sysctl(query_sysctl_mib);
151*b0d29bc4SBrooks Davis         } else
152*b0d29bc4SBrooks Davis             UNREACHABLE_MSG("Unimplemented memory query type");
153*b0d29bc4SBrooks Davis         LI(F("Physical memory as returned by query type '%s': %s") %
154*b0d29bc4SBrooks Davis            query_type % amount);
155*b0d29bc4SBrooks Davis     }
156*b0d29bc4SBrooks Davis     POST(amount > -1);
157*b0d29bc4SBrooks Davis     return units::bytes(amount);
158*b0d29bc4SBrooks Davis }
159