meminfo.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "lib/meminfo/meminfo.h"
  6. #include "lib/cc/compat_compiler.h"
  7. #include "lib/cc/torint.h"
  8. #include "lib/fs/files.h"
  9. #include "lib/log/torlog.h"
  10. #include "lib/malloc/util_malloc.h"
  11. #ifdef HAVE_SYS_SYSCTL_H
  12. #include <sys/sysctl.h>
  13. #endif
  14. #ifdef HAVE_FCNTL_H
  15. #include <fcntl.h>
  16. #endif
  17. #ifdef HAVE_MALLOC_H
  18. #include <malloc.h>
  19. #endif
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #ifdef _WIN32
  24. #include <windows.h>
  25. #endif
  26. #include <string.h>
  27. DISABLE_GCC_WARNING(aggregate-return)
  28. /** Call the platform malloc info function, and dump the results to the log at
  29. * level <b>severity</b>. If no such function exists, do nothing. */
  30. void
  31. tor_log_mallinfo(int severity)
  32. {
  33. #ifdef HAVE_MALLINFO
  34. struct mallinfo mi;
  35. memset(&mi, 0, sizeof(mi));
  36. mi = mallinfo();
  37. tor_log(severity, LD_MM,
  38. "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
  39. "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
  40. "keepcost=%d",
  41. mi.arena, mi.ordblks, mi.smblks, mi.hblks,
  42. mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
  43. mi.keepcost);
  44. #else /* !(defined(HAVE_MALLINFO)) */
  45. (void)severity;
  46. #endif /* defined(HAVE_MALLINFO) */
  47. }
  48. ENABLE_GCC_WARNING(aggregate-return)
  49. #if defined(HW_PHYSMEM64)
  50. /* This appears to be an OpenBSD thing */
  51. #define INT64_HW_MEM HW_PHYSMEM64
  52. #elif defined(HW_MEMSIZE)
  53. /* OSX defines this one */
  54. #define INT64_HW_MEM HW_MEMSIZE
  55. #endif /* defined(HW_PHYSMEM64) || ... */
  56. /**
  57. * Helper: try to detect the total system memory, and return it. On failure,
  58. * return 0.
  59. */
  60. static uint64_t
  61. get_total_system_memory_impl(void)
  62. {
  63. #if defined(__linux__)
  64. /* On linux, sysctl is deprecated. Because proc is so awesome that you
  65. * shouldn't _want_ to write portable code, I guess? */
  66. unsigned long long result=0;
  67. int fd = -1;
  68. char *s = NULL;
  69. const char *cp;
  70. size_t file_size=0;
  71. if (-1 == (fd = tor_open_cloexec("/proc/meminfo",O_RDONLY,0)))
  72. return 0;
  73. s = read_file_to_str_until_eof(fd, 65536, &file_size);
  74. if (!s)
  75. goto err;
  76. cp = strstr(s, "MemTotal:");
  77. if (!cp)
  78. goto err;
  79. /* Use the system sscanf so that space will match a wider number of space */
  80. if (sscanf(cp, "MemTotal: %llu kB\n", &result) != 1)
  81. goto err;
  82. close(fd);
  83. tor_free(s);
  84. return result * 1024;
  85. /* LCOV_EXCL_START Can't reach this unless proc is broken. */
  86. err:
  87. tor_free(s);
  88. close(fd);
  89. return 0;
  90. /* LCOV_EXCL_STOP */
  91. #elif defined (_WIN32)
  92. /* Windows has MEMORYSTATUSEX; pretty straightforward. */
  93. MEMORYSTATUSEX ms;
  94. memset(&ms, 0, sizeof(ms));
  95. ms.dwLength = sizeof(ms);
  96. if (! GlobalMemoryStatusEx(&ms))
  97. return 0;
  98. return ms.ullTotalPhys;
  99. #elif defined(HAVE_SYSCTL) && defined(INT64_HW_MEM)
  100. /* On many systems, HW_PYHSMEM is clipped to 32 bits; let's use a better
  101. * variant if we know about it. */
  102. uint64_t memsize = 0;
  103. size_t len = sizeof(memsize);
  104. int mib[2] = {CTL_HW, INT64_HW_MEM};
  105. if (sysctl(mib,2,&memsize,&len,NULL,0))
  106. return 0;
  107. return memsize;
  108. #elif defined(HAVE_SYSCTL) && defined(HW_PHYSMEM)
  109. /* On some systems (like FreeBSD I hope) you can use a size_t with
  110. * HW_PHYSMEM. */
  111. size_t memsize=0;
  112. size_t len = sizeof(memsize);
  113. int mib[2] = {CTL_HW, HW_USERMEM};
  114. if (sysctl(mib,2,&memsize,&len,NULL,0))
  115. return 0;
  116. return memsize;
  117. #else
  118. /* I have no clue. */
  119. return 0;
  120. #endif /* defined(__linux__) || ... */
  121. }
  122. /**
  123. * Try to find out how much physical memory the system has. On success,
  124. * return 0 and set *<b>mem_out</b> to that value. On failure, return -1.
  125. */
  126. MOCK_IMPL(int,
  127. get_total_system_memory, (size_t *mem_out))
  128. {
  129. static size_t mem_cached=0;
  130. uint64_t m = get_total_system_memory_impl();
  131. if (0 == m) {
  132. /* LCOV_EXCL_START -- can't make this happen without mocking. */
  133. /* We couldn't find our memory total */
  134. if (0 == mem_cached) {
  135. /* We have no cached value either */
  136. *mem_out = 0;
  137. return -1;
  138. }
  139. *mem_out = mem_cached;
  140. return 0;
  141. /* LCOV_EXCL_STOP */
  142. }
  143. #if SIZE_MAX != UINT64_MAX
  144. if (m > SIZE_MAX) {
  145. /* I think this could happen if we're a 32-bit Tor running on a 64-bit
  146. * system: we could have more system memory than would fit in a
  147. * size_t. */
  148. m = SIZE_MAX;
  149. }
  150. #endif /* SIZE_MAX != UINT64_MAX */
  151. *mem_out = mem_cached = (size_t) m;
  152. return 0;
  153. }