swap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "../util.h"
  7. #if defined(__linux__)
  8. static int
  9. get_swap_info(long *s_total, long *s_free, long *s_cached)
  10. {
  11. FILE *fp;
  12. struct {
  13. const char *name;
  14. const size_t len;
  15. long *var;
  16. } ent[] = {
  17. { "SwapTotal", sizeof("SwapTotal") - 1, s_total },
  18. { "SwapFree", sizeof("SwapFree") - 1, s_free },
  19. { "SwapCached", sizeof("SwapCached") - 1, s_cached },
  20. };
  21. size_t line_len = 0, i, left;
  22. char *line = NULL;
  23. /* get number of fields we want to extract */
  24. for (i = 0, left = 0; i < LEN(ent); i++) {
  25. if (ent[i].var) {
  26. left++;
  27. }
  28. }
  29. if (!(fp = fopen("/proc/meminfo", "r"))) {
  30. warn("fopen '/proc/meminfo':");
  31. return 1;
  32. }
  33. /* read file line by line and extract field information */
  34. while (left > 0 && getline(&line, &line_len, fp) >= 0) {
  35. for (i = 0; i < LEN(ent); i++) {
  36. if (ent[i].var &&
  37. !strncmp(line, ent[i].name, ent[i].len)) {
  38. sscanf(line + ent[i].len + 1,
  39. "%ld kB\n", ent[i].var);
  40. left--;
  41. break;
  42. }
  43. }
  44. }
  45. free(line);
  46. if (ferror(fp)) {
  47. warn("getline '/proc/meminfo':");
  48. return 1;
  49. }
  50. fclose(fp);
  51. return 0;
  52. }
  53. const char *
  54. swap_free(void)
  55. {
  56. long free;
  57. if (get_swap_info(NULL, &free, NULL)) {
  58. return NULL;
  59. }
  60. return fmt_human(free * 1024, 1024);
  61. }
  62. const char *
  63. swap_perc(void)
  64. {
  65. long total, free, cached;
  66. if (get_swap_info(&total, &free, &cached) || total == 0) {
  67. return NULL;
  68. }
  69. return bprintf("%d", 100 * (total - free - cached) / total);
  70. }
  71. const char *
  72. swap_total(void)
  73. {
  74. long total;
  75. if (get_swap_info(&total, NULL, NULL)) {
  76. return NULL;
  77. }
  78. return fmt_human(total * 1024, 1024);
  79. }
  80. const char *
  81. swap_used(void)
  82. {
  83. long total, free, cached;
  84. if (get_swap_info(&total, &free, &cached)) {
  85. return NULL;
  86. }
  87. return fmt_human((total - free - cached) * 1024, 1024);
  88. }
  89. #elif defined(__OpenBSD__)
  90. #include <stdlib.h>
  91. #include <sys/swap.h>
  92. #include <sys/types.h>
  93. #include <unistd.h>
  94. static int
  95. getstats(int *total, int *used)
  96. {
  97. struct swapent *sep, *fsep;
  98. int rnswap, nswap, i;
  99. if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) < 1) {
  100. warn("swaptctl 'SWAP_NSWAP':");
  101. return 1;
  102. }
  103. if (!(fsep = sep = calloc(nswap, sizeof(*sep)))) {
  104. warn("calloc 'nswap':");
  105. return 1;
  106. }
  107. if ((rnswap = swapctl(SWAP_STATS, (void *)sep, nswap)) < 0) {
  108. warn("swapctl 'SWAP_STATA':");
  109. return 1;
  110. }
  111. if (nswap != rnswap) {
  112. warn("getstats: SWAP_STATS != SWAP_NSWAP");
  113. return 1;
  114. }
  115. *total = 0;
  116. *used = 0;
  117. for (i = 0; i < rnswap; i++) {
  118. *total += sep->se_nblks >> 1;
  119. *used += sep->se_inuse >> 1;
  120. }
  121. free(fsep);
  122. return 0;
  123. }
  124. const char *
  125. swap_free(void)
  126. {
  127. int total, used;
  128. if (getstats(&total, &used)) {
  129. return NULL;
  130. }
  131. return fmt_human((total - used) * 1024, 1024);
  132. }
  133. const char *
  134. swap_perc(void)
  135. {
  136. int total, used;
  137. if (getstats(&total, &used)) {
  138. return NULL;
  139. }
  140. if (total == 0) {
  141. return NULL;
  142. }
  143. return bprintf("%d", 100 * used / total);
  144. }
  145. const char *
  146. swap_total(void)
  147. {
  148. int total, used;
  149. if (getstats(&total, &used)) {
  150. return NULL;
  151. }
  152. return fmt_human(total * 1024, 1024);
  153. }
  154. const char *
  155. swap_used(void)
  156. {
  157. int total, used;
  158. if (getstats(&total, &used)) {
  159. return NULL;
  160. }
  161. return fmt_human(used * 1024, 1024);
  162. }
  163. #elif defined(__FreeBSD__)
  164. #include <stdlib.h>
  165. #include <sys/types.h>
  166. #include <fcntl.h>
  167. #include <unistd.h>
  168. #include <kvm.h>
  169. static int getswapinfo(struct kvm_swap *swap_info, size_t size)
  170. {
  171. kvm_t *kd;
  172. kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, NULL);
  173. if(kd == NULL) {
  174. warn("kvm_openfiles '/dev/null':");
  175. return 0;
  176. }
  177. if(kvm_getswapinfo(kd, swap_info, size, 0 /* Unused flags */) == -1) {
  178. warn("kvm_getswapinfo:");
  179. kvm_close(kd);
  180. return 0;
  181. }
  182. kvm_close(kd);
  183. return 1;
  184. }
  185. const char *
  186. swap_free(void)
  187. {
  188. struct kvm_swap swap_info[1];
  189. long used, total;
  190. if(!getswapinfo(swap_info, 1))
  191. return NULL;
  192. total = swap_info[0].ksw_total;
  193. used = swap_info[0].ksw_used;
  194. return fmt_human((total - used) * getpagesize(), 1024);
  195. }
  196. const char *
  197. swap_perc(void)
  198. {
  199. struct kvm_swap swap_info[1];
  200. long used, total;
  201. if(!getswapinfo(swap_info, 1))
  202. return NULL;
  203. total = swap_info[0].ksw_total;
  204. used = swap_info[0].ksw_used;
  205. return bprintf("%d", used * 100 / total);
  206. }
  207. const char *
  208. swap_total(void)
  209. {
  210. struct kvm_swap swap_info[1];
  211. long total;
  212. if(!getswapinfo(swap_info, 1))
  213. return NULL;
  214. total = swap_info[0].ksw_total;
  215. return fmt_human(total * getpagesize(), 1024);
  216. }
  217. const char *
  218. swap_used(void)
  219. {
  220. struct kvm_swap swap_info[1];
  221. long used;
  222. if(!getswapinfo(swap_info, 1))
  223. return NULL;
  224. used = swap_info[0].ksw_used;
  225. return fmt_human(used * getpagesize(), 1024);
  226. }
  227. #endif