(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / unix / sysv / linux / getsysstats.c
blob985a0860cb434cedb801a4422b707f43c71bfae5
1 /* Determine various system internal values, Linux version.
2 Copyright (C) 1996-2001, 2002, 2003 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 /* The default value for the /proc filesystem mount point. */
38 static const char path_proc[] = "/proc";
40 /* Actual mount point of /proc filesystem. */
41 libc_freeres_ptr (static char *mount_proc);
43 /* Determine the path to the /proc filesystem if available. */
44 static const char *
45 internal_function
46 get_proc_path (char *buffer, size_t bufsize)
48 struct mntent mount_point;
49 struct mntent *entry;
50 char *result = NULL;
51 char *copy_result;
52 FILE *fp;
54 /* First find the mount point of the proc filesystem. */
55 fp = __setmntent (_PATH_MOUNTED, "r");
56 if (fp == NULL)
57 fp = __setmntent (_PATH_MNTTAB, "r");
58 if (fp != NULL)
60 /* We don't need locking. */
61 (void) __fsetlocking (fp, FSETLOCKING_BYCALLER);
63 while ((entry = __getmntent_r (fp, &mount_point, buffer, bufsize))
64 != NULL)
65 if (strcmp (mount_point.mnt_type, "proc") == 0)
67 result = mount_point.mnt_dir;
68 break;
70 __endmntent (fp);
73 /* If we haven't found anything this is generally a bad sign but we
74 handle it gracefully. We return what is hopefully the right
75 answer (/proc) but we don't remember this. This will enable
76 programs which started before the system is fully running to
77 adjust themselves. */
78 if (result == NULL)
79 return path_proc;
81 /* Make a copy we can keep around. */
82 copy_result = __strdup (result);
83 if (copy_result == NULL)
84 return result;
86 /* Now store the copied value. But do it atomically. */
87 assert (sizeof (long int) == sizeof (void *__unbounded));
88 if (atomic_compare_and_exchange_bool_acq (&mount_proc, copy_result, NULL))
89 /* Replacing the value failed. This means another thread was
90 faster and we don't need the copy anymore. */
91 free (copy_result);
92 #if __BOUNDED_POINTERS__
93 else
95 /* compare_and_swap only copied the pointer value, so we must
96 now copy the bounds as well. */
97 __ptrlow (mount_proc) = __ptrlow (copy_result);
98 __ptrhigh (mount_proc) = __ptrhigh (copy_result);
100 #endif
102 return mount_proc;
106 /* How we can determine the number of available processors depends on
107 the configuration. There is currently (as of version 2.0.21) no
108 system call to determine the number. It is planned for the 2.1.x
109 series to add this, though.
111 One possibility to implement it for systems using Linux 2.0 is to
112 examine the pseudo file /proc/cpuinfo. Here we have one entry for
113 each processor.
115 But not all systems have support for the /proc filesystem. If it
116 is not available we simply return 1 since there is no way. */
118 /* Other architectures use different formats for /proc/cpuinfo. This
119 provides a hook for alternative parsers. */
120 #ifndef GET_NPROCS_PARSER
121 # define GET_NPROCS_PARSER(FP, BUFFER, RESULT) \
122 do \
124 (RESULT) = 0; \
125 /* Read all lines and count the lines starting with the string \
126 "processor". We don't have to fear extremely long lines since \
127 the kernel will not generate them. 8192 bytes are really \
128 enough. */ \
129 while (fgets_unlocked (BUFFER, sizeof (BUFFER), FP) != NULL) \
130 if (strncmp (BUFFER, "processor", 9) == 0) \
131 ++(RESULT); \
133 while (0)
134 #endif
137 __get_nprocs ()
139 FILE *fp;
140 char buffer[8192];
141 const char *proc_path;
142 int result = 1;
144 /* XXX Here will come a test for the new system call. */
146 /* Get mount point of proc filesystem. */
147 proc_path = get_proc_path (buffer, sizeof buffer);
149 /* If we haven't found an appropriate entry return 1. */
150 if (proc_path != NULL)
152 char *proc_fname = alloca (strlen (proc_path) + sizeof ("/cpuinfo"));
154 /* The /proc/stat format is more uniform, use it by default. */
155 __stpcpy (__stpcpy (proc_fname, proc_path), "/stat");
157 fp = fopen (proc_fname, "rc");
158 if (fp != NULL)
160 /* No threads use this stream. */
161 __fsetlocking (fp, FSETLOCKING_BYCALLER);
163 result = 0;
164 while (fgets_unlocked (buffer, sizeof (buffer), fp) != NULL)
165 if (strncmp (buffer, "cpu", 3) == 0 && isdigit (buffer[3]))
166 ++result;
168 fclose (fp);
170 else
172 __stpcpy (__stpcpy (proc_fname, proc_path), "/cpuinfo");
174 fp = fopen (proc_fname, "rc");
175 if (fp != NULL)
177 /* No threads use this stream. */
178 __fsetlocking (fp, FSETLOCKING_BYCALLER);
179 GET_NPROCS_PARSER (fp, buffer, result);
180 fclose (fp);
185 return result;
187 weak_alias (__get_nprocs, get_nprocs)
190 #ifdef GET_NPROCS_CONF_PARSER
191 /* On some architectures it is possible to distinguish between configured
192 and active cpus. */
194 __get_nprocs_conf ()
196 FILE *fp;
197 char buffer[8192];
198 const char *proc_path;
199 int result = 1;
201 /* XXX Here will come a test for the new system call. */
203 /* Get mount point of proc filesystem. */
204 proc_path = get_proc_path (buffer, sizeof buffer);
206 /* If we haven't found an appropriate entry return 1. */
207 if (proc_path != NULL)
209 char *proc_cpuinfo = alloca (strlen (proc_path) + sizeof ("/cpuinfo"));
210 __stpcpy (__stpcpy (proc_cpuinfo, proc_path), "/cpuinfo");
212 fp = fopen (proc_cpuinfo, "rc");
213 if (fp != NULL)
215 /* No threads use this stream. */
216 __fsetlocking (fp, FSETLOCKING_BYCALLER);
217 GET_NPROCS_CONF_PARSER (fp, buffer, result);
218 fclose (fp);
222 return result;
224 #else
225 /* As far as I know Linux has no separate numbers for configured and
226 available processors. So make the `get_nprocs_conf' function an
227 alias. */
228 strong_alias (__get_nprocs, __get_nprocs_conf)
229 #endif
230 weak_alias (__get_nprocs_conf, get_nprocs_conf)
232 /* General function to get information about memory status from proc
233 filesystem. */
234 static long int
235 internal_function
236 phys_pages_info (const char *format)
238 FILE *fp;
239 char buffer[8192];
240 const char *proc_path;
241 long int result = -1;
243 /* Get mount point of proc filesystem. */
244 proc_path = get_proc_path (buffer, sizeof buffer);
246 /* If we haven't found an appropriate entry return 1. */
247 if (proc_path != NULL)
249 char *proc_meminfo = alloca (strlen (proc_path) + sizeof ("/meminfo"));
250 __stpcpy (__stpcpy (proc_meminfo, proc_path), "/meminfo");
252 fp = fopen (proc_meminfo, "rc");
253 if (fp != NULL)
255 /* No threads use this stream. */
256 __fsetlocking (fp, FSETLOCKING_BYCALLER);
258 result = 0;
259 /* Read all lines and count the lines starting with the
260 string "processor". We don't have to fear extremely long
261 lines since the kernel will not generate them. 8192
262 bytes are really enough. */
263 while (fgets_unlocked (buffer, sizeof buffer, fp) != NULL)
264 if (sscanf (buffer, format, &result) == 1)
266 result /= (__getpagesize () / 1024);
267 break;
270 fclose (fp);
274 if (result == -1)
275 /* We cannot get the needed value: signal an error. */
276 __set_errno (ENOSYS);
278 return result;
282 /* Return the number of pages of physical memory in the system. There
283 is currently (as of version 2.0.21) no system call to determine the
284 number. It is planned for the 2.1.x series to add this, though.
286 One possibility to implement it for systems using Linux 2.0 is to
287 examine the pseudo file /proc/cpuinfo. Here we have one entry for
288 each processor.
290 But not all systems have support for the /proc filesystem. If it
291 is not available we return -1 as an error signal. */
292 long int
293 __get_phys_pages ()
295 /* XXX Here will come a test for the new system call. */
297 return phys_pages_info ("MemTotal: %ld kB");
299 weak_alias (__get_phys_pages, get_phys_pages)
302 /* Return the number of available pages of physical memory in the
303 system. There is currently (as of version 2.0.21) no system call
304 to determine the number. It is planned for the 2.1.x series to add
305 this, though.
307 One possibility to implement it for systems using Linux 2.0 is to
308 examine the pseudo file /proc/cpuinfo. Here we have one entry for
309 each processor.
311 But not all systems have support for the /proc filesystem. If it
312 is not available we return -1 as an error signal. */
313 long int
314 __get_avphys_pages ()
316 /* XXX Here will come a test for the new system call. */
318 return phys_pages_info ("MemFree: %ld kB");
320 weak_alias (__get_avphys_pages, get_avphys_pages)