memlimit.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*-
  2. * Copyright 2009 Colin Percival
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * This file was originally written by Colin Percival as part of the Tarsnap
  27. * online backup system.
  28. */
  29. /* We use non-POSIX functionality in this file. */
  30. #undef _POSIX_C_SOURCE
  31. #undef _XOPEN_SOURCE
  32. #include "platform.h"
  33. #include <sys/types.h>
  34. #include <sys/resource.h>
  35. #ifdef HAVE_SYS_PARAM_H
  36. #include <sys/param.h>
  37. #endif
  38. #ifdef HAVE_SYS_SYSCTL_H
  39. #include <sys/sysctl.h>
  40. #endif
  41. #ifdef HAVE_SYS_SYSINFO_H
  42. #include <sys/sysinfo.h>
  43. #endif
  44. #include <errno.h>
  45. #include <stddef.h>
  46. #include <stdint.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. #ifdef DEBUG
  50. #include <stdio.h>
  51. #endif
  52. #include "memlimit.h"
  53. /* If we don't have CTL_HW, we can't use HW_USERMEM. */
  54. #ifndef CTL_HW
  55. #undef HW_USERMEM
  56. #endif
  57. #ifdef CTL_HW
  58. static int
  59. memlimit_sysctl_hw(size_t * memlimit, int mibleaf)
  60. {
  61. int mib[2];
  62. uint8_t sysctlbuf[8];
  63. size_t sysctlbuflen = 8;
  64. uint64_t sysctlval;
  65. /* Ask the kernel how much RAM we have. */
  66. mib[0] = CTL_HW;
  67. mib[1] = mibleaf;
  68. if (sysctl(mib, 2, sysctlbuf, &sysctlbuflen, NULL, 0))
  69. return (1);
  70. /*
  71. * If we read 8 bytes out, assume this is a system-endian uint64_t.
  72. * If we only read 4 bytes out, the OS is trying to give us a
  73. * uint32_t answer -- but given how many systems now have 4GB+ of RAM,
  74. * it's probably truncating, and we really can't trust the value we
  75. * have returned to us.
  76. */
  77. if (sysctlbuflen == sizeof(uint64_t))
  78. memcpy(&sysctlval, sysctlbuf, sizeof(uint64_t));
  79. else if (sysctlbuflen == sizeof(uint32_t))
  80. sysctlval = SIZE_MAX;
  81. else
  82. return (1);
  83. /* Return the sysctl value, but clamp to SIZE_MAX if necessary. */
  84. #if UINT64_MAX > SIZE_MAX
  85. if (sysctlval > SIZE_MAX)
  86. *memlimit = SIZE_MAX;
  87. else
  88. *memlimit = (size_t)sysctlval;
  89. #else
  90. *memlimit = sysctlval;
  91. #endif
  92. /* Success! */
  93. return (0);
  94. }
  95. #endif
  96. /* If we don't HAVE_STRUCT_SYSINFO, we can't use sysinfo. */
  97. #ifndef HAVE_STRUCT_SYSINFO
  98. #undef HAVE_SYSINFO
  99. #endif
  100. /* If we don't HAVE_STRUCT_SYSINFO_TOTALRAM, we can't use sysinfo. */
  101. #ifndef HAVE_STRUCT_SYSINFO_TOTALRAM
  102. #undef HAVE_SYSINFO
  103. #endif
  104. #ifdef HAVE_SYSINFO
  105. static int
  106. memlimit_sysinfo(size_t * memlimit)
  107. {
  108. struct sysinfo info;
  109. uint64_t totalmem;
  110. /* Get information from the kernel. */
  111. if (sysinfo(&info))
  112. return (1);
  113. totalmem = info.totalram;
  114. /* If we're on a modern kernel, adjust based on mem_unit. */
  115. #ifdef HAVE_STRUCT_SYSINFO_MEM_UNIT
  116. totalmem = totalmem * info.mem_unit;
  117. #endif
  118. /* Return the value, but clamp to SIZE_MAX if necessary. */
  119. #if UINT64_MAX > SIZE_MAX
  120. if (totalmem > SIZE_MAX)
  121. *memlimit = SIZE_MAX;
  122. else
  123. *memlimit = (size_t)totalmem;
  124. #else
  125. *memlimit = totalmem;
  126. #endif
  127. /* Success! */
  128. return (0);
  129. }
  130. #endif /* HAVE_SYSINFO */
  131. static int
  132. memlimit_rlimit(size_t * memlimit)
  133. {
  134. struct rlimit rl;
  135. uint64_t memrlimit;
  136. /* Find the least of... */
  137. memrlimit = (uint64_t)(-1);
  138. /* ... RLIMIT_AS... */
  139. #ifdef RLIMIT_AS
  140. if (getrlimit(RLIMIT_AS, &rl))
  141. return (1);
  142. if ((rl.rlim_cur != RLIM_INFINITY) &&
  143. ((uint64_t)rl.rlim_cur < memrlimit))
  144. memrlimit = (uint64_t)rl.rlim_cur;
  145. #endif
  146. #ifndef HAVE_MMAP
  147. /* ... RLIMIT_DATA (if we're not using mmap)... */
  148. if (getrlimit(RLIMIT_DATA, &rl))
  149. return (1);
  150. if ((rl.rlim_cur != RLIM_INFINITY) &&
  151. ((uint64_t)rl.rlim_cur < memrlimit))
  152. memrlimit = (uint64_t)rl.rlim_cur;
  153. #endif
  154. /* ... and RLIMIT_RSS. */
  155. #ifdef RLIMIT_RSS
  156. if (getrlimit(RLIMIT_RSS, &rl))
  157. return (1);
  158. if ((rl.rlim_cur != RLIM_INFINITY) &&
  159. ((uint64_t)rl.rlim_cur < memrlimit))
  160. memrlimit = (uint64_t)rl.rlim_cur;
  161. #endif
  162. /* Return the value, but clamp to SIZE_MAX if necessary. */
  163. #if UINT64_MAX > SIZE_MAX
  164. if (memrlimit > SIZE_MAX)
  165. *memlimit = SIZE_MAX;
  166. else
  167. *memlimit = (size_t)memrlimit;
  168. #else
  169. *memlimit = memrlimit;
  170. #endif
  171. /* Success! */
  172. return (0);
  173. }
  174. #ifdef _SC_PHYS_PAGES
  175. /* Some systems define _SC_PAGESIZE instead of _SC_PAGE_SIZE. */
  176. #ifndef _SC_PAGE_SIZE
  177. #define _SC_PAGE_SIZE _SC_PAGESIZE
  178. #endif
  179. static int
  180. memlimit_sysconf(size_t * memlimit)
  181. {
  182. long pagesize;
  183. long physpages;
  184. uint64_t totalmem;
  185. /* Set errno to 0 in order to distinguish "no limit" from "error". */
  186. errno = 0;
  187. /* Read the two limits. */
  188. if (((pagesize = sysconf(_SC_PAGE_SIZE)) == -1) ||
  189. ((physpages = sysconf(_SC_PHYS_PAGES)) == -1)) {
  190. /*
  191. * Did an error occur? OS X may return EINVAL due to not
  192. * supporting _SC_PHYS_PAGES in spite of defining it.
  193. */
  194. if (errno != 0 && errno != EINVAL)
  195. return (1);
  196. /* If not, there is no limit. */
  197. totalmem = (uint64_t)(-1);
  198. } else {
  199. /* Compute the limit. */
  200. totalmem = (uint64_t)(pagesize) * (uint64_t)(physpages);
  201. }
  202. /* Return the value, but clamp to SIZE_MAX if necessary. */
  203. #if UINT64_MAX > SIZE_MAX
  204. if (totalmem > SIZE_MAX)
  205. *memlimit = SIZE_MAX;
  206. else
  207. *memlimit = (size_t)totalmem;
  208. #else
  209. *memlimit = totalmem;
  210. #endif
  211. /* Success! */
  212. return (0);
  213. }
  214. #endif
  215. /**
  216. * memtouse(maxmem, maxmemfrac, memlimit):
  217. * Examine the system and return via memlimit the amount of RAM which should
  218. * be used -- the specified fraction of the available RAM, but no more than
  219. * maxmem, and no less than 1MiB.
  220. */
  221. int
  222. memtouse(size_t maxmem, double maxmemfrac, size_t * memlimit)
  223. {
  224. size_t usermem_memlimit, memsize_memlimit;
  225. size_t sysinfo_memlimit, rlimit_memlimit;
  226. size_t sysconf_memlimit;
  227. size_t memlimit_min;
  228. size_t memavail;
  229. /* Get memory limits. */
  230. #ifdef HW_USERMEM
  231. if (memlimit_sysctl_hw(&usermem_memlimit, HW_USERMEM))
  232. return (1);
  233. #else
  234. usermem_memlimit = SIZE_MAX;
  235. #endif
  236. #ifdef HW_MEMSIZE
  237. if (memlimit_sysctl_hw(&memsize_memlimit, HW_MEMSIZE))
  238. return (1);
  239. #else
  240. memsize_memlimit = SIZE_MAX;
  241. #endif
  242. #ifdef HAVE_SYSINFO
  243. if (memlimit_sysinfo(&sysinfo_memlimit))
  244. return (1);
  245. #else
  246. sysinfo_memlimit = SIZE_MAX;
  247. #endif
  248. if (memlimit_rlimit(&rlimit_memlimit))
  249. return (1);
  250. #ifdef _SC_PHYS_PAGES
  251. if (memlimit_sysconf(&sysconf_memlimit))
  252. return (1);
  253. #else
  254. sysconf_memlimit = SIZE_MAX;
  255. #endif
  256. #ifdef DEBUG
  257. /* rlimit has two '\t' so that they line up. */
  258. fprintf(stderr, "Memory limits are:\n\tusermem:\t%zu\n"
  259. "\tmemsize:\t%zu\n\tsysinfo:\t%zu\n\trlimit:\t\t%zu\n"
  260. "\tsysconf:\t%zu\n", usermem_memlimit, memsize_memlimit,
  261. sysinfo_memlimit, rlimit_memlimit, sysconf_memlimit);
  262. #endif
  263. /*
  264. * Some systems return bogus values for hw.usermem due to ZFS making
  265. * use of wired pages. Assume that at least 50% of physical pages
  266. * are available to userland on demand.
  267. */
  268. if (sysconf_memlimit != SIZE_MAX) {
  269. if (usermem_memlimit < sysconf_memlimit / 2)
  270. usermem_memlimit = sysconf_memlimit / 2;
  271. }
  272. /* Find the smallest of them. */
  273. memlimit_min = SIZE_MAX;
  274. if (memlimit_min > usermem_memlimit)
  275. memlimit_min = usermem_memlimit;
  276. if (memlimit_min > memsize_memlimit)
  277. memlimit_min = memsize_memlimit;
  278. if (memlimit_min > sysinfo_memlimit)
  279. memlimit_min = sysinfo_memlimit;
  280. if (memlimit_min > rlimit_memlimit)
  281. memlimit_min = rlimit_memlimit;
  282. if (memlimit_min > sysconf_memlimit)
  283. memlimit_min = sysconf_memlimit;
  284. /* Only use the specified fraction of the available memory. */
  285. if ((maxmemfrac > 0.5) || (maxmemfrac == 0.0))
  286. maxmemfrac = 0.5;
  287. memavail = (size_t)(maxmemfrac * memlimit_min);
  288. /* Don't use more than the specified maximum. */
  289. if ((maxmem > 0) && (memavail > maxmem))
  290. memavail = maxmem;
  291. /* But always allow at least 1 MiB. */
  292. if (memavail < 1048576)
  293. memavail = 1048576;
  294. #ifdef DEBUG
  295. fprintf(stderr, "Allowing up to %zu memory to be used\n", memavail);
  296. #endif
  297. /* Return limit via the provided pointer. */
  298. *memlimit = memavail;
  299. return (0);
  300. }