File: ax_cache_size.m4

package info (click to toggle)
libm4ri 20200125-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,560 kB
  • sloc: ansic: 12,633; sh: 4,304; makefile: 137
file content (140 lines) | stat: -rw-r--r-- 4,560 bytes parent folder | download | duplicates (2)
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
# ===========================================================================
#             http://autoconf-archive.cryp.to/ax_cache_size.html
# ===========================================================================
#
# SYNOPSIS
#
#   AX_CACHE_SIZE
#
# DESCRIPTION
#
#   Find L1 and L2 caches size by reading the corresponding file on UNIX or
#   by requesting cpuid. The results are available in the substituted variables
#   M4RI_CPU_L1_CACHE and M4RI_CPU_L2_CACHE.
#
#   This macro depends on AX_GCC_X86_CPUID, AC_PROG_SED, and AX_CPU_VENDOR.
#
# LAST MODIFICATION
#
#   2011-04-11
#
# COPYLEFT
#
#   Copyright (c) 2008 Christophe Tournayre <turn3r@users.sourceforge.net>
#
# Patched by:
#
#   Copyright (c) 2008 Martin Albrecht <M.R.Albrecht@rhul.ac.uk>
#   Copyright (c) 2008 Arnaud Bergeron <abergeron@gmail.com>
#
#   Copying and distribution of this file, with or without modification, are
#   permitted in any medium without royalty provided the copyright notice
#   and this notice are preserved.

AC_DEFUN([AX_CACHE_SIZE],
[
    AC_REQUIRE([AC_PROG_SED])
    AC_REQUIRE([AX_GCC_X86_CPUID])
    AC_REQUIRE([AX_CPU_VENDOR])

    AX_CPU_VENDOR

    ax_l1_size=
    ax_l2_size=

    #Check if the variable is present
    if test -e /sys/devices/system/cpu/cpu0/cache/index0/size; then
        for idx in `seq 0 3`; do
            if test -e /sys/devices/system/cpu/cpu0/cache/index$idx/size ; then
                level=`cat /sys/devices/system/cpu/cpu0/cache/index$idx/level`
                size=`cat /sys/devices/system/cpu/cpu0/cache/index$idx/size`
                eval CPU0\_L$level\_CACHE="$size"
            fi
        done

        ax_l1_size=$CPU0_L1_CACHE
        ax_l2_size=$CPU0_L2_CACHE
        ax_l3_size=$CPU0_L3_CACHE

    else
      if test "x$ax_cv_cpu_vendor" != "xUnknown"; then
        #Or use CPUID
	AX_GCC_X86_CPUID(0x80000000)
	cpu_exthigh=`echo $ax_cv_gcc_x86_cpuid_0x80000000 | cut -d ":" -f 1`
	if test "x$cpu_exthi" > "x80000004"; then
          AX_GCC_X86_CPUID(0x80000005) # For L1 cache
          l1_hexval=`echo $ax_cv_gcc_x86_cpuid_0x80000005 | cut -d ":" -f 4`
          ax_l1_size=$((0x$l1_hexval >> 24))
        fi

	if test "x$cpu_exthi" > "x80000005"; then
          AX_GCC_X86_CPUID(0x80000006) # For L2 cache
          l2_hexval=`echo $ax_cv_gcc_x86_cpuid_0x80000006 | cut -d ":" -f 3`
          ax_l2_size=$((0x$l2_hexval >> 16))
        fi

	if test "x$cpu_exthi" > "x80000005"; then
          AX_GCC_X86_CPUID(0x80000006) # For L3 cache
          l2_hexval=`echo $ax_cv_gcc_x86_cpuid_0x80000006 | cut -d ":" -f 4`
          ax_l2_size=$((0x$l2_hexval >> 18))*512
        fi

      fi

      #Or use sysctl
      sysctl_exe=
      if test -x /usr/sbin/sysctl ; then
	sysctl_exe=/usr/sbin/sysctl
      elif test -x /sbin/sysctl ; then
	sysctl_exe=/sbin/sysctl
      fi
      if test -n "$sysctl_exe"; then
	if test -z "$ax_l2_size" -o "$ax_l2_size" = "0"; then
          sysctl_out=`$sysctl_exe -n hw.l2cachesize 2>/dev/null`;
          if test ! -z "$sysctl_out"; then
            ax_l2_size=$(($sysctl_out / 1024))
          fi;

	fi
	if test -z "$ax_l1_size" -o "$ax_l1_size" = "0" ; then
          sysctl_out=`$sysctl_exe -n hw.l1dcachesize 2>/dev/null`;
          if test ! -z "$sysctl_out"; then
  	    ax_l1_size=$(($sysctl_out / 1024))
          fi;
	fi
	if test -z "$ax_l1_size" -o "ax_l1_size" = "0" ; then
          sysctl_out=`$sysctl_exe -n hw.l1cachesize 2>/dev/null`;
          if test ! -z "$sysctl_out"; then
             ax_l1_size=$(($sysctl_out / 1024))
          fi;
        fi
      fi
    fi

    test -z "$ax_l1_size" && ax_l1_size=0
    test -z "$ax_l2_size" && ax_l2_size=0
    test -z "$ax_l3_size" && ax_l3_size=$ax_l2_size

    # Keep only digits if there is a unit (ie 1024K -> 1024) and convert in Bytes
    AC_MSG_CHECKING(the L1 cache size)
    ax_l1_size=`echo $ax_l1_size | $SED 's/\([[0-9]]\)[[A-Za-z]]$/\1/g'`
    ax_l1_size=$(($ax_l1_size*1024))
    AC_MSG_RESULT( $ax_l1_size Bytes)

    AC_MSG_CHECKING(the L2 cache size)
    ax_l2_size=`echo $ax_l2_size | $SED 's/\([[0-9]]\)[[A-Za-z]]$/\1/g'`
    ax_l2_size=$(($ax_l2_size*1024))
    AC_MSG_RESULT( $ax_l2_size Bytes)

    AC_MSG_CHECKING(the L3 cache size)
    ax_l3_size=`echo $ax_l3_size | $SED 's/\([[0-9]]\)[[A-Za-z]]$/\1/g'`
    ax_l3_size=$(($ax_l3_size*1024))
    AC_MSG_RESULT( $ax_l3_size Bytes)

    M4RI_CPU_L1_CACHE=${ax_l1_size}
    M4RI_CPU_L2_CACHE=${ax_l2_size}
    M4RI_CPU_L3_CACHE=${ax_l3_size}
    AC_SUBST(M4RI_CPU_L1_CACHE)
    AC_SUBST(M4RI_CPU_L2_CACHE)
    AC_SUBST(M4RI_CPU_L3_CACHE)
])