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