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
28 #include <stdio_ext.h>
32 #include <sys/sysinfo.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. */
46 get_proc_path (char *buffer
, size_t bufsize
)
48 struct mntent mount_point
;
54 /* First find the mount point of the proc filesystem. */
55 fp
= __setmntent (_PATH_MOUNTED
, "r");
57 fp
= __setmntent (_PATH_MNTTAB
, "r");
60 /* We don't need locking. */
61 (void) __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
63 while ((entry
= __getmntent_r (fp
, &mount_point
, buffer
, bufsize
))
65 if (strcmp (mount_point
.mnt_type
, "proc") == 0)
67 result
= mount_point
.mnt_dir
;
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
81 /* Make a copy we can keep around. */
82 copy_result
= __strdup (result
);
83 if (copy_result
== NULL
)
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. */
92 #if __BOUNDED_POINTERS__
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
);
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
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) \
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 \
129 while (fgets_unlocked (BUFFER, sizeof (BUFFER), FP) != NULL) \
130 if (strncmp (BUFFER, "processor", 9) == 0) \
141 const char *proc_path
;
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");
160 /* No threads use this stream. */
161 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
164 while (fgets_unlocked (buffer
, sizeof (buffer
), fp
) != NULL
)
165 if (strncmp (buffer
, "cpu", 3) == 0 && isdigit (buffer
[3]))
172 __stpcpy (__stpcpy (proc_fname
, proc_path
), "/cpuinfo");
174 fp
= fopen (proc_fname
, "rc");
177 /* No threads use this stream. */
178 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
179 GET_NPROCS_PARSER (fp
, buffer
, 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
198 const char *proc_path
;
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");
215 /* No threads use this stream. */
216 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
217 GET_NPROCS_CONF_PARSER (fp
, buffer
, result
);
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
228 strong_alias (__get_nprocs
, __get_nprocs_conf
)
230 weak_alias (__get_nprocs_conf
, get_nprocs_conf
)
232 /* General function to get information about memory status from proc
236 phys_pages_info (const char *format
)
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");
255 /* No threads use this stream. */
256 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
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);
275 /* We cannot get the needed value: signal an error. */
276 __set_errno (ENOSYS
);
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
290 But not all systems have support for the /proc filesystem. If it
291 is not available we return -1 as an error signal. */
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
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
311 But not all systems have support for the /proc filesystem. If it
312 is not available we return -1 as an error signal. */
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
)