Update.
[glibc.git] / sysdeps / unix / sysv / linux / getsysstats.c
blobdd1c77a99ec2231dda1a4f95bcd310c9ba19edf4
1 /* Determine various system internal values, Linux version.
2 Copyright (C) 1996, 1997 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 internal_function
34 get_proc_path (char *buffer, size_t bufsize)
36 FILE *fp;
37 struct mntent mount_point;
38 struct mntent *entry;
39 char *result = NULL;
41 /* First find the mount point of the proc filesystem. */
42 fp = __setmntent (_PATH_MNTTAB, "r");
43 if (fp != NULL)
45 while ((entry = __getmntent_r (fp, &mount_point, buffer, bufsize))
46 != NULL)
47 if (strcmp (mount_point.mnt_type, "proc") == 0)
49 result = mount_point.mnt_dir;
50 break;
52 __endmntent (fp);
55 return result;
59 /* How we can determine the number of available processors depends on
60 the configuration. There is currently (as of version 2.0.21) no
61 system call to determine the number. It is planned for the 2.1.x
62 series to add this, though.
64 One possibility to implement it for systems using Linux 2.0 is to
65 examine the pseudo file /proc/meminfo. Here we have one entry for
66 each processor.
68 But not all systems have support for the /proc filesystem. If it
69 is not available we simply return 1 since there is no way. */
70 int
71 __get_nprocs ()
73 FILE *fp;
74 char buffer[8192];
75 char *proc_path;
76 int result = 1;
78 /* XXX Here will come a test for the new system call. */
80 /* Get mount point of proc filesystem. */
81 proc_path = get_proc_path (buffer, sizeof buffer);
83 /* If we haven't found an appropriate entry return 1. */
84 if (proc_path != NULL)
86 char *proc_cpuinfo = alloca (strlen (proc_path) + sizeof ("/cpuinfo"));
87 __stpcpy (__stpcpy (proc_cpuinfo, proc_path), "/cpuinfo");
89 fp = fopen (proc_cpuinfo, "r");
90 if (fp != NULL)
92 result = 0;
93 /* Read all lines and count the lines starting with the
94 string "processor". We don't have to fear extremely long
95 lines since the kernel will not generate them. 8192
96 bytes are really enough. */
97 while (fgets (buffer, sizeof buffer, fp) != NULL)
98 if (strncmp (buffer, "processor", 9) == 0)
99 ++result;
101 fclose (fp);
105 return result;
107 weak_alias (__get_nprocs, get_nprocs)
109 /* As far as I know Linux has no separate numbers for configured and
110 available processors. So make the `get_nprocs_conf' function an
111 alias. */
112 strong_alias (__get_nprocs, __get_nprocs_conf)
113 weak_alias (__get_nprocs, get_nprocs_conf)
116 /* General function to get information about memory status from proc
117 filesystem. */
118 static int
119 internal_function
120 phys_pages_info (const char *format)
122 FILE *fp;
123 char buffer[8192];
124 char *proc_path;
125 int result = -1;
127 /* Get mount point of proc filesystem. */
128 proc_path = get_proc_path (buffer, sizeof buffer);
130 /* If we haven't found an appropriate entry return 1. */
131 if (proc_path != NULL)
133 char *proc_meminfo = alloca (strlen (proc_path) + sizeof ("/meminfo"));
134 __stpcpy (__stpcpy (proc_meminfo, proc_path), "/meminfo");
136 fp = fopen (proc_meminfo, "r");
137 if (fp != NULL)
139 result = 0;
140 /* Read all lines and count the lines starting with the
141 string "processor". We don't have to fear extremely long
142 lines since the kernel will not generate them. 8192
143 bytes are really enough. */
144 while (fgets (buffer, sizeof buffer, fp) != NULL)
145 if (sscanf (buffer, format, &result) == 1)
147 result /= (__getpagesize () / 1024);
148 break;
151 fclose (fp);
155 if (result == -1)
156 /* We cannot get the needed value: signal an error. */
157 __set_errno (ENOSYS);
159 return result;
163 /* Return the number of pages of physical memory in the system. There
164 is currently (as of version 2.0.21) no system call to determine the
165 number. It is planned for the 2.1.x series to add this, though.
167 One possibility to implement it for systems using Linux 2.0 is to
168 examine the pseudo file /proc/cpuinfo. Here we have one entry for
169 each processor.
171 But not all systems have support for the /proc filesystem. If it
172 is not available we return -1 as an error signal. */
174 __get_phys_pages ()
176 /* XXX Here will come a test for the new system call. */
178 return phys_pages_info ("MemTotal: %d kB");
180 weak_alias (__get_phys_pages, get_phys_pages)
183 /* Return the number of available pages of physical memory in the
184 system. There is currently (as of version 2.0.21) no system call
185 to determine the number. It is planned for the 2.1.x series to add
186 this, though.
188 One possibility to implement it for systems using Linux 2.0 is to
189 examine the pseudo file /proc/cpuinfo. Here we have one entry for
190 each processor.
192 But not all systems have support for the /proc filesystem. If it
193 is not available we return -1 as an error signal. */
195 __get_avphys_pages ()
197 /* XXX Here will come a test for the new system call. */
199 return phys_pages_info ("MemFree: %d kB");
201 weak_alias (__get_avphys_pages, get_avphys_pages)