* sysdeps/unix/sysv/linux/Versions: Move sync_file_range to
[glibc.git] / sysdeps / unix / sysv / linux / getsysstats.c
blobd655ba3b27e9b72889bc719bfc1f654c26e68bd2
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
19 02111-1307 USA. */
21 #include <alloca.h>
22 #include <assert.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <mntent.h>
26 #include <paths.h>
27 #include <stdio.h>
28 #include <stdio_ext.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/sysinfo.h>
34 #include <atomic.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
44 each processor.
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) \
53 do \
54 { \
55 (RESULT) = 0; \
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 \
59 enough. */ \
60 while (fgets_unlocked (BUFFER, sizeof (BUFFER), FP) != NULL) \
61 if (strncmp (BUFFER, "processor", 9) == 0) \
62 ++(RESULT); \
63 } \
64 while (0)
65 #endif
67 int
68 __get_nprocs ()
70 char buffer[8192];
71 int result = 1;
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");
77 if (fp != NULL)
79 /* No threads use this stream. */
80 __fsetlocking (fp, FSETLOCKING_BYCALLER);
82 result = 0;
83 while (fgets_unlocked (buffer, sizeof (buffer), fp) != NULL)
84 if (strncmp (buffer, "cpu", 3) == 0 && isdigit (buffer[3]))
85 ++result;
87 fclose (fp);
89 else
91 fp = fopen ("/proc/cpuinfo", "rc");
92 if (fp != NULL)
94 /* No threads use this stream. */
95 __fsetlocking (fp, FSETLOCKING_BYCALLER);
96 GET_NPROCS_PARSER (fp, buffer, result);
97 fclose (fp);
101 return 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
108 and active cpus. */
110 __get_nprocs_conf ()
112 char buffer[8192];
113 int result = 1;
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");
119 if (fp != NULL)
121 /* No threads use this stream. */
122 __fsetlocking (fp, FSETLOCKING_BYCALLER);
123 GET_NPROCS_CONF_PARSER (fp, buffer, result);
124 fclose (fp);
127 return result;
129 #else
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
132 alias. */
133 strong_alias (__get_nprocs, __get_nprocs_conf)
134 #endif
135 weak_alias (__get_nprocs_conf, get_nprocs_conf)
137 /* General function to get information about memory status from proc
138 filesystem. */
139 static long int
140 internal_function
141 phys_pages_info (const char *format)
143 char buffer[8192];
144 long int result = -1;
146 /* If we haven't found an appropriate entry return 1. */
147 FILE *fp = fopen ("/proc/meminfo", "rc");
148 if (fp != NULL)
150 /* No threads use this stream. */
151 __fsetlocking (fp, FSETLOCKING_BYCALLER);
153 result = 0;
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);
162 break;
165 fclose (fp);
168 if (result == -1)
169 /* We cannot get the needed value: signal an error. */
170 __set_errno (ENOSYS);
172 return result;
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
182 each processor.
184 But not all systems have support for the /proc filesystem. If it
185 is not available we return -1 as an error signal. */
186 long int
187 __get_phys_pages ()
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
199 this, though.
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
203 each processor.
205 But not all systems have support for the /proc filesystem. If it
206 is not available we return -1 as an error signal. */
207 long int
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)