1 /* Determine various system internal values, Linux version.
2 Copyright (C) 1996-2001, 2002, 2003, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <stdio_ext.h>
32 #include <sys/sysinfo.h>
37 /* How we can determine the number of available processors depends on
38 the configuration. There is currently (as of version 2.0.21) no
39 system call to determine the number. It is planned for the 2.1.x
40 series to add this, though.
42 One possibility to implement it for systems using Linux 2.0 is to
43 examine the pseudo file /proc/cpuinfo. Here we have one entry for
46 But not all systems have support for the /proc filesystem. If it
47 is not available we simply return 1 since there is no way. */
49 /* Other architectures use different formats for /proc/cpuinfo. This
50 provides a hook for alternative parsers. */
51 #ifndef GET_NPROCS_PARSER
52 # define GET_NPROCS_PARSER(FP, BUFFER, RESULT) \
56 /* Read all lines and count the lines starting with the string \
57 "processor". We don't have to fear extremely long lines since \
58 the kernel will not generate them. 8192 bytes are really \
60 while (fgets_unlocked (BUFFER, sizeof (BUFFER), FP) != NULL) \
61 if (strncmp (BUFFER, "processor", 9) == 0) \
73 /* XXX Here will come a test for the new system call. */
75 /* The /proc/stat format is more uniform, use it by default. */
76 FILE *fp
= fopen ("/proc/stat", "rc");
79 /* No threads use this stream. */
80 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
83 while (fgets_unlocked (buffer
, sizeof (buffer
), fp
) != NULL
)
84 if (strncmp (buffer
, "cpu", 3) == 0 && isdigit (buffer
[3]))
91 fp
= fopen ("/proc/cpuinfo", "rc");
94 /* No threads use this stream. */
95 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
96 GET_NPROCS_PARSER (fp
, buffer
, result
);
103 weak_alias (__get_nprocs
, get_nprocs
)
106 #ifdef GET_NPROCS_CONF_PARSER
107 /* On some architectures it is possible to distinguish between configured
115 /* XXX Here will come a test for the new system call. */
117 /* If we haven't found an appropriate entry return 1. */
118 FILE *fp
= fopen ("/proc/cpuinfo", "rc");
121 /* No threads use this stream. */
122 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
123 GET_NPROCS_CONF_PARSER (fp
, buffer
, result
);
130 /* As far as I know Linux has no separate numbers for configured and
131 available processors. So make the `get_nprocs_conf' function an
133 strong_alias (__get_nprocs
, __get_nprocs_conf
)
135 weak_alias (__get_nprocs_conf
, get_nprocs_conf
)
137 /* General function to get information about memory status from proc
141 phys_pages_info (const char *format
)
144 long int result
= -1;
146 /* If we haven't found an appropriate entry return 1. */
147 FILE *fp
= fopen ("/proc/meminfo", "rc");
150 /* No threads use this stream. */
151 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
154 /* Read all lines and count the lines starting with the
155 string "processor". We don't have to fear extremely long
156 lines since the kernel will not generate them. 8192
157 bytes are really enough. */
158 while (fgets_unlocked (buffer
, sizeof buffer
, fp
) != NULL
)
159 if (sscanf (buffer
, format
, &result
) == 1)
161 result
/= (__getpagesize () / 1024);
169 /* We cannot get the needed value: signal an error. */
170 __set_errno (ENOSYS
);
176 /* Return the number of pages of physical memory in the system. There
177 is currently (as of version 2.0.21) no system call to determine the
178 number. It is planned for the 2.1.x series to add this, though.
180 One possibility to implement it for systems using Linux 2.0 is to
181 examine the pseudo file /proc/cpuinfo. Here we have one entry for
184 But not all systems have support for the /proc filesystem. If it
185 is not available we return -1 as an error signal. */
189 /* XXX Here will come a test for the new system call. */
191 return phys_pages_info ("MemTotal: %ld kB");
193 weak_alias (__get_phys_pages
, get_phys_pages
)
196 /* Return the number of available pages of physical memory in the
197 system. There is currently (as of version 2.0.21) no system call
198 to determine the number. It is planned for the 2.1.x series to add
201 One possibility to implement it for systems using Linux 2.0 is to
202 examine the pseudo file /proc/cpuinfo. Here we have one entry for
205 But not all systems have support for the /proc filesystem. If it
206 is not available we return -1 as an error signal. */
208 __get_avphys_pages ()
210 /* XXX Here will come a test for the new system call. */
212 return phys_pages_info ("MemFree: %ld kB");
214 weak_alias (__get_avphys_pages
, get_avphys_pages
)