PageRenderTime 75ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/erts/emulator/beam/erl_alloc.c

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