cpu.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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], sum;
  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. sum = (b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) -
  34. (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]);
  35. if (sum == 0) {
  36. return NULL;
  37. }
  38. return bprintf("%d", (int)(100 *
  39. ((b[0] + b[1] + b[2] + b[5] + b[6]) -
  40. (a[0] + a[1] + a[2] + a[5] + a[6])) / sum));
  41. }
  42. #elif defined(__OpenBSD__)
  43. #include <sys/param.h>
  44. #include <sys/sched.h>
  45. #include <sys/sysctl.h>
  46. const char *
  47. cpu_freq(void)
  48. {
  49. int freq, mib[2];
  50. size_t size;
  51. mib[0] = CTL_HW;
  52. mib[1] = HW_CPUSPEED;
  53. size = sizeof(freq);
  54. /* in MHz */
  55. if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
  56. warn("sysctl 'HW_CPUSPEED':");
  57. return NULL;
  58. }
  59. return fmt_human(freq * 1E6, 1000);
  60. }
  61. const char *
  62. cpu_perc(void)
  63. {
  64. int mib[2];
  65. static uintmax_t a[CPUSTATES];
  66. uintmax_t b[CPUSTATES], sum;
  67. size_t size;
  68. mib[0] = CTL_KERN;
  69. mib[1] = KERN_CPTIME;
  70. size = sizeof(a);
  71. memcpy(b, a, sizeof(b));
  72. if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) {
  73. warn("sysctl 'KERN_CPTIME':");
  74. return NULL;
  75. }
  76. if (b[0] == 0) {
  77. return NULL;
  78. }
  79. sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) -
  80. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]);
  81. if (sum == 0) {
  82. return NULL;
  83. }
  84. return bprintf("%d", 100 *
  85. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
  86. a[CP_INTR]) -
  87. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] +
  88. b[CP_INTR])) / sum);
  89. }
  90. #elif defined(__FreeBSD__)
  91. #include <sys/param.h>
  92. #include <sys/sysctl.h>
  93. #include <devstat.h>
  94. const char *
  95. cpu_freq(void)
  96. {
  97. int freq;
  98. size_t size;
  99. size = sizeof(freq);
  100. /* in MHz */
  101. if (sysctlbyname("hw.clockrate", &freq, &size, NULL, 0) == -1
  102. || !size) {
  103. warn("sysctlbyname 'hw.clockrate':");
  104. return NULL;
  105. }
  106. return fmt_human(freq * 1E6, 1000);
  107. }
  108. const char *
  109. cpu_perc(void)
  110. {
  111. size_t size;
  112. static long a[CPUSTATES];
  113. long b[CPUSTATES], sum;
  114. size = sizeof(a);
  115. memcpy(b, a, sizeof(b));
  116. if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) == -1
  117. || !size) {
  118. warn("sysctlbyname 'kern.cp_time':");
  119. return NULL;
  120. }
  121. if (b[0] == 0) {
  122. return NULL;
  123. }
  124. sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) -
  125. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]);
  126. if (sum == 0) {
  127. return NULL;
  128. }
  129. return bprintf("%d", 100 *
  130. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
  131. a[CP_INTR]) -
  132. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] +
  133. b[CP_INTR])) / sum);
  134. }
  135. #endif