xref: /freebsd/contrib/kyua/m4/memory.m4 (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davisdnl Copyright 2012 The Kyua Authors.
2*b0d29bc4SBrooks Davisdnl All rights reserved.
3*b0d29bc4SBrooks Davisdnl
4*b0d29bc4SBrooks Davisdnl Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davisdnl modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davisdnl met:
7*b0d29bc4SBrooks Davisdnl
8*b0d29bc4SBrooks Davisdnl * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davisdnl   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davisdnl * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davisdnl   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davisdnl   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davisdnl * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davisdnl   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davisdnl   without specific prior written permission.
16*b0d29bc4SBrooks Davisdnl
17*b0d29bc4SBrooks Davisdnl THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davisdnl "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davisdnl LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davisdnl A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davisdnl OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davisdnl SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davisdnl LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davisdnl DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davisdnl THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davisdnl (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davisdnl OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis
29*b0d29bc4SBrooks Davisdnl \file m4/memory.m4
30*b0d29bc4SBrooks Davisdnl
31*b0d29bc4SBrooks Davisdnl Macros to configure the utils::memory module.
32*b0d29bc4SBrooks Davis
33*b0d29bc4SBrooks Davis
34*b0d29bc4SBrooks Davisdnl Entry point to detect all features needed by utils::memory.
35*b0d29bc4SBrooks Davisdnl
36*b0d29bc4SBrooks Davisdnl This looks for a mechanism to check the available physical memory in the
37*b0d29bc4SBrooks Davisdnl system.
38*b0d29bc4SBrooks DavisAC_DEFUN([KYUA_MEMORY], [
39*b0d29bc4SBrooks Davis    memory_query=unknown
40*b0d29bc4SBrooks Davis    memory_mib=none
41*b0d29bc4SBrooks Davis
42*b0d29bc4SBrooks Davis    _KYUA_SYSCTLBYNAME([have_sysctlbyname=yes], [have_sysctlbyname=no])
43*b0d29bc4SBrooks Davis    if test "${have_sysctlbyname}" = yes; then
44*b0d29bc4SBrooks Davis        _KYUA_SYSCTL_MIB([hw.usermem64], [hw_usermem64],
45*b0d29bc4SBrooks Davis                         [memory_mib="hw.usermem64"], [])
46*b0d29bc4SBrooks Davis        if test "${memory_mib}" = none; then
47*b0d29bc4SBrooks Davis            _KYUA_SYSCTL_MIB([hw.usermem], [hw_usermem],
48*b0d29bc4SBrooks Davis                             [memory_mib="hw.usermem"], [])
49*b0d29bc4SBrooks Davis        fi
50*b0d29bc4SBrooks Davis        if test "${memory_mib}" != none; then
51*b0d29bc4SBrooks Davis            memory_query=sysctlbyname
52*b0d29bc4SBrooks Davis        fi
53*b0d29bc4SBrooks Davis    fi
54*b0d29bc4SBrooks Davis
55*b0d29bc4SBrooks Davis    if test "${memory_query}" = unknown; then
56*b0d29bc4SBrooks Davis        AC_MSG_WARN([Don't know how to query the amount of physical memory])
57*b0d29bc4SBrooks Davis        AC_MSG_WARN([The test case's require.memory property will not work])
58*b0d29bc4SBrooks Davis    fi
59*b0d29bc4SBrooks Davis
60*b0d29bc4SBrooks Davis    AC_DEFINE_UNQUOTED([MEMORY_QUERY_TYPE], ["${memory_query}"],
61*b0d29bc4SBrooks Davis                       [Define to the memory query type])
62*b0d29bc4SBrooks Davis    AC_DEFINE_UNQUOTED([MEMORY_QUERY_SYSCTL_MIB], ["${memory_mib}"],
63*b0d29bc4SBrooks Davis                       [Define to the name of the sysctl MIB])
64*b0d29bc4SBrooks Davis])
65*b0d29bc4SBrooks Davis
66*b0d29bc4SBrooks Davis
67*b0d29bc4SBrooks Davisdnl Detects the availability of the sysctlbyname(3) function.
68*b0d29bc4SBrooks Davisdnl
69*b0d29bc4SBrooks Davisdnl \param action_if_found Code to run if the function is found.
70*b0d29bc4SBrooks Davisdnl \param action_if_not_found Code to run if the function is not found.
71*b0d29bc4SBrooks DavisAC_DEFUN([_KYUA_SYSCTLBYNAME], [
72*b0d29bc4SBrooks Davis    AC_CHECK_HEADERS([sys/types.h sys/sysctl.h])  dnl Darwin 11.2
73*b0d29bc4SBrooks Davis    AC_CHECK_HEADERS([sys/param.h sys/sysctl.h])  dnl NetBSD 6.0
74*b0d29bc4SBrooks Davis
75*b0d29bc4SBrooks Davis    AC_CHECK_FUNCS([sysctlbyname], [$1], [$2])
76*b0d29bc4SBrooks Davis])
77*b0d29bc4SBrooks Davis
78*b0d29bc4SBrooks Davis
79*b0d29bc4SBrooks Davisdnl Looks for a specific sysctl MIB.
80*b0d29bc4SBrooks Davisdnl
81*b0d29bc4SBrooks Davisdnl \pre sysctlbyname(3) must be present in the system.
82*b0d29bc4SBrooks Davisdnl
83*b0d29bc4SBrooks Davisdnl \param mib_name The name of the MIB to check for.
84*b0d29bc4SBrooks Davisdnl \param flat_mib_name The name of the MIB as a shell variable, for use in
85*b0d29bc4SBrooks Davisdnl     cache variable names.  This should be automatically computed with
86*b0d29bc4SBrooks Davisdnl     m4_bpatsubst or similar, but my inability to make the code readable
87*b0d29bc4SBrooks Davisdnl     made me add this parameter instead.
88*b0d29bc4SBrooks Davisdnl \param action_if_found Code to run if the MIB is found.
89*b0d29bc4SBrooks Davisdnl \param action_if_not_found Code to run if the MIB is not found.
90*b0d29bc4SBrooks DavisAC_DEFUN([_KYUA_SYSCTL_MIB], [
91*b0d29bc4SBrooks Davis    AC_CACHE_CHECK(
92*b0d29bc4SBrooks Davis        [if the $1 sysctl MIB exists],
93*b0d29bc4SBrooks Davis        [kyua_cv_sysctl_$2], [
94*b0d29bc4SBrooks Davis        AC_RUN_IFELSE([AC_LANG_PROGRAM([
95*b0d29bc4SBrooks Davis#if defined(HAVE_SYS_TYPES_H)
96*b0d29bc4SBrooks Davis#   include <sys/types.h>
97*b0d29bc4SBrooks Davis#endif
98*b0d29bc4SBrooks Davis#if defined(HAVE_SYS_PARAM_H)
99*b0d29bc4SBrooks Davis#   include <sys/param.h>
100*b0d29bc4SBrooks Davis#endif
101*b0d29bc4SBrooks Davis#if defined(HAVE_SYS_SYSCTL_H)
102*b0d29bc4SBrooks Davis#   include <sys/sysctl.h>
103*b0d29bc4SBrooks Davis#endif
104*b0d29bc4SBrooks Davis#include <stdint.h>
105*b0d29bc4SBrooks Davis#include <stdlib.h>
106*b0d29bc4SBrooks Davis], [
107*b0d29bc4SBrooks Davis    int64_t memory;
108*b0d29bc4SBrooks Davis    size_t memory_length = sizeof(memory);
109*b0d29bc4SBrooks Davis    if (sysctlbyname("$1", &memory, &memory_length, NULL, 0) == -1)
110*b0d29bc4SBrooks Davis        return EXIT_FAILURE;
111*b0d29bc4SBrooks Davis    else
112*b0d29bc4SBrooks Davis        return EXIT_SUCCESS;
113*b0d29bc4SBrooks Davis])],
114*b0d29bc4SBrooks Davis        [kyua_cv_sysctl_$2=yes],
115*b0d29bc4SBrooks Davis        [kyua_cv_sysctl_$2=no])
116*b0d29bc4SBrooks Davis    ])
117*b0d29bc4SBrooks Davis    if test "${kyua_cv_sysctl_$2}" = yes; then
118*b0d29bc4SBrooks Davis        m4_default([$3], [:])
119*b0d29bc4SBrooks Davis    else
120*b0d29bc4SBrooks Davis        m4_default([$4], [:])
121*b0d29bc4SBrooks Davis    fi
122*b0d29bc4SBrooks Davis])
123