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