meminfo.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef MEMINFO_H
  2. #define MEMINFO_H
  3. #include <stdio.h>
  4. #include <inttypes.h>
  5. #include "util.h"
  6. #define MEMINFO_FD(fd_ptr) sysfs_fd_or_rewind((fd_ptr), "/", "proc", "meminfo")
  7. /* clang-format off */
  8. #define MEMINFO_INIT_RAM_FIELDS(T, F, A, B, C, S, R) \
  9. { \
  10. { WITH_SSIZE("MemTotal"), (T) }, \
  11. { WITH_SSIZE("MemFree"), (F) }, \
  12. { WITH_SSIZE("MemAvailable"), (A) }, \
  13. { WITH_SSIZE("Buffers"), (B) }, \
  14. { WITH_SSIZE("Cached"), (C) }, \
  15. { WITH_SSIZE("Shmem"), (S) }, \
  16. { WITH_SSIZE("SReclaimable"), (R) }, \
  17. }
  18. #define MEMINFO_INIT_SWAP_FIELDS(C, T, F) \
  19. { \
  20. { WITH_SSIZE("SwapCached"), (C) }, \
  21. { WITH_SSIZE("SwapTotal"), (T) }, \
  22. { WITH_SSIZE("SwapFree"), (F) }, \
  23. }
  24. #define MEMINFO_INIT_RAM \
  25. { \
  26. 0 /* total */, \
  27. 0 /* free */, \
  28. 0 /* available */, \
  29. 0 /* buffers */, \
  30. 0 /* cached */, \
  31. 0 /* shared */, \
  32. 0 /* reclaimable */ \
  33. }
  34. #define MEMINFO_INIT_SWAP \
  35. { \
  36. 0 /* cached */, \
  37. 0 /* total */, \
  38. 0 /* free */, \
  39. }
  40. /* clang-format on */
  41. #define memory_t_format "%8" PRIu64
  42. typedef uint64_t memory_t;
  43. struct meminfo_field {
  44. const char *key;
  45. size_t key_len;
  46. memory_t * value_ptr;
  47. };
  48. struct meminfo_ram {
  49. /* clang-format off */
  50. memory_t
  51. total,
  52. free,
  53. available,
  54. buffers,
  55. cached,
  56. shared,
  57. reclaimable
  58. ;
  59. /* clang-format on */
  60. };
  61. struct meminfo_swap {
  62. memory_t cached, total, free;
  63. };
  64. /* read single field from meminfo */
  65. uint8_t read_meminfo(int fd, const char *key, memory_t *val);
  66. /*
  67. * read multiple fields from meminfo
  68. *
  69. * meminfo_field array should be sorted as in /proc/meminfo,
  70. * because fptr read sequentially and not seeked while reading
  71. */
  72. uint8_t
  73. read_meminfo_keys(int fd, const struct meminfo_field *info, size_t size);
  74. /* get only those fields which needed for `ram` components */
  75. uint8_t get_meminfo_ram(int fd, struct meminfo_ram *info);
  76. /* get only those fields which needed for `swap` components */
  77. uint8_t get_meminfo_swap(int fd, struct meminfo_swap *info);
  78. #endif /* MEMINFO_H */