vtimes.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <stddef.h>
  15. #include <sys/resource.h>
  16. #include <shlib-compat.h>
  17. #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_33)
  18. /* Granularity of the `vm_utime' and `vm_stime' fields of a `struct vtimes'.
  19. (This is the frequency of the machine's power supply, in Hz.) */
  20. # define VTIMES_UNITS_PER_SECOND 60
  21. struct vtimes
  22. {
  23. /* User time used in units of 1/VTIMES_UNITS_PER_SECOND seconds. */
  24. int vm_utime;
  25. /* System time used in units of 1/VTIMES_UNITS_PER_SECOND seconds. */
  26. int vm_stime;
  27. /* Amount of data and stack memory used (kilobyte-seconds). */
  28. unsigned int vm_idsrss;
  29. /* Amount of text memory used (kilobyte-seconds). */
  30. unsigned int vm_ixrss;
  31. /* Maximum resident set size (text, data, and stack) (kilobytes). */
  32. int vm_maxrss;
  33. /* Number of hard page faults (i.e. those that required I/O). */
  34. int vm_majflt;
  35. /* Number of soft page faults (i.e. those serviced by reclaiming
  36. a page from the list of pages awaiting reallocation. */
  37. int vm_minflt;
  38. /* Number of times a process was swapped out of physical memory. */
  39. int vm_nswap;
  40. /* Number of input operations via the file system. Note: This
  41. and `ru_oublock' do not include operations with the cache. */
  42. int vm_inblk;
  43. /* Number of output operations via the file system. */
  44. int vm_oublk;
  45. };
  46. /* Return the number of 1/VTIMES_UNITS_PER_SECOND-second
  47. units in the `struct timeval' TV. */
  48. # define TIMEVAL_TO_VTIMES(tv) \
  49. ((tv.tv_sec * VTIMES_UNITS_PER_SECOND) \
  50. + (tv.tv_usec * VTIMES_UNITS_PER_SECOND / 1000000))
  51. /* If VT is not NULL, write statistics for WHO into *VT.
  52. Return 0 for success, -1 for failure. */
  53. static int
  54. vtimes_one (struct vtimes *vt, enum __rusage_who who)
  55. {
  56. if (vt != NULL)
  57. {
  58. struct rusage usage;
  59. if (__getrusage (who, &usage) < 0)
  60. return -1;
  61. vt->vm_utime = TIMEVAL_TO_VTIMES (usage.ru_utime);
  62. vt->vm_stime = TIMEVAL_TO_VTIMES (usage.ru_stime);
  63. vt->vm_idsrss = usage.ru_idrss + usage.ru_isrss;
  64. vt->vm_majflt = usage.ru_majflt;
  65. vt->vm_minflt = usage.ru_minflt;
  66. vt->vm_nswap = usage.ru_nswap;
  67. vt->vm_inblk = usage.ru_inblock;
  68. vt->vm_oublk = usage.ru_oublock;
  69. }
  70. return 0;
  71. }
  72. /* If CURRENT is not NULL, write statistics for the current process into
  73. *CURRENT. If CHILD is not NULL, write statistics for all terminated child
  74. processes into *CHILD. Returns 0 for success, -1 for failure. */
  75. int
  76. __vtimes (struct vtimes *current, struct vtimes *child)
  77. {
  78. if (vtimes_one (current, RUSAGE_SELF) < 0
  79. || vtimes_one (child, RUSAGE_CHILDREN) < 0)
  80. return -1;
  81. return 0;
  82. }
  83. compat_symbol (libc, __vtimes, vtimes, GLIBC_2_0);
  84. #endif /* SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_33) */