My slstatus configuration
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.
 
 
 
 

102 lines
2.2 KiB

  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. #elif defined(__OpenBSD__)
  39. #include <sys/param.h>
  40. #include <sys/sched.h>
  41. #include <sys/sysctl.h>
  42. const char *
  43. cpu_freq(void)
  44. {
  45. int freq, mib[2];
  46. size_t size;
  47. mib[0] = CTL_HW;
  48. mib[1] = HW_CPUSPEED;
  49. size = sizeof(freq);
  50. if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
  51. fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n", strerror(errno));
  52. return NULL;
  53. }
  54. return bprintf("%d", freq);
  55. }
  56. const char *
  57. cpu_perc(void)
  58. {
  59. int mib[2], perc;
  60. static int valid;
  61. static long int a[CPUSTATES];
  62. long int b[CPUSTATES];
  63. size_t size;
  64. mib[0] = CTL_KERN;
  65. mib[1] = KERN_CPTIME;
  66. size = sizeof(a);
  67. memcpy(b, a, sizeof(b));
  68. if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) {
  69. fprintf(stderr, "sysctl 'KERN_CPTIME': %s\n", strerror(errno));
  70. return NULL;
  71. }
  72. if (!valid) {
  73. valid = 1;
  74. return NULL;
  75. }
  76. perc = 100 *
  77. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR]) -
  78. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR])) /
  79. ((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. return bprintf("%d", perc);
  82. }
  83. #endif