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.
 
 
 
 

104 rivejä
2.2 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. #include <inttypes.h>
  8. #include <stdint.h>
  9. const char *
  10. cpu_freq(void)
  11. {
  12. uint64_t freq;
  13. /* in kHz */
  14. if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
  15. "%"SCNu64, &freq) != 1) {
  16. return NULL;
  17. }
  18. return fmt_human_10(freq * 1000);
  19. }
  20. const char *
  21. cpu_perc(void)
  22. {
  23. static long double a[7];
  24. long double b[7];
  25. memcpy(b, a, sizeof(b));
  26. /* cpu user nice system idle iowait irq softirq */
  27. if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
  28. &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != 7) {
  29. return NULL;
  30. }
  31. if (b[0] == 0) {
  32. return NULL;
  33. }
  34. return bprintf("%d", (int)(100 *
  35. ((b[0] + b[1] + b[2] + b[5] + b[6]) -
  36. (a[0] + a[1] + a[2] + a[5] + a[6])) /
  37. ((b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) -
  38. (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]))));
  39. }
  40. #elif defined(__OpenBSD__)
  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_10((size_t)freq * 1000 * 1000);
  58. }
  59. const char *
  60. cpu_perc(void)
  61. {
  62. int mib[2];
  63. static long int a[CPUSTATES];
  64. long int 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