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.
 
 
 
 

212 lines
3.9 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. static size_t
  8. pread(const char *path, char *buf, size_t bufsiz)
  9. {
  10. FILE *fp;
  11. size_t bytes_read;
  12. if (!(fp = fopen(path, "r"))) {
  13. fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno));
  14. return 0;
  15. }
  16. if (!(bytes_read = fread(buf, sizeof(char), bufsiz, fp))) {
  17. fprintf(stderr, "fread '%s': %s\n", path, strerror(errno));
  18. fclose(fp);
  19. return 0;
  20. }
  21. fclose(fp);
  22. buf[bytes_read] = '\0';
  23. return bytes_read;
  24. }
  25. const char *
  26. swap_free(void)
  27. {
  28. long total, free;
  29. char *match;
  30. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  31. return NULL;
  32. }
  33. if (!(match = strstr(buf, "SwapTotal"))) {
  34. return NULL;
  35. }
  36. sscanf(match, "SwapTotal: %ld kB\n", &total);
  37. if (!(match = strstr(buf, "SwapFree"))) {
  38. return NULL;
  39. }
  40. sscanf(match, "SwapFree: %ld kB\n", &free);
  41. return bprintf("%f", (float)free / 1024 / 1024);
  42. }
  43. const char *
  44. swap_perc(void)
  45. {
  46. long total, free, cached;
  47. char *match;
  48. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  49. return NULL;
  50. }
  51. if (!(match = strstr(buf, "SwapTotal"))) {
  52. return NULL;
  53. }
  54. sscanf(match, "SwapTotal: %ld kB\n", &total);
  55. if (!(match = strstr(buf, "SwapCached"))) {
  56. return NULL;
  57. }
  58. sscanf(match, "SwapCached: %ld kB\n", &cached);
  59. if (!(match = strstr(buf, "SwapFree"))) {
  60. return NULL;
  61. }
  62. sscanf(match, "SwapFree: %ld kB\n", &free);
  63. return bprintf("%d", 100 * (total - free - cached) / total);
  64. }
  65. const char *
  66. swap_total(void)
  67. {
  68. long total;
  69. char *match;
  70. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  71. return NULL;
  72. }
  73. if (!(match = strstr(buf, "SwapTotal"))) {
  74. return NULL;
  75. }
  76. sscanf(match, "SwapTotal: %ld kB\n", &total);
  77. return bprintf("%f", (float)total / 1024 / 1024);
  78. }
  79. const char *
  80. swap_used(void)
  81. {
  82. long total, free, cached;
  83. char *match;
  84. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  85. return NULL;
  86. }
  87. if (!(match = strstr(buf, "SwapTotal"))) {
  88. return NULL;
  89. }
  90. sscanf(match, "SwapTotal: %ld kB\n", &total);
  91. if (!(match = strstr(buf, "SwapCached"))) {
  92. return NULL;
  93. }
  94. sscanf(match, "SwapCached: %ld kB\n", &cached);
  95. if (!(match = strstr(buf, "SwapFree"))) {
  96. return NULL;
  97. }
  98. sscanf(match, "SwapFree: %ld kB\n", &free);
  99. return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
  100. }
  101. #elif defined(__OpenBSD__)
  102. #include <stdlib.h>
  103. #include <sys/param.h> /* dbtob */
  104. #include <sys/swap.h>
  105. #include <sys/types.h>
  106. #include <unistd.h>
  107. #define dbtoqb(b) dbtob((int64_t)(b))
  108. static void
  109. getstats(int *total, int *used)
  110. {
  111. struct swapent *sep, *fsep;
  112. int rnswap, nswap, i;
  113. nswap = swapctl(SWAP_NSWAP, 0, 0);
  114. if (nswap < 1) {
  115. fprintf(stderr, "swaptctl 'SWAP_NSWAP': %s\n", strerror(errno));
  116. }
  117. fsep = sep = calloc(nswap, sizeof(*sep));
  118. if (!sep) {
  119. fprintf(stderr, "calloc 'nswap': %s\n", strerror(errno));
  120. }
  121. rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
  122. if (rnswap < 0) {
  123. fprintf(stderr, "swapctl 'SWAP_STATA': %s\n", strerror(errno));
  124. }
  125. if (nswap != rnswap) {
  126. fprintf(stderr, "SWAP_STATS != SWAP_NSWAP\n");
  127. }
  128. *total = 0;
  129. *used = 0;
  130. for (i = 0; i < rnswap; i++) {
  131. *total += dbtoqb(sep->se_nblks);
  132. *used += dbtoqb(sep->se_inuse);
  133. }
  134. free(fsep);
  135. }
  136. const char *
  137. swap_free(void)
  138. {
  139. int total, used;
  140. getstats(&total, &used);
  141. return bprintf("%f", (float)(total - used) / 1024 / 1024 / 1024);
  142. }
  143. const char *
  144. swap_perc(void)
  145. {
  146. int total, used;
  147. getstats(&total, &used);
  148. return bprintf("%d", 100 * used / total);
  149. }
  150. const char *
  151. swap_total(void)
  152. {
  153. int total, used;
  154. getstats(&total, &used);
  155. return bprintf("%f", (float)total / 1024 / 1024 / 1024);
  156. }
  157. const char *
  158. swap_used(void)
  159. {
  160. int total, used;
  161. getstats(&total, &used);
  162. return bprintf("%f", (float)used / 1024 / 1024 / 1024);
  163. }
  164. #endif