PageRenderTime 208ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/erts/emulator/beam/erl_alloc.c

https://github.com/andysan/otp
C | 3315 lines | 2849 code | 382 blank | 84 comment | 478 complexity | 35769bbd718f085db90604c9d872da3c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. /*
  2. * %CopyrightBegin%
  3. *
  4. * Copyright Ericsson AB 2002-2010. All Rights Reserved.
  5. *
  6. * The contents of this file are subject to the Erlang Public License,
  7. * Version 1.1, (the "License"); you may not use this file except in
  8. * compliance with the License. You should have received a copy of the
  9. * Erlang Public License along with this software. If not, it can be
  10. * retrieved online at http://www.erlang.org/.
  11. *
  12. * Software distributed under the License is distributed on an "AS IS"
  13. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  14. * the License for the specific language governing rights and limitations
  15. * under the License.
  16. *
  17. * %CopyrightEnd%
  18. */
  19. /*
  20. * Description: Management of memory allocators.
  21. *
  22. * Author: Rickard Green
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. # include "config.h"
  26. #endif
  27. #define ERTS_ALLOC_C__
  28. #define ERTS_ALC_INTERNAL__
  29. #include "sys.h"
  30. #define ERL_THREADS_EMU_INTERNAL__
  31. #include "erl_threads.h"
  32. #include "global.h"
  33. #include "erl_db.h"
  34. #include "erl_binary.h"
  35. #include "erl_bits.h"
  36. #include "erl_instrument.h"
  37. #include "erl_mseg.h"
  38. #include "erl_monitors.h"
  39. #include "erl_bif_timer.h"
  40. #if defined(ERTS_ALC_T_DRV_SEL_D_STATE) || defined(ERTS_ALC_T_DRV_EV_D_STATE)
  41. #include "erl_check_io.h"
  42. #endif
  43. #define GET_ERL_GF_ALLOC_IMPL
  44. #include "erl_goodfit_alloc.h"
  45. #define GET_ERL_BF_ALLOC_IMPL
  46. #include "erl_bestfit_alloc.h"
  47. #define GET_ERL_AF_ALLOC_IMPL
  48. #include "erl_afit_alloc.h"
  49. #define ERTS_ALC_DEFAULT_MAX_THR_PREF 16
  50. #if defined(SMALL_MEMORY) || defined(PURIFY) || defined(VALGRIND)
  51. #define AU_ALLOC_DEFAULT_ENABLE(X) 0
  52. #else
  53. #define AU_ALLOC_DEFAULT_ENABLE(X) (X)
  54. #endif
  55. #ifdef DEBUG
  56. static Uint install_debug_functions(void);
  57. #if 0
  58. #define HARD_DEBUG
  59. #ifdef __GNUC__
  60. #warning "* * * * * * * * * * * * * *"
  61. #warning "* HARD DEBUG IS ENABLED! *"
  62. #warning "* * * * * * * * * * * * * *"
  63. #endif
  64. #endif
  65. #endif
  66. ErtsAllocatorFunctions_t erts_allctrs[ERTS_ALC_A_MAX+1];
  67. ErtsAllocatorInfo_t erts_allctrs_info[ERTS_ALC_A_MAX+1];
  68. ErtsAllocatorThrSpec_t erts_allctr_thr_spec[ERTS_ALC_A_MAX+1];
  69. #define ERTS_MIN(A, B) ((A) < (B) ? (A) : (B))
  70. #define ERTS_MAX(A, B) ((A) > (B) ? (A) : (B))
  71. typedef union {
  72. GFAllctr_t gfa;
  73. char align_gfa[ERTS_ALC_CACHE_LINE_ALIGN_SIZE(sizeof(GFAllctr_t))];
  74. BFAllctr_t bfa;
  75. char align_bfa[ERTS_ALC_CACHE_LINE_ALIGN_SIZE(sizeof(BFAllctr_t))];
  76. AFAllctr_t afa;
  77. char align_afa[ERTS_ALC_CACHE_LINE_ALIGN_SIZE(sizeof(AFAllctr_t))];
  78. } ErtsAllocatorState_t;
  79. static ErtsAllocatorState_t sl_alloc_state;
  80. static ErtsAllocatorState_t std_alloc_state;
  81. static ErtsAllocatorState_t ll_alloc_state;
  82. static ErtsAllocatorState_t temp_alloc_state;
  83. static ErtsAllocatorState_t eheap_alloc_state;
  84. static ErtsAllocatorState_t binary_alloc_state;
  85. static ErtsAllocatorState_t ets_alloc_state;
  86. static ErtsAllocatorState_t driver_alloc_state;
  87. ErtsAlcType_t erts_fix_core_allocator_ix;
  88. #ifdef ERTS_ALC_N_MIN_A_FIXED_SIZE
  89. static void *(*fix_core_allocator)(ErtsAlcType_t, void *, Uint);
  90. static void *fix_core_extra;
  91. static void *fix_core_alloc(Uint size)
  92. {
  93. void *res;
  94. res = (*fix_core_allocator)(ERTS_ALC_T_UNDEF, fix_core_extra, size);
  95. if (erts_mtrace_enabled)
  96. erts_mtrace_crr_alloc(res,
  97. ERTS_ALC_A_FIXED_SIZE,
  98. erts_fix_core_allocator_ix,
  99. size);
  100. return res;
  101. }
  102. #endif
  103. enum allctr_type {
  104. GOODFIT,
  105. BESTFIT,
  106. AFIT
  107. };
  108. struct au_init {
  109. int enable;
  110. int thr_spec;
  111. enum allctr_type atype;
  112. struct {
  113. AllctrInit_t util;
  114. GFAllctrInit_t gf;
  115. BFAllctrInit_t bf;
  116. AFAllctrInit_t af;
  117. } init;
  118. struct {
  119. int mmbcs;
  120. int lmbcs;
  121. int smbcs;
  122. int mmmbc;
  123. } default_;
  124. };
  125. #define DEFAULT_ALLCTR_INIT { \
  126. ERTS_DEFAULT_ALLCTR_INIT, \
  127. ERTS_DEFAULT_GF_ALLCTR_INIT, \
  128. ERTS_DEFAULT_BF_ALLCTR_INIT, \
  129. ERTS_DEFAULT_AF_ALLCTR_INIT \
  130. }
  131. typedef struct {
  132. int erts_alloc_config;
  133. #if HAVE_ERTS_MSEG
  134. ErtsMsegInit_t mseg;
  135. #endif
  136. int trim_threshold;
  137. int top_pad;
  138. AlcUInit_t alloc_util;
  139. struct {
  140. int stat;
  141. int map;
  142. char *mtrace;
  143. char *nodename;
  144. } instr;
  145. struct au_init sl_alloc;
  146. struct au_init std_alloc;
  147. struct au_init ll_alloc;
  148. struct au_init temp_alloc;
  149. struct au_init eheap_alloc;
  150. struct au_init binary_alloc;
  151. struct au_init ets_alloc;
  152. struct au_init driver_alloc;
  153. } erts_alc_hndl_args_init_t;
  154. #define ERTS_AU_INIT__ {0, 0, GOODFIT, DEFAULT_ALLCTR_INIT, {1,1,1,1}}
  155. #define SET_DEFAULT_ALLOC_OPTS(IP) \
  156. do { \
  157. struct au_init aui__ = ERTS_AU_INIT__; \
  158. sys_memcpy((void *) (IP), (void *) &aui__, sizeof(struct au_init)); \
  159. } while (0)
  160. static void
  161. set_default_sl_alloc_opts(struct au_init *ip)
  162. {
  163. SET_DEFAULT_ALLOC_OPTS(ip);
  164. ip->enable = AU_ALLOC_DEFAULT_ENABLE(1);
  165. ip->thr_spec = 1;
  166. ip->atype = GOODFIT;
  167. ip->init.util.name_prefix = "sl_";
  168. ip->init.util.mmmbc = 5;
  169. ip->init.util.alloc_no = ERTS_ALC_A_SHORT_LIVED;
  170. #ifndef SMALL_MEMORY
  171. ip->init.util.mmbcs = 128*1024; /* Main carrier size */
  172. #else
  173. ip->init.util.mmbcs = 32*1024; /* Main carrier size */
  174. #endif
  175. ip->init.util.ts = ERTS_ALC_MTA_SHORT_LIVED;
  176. ip->init.util.rsbcst = 80;
  177. }
  178. static void
  179. set_default_std_alloc_opts(struct au_init *ip)
  180. {
  181. SET_DEFAULT_ALLOC_OPTS(ip);
  182. ip->enable = AU_ALLOC_DEFAULT_ENABLE(1);
  183. ip->thr_spec = 1;
  184. ip->atype = BESTFIT;
  185. ip->init.util.name_prefix = "std_";
  186. ip->init.util.mmmbc = 5;
  187. ip->init.util.alloc_no = ERTS_ALC_A_STANDARD;
  188. #ifndef SMALL_MEMORY
  189. ip->init.util.mmbcs = 128*1024; /* Main carrier size */
  190. #else
  191. ip->init.util.mmbcs = 32*1024; /* Main carrier size */
  192. #endif
  193. ip->init.util.ts = ERTS_ALC_MTA_STANDARD;
  194. }
  195. static void
  196. set_default_ll_alloc_opts(struct au_init *ip)
  197. {
  198. SET_DEFAULT_ALLOC_OPTS(ip);
  199. ip->enable = AU_ALLOC_DEFAULT_ENABLE(1);
  200. ip->thr_spec = 0;
  201. ip->atype = BESTFIT;
  202. ip->init.bf.ao = 1;
  203. ip->init.util.ramv = 0;
  204. ip->init.util.mmsbc = 0;
  205. ip->init.util.mmmbc = 0;
  206. ip->init.util.sbct = ~((UWord) 0);
  207. ip->init.util.name_prefix = "ll_";
  208. ip->init.util.alloc_no = ERTS_ALC_A_LONG_LIVED;
  209. #ifndef SMALL_MEMORY
  210. ip->init.util.mmbcs = 2*1024*1024; /* Main carrier size */
  211. #else
  212. ip->init.util.mmbcs = 1*1024*1024; /* Main carrier size */
  213. #endif
  214. ip->init.util.ts = ERTS_ALC_MTA_LONG_LIVED;
  215. ip->init.util.asbcst = 0;
  216. ip->init.util.rsbcst = 0;
  217. ip->init.util.rsbcmt = 0;
  218. ip->init.util.rmbcmt = 0;
  219. }
  220. static void
  221. set_default_temp_alloc_opts(struct au_init *ip)
  222. {
  223. SET_DEFAULT_ALLOC_OPTS(ip);
  224. ip->enable = AU_ALLOC_DEFAULT_ENABLE(1);
  225. ip->thr_spec = 1;
  226. ip->atype = AFIT;
  227. ip->init.util.name_prefix = "temp_";
  228. ip->init.util.alloc_no = ERTS_ALC_A_TEMPORARY;
  229. #ifndef SMALL_MEMORY
  230. ip->init.util.mmbcs = 128*1024; /* Main carrier size */
  231. #else
  232. ip->init.util.mmbcs = 32*1024; /* Main carrier size */
  233. #endif
  234. ip->init.util.ts = ERTS_ALC_MTA_TEMPORARY;
  235. ip->init.util.rsbcst = 90;
  236. ip->init.util.rmbcmt = 100;
  237. }
  238. static void
  239. set_default_eheap_alloc_opts(struct au_init *ip)
  240. {
  241. SET_DEFAULT_ALLOC_OPTS(ip);
  242. ip->enable = AU_ALLOC_DEFAULT_ENABLE(1);
  243. ip->thr_spec = 1;
  244. ip->atype = GOODFIT;
  245. ip->init.util.mmmbc = 100;
  246. ip->init.util.name_prefix = "eheap_";
  247. ip->init.util.alloc_no = ERTS_ALC_A_EHEAP;
  248. #ifndef SMALL_MEMORY
  249. ip->init.util.mmbcs = 512*1024; /* Main carrier size */
  250. #else
  251. ip->init.util.mmbcs = 256*1024; /* Main carrier size */
  252. #endif
  253. ip->init.util.ts = ERTS_ALC_MTA_EHEAP;
  254. ip->init.util.rsbcst = 50;
  255. }
  256. static void
  257. set_default_binary_alloc_opts(struct au_init *ip)
  258. {
  259. SET_DEFAULT_ALLOC_OPTS(ip);
  260. ip->enable = AU_ALLOC_DEFAULT_ENABLE(1);
  261. ip->thr_spec = 1;
  262. ip->atype = BESTFIT;
  263. ip->init.util.mmmbc = 50;
  264. ip->init.util.name_prefix = "binary_";
  265. ip->init.util.alloc_no = ERTS_ALC_A_BINARY;
  266. #ifndef SMALL_MEMORY
  267. ip->init.util.mmbcs = 128*1024; /* Main carrier size */
  268. #else
  269. ip->init.util.mmbcs = 32*1024; /* Main carrier size */
  270. #endif
  271. ip->init.util.ts = ERTS_ALC_MTA_BINARY;
  272. }
  273. static void
  274. set_default_ets_alloc_opts(struct au_init *ip)
  275. {
  276. SET_DEFAULT_ALLOC_OPTS(ip);
  277. ip->enable = AU_ALLOC_DEFAULT_ENABLE(1);
  278. ip->thr_spec = 1;
  279. ip->atype = BESTFIT;
  280. ip->init.util.mmmbc = 100;
  281. ip->init.util.name_prefix = "ets_";
  282. ip->init.util.alloc_no = ERTS_ALC_A_ETS;
  283. #ifndef SMALL_MEMORY
  284. ip->init.util.mmbcs = 128*1024; /* Main carrier size */
  285. #else
  286. ip->init.util.mmbcs = 32*1024; /* Main carrier size */
  287. #endif
  288. ip->init.util.ts = ERTS_ALC_MTA_ETS;
  289. }
  290. static void
  291. set_default_driver_alloc_opts(struct au_init *ip)
  292. {
  293. SET_DEFAULT_ALLOC_OPTS(ip);
  294. ip->enable = AU_ALLOC_DEFAULT_ENABLE(1);
  295. ip->thr_spec = 1;
  296. ip->atype = BESTFIT;
  297. ip->init.util.name_prefix = "driver_";
  298. ip->init.util.alloc_no = ERTS_ALC_A_DRIVER;
  299. #ifndef SMALL_MEMORY
  300. ip->init.util.mmbcs = 128*1024; /* Main carrier size */
  301. #else
  302. ip->init.util.mmbcs = 32*1024; /* Main carrier size */
  303. #endif
  304. ip->init.util.ts = ERTS_ALC_MTA_DRIVER;
  305. }
  306. #ifdef ERTS_SMP
  307. static void
  308. adjust_tpref(struct au_init *ip, int no_sched)
  309. {
  310. if (ip->thr_spec) {
  311. Uint allocs;
  312. if (ip->thr_spec < 0) {/* User specified amount */
  313. allocs = abs(ip->thr_spec);
  314. if (allocs > no_sched)
  315. allocs = no_sched;
  316. }
  317. else if (no_sched > ERTS_ALC_DEFAULT_MAX_THR_PREF)
  318. allocs = ERTS_ALC_DEFAULT_MAX_THR_PREF;
  319. else
  320. allocs = no_sched;
  321. if (allocs <= 1)
  322. ip->thr_spec = 0;
  323. else {
  324. ip->thr_spec = (int) allocs;
  325. ip->thr_spec *= -1; /* thread preferred */
  326. /* If default ... */
  327. /* ... shrink main multi-block carrier size */
  328. if (ip->default_.mmbcs)
  329. ip->init.util.mmbcs /= ERTS_MIN(4, allocs);
  330. /* ... shrink largest multi-block carrier size */
  331. if (ip->default_.lmbcs)
  332. ip->init.util.lmbcs /= ERTS_MIN(2, allocs);
  333. /* ... shrink smallest multi-block carrier size */
  334. if (ip->default_.smbcs)
  335. ip->init.util.smbcs /= ERTS_MIN(4, allocs);
  336. /* ... and more than three allocators shrink
  337. max mseg multi-block carriers */
  338. if (ip->default_.mmmbc && allocs > 2) {
  339. ip->init.util.mmmbc /= ERTS_MIN(4, allocs - 1);
  340. if (ip->init.util.mmmbc < 3)
  341. ip->init.util.mmmbc = 3;
  342. }
  343. }
  344. }
  345. }
  346. #endif
  347. static void handle_args(int *, char **, erts_alc_hndl_args_init_t *);
  348. static void
  349. set_au_allocator(ErtsAlcType_t alctr_n, struct au_init *init);
  350. static void
  351. start_au_allocator(ErtsAlcType_t alctr_n,
  352. struct au_init *init,
  353. ErtsAllocatorState_t *state);
  354. static void
  355. refuse_af_strategy(struct au_init *init)
  356. {
  357. if (init->atype == AFIT)
  358. init->atype = GOODFIT;
  359. }
  360. static void init_thr_ix(int static_ixs);
  361. #ifdef HARD_DEBUG
  362. static void hdbg_init(void);
  363. #endif
  364. void
  365. erts_alloc_init(int *argc, char **argv, ErtsAllocInitOpts *eaiop)
  366. {
  367. UWord extra_block_size = 0;
  368. int i;
  369. erts_alc_hndl_args_init_t init = {
  370. 0,
  371. #if HAVE_ERTS_MSEG
  372. ERTS_MSEG_INIT_DEFAULT_INITIALIZER,
  373. #endif
  374. ERTS_DEFAULT_TRIM_THRESHOLD,
  375. ERTS_DEFAULT_TOP_PAD,
  376. ERTS_DEFAULT_ALCU_INIT
  377. };
  378. #ifdef HARD_DEBUG
  379. hdbg_init();
  380. #endif
  381. erts_sys_alloc_init();
  382. init_thr_ix(erts_no_schedulers);
  383. erts_init_utils_mem();
  384. set_default_sl_alloc_opts(&init.sl_alloc);
  385. set_default_std_alloc_opts(&init.std_alloc);
  386. set_default_ll_alloc_opts(&init.ll_alloc);
  387. set_default_temp_alloc_opts(&init.temp_alloc);
  388. set_default_eheap_alloc_opts(&init.eheap_alloc);
  389. set_default_binary_alloc_opts(&init.binary_alloc);
  390. set_default_ets_alloc_opts(&init.ets_alloc);
  391. set_default_driver_alloc_opts(&init.driver_alloc);
  392. if (argc && argv)
  393. handle_args(argc, argv, &init);
  394. if (erts_no_schedulers <= 1) {
  395. init.sl_alloc.thr_spec = 0;
  396. init.std_alloc.thr_spec = 0;
  397. init.ll_alloc.thr_spec = 0;
  398. init.eheap_alloc.thr_spec = 0;
  399. init.binary_alloc.thr_spec = 0;
  400. init.ets_alloc.thr_spec = 0;
  401. init.driver_alloc.thr_spec = 0;
  402. }
  403. if (init.erts_alloc_config) {
  404. /* Adjust flags that erts_alloc_config won't like */
  405. init.temp_alloc.thr_spec = 0;
  406. init.sl_alloc.thr_spec = 0;
  407. init.std_alloc.thr_spec = 0;
  408. init.ll_alloc.thr_spec = 0;
  409. init.eheap_alloc.thr_spec = 0;
  410. init.binary_alloc.thr_spec = 0;
  411. init.ets_alloc.thr_spec = 0;
  412. init.driver_alloc.thr_spec = 0;
  413. }
  414. #ifdef ERTS_SMP
  415. /* Only temp_alloc can use thread specific interface */
  416. if (init.temp_alloc.thr_spec)
  417. init.temp_alloc.thr_spec = erts_no_schedulers;
  418. /* Others must use thread preferred interface */
  419. adjust_tpref(&init.sl_alloc, erts_no_schedulers);
  420. adjust_tpref(&init.std_alloc, erts_no_schedulers);
  421. adjust_tpref(&init.ll_alloc, erts_no_schedulers);
  422. adjust_tpref(&init.eheap_alloc, erts_no_schedulers);
  423. adjust_tpref(&init.binary_alloc, erts_no_schedulers);
  424. adjust_tpref(&init.ets_alloc, erts_no_schedulers);
  425. adjust_tpref(&init.driver_alloc, erts_no_schedulers);
  426. #else
  427. /* No thread specific if not smp */
  428. init.temp_alloc.thr_spec = 0;
  429. #endif
  430. /*
  431. * The following allocators cannot be run with afit strategy.
  432. * Make sure they don't...
  433. */
  434. refuse_af_strategy(&init.sl_alloc);
  435. refuse_af_strategy(&init.std_alloc);
  436. refuse_af_strategy(&init.ll_alloc);
  437. refuse_af_strategy(&init.eheap_alloc);
  438. refuse_af_strategy(&init.binary_alloc);
  439. refuse_af_strategy(&init.ets_alloc);
  440. refuse_af_strategy(&init.driver_alloc);
  441. #ifdef ERTS_SMP
  442. if (!init.temp_alloc.thr_spec)
  443. refuse_af_strategy(&init.temp_alloc);
  444. #endif
  445. erts_mtrace_pre_init();
  446. #if HAVE_ERTS_MSEG
  447. erts_mseg_init(&init.mseg);
  448. #endif
  449. erts_alcu_init(&init.alloc_util);
  450. erts_afalc_init();
  451. erts_bfalc_init();
  452. erts_gfalc_init();
  453. for (i = ERTS_ALC_A_MIN; i <= ERTS_ALC_A_MAX; i++) {
  454. erts_allctrs[i].alloc = NULL;
  455. erts_allctrs[i].realloc = NULL;
  456. erts_allctrs[i].free = NULL;
  457. erts_allctrs[i].extra = NULL;
  458. erts_allctrs_info[i].alloc_util = 0;
  459. erts_allctrs_info[i].enabled = 0;
  460. erts_allctrs_info[i].thr_spec = 0;
  461. erts_allctrs_info[i].extra = NULL;
  462. }
  463. #ifdef ERTS_ALC_N_MIN_A_FIXED_SIZE
  464. #if !defined(PURIFY) && !defined(VALGRIND)
  465. erts_allctrs[ERTS_ALC_A_FIXED_SIZE].alloc = erts_fix_alloc;
  466. erts_allctrs[ERTS_ALC_A_FIXED_SIZE].realloc = erts_fix_realloc;
  467. erts_allctrs[ERTS_ALC_A_FIXED_SIZE].free = erts_fix_free;
  468. erts_allctrs_info[ERTS_ALC_A_FIXED_SIZE].enabled = 1;
  469. #else
  470. erts_allctrs[ERTS_ALC_A_FIXED_SIZE].alloc = erts_sys_alloc;
  471. erts_allctrs[ERTS_ALC_A_FIXED_SIZE].realloc = erts_sys_realloc;
  472. erts_allctrs[ERTS_ALC_A_FIXED_SIZE].free = erts_sys_free;
  473. erts_allctrs_info[ERTS_ALC_A_FIXED_SIZE].enabled = 0;
  474. #endif
  475. #endif
  476. erts_allctrs[ERTS_ALC_A_SYSTEM].alloc = erts_sys_alloc;
  477. erts_allctrs[ERTS_ALC_A_SYSTEM].realloc = erts_sys_realloc;
  478. erts_allctrs[ERTS_ALC_A_SYSTEM].free = erts_sys_free;
  479. erts_allctrs_info[ERTS_ALC_A_SYSTEM].enabled = 1;
  480. set_au_allocator(ERTS_ALC_A_TEMPORARY, &init.temp_alloc);
  481. set_au_allocator(ERTS_ALC_A_SHORT_LIVED, &init.sl_alloc);
  482. set_au_allocator(ERTS_ALC_A_STANDARD, &init.std_alloc);
  483. set_au_allocator(ERTS_ALC_A_LONG_LIVED, &init.ll_alloc);
  484. set_au_allocator(ERTS_ALC_A_EHEAP, &init.eheap_alloc);
  485. set_au_allocator(ERTS_ALC_A_BINARY, &init.binary_alloc);
  486. set_au_allocator(ERTS_ALC_A_ETS, &init.ets_alloc);
  487. set_au_allocator(ERTS_ALC_A_DRIVER, &init.driver_alloc);
  488. for (i = ERTS_ALC_A_MIN; i <= ERTS_ALC_A_MAX; i++) {
  489. if (!erts_allctrs[i].alloc)
  490. erl_exit(ERTS_ABORT_EXIT,
  491. "Missing alloc function for %s\n", ERTS_ALC_A2AD(i));
  492. if (!erts_allctrs[i].realloc)
  493. erl_exit(ERTS_ABORT_EXIT,
  494. "Missing realloc function for %s\n", ERTS_ALC_A2AD(i));
  495. if (!erts_allctrs[i].free)
  496. erl_exit(ERTS_ABORT_EXIT,
  497. "Missing free function for %s\n", ERTS_ALC_A2AD(i));
  498. }
  499. sys_alloc_opt(SYS_ALLOC_OPT_TRIM_THRESHOLD, init.trim_threshold);
  500. sys_alloc_opt(SYS_ALLOC_OPT_TOP_PAD, init.top_pad);
  501. if (erts_allctrs_info[ERTS_FIX_CORE_ALLOCATOR].enabled)
  502. erts_fix_core_allocator_ix = ERTS_FIX_CORE_ALLOCATOR;
  503. else
  504. erts_fix_core_allocator_ix = ERTS_ALC_A_SYSTEM;
  505. erts_mtrace_init(init.instr.mtrace, init.instr.nodename);
  506. start_au_allocator(ERTS_ALC_A_TEMPORARY,
  507. &init.temp_alloc,
  508. &temp_alloc_state);
  509. start_au_allocator(ERTS_ALC_A_SHORT_LIVED,
  510. &init.sl_alloc,
  511. &sl_alloc_state);
  512. start_au_allocator(ERTS_ALC_A_STANDARD,
  513. &init.std_alloc,
  514. &std_alloc_state);
  515. start_au_allocator(ERTS_ALC_A_LONG_LIVED,
  516. &init.ll_alloc,
  517. &ll_alloc_state);
  518. start_au_allocator(ERTS_ALC_A_EHEAP,
  519. &init.eheap_alloc,
  520. &eheap_alloc_state);
  521. start_au_allocator(ERTS_ALC_A_BINARY,
  522. &init.binary_alloc,
  523. &binary_alloc_state);
  524. start_au_allocator(ERTS_ALC_A_ETS,
  525. &init.ets_alloc,
  526. &ets_alloc_state);
  527. start_au_allocator(ERTS_ALC_A_DRIVER,
  528. &init.driver_alloc,
  529. &driver_alloc_state);
  530. fix_core_allocator = erts_allctrs[erts_fix_core_allocator_ix].alloc;
  531. fix_core_extra = erts_allctrs[erts_fix_core_allocator_ix].extra;
  532. erts_mtrace_install_wrapper_functions();
  533. extra_block_size += erts_instr_init(init.instr.stat, init.instr.map);
  534. #ifdef DEBUG
  535. extra_block_size += install_debug_functions();
  536. #endif
  537. #ifdef ERTS_ALC_N_MIN_A_FIXED_SIZE
  538. erts_init_fix_alloc(extra_block_size, fix_core_alloc);
  539. #if !defined(PURIFY) && !defined(VALGRIND)
  540. erts_set_fix_size(ERTS_ALC_T_PROC, sizeof(Process));
  541. erts_set_fix_size(ERTS_ALC_T_DB_TABLE, sizeof(DbTable));
  542. erts_set_fix_size(ERTS_ALC_T_ATOM, sizeof(Atom));
  543. erts_set_fix_size(ERTS_ALC_T_EXPORT, sizeof(Export));
  544. erts_set_fix_size(ERTS_ALC_T_MODULE, sizeof(Module));
  545. erts_set_fix_size(ERTS_ALC_T_REG_PROC, sizeof(RegProc));
  546. erts_set_fix_size(ERTS_ALC_T_MONITOR_SH, ERTS_MONITOR_SH_SIZE*sizeof(Uint));
  547. erts_set_fix_size(ERTS_ALC_T_NLINK_SH, ERTS_LINK_SH_SIZE*sizeof(Uint));
  548. erts_set_fix_size(ERTS_ALC_T_FUN_ENTRY, sizeof(ErlFunEntry));
  549. #ifdef ERTS_ALC_T_DRV_EV_D_STATE
  550. erts_set_fix_size(ERTS_ALC_T_DRV_EV_D_STATE,
  551. sizeof(ErtsDrvEventDataState));
  552. #endif
  553. #ifdef ERTS_ALC_T_DRV_SEL_D_STATE
  554. erts_set_fix_size(ERTS_ALC_T_DRV_SEL_D_STATE,
  555. sizeof(ErtsDrvSelectDataState));
  556. #endif
  557. #endif
  558. #endif
  559. }
  560. static void
  561. set_au_allocator(ErtsAlcType_t alctr_n, struct au_init *init)
  562. {
  563. ErtsAllocatorFunctions_t *af = &erts_allctrs[alctr_n];
  564. ErtsAllocatorInfo_t *ai = &erts_allctrs_info[alctr_n];
  565. ErtsAllocatorThrSpec_t *tspec = &erts_allctr_thr_spec[alctr_n];
  566. if (!init->enable) {
  567. af->alloc = erts_sys_alloc;
  568. af->realloc = erts_sys_realloc;
  569. af->free = erts_sys_free;
  570. af->extra = NULL;
  571. ai->alloc_util = 0;
  572. ai->enabled = 0;
  573. ai->extra = NULL;
  574. return;
  575. }
  576. tspec->enabled = 0;
  577. tspec->all_thr_safe = 0;
  578. ai->thr_spec = 0;
  579. #ifdef USE_THREADS
  580. if (init->thr_spec) {
  581. if (init->thr_spec > 0) {
  582. af->alloc = erts_alcu_alloc_thr_spec;
  583. if (init->init.util.ramv)
  584. af->realloc = erts_alcu_realloc_mv_thr_spec;
  585. else
  586. af->realloc = erts_alcu_realloc_thr_spec;
  587. af->free = erts_alcu_free_thr_spec;
  588. }
  589. else {
  590. af->alloc = erts_alcu_alloc_thr_pref;
  591. if (init->init.util.ramv)
  592. af->realloc = erts_alcu_realloc_mv_thr_pref;
  593. else
  594. af->realloc = erts_alcu_realloc_thr_pref;
  595. af->free = erts_alcu_free_thr_pref;
  596. tspec->all_thr_safe = 1;
  597. }
  598. tspec->enabled = 1;
  599. tspec->size = abs(init->thr_spec) + 1;
  600. ai->thr_spec = tspec->size;
  601. }
  602. else if (init->init.util.ts) {
  603. af->alloc = erts_alcu_alloc_ts;
  604. if (init->init.util.ramv)
  605. af->realloc = erts_alcu_realloc_mv_ts;
  606. else
  607. af->realloc = erts_alcu_realloc_ts;
  608. af->free = erts_alcu_free_ts;
  609. }
  610. else
  611. #endif
  612. {
  613. af->alloc = erts_alcu_alloc;
  614. if (init->init.util.ramv)
  615. af->realloc = erts_alcu_realloc_mv;
  616. else
  617. af->realloc = erts_alcu_realloc;
  618. af->free = erts_alcu_free;
  619. }
  620. af->extra = NULL;
  621. ai->alloc_util = 1;
  622. ai->enabled = 1;
  623. }
  624. static void
  625. start_au_allocator(ErtsAlcType_t alctr_n,
  626. struct au_init *init,
  627. ErtsAllocatorState_t *state)
  628. {
  629. int i;
  630. int size = 1;
  631. void *as0;
  632. enum allctr_type atype;
  633. ErtsAllocatorFunctions_t *af = &erts_allctrs[alctr_n];
  634. ErtsAllocatorInfo_t *ai = &erts_allctrs_info[alctr_n];
  635. ErtsAllocatorThrSpec_t *tspec = &erts_allctr_thr_spec[alctr_n];
  636. if (!init->enable)
  637. return;
  638. if (init->thr_spec) {
  639. void *states = erts_sys_alloc(0,
  640. NULL,
  641. ((sizeof(Allctr_t *)
  642. * (tspec->size + 1))
  643. + (sizeof(ErtsAllocatorState_t)
  644. * tspec->size)
  645. + ERTS_CACHE_LINE_SIZE - 1));
  646. if (!states)
  647. erl_exit(ERTS_ABORT_EXIT,
  648. "Failed to allocate allocator states for %salloc\n",
  649. init->init.util.name_prefix);
  650. tspec->allctr = (Allctr_t **) states;
  651. states = ((char *) states) + sizeof(Allctr_t *) * (tspec->size + 1);
  652. states = ((((UWord) states) & ERTS_CACHE_LINE_MASK)
  653. ? (void *) ((((UWord) states) & ~ERTS_CACHE_LINE_MASK)
  654. + ERTS_CACHE_LINE_SIZE)
  655. : (void *) states);
  656. tspec->allctr[0] = init->thr_spec > 0 ? (Allctr_t *) state : (Allctr_t *) NULL;
  657. size = tspec->size;
  658. for (i = 1; i < size; i++)
  659. tspec->allctr[i] = (Allctr_t *)
  660. &((ErtsAllocatorState_t *) states)[i-1];
  661. }
  662. for (i = 0; i < size; i++) {
  663. void *as;
  664. atype = init->atype;
  665. if (!init->thr_spec)
  666. as0 = state;
  667. else {
  668. as0 = (void *) tspec->allctr[i];
  669. if (!as0)
  670. continue;
  671. if (i == 0) {
  672. if (atype == AFIT)
  673. atype = GOODFIT;
  674. init->init.util.ts = 1;
  675. }
  676. else {
  677. if (init->thr_spec < 0) {
  678. init->init.util.ts = 1;
  679. init->init.util.tspec = 0;
  680. init->init.util.tpref = -1*init->thr_spec;
  681. }
  682. else {
  683. init->init.util.ts = 0;
  684. init->init.util.tspec = init->thr_spec + 1;
  685. init->init.util.tpref = 0;
  686. }
  687. }
  688. }
  689. switch (atype) {
  690. case GOODFIT:
  691. as = (void *) erts_gfalc_start((GFAllctr_t *) as0,
  692. &init->init.gf,
  693. &init->init.util);
  694. break;
  695. case BESTFIT:
  696. as = (void *) erts_bfalc_start((BFAllctr_t *) as0,
  697. &init->init.bf,
  698. &init->init.util);
  699. break;
  700. case AFIT:
  701. as = (void *) erts_afalc_start((AFAllctr_t *) as0,
  702. &init->init.af,
  703. &init->init.util);
  704. break;
  705. default:
  706. as = NULL;
  707. ASSERT(0);
  708. }
  709. if (!as)
  710. erl_exit(ERTS_ABORT_EXIT,
  711. "Failed to start %salloc\n", init->init.util.name_prefix);
  712. ASSERT(as == (void *) as0);
  713. af->extra = as;
  714. }
  715. if (init->thr_spec) {
  716. af->extra = tspec;
  717. init->init.util.ts = 1;
  718. }
  719. ai->extra = af->extra;
  720. }
  721. static void bad_param(char *param_start, char *param_end)
  722. {
  723. size_t len = param_end - param_start;
  724. char param[100];
  725. if (len > 99)
  726. len = 99;
  727. sys_memcpy((void *) param, (void *) param_start, len);
  728. param[len] = '\0';
  729. erts_fprintf(stderr, "bad \"%s\" parameter\n", param);
  730. erts_usage();
  731. }
  732. static void bad_value(char *param_start, char *param_end, char *value)
  733. {
  734. size_t len = param_end - param_start;
  735. char param[100];
  736. if (len > 99)
  737. len = 99;
  738. sys_memcpy((void *) param, (void *) param_start, len);
  739. param[len] = '\0';
  740. erts_fprintf(stderr, "bad \"%s\" value: %s\n", param, value);
  741. erts_usage();
  742. }
  743. /* Get arg marks argument as handled by
  744. putting NULL in argv */
  745. static char *
  746. get_value(char* rest, char** argv, int* ip)
  747. {
  748. char *param = argv[*ip]+1;
  749. argv[*ip] = NULL;
  750. if (*rest == '\0') {
  751. char *next = argv[*ip + 1];
  752. if (next[0] == '-'
  753. && next[1] == '-'
  754. && next[2] == '\0') {
  755. bad_value(param, rest, "");
  756. }
  757. (*ip)++;
  758. argv[*ip] = NULL;
  759. return next;
  760. }
  761. return rest;
  762. }
  763. static ERTS_INLINE int
  764. has_prefix(const char *prefix, const char *string)
  765. {
  766. int i;
  767. for (i = 0; prefix[i]; i++)
  768. if (prefix[i] != string[i])
  769. return 0;
  770. return 1;
  771. }
  772. static int
  773. get_bool_value(char *param_end, char** argv, int* ip)
  774. {
  775. char *param = argv[*ip]+1;
  776. char *value = get_value(param_end, argv, ip);
  777. if (strcmp(value, "true") == 0)
  778. return 1;
  779. else if (strcmp(value, "false") == 0)
  780. return 0;
  781. else
  782. bad_value(param, param_end, value);
  783. return -1;
  784. }
  785. static Uint
  786. get_kb_value(char *param_end, char** argv, int* ip)
  787. {
  788. Sint tmp;
  789. Uint max = ((~((Uint) 0))/1024) + 1;
  790. char *rest;
  791. char *param = argv[*ip]+1;
  792. char *value = get_value(param_end, argv, ip);
  793. errno = 0;
  794. tmp = (Sint) strtol(value, &rest, 10);
  795. if (errno != 0 || rest == value || tmp < 0 || max < ((Uint) tmp))
  796. bad_value(param, param_end, value);
  797. if (max == (Uint) tmp)
  798. return ~((Uint) 0);
  799. else
  800. return ((Uint) tmp)*1024;
  801. }
  802. static Uint
  803. get_amount_value(char *param_end, char** argv, int* ip)
  804. {
  805. Sint tmp;
  806. char *rest;
  807. char *param = argv[*ip]+1;
  808. char *value = get_value(param_end, argv, ip);
  809. errno = 0;
  810. tmp = (Sint) strtol(value, &rest, 10);
  811. if (errno != 0 || rest == value || tmp < 0)
  812. bad_value(param, param_end, value);
  813. return (Uint) tmp;
  814. }
  815. static int
  816. get_bool_or_possitive_amount_value(int *bool, Uint *amount,
  817. char *param_end, char** argv, int* ip)
  818. {
  819. char *param = argv[*ip]+1;
  820. char *value = get_value(param_end, argv, ip);
  821. if (strcmp(value, "true") == 0) {
  822. *bool = 1;
  823. return 1;
  824. }
  825. else if (strcmp(value, "false") == 0) {
  826. *bool = 0;
  827. return 1;
  828. }
  829. else {
  830. Sint tmp;
  831. char *rest;
  832. errno = 0;
  833. tmp = (Sint) strtol(value, &rest, 10);
  834. if (errno != 0 || rest == value || tmp <= 0) {
  835. bad_value(param, param_end, value);
  836. return -1;
  837. }
  838. *amount = (Uint) tmp;
  839. return 0;
  840. }
  841. }
  842. static void
  843. handle_au_arg(struct au_init *auip,
  844. char* sub_param,
  845. char** argv,
  846. int* ip)
  847. {
  848. char *param = argv[*ip]+1;
  849. switch (sub_param[0]) {
  850. case 'a':
  851. if(has_prefix("asbcst", sub_param)) {
  852. auip->init.util.asbcst = get_kb_value(sub_param + 6, argv, ip);
  853. }
  854. else if(has_prefix("as", sub_param)) {
  855. char *alg = get_value(sub_param + 2, argv, ip);
  856. if (strcmp("bf", alg) == 0) {
  857. auip->atype = BESTFIT;
  858. auip->init.bf.ao = 0;
  859. }
  860. else if (strcmp("aobf", alg) == 0) {
  861. auip->atype = BESTFIT;
  862. auip->init.bf.ao = 1;
  863. }
  864. else if (strcmp("gf", alg) == 0) {
  865. auip->atype = GOODFIT;
  866. }
  867. else if (strcmp("af", alg) == 0) {
  868. auip->atype = AFIT;
  869. }
  870. else {
  871. bad_value(param, sub_param + 1, alg);
  872. }
  873. }
  874. else
  875. goto bad_switch;
  876. break;
  877. case 'e':
  878. auip->enable = get_bool_value(sub_param+1, argv, ip);
  879. break;
  880. case 'l':
  881. if (has_prefix("lmbcs", sub_param)) {
  882. auip->default_.lmbcs = 0;
  883. auip->init.util.lmbcs = get_kb_value(sub_param + 5, argv, ip);
  884. }
  885. else
  886. goto bad_switch;
  887. break;
  888. case 'm':
  889. if (has_prefix("mbcgs", sub_param)) {
  890. auip->init.util.mbcgs = get_amount_value(sub_param + 5, argv, ip);
  891. }
  892. else if (has_prefix("mbsd", sub_param)) {
  893. auip->init.gf.mbsd = get_amount_value(sub_param + 4, argv, ip);
  894. if (auip->init.gf.mbsd < 1)
  895. auip->init.gf.mbsd = 1;
  896. }
  897. else if (has_prefix("mmbcs", sub_param)) {
  898. auip->default_.mmbcs = 0;
  899. auip->init.util.mmbcs = get_kb_value(sub_param + 5, argv, ip);
  900. }
  901. else if (has_prefix("mmmbc", sub_param)) {
  902. auip->default_.mmmbc = 0;
  903. auip->init.util.mmmbc = get_amount_value(sub_param + 5, argv, ip);
  904. }
  905. else if (has_prefix("mmsbc", sub_param)) {
  906. auip->init.util.mmsbc = get_amount_value(sub_param + 5, argv, ip);
  907. }
  908. else
  909. goto bad_switch;
  910. break;
  911. case 'r':
  912. if(has_prefix("rsbcmt", sub_param)) {
  913. auip->init.util.rsbcmt = get_amount_value(sub_param + 6, argv, ip);
  914. if (auip->init.util.rsbcmt > 100)
  915. auip->init.util.rsbcmt = 100;
  916. }
  917. else if(has_prefix("rsbcst", sub_param)) {
  918. auip->init.util.rsbcst = get_amount_value(sub_param + 6, argv, ip);
  919. if (auip->init.util.rsbcst > 100)
  920. auip->init.util.rsbcst = 100;
  921. }
  922. else if (has_prefix("rmbcmt", sub_param)) {
  923. auip->init.util.rmbcmt = get_amount_value(sub_param + 6, argv, ip);
  924. if (auip->init.util.rmbcmt > 100)
  925. auip->init.util.rmbcmt = 100;
  926. }
  927. else if (has_prefix("ramv", sub_param)) {
  928. auip->init.util.ramv = get_bool_value(sub_param + 4, argv, ip);
  929. }
  930. else
  931. goto bad_switch;
  932. break;
  933. case 's':
  934. if(has_prefix("sbct", sub_param)) {
  935. auip->init.util.sbct = get_kb_value(sub_param + 4, argv, ip);
  936. }
  937. else if (has_prefix("smbcs", sub_param)) {
  938. auip->default_.smbcs = 0;
  939. auip->init.util.smbcs = get_kb_value(sub_param + 5, argv, ip);
  940. }
  941. else
  942. goto bad_switch;
  943. break;
  944. case 't': {
  945. Uint no;
  946. int enable;
  947. int res = get_bool_or_possitive_amount_value(&enable,
  948. &no,
  949. sub_param+1,
  950. argv,
  951. ip);
  952. if (res > 0)
  953. auip->thr_spec = enable ? 1 : 0;
  954. else if (res == 0) {
  955. int allocs = (int) no;
  956. if (allocs < 0)
  957. allocs = INT_MIN;
  958. else {
  959. allocs *= -1;
  960. }
  961. auip->thr_spec = allocs;
  962. }
  963. break;
  964. }
  965. default:
  966. bad_switch:
  967. bad_param(param, sub_param);
  968. }
  969. }
  970. static void
  971. handle_args(int *argc, char **argv, erts_alc_hndl_args_init_t *init)
  972. {
  973. struct au_init *aui[] = {
  974. &init->binary_alloc,
  975. &init->std_alloc,
  976. &init->ets_alloc,
  977. &init->eheap_alloc,
  978. &init->ll_alloc,
  979. &init->driver_alloc,
  980. &init->sl_alloc,
  981. &init->temp_alloc
  982. };
  983. int aui_sz = (int) sizeof(aui)/sizeof(aui[0]);
  984. char *arg;
  985. char *rest;
  986. int i, j;
  987. i = 1;
  988. ASSERT(argc && argv && init);
  989. while (i < *argc) {
  990. if(argv[i][0] == '-') {
  991. char *param = argv[i]+1;
  992. switch (argv[i][1]) {
  993. case 'M':
  994. switch (argv[i][2]) {
  995. case 'B':
  996. handle_au_arg(&init->binary_alloc, &argv[i][3], argv, &i);
  997. break;
  998. case 'D':
  999. handle_au_arg(&init->std_alloc, &argv[i][3], argv, &i);
  1000. break;
  1001. case 'E':
  1002. handle_au_arg(&init->ets_alloc, &argv[i][3], argv, &i);
  1003. break;
  1004. case 'F': /* fix_alloc */
  1005. if (has_prefix("e", param+2)) {
  1006. arg = get_value(param+3, argv, &i);
  1007. if (strcmp("true", arg) != 0)
  1008. bad_value(param, param+3, arg);
  1009. }
  1010. else
  1011. bad_param(param, param+2);
  1012. break;
  1013. case 'H':
  1014. handle_au_arg(&init->eheap_alloc, &argv[i][3], argv, &i);
  1015. break;
  1016. case 'L':
  1017. handle_au_arg(&init->ll_alloc, &argv[i][3], argv, &i);
  1018. break;
  1019. case 'M':
  1020. if (has_prefix("amcbf", argv[i]+3)) {
  1021. #if HAVE_ERTS_MSEG
  1022. init->mseg.amcbf =
  1023. #endif
  1024. get_kb_value(argv[i]+8, argv, &i);
  1025. }
  1026. else if (has_prefix("rmcbf", argv[i]+3)) {
  1027. #if HAVE_ERTS_MSEG
  1028. init->mseg.rmcbf =
  1029. #endif
  1030. get_amount_value(argv[i]+8, argv, &i);
  1031. }
  1032. else if (has_prefix("mcs", argv[i]+3)) {
  1033. #if HAVE_ERTS_MSEG
  1034. init->mseg.mcs =
  1035. #endif
  1036. get_amount_value(argv[i]+6, argv, &i);
  1037. }
  1038. else if (has_prefix("cci", argv[i]+3)) {
  1039. #if HAVE_ERTS_MSEG
  1040. init->mseg.cci =
  1041. #endif
  1042. get_amount_value(argv[i]+6, argv, &i);
  1043. }
  1044. else {
  1045. bad_param(param, param+2);
  1046. }
  1047. break;
  1048. case 'R':
  1049. handle_au_arg(&init->driver_alloc, &argv[i][3], argv, &i);
  1050. break;
  1051. case 'S':
  1052. handle_au_arg(&init->sl_alloc, &argv[i][3], argv, &i);
  1053. break;
  1054. case 'T':
  1055. handle_au_arg(&init->temp_alloc, &argv[i][3], argv, &i);
  1056. break;
  1057. case 'Y': { /* sys_alloc */
  1058. if (has_prefix("tt", param+2)) {
  1059. /* set trim threshold */
  1060. arg = get_value(param+4, argv, &i);
  1061. errno = 0;
  1062. init->trim_threshold = (int) strtol(arg, &rest, 10);
  1063. if (errno != 0
  1064. || rest == arg
  1065. || init->trim_threshold < 0
  1066. || (INT_MAX/1024) < init->trim_threshold) {
  1067. bad_value(param, param+4, arg);
  1068. }
  1069. VERBOSE(DEBUG_SYSTEM,
  1070. ("using trim threshold: %d\n",
  1071. init->trim_threshold));
  1072. init->trim_threshold *= 1024;
  1073. }
  1074. else if (has_prefix("tp", param+2)) {
  1075. /* set top pad */
  1076. arg = get_value(param+4, argv, &i);
  1077. errno = 0;
  1078. init->top_pad = (int) strtol(arg, &rest, 10);
  1079. if (errno != 0
  1080. || rest == arg
  1081. || init->top_pad < 0
  1082. || (INT_MAX/1024) < init->top_pad) {
  1083. bad_value(param, param+4, arg);
  1084. }
  1085. VERBOSE(DEBUG_SYSTEM,
  1086. ("using top pad: %d\n",init->top_pad));
  1087. init->top_pad *= 1024;
  1088. }
  1089. else if (has_prefix("m", param+2)) {
  1090. /* Has been handled by erlexec */
  1091. (void) get_value(param+3, argv, &i);
  1092. }
  1093. else if (has_prefix("e", param+2)) {
  1094. arg = get_value(param+3, argv, &i);
  1095. if (strcmp("true", arg) != 0)
  1096. bad_value(param, param+3, arg);
  1097. }
  1098. else
  1099. bad_param(param, param+2);
  1100. break;
  1101. }
  1102. case 'e':
  1103. switch (argv[i][3]) {
  1104. case 'a': {
  1105. int a;
  1106. arg = get_value(argv[i]+4, argv, &i);
  1107. if (strcmp("min", arg) == 0) {
  1108. for (a = 0; a < aui_sz; a++)
  1109. aui[a]->enable = 0;
  1110. }
  1111. else if (strcmp("max", arg) == 0) {
  1112. for (a = 0; a < aui_sz; a++)
  1113. aui[a]->enable = 1;
  1114. }
  1115. else if (strcmp("config", arg) == 0) {
  1116. init->erts_alloc_config = 1;
  1117. }
  1118. else if (strcmp("r9c", arg) == 0
  1119. || strcmp("r10b", arg) == 0
  1120. || strcmp("r11b", arg) == 0) {
  1121. set_default_sl_alloc_opts(&init->sl_alloc);
  1122. set_default_std_alloc_opts(&init->std_alloc);
  1123. set_default_ll_alloc_opts(&init->ll_alloc);
  1124. set_default_temp_alloc_opts(&init->temp_alloc);
  1125. set_default_eheap_alloc_opts(&init->eheap_alloc);
  1126. set_default_binary_alloc_opts(&init->binary_alloc);
  1127. set_default_ets_alloc_opts(&init->ets_alloc);
  1128. set_default_driver_alloc_opts(&init->driver_alloc);
  1129. init->driver_alloc.enable = 0;
  1130. if (strcmp("r9c", arg) == 0) {
  1131. init->sl_alloc.enable = 0;
  1132. init->std_alloc.enable = 0;
  1133. init->binary_alloc.enable = 0;
  1134. init->ets_alloc.enable = 0;
  1135. }
  1136. for (a = 0; a < aui_sz; a++) {
  1137. aui[a]->thr_spec = 0;
  1138. aui[a]->init.util.ramv = 0;
  1139. aui[a]->init.util.mmmbc = 10;
  1140. aui[a]->init.util.lmbcs = 5*1024*1024;
  1141. }
  1142. }
  1143. else {
  1144. bad_param(param, param+3);
  1145. }
  1146. break;
  1147. }
  1148. default:
  1149. bad_param(param, param+1);
  1150. }
  1151. break;
  1152. case 'i':
  1153. switch (argv[i][3]) {
  1154. case 's':
  1155. arg = get_value(argv[i]+4, argv, &i);
  1156. if (strcmp("true", arg) == 0)
  1157. init->instr.stat = 1;
  1158. else if (strcmp("false", arg) == 0)
  1159. init->instr.stat = 0;
  1160. else
  1161. bad_value(param, param+3, arg);
  1162. break;
  1163. case 'm':
  1164. arg = get_value(argv[i]+4, argv, &i);
  1165. if (strcmp("true", arg) == 0)
  1166. init->instr.map = 1;
  1167. else if (strcmp("false", arg) == 0)
  1168. init->instr.map = 0;
  1169. else
  1170. bad_value(param, param+3, arg);
  1171. break;
  1172. case 't':
  1173. init->instr.mtrace = get_value(argv[i]+4, argv, &i);
  1174. break;
  1175. default:
  1176. bad_param(param, param+2);
  1177. }
  1178. break;
  1179. case 'u':
  1180. if (has_prefix("ycs", argv[i]+3)) {
  1181. init->alloc_util.ycs
  1182. = get_kb_value(argv[i]+6, argv, &i);
  1183. }
  1184. else if (has_prefix("mmc", argv[i]+3)) {
  1185. init->alloc_util.mmc
  1186. = get_amount_value(argv[i]+6, argv, &i);
  1187. }
  1188. else {
  1189. int a;
  1190. int start = i;
  1191. char *param = argv[i];
  1192. char *val = i+1 < *argc ? argv[i+1] : NULL;
  1193. for (a = 0; a < aui_sz; a++) {
  1194. if (a > 0) {
  1195. ASSERT(i == start || i == start+1);
  1196. argv[start] = param;
  1197. if (i != start)
  1198. argv[start + 1] = val;
  1199. i = start;
  1200. }
  1201. handle_au_arg(aui[a], &argv[i][3], argv, &i);
  1202. }
  1203. }
  1204. break;
  1205. default:
  1206. bad_param(param, param+1);
  1207. }
  1208. break;
  1209. case '-':
  1210. if (argv[i][2] == '\0') {
  1211. /* End of system flags reached */
  1212. if (init->instr.mtrace
  1213. /* || init->instr.stat
  1214. || init->instr.map */) {
  1215. while (i < *argc) {
  1216. if(strcmp(argv[i], "-sname") == 0
  1217. || strcmp(argv[i], "-name") == 0) {
  1218. if (i + 1 <*argc) {
  1219. init->instr.nodename = argv[i+1];
  1220. break;
  1221. }
  1222. }
  1223. i++;
  1224. }
  1225. }
  1226. goto args_parsed;
  1227. }
  1228. break;
  1229. default:
  1230. break;
  1231. }
  1232. }
  1233. i++;
  1234. }
  1235. args_parsed:
  1236. /* Handled arguments have been marked with NULL. Slide arguments
  1237. not handled towards the beginning of argv. */
  1238. for (i = 0, j = 0; i < *argc; i++) {
  1239. if (argv[i])
  1240. argv[j++] = argv[i];
  1241. }
  1242. *argc = j;
  1243. #if HALFWORD_HEAP
  1244. /* If halfword heap, silently ignore any disabling of internal
  1245. allocators */
  1246. for (i = 0; i < aui_sz; ++i)
  1247. aui[i]->enable = 1;
  1248. #endif
  1249. }
  1250. static char *type_no_str(ErtsAlcType_t n)
  1251. {
  1252. #if ERTS_ALC_N_MIN != 0
  1253. if (n < ERTS_ALC_N_MIN)
  1254. return NULL;
  1255. #endif
  1256. if (n > ERTS_ALC_N_MAX)
  1257. return NULL;
  1258. return (char *) ERTS_ALC_N2TD(n);
  1259. }
  1260. #define type_str(T) type_no_str(ERTS_ALC_T2N((T)))
  1261. erts_tsd_key_t thr_ix_key;
  1262. erts_spinlock_t alloc_thr_ix_lock;
  1263. int last_thr_ix;
  1264. int first_dyn_thr_ix;
  1265. static void
  1266. init_thr_ix(int static_ixs)
  1267. {
  1268. erts_tsd_key_create(&thr_ix_key);
  1269. erts_spinlock_init(&alloc_thr_ix_lock, "alloc_thr_ix_lock");
  1270. last_thr_ix = -4711;
  1271. first_dyn_thr_ix = static_ixs+1;
  1272. }
  1273. int
  1274. erts_alc_get_thr_ix(void)
  1275. {
  1276. int ix = (int)(long) erts_tsd_get(thr_ix_key);
  1277. if (ix == 0) {
  1278. erts_spin_lock(&alloc_thr_ix_lock);
  1279. last_thr_ix++;
  1280. if (last_thr_ix < 0)
  1281. last_thr_ix = first_dyn_thr_ix;
  1282. ix = last_thr_ix;
  1283. erts_spin_unlock(&alloc_thr_ix_lock);
  1284. erts_tsd_set(thr_ix_key, (void *)(long) ix);
  1285. }
  1286. ASSERT(ix > 0);
  1287. return ix;
  1288. }
  1289. void erts_alloc_reg_scheduler_id(Uint id)
  1290. {
  1291. int ix = (int) id;
  1292. ASSERT(0 < ix && ix <= first_dyn_thr_ix);
  1293. ASSERT(0 == (int) (long) erts_tsd_get(thr_ix_key));
  1294. erts_tsd_set(thr_ix_key, (void *)(long) ix);
  1295. }
  1296. __decl_noreturn void
  1297. erts_alc_fatal_error(int error, int func, ErtsAlcType_t n, ...)
  1298. {
  1299. char buf[10];
  1300. char *t_str;
  1301. char *allctr_str;
  1302. ASSERT(n >= ERTS_ALC_N_MIN);
  1303. ASSERT(n <= ERTS_ALC_N_MAX);
  1304. if (n < ERTS_ALC_N_MIN || ERTS_ALC_N_MAX < n)
  1305. allctr_str = "UNKNOWN";
  1306. else {
  1307. ErtsAlcType_t a = ERTS_ALC_T2A(ERTS_ALC_N2T(n));
  1308. if (erts_allctrs_info[a].enabled)
  1309. allctr_str = (char *) ERTS_ALC_A2AD(a);
  1310. else
  1311. allctr_str = (char *) ERTS_ALC_A2AD(ERTS_ALC_A_SYSTEM);
  1312. }
  1313. t_str = type_no_str(n);
  1314. if (!t_str) {
  1315. sprintf(buf, "%d", (int) n);
  1316. t_str = buf;
  1317. }
  1318. switch (error) {
  1319. case ERTS_ALC_E_NOTSUP: {
  1320. char *op_str;
  1321. switch (func) {
  1322. case ERTS_ALC_O_ALLOC: op_str = "alloc"; break;
  1323. case ERTS_ALC_O_REALLOC: op_str = "realloc"; break;
  1324. case ERTS_ALC_O_FREE: op_str = "free"; break;
  1325. default: op_str = "UNKNOWN"; break;
  1326. }
  1327. erl_exit(ERTS_ABORT_EXIT,
  1328. "%s: %s operation not supported (memory type: \"%s\")\n",
  1329. allctr_str, op_str, t_str);
  1330. break;
  1331. }
  1332. case ERTS_ALC_E_NOMEM: {
  1333. Uint size;
  1334. va_list argp;
  1335. char *op = func == ERTS_ALC_O_REALLOC ? "reallocate" : "allocate";
  1336. va_start(argp, n);
  1337. size = va_arg(argp, Uint);
  1338. va_end(argp);
  1339. erl_exit(1,
  1340. "%s: Cannot %s %lu bytes of memory (of type \"%s\").\n",
  1341. allctr_str, op, size, t_str);
  1342. break;
  1343. }
  1344. case ERTS_ALC_E_NOALLCTR:
  1345. erl_exit(ERTS_ABORT_EXIT,
  1346. "erts_alloc: Unknown allocator type: %d\n",
  1347. ERTS_ALC_T2A(ERTS_ALC_N2T(n)));
  1348. break;
  1349. default:
  1350. erl_exit(ERTS_ABORT_EXIT, "erts_alloc: Unknown error: %d\n", error);
  1351. break;
  1352. }
  1353. }
  1354. __decl_noreturn void
  1355. erts_alloc_enomem(ErtsAlcType_t type, Uint size)
  1356. {
  1357. erts_alloc_n_enomem(ERTS_ALC_T2N(type), size);
  1358. }
  1359. __decl_noreturn void
  1360. erts_alloc_n_enomem(ErtsAlcType_t n, Uint size)
  1361. {
  1362. erts_alc_fatal_error(ERTS_ALC_E_NOMEM, ERTS_ALC_O_ALLOC, n, size);
  1363. }
  1364. __decl_noreturn void
  1365. erts_realloc_enomem(ErtsAlcType_t type, void *ptr, Uint size)
  1366. {
  1367. erts_realloc_n_enomem(ERTS_ALC_T2N(type), ptr, size);
  1368. }
  1369. __decl_noreturn void
  1370. erts_realloc_n_enomem(ErtsAlcType_t n, void *ptr, Uint size)
  1371. {
  1372. erts_alc_fatal_error(ERTS_ALC_E_NOMEM, ERTS_ALC_O_REALLOC, n, size);
  1373. }
  1374. static ERTS_INLINE Uint
  1375. alcu_size(ErtsAlcType_t ai)
  1376. {
  1377. Uint res = 0;
  1378. ASSERT(erts_allctrs_info[ai].enabled);
  1379. ASSERT(erts_allctrs_info[ai].alloc_util);
  1380. if (!erts_allctrs_info[ai].thr_spec) {
  1381. Allctr_t *allctr = erts_allctrs_info[ai].extra;
  1382. AllctrSize_t asize;
  1383. erts_alcu_current_size(allctr, &asize);
  1384. res += asize.blocks;
  1385. }
  1386. else {
  1387. ErtsAllocatorThrSpec_t *tspec = &erts_allctr_thr_spec[ai];
  1388. int i;
  1389. ASSERT(tspec->all_thr_safe);
  1390. ASSERT(tspec->enabled);
  1391. for (i = tspec->size - 1; i >= 0; i--) {
  1392. Allctr_t *allctr = tspec->allctr[i];
  1393. AllctrSize_t asize;
  1394. if (allctr) {
  1395. erts_alcu_current_size(allctr, &asize);
  1396. res += asize.blocks;
  1397. }
  1398. }
  1399. }
  1400. return res;
  1401. }
  1402. Eterm
  1403. erts_memory(int *print_to_p, void *print_to_arg, void *proc, Eterm earg)
  1404. {
  1405. #define ERTS_MEM_NEED_ALL_ALCU (!erts_instr_stat && want_tot_or_sys)
  1406. ErtsFixInfo efi;
  1407. struct {
  1408. int total;
  1409. int processes;
  1410. int processes_used;
  1411. int system;
  1412. int atom;
  1413. int atom_used;
  1414. int binary;
  1415. int code;
  1416. int ets;
  1417. int maximum;
  1418. } want = {0};
  1419. struct {
  1420. Uint total;
  1421. Uint processes;
  1422. Uint processes_used;
  1423. Uint system;
  1424. Uint atom;
  1425. Uint atom_used;
  1426. Uint binary;
  1427. Uint code;
  1428. Uint ets;
  1429. Uint maximum;
  1430. } size = {0};
  1431. Eterm atoms[sizeof(size)/sizeof(Uint)];
  1432. Uint *uintps[sizeof(size)/sizeof(Uint)];
  1433. Eterm euints[sizeof(size)/sizeof(Uint)];
  1434. int want_tot_or_sys;
  1435. int length;
  1436. Eterm res = THE_NON_VALUE;
  1437. ErtsAlcType_t ai;
  1438. int only_one_value = 0;
  1439. /* Figure out whats wanted... */
  1440. length = 0;
  1441. if (is_non_value(earg)) { /* i.e. wants all */
  1442. want.total = 1;
  1443. atoms[length] = am_total;
  1444. uintps[length++] = &size.total;
  1445. want.processes = 1;
  1446. atoms[length] = am_processes;
  1447. uintps[length++] = &size.processes;
  1448. want.processes_used = 1;
  1449. atoms[length] = am_processes_used;
  1450. uintps[length++] = &size.processes_used;
  1451. want.system = 1;
  1452. atoms[length] = am_system;
  1453. uintps[length++] = &size.system;
  1454. want.atom = 1;
  1455. atoms[length] = am_atom;
  1456. uintps[length++] = &size.atom;
  1457. want.atom_used = 1;
  1458. atoms[length] = am_atom_used;
  1459. uintps[length++] = &size.atom_used;
  1460. want.binary = 1;
  1461. atoms[length] = am_binary;
  1462. uintps[length++] = &size.binary;
  1463. want.code = 1;
  1464. atoms[length] = am_code;
  1465. uintps[length++] = &size.code;
  1466. want.ets = 1;
  1467. atoms[length] = am_ets;
  1468. uintps[length++] = &size.ets;
  1469. want.maximum = erts_instr_stat;
  1470. if (want.maximum) {
  1471. atoms[length] = am_maximum;
  1472. uintps[length++] = &size.maximum;
  1473. }
  1474. }
  1475. else {
  1476. DeclareTmpHeapNoproc(tmp_heap,2);
  1477. Eterm wanted_list;
  1478. if (is_nil(earg))
  1479. return NIL;
  1480. UseTmpHeapNoproc(2);
  1481. if (is_not_atom(earg))
  1482. wanted_list = earg;
  1483. else {
  1484. wanted_list = CONS(&tmp_heap[0], earg, NIL);
  1485. only_one_value = 1;
  1486. }
  1487. while (is_list(wanted_list)) {
  1488. switch (CAR(list_val(wanted_list))) {
  1489. case am_total:
  1490. if (!want.total) {
  1491. want.total = 1;
  1492. atoms[length] = am_total;
  1493. uintps[length++] = &size.total;
  1494. }
  1495. break;
  1496. case am_processes:
  1497. if (!want.processes) {
  1498. want.processes = 1;
  1499. atoms[length] = am_processes;
  1500. uintps[length++] = &size.processes;
  1501. }
  1502. break;
  1503. case am_processes_used:
  1504. if (!want.processes_used) {
  1505. want.processes_used = 1;
  1506. atoms[length] = am_processes_used;
  1507. uintps[length++] = &size.processes_used;
  1508. }
  1509. break;
  1510. case am_system:
  1511. if (!want.system) {
  1512. want.system = 1;
  1513. atoms[length] = am_system;
  1514. uintps[length++] = &size.system;
  1515. }
  1516. break;
  1517. case am_atom:
  1518. if (!want.atom) {
  1519. want.atom = 1;
  1520. atoms[length] = am_atom;
  1521. uintps[length++] = &size.atom;
  1522. }
  1523. break;
  1524. case am_atom_used:
  1525. if (!want.atom_used) {
  1526. want.atom_used = 1;
  1527. atoms[length] = am_atom_used;
  1528. uintps[length++] = &size.atom_used;
  1529. }
  1530. break;
  1531. case am_binary:
  1532. if (!want.binary) {
  1533. want.binary = 1;
  1534. atoms[length] = am_binary;
  1535. uintps[length++] = &size.binary;
  1536. }
  1537. break;
  1538. case am_code:
  1539. if (!want.code) {
  1540. want.code = 1;
  1541. atoms[length] = am_code;
  1542. uintps[length++] = &size.code;
  1543. }
  1544. break;
  1545. case am_ets:
  1546. if (!want.ets) {
  1547. want.ets = 1;
  1548. atoms[length] = am_ets;
  1549. uintps[length++] = &size.ets;
  1550. }
  1551. break;
  1552. case am_maximum:
  1553. if (erts_instr_stat) {
  1554. if (!want.maximum) {
  1555. want.maximum = 1;
  1556. atoms[length] = am_maximum;
  1557. uintps[length++] = &size.maximum;
  1558. }
  1559. } else {
  1560. UnUseTmpHeapNoproc(2);
  1561. return am_badarg;
  1562. }
  1563. break;
  1564. default:
  1565. UnUseTmpHeapNoproc(2);
  1566. return am_badarg;
  1567. }
  1568. wanted_list = CDR(list_val(wanted_list));
  1569. }
  1570. UnUseTmpHeapNoproc(2);
  1571. if (is_not_nil(wanted_list))
  1572. return am_badarg;
  1573. }
  1574. /* All alloc_util allocators *have* to be enabled */
  1575. for (ai = ERTS_ALC_A_MIN; ai <= ERTS_ALC_A_MAX; ai++) {
  1576. switch (ai) {
  1577. case ERTS_ALC_A_SYSTEM:
  1578. case ERTS_ALC_A_FIXED_SIZE:
  1579. break;
  1580. default:
  1581. if (!erts_allctrs_info[ai].enabled
  1582. || !erts_allctrs_info[ai].alloc_util) {
  1583. return am_notsup;
  1584. }
  1585. break;
  1586. }
  1587. }
  1588. ASSERT(length <= sizeof(atoms)/sizeof(Eterm));
  1589. ASSERT(length <= sizeof(euints)/sizeof(Eterm));
  1590. ASSERT(length <= sizeof(uintps)/sizeof(Uint));
  1591. if (proc) {
  1592. ERTS_SMP_LC_ASSERT(ERTS_PROC_LOCK_MAIN
  1593. == erts_proc_lc_my_proc_locks(proc));
  1594. /* We'll need locks early in the lock order */
  1595. erts_smp_proc_unlock(proc, ERTS_PROC_LOCK_MAIN);
  1596. }
  1597. /* Calculate values needed... */
  1598. want_tot_or_sys = want.total || want.system;
  1599. if (ERTS_MEM_NEED_ALL_ALCU) {
  1600. size.total = 0;
  1601. for (ai = ERTS_ALC_A_MIN; ai <= ERTS_ALC_A_MAX; ai++) {
  1602. if (erts_allctrs_info[ai].alloc_util) {
  1603. Uint *save;
  1604. Uint asz;
  1605. switch (ai) {
  1606. case ERTS_ALC_A_TEMPORARY:
  1607. /*
  1608. * Often not thread safe and usually never
  1609. * contain any allocated memory.
  1610. */
  1611. continue;
  1612. case ERTS_ALC_A_EHEAP:
  1613. save = &size.processes;
  1614. break;
  1615. case ERTS_ALC_A_ETS:
  1616. save = &size.ets;
  1617. break;
  1618. case ERTS_ALC_A_BINARY:
  1619. save = &size.binary;
  1620. break;
  1621. default:
  1622. save = NULL;
  1623. break;
  1624. }
  1625. asz = alcu_size(ai);
  1626. if (save)
  1627. *save = asz;
  1628. size.total += asz;
  1629. }
  1630. }
  1631. }
  1632. if (want_tot_or_sys || want.processes || want.processes_used) {
  1633. Uint tmp;
  1634. if (ERTS_MEM_NEED_ALL_ALCU)
  1635. tmp = size.processes;
  1636. else
  1637. tmp = alcu_size(ERTS_ALC_A_EHEAP);
  1638. tmp += erts_max_processes*sizeof(Process*);
  1639. #ifdef HYBRID
  1640. tmp += erts_max_processes*sizeof(Process*);
  1641. #endif
  1642. tmp += erts_bif_timer_memory_size();
  1643. tmp += erts_tot_link_lh_size();
  1644. size.processes = size.processes_used = tmp;
  1645. erts_fix_info(ERTS_ALC_T_NLINK_SH, &efi);
  1646. size.processes += efi.total;
  1647. size.processes_used += efi.used;
  1648. erts_fix_info(ERTS_ALC_T_MONITOR_SH, &efi);
  1649. size.processes += efi.total;
  1650. size.processes_used += efi.used;
  1651. erts_fix_info(ERTS_ALC_T_PROC, &efi);
  1652. size.processes += efi.total;
  1653. size.processes_used += efi.used;
  1654. erts_fix_info(ERTS_ALC_T_REG_PROC, &efi);
  1655. size.processes += efi.total;
  1656. size.processes_used += efi.used;
  1657. }
  1658. if (want.atom || want.atom_used) {
  1659. Uint reserved_atom_space, atom_space;
  1660. erts_atom_get_text_space_sizes(&reserved_atom_space, &atom_space);
  1661. size.atom = size.atom_used = atom_table_sz();
  1662. erts_fix_info(ERTS_ALC_T_ATOM, &efi);
  1663. if (want.atom) {
  1664. size.atom += reserved_atom_space;
  1665. size.atom += efi.total;
  1666. }
  1667. if (want.atom_used) {
  1668. size.atom_used += atom_space;
  1669. size.atom_used += efi.used;
  1670. }
  1671. }
  1672. if (!ERTS_MEM_NEED_ALL_ALCU && want.binary)
  1673. size.binary = alcu_size(ERTS_ALC_A_BINARY);
  1674. if (want.code) {
  1675. size.code = module_table_sz();
  1676. erts_fix_info(ERTS_ALC_T_MODULE, &efi);
  1677. size.code += efi.used;
  1678. size.code += export_table_sz();
  1679. erts_fix_info(ERTS_ALC_T_EXPORT, &efi);
  1680. size.code += efi.used;
  1681. size.code += erts_fun_table_sz();
  1682. erts_fix_info(ERTS_ALC_T_FUN_ENTRY, &efi);
  1683. size.code += efi.used;
  1684. size.code += allocated_modules*sizeof(Range);
  1685. size.code += erts_total_code_size;
  1686. }
  1687. if (want.ets) {
  1688. if (!ERTS_MEM_NEED_ALL_ALCU)
  1689. size.ets = alcu_size(ERTS_ALC_A_ETS);
  1690. size.ets += erts_get_ets_misc_mem_size();
  1691. }
  1692. if (erts_instr_stat && (want_tot_or_sys || want.maximum)) {
  1693. if (want_tot_or_sys) {
  1694. size.total = erts_instr_get_total();
  1695. size.system = size.total - size.processes;
  1696. }
  1697. size.maximum = erts_instr_get_max_total();
  1698. }
  1699. else if (want_tot_or_sys) {
  1700. size.system = size.total - size.processes;
  1701. }
  1702. if (print_to_p) {
  1703. int i;
  1704. int to = *print_to_p;
  1705. void *arg = print_to_arg;
  1706. /* Print result... */
  1707. erts_print(to, arg, "=memory\n");
  1708. for (i = 0; i < length; i++)
  1709. erts_print(to, arg, "%T: %bpu\n", atoms[i], *uintps[i]);
  1710. }
  1711. if (proc) {
  1712. /* Build erlang term result... */
  1713. Uint *hp;
  1714. Uint hsz;
  1715. erts_smp_proc_lock(proc, ERTS_PROC_LOCK_MAIN);
  1716. if (only_one_value) {
  1717. ASSERT(length == 1);
  1718. hsz = 0;
  1719. erts_bld_uint(NULL, &hsz, *uintps[0]);
  1720. hp = hsz ? HAlloc((Process *) proc, hsz) : NULL;
  1721. res = erts_bld_uint(&hp, NULL, *uintps[0]);
  1722. }
  1723. else {
  1724. Uint **hpp = NULL;
  1725. Uint *hszp = &hsz;
  1726. hsz = 0;
  1727. while (1) {
  1728. int i;
  1729. for (i = 0; i < length; i++)
  1730. euints[i] = erts_bld_uint(hpp, hszp, *uintps[i]);
  1731. res = erts_bld_2tup_list(hpp, hszp, length, atoms, euints);
  1732. if (hpp)
  1733. break;
  1734. hp = HAlloc((Process *) proc, hsz);
  1735. hpp = &hp;
  1736. hszp = NULL;
  1737. }
  1738. }
  1739. }
  1740. return res;
  1741. #undef ERTS_MEM_NEED_ALL_ALCU
  1742. }
  1743. struct aa_values {
  1744. Uint arity;
  1745. const char *name;
  1746. Uint ui[2];
  1747. };
  1748. Eterm
  1749. erts_allocated_areas(int *print_to_p, void *print_to_arg, void *proc)
  1750. {
  1751. #define MAX_AA_VALUES \
  1752. (20 + (ERTS_ALC_N_MAX_A_FIXED_SIZE - ERTS_ALC_N_MIN_A_FIXED_SIZE + 1))
  1753. struct aa_values values[MAX_AA_VALUES];
  1754. Eterm res = THE_NON_VALUE;
  1755. int i, length;
  1756. ErtsFixInfo efi;
  1757. Uint reserved_atom_space, atom_space;
  1758. if (proc) {
  1759. ERTS_SMP_LC_ASSERT(ERTS_PROC_LOCK_MAIN
  1760. == erts_proc_lc_my_proc_locks(proc));
  1761. /* We'll need locks early in the lock order */
  1762. erts_smp_proc_unlock(proc, ERTS_PROC_LOCK_MAIN);
  1763. }
  1764. i = 0;
  1765. if (erts_instr_stat) {
  1766. values[i].arity = 2;
  1767. values[i].name = "total";
  1768. values[i].ui[0] = erts_instr_get_total();
  1769. i++;
  1770. values[i].arity = 2;
  1771. values[i].name = "maximum";
  1772. values[i].ui[0] = erts_instr_get_max_total();
  1773. i++;
  1774. }
  1775. values[i].arity = 2;
  1776. values[i].name = "sys_misc";
  1777. values[i].ui[0] = erts_sys_misc_mem_sz();
  1778. i++;
  1779. values[i].arity = 2;
  1780. values[i].name = "static";
  1781. values[i].ui[0] =
  1782. erts_max_ports*sizeof(Port) /* Port table */
  1783. + erts_timer_wheel_memory_size() /* Timer wheel */
  1784. #ifdef SYS_TMP_BUF_SIZE
  1785. + SYS_TMP_BUF_SIZE /* tmp_buf in sys on vxworks & ose */
  1786. #endif
  1787. ;
  1788. i++;
  1789. erts_atom_get_text_space_sizes(&reserved_atom_space, &atom_space);
  1790. values[i].arity = 3;
  1791. values[i].name = "atom_space";
  1792. values[i].ui[0] = reserved_atom_space;
  1793. values[i].ui[1] = atom_space;
  1794. i++;
  1795. values[i].arity = 2;
  1796. values[i].name = "atom_table";
  1797. values[i].ui[0] = atom_table_sz();
  1798. i++;
  1799. values[i].arity = 2;
  1800. values[i].name = "module_table";
  1801. values[i].ui[0] = module_table_sz();
  1802. i++;
  1803. values[i].arity = 2;
  1804. values[i].name = "export_table";
  1805. values[i].ui[0] = export_table_sz();
  1806. i++;
  1807. values[i].arity = 2;
  1808. values[i].name = "register_table";
  1809. values[i].ui[0] = process_reg_sz();
  1810. i++;
  1811. values[i].arity = 2;
  1812. values[i].name = "fun_table";
  1813. values[i].ui[0] = erts_fun_table_sz();
  1814. i++;
  1815. values[i].arity = 2;
  1816. values[i].name = "module_refs";
  1817. values[i].ui[0] = allocated_modules*sizeof(Range);
  1818. i++;
  1819. values[i].arity = 2;
  1820. values[i].name = "loaded_code";
  1821. values[i].ui[0] = erts_total_code_size;
  1822. i++;
  1823. values[i].arity = 2;
  1824. values[i].name = "dist_table";
  1825. values[i].ui[0] = erts_dist_table_size();
  1826. i++;
  1827. values[i].arity = 2;
  1828. values[i].name = "node_table";
  1829. values[i].ui[0] = erts_node_table_size();
  1830. i++;
  1831. values[i].arity = 2;
  1832. values[i].name = "bits_bufs_size";
  1833. values[i].ui[0] = erts_bits_bufs_size();
  1834. i++;
  1835. values[i].arity = 2;
  1836. values[i].name = "bif_timer";
  1837. values[i].ui[0] = erts_bif_timer_memory_size();
  1838. i++;
  1839. values[i].arity = 2;
  1840. values[i].name = "link_lh";
  1841. values[i].ui[0] = erts_tot_link_lh_size();
  1842. i++;
  1843. {
  1844. Uint n;
  1845. for (n = ERTS_ALC_N_MIN_A_FIXED_SIZE;
  1846. n <= ERTS_ALC_N_MAX_A_FIXED_SIZE;
  1847. n++) {
  1848. erts_fix_info(ERTS_ALC_N2T(n), &efi);
  1849. values[i].arity = 3;
  1850. values[i].name = ERTS_ALC_N2TD(n);
  1851. values[i].ui[0] = efi.total;
  1852. values[i].ui[1] = efi.used;
  1853. i++;
  1854. }
  1855. }
  1856. length = i;
  1857. ASSERT(length <= MAX_AA_VALUES);
  1858. if (print_to_p) {
  1859. /* Print result... */
  1860. int to = *print_to_p;
  1861. void *arg = print_to_arg;
  1862. erts_print(to, arg, "=allocated_areas\n");
  1863. for (i = 0; i < length; i++) {
  1864. switch (values[i].arity) {
  1865. case 2:
  1866. erts_print(to, arg, "%s: %bpu\n",
  1867. values[i].name, values[i].ui[0]);
  1868. break;
  1869. case 3:
  1870. erts_print(to, arg, "%s: %bpu %bpu\n",
  1871. values[i].name, values[i].ui[0], values[i].ui[1]);
  1872. break;
  1873. default:
  1874. erts_print(to, arg, "ERROR: internal_error\n");
  1875. ASSERT(0);
  1876. return am_internal_error;
  1877. }
  1878. }
  1879. }
  1880. if (proc) {
  1881. /* Build erlang term result... */
  1882. Eterm tuples[MAX_AA_VALUES];
  1883. Uint *hp;
  1884. Uint **hpp;
  1885. Uint hsz;
  1886. Uint *hszp;
  1887. erts_smp_proc_lock(proc, ERTS_PROC_LOCK_MAIN);
  1888. hpp = NULL;
  1889. hsz = 0;
  1890. hszp = &hsz;
  1891. while (1) {
  1892. int i;
  1893. for (i = 0; i < length; i++) {
  1894. Eterm atom;
  1895. if (hpp)
  1896. atom = am_atom_put(values[i].name,
  1897. (int) strlen(values[i].name));
  1898. else
  1899. atom = am_true;
  1900. switch (values[i].arity) {
  1901. case 2:
  1902. tuples[i] = erts_bld_tuple(hpp, hszp, 2,
  1903. atom,
  1904. erts_bld_uint(hpp, hszp,
  1905. values[i].ui[0]));
  1906. break;
  1907. case 3:
  1908. tuples[i] = erts_bld_tuple(hpp, hszp, 3,
  1909. atom,
  1910. erts_bld_uint(hpp, hszp,
  1911. values[i].ui[0]),
  1912. erts_bld_uint(hpp, hszp,
  1913. values[i].ui[1]));
  1914. break;
  1915. default:
  1916. ASSERT(0);
  1917. return am_internal_error;
  1918. }
  1919. }
  1920. res = erts_bld_list(hpp, hszp, length, tuples);
  1921. if (hpp)
  1922. break;
  1923. hp = HAlloc((Process *) proc, hsz);
  1924. hpp = &hp;
  1925. hszp = NULL;
  1926. }
  1927. }
  1928. return res;
  1929. #undef MAX_AA_VALUES
  1930. }
  1931. Eterm
  1932. erts_alloc_util_allocators(void *proc)
  1933. {
  1934. Eterm res;
  1935. Uint *hp;
  1936. Uint sz;
  1937. int i;
  1938. /*
  1939. * Currently all allocators except sys_alloc and fix_alloc are
  1940. * alloc_util allocators.
  1941. */
  1942. sz = ((ERTS_ALC_A_MAX + 1 - ERTS_ALC_A_MIN) - 2)*2;
  1943. ASSERT(sz > 0);
  1944. hp = HAlloc((Process *) proc, sz);
  1945. res = NIL;
  1946. for (i = ERTS_ALC_A_MAX; i >= ERTS_ALC_A_MIN; i--) {
  1947. switch (i) {
  1948. case ERTS_ALC_A_SYSTEM:
  1949. case ERTS_ALC_A_FIXED_SIZE:
  1950. break;
  1951. default: {
  1952. char *alc_str = (char *) ERTS_ALC_A2AD(i);
  1953. Eterm alc = am_atom_put(alc_str, sys_strlen(alc_str));
  1954. res = CONS(hp, alc, res);
  1955. hp += 2;
  1956. break;
  1957. }
  1958. }
  1959. }
  1960. return res;
  1961. }
  1962. Eterm
  1963. erts_allocator_info_term(void *proc, Eterm which_alloc, int only_sz)
  1964. {
  1965. #define ERTS_AIT_RET(R) \
  1966. do { res = (R); goto done; } while (0)
  1967. #define ERTS_AIT_HALLOC(P, S) \
  1968. do { hp = HAlloc((P), (S)); hp_end = hp + (S); } while (0)
  1969. ErtsAlcType_t i;
  1970. Uint sz = 0;
  1971. Uint *hp = NULL;
  1972. Uint *hp_end = NULL;
  1973. Eterm res = am_undefined;
  1974. if (is_not_atom(which_alloc))
  1975. goto done;
  1976. for (i = ERTS_ALC_A_MIN; i <= ERTS_ALC_A_MAX; i++) {
  1977. if (erts_is_atom_str((char *) ERTS_ALC_A2AD(i), which_alloc)) {
  1978. if (!erts_allctrs_info[i].enabled)
  1979. ERTS_AIT_RET(am_false);
  1980. else {
  1981. if (erts_allctrs_info[i].alloc_util) {
  1982. Eterm ires, tmp;
  1983. Eterm **hpp;
  1984. Uint *szp;
  1985. Eterm (*info_func)(Allctr_t *,
  1986. int,
  1987. int *,
  1988. void *,
  1989. Uint **,
  1990. Uint *);
  1991. info_func = (only_sz
  1992. ? erts_alcu_sz_info
  1993. : erts_alcu_info);
  1994. if (erts_allctrs_info[i].thr_spec) {
  1995. ErtsAllocatorThrSpec_t *tspec = &erts_allctr_thr_spec[i];
  1996. int j;
  1997. int block_system = !tspec->all_thr_safe;
  1998. if (block_system) {
  1999. erts_smp_proc_unlock(proc, ERTS_PROC_LOCK_MAIN);
  2000. erts_smp_block_system(0);
  2001. }
  2002. ASSERT(tspec->enabled);
  2003. szp = &sz;
  2004. hpp = NULL;
  2005. while (1) {
  2006. ires = NIL;
  2007. for (j = tspec->size - 1; j >= 0; j--) {
  2008. Allctr_t *allctr = tspec->allctr[j];
  2009. if (allctr) {
  2010. tmp = erts_bld_tuple(hpp,
  2011. szp,
  2012. 3,
  2013. erts_bld_atom(hpp,
  2014. szp,
  2015. "instance"),
  2016. make_small((Uint) j),
  2017. (*info_func)(allctr,
  2018. hpp != NULL,
  2019. NULL,
  2020. NULL,
  2021. hpp,
  2022. szp));
  2023. ires = erts_bld_cons(hpp, szp, tmp, ires);
  2024. }
  2025. }
  2026. if (hpp)
  2027. break;
  2028. ERTS_AIT_HALLOC((Process *) proc, sz);
  2029. hpp = &hp;
  2030. szp = NULL;
  2031. }
  2032. if (block_system) {
  2033. erts_smp_release_system();
  2034. erts_smp_proc_lock(proc, ERTS_PROC_LOCK_MAIN);
  2035. }
  2036. }
  2037. else {
  2038. Allctr_t *allctr = erts_allctrs_info[i].extra;
  2039. szp = &sz;
  2040. hpp = NULL;
  2041. while (1) {
  2042. ires = NIL;
  2043. tmp = erts_bld_tuple(hpp,
  2044. szp,
  2045. 3,
  2046. erts_bld_atom(hpp,
  2047. szp,
  2048. "instance"),
  2049. make_small((Uint) 0),
  2050. (*info_func)(allctr,
  2051. hpp != NULL,
  2052. NULL,
  2053. NULL,
  2054. hpp,
  2055. szp));
  2056. ires = erts_bld_cons(hpp, szp, tmp, ires);
  2057. if (hpp)
  2058. break;
  2059. ERTS_AIT_HALLOC((Process *) proc, sz);
  2060. hpp = &hp;
  2061. szp = NULL;
  2062. }
  2063. }
  2064. ERTS_AIT_RET(ires);
  2065. }
  2066. else {
  2067. Eterm *szp, **hpp;
  2068. switch (i) {
  2069. case ERTS_ALC_A_SYSTEM: {
  2070. SysAllocStat sas;
  2071. Eterm opts_am;
  2072. Eterm opts;
  2073. Eterm as[4]; /* Ok even if !HEAP_ON_C_STACK, not really heap data on stack */
  2074. Eterm ts[4]; /* Ok even if !HEAP_ON_C_STACK, not really heap data on stack */
  2075. int l;
  2076. if (only_sz)
  2077. ERTS_AIT_RET(NIL);
  2078. sys_alloc_stat(&sas);
  2079. opts_am = am_atom_put("options", 7);
  2080. szp = &sz;
  2081. hpp = NULL;
  2082. restart_sys_alloc:
  2083. l = 0;
  2084. as[l] = am_atom_put("e", 1);
  2085. ts[l++] = am_true;
  2086. as[l] = am_atom_put("m", 1);
  2087. ts[l++] = am_atom_put("libc", 4);
  2088. if(sas.trim_threshold >= 0) {
  2089. as[l] = am_atom_put("tt", 2);
  2090. ts[l++] = erts_bld_uint(hpp, szp,
  2091. (Uint) sas.trim_threshold);
  2092. }
  2093. if(sas.top_pad >= 0) {
  2094. as[l] = am_atom_put("tp", 2);
  2095. ts[l++] = erts_bld_uint(hpp, szp, (Uint) sas.top_pad);
  2096. }
  2097. opts = erts_bld_2tup_list(hpp, szp, l, as, ts);
  2098. res = erts_bld_2tup_list(hpp, szp, 1, &opts_am, &opts);
  2099. if (szp) {
  2100. ERTS_AIT_HALLOC((Process *) proc, sz);
  2101. szp = NULL;
  2102. hpp = &hp;
  2103. goto restart_sys_alloc;
  2104. }
  2105. ERTS_AIT_RET(res);
  2106. }
  2107. case ERTS_ALC_A_FIXED_SIZE: {
  2108. ErtsAlcType_t n;
  2109. Eterm as[2], vs[2];
  2110. if (only_sz)
  2111. ERTS_AIT_RET(NIL);
  2112. as[0] = am_atom_put("options", 7);
  2113. as[1] = am_atom_put("pools", 5);
  2114. szp = &sz;
  2115. hpp = NULL;
  2116. restart_fix_alloc:
  2117. vs[0] = erts_bld_cons(hpp, szp,
  2118. erts_bld_tuple(hpp, szp, 2,
  2119. am_atom_put("e",
  2120. 1),
  2121. am_true),
  2122. NIL);
  2123. vs[1] = NIL;
  2124. for (n = ERTS_ALC_N_MIN_A_FIXED_SIZE;
  2125. n <= ERTS_ALC_N_MAX_A_FIXED_SIZE;
  2126. n++) {
  2127. ErtsFixInfo efi;
  2128. erts_fix_info(ERTS_ALC_N2T(n), &efi);
  2129. vs[1] = erts_bld_cons(
  2130. hpp, szp,
  2131. erts_bld_tuple(
  2132. hpp, szp, 3,
  2133. am_atom_put((char *) ERTS_ALC_N2TD(n),
  2134. strlen(ERTS_ALC_N2TD(n))),
  2135. erts_bld_uint(hpp, szp, efi.total),
  2136. erts_bld_uint(hpp, szp, efi.used)),
  2137. vs[1]);
  2138. }
  2139. res = erts_bld_2tup_list(hpp, szp, 2, as, vs);
  2140. if (szp) {
  2141. ERTS_AIT_HALLOC((Process *) proc, sz);
  2142. szp = NULL;
  2143. hpp = &hp;
  2144. goto restart_fix_alloc;
  2145. }
  2146. ERTS_AIT_RET(res);
  2147. }
  2148. default:
  2149. ASSERT(0);
  2150. goto done;
  2151. }
  2152. }
  2153. }
  2154. }
  2155. }
  2156. if (ERTS_IS_ATOM_STR("mseg_alloc", which_alloc)) {
  2157. #if HAVE_ERTS_MSEG
  2158. if (only_sz)
  2159. ERTS_AIT_RET(NIL);
  2160. erts_mseg_info(NULL, NULL, 0, NULL, &sz);
  2161. if (sz)
  2162. ERTS_AIT_HALLOC((Process *) proc, sz);
  2163. ERTS_AIT_RET(erts_mseg_info(NULL, NULL, 1, &hp, NULL));
  2164. #else
  2165. ERTS_AIT_RET(am_false);
  2166. #endif
  2167. }
  2168. else if (ERTS_IS_ATOM_STR("alloc_util", which_alloc)) {
  2169. if (only_sz)
  2170. ERTS_AIT_RET(NIL);
  2171. erts_alcu_au_info_options(NULL, NULL, NULL, &sz);
  2172. if (sz)
  2173. ERTS_AIT_HALLOC((Process *) proc, sz);
  2174. ERTS_AIT_RET(erts_alcu_au_info_options(NULL, NULL, &hp, NULL));
  2175. }
  2176. done:
  2177. if (hp) {
  2178. ASSERT(hp_end >= hp);
  2179. HRelease((Process *) proc, hp_end, hp);
  2180. }
  2181. return res;
  2182. #undef ERTS_AIT_RET
  2183. #undef ERTS_AIT_HALLOC
  2184. }
  2185. void
  2186. erts_allocator_info(int to, void *arg)
  2187. {
  2188. ErtsAlcType_t a;
  2189. ERTS_SMP_LC_ASSERT(erts_smp_is_system_blocked(0)
  2190. || (ERTS_IS_CRASH_DUMPING
  2191. && erts_smp_is_system_blocked(ERTS_BS_FLG_ALLOW_GC)));
  2192. for (a = ERTS_ALC_A_MIN; a <= ERTS_ALC_A_MAX; a++) {
  2193. int ai;
  2194. for (ai = 0; ai == 0 || ai < erts_allctrs_info[a].thr_spec; ai++) {
  2195. if (erts_allctrs_info[a].thr_spec) {
  2196. if (!erts_allctr_thr_spec[a].allctr[ai])
  2197. continue;
  2198. erts_print(to, arg, "=allocator:%s[%d]\n",
  2199. ERTS_ALC_A2AD(a), ai);
  2200. }
  2201. else {
  2202. erts_print(to, arg, "=allocator:%s\n", ERTS_ALC_A2AD(a));
  2203. }
  2204. if (!erts_allctrs_info[a].enabled)
  2205. erts_print(to, arg, "option e: false\n");
  2206. else {
  2207. if (erts_allctrs_info[a].alloc_util) {
  2208. void *as;
  2209. if (!erts_allctrs_info[a].thr_spec)
  2210. as = erts_allctrs_info[a].extra;
  2211. else {
  2212. ASSERT(erts_allctr_thr_spec[a].enabled);
  2213. as = erts_allctr_thr_spec[a].allctr[ai];
  2214. }
  2215. /* Binary alloc has its own thread safety... */
  2216. erts_alcu_info(as, 0, &to, arg, NULL, NULL);
  2217. }
  2218. else {
  2219. switch (a) {
  2220. case ERTS_ALC_A_SYSTEM: {
  2221. SysAllocStat sas;
  2222. erts_print(to, arg, "option e: true\n");
  2223. erts_print(to, arg, "option m: libc\n");
  2224. sys_alloc_stat(&sas);
  2225. if(sas.trim_threshold >= 0)
  2226. erts_print(to, arg, "option tt: %d\n", sas.trim_threshold);
  2227. if(sas.top_pad >= 0)
  2228. erts_print(to, arg, "option tp: %d\n", sas.top_pad);
  2229. break;
  2230. }
  2231. case ERTS_ALC_A_FIXED_SIZE: {
  2232. ErtsAlcType_t n;
  2233. erts_print(to, arg, "option e: true\n");
  2234. for (n = ERTS_ALC_N_MIN_A_FIXED_SIZE;
  2235. n <= ERTS_ALC_N_MAX_A_FIXED_SIZE;
  2236. n++) {
  2237. ErtsFixInfo efi;
  2238. erts_fix_info(ERTS_ALC_N2T(n), &efi);
  2239. erts_print(to, arg, "%s: %lu %lu\n",
  2240. ERTS_ALC_N2TD(n),
  2241. efi.total,
  2242. efi.used);
  2243. }
  2244. break;
  2245. }
  2246. default:
  2247. ASSERT(0);
  2248. break;
  2249. }
  2250. }
  2251. }
  2252. }
  2253. }
  2254. #if HAVE_ERTS_MSEG
  2255. erts_print(to, arg, "=allocator:mseg_alloc\n");
  2256. erts_mseg_info(&to, arg, 0, NULL, NULL);
  2257. #endif
  2258. erts_print(to, arg, "=allocator:alloc_util\n");
  2259. erts_alcu_au_info_options(&to, arg, NULL, NULL);
  2260. erts_print(to, arg, "=allocator:instr\n");
  2261. erts_print(to, arg, "option m: %s\n",
  2262. erts_instr_memory_map ? "true" : "false");
  2263. erts_print(to, arg, "option s: %s\n",
  2264. erts_instr_stat ? "true" : "false");
  2265. erts_print(to, arg, "option t: %s\n",
  2266. erts_mtrace_enabled ? "true" : "false");
  2267. }
  2268. Eterm
  2269. erts_allocator_options(void *proc)
  2270. {
  2271. #if HAVE_ERTS_MSEG
  2272. int use_mseg = 0;
  2273. #endif
  2274. Uint sz, *szp, *hp, **hpp;
  2275. Eterm res, features, settings;
  2276. Eterm atoms[ERTS_ALC_A_MAX-ERTS_ALC_A_MIN+5];
  2277. Uint terms[ERTS_ALC_A_MAX-ERTS_ALC_A_MIN+5];
  2278. int a, length;
  2279. SysAllocStat sas;
  2280. Uint *endp = NULL;
  2281. sys_alloc_stat(&sas);
  2282. /* First find out the heap size needed ... */
  2283. hpp = NULL;
  2284. szp = &sz;
  2285. sz = 0;
  2286. bld_term:
  2287. length = 0;
  2288. features = NIL;
  2289. settings = NIL;
  2290. for (a = ERTS_ALC_A_MIN; a <= ERTS_ALC_A_MAX; a++) {
  2291. Eterm tmp = NIL;
  2292. atoms[length] = am_atom_put((char *) ERTS_ALC_A2AD(a),
  2293. strlen(ERTS_ALC_A2AD(a)));
  2294. if (erts_allctrs_info[a].enabled) {
  2295. if (erts_allctrs_info[a].alloc_util) {
  2296. Allctr_t *allctr;
  2297. #if HAVE_ERTS_MSEG
  2298. use_mseg++;
  2299. #endif
  2300. if (erts_allctr_thr_spec[a].enabled)
  2301. allctr = erts_allctr_thr_spec[a].allctr[1];
  2302. else
  2303. allctr = erts_allctrs_info[a].extra;
  2304. tmp = erts_alcu_info_options(allctr, NULL, NULL, hpp, szp);
  2305. }
  2306. else {
  2307. int l = 0;
  2308. Eterm as[4];
  2309. Eterm ts[4];
  2310. as[l] = am_atom_put("e", 1);
  2311. ts[l++] = am_true;
  2312. switch (a) {
  2313. case ERTS_ALC_A_SYSTEM:
  2314. as[l] = am_atom_put("m", 1);
  2315. ts[l++] = am_atom_put("libc", 4);
  2316. if(sas.trim_threshold >= 0) {
  2317. as[l] = am_atom_put("tt", 2);
  2318. ts[l++] = erts_bld_uint(hpp, szp,
  2319. (Uint) sas.trim_threshold);
  2320. }
  2321. if(sas.top_pad >= 0) {
  2322. as[l] = am_atom_put("tp", 2);
  2323. ts[l++] = erts_bld_uint(hpp, szp, (Uint) sas.top_pad);
  2324. }
  2325. break;
  2326. default:
  2327. break;
  2328. }
  2329. tmp = erts_bld_2tup_list(hpp, szp, l, as, ts);
  2330. }
  2331. }
  2332. else {
  2333. Eterm atom = am_atom_put("e", 1);
  2334. Eterm term = am_false;
  2335. tmp = erts_bld_2tup_list(hpp, szp, 1, &atom, &term);
  2336. }
  2337. terms[length++] = tmp;
  2338. }
  2339. #if HAVE_ERTS_MSEG
  2340. if (use_mseg) {
  2341. atoms[length] = am_atom_put("mseg_alloc", 10);
  2342. terms[length++] = erts_mseg_info_options(NULL, NULL, hpp, szp);
  2343. }
  2344. #endif
  2345. atoms[length] = am_atom_put("alloc_util", 10);
  2346. terms[length++] = erts_alcu_au_info_options(NULL, NULL, hpp, szp);
  2347. {
  2348. Eterm o[3], v[3];
  2349. o[0] = am_atom_put("m", 1);
  2350. v[0] = erts_instr_memory_map ? am_true : am_false;
  2351. o[1] = am_atom_put("s", 1);
  2352. v[1] = erts_instr_stat ? am_true : am_false;
  2353. o[2] = am_atom_put("t", 1);
  2354. v[2] = erts_mtrace_enabled ? am_true : am_false;
  2355. atoms[length] = am_atom_put("instr", 5);
  2356. terms[length++] = erts_bld_2tup_list(hpp, szp, 3, o, v);
  2357. }
  2358. settings = erts_bld_2tup_list(hpp, szp, length, atoms, terms);
  2359. length = 0;
  2360. for (a = ERTS_ALC_A_MIN; a <= ERTS_ALC_A_MAX; a++) {
  2361. if (erts_allctrs_info[a].enabled) {
  2362. terms[length++] = am_atom_put((char *) ERTS_ALC_A2AD(a),
  2363. strlen(ERTS_ALC_A2AD(a)));
  2364. }
  2365. }
  2366. #if HAVE_ERTS_MSEG
  2367. if (use_mseg)
  2368. terms[length++] = am_atom_put("mseg_alloc", 10);
  2369. #endif
  2370. features = length ? erts_bld_list(hpp, szp, length, terms) : NIL;
  2371. #if defined(__GLIBC__)
  2372. {
  2373. Eterm AM_glibc = am_atom_put("glibc", 5);
  2374. Eterm version;
  2375. version = erts_bld_cons(hpp,
  2376. szp,
  2377. make_small(__GLIBC__),
  2378. #ifdef __GLIBC_MINOR__
  2379. erts_bld_cons(hpp,
  2380. szp,
  2381. make_small(__GLIBC_MINOR__),
  2382. NIL)
  2383. #else
  2384. NIL
  2385. #endif
  2386. );
  2387. res = erts_bld_tuple(hpp, szp, 4,
  2388. AM_glibc, version, features, settings);
  2389. }
  2390. #else /* unknown allocator */
  2391. res = erts_bld_tuple(hpp, szp, 4,
  2392. am_undefined, NIL, features, settings);
  2393. #endif
  2394. if (szp) {
  2395. /* ... and then build the term */
  2396. hp = HAlloc((Process *) proc, sz);
  2397. endp = hp + sz;
  2398. hpp = &hp;
  2399. szp = NULL;
  2400. goto bld_term;
  2401. }
  2402. ASSERT(endp >= hp);
  2403. HRelease((Process *) proc, endp, hp);
  2404. return res;
  2405. }
  2406. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  2407. * Deprecated functions *
  2408. * *
  2409. * These functions are still defined since "non-OTP linked in drivers" may *
  2410. * contain (illegal) calls to them. *
  2411. \* */
  2412. /* --- DO *NOT* USE THESE FUNCTIONS --- */
  2413. void *sys_alloc(Uint sz)
  2414. { return erts_alloc_fnf(ERTS_ALC_T_UNDEF, sz); }
  2415. void *sys_realloc(void *ptr, Uint sz)
  2416. { return erts_realloc_fnf(ERTS_ALC_T_UNDEF, ptr, sz); }
  2417. void sys_free(void *ptr)
  2418. { erts_free(ERTS_ALC_T_UNDEF, ptr); }
  2419. void *safe_alloc(Uint sz)
  2420. { return erts_alloc(ERTS_ALC_T_UNDEF, sz); }
  2421. void *safe_realloc(void *ptr, Uint sz)
  2422. { return erts_realloc(ERTS_ALC_T_UNDEF, ptr, sz); }
  2423. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  2424. * NOTE: erts_alc_test() is only supposed to be used for testing. *
  2425. * *
  2426. * Keep alloc_SUITE_data/allocator_test.h updated if changes are made *
  2427. * to erts_alc_test() *
  2428. \* */
  2429. #define ERTS_ALC_TEST_ABORT erl_exit(ERTS_ABORT_EXIT, "%s:%d: Internal error\n")
  2430. unsigned long erts_alc_test(unsigned long op,
  2431. unsigned long a1,
  2432. unsigned long a2,
  2433. unsigned long a3)
  2434. {
  2435. switch (op >> 8) {
  2436. case 0x0: return erts_alcu_test(op, a1, a2);
  2437. case 0x1: return erts_gfalc_test(op, a1, a2);
  2438. case 0x2: return erts_bfalc_test(op, a1, a2);
  2439. case 0x3: return erts_afalc_test(op, a1, a2);
  2440. case 0x4: return erts_mseg_test(op, a1, a2, a3);
  2441. case 0xf:
  2442. switch (op) {
  2443. case 0xf00:
  2444. #ifdef USE_THREADS
  2445. if (((Allctr_t *) a1)->thread_safe)
  2446. return (unsigned long) erts_alcu_alloc_ts(ERTS_ALC_T_UNDEF,
  2447. (void *) a1,
  2448. (Uint) a2);
  2449. else
  2450. #endif
  2451. return (unsigned long) erts_alcu_alloc(ERTS_ALC_T_UNDEF,
  2452. (void *) a1,
  2453. (Uint) a2);
  2454. case 0xf01:
  2455. #ifdef USE_THREADS
  2456. if (((Allctr_t *) a1)->thread_safe)
  2457. return (unsigned long) erts_alcu_realloc_ts(ERTS_ALC_T_UNDEF,
  2458. (void *) a1,
  2459. (void *) a2,
  2460. (Uint) a3);
  2461. else
  2462. #endif
  2463. return (unsigned long) erts_alcu_realloc(ERTS_ALC_T_UNDEF,
  2464. (void *) a1,
  2465. (void *) a2,
  2466. (Uint) a3);
  2467. case 0xf02:
  2468. #ifdef USE_THREADS
  2469. if (((Allctr_t *) a1)->thread_safe)
  2470. erts_alcu_free_ts(ERTS_ALC_T_UNDEF, (void *) a1, (void *) a2);
  2471. else
  2472. #endif
  2473. erts_alcu_free(ERTS_ALC_T_UNDEF, (void *) a1, (void *) a2);
  2474. return 0;
  2475. case 0xf03: {
  2476. Allctr_t *allctr;
  2477. struct au_init init;
  2478. SET_DEFAULT_ALLOC_OPTS(&init);
  2479. init.enable = 1;
  2480. init.atype = GOODFIT;
  2481. init.init.util.name_prefix = (char *) a1;
  2482. init.init.util.ts = a2 ? 1 : 0;
  2483. if ((char **) a3) {
  2484. char **argv = (char **) a3;
  2485. int i = 0;
  2486. while (argv[i]) {
  2487. if (argv[i][0] == '-' && argv[i][1] == 't')
  2488. handle_au_arg(&init, &argv[i][2], argv, &i);
  2489. else
  2490. return (unsigned long) NULL;
  2491. i++;
  2492. }
  2493. }
  2494. switch (init.atype) {
  2495. case GOODFIT:
  2496. allctr = erts_gfalc_start((GFAllctr_t *)
  2497. erts_alloc(ERTS_ALC_T_UNDEF,
  2498. sizeof(GFAllctr_t)),
  2499. &init.init.gf,
  2500. &init.init.util);
  2501. break;
  2502. case BESTFIT:
  2503. allctr = erts_bfalc_start((BFAllctr_t *)
  2504. erts_alloc(ERTS_ALC_T_UNDEF,
  2505. sizeof(BFAllctr_t)),
  2506. &init.init.bf,
  2507. &init.init.util);
  2508. break;
  2509. case AFIT:
  2510. allctr = erts_afalc_start((AFAllctr_t *)
  2511. erts_alloc(ERTS_ALC_T_UNDEF,
  2512. sizeof(AFAllctr_t)),
  2513. &init.init.af,
  2514. &init.init.util);
  2515. break;
  2516. default:
  2517. ASSERT(0);
  2518. allctr = NULL;
  2519. break;
  2520. }
  2521. return (unsigned long) allctr;
  2522. }
  2523. case 0xf04:
  2524. erts_alcu_stop((Allctr_t *) a1);
  2525. erts_free(ERTS_ALC_T_UNDEF, (void *) a1);
  2526. break;
  2527. #ifdef USE_THREADS
  2528. case 0xf05: return (unsigned long) 1;
  2529. case 0xf06: return (unsigned long) ((Allctr_t *) a1)->thread_safe;
  2530. #ifdef ETHR_NO_FORKSAFETY
  2531. case 0xf07: return (unsigned long) 0;
  2532. #else
  2533. case 0xf07: return (unsigned long) ((Allctr_t *) a1)->thread_safe;
  2534. #endif
  2535. case 0xf08: {
  2536. ethr_mutex *mtx = erts_alloc(ERTS_ALC_T_UNDEF, sizeof(ethr_mutex));
  2537. if (ethr_mutex_init(mtx) != 0)
  2538. ERTS_ALC_TEST_ABORT;
  2539. return (unsigned long) mtx;
  2540. }
  2541. case 0xf09: {
  2542. ethr_mutex *mtx = (ethr_mutex *) a1;
  2543. if (ethr_mutex_destroy(mtx) != 0)
  2544. ERTS_ALC_TEST_ABORT;
  2545. erts_free(ERTS_ALC_T_UNDEF, (void *) mtx);
  2546. break;
  2547. }
  2548. case 0xf0a:
  2549. ethr_mutex_lock((ethr_mutex *) a1);
  2550. break;
  2551. case 0xf0b:
  2552. ethr_mutex_unlock((ethr_mutex *) a1);
  2553. break;
  2554. case 0xf0c: {
  2555. ethr_cond *cnd = erts_alloc(ERTS_ALC_T_UNDEF, sizeof(ethr_cond));
  2556. if (ethr_cond_init(cnd) != 0)
  2557. ERTS_ALC_TEST_ABORT;
  2558. return (unsigned long) cnd;
  2559. }
  2560. case 0xf0d: {
  2561. ethr_cond *cnd = (ethr_cond *) a1;
  2562. if (ethr_cond_destroy(cnd) != 0)
  2563. ERTS_ALC_TEST_ABORT;
  2564. erts_free(ERTS_ALC_T_UNDEF, (void *) cnd);
  2565. break;
  2566. }
  2567. case 0xf0e:
  2568. ethr_cond_broadcast((ethr_cond *) a1);
  2569. break;
  2570. case 0xf0f: {
  2571. int res;
  2572. do {
  2573. res = ethr_cond_wait((ethr_cond *) a1, (ethr_mutex *) a2);
  2574. } while (res == EINTR);
  2575. break;
  2576. }
  2577. case 0xf10: {
  2578. ethr_tid *tid = erts_alloc(ERTS_ALC_T_UNDEF, sizeof(ethr_tid));
  2579. if (ethr_thr_create(tid,
  2580. (void * (*)(void *)) a1,
  2581. (void *) a2,
  2582. NULL) != 0)
  2583. ERTS_ALC_TEST_ABORT;
  2584. return (unsigned long) tid;
  2585. }
  2586. case 0xf11: {
  2587. ethr_tid *tid = (ethr_tid *) a1;
  2588. if (ethr_thr_join(*tid, NULL) != 0)
  2589. ERTS_ALC_TEST_ABORT;
  2590. erts_free(ERTS_ALC_T_UNDEF, (void *) tid);
  2591. break;
  2592. }
  2593. case 0xf12:
  2594. ethr_thr_exit((void *) a1);
  2595. ERTS_ALC_TEST_ABORT;
  2596. break;
  2597. #endif /* #ifdef USE_THREADS */
  2598. default:
  2599. break;
  2600. }
  2601. return (unsigned long) 0;
  2602. default:
  2603. break;
  2604. }
  2605. ASSERT(0);
  2606. return ~((unsigned long) 0);
  2607. }
  2608. #ifdef DEBUG
  2609. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  2610. * Debug stuff *
  2611. \* */
  2612. #if 0
  2613. #define PRINT_OPS
  2614. #else
  2615. #undef PRINT_OPS
  2616. #endif
  2617. #ifdef HARD_DEBUG
  2618. #define FENCE_SZ (4*sizeof(UWord))
  2619. #else
  2620. #define FENCE_SZ (3*sizeof(UWord))
  2621. #endif
  2622. #if defined(ARCH_64)
  2623. #define FENCE_PATTERN 0xABCDEF97ABCDEF97
  2624. #else
  2625. #define FENCE_PATTERN 0xABCDEF97
  2626. #endif
  2627. #define TYPE_PATTERN_MASK ERTS_ALC_N_MASK
  2628. #define TYPE_PATTERN_SHIFT 16
  2629. #define FIXED_FENCE_PATTERN_MASK \
  2630. (~((UWord) (TYPE_PATTERN_MASK << TYPE_PATTERN_SHIFT)))
  2631. #define FIXED_FENCE_PATTERN \
  2632. (FENCE_PATTERN & FIXED_FENCE_PATTERN_MASK)
  2633. #define MK_PATTERN(T) \
  2634. (FIXED_FENCE_PATTERN | (((T) & TYPE_PATTERN_MASK) << TYPE_PATTERN_SHIFT))
  2635. #define GET_TYPE_OF_PATTERN(P) \
  2636. (((P) >> TYPE_PATTERN_SHIFT) & TYPE_PATTERN_MASK)
  2637. #ifdef HARD_DEBUG
  2638. #define ERL_ALC_HDBG_MAX_MBLK 100000
  2639. #define ERTS_ALC_O_CHECK -1
  2640. typedef struct hdbg_mblk_ hdbg_mblk;
  2641. struct hdbg_mblk_ {
  2642. hdbg_mblk *next;
  2643. hdbg_mblk *prev;
  2644. void *p;
  2645. Uint s;
  2646. ErtsAlcType_t n;
  2647. };
  2648. static hdbg_mblk hdbg_mblks[ERL_ALC_HDBG_MAX_MBLK];
  2649. static hdbg_mblk *free_hdbg_mblks;
  2650. static hdbg_mblk *used_hdbg_mblks;
  2651. static erts_mtx_t hdbg_mblk_mtx;
  2652. static void
  2653. hdbg_init(void)
  2654. {
  2655. int i;
  2656. for (i = 0; i < ERL_ALC_HDBG_MAX_MBLK-1; i++)
  2657. hdbg_mblks[i].next = &hdbg_mblks[i+1];
  2658. hdbg_mblks[ERL_ALC_HDBG_MAX_MBLK-1].next = NULL;
  2659. free_hdbg_mblks = &hdbg_mblks[0];
  2660. used_hdbg_mblks = NULL;
  2661. erts_mtx_init(&hdbg_mblk_mtx, "erts_alloc_hard_debug");
  2662. }
  2663. static void *check_memory_fence(void *ptr,
  2664. Uint *size,
  2665. ErtsAlcType_t n,
  2666. int func);
  2667. void erts_hdbg_chk_blks(void);
  2668. void
  2669. erts_hdbg_chk_blks(void)
  2670. {
  2671. hdbg_mblk *mblk;
  2672. erts_mtx_lock(&hdbg_mblk_mtx);
  2673. for (mblk = used_hdbg_mblks; mblk; mblk = mblk->next) {
  2674. Uint sz;
  2675. check_memory_fence(mblk->p, &sz, mblk->n, ERTS_ALC_O_CHECK);
  2676. ASSERT(sz == mblk->s);
  2677. }
  2678. erts_mtx_unlock(&hdbg_mblk_mtx);
  2679. }
  2680. static hdbg_mblk *
  2681. hdbg_alloc(void *p, Uint s, ErtsAlcType_t n)
  2682. {
  2683. hdbg_mblk *mblk;
  2684. erts_mtx_lock(&hdbg_mblk_mtx);
  2685. mblk = free_hdbg_mblks;
  2686. if (!mblk) {
  2687. erts_fprintf(stderr,
  2688. "Ran out of debug blocks; please increase "
  2689. "ERL_ALC_HDBG_MAX_MBLK=%d and recompile!\n",
  2690. ERL_ALC_HDBG_MAX_MBLK);
  2691. abort();
  2692. }
  2693. free_hdbg_mblks = mblk->next;
  2694. mblk->p = p;
  2695. mblk->s = s;
  2696. mblk->n = n;
  2697. mblk->next = used_hdbg_mblks;
  2698. mblk->prev = NULL;
  2699. if (used_hdbg_mblks)
  2700. used_hdbg_mblks->prev = mblk;
  2701. used_hdbg_mblks = mblk;
  2702. erts_mtx_unlock(&hdbg_mblk_mtx);
  2703. return (void *) mblk;
  2704. }
  2705. static void
  2706. hdbg_free(hdbg_mblk *mblk)
  2707. {
  2708. erts_mtx_lock(&hdbg_mblk_mtx);
  2709. if (mblk->next)
  2710. mblk->next->prev = mblk->prev;
  2711. if (mblk->prev)
  2712. mblk->prev->next = mblk->next;
  2713. else
  2714. used_hdbg_mblks = mblk->next;
  2715. mblk->next = free_hdbg_mblks;
  2716. free_hdbg_mblks = mblk;
  2717. erts_mtx_unlock(&hdbg_mblk_mtx);
  2718. }
  2719. #endif
  2720. #ifdef ERTS_ALLOC_UTIL_HARD_DEBUG
  2721. static void *check_memory_fence(void *ptr, Uint *size, ErtsAlcType_t n, int func);
  2722. void check_allocated_block( Uint type, void *blk)
  2723. {
  2724. Uint dummy;
  2725. check_memory_fence(blk, &dummy, ERTS_ALC_T2N(type), ERTS_ALC_O_FREE);
  2726. }
  2727. void check_allocators(void)
  2728. {
  2729. int i;
  2730. if (!erts_initialized)
  2731. return;
  2732. for (i = ERTS_ALC_A_MIN; i <= ERTS_ALC_A_MAX; ++i) {
  2733. if (erts_allctrs_info[i].alloc_util) {
  2734. ErtsAllocatorFunctions_t *real_af = (ErtsAllocatorFunctions_t *) erts_allctrs[i].extra;
  2735. Allctr_t *allctr = real_af->extra;
  2736. Carrier_t *ct;
  2737. #ifdef USE_THREADS
  2738. if (allctr->thread_safe)
  2739. erts_mtx_lock(&allctr->mutex);
  2740. #endif
  2741. if (allctr->check_mbc) {
  2742. for (ct = allctr->mbc_list.first; ct; ct = ct->next) {
  2743. fprintf(stderr,"Checking allocator %d\r\n",i);
  2744. allctr->check_mbc(allctr,ct);
  2745. }
  2746. }
  2747. #ifdef USE_THREADS
  2748. if (allctr->thread_safe)
  2749. erts_mtx_unlock(&allctr->mutex);
  2750. #endif
  2751. }
  2752. }
  2753. }
  2754. #endif
  2755. static void *
  2756. set_memory_fence(void *ptr, Uint sz, ErtsAlcType_t n)
  2757. {
  2758. UWord *ui_ptr;
  2759. UWord pattern;
  2760. #ifdef HARD_DEBUG
  2761. hdbg_mblk **mblkpp;
  2762. #endif
  2763. if (!ptr)
  2764. return NULL;
  2765. ui_ptr = (UWord *) ptr;
  2766. pattern = MK_PATTERN(n);
  2767. #ifdef HARD_DEBUG
  2768. mblkpp = (hdbg_mblk **) ui_ptr++;
  2769. #endif
  2770. *(ui_ptr++) = sz;
  2771. *(ui_ptr++) = pattern;
  2772. memcpy((void *) (((char *) ui_ptr)+sz), (void *) &pattern, sizeof(UWord));
  2773. #ifdef HARD_DEBUG
  2774. *mblkpp = hdbg_alloc((void *) ui_ptr, sz, n);
  2775. #endif
  2776. return (void *) ui_ptr;
  2777. }
  2778. static void *
  2779. check_memory_fence(void *ptr, Uint *size, ErtsAlcType_t n, int func)
  2780. {
  2781. Uint sz;
  2782. Uint found_type;
  2783. UWord pre_pattern;
  2784. UWord post_pattern;
  2785. UWord *ui_ptr;
  2786. #ifdef HARD_DEBUG
  2787. hdbg_mblk *mblk;
  2788. #endif
  2789. if (!ptr)
  2790. return NULL;
  2791. ui_ptr = (UWord *) ptr;
  2792. pre_pattern = *(--ui_ptr);
  2793. *size = sz = *(--ui_ptr);
  2794. #ifdef HARD_DEBUG
  2795. mblk = (hdbg_mblk *) *(--ui_ptr);
  2796. #endif
  2797. found_type = GET_TYPE_OF_PATTERN(pre_pattern);
  2798. if (pre_pattern != MK_PATTERN(n)) {
  2799. if ((FIXED_FENCE_PATTERN_MASK & pre_pattern) != FIXED_FENCE_PATTERN)
  2800. erl_exit(ERTS_ABORT_EXIT,
  2801. "ERROR: Fence at beginning of memory block (p=0x%u) "
  2802. "clobbered.\n",
  2803. (unsigned long) ptr);
  2804. }
  2805. memcpy((void *) &post_pattern, (void *) (((char *)ptr)+sz), sizeof(UWord));
  2806. if (post_pattern != MK_PATTERN(n)
  2807. || pre_pattern != post_pattern) {
  2808. char fbuf[10];
  2809. char obuf[10];
  2810. char *ftype;
  2811. char *otype;
  2812. char *op_str;
  2813. if ((FIXED_FENCE_PATTERN_MASK & post_pattern) != FIXED_FENCE_PATTERN)
  2814. erl_exit(ERTS_ABORT_EXIT,
  2815. "ERROR: Fence at end of memory block (p=0x%u, sz=%u) "
  2816. "clobbered.\n",
  2817. (unsigned long) ptr, (unsigned long) sz);
  2818. if (found_type != GET_TYPE_OF_PATTERN(post_pattern))
  2819. erl_exit(ERTS_ABORT_EXIT,
  2820. "ERROR: Fence around memory block (p=0x%u, sz=%u) "
  2821. "clobbered.\n",
  2822. (unsigned long) ptr, (unsigned long) sz);
  2823. ftype = type_no_str(found_type);
  2824. if (!ftype) {
  2825. sprintf(fbuf, "%d", (int) found_type);
  2826. ftype = fbuf;
  2827. }
  2828. otype = type_no_str(n);
  2829. if (!otype) {
  2830. sprintf(obuf, "%d", (int) n);
  2831. otype = obuf;
  2832. }
  2833. switch (func) {
  2834. case ERTS_ALC_O_ALLOC: op_str = "allocated"; break;
  2835. case ERTS_ALC_O_REALLOC: op_str = "reallocated"; break;
  2836. case ERTS_ALC_O_FREE: op_str = "freed"; break;
  2837. default: op_str = "???"; break;
  2838. }
  2839. erl_exit(ERTS_ABORT_EXIT,
  2840. "ERROR: Memory block (p=0x%u, sz=%u) allocated as type \"%s\","
  2841. " but %s as type \"%s\".\n",
  2842. (unsigned long) ptr, (unsigned long) sz, ftype, op_str, otype);
  2843. }
  2844. #ifdef HARD_DEBUG
  2845. switch (func) {
  2846. case ERTS_ALC_O_REALLOC:
  2847. case ERTS_ALC_O_FREE:
  2848. hdbg_free(mblk);
  2849. break;
  2850. default:
  2851. break;
  2852. }
  2853. #endif
  2854. return (void *) ui_ptr;
  2855. }
  2856. static ErtsAllocatorFunctions_t real_allctrs[ERTS_ALC_A_MAX+1];
  2857. static void *
  2858. debug_alloc(ErtsAlcType_t n, void *extra, Uint size)
  2859. {
  2860. ErtsAllocatorFunctions_t *real_af = (ErtsAllocatorFunctions_t *) extra;
  2861. Uint dsize;
  2862. void *res;
  2863. #ifdef HARD_DEBUG
  2864. erts_hdbg_chk_blks();
  2865. #endif
  2866. ASSERT(ERTS_ALC_N_MIN <= n && n <= ERTS_ALC_N_MAX);
  2867. dsize = size + FENCE_SZ;
  2868. res = (*real_af->alloc)(n, real_af->extra, dsize);
  2869. res = set_memory_fence(res, size, n);
  2870. #ifdef PRINT_OPS
  2871. fprintf(stderr, "0x%lx = alloc(%s, %lu)\r\n",
  2872. (Uint) res, ERTS_ALC_N2TD(n), size);
  2873. #endif
  2874. return res;
  2875. }
  2876. static void *
  2877. debug_realloc(ErtsAlcType_t n, void *extra, void *ptr, Uint size)
  2878. {
  2879. ErtsAllocatorFunctions_t *real_af = (ErtsAllocatorFunctions_t *) extra;
  2880. Uint dsize;
  2881. Uint old_size;
  2882. void *dptr;
  2883. void *res;
  2884. ASSERT(ERTS_ALC_N_MIN <= n && n <= ERTS_ALC_N_MAX);
  2885. dsize = size + FENCE_SZ;
  2886. dptr = check_memory_fence(ptr, &old_size, n, ERTS_ALC_O_REALLOC);
  2887. #ifdef HARD_DEBUG
  2888. erts_hdbg_chk_blks();
  2889. #endif
  2890. if (old_size > size)
  2891. sys_memset((void *) (((char *) ptr) + size),
  2892. 0xf,
  2893. sizeof(Uint) + old_size - size);
  2894. res = (*real_af->realloc)(n, real_af->extra, dptr, dsize);
  2895. res = set_memory_fence(res, size, n);
  2896. #ifdef PRINT_OPS
  2897. fprintf(stderr, "0x%lx = realloc(%s, 0x%lx, %lu)\r\n",
  2898. (Uint) res, ERTS_ALC_N2TD(n), (Uint) ptr, size);
  2899. #endif
  2900. return res;
  2901. }
  2902. static void
  2903. debug_free(ErtsAlcType_t n, void *extra, void *ptr)
  2904. {
  2905. ErtsAllocatorFunctions_t *real_af = (ErtsAllocatorFunctions_t *) extra;
  2906. void *dptr;
  2907. Uint size;
  2908. ASSERT(ERTS_ALC_N_MIN <= n && n <= ERTS_ALC_N_MAX);
  2909. dptr = check_memory_fence(ptr, &size, n, ERTS_ALC_O_FREE);
  2910. sys_memset((void *) dptr, n, size + FENCE_SZ);
  2911. (*real_af->free)(n, real_af->extra, dptr);
  2912. #ifdef PRINT_OPS
  2913. fprintf(stderr, "free(%s, 0x%lx)\r\n", ERTS_ALC_N2TD(n), (Uint) ptr);
  2914. #endif
  2915. #ifdef HARD_DEBUG
  2916. erts_hdbg_chk_blks();
  2917. #endif
  2918. }
  2919. static Uint
  2920. install_debug_functions(void)
  2921. {
  2922. int i;
  2923. ASSERT(sizeof(erts_allctrs) == sizeof(real_allctrs));
  2924. sys_memcpy((void *)real_allctrs,(void *)erts_allctrs,sizeof(erts_allctrs));
  2925. for (i = ERTS_ALC_A_MIN; i <= ERTS_ALC_A_MAX; i++) {
  2926. erts_allctrs[i].alloc = debug_alloc;
  2927. erts_allctrs[i].realloc = debug_realloc;
  2928. erts_allctrs[i].free = debug_free;
  2929. erts_allctrs[i].extra = (void *) &real_allctrs[i];
  2930. }
  2931. return FENCE_SZ;
  2932. }
  2933. #endif /* #ifdef DEBUG */