swap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "../util.h"
  8. #if defined(__linux__)
  9. static int
  10. get_swap_info(long *s_total, long *s_free, long *s_cached)
  11. {
  12. FILE *fp;
  13. struct {
  14. const char *name;
  15. const size_t len;
  16. long *var;
  17. } ent[] = {
  18. { "SwapTotal", sizeof("SwapTotal") - 1, s_total },
  19. { "SwapFree", sizeof("SwapFree") - 1, s_free },
  20. { "SwapCached", sizeof("SwapCached") - 1, s_cached },
  21. };
  22. size_t line_len = 0, i, left;
  23. char *line = NULL;
  24. /* get number of fields we want to extract */
  25. for (i = 0, left = 0; i < LEN(ent); i++) {
  26. if (ent[i].var) {
  27. left++;
  28. }
  29. }
  30. if (!(fp = fopen("/proc/meminfo", "r"))) {
  31. warn("fopen '/proc/meminfo':");
  32. return 1;
  33. }
  34. /* read file line by line and extract field information */
  35. while (left > 0 && getline(&line, &line_len, fp) >= 0) {
  36. for (i = 0; i < LEN(ent); i++) {
  37. if (ent[i].var &&
  38. !strncmp(line, ent[i].name, ent[i].len)) {
  39. sscanf(line + ent[i].len + 1, "%ld kB\n",
  40. ent[i].var);
  41. left--;
  42. break;
  43. }
  44. }
  45. }
  46. free(line);
  47. if (ferror(fp)) {
  48. warn("getline '/proc/meminfo':");
  49. return 1;
  50. }
  51. fclose(fp);
  52. return 0;
  53. }
  54. const char *
  55. swap_free(void)
  56. {
  57. long free;
  58. if (get_swap_info(NULL, &free, NULL)) {
  59. return NULL;
  60. }
  61. return fmt_human(free * 1024, 1024);
  62. }
  63. const char *
  64. swap_perc(void)
  65. {
  66. long total, free, cached;
  67. if (get_swap_info(&total, &free, &cached) || total == 0) {
  68. return NULL;
  69. }
  70. return bprintf("%d", 100 * (total - free - cached) / total);
  71. }
  72. const char *
  73. swap_total(void)
  74. {
  75. long total;
  76. if (get_swap_info(&total, NULL, NULL)) {
  77. return NULL;
  78. }
  79. return fmt_human(total * 1024, 1024);
  80. }
  81. const char *
  82. swap_used(void)
  83. {
  84. long total, free, cached;
  85. if (get_swap_info(&total, &free, &cached)) {
  86. return NULL;
  87. }
  88. return fmt_human((total - free - cached) * 1024, 1024);
  89. }
  90. #elif defined(__OpenBSD__)
  91. #include <stdlib.h>
  92. #include <sys/swap.h>
  93. #include <sys/types.h>
  94. #include <unistd.h>
  95. static int
  96. getstats(int *total, int *used)
  97. {
  98. struct swapent *sep, *fsep;
  99. int rnswap, nswap, i;
  100. if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) < 1) {
  101. warn("swaptctl 'SWAP_NSWAP':");
  102. return 1;
  103. }
  104. if (!(fsep = sep = calloc(nswap, sizeof(*sep)))) {
  105. warn("calloc 'nswap':");
  106. return 1;
  107. }
  108. if ((rnswap = swapctl(SWAP_STATS, (void *)sep, nswap)) < 0) {
  109. warn("swapctl 'SWAP_STATA':");
  110. return 1;
  111. }
  112. if (nswap != rnswap) {
  113. warn("getstats: SWAP_STATS != SWAP_NSWAP");
  114. return 1;
  115. }
  116. *total = 0;
  117. *used = 0;
  118. for (i = 0; i < rnswap; i++) {
  119. *total += sep->se_nblks >> 1;
  120. *used += sep->se_inuse >> 1;
  121. }
  122. free(fsep);
  123. return 0;
  124. }
  125. const char *
  126. swap_free(void)
  127. {
  128. int total, used;
  129. if (getstats(&total, &used)) {
  130. return NULL;
  131. }
  132. return fmt_human((total - used) * 1024, 1024);
  133. }
  134. const char *
  135. swap_perc(void)
  136. {
  137. int total, used;
  138. if (getstats(&total, &used)) {
  139. return NULL;
  140. }
  141. if (total == 0) {
  142. return NULL;
  143. }
  144. return bprintf("%d", 100 * used / total);
  145. }
  146. const char *
  147. swap_total(void)
  148. {
  149. int total, used;
  150. if (getstats(&total, &used)) {
  151. return NULL;
  152. }
  153. return fmt_human(total * 1024, 1024);
  154. }
  155. const char *
  156. swap_used(void)
  157. {
  158. int total, used;
  159. if (getstats(&total, &used)) {
  160. return NULL;
  161. }
  162. return fmt_human(used * 1024, 1024);
  163. }
  164. #endif