My slstatus configuration
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

swap.c 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. warn("fopen '%s':", path);
  14. return 0;
  15. }
  16. if (!(bytes_read = fread(buf, sizeof(char), bufsiz, fp))) {
  17. warn("fread '%s':", path);
  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. static void
  108. getstats(int *total, int *used)
  109. {
  110. struct swapent *sep, *fsep;
  111. int rnswap, nswap, i;
  112. nswap = swapctl(SWAP_NSWAP, 0, 0);
  113. if (nswap < 1) {
  114. warn("swaptctl 'SWAP_NSWAP':");
  115. }
  116. fsep = sep = calloc(nswap, sizeof(*sep));
  117. if (!sep) {
  118. warn("calloc 'nswap':");
  119. }
  120. rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
  121. if (rnswap < 0) {
  122. warn("swapctl 'SWAP_STATA':");
  123. }
  124. if (nswap != rnswap) {
  125. warn("getstats: SWAP_STATS != SWAP_NSWAP");
  126. }
  127. *total = 0;
  128. *used = 0;
  129. for (i = 0; i < rnswap; i++) {
  130. *total += sep->se_nblks >> 1;
  131. *used += sep->se_inuse >> 1;
  132. }
  133. free(fsep);
  134. }
  135. const char *
  136. swap_free(void)
  137. {
  138. int total, used;
  139. getstats(&total, &used);
  140. return bprintf("%f", (float)(total - used) / 1024 / 1024);
  141. }
  142. const char *
  143. swap_perc(void)
  144. {
  145. int total, used;
  146. getstats(&total, &used);
  147. return bprintf("%d", 100 * used / total);
  148. }
  149. const char *
  150. swap_total(void)
  151. {
  152. int total, used;
  153. getstats(&total, &used);
  154. return bprintf("%f", (float)total / 1024 / 1024);
  155. }
  156. const char *
  157. swap_used(void)
  158. {
  159. int total, used;
  160. getstats(&total, &used);
  161. return bprintf("%f", (float)used / 1024 / 1024);
  162. }
  163. #endif