My slstatus configuration
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

107 Zeilen
2.2 KiB

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