total_memory.c 993 B

12345678910111213141516171819202122232425262728293031
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#memory-size */
  2. #define _GNU_SOURCE
  3. #include <stdio.h>
  4. #include <sys/sysinfo.h>
  5. #include <unistd.h>
  6. int main(void) {
  7. /* PAGESIZE is POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/
  8. * but PHYS_PAGES and AVPHYS_PAGES are glibc extensions. I bet those are
  9. * parsed from /proc/meminfo. */
  10. printf(
  11. "sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) = 0x%lX\n",
  12. sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE)
  13. );
  14. printf(
  15. "sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE) = 0x%lX\n",
  16. sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE)
  17. );
  18. /* glibc extensions. man says they are parsed from /proc/meminfo. */
  19. printf(
  20. "get_phys_pages() * sysconf(_SC_PAGESIZE) = 0x%lX\n",
  21. get_phys_pages() * sysconf(_SC_PAGESIZE)
  22. );
  23. printf(
  24. "get_avphys_pages() * sysconf(_SC_PAGESIZE) = 0x%lX\n",
  25. get_avphys_pages() * sysconf(_SC_PAGESIZE)
  26. );
  27. }