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.
 
 
 
 

100 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. static int valid;
  19. static long double a[7];
  20. long double b[7];
  21. memcpy(b, a, sizeof(b));
  22. /* cpu user nice system idle iowait irq softirq */
  23. if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
  24. &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != 7) {
  25. return NULL;
  26. }
  27. if (!valid) {
  28. valid = 1;
  29. return NULL;
  30. }
  31. return bprintf("%d", (int)(100 *
  32. ((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. }
  37. #elif defined(__OpenBSD__)
  38. #include <sys/param.h>
  39. #include <sys/sched.h>
  40. #include <sys/sysctl.h>
  41. const char *
  42. cpu_freq(void)
  43. {
  44. int freq, mib[2];
  45. size_t size;
  46. mib[0] = CTL_HW;
  47. mib[1] = HW_CPUSPEED;
  48. size = sizeof(freq);
  49. if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
  50. fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n", strerror(errno));
  51. return NULL;
  52. }
  53. return bprintf("%d", freq);
  54. }
  55. const char *
  56. cpu_perc(void)
  57. {
  58. int mib[2];
  59. static int valid;
  60. static long int a[CPUSTATES];
  61. long int b[CPUSTATES];
  62. size_t size;
  63. mib[0] = CTL_KERN;
  64. mib[1] = KERN_CPTIME;
  65. size = sizeof(a);
  66. memcpy(b, a, sizeof(b));
  67. if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) {
  68. fprintf(stderr, "sysctl 'KERN_CPTIME': %s\n", strerror(errno));
  69. return NULL;
  70. }
  71. if (!valid) {
  72. valid = 1;
  73. return NULL;
  74. }
  75. return bprintf("%d", 100 *
  76. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR]) -
  77. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR])) /
  78. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] +
  79. a[CP_IDLE]) -
  80. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] +
  81. b[CP_IDLE])));
  82. }
  83. #endif