My slstatus configuration
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

89 rader
1.7 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #if defined(__OpenBSD__)
  6. #include <sys/sysctl.h>
  7. #endif
  8. #include "../util.h"
  9. #if defined(__linux__)
  10. const char *
  11. cpu_freq(void)
  12. {
  13. int freq;
  14. return (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
  15. "%i", &freq) == 1) ?
  16. bprintf("%d", (freq + 500) / 1000) : NULL;
  17. }
  18. const char *
  19. cpu_perc(void)
  20. {
  21. int perc;
  22. static long double a[7];
  23. static int valid;
  24. long double b[7];
  25. memcpy(b, a, sizeof(b));
  26. if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2],
  27. &a[3], &a[4], &a[5], &a[6]) != 7) {
  28. return NULL;
  29. }
  30. if (!valid) {
  31. valid = 1;
  32. return NULL;
  33. }
  34. perc = 100 * ((b[0]+b[1]+b[2]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[5]+a[6])) /
  35. ((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]));
  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", &a[0], &a[1], &a[2],
  47. &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]) - (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]));
  56. return bprintf("%d", perc);
  57. }
  58. #elif defined(__OpenBSD__)
  59. const char *
  60. cpu_freq(void)
  61. {
  62. int freq, mib[2];
  63. size_t size;
  64. mib[0] = CTL_HW;
  65. mib[1] = HW_CPUSPEED;
  66. size = sizeof(freq);
  67. if (sysctl(mib, 2, &freq, &size, NULL, 0) == -1) {
  68. fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n", strerror(errno));
  69. return NULL;
  70. }
  71. return bprintf("%d", freq);
  72. }
  73. #endif