memlimit.c 7.6 KB

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