Back to index

glibc  2.9
sysconf.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <assert.h>
00020 #include <stdbool.h>
00021 #include <stdlib.h>
00022 #include <unistd.h>
00023 
00024 
00025 static long int linux_sysconf (int name);
00026 
00027 #define CSHAPE(totalsize, linesize, assoc) \
00028   ((totalsize & ~0xff) | (linesize << 4) | assoc)
00029 
00030 extern long __libc_alpha_cache_shape[4];
00031 
00032 static inline unsigned long
00033 implver (void)
00034 {
00035   unsigned long i;
00036 #if __GNUC_PREREQ(3,3)
00037   i = __builtin_alpha_implver ();
00038 #else
00039   asm ("implver %0" : "=r" (i));
00040 #endif
00041   return i;
00042 }
00043 
00044 static inline unsigned long
00045 amask (unsigned long x)
00046 {
00047   unsigned long r;
00048 #if __GNUC_PREREQ(3,3)
00049   r = __builtin_alpha_amask (x);
00050 #else
00051   asm ("amask %1,%0" : "=r"(r) : "Ir"(x));
00052 #endif
00053   return r;
00054 }
00055 
00056 /* Get the value of the system variable NAME.  */
00057 long int
00058 __sysconf (int name)
00059 {
00060   long shape, index;
00061 
00062   /* We only handle the cache information here (for now).  */
00063   if (name < _SC_LEVEL1_ICACHE_SIZE || name > _SC_LEVEL4_CACHE_LINESIZE)
00064     return linux_sysconf (name);
00065 
00066   /* No Alpha has L4 caches.  */
00067   if (name >= _SC_LEVEL4_CACHE_SIZE)
00068     return -1;
00069 
00070   index = (name - _SC_LEVEL1_ICACHE_SIZE) / 3;
00071   shape = __libc_alpha_cache_shape[index];
00072   if (shape == -2)
00073     {
00074       long shape_l1i, shape_l1d, shape_l2, shape_l3 = -1;
00075 
00076       /* ??? In the cases below for which we do not know L1 cache sizes,
00077         we could do timings to measure sizes.  But for the Bcache, it's
00078         generally big enough that (without additional help) TLB effects
00079         get in the way.  We'd either need to be able to allocate large
00080         pages or have the kernel do the timings from KSEG.  Fortunately,
00081         kernels beginning with 2.6.5 will pass us this info in auxvec.  */
00082 
00083       switch (implver())
00084        {
00085        case 0: /* EV4 */
00086          /* EV4/LCA45 had 8k L1 caches; EV45 had 16k L1 caches.  */
00087          /* EV4/EV45 had 128k to 16M 32-byte direct Bcache.  LCA45
00088             had 64k to 8M 8-byte direct Bcache.  Can't tell.  */
00089          shape_l1i = shape_l1d = shape_l2 = CSHAPE (0, 5, 1);
00090          break;
00091 
00092        case 1: /* EV5 */
00093          if (amask (1 << 8))
00094            {
00095              /* MAX insns not present; either EV5 or EV56.  */
00096              shape_l1i = shape_l1d = CSHAPE(8*1024, 5, 1);
00097              /* ??? L2 and L3 *can* be configured as 32-byte line.  */
00098              shape_l2 = CSHAPE (96*1024, 6, 3);
00099              /* EV5/EV56 has 1M to 16M Bcache.  */
00100              shape_l3 = CSHAPE (0, 6, 1);
00101            }
00102          else
00103            {
00104              /* MAX insns present; either PCA56 or PCA57.  */
00105              /* PCA56 had 16k 64-byte cache; PCA57 had 32k Icache.  */
00106              /* PCA56 had 8k 64-byte cache; PCA57 had 16k Dcache.  */
00107              /* PCA5[67] had 512k to 4M Bcache.  */
00108              shape_l1i = shape_l1d = shape_l2 = CSHAPE (0, 6, 1);
00109            }
00110          break;
00111 
00112        case 2: /* EV6 */
00113          shape_l1i = shape_l1d = CSHAPE(64*1024, 6, 2);
00114          /* EV6/EV67/EV68* had 1M to 16M Bcache.  */
00115          shape_l2 = CSHAPE (0, 6, 1);
00116          break;
00117 
00118        case 3: /* EV7 */
00119          shape_l1i = shape_l1d = CSHAPE(64*1024, 6, 2);
00120          shape_l2 = CSHAPE(7*1024*1024/4, 6, 7);
00121          break;
00122 
00123        default:
00124          shape_l1i = shape_l1d = shape_l2 = 0;
00125          break;
00126        }
00127 
00128       __libc_alpha_cache_shape[0] = shape_l1i;
00129       __libc_alpha_cache_shape[1] = shape_l1d;
00130       __libc_alpha_cache_shape[2] = shape_l2;
00131       __libc_alpha_cache_shape[3] = shape_l3;
00132       shape = __libc_alpha_cache_shape[index];
00133     }
00134 
00135   if (shape <= 0)
00136     return shape;
00137 
00138   switch (name % 3)
00139     {
00140     case 0: /* total size */
00141       return shape & -0x100;
00142     case 1: /* associativity */
00143       return shape & 0xf;
00144     default: /* line size */
00145       return 1L << ((shape >> 4) & 0xf);
00146     }
00147 }
00148 
00149 /* Now the generic Linux version.  */
00150 #undef __sysconf
00151 #define __sysconf static linux_sysconf
00152 #include "../sysconf.c"