swap.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <err.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include <sys/sysinfo.h>
  7. #include "../lib/util.h"
  8. #include "../aslstatus.h"
  9. #include "../lib/meminfo.h"
  10. #define DEF_SWAP(STRUCT, STATIC, OUT) \
  11. int* fd = (STATIC)->data; \
  12. struct meminfo_swap STRUCT = MEMINFO_INIT_SWAP; \
  13. if (!(STATIC)->cleanup) (STATIC)->cleanup = fd_cleanup; \
  14. if (!MEMINFO_FD(fd)) ERRRET(OUT); \
  15. if (!get_meminfo_swap(*fd, &STRUCT)) ERRRET(OUT)
  16. void
  17. swap_free(char* out,
  18. const char __unused* _a,
  19. uint32_t __unused _i,
  20. static_data_t* static_data)
  21. {
  22. DEF_SWAP(info, static_data, out);
  23. fmt_human(out, info.free * 1024);
  24. }
  25. void
  26. swap_perc(char* out,
  27. const char __unused* _a,
  28. uint32_t __unused _i,
  29. static_data_t* static_data)
  30. {
  31. DEF_SWAP(info, static_data, out);
  32. bprintf(out,
  33. "%" PRIperc,
  34. (percent_t)(100 * (info.total - info.free - info.cached)
  35. / info.total));
  36. }
  37. void
  38. swap_total(char* out,
  39. const char __unused* _a,
  40. uint32_t __unused _i,
  41. void __unused* _p)
  42. {
  43. struct sysinfo info;
  44. if (!!sysinfo(&info)) ERRRET(out);
  45. fmt_human(out, info.totalswap * info.mem_unit);
  46. }
  47. void
  48. swap_used(char* out,
  49. const char __unused* _a,
  50. uint32_t __unused _i,
  51. static_data_t* static_data)
  52. {
  53. DEF_SWAP(info, static_data, out);
  54. fmt_human(out, (info.total - info.free - info.cached) * 1024);
  55. }