PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/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

Large files files are truncated, but you can click here to view the full file

  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].ar

Large files files are truncated, but you can click here to view the full file