getsysstats.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* Determine various system internal values, Linux version.
  2. Copyright (C) 1996-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <alloca.h>
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include <dirent.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <mntent.h>
  23. #include <paths.h>
  24. #include <stdio.h>
  25. #include <stdio_ext.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <sys/sysinfo.h>
  30. #include <atomic.h>
  31. #include <not-cancel.h>
  32. /* How we can determine the number of available processors depends on
  33. the configuration. There is currently (as of version 2.0.21) no
  34. system call to determine the number. It is planned for the 2.1.x
  35. series to add this, though.
  36. One possibility to implement it for systems using Linux 2.0 is to
  37. examine the pseudo file /proc/cpuinfo. Here we have one entry for
  38. each processor.
  39. But not all systems have support for the /proc filesystem. If it
  40. is not available we simply return 1 since there is no way. */
  41. /* Other architectures use different formats for /proc/cpuinfo. This
  42. provides a hook for alternative parsers. */
  43. #ifndef GET_NPROCS_PARSER
  44. # define GET_NPROCS_PARSER(FD, BUFFER, CP, RE, BUFFER_END, RESULT) \
  45. do \
  46. { \
  47. (RESULT) = 0; \
  48. /* Read all lines and count the lines starting with the string \
  49. "processor". We don't have to fear extremely long lines since \
  50. the kernel will not generate them. 8192 bytes are really \
  51. enough. */ \
  52. char *l; \
  53. while ((l = next_line (FD, BUFFER, &CP, &RE, BUFFER_END)) != NULL) \
  54. if (strncmp (l, "processor", 9) == 0) \
  55. ++(RESULT); \
  56. } \
  57. while (0)
  58. #endif
  59. static char *
  60. next_line (int fd, char *const buffer, char **cp, char **re,
  61. char *const buffer_end)
  62. {
  63. char *res = *cp;
  64. char *nl = memchr (*cp, '\n', *re - *cp);
  65. if (nl == NULL)
  66. {
  67. if (*cp != buffer)
  68. {
  69. if (*re == buffer_end)
  70. {
  71. memmove (buffer, *cp, *re - *cp);
  72. *re = buffer + (*re - *cp);
  73. *cp = buffer;
  74. ssize_t n = __read_nocancel (fd, *re, buffer_end - *re);
  75. if (n < 0)
  76. return NULL;
  77. *re += n;
  78. nl = memchr (*cp, '\n', *re - *cp);
  79. while (nl == NULL && *re == buffer_end)
  80. {
  81. /* Truncate too long lines. */
  82. *re = buffer + 3 * (buffer_end - buffer) / 4;
  83. n = __read_nocancel (fd, *re, buffer_end - *re);
  84. if (n < 0)
  85. return NULL;
  86. nl = memchr (*re, '\n', n);
  87. **re = '\n';
  88. *re += n;
  89. }
  90. }
  91. else
  92. nl = memchr (*cp, '\n', *re - *cp);
  93. res = *cp;
  94. }
  95. if (nl == NULL)
  96. nl = *re - 1;
  97. }
  98. *cp = nl + 1;
  99. assert (*cp <= *re);
  100. return res == *re ? NULL : res;
  101. }
  102. int
  103. __get_nprocs (void)
  104. {
  105. static int cached_result = -1;
  106. static time_t timestamp;
  107. time_t now = time (NULL);
  108. time_t prev = timestamp;
  109. atomic_read_barrier ();
  110. if (now == prev && cached_result > -1)
  111. return cached_result;
  112. /* XXX Here will come a test for the new system call. */
  113. const size_t buffer_size = __libc_use_alloca (8192) ? 8192 : 512;
  114. char *buffer = alloca (buffer_size);
  115. char *buffer_end = buffer + buffer_size;
  116. char *cp = buffer_end;
  117. char *re = buffer_end;
  118. const int flags = O_RDONLY | O_CLOEXEC;
  119. int fd = __open_nocancel ("/sys/devices/system/cpu/online", flags);
  120. char *l;
  121. int result = 0;
  122. if (fd != -1)
  123. {
  124. l = next_line (fd, buffer, &cp, &re, buffer_end);
  125. if (l != NULL)
  126. do
  127. {
  128. char *endp;
  129. unsigned long int n = strtoul (l, &endp, 10);
  130. if (l == endp)
  131. {
  132. result = 0;
  133. break;
  134. }
  135. unsigned long int m = n;
  136. if (*endp == '-')
  137. {
  138. l = endp + 1;
  139. m = strtoul (l, &endp, 10);
  140. if (l == endp)
  141. {
  142. result = 0;
  143. break;
  144. }
  145. }
  146. result += m - n + 1;
  147. l = endp;
  148. while (l < re && isspace (*l))
  149. ++l;
  150. }
  151. while (l < re);
  152. __close_nocancel_nostatus (fd);
  153. if (result > 0)
  154. goto out;
  155. }
  156. cp = buffer_end;
  157. re = buffer_end;
  158. /* Default to an SMP system in case we cannot obtain an accurate
  159. number. */
  160. result = 2;
  161. /* The /proc/stat format is more uniform, use it by default. */
  162. fd = __open_nocancel ("/proc/stat", flags);
  163. if (fd != -1)
  164. {
  165. result = 0;
  166. while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL)
  167. /* The current format of /proc/stat has all the cpu* entries
  168. at the front. We assume here that stays this way. */
  169. if (strncmp (l, "cpu", 3) != 0)
  170. break;
  171. else if (isdigit (l[3]))
  172. ++result;
  173. __close_nocancel_nostatus (fd);
  174. }
  175. else
  176. {
  177. fd = __open_nocancel ("/proc/cpuinfo", flags);
  178. if (fd != -1)
  179. {
  180. GET_NPROCS_PARSER (fd, buffer, cp, re, buffer_end, result);
  181. __close_nocancel_nostatus (fd);
  182. }
  183. }
  184. out:
  185. cached_result = result;
  186. atomic_write_barrier ();
  187. timestamp = now;
  188. return result;
  189. }
  190. libc_hidden_def (__get_nprocs)
  191. weak_alias (__get_nprocs, get_nprocs)
  192. /* On some architectures it is possible to distinguish between configured
  193. and active cpus. */
  194. int
  195. __get_nprocs_conf (void)
  196. {
  197. /* XXX Here will come a test for the new system call. */
  198. /* Try to use the sysfs filesystem. It has actual information about
  199. online processors. */
  200. DIR *dir = __opendir ("/sys/devices/system/cpu");
  201. if (dir != NULL)
  202. {
  203. int count = 0;
  204. struct dirent64 *d;
  205. while ((d = __readdir64 (dir)) != NULL)
  206. /* NB: the sysfs has d_type support. */
  207. if (d->d_type == DT_DIR && strncmp (d->d_name, "cpu", 3) == 0)
  208. {
  209. char *endp;
  210. unsigned long int nr = strtoul (d->d_name + 3, &endp, 10);
  211. if (nr != ULONG_MAX && endp != d->d_name + 3 && *endp == '\0')
  212. ++count;
  213. }
  214. __closedir (dir);
  215. return count;
  216. }
  217. int result = 1;
  218. #ifdef GET_NPROCS_CONF_PARSER
  219. /* If we haven't found an appropriate entry return 1. */
  220. FILE *fp = fopen ("/proc/cpuinfo", "rce");
  221. if (fp != NULL)
  222. {
  223. char buffer[8192];
  224. /* No threads use this stream. */
  225. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  226. GET_NPROCS_CONF_PARSER (fp, buffer, result);
  227. fclose (fp);
  228. }
  229. #else
  230. result = __get_nprocs ();
  231. #endif
  232. return result;
  233. }
  234. libc_hidden_def (__get_nprocs_conf)
  235. weak_alias (__get_nprocs_conf, get_nprocs_conf)
  236. /* Compute (num*mem_unit)/pagesize, but avoid overflowing long int.
  237. In practice, mem_unit is never bigger than the page size, so after
  238. the first loop it is 1. [In the kernel, it is initialized to
  239. PAGE_SIZE in mm/page_alloc.c:si_meminfo(), and then in
  240. kernel.sys.c:do_sysinfo() it is set to 1 if unsigned long can
  241. represent all the sizes measured in bytes]. */
  242. static long int
  243. sysinfo_mempages (unsigned long int num, unsigned int mem_unit)
  244. {
  245. unsigned long int ps = __getpagesize ();
  246. while (mem_unit > 1 && ps > 1)
  247. {
  248. mem_unit >>= 1;
  249. ps >>= 1;
  250. }
  251. num *= mem_unit;
  252. while (ps > 1)
  253. {
  254. ps >>= 1;
  255. num >>= 1;
  256. }
  257. return num;
  258. }
  259. /* Return the number of pages of total/available physical memory in
  260. the system. This used to be done by parsing /proc/meminfo, but
  261. that's unnecessarily expensive (and /proc is not always available).
  262. The sysinfo syscall provides the same information, and has been
  263. available at least since kernel 2.3.48. */
  264. long int
  265. __get_phys_pages (void)
  266. {
  267. struct sysinfo info;
  268. __sysinfo (&info);
  269. return sysinfo_mempages (info.totalram, info.mem_unit);
  270. }
  271. libc_hidden_def (__get_phys_pages)
  272. weak_alias (__get_phys_pages, get_phys_pages)
  273. long int
  274. __get_avphys_pages (void)
  275. {
  276. struct sysinfo info;
  277. __sysinfo (&info);
  278. return sysinfo_mempages (info.freeram, info.mem_unit);
  279. }
  280. libc_hidden_def (__get_avphys_pages)
  281. weak_alias (__get_avphys_pages, get_avphys_pages)