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.

cpu.c 1.2 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "util.h"
  4. const char *
  5. cpu_freq(void)
  6. {
  7. int freq;
  8. return (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
  9. "%i", &freq) == 1) ?
  10. bprintf("%d", (freq + 500) / 1000) : NULL;
  11. }
  12. const char *
  13. cpu_perc(void)
  14. {
  15. int perc;
  16. static long double a[7];
  17. static int valid;
  18. long double b[7];
  19. memcpy(b, a, sizeof(b));
  20. if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2],
  21. &a[3], &a[4], &a[5], &a[6]) != 7) {
  22. return NULL;
  23. }
  24. if (!valid) {
  25. valid = 1;
  26. return NULL;
  27. }
  28. perc = 100 * ((b[0]+b[1]+b[2]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[5]+a[6])) /
  29. ((b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]));
  30. return bprintf("%d", perc);
  31. }
  32. const char *
  33. cpu_iowait(void)
  34. {
  35. int perc;
  36. static int valid;
  37. static long double a[7];
  38. long double b[7];
  39. memcpy(b, a, sizeof(b));
  40. if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2],
  41. &a[3], &a[4], &a[5], &a[6]) != 7) {
  42. return NULL;
  43. }
  44. if (!valid) {
  45. valid = 1;
  46. return NULL;
  47. }
  48. perc = 100 * ((b[4]) - (a[4])) /
  49. ((b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]));
  50. return bprintf("%d", perc);
  51. }