memlimit.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <assert.h>
  45. #include <errno.h>
  46. #include <stddef.h>
  47. #include <stdint.h>
  48. #include <string.h>
  49. #include <unistd.h>
  50. #ifdef DEBUG
  51. #include <stdio.h>
  52. #endif
  53. #include "memlimit.h"
  54. /* If we don't have CTL_HW, we can't use HW_USERMEM. */
  55. #ifndef CTL_HW
  56. #undef HW_USERMEM
  57. #endif
  58. /* Is RLIMIT_DATA relevant on this platform? */
  59. #if !defined(HAVE_MMAP) || defined(__OpenBSD__)
  60. #define USE_RLIMIT_DATA
  61. #endif
  62. #ifdef CTL_HW
  63. static int
  64. memlimit_sysctl_hw(size_t * memlimit, int mibleaf)
  65. {
  66. int mib[2];
  67. uint8_t sysctlbuf[8];
  68. size_t sysctlbuflen = 8;
  69. uint64_t sysctlval;
  70. /* Ask the kernel how much RAM we have. */
  71. mib[0] = CTL_HW;
  72. mib[1] = mibleaf;
  73. if (sysctl(mib, 2, sysctlbuf, &sysctlbuflen, NULL, 0))
  74. return (1);
  75. /*
  76. * If we read 8 bytes out, assume this is a system-endian uint64_t.
  77. * If we only read 4 bytes out, the OS is trying to give us a
  78. * uint32_t answer -- but given how many systems now have 4GB+ of RAM,
  79. * it's probably truncating, and we really can't trust the value we
  80. * have returned to us.
  81. */
  82. if (sysctlbuflen == sizeof(uint64_t))
  83. memcpy(&sysctlval, sysctlbuf, sizeof(uint64_t));
  84. else if (sysctlbuflen == sizeof(uint32_t))
  85. sysctlval = SIZE_MAX;
  86. else
  87. return (1);
  88. /* Return the sysctl value, but clamp to SIZE_MAX if necessary. */
  89. #if UINT64_MAX > SIZE_MAX
  90. if (sysctlval > SIZE_MAX)
  91. *memlimit = SIZE_MAX;
  92. else
  93. *memlimit = (size_t)sysctlval;
  94. #else
  95. *memlimit = sysctlval;
  96. #endif
  97. /* Success! */
  98. return (0);
  99. }
  100. #endif
  101. /* If we don't HAVE_STRUCT_SYSINFO, we can't use sysinfo. */
  102. #ifndef HAVE_STRUCT_SYSINFO
  103. #undef HAVE_SYSINFO
  104. #endif
  105. /* If we don't HAVE_STRUCT_SYSINFO_TOTALRAM, we can't use sysinfo. */
  106. #ifndef HAVE_STRUCT_SYSINFO_TOTALRAM
  107. #undef HAVE_SYSINFO
  108. #endif
  109. #ifdef HAVE_SYSINFO
  110. static int
  111. memlimit_sysinfo(size_t * memlimit)
  112. {
  113. struct sysinfo info;
  114. uint64_t totalmem;
  115. /* Get information from the kernel. */
  116. if (sysinfo(&info))
  117. return (1);
  118. totalmem = info.totalram;
  119. /* If we're on a modern kernel, adjust based on mem_unit. */
  120. #ifdef HAVE_STRUCT_SYSINFO_MEM_UNIT
  121. totalmem = totalmem * info.mem_unit;
  122. #endif
  123. /* Return the value, but clamp to SIZE_MAX if necessary. */
  124. #if UINT64_MAX > SIZE_MAX
  125. if (totalmem > SIZE_MAX)
  126. *memlimit = SIZE_MAX;
  127. else
  128. *memlimit = (size_t)totalmem;
  129. #else
  130. *memlimit = totalmem;
  131. #endif
  132. /* Success! */
  133. return (0);
  134. }
  135. #endif /* HAVE_SYSINFO */
  136. static int
  137. memlimit_rlimit(size_t * memlimit)
  138. {
  139. struct rlimit rl;
  140. uint64_t memrlimit;
  141. /* Find the least of... */
  142. memrlimit = (uint64_t)(-1);
  143. /* ... RLIMIT_AS... */
  144. #ifdef RLIMIT_AS
  145. if (getrlimit(RLIMIT_AS, &rl))
  146. return (1);
  147. if ((rl.rlim_cur != RLIM_INFINITY) &&
  148. ((uint64_t)rl.rlim_cur < memrlimit))
  149. memrlimit = (uint64_t)rl.rlim_cur;
  150. #endif
  151. #ifdef USE_RLIMIT_DATA
  152. /* ... RLIMIT_DATA (if we're not using mmap)... */
  153. if (getrlimit(RLIMIT_DATA, &rl))
  154. return (1);
  155. if ((rl.rlim_cur != RLIM_INFINITY) &&
  156. ((uint64_t)rl.rlim_cur < memrlimit))
  157. memrlimit = (uint64_t)rl.rlim_cur;
  158. #endif
  159. /* ... and RLIMIT_RSS. */
  160. #ifdef RLIMIT_RSS
  161. if (getrlimit(RLIMIT_RSS, &rl))
  162. return (1);
  163. if ((rl.rlim_cur != RLIM_INFINITY) &&
  164. ((uint64_t)rl.rlim_cur < memrlimit))
  165. memrlimit = (uint64_t)rl.rlim_cur;
  166. #endif
  167. /* Return the value, but clamp to SIZE_MAX if necessary. */
  168. #if UINT64_MAX > SIZE_MAX
  169. if (memrlimit > SIZE_MAX)
  170. *memlimit = SIZE_MAX;
  171. else
  172. *memlimit = (size_t)memrlimit;
  173. #else
  174. *memlimit = memrlimit;
  175. #endif
  176. /* Success! */
  177. return (0);
  178. }
  179. #ifdef _SC_PHYS_PAGES
  180. /* Some systems define _SC_PAGESIZE instead of _SC_PAGE_SIZE. */
  181. #ifndef _SC_PAGE_SIZE
  182. #define _SC_PAGE_SIZE _SC_PAGESIZE
  183. #endif
  184. static int
  185. memlimit_sysconf(size_t * memlimit)
  186. {
  187. long pagesize;
  188. long physpages;
  189. uint64_t totalmem;
  190. /* Set errno to 0 in order to distinguish "no limit" from "error". */
  191. errno = 0;
  192. /* Read the two limits. */
  193. if (((pagesize = sysconf(_SC_PAGE_SIZE)) == -1) ||
  194. ((physpages = sysconf(_SC_PHYS_PAGES)) == -1)) {
  195. /*
  196. * Did an error occur? OS X may return EINVAL due to not
  197. * supporting _SC_PHYS_PAGES in spite of defining it.
  198. */
  199. if (errno != 0 && errno != EINVAL)
  200. return (1);
  201. /* If not, there is no limit. */
  202. totalmem = (uint64_t)(-1);
  203. } else {
  204. /* Compute the limit. */
  205. totalmem = (uint64_t)(pagesize) * (uint64_t)(physpages);
  206. }
  207. /* Return the value, but clamp to SIZE_MAX if necessary. */
  208. #if UINT64_MAX > SIZE_MAX
  209. if (totalmem > SIZE_MAX)
  210. *memlimit = SIZE_MAX;
  211. else
  212. *memlimit = (size_t)totalmem;
  213. #else
  214. *memlimit = totalmem;
  215. #endif
  216. /* Success! */
  217. return (0);
  218. }
  219. #endif
  220. /**
  221. * memtouse(maxmem, maxmemfrac, memlimit):
  222. * Examine the system and return the amount of RAM which should be
  223. * used in ${memlimit}. This value should be the specified
  224. * ${maxmemfrac} fraction of available RAM, but no more than
  225. * ${maxmem} and no less than 1 MiB. ${maxmemfrac} must be larger than 0
  226. * and <= 1.0.
  227. */
  228. int
  229. memtouse(size_t maxmem, double maxmemfrac, size_t * memlimit)
  230. {
  231. size_t usermem_memlimit, memsize_memlimit;
  232. size_t sysinfo_memlimit, rlimit_memlimit;
  233. size_t sysconf_memlimit;
  234. size_t memlimit_min;
  235. size_t memavail;
  236. /* Check limit. */
  237. assert((maxmemfrac > 0) && (maxmemfrac <= 1.0));
  238. /* Get memory limits. */
  239. #ifdef HW_USERMEM
  240. if (memlimit_sysctl_hw(&usermem_memlimit, HW_USERMEM))
  241. return (1);
  242. #else
  243. usermem_memlimit = SIZE_MAX;
  244. #endif
  245. #ifdef HW_MEMSIZE
  246. if (memlimit_sysctl_hw(&memsize_memlimit, HW_MEMSIZE))
  247. return (1);
  248. #else
  249. memsize_memlimit = SIZE_MAX;
  250. #endif
  251. #ifdef HAVE_SYSINFO
  252. if (memlimit_sysinfo(&sysinfo_memlimit))
  253. return (1);
  254. #else
  255. sysinfo_memlimit = SIZE_MAX;
  256. #endif
  257. if (memlimit_rlimit(&rlimit_memlimit))
  258. return (1);
  259. #ifdef _SC_PHYS_PAGES
  260. if (memlimit_sysconf(&sysconf_memlimit))
  261. return (1);
  262. #else
  263. sysconf_memlimit = SIZE_MAX;
  264. #endif
  265. #ifdef DEBUG
  266. /* rlimit has two '\t' so that they line up. */
  267. fprintf(stderr, "Memory limits are:\n\tusermem:\t%zu\n"
  268. "\tmemsize:\t%zu\n\tsysinfo:\t%zu\n\trlimit:\t\t%zu\n"
  269. "\tsysconf:\t%zu\n", usermem_memlimit, memsize_memlimit,
  270. sysinfo_memlimit, rlimit_memlimit, sysconf_memlimit);
  271. #endif
  272. /*
  273. * Some systems return bogus values for hw.usermem due to ZFS making
  274. * use of wired pages. Assume that at least 50% of physical pages
  275. * are available to userland on demand.
  276. */
  277. if (sysconf_memlimit != SIZE_MAX) {
  278. if (usermem_memlimit < sysconf_memlimit / 2)
  279. usermem_memlimit = sysconf_memlimit / 2;
  280. }
  281. /* Find the smallest of them. */
  282. memlimit_min = SIZE_MAX;
  283. if (memlimit_min > usermem_memlimit)
  284. memlimit_min = usermem_memlimit;
  285. if (memlimit_min > memsize_memlimit)
  286. memlimit_min = memsize_memlimit;
  287. if (memlimit_min > sysinfo_memlimit)
  288. memlimit_min = sysinfo_memlimit;
  289. if (memlimit_min > rlimit_memlimit)
  290. memlimit_min = rlimit_memlimit;
  291. if (memlimit_min > sysconf_memlimit)
  292. memlimit_min = sysconf_memlimit;
  293. #ifdef DEBUG
  294. /* This line is continued after the maxmemfrac calculation. */
  295. fprintf(stderr, "Minimum is %zu;", memlimit_min);
  296. #endif
  297. /* Only use the specified fraction of the available memory. */
  298. memavail = (size_t)(maxmemfrac * (double)memlimit_min);
  299. #ifdef DEBUG
  300. fprintf(stderr, " will apply maxmemfrac of %g\n", maxmemfrac);
  301. #endif
  302. /* Don't use more than the specified maximum. */
  303. if ((maxmem > 0) && (memavail > maxmem))
  304. memavail = maxmem;
  305. /* But always allow at least 1 MiB. */
  306. if (memavail < 1048576)
  307. memavail = 1048576;
  308. #ifdef DEBUG
  309. fprintf(stderr, "Memory allowed:\t\t%zu\n", memavail);
  310. #endif
  311. /* Return limit via the provided pointer. */
  312. *memlimit = memavail;
  313. return (0);
  314. }