cpu.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.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. int freq;
  11. return (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
  12. "%i", &freq) == 1) ?
  13. bprintf("%d", (freq + 500) / 1000) : NULL;
  14. }
  15. const char *
  16. cpu_perc(void)
  17. {
  18. int perc;
  19. static long double a[7];
  20. static int valid;
  21. long double b[7];
  22. memcpy(b, a, sizeof(b));
  23. /* cpu user nice system idle iowait irq softirq */
  24. if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
  25. &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != 7) {
  26. return NULL;
  27. }
  28. if (!valid) {
  29. valid = 1;
  30. return NULL;
  31. }
  32. perc = 100 * ((b[0] + b[1] + b[2] + b[5] + b[6]) -
  33. (a[0] + a[1] + a[2] + a[5] + a[6])) /
  34. ((b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) -
  35. (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]));
  36. return bprintf("%d", perc);
  37. }
  38. const char *
  39. cpu_iowait(void)
  40. {
  41. int perc;
  42. static int valid;
  43. static long double a[7];
  44. long double b[7];
  45. memcpy(b, a, sizeof(b));
  46. if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
  47. &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != 7) {
  48. return NULL;
  49. }
  50. if (!valid) {
  51. valid = 1;
  52. return NULL;
  53. }
  54. perc = 100 * ((b[4]) - (a[4])) /
  55. ((b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]) -
  56. (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]));
  57. return bprintf("%d", perc);
  58. }
  59. #elif defined(__OpenBSD__)
  60. #include <sys/param.h>
  61. #include <sys/sched.h>
  62. #include <sys/sysctl.h>
  63. const char *
  64. cpu_freq(void)
  65. {
  66. int freq, mib[2];
  67. size_t size;
  68. mib[0] = CTL_HW;
  69. mib[1] = HW_CPUSPEED;
  70. size = sizeof(freq);
  71. if (sysctl(mib, 2, &freq, &size, NULL, 0) == -1) {
  72. fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n",
  73. strerror(errno));
  74. return NULL;
  75. }
  76. return bprintf("%d", freq);
  77. }
  78. const char *
  79. cpu_perc(void)
  80. {
  81. int mib[2], perc;
  82. static int valid;
  83. static long int a[CPUSTATES];
  84. long int b[CPUSTATES];
  85. size_t size;
  86. mib[0] = CTL_KERN;
  87. mib[1] = KERN_CPTIME;
  88. size = sizeof(a);
  89. memcpy(b, a, sizeof(b));
  90. if (sysctl(mib, 2, &a, &size, NULL, 0) == -1) {
  91. fprintf(stderr, "sysctl 'KERN_CPTIME': %s\n", strerror(errno));
  92. return NULL;
  93. }
  94. if (!valid) {
  95. valid = 1;
  96. return NULL;
  97. }
  98. perc = 100 *
  99. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR]) -
  100. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR])) /
  101. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) -
  102. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]));
  103. return bprintf("%d", perc);
  104. }
  105. #endif