You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
8.4 KiB

  1. /**
  2. @file
  3. @ingroup util
  4. @brief CPU statistics.
  5. @copyright@parblock
  6. Copyright (c) 1994-1998 The Regents of the Univ. of California.
  7. All rights reserved.
  8. Permission is hereby granted, without written agreement and without license
  9. or royalty fees, to use, copy, modify, and distribute this software and its
  10. documentation for any purpose, provided that the above copyright notice and
  11. the following two paragraphs appear in all copies of this software.
  12. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  13. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  14. OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  15. CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  19. "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
  20. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  21. @endparblock
  22. @copyright@parblock
  23. Copyright (c) 1999-2015, Regents of the University of Colorado
  24. All rights reserved.
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions
  27. are met:
  28. Redistributions of source code must retain the above copyright
  29. notice, this list of conditions and the following disclaimer.
  30. Redistributions in binary form must reproduce the above copyright
  31. notice, this list of conditions and the following disclaimer in the
  32. documentation and/or other materials provided with the distribution.
  33. Neither the name of the University of Colorado nor the names of its
  34. contributors may be used to endorse or promote products derived from
  35. this software without specific prior written permission.
  36. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  40. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  41. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  42. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  46. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  47. POSSIBILITY OF SUCH DAMAGE.
  48. @endparblock
  49. */
  50. #define _BSD_SOURCE
  51. #include "util.h"
  52. #if HAVE_SYS_TIME_H == 1
  53. #include <sys/time.h>
  54. #endif
  55. #if HAVE_SYS_RESOURCE_H == 1
  56. #include <sys/resource.h>
  57. #endif
  58. #ifdef BSD
  59. #if defined(_IBMR2)
  60. #define etext _etext
  61. #define edata _edata
  62. #define end _end
  63. #endif
  64. extern int end, etext, edata;
  65. #endif
  66. #ifdef _WIN32
  67. #include <winsock2.h>
  68. #include <psapi.h>
  69. #endif
  70. /**
  71. @brief Prints CPU statistics.
  72. The amount of detail printed depends on the host operating system.
  73. */
  74. void
  75. util_print_cpu_stats(FILE *fp)
  76. {
  77. #if HAVE_GETRUSAGE == 1 && HAVE_GETRLIMIT == 1
  78. struct rusage rusage;
  79. double user, system, scale;
  80. long text, data;
  81. struct rlimit rlp;
  82. long vm_limit, vm_soft_limit;
  83. char hostname[257];
  84. #ifdef BSD
  85. long vm_text, vm_init_data, vm_uninit_data, vm_sbrk_data;
  86. #endif
  87. /* Get the hostname */
  88. (void) gethostname(hostname, sizeof(hostname));
  89. hostname[sizeof(hostname)-1] = '\0'; /* just in case */
  90. #ifdef BSD
  91. /* Get the virtual memory sizes */
  92. vm_text = (long) (((long) (&etext)) / 1024.0 + 0.5);
  93. vm_init_data = (long) (((long) (&edata) - (long) (&etext)) / 1024.0 + 0.5);
  94. vm_uninit_data = (long) (((long) (&end) - (long) (&edata)) / 1024.0 + 0.5);
  95. vm_sbrk_data = (long) (((long) sbrk(0) - (long) (&end)) / 1024.0 + 0.5);
  96. #endif
  97. /* Get virtual memory limits */
  98. (void) getrlimit(RLIMIT_DATA, &rlp);
  99. vm_limit = (long) (rlp.rlim_max / 1024.0 + 0.5);
  100. vm_soft_limit = (long) (rlp.rlim_cur / 1024.0 + 0.5);
  101. /* Get usage stats */
  102. (void) getrusage(RUSAGE_SELF, &rusage);
  103. user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec * 1e-6;
  104. system = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec * 1e-6;
  105. scale = (user + system)*100.0;
  106. if (scale == 0.0) scale = 0.001;
  107. text = (int) (rusage.ru_ixrss / scale + 0.5);
  108. data = (int) ((rusage.ru_idrss + rusage.ru_isrss) / scale + 0.5);
  109. #elif defined(_WIN32)
  110. char hostname[257];
  111. WSADATA wsaData;
  112. FILETIME creationTime, exitTime, kernelTime, userTime;
  113. double user, system;
  114. MEMORYSTATUSEX statex;
  115. size_t vm_limit;
  116. PROCESS_MEMORY_COUNTERS pmc;
  117. size_t peak_working_set;
  118. long page_faults;
  119. /* Get the hostname */
  120. WSAStartup(MAKEWORD(2, 2), &wsaData);
  121. (void) gethostname(hostname, sizeof(hostname));
  122. hostname[sizeof(hostname)-1] = '\0'; /* just in case */
  123. WSACleanup();
  124. /* Get usage stats */
  125. if (GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime,
  126. &kernelTime, &userTime)) {
  127. ULARGE_INTEGER integerSystemTime, integerUserTime;
  128. integerUserTime.u.LowPart = userTime.dwLowDateTime;
  129. integerUserTime.u.HighPart = userTime.dwHighDateTime;
  130. user = (double) integerUserTime.QuadPart * 1e-7;
  131. integerSystemTime.u.LowPart = kernelTime.dwLowDateTime;
  132. integerSystemTime.u.HighPart = kernelTime.dwHighDateTime;
  133. system = (double) integerSystemTime.QuadPart * 1e-7;
  134. } else {
  135. user = system = 0.0;
  136. }
  137. statex.dwLength = sizeof(statex);
  138. if (GlobalMemoryStatusEx(&statex)) {
  139. vm_limit = (size_t) (statex.ullTotalVirtual / 1024.0 + 0.5);
  140. } else {
  141. vm_limit = 0;
  142. }
  143. if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
  144. peak_working_set = (size_t) (pmc.PeakWorkingSetSize / 1024.0 + 0.5);
  145. page_faults = (long) pmc.PageFaultCount;
  146. } else {
  147. peak_working_set = 0;
  148. page_faults = 0;
  149. }
  150. #endif
  151. #if (HAVE_GETRUSAGE == 1 && HAVE_GETRLIMIT == 1) || defined(_WIN32)
  152. (void) fprintf(fp, "Runtime Statistics\n");
  153. (void) fprintf(fp, "------------------\n");
  154. (void) fprintf(fp, "Machine name: %s\n", hostname);
  155. (void) fprintf(fp, "User time %6.1f seconds\n", user);
  156. (void) fprintf(fp, "System time %6.1f seconds\n\n", system);
  157. #if HAVE_GETRUSAGE == 1 && HAVE_GETRLIMIT == 1
  158. (void) fprintf(fp, "Average resident text size = %5ldK\n", text);
  159. (void) fprintf(fp, "Average resident data+stack size = %5ldK\n", data);
  160. (void) fprintf(fp, "Maximum resident size = %5ldK\n\n",
  161. rusage.ru_maxrss);
  162. #if defined(BSD)
  163. (void) fprintf(fp, "Virtual text size = %5ldK\n",
  164. vm_text);
  165. (void) fprintf(fp, "Virtual data size = %5ldK\n",
  166. vm_init_data + vm_uninit_data + vm_sbrk_data);
  167. (void) fprintf(fp, " data size initialized = %5ldK\n",
  168. vm_init_data);
  169. (void) fprintf(fp, " data size uninitialized = %5ldK\n",
  170. vm_uninit_data);
  171. (void) fprintf(fp, " data size sbrk = %5ldK\n",
  172. vm_sbrk_data);
  173. #endif
  174. (void) fprintf(fp, "Virtual memory limit = ");
  175. if (rlp.rlim_cur == RLIM_INFINITY)
  176. (void) fprintf(fp, "unlimited");
  177. else
  178. (void) fprintf(fp, "%5ldK", vm_soft_limit);
  179. if (rlp.rlim_max == RLIM_INFINITY)
  180. (void) fprintf(fp, " (unlimited)\n");
  181. else
  182. (void) fprintf(fp, " (%ldK)\n\n", vm_limit);
  183. (void) fprintf(fp, "Major page faults = %ld\n", rusage.ru_majflt);
  184. (void) fprintf(fp, "Minor page faults = %ld\n", rusage.ru_minflt);
  185. (void) fprintf(fp, "Swaps = %ld\n", rusage.ru_nswap);
  186. (void) fprintf(fp, "Input blocks = %ld\n", rusage.ru_inblock);
  187. (void) fprintf(fp, "Output blocks = %ld\n", rusage.ru_oublock);
  188. (void) fprintf(fp, "Context switch (voluntary) = %ld\n", rusage.ru_nvcsw);
  189. (void) fprintf(fp, "Context switch (involuntary) = %ld\n", rusage.ru_nivcsw);
  190. #else
  191. (void) fprintf(fp, "Maximum resident size = ");
  192. if (peak_working_set == 0)
  193. (void) fprintf(fp, "unavailable\n");
  194. else
  195. (void) fprintf(fp, "%" PRIszt "K\n", peak_working_set);
  196. (void) fprintf(fp, "Virtual memory limit = ");
  197. if (vm_limit == 0)
  198. (void) fprintf(fp, "unavailable\n");
  199. else
  200. (void) fprintf(fp, "%5" PRIszt "K\n", vm_limit);
  201. (void) fprintf(fp, "Page faults = %ld\n", page_faults);
  202. #endif
  203. #else
  204. (void) fprintf(fp, "Usage statistics not available\n");
  205. #endif
  206. }