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.

89 lines
3.1 KiB

  1. /* LINTLIBRARY */
  2. #include "util.h"
  3. #ifdef BSD
  4. #include <sys/types.h>
  5. #include <sys/time.h>
  6. #include <sys/resource.h>
  7. #if defined(_IBMR2)
  8. #define etext _etext
  9. #define edata _edata
  10. #define end _end
  11. #endif
  12. extern int end, etext, edata;
  13. #endif
  14. void
  15. util_print_cpu_stats(FILE *fp)
  16. {
  17. #ifdef BSD
  18. struct rusage rusage;
  19. struct rlimit rlp;
  20. long text, data, vm_limit, vm_soft_limit;
  21. double user, system, scale;
  22. char hostname[257];
  23. long vm_text, vm_init_data, vm_uninit_data, vm_sbrk_data;
  24. /* Get the hostname */
  25. (void) gethostname(hostname, 256);
  26. hostname[256] = '\0'; /* just in case */
  27. /* Get the virtual memory sizes */
  28. vm_text = (long) (((long) (&etext)) / 1024.0 + 0.5);
  29. vm_init_data = (long) (((long) (&edata) - (long) (&etext)) / 1024.0 + 0.5);
  30. vm_uninit_data = (long) (((long) (&end) - (long) (&edata)) / 1024.0 + 0.5);
  31. vm_sbrk_data = (long) (((long) sbrk(0) - (long) (&end)) / 1024.0 + 0.5);
  32. /* Get virtual memory limits */
  33. (void) getrlimit(RLIMIT_DATA, &rlp);
  34. vm_limit = (long) (rlp.rlim_max / 1024.0 + 0.5);
  35. vm_soft_limit = (long) (rlp.rlim_cur / 1024.0 + 0.5);
  36. /* Get usage stats */
  37. (void) getrusage(RUSAGE_SELF, &rusage);
  38. user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec/1.0e6;
  39. system = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec/1.0e6;
  40. scale = (user + system)*100.0;
  41. if (scale == 0.0) scale = 0.001;
  42. (void) fprintf(fp, "Runtime Statistics\n");
  43. (void) fprintf(fp, "------------------\n");
  44. (void) fprintf(fp, "Machine name: %s\n", hostname);
  45. (void) fprintf(fp, "User time %6.1f seconds\n", user);
  46. (void) fprintf(fp, "System time %6.1f seconds\n\n", system);
  47. text = (int) (rusage.ru_ixrss / scale + 0.5);
  48. data = (int) ((rusage.ru_idrss + rusage.ru_isrss) / scale + 0.5);
  49. (void) fprintf(fp, "Average resident text size = %5ldK\n", text);
  50. (void) fprintf(fp, "Average resident data+stack size = %5ldK\n", data);
  51. (void) fprintf(fp, "Maximum resident size = %5ldK\n\n",
  52. rusage.ru_maxrss/2);
  53. (void) fprintf(fp, "Virtual text size = %5ldK\n",
  54. vm_text);
  55. (void) fprintf(fp, "Virtual data size = %5ldK\n",
  56. vm_init_data + vm_uninit_data + vm_sbrk_data);
  57. (void) fprintf(fp, " data size initialized = %5ldK\n",
  58. vm_init_data);
  59. (void) fprintf(fp, " data size uninitialized = %5ldK\n",
  60. vm_uninit_data);
  61. (void) fprintf(fp, " data size sbrk = %5ldK\n",
  62. vm_sbrk_data);
  63. (void) fprintf(fp, "Virtual memory limit = %5ldK (%ldK)\n\n",
  64. vm_soft_limit, vm_limit);
  65. (void) fprintf(fp, "Major page faults = %ld\n", rusage.ru_majflt);
  66. (void) fprintf(fp, "Minor page faults = %ld\n", rusage.ru_minflt);
  67. (void) fprintf(fp, "Swaps = %ld\n", rusage.ru_nswap);
  68. (void) fprintf(fp, "Input blocks = %ld\n", rusage.ru_inblock);
  69. (void) fprintf(fp, "Output blocks = %ld\n", rusage.ru_oublock);
  70. (void) fprintf(fp, "Context switch (voluntary) = %ld\n", rusage.ru_nvcsw);
  71. (void) fprintf(fp, "Context switch (involuntary) = %ld\n", rusage.ru_nivcsw);
  72. #else
  73. (void) fprintf(fp, "Usage statistics not available\n");
  74. #endif
  75. }