cpu.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "../util.h"
  6. #if defined(__linux__)
  7. const char *
  8. cpu_freq(void)
  9. {
  10. uintmax_t freq;
  11. /* in kHz */
  12. if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/"
  13. "scaling_cur_freq", "%ju", &freq) != 1) {
  14. return NULL;
  15. }
  16. return fmt_human(freq * 1000, 1000);
  17. }
  18. const char *
  19. cpu_perc(void)
  20. {
  21. static long double a[7];
  22. long double b[7];
  23. memcpy(b, a, sizeof(b));
  24. /* cpu user nice system idle iowait irq softirq */
  25. if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
  26. &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6])
  27. != 7) {
  28. return NULL;
  29. }
  30. if (b[0] == 0) {
  31. return NULL;
  32. }
  33. return bprintf("%d", (int)(100 *
  34. ((b[0] + b[1] + b[2] + b[5] + b[6]) -
  35. (a[0] + a[1] + a[2] + a[5] + a[6])) /
  36. ((b[0] + b[1] + b[2] + b[3] + b[4] + b[5] +
  37. b[6]) -
  38. (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] +
  39. a[6]))));
  40. }
  41. #elif defined(__OpenBSD__)
  42. #include <sys/param.h>
  43. #include <sys/sched.h>
  44. #include <sys/sysctl.h>
  45. const char *
  46. cpu_freq(void)
  47. {
  48. int freq, mib[2];
  49. size_t size;
  50. mib[0] = CTL_HW;
  51. mib[1] = HW_CPUSPEED;
  52. size = sizeof(freq);
  53. /* in MHz */
  54. if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
  55. warn("sysctl 'HW_CPUSPEED':");
  56. return NULL;
  57. }
  58. return fmt_human(freq * 1E6, 1000);
  59. }
  60. const char *
  61. cpu_perc(void)
  62. {
  63. int mib[2];
  64. static uintmax_t a[CPUSTATES];
  65. uintmax_t b[CPUSTATES];
  66. size_t size;
  67. mib[0] = CTL_KERN;
  68. mib[1] = KERN_CPTIME;
  69. size = sizeof(a);
  70. memcpy(b, a, sizeof(b));
  71. if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) {
  72. warn("sysctl 'KERN_CPTIME':");
  73. return NULL;
  74. }
  75. if (b[0] == 0) {
  76. return NULL;
  77. }
  78. return bprintf("%d", 100 *
  79. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
  80. a[CP_INTR]) -
  81. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] +
  82. b[CP_INTR])) /
  83. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
  84. a[CP_INTR] + a[CP_IDLE]) -
  85. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] +
  86. b[CP_INTR] + b[CP_IDLE])));
  87. }
  88. #elif defined(__FreeBSD__)
  89. #include <sys/param.h>
  90. #include <sys/sysctl.h>
  91. #include <devstat.h>
  92. const char *
  93. cpu_freq(void)
  94. {
  95. int freq;
  96. size_t size;
  97. size = sizeof(freq);
  98. /* in MHz */
  99. if (sysctlbyname("hw.clockrate", &freq, &size, NULL, 0) == -1
  100. || !size) {
  101. warn("sysctlbyname 'hw.clockrate':");
  102. return NULL;
  103. }
  104. return fmt_human(freq * 1E6, 1000);
  105. }
  106. const char *
  107. cpu_perc(void)
  108. {
  109. size_t size;
  110. static long a[CPUSTATES];
  111. long b[CPUSTATES];
  112. size = sizeof(a);
  113. memcpy(b, a, sizeof(b));
  114. if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) == -1
  115. || !size) {
  116. warn("sysctlbyname 'kern.cp_time':");
  117. return NULL;
  118. }
  119. if (b[0] == 0) {
  120. return NULL;
  121. }
  122. return bprintf("%d", 100 *
  123. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
  124. a[CP_INTR]) -
  125. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] +
  126. b[CP_INTR])) /
  127. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
  128. a[CP_INTR] + a[CP_IDLE]) -
  129. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] +
  130. b[CP_INTR] + b[CP_IDLE])));
  131. }
  132. #endif