My slstatus configuration
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

104 wiersze
2.2 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "../util.h"
  5. #if defined(__linux__)
  6. #include <inttypes.h>
  7. #include <stdint.h>
  8. const char *
  9. cpu_freq(void)
  10. {
  11. uintmax_t freq;
  12. /* in kHz */
  13. if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
  14. "%" PRIuMAX, &freq) != 1) {
  15. return NULL;
  16. }
  17. return fmt_human(freq * 1000, 1000);
  18. }
  19. const char *
  20. cpu_perc(void)
  21. {
  22. static long double a[7];
  23. long double b[7];
  24. memcpy(b, a, sizeof(b));
  25. /* cpu user nice system idle iowait irq softirq */
  26. if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
  27. &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != 7) {
  28. return NULL;
  29. }
  30. if (b[0] == 0) {
  31. return NULL;
  32. }
  33. return bprintf("%d", (int)(100 *
  34. ((b[0] + b[1] + b[2] + b[5] + b[6]) -
  35. (a[0] + a[1] + a[2] + a[5] + a[6])) /
  36. ((b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) -
  37. (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]))));
  38. }
  39. #elif defined(__OpenBSD__)
  40. #include <inttypes.h>
  41. #include <sys/param.h>
  42. #include <sys/sched.h>
  43. #include <sys/sysctl.h>
  44. const char *
  45. cpu_freq(void)
  46. {
  47. int freq, mib[2];
  48. size_t size;
  49. mib[0] = CTL_HW;
  50. mib[1] = HW_CPUSPEED;
  51. size = sizeof(freq);
  52. /* in MHz */
  53. if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
  54. warn("sysctl 'HW_CPUSPEED':");
  55. return NULL;
  56. }
  57. return fmt_human((size_t)freq * 1000 * 1000, 1000);
  58. }
  59. const char *
  60. cpu_perc(void)
  61. {
  62. int mib[2];
  63. static uintmax_t a[CPUSTATES];
  64. uintmax_t b[CPUSTATES];
  65. size_t size;
  66. mib[0] = CTL_KERN;
  67. mib[1] = KERN_CPTIME;
  68. size = sizeof(a);
  69. memcpy(b, a, sizeof(b));
  70. if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) {
  71. warn("sysctl 'KERN_CPTIME':");
  72. return NULL;
  73. }
  74. if (b[0] == 0) {
  75. return NULL;
  76. }
  77. return bprintf("%d", 100 *
  78. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR]) -
  79. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR])) /
  80. ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] +
  81. a[CP_IDLE]) -
  82. (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] +
  83. b[CP_IDLE])));
  84. }
  85. #endif