memlimit.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #include "scrypt_platform.h"
  32. #include <sys/types.h>
  33. #include <sys/resource.h>
  34. #ifdef HAVE_SYS_PARAM_H
  35. #include <sys/param.h>
  36. #endif
  37. #ifdef HAVE_SYS_SYSCTL_H
  38. #include <sys/sysctl.h>
  39. #endif
  40. #ifdef HAVE_SYS_SYSINFO_H
  41. #include <sys/sysinfo.h>
  42. #endif
  43. #include <errno.h>
  44. #include <stddef.h>
  45. #include <stdint.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #ifdef DEBUG
  49. #include <stdio.h>
  50. #endif
  51. #include "memlimit.h"
  52. /* If we don't have CTL_HW, we can't use HW_USERMEM. */
  53. #ifndef CTL_HW
  54. #undef HW_USERMEM
  55. #endif
  56. #ifdef CTL_HW
  57. static int
  58. memlimit_sysctl_hw(size_t * memlimit, int mibleaf)
  59. {
  60. int mib[2];
  61. uint8_t sysctlbuf[8];
  62. size_t sysctlbuflen = 8;
  63. uint64_t sysctlval;
  64. /* Ask the kernel how much RAM we have. */
  65. mib[0] = CTL_HW;
  66. mib[1] = mibleaf;
  67. if (sysctl(mib, 2, sysctlbuf, &sysctlbuflen, NULL, 0))
  68. return (1);
  69. /*
  70. * If we read 8 bytes out, assume this is a system-endian uint64_t.
  71. * If we only read 4 bytes out, the OS is trying to give us a
  72. * uint32_t answer -- but given how many systems now have 4GB+ of RAM,
  73. * it's probably truncating, and we really can't trust the value we
  74. * have returned to us.
  75. */
  76. if (sysctlbuflen == sizeof(uint64_t))
  77. memcpy(&sysctlval, sysctlbuf, sizeof(uint64_t));
  78. else if (sysctlbuflen == sizeof(uint32_t))
  79. sysctlval = SIZE_MAX;
  80. else
  81. return (1);
  82. /* Return the sysctl value, but clamp to SIZE_MAX if necessary. */
  83. #if UINT64_MAX > SIZE_MAX
  84. if (sysctlval > SIZE_MAX)
  85. *memlimit = SIZE_MAX;
  86. else
  87. *memlimit = (size_t)sysctlval;
  88. #else
  89. *memlimit = sysctlval;
  90. #endif
  91. /* Success! */
  92. return (0);
  93. }
  94. #endif
  95. /* If we don't HAVE_STRUCT_SYSINFO, we can't use sysinfo. */
  96. #ifndef HAVE_STRUCT_SYSINFO
  97. #undef HAVE_SYSINFO
  98. #endif
  99. /* If we don't HAVE_STRUCT_SYSINFO_TOTALRAM, we can't use sysinfo. */
  100. #ifndef HAVE_STRUCT_SYSINFO_TOTALRAM
  101. #undef HAVE_SYSINFO
  102. #endif
  103. #ifdef HAVE_SYSINFO
  104. static int
  105. memlimit_sysinfo(size_t * memlimit)
  106. {
  107. struct sysinfo info;
  108. uint64_t totalmem;
  109. /* Get information from the kernel. */
  110. if (sysinfo(&info))
  111. return (1);
  112. totalmem = info.totalram;
  113. /* If we're on a modern kernel, adjust based on mem_unit. */
  114. #ifdef HAVE_STRUCT_SYSINFO_MEM_UNIT
  115. totalmem = totalmem * info.mem_unit;
  116. #endif
  117. /* Return the value, but clamp to SIZE_MAX if necessary. */
  118. #if UINT64_MAX > SIZE_MAX
  119. if (totalmem > SIZE_MAX)
  120. *memlimit = SIZE_MAX;
  121. else
  122. *memlimit = (size_t)totalmem;
  123. #else
  124. *memlimit = totalmem;
  125. #endif
  126. /* Success! */
  127. return (0);
  128. }
  129. #endif /* HAVE_SYSINFO */
  130. static int
  131. memlimit_rlimit(size_t * memlimit)
  132. {
  133. struct rlimit rl;
  134. uint64_t memrlimit;
  135. /* Find the least of... */
  136. memrlimit = (uint64_t)(-1);
  137. /* ... RLIMIT_AS... */
  138. #ifdef RLIMIT_AS
  139. if (getrlimit(RLIMIT_AS, &rl))
  140. return (1);
  141. if ((rl.rlim_cur != RLIM_INFINITY) &&
  142. ((uint64_t)rl.rlim_cur < memrlimit))
  143. memrlimit = (uint64_t)rl.rlim_cur;
  144. #endif
  145. /* ... RLIMIT_DATA... */
  146. if (getrlimit(RLIMIT_DATA, &rl))
  147. return (1);
  148. if ((rl.rlim_cur != RLIM_INFINITY) &&
  149. ((uint64_t)rl.rlim_cur < memrlimit))
  150. memrlimit = (uint64_t)rl.rlim_cur;
  151. /* ... and RLIMIT_RSS. */
  152. #ifdef RLIMIT_RSS
  153. if (getrlimit(RLIMIT_RSS, &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. /* Return the value, but clamp to SIZE_MAX if necessary. */
  160. #if UINT64_MAX > SIZE_MAX
  161. if (memrlimit > SIZE_MAX)
  162. *memlimit = SIZE_MAX;
  163. else
  164. *memlimit = (size_t)memrlimit;
  165. #else
  166. *memlimit = memrlimit;
  167. #endif
  168. /* Success! */
  169. return (0);
  170. }
  171. #ifdef _SC_PHYS_PAGES
  172. /* Some systems define _SC_PAGESIZE instead of _SC_PAGE_SIZE. */
  173. #ifndef _SC_PAGE_SIZE
  174. #define _SC_PAGE_SIZE _SC_PAGESIZE
  175. #endif
  176. static int
  177. memlimit_sysconf(size_t * memlimit)
  178. {
  179. long pagesize;
  180. long physpages;
  181. uint64_t totalmem;
  182. /* Set errno to 0 in order to distinguish "no limit" from "error". */
  183. errno = 0;
  184. /* Read the two limits. */
  185. if (((pagesize = sysconf(_SC_PAGE_SIZE)) == -1) ||
  186. ((physpages = sysconf(_SC_PHYS_PAGES)) == -1)) {
  187. /*
  188. * Did an error occur? OS X may return EINVAL due to not
  189. * supporting _SC_PHYS_PAGES in spite of defining it.
  190. */
  191. if (errno != 0 && errno != EINVAL)
  192. return (1);
  193. /* If not, there is no limit. */
  194. totalmem = (uint64_t)(-1);
  195. } else {
  196. /* Compute the limit. */
  197. totalmem = (uint64_t)(pagesize) * (uint64_t)(physpages);
  198. }
  199. /* Return the value, but clamp to SIZE_MAX if necessary. */
  200. #if UINT64_MAX > SIZE_MAX
  201. if (totalmem > SIZE_MAX)
  202. *memlimit = SIZE_MAX;
  203. else
  204. *memlimit = (size_t)totalmem;
  205. #else
  206. *memlimit = totalmem;
  207. #endif
  208. /* Success! */
  209. return (0);
  210. }
  211. #endif
  212. int
  213. memtouse(size_t maxmem, double maxmemfrac, size_t * memlimit)
  214. {
  215. size_t usermem_memlimit, memsize_memlimit;
  216. size_t sysinfo_memlimit, rlimit_memlimit;
  217. size_t sysconf_memlimit;
  218. size_t memlimit_min;
  219. size_t memavail;
  220. /* Get memory limits. */
  221. #ifdef HW_USERMEM
  222. if (memlimit_sysctl_hw(&usermem_memlimit, HW_USERMEM))
  223. return (1);
  224. #else
  225. usermem_memlimit = SIZE_MAX;
  226. #endif
  227. #ifdef HW_MEMSIZE
  228. if (memlimit_sysctl_hw(&memsize_memlimit, HW_MEMSIZE))
  229. return (1);
  230. #else
  231. memsize_memlimit = SIZE_MAX;
  232. #endif
  233. #ifdef HAVE_SYSINFO
  234. if (memlimit_sysinfo(&sysinfo_memlimit))
  235. return (1);
  236. #else
  237. sysinfo_memlimit = SIZE_MAX;
  238. #endif
  239. if (memlimit_rlimit(&rlimit_memlimit))
  240. return (1);
  241. #ifdef _SC_PHYS_PAGES
  242. if (memlimit_sysconf(&sysconf_memlimit))
  243. return (1);
  244. #else
  245. sysconf_memlimit = SIZE_MAX;
  246. #endif
  247. #ifdef DEBUG
  248. fprintf(stderr, "Memory limits are %zu %zu %zu %zu %zu\n",
  249. usermem_memlimit, memsize_memlimit,
  250. sysinfo_memlimit, rlimit_memlimit,
  251. sysconf_memlimit);
  252. #endif
  253. /* Find the smallest of them. */
  254. memlimit_min = SIZE_MAX;
  255. if (memlimit_min > usermem_memlimit)
  256. memlimit_min = usermem_memlimit;
  257. if (memlimit_min > memsize_memlimit)
  258. memlimit_min = memsize_memlimit;
  259. if (memlimit_min > sysinfo_memlimit)
  260. memlimit_min = sysinfo_memlimit;
  261. if (memlimit_min > rlimit_memlimit)
  262. memlimit_min = rlimit_memlimit;
  263. if (memlimit_min > sysconf_memlimit)
  264. memlimit_min = sysconf_memlimit;
  265. /* Only use the specified fraction of the available memory. */
  266. if ((maxmemfrac > 0.5) || (maxmemfrac == 0.0))
  267. maxmemfrac = 0.5;
  268. memavail = (size_t)(maxmemfrac * memlimit_min);
  269. /* Don't use more than the specified maximum. */
  270. if ((maxmem > 0) && (memavail > maxmem))
  271. memavail = maxmem;
  272. /* But always allow at least 1 MiB. */
  273. if (memavail < 1048576)
  274. memavail = 1048576;
  275. #ifdef DEBUG
  276. fprintf(stderr, "Allowing up to %zu memory to be used\n", memavail);
  277. #endif
  278. /* Return limit via the provided pointer. */
  279. *memlimit = memavail;
  280. return (0);
  281. }