You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
14 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ident "$Id$"
  4. /*
  5. COPYING CONDITIONS NOTICE:
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation, and provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain this COPYING
  11. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  12. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  13. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  14. GRANT (below).
  15. * Redistributions in binary form must reproduce this COPYING
  16. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  17. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  18. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  19. GRANT (below) in the documentation and/or other materials
  20. provided with the distribution.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. 02110-1301, USA.
  25. COPYRIGHT NOTICE:
  26. TokuDB, Tokutek Fractal Tree Indexing Library.
  27. Copyright (C) 2007-2013 Tokutek, Inc.
  28. DISCLAIMER:
  29. This program is distributed in the hope that it will be useful, but
  30. WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. General Public License for more details.
  33. UNIVERSITY PATENT NOTICE:
  34. The technology is licensed by the Massachusetts Institute of
  35. Technology, Rutgers State University of New Jersey, and the Research
  36. Foundation of State University of New York at Stony Brook under
  37. United States of America Serial No. 11/760379 and to the patents
  38. and/or patent applications resulting from it.
  39. PATENT MARKING NOTICE:
  40. This software is covered by US Patent No. 8,185,551.
  41. PATENT RIGHTS GRANT:
  42. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  43. Tokutek as part of the Fractal Tree project.
  44. "PATENT CLAIMS" means the claims of patents that are owned or
  45. licensable by Tokutek, both currently or in the future; and that in
  46. the absence of this license would be infringed by THIS
  47. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  48. "PATENT CHALLENGE" shall mean a challenge to the validity,
  49. patentability, enforceability and/or non-infringement of any of the
  50. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  51. Tokutek hereby grants to you, for the term and geographical scope of
  52. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  53. irrevocable (except as stated in this section) patent license to
  54. make, have made, use, offer to sell, sell, import, transfer, and
  55. otherwise run, modify, and propagate the contents of THIS
  56. IMPLEMENTATION, where such license applies only to the PATENT
  57. CLAIMS. This grant does not include claims that would be infringed
  58. only as a consequence of further modifications of THIS
  59. IMPLEMENTATION. If you or your agent or licensee institute or order
  60. or agree to the institution of patent litigation against any entity
  61. (including a cross-claim or counterclaim in a lawsuit) alleging that
  62. THIS IMPLEMENTATION constitutes direct or contributory patent
  63. infringement, or inducement of patent infringement, then any rights
  64. granted to you under this License shall terminate as of the date
  65. such litigation is filed. If you or your agent or exclusive
  66. licensee institute or order or agree to the institution of a PATENT
  67. CHALLENGE, then Tokutek may terminate any rights granted to you
  68. under this License.
  69. */
  70. #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
  71. #include "toku_config.h"
  72. #include <toku_portability.h>
  73. #include <string.h>
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #if defined(HAVE_MALLOC_H)
  77. # include <malloc.h>
  78. #elif defined(HAVE_SYS_MALLOC_H)
  79. # include <sys/malloc.h>
  80. #endif
  81. #include <dlfcn.h>
  82. #include <toku_race_tools.h>
  83. #include "memory.h"
  84. #include "toku_assert.h"
  85. #include <portability/toku_atomic.h>
  86. static malloc_fun_t t_malloc = 0;
  87. static malloc_aligned_fun_t t_malloc_aligned = 0;
  88. static malloc_fun_t t_xmalloc = 0;
  89. static malloc_aligned_fun_t t_xmalloc_aligned = 0;
  90. static free_fun_t t_free = 0;
  91. static realloc_fun_t t_realloc = 0;
  92. static realloc_aligned_fun_t t_realloc_aligned = 0;
  93. static realloc_fun_t t_xrealloc = 0;
  94. static LOCAL_MEMORY_STATUS_S status;
  95. int toku_memory_do_stats = 0;
  96. static bool memory_startup_complete;
  97. int
  98. toku_memory_startup(void) {
  99. if (memory_startup_complete) {
  100. return 0;
  101. }
  102. memory_startup_complete = true;
  103. int result = 0;
  104. #if defined(HAVE_M_MMAP_THRESHOLD)
  105. // initialize libc malloc
  106. size_t mmap_threshold = 64 * 1024; // 64K and larger should be malloced with mmap().
  107. int success = mallopt(M_MMAP_THRESHOLD, mmap_threshold);
  108. if (success) {
  109. status.mallocator_version = "libc";
  110. status.mmap_threshold = mmap_threshold;
  111. } else
  112. result = EINVAL;
  113. #else
  114. // just a guess
  115. status.mallocator_version = "darwin";
  116. status.mmap_threshold = 16 * 1024;
  117. #endif
  118. // jemalloc has a mallctl function, while libc malloc does not. we can check if jemalloc
  119. // is loaded by checking if the mallctl function can be found. if it can, we call it
  120. // to get version and mmap threshold configuration.
  121. typedef int (*mallctl_fun_t)(const char *, void *, size_t *, void *, size_t);
  122. mallctl_fun_t mallctl_f;
  123. mallctl_f = (mallctl_fun_t) dlsym(RTLD_DEFAULT, "mallctl");
  124. if (mallctl_f) { // jemalloc is loaded
  125. size_t version_length = sizeof status.mallocator_version;
  126. result = mallctl_f("version", &status.mallocator_version, &version_length, NULL, 0);
  127. if (result == 0) {
  128. size_t lg_chunk; // log2 of the mmap threshold
  129. size_t lg_chunk_length = sizeof lg_chunk;
  130. result = mallctl_f("opt.lg_chunk", &lg_chunk, &lg_chunk_length, NULL, 0);
  131. if (result == 0)
  132. status.mmap_threshold = 1 << lg_chunk;
  133. }
  134. }
  135. return result;
  136. }
  137. static bool memory_shutdown_complete;
  138. void
  139. toku_memory_shutdown(void) {
  140. if (memory_shutdown_complete) {
  141. return;
  142. }
  143. memory_shutdown_complete = true;
  144. }
  145. void
  146. toku_memory_get_status(LOCAL_MEMORY_STATUS s) {
  147. *s = status;
  148. }
  149. // jemalloc's malloc_usable_size does not work with a NULL pointer, so we implement a version that works
  150. static size_t
  151. my_malloc_usable_size(void *p) {
  152. return p == NULL ? 0 : os_malloc_usable_size(p);
  153. }
  154. // Note that max_in_use may be slightly off because use of max_in_use is not thread-safe.
  155. // It is not worth the overhead to make it completely accurate, but
  156. // this logic is intended to guarantee that it increases monotonically.
  157. // Note that status.sum_used and status.sum_freed increase monotonically
  158. // and that status.max_in_use is declared volatile.
  159. static inline void
  160. set_max(uint64_t sum_used, uint64_t sum_freed) {
  161. if (sum_used >= sum_freed) {
  162. uint64_t in_use = sum_used - sum_freed;
  163. uint64_t old_max;
  164. do {
  165. old_max = status.max_in_use;
  166. } while (old_max < in_use &&
  167. !toku_sync_bool_compare_and_swap(&status.max_in_use, old_max, in_use));
  168. }
  169. }
  170. size_t
  171. toku_memory_footprint(void * p, size_t touched) {
  172. size_t rval = 0;
  173. size_t pagesize = toku_os_get_pagesize();
  174. if (p) {
  175. size_t usable = my_malloc_usable_size(p);
  176. if (usable >= status.mmap_threshold) {
  177. int num_pages = (touched + pagesize) / pagesize;
  178. rval = num_pages * pagesize;
  179. }
  180. else {
  181. rval = usable;
  182. }
  183. }
  184. return rval;
  185. }
  186. void *
  187. toku_malloc(size_t size) {
  188. void *p = t_malloc ? t_malloc(size) : os_malloc(size);
  189. if (p) {
  190. TOKU_ANNOTATE_NEW_MEMORY(p, size); // see #4671 and https://bugs.kde.org/show_bug.cgi?id=297147
  191. if (toku_memory_do_stats) {
  192. size_t used = my_malloc_usable_size(p);
  193. toku_sync_add_and_fetch(&status.malloc_count, 1);
  194. toku_sync_add_and_fetch(&status.requested,size);
  195. toku_sync_add_and_fetch(&status.used, used);
  196. set_max(status.used, status.freed);
  197. }
  198. } else {
  199. toku_sync_add_and_fetch(&status.malloc_fail, 1);
  200. }
  201. return p;
  202. }
  203. void *toku_malloc_aligned(size_t alignment, size_t size) {
  204. void *p = t_malloc_aligned ? t_malloc_aligned(alignment, size) : os_malloc_aligned(alignment, size);
  205. if (p) {
  206. TOKU_ANNOTATE_NEW_MEMORY(p, size); // see #4671 and https://bugs.kde.org/show_bug.cgi?id=297147
  207. if (toku_memory_do_stats) {
  208. size_t used = my_malloc_usable_size(p);
  209. toku_sync_add_and_fetch(&status.malloc_count, 1);
  210. toku_sync_add_and_fetch(&status.requested,size);
  211. toku_sync_add_and_fetch(&status.used, used);
  212. set_max(status.used, status.freed);
  213. }
  214. } else {
  215. toku_sync_add_and_fetch(&status.malloc_fail, 1);
  216. }
  217. return p;
  218. }
  219. void *
  220. toku_calloc(size_t nmemb, size_t size) {
  221. size_t newsize = nmemb * size;
  222. void *p = toku_malloc(newsize);
  223. if (p) memset(p, 0, newsize);
  224. return p;
  225. }
  226. void *
  227. toku_realloc(void *p, size_t size) {
  228. size_t used_orig = p ? my_malloc_usable_size(p) : 0;
  229. void *q = t_realloc ? t_realloc(p, size) : os_realloc(p, size);
  230. if (q) {
  231. if (toku_memory_do_stats) {
  232. size_t used = my_malloc_usable_size(q);
  233. toku_sync_add_and_fetch(&status.realloc_count, 1);
  234. toku_sync_add_and_fetch(&status.requested, size);
  235. toku_sync_add_and_fetch(&status.used, used);
  236. toku_sync_add_and_fetch(&status.freed, used_orig);
  237. set_max(status.used, status.freed);
  238. }
  239. } else {
  240. toku_sync_add_and_fetch(&status.realloc_fail, 1);
  241. }
  242. return q;
  243. }
  244. void *toku_realloc_aligned(size_t alignment, void *p, size_t size) {
  245. size_t used_orig = p ? my_malloc_usable_size(p) : 0;
  246. void *q = t_realloc_aligned ? t_realloc_aligned(alignment, p, size) : os_realloc_aligned(alignment, p, size);
  247. if (q) {
  248. if (toku_memory_do_stats) {
  249. size_t used = my_malloc_usable_size(q);
  250. toku_sync_add_and_fetch(&status.realloc_count, 1);
  251. toku_sync_add_and_fetch(&status.requested, size);
  252. toku_sync_add_and_fetch(&status.used, used);
  253. toku_sync_add_and_fetch(&status.freed, used_orig);
  254. set_max(status.used, status.freed);
  255. }
  256. } else {
  257. toku_sync_add_and_fetch(&status.realloc_fail, 1);
  258. }
  259. return q;
  260. }
  261. void *
  262. toku_memdup(const void *v, size_t len) {
  263. void *p = toku_malloc(len);
  264. if (p) memcpy(p, v,len);
  265. return p;
  266. }
  267. char *
  268. toku_strdup(const char *s) {
  269. return (char *) toku_memdup(s, strlen(s)+1);
  270. }
  271. void
  272. toku_free(void *p) {
  273. if (p) {
  274. if (toku_memory_do_stats) {
  275. size_t used = my_malloc_usable_size(p);
  276. toku_sync_add_and_fetch(&status.free_count, 1);
  277. toku_sync_add_and_fetch(&status.freed, used);
  278. }
  279. if (t_free)
  280. t_free(p);
  281. else
  282. os_free(p);
  283. }
  284. }
  285. void *
  286. toku_xmalloc(size_t size) {
  287. void *p = t_xmalloc ? t_xmalloc(size) : os_malloc(size);
  288. if (p == NULL) // avoid function call in common case
  289. resource_assert(p);
  290. TOKU_ANNOTATE_NEW_MEMORY(p, size); // see #4671 and https://bugs.kde.org/show_bug.cgi?id=297147
  291. if (toku_memory_do_stats) {
  292. size_t used = my_malloc_usable_size(p);
  293. toku_sync_add_and_fetch(&status.malloc_count, 1);
  294. toku_sync_add_and_fetch(&status.requested, size);
  295. toku_sync_add_and_fetch(&status.used, used);
  296. set_max(status.used, status.freed);
  297. }
  298. return p;
  299. }
  300. void* toku_xmalloc_aligned(size_t alignment, size_t size)
  301. // Effect: Perform a malloc(size) with the additional property that the returned pointer is a multiple of ALIGNMENT.
  302. // Fail with a resource_assert if the allocation fails (don't return an error code).
  303. // Requires: alignment is a power of two.
  304. {
  305. void *p = t_xmalloc_aligned ? t_xmalloc_aligned(alignment, size) : os_malloc_aligned(alignment,size);
  306. resource_assert(p);
  307. if (toku_memory_do_stats) {
  308. size_t used = my_malloc_usable_size(p);
  309. toku_sync_add_and_fetch(&status.malloc_count, 1);
  310. toku_sync_add_and_fetch(&status.requested, size);
  311. toku_sync_add_and_fetch(&status.used, used);
  312. set_max(status.used, status.freed);
  313. }
  314. return p;
  315. }
  316. void *
  317. toku_xcalloc(size_t nmemb, size_t size) {
  318. size_t newsize = nmemb * size;
  319. void *vp = toku_xmalloc(newsize);
  320. if (vp) memset(vp, 0, newsize);
  321. return vp;
  322. }
  323. void *
  324. toku_xrealloc(void *v, size_t size) {
  325. size_t used_orig = v ? my_malloc_usable_size(v) : 0;
  326. void *p = t_xrealloc ? t_xrealloc(v, size) : os_realloc(v, size);
  327. if (p == 0) // avoid function call in common case
  328. resource_assert(p);
  329. if (toku_memory_do_stats) {
  330. size_t used = my_malloc_usable_size(p);
  331. toku_sync_add_and_fetch(&status.realloc_count, 1);
  332. toku_sync_add_and_fetch(&status.requested, size);
  333. toku_sync_add_and_fetch(&status.used, used);
  334. toku_sync_add_and_fetch(&status.freed, used_orig);
  335. set_max(status.used, status.freed);
  336. }
  337. return p;
  338. }
  339. size_t
  340. toku_malloc_usable_size(void *p) {
  341. return my_malloc_usable_size(p);
  342. }
  343. void *
  344. toku_xmemdup (const void *v, size_t len) {
  345. void *p = toku_xmalloc(len);
  346. memcpy(p, v, len);
  347. return p;
  348. }
  349. char *
  350. toku_xstrdup (const char *s) {
  351. return (char *) toku_xmemdup(s, strlen(s)+1);
  352. }
  353. void
  354. toku_set_func_malloc(malloc_fun_t f) {
  355. t_malloc = f;
  356. t_xmalloc = f;
  357. }
  358. void
  359. toku_set_func_xmalloc_only(malloc_fun_t f) {
  360. t_xmalloc = f;
  361. }
  362. void
  363. toku_set_func_malloc_only(malloc_fun_t f) {
  364. t_malloc = f;
  365. }
  366. void
  367. toku_set_func_realloc(realloc_fun_t f) {
  368. t_realloc = f;
  369. t_xrealloc = f;
  370. }
  371. void
  372. toku_set_func_xrealloc_only(realloc_fun_t f) {
  373. t_xrealloc = f;
  374. }
  375. void
  376. toku_set_func_realloc_only(realloc_fun_t f) {
  377. t_realloc = f;
  378. }
  379. void
  380. toku_set_func_free(free_fun_t f) {
  381. t_free = f;
  382. }
  383. #include <toku_race_tools.h>
  384. void __attribute__((constructor)) toku_memory_helgrind_ignore(void);
  385. void
  386. toku_memory_helgrind_ignore(void) {
  387. TOKU_VALGRIND_HG_DISABLE_CHECKING(&status, sizeof status);
  388. }