1 /* Determine various system internal values, Linux version.
2 Copyright (C) 1996-2014 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, see
18 <http://www.gnu.org/licenses/>. */
29 #include <stdio_ext.h>
33 #include <sys/sysinfo.h>
36 #include <not-cancel.h>
39 /* How we can determine the number of available processors depends on
40 the configuration. There is currently (as of version 2.0.21) no
41 system call to determine the number. It is planned for the 2.1.x
42 series to add this, though.
44 One possibility to implement it for systems using Linux 2.0 is to
45 examine the pseudo file /proc/cpuinfo. Here we have one entry for
48 But not all systems have support for the /proc filesystem. If it
49 is not available we simply return 1 since there is no way. */
52 /* Other architectures use different formats for /proc/cpuinfo. This
53 provides a hook for alternative parsers. */
54 #ifndef GET_NPROCS_PARSER
55 # define GET_NPROCS_PARSER(FD, BUFFER, CP, RE, BUFFER_END, RESULT) \
59 /* Read all lines and count the lines starting with the string \
60 "processor". We don't have to fear extremely long lines since \
61 the kernel will not generate them. 8192 bytes are really \
64 while ((l = next_line (FD, BUFFER, &CP, &RE, BUFFER_END)) != NULL) \
65 if (strncmp (l, "processor", 9) == 0) \
73 next_line (int fd
, char *const buffer
, char **cp
, char **re
,
74 char *const buffer_end
)
77 char *nl
= memchr (*cp
, '\n', *re
- *cp
);
82 if (*re
== buffer_end
)
84 memmove (buffer
, *cp
, *re
- *cp
);
85 *re
= buffer
+ (*re
- *cp
);
88 ssize_t n
= read_not_cancel (fd
, *re
, buffer_end
- *re
);
94 nl
= memchr (*cp
, '\n', *re
- *cp
);
95 while (nl
== NULL
&& *re
== buffer_end
)
97 /* Truncate too long lines. */
98 *re
= buffer
+ 3 * (buffer_end
- buffer
) / 4;
99 n
= read_not_cancel (fd
, *re
, buffer_end
- *re
);
103 nl
= memchr (*re
, '\n', n
);
109 nl
= memchr (*cp
, '\n', *re
- *cp
);
121 return res
== *re
? NULL
: res
;
128 static int cached_result
= -1;
129 static time_t timestamp
;
131 time_t now
= time (NULL
);
132 time_t prev
= timestamp
;
133 atomic_read_barrier ();
134 if (now
== prev
&& cached_result
> -1)
135 return cached_result
;
137 /* XXX Here will come a test for the new system call. */
139 const size_t buffer_size
= __libc_use_alloca (8192) ? 8192 : 512;
140 char *buffer
= alloca (buffer_size
);
141 char *buffer_end
= buffer
+ buffer_size
;
142 char *cp
= buffer_end
;
143 char *re
= buffer_end
;
145 const int flags
= O_RDONLY
| O_CLOEXEC
;
146 int fd
= open_not_cancel_2 ("/sys/devices/system/cpu/online", flags
);
151 l
= next_line (fd
, buffer
, &cp
, &re
, buffer_end
);
156 unsigned long int n
= strtoul (l
, &endp
, 10);
163 unsigned long int m
= n
;
167 m
= strtoul (l
, &endp
, 10);
178 while (l
< re
&& isspace (*l
))
183 close_not_cancel_no_status (fd
);
193 /* The /proc/stat format is more uniform, use it by default. */
194 fd
= open_not_cancel_2 ("/proc/stat", flags
);
199 while ((l
= next_line (fd
, buffer
, &cp
, &re
, buffer_end
)) != NULL
)
200 /* The current format of /proc/stat has all the cpu* entries
201 at the front. We assume here that stays this way. */
202 if (strncmp (l
, "cpu", 3) != 0)
204 else if (isdigit (l
[3]))
207 close_not_cancel_no_status (fd
);
211 fd
= open_not_cancel_2 ("/proc/cpuinfo", flags
);
214 GET_NPROCS_PARSER (fd
, buffer
, cp
, re
, buffer_end
, result
);
215 close_not_cancel_no_status (fd
);
220 cached_result
= result
;
221 atomic_write_barrier ();
226 weak_alias (__get_nprocs
, get_nprocs
)
229 /* On some architectures it is possible to distinguish between configured
232 __get_nprocs_conf (void)
234 /* XXX Here will come a test for the new system call. */
236 /* Try to use the sysfs filesystem. It has actual information about
237 online processors. */
238 DIR *dir
= __opendir ("/sys/devices/system/cpu");
244 while ((d
= __readdir64 (dir
)) != NULL
)
245 /* NB: the sysfs has d_type support. */
246 if (d
->d_type
== DT_DIR
&& strncmp (d
->d_name
, "cpu", 3) == 0)
249 unsigned long int nr
= strtoul (d
->d_name
+ 3, &endp
, 10);
250 if (nr
!= ULONG_MAX
&& endp
!= d
->d_name
+ 3 && *endp
== '\0')
261 #ifdef GET_NPROCS_CONF_PARSER
262 /* If we haven't found an appropriate entry return 1. */
263 FILE *fp
= fopen ("/proc/cpuinfo", "rce");
268 /* No threads use this stream. */
269 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
270 GET_NPROCS_CONF_PARSER (fp
, buffer
, result
);
274 result
= __get_nprocs ();
279 weak_alias (__get_nprocs_conf
, get_nprocs_conf
)
281 /* General function to get information about memory status from proc
285 phys_pages_info (const char *format
)
288 long int result
= -1;
290 /* If we haven't found an appropriate entry return 1. */
291 FILE *fp
= fopen ("/proc/meminfo", "rce");
294 /* No threads use this stream. */
295 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
298 /* Read all lines and count the lines starting with the
299 string "processor". We don't have to fear extremely long
300 lines since the kernel will not generate them. 8192
301 bytes are really enough. */
302 while (__fgets_unlocked (buffer
, sizeof buffer
, fp
) != NULL
)
303 if (sscanf (buffer
, format
, &result
) == 1)
305 result
/= (__getpagesize () / 1024);
313 /* We cannot get the needed value: signal an error. */
314 __set_errno (ENOSYS
);
320 /* Return the number of pages of physical memory in the system. There
321 is currently (as of version 2.0.21) no system call to determine the
322 number. It is planned for the 2.1.x series to add this, though.
324 One possibility to implement it for systems using Linux 2.0 is to
325 examine the pseudo file /proc/cpuinfo. Here we have one entry for
328 But not all systems have support for the /proc filesystem. If it
329 is not available we return -1 as an error signal. */
331 __get_phys_pages (void)
333 /* XXX Here will come a test for the new system call. */
335 return phys_pages_info ("MemTotal: %ld kB");
337 weak_alias (__get_phys_pages
, get_phys_pages
)
340 /* Return the number of available pages of physical memory in the
341 system. There is currently (as of version 2.0.21) no system call
342 to determine the number. It is planned for the 2.1.x series to add
345 One possibility to implement it for systems using Linux 2.0 is to
346 examine the pseudo file /proc/cpuinfo. Here we have one entry for
349 But not all systems have support for the /proc filesystem. If it
350 is not available we return -1 as an error signal. */
352 __get_avphys_pages (void)
354 /* XXX Here will come a test for the new system call. */
356 return phys_pages_info ("MemFree: %ld kB");
358 weak_alias (__get_avphys_pages
, get_avphys_pages
)