Update.
[glibc.git] / sysdeps / unix / sysv / linux / getsysstats.c
blob2b855c7a8465df58f35a91c16c531fa7a02141e4
1 /* getsysstats - Determine various system internal values, Linux version.
2 Copyright (C) 1996 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <alloca.h>
22 #include <errno.h>
23 #include <mntent.h>
24 #include <paths.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/sysinfo.h>
31 /* Determine the path to the /proc filesystem if available. */
32 static char *
33 get_proc_path (char *buffer, size_t bufsize)
35 FILE *fp;
36 struct mntent mount_point;
37 struct mntent *entry;
38 char *result = NULL;
40 /* First find the mount point of the proc filesystem. */
41 fp = __setmntent (_PATH_MNTTAB, "r");
42 if (fp != NULL)
44 while ((entry = __getmntent_r (fp, &mount_point, buffer, bufsize))
45 != NULL)
46 if (strcmp (mount_point.mnt_type, "proc") == 0)
48 result = mount_point.mnt_dir;
49 break;
51 __endmntent (fp);
54 return result;
58 /* How we can determine the number of available processors depends on
59 the configuration. There is currently (as of version 2.0.21) no
60 system call to determine the number. It is planned for the 2.1.x
61 series to add this, though.
63 One possibility to implement it for systems using Linux 2.0 is to
64 examine the pseudo file /proc/meminfo. Here we have one entry for
65 each processor.
67 But not all systems have support for the /proc filesystem. If it
68 is not available we simply return 1 since there is no way. */
69 int
70 __get_nprocs ()
72 FILE *fp;
73 char buffer[8192];
74 char *proc_path;
75 int result = 1;
77 /* XXX Here will come a test for the new system call. */
79 /* Get mount point of proc filesystem. */
80 proc_path = get_proc_path (buffer, sizeof buffer);
82 /* If we haven't found an appropriate entry return 1. */
83 if (proc_path != NULL)
85 char *proc_cpuinfo = alloca (strlen (proc_path) + sizeof ("/cpuinfo"));
86 __stpcpy (__stpcpy (proc_cpuinfo, proc_path), "/cpuinfo");
88 fp = fopen (proc_cpuinfo, "r");
89 if (fp != NULL)
91 result = 0;
92 /* Read all lines and count the lines starting with the
93 string "processor". We don't have to fear extremely long
94 lines since the kernel will not generate them. 8192
95 bytes are really enough. */
96 while (fgets (buffer, sizeof buffer, fp) != NULL)
97 if (strncmp (buffer, "processor", 9) == 0)
98 ++result;
100 fclose (fp);
104 return result;
106 weak_alias (__get_nprocs, get_nprocs)
108 /* As far as I know Linux has no separate numbers for configured and
109 available processors. So make the `get_nprocs_conf' function an
110 alias. */
111 strong_alias (__get_nprocs, __get_nprocs_conf)
112 weak_alias (__get_nprocs, get_nprocs_conf)
115 /* General function to get information about memory status from proc
116 filesystem. */
117 static int
118 phys_pages_info (const char *format)
120 FILE *fp;
121 char buffer[8192];
122 char *proc_path;
123 int result = -1;
125 /* Get mount point of proc filesystem. */
126 proc_path = get_proc_path (buffer, sizeof buffer);
128 /* If we haven't found an appropriate entry return 1. */
129 if (proc_path != NULL)
131 char *proc_meminfo = alloca (strlen (proc_path) + sizeof ("/meminfo"));
132 __stpcpy (__stpcpy (proc_meminfo, proc_path), "/meminfo");
134 fp = fopen (proc_meminfo, "r");
135 if (fp != NULL)
137 result = 0;
138 /* Read all lines and count the lines starting with the
139 string "processor". We don't have to fear extremely long
140 lines since the kernel will not generate them. 8192
141 bytes are really enough. */
142 while (fgets (buffer, sizeof buffer, fp) != NULL)
143 if (sscanf (buffer, format, &result) == 1)
145 result /= (__getpagesize () / 1024);
146 break;
149 fclose (fp);
153 if (result == -1)
154 /* We cannot get the needed value: signal an error. */
155 __set_errno (ENOSYS);
157 return result;
161 /* Return the number of pages of physical memory in the system. There
162 is currently (as of version 2.0.21) no system call to determine the
163 number. It is planned for the 2.1.x series to add this, though.
165 One possibility to implement it for systems using Linux 2.0 is to
166 examine the pseudo file /proc/cpuinfo. Here we have one entry for
167 each processor.
169 But not all systems have support for the /proc filesystem. If it
170 is not available we return -1 as an error signal. */
172 __get_phys_pages ()
174 /* XXX Here will come a test for the new system call. */
176 return phys_pages_info ("MemTotal: %d kB");
178 weak_alias (__get_phys_pages, get_phys_pages)
181 /* Return the number of available pages of physical memory in the
182 system. There is currently (as of version 2.0.21) no system call
183 to determine the number. It is planned for the 2.1.x series to add
184 this, though.
186 One possibility to implement it for systems using Linux 2.0 is to
187 examine the pseudo file /proc/cpuinfo. Here we have one entry for
188 each processor.
190 But not all systems have support for the /proc filesystem. If it
191 is not available we return -1 as an error signal. */
193 __get_avphys_pages ()
195 /* XXX Here will come a test for the new system call. */
197 return phys_pages_info ("MemFree: %d kB");
199 weak_alias (__get_avphys_pages, get_avphys_pages)