My slstatus configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

93 行
1.8 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. 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. perc = 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. 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/sysctl.h>
  61. const char *
  62. cpu_freq(void)
  63. {
  64. int freq, mib[2];
  65. size_t size;
  66. mib[0] = CTL_HW;
  67. mib[1] = HW_CPUSPEED;
  68. size = sizeof(freq);
  69. if (sysctl(mib, 2, &freq, &size, NULL, 0) == -1) {
  70. fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n",
  71. strerror(errno));
  72. return NULL;
  73. }
  74. return bprintf("%d", freq);
  75. }
  76. #endif