PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/erts/emulator/beam/erl_init.c

https://github.com/Bwooce/otp
C | 1614 lines | 1288 code | 199 blank | 127 comment | 239 complexity | 285460c15e857bfcc20e4f8e146e18e0 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-2-Clause
  1. /*
  2. * %CopyrightBegin%
  3. *
  4. * Copyright Ericsson AB 1997-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. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include "sys.h"
  23. #include <ctype.h>
  24. #include "erl_vm.h"
  25. #include "global.h"
  26. #include "erl_process.h"
  27. #include "error.h"
  28. #include "erl_version.h"
  29. #include "erl_db.h"
  30. #include "beam_bp.h"
  31. #include "erl_bits.h"
  32. #include "erl_binary.h"
  33. #include "dist.h"
  34. #include "erl_mseg.h"
  35. #include "erl_nmgc.h"
  36. #include "erl_threads.h"
  37. #include "erl_bif_timer.h"
  38. #include "erl_instrument.h"
  39. #include "erl_printf_term.h"
  40. #include "erl_misc_utils.h"
  41. #include "packet_parser.h"
  42. #include "erl_cpu_topology.h"
  43. #ifdef HIPE
  44. #include "hipe_mode_switch.h" /* for hipe_mode_switch_init() */
  45. #include "hipe_signal.h" /* for hipe_signal_init() */
  46. #endif
  47. #ifdef HAVE_SYS_RESOURCE_H
  48. # include <sys/resource.h>
  49. #endif
  50. /*
  51. * Note about VxWorks: All variables must be initialized by executable code,
  52. * not by an initializer. Otherwise a new instance of the emulator will
  53. * inherit previous values.
  54. */
  55. extern void erl_crash_dump_v(char *, int, char *, va_list);
  56. #ifdef __WIN32__
  57. extern void ConNormalExit(void);
  58. extern void ConWaitForExit(void);
  59. #endif
  60. static void erl_init(int ncpu);
  61. #define ERTS_MIN_COMPAT_REL 7
  62. #ifdef ERTS_SMP
  63. erts_smp_atomic_t erts_writing_erl_crash_dump;
  64. #else
  65. volatile int erts_writing_erl_crash_dump = 0;
  66. #endif
  67. int erts_initialized = 0;
  68. #if defined(USE_THREADS) && !defined(ERTS_SMP)
  69. static erts_tid_t main_thread;
  70. #endif
  71. int erts_use_sender_punish;
  72. /*
  73. * Configurable parameters.
  74. */
  75. Uint display_items; /* no of items to display in traces etc */
  76. Uint display_loads; /* print info about loaded modules */
  77. int H_MIN_SIZE; /* The minimum heap grain */
  78. int BIN_VH_MIN_SIZE; /* The minimum binary virtual*/
  79. Uint32 erts_debug_flags; /* Debug flags. */
  80. #ifdef ERTS_OPCODE_COUNTER_SUPPORT
  81. int count_instructions;
  82. #endif
  83. int erts_backtrace_depth; /* How many functions to show in a backtrace
  84. * in error codes.
  85. */
  86. int erts_async_max_threads; /* number of threads for async support */
  87. int erts_async_thread_suggested_stack_size;
  88. erts_smp_atomic32_t erts_max_gen_gcs;
  89. Eterm erts_error_logger_warnings; /* What to map warning logs to, am_error,
  90. am_info or am_warning, am_error is
  91. the default for BC */
  92. int erts_compat_rel;
  93. static int use_multi_run_queue;
  94. static int no_schedulers;
  95. static int no_schedulers_online;
  96. #ifdef DEBUG
  97. Uint32 verbose; /* See erl_debug.h for information about verbose */
  98. #endif
  99. int erts_disable_tolerant_timeofday; /* Time correction can be disabled it is
  100. * not and/or it is too slow.
  101. */
  102. int erts_atom_table_size = ATOM_LIMIT; /* Maximum number of atoms */
  103. int erts_modified_timing_level;
  104. int erts_no_crash_dump = 0; /* Use -d to suppress crash dump. */
  105. /*
  106. * Other global variables.
  107. */
  108. ErtsModifiedTimings erts_modified_timings[] = {
  109. /* 0 */ {make_small(0), CONTEXT_REDS, INPUT_REDUCTIONS},
  110. /* 1 */ {make_small(0), 2*CONTEXT_REDS, 2*INPUT_REDUCTIONS},
  111. /* 2 */ {make_small(0), CONTEXT_REDS/2, INPUT_REDUCTIONS/2},
  112. /* 3 */ {make_small(0), 3*CONTEXT_REDS, 3*INPUT_REDUCTIONS},
  113. /* 4 */ {make_small(0), CONTEXT_REDS/3, 3*INPUT_REDUCTIONS},
  114. /* 5 */ {make_small(0), 4*CONTEXT_REDS, INPUT_REDUCTIONS/2},
  115. /* 6 */ {make_small(1), CONTEXT_REDS/4, 2*INPUT_REDUCTIONS},
  116. /* 7 */ {make_small(1), 5*CONTEXT_REDS, INPUT_REDUCTIONS/3},
  117. /* 8 */ {make_small(10), CONTEXT_REDS/5, 3*INPUT_REDUCTIONS},
  118. /* 9 */ {make_small(10), 6*CONTEXT_REDS, INPUT_REDUCTIONS/4}
  119. };
  120. #define ERTS_MODIFIED_TIMING_LEVELS \
  121. (sizeof(erts_modified_timings)/sizeof(ErtsModifiedTimings))
  122. Export *erts_delay_trap = NULL;
  123. int erts_use_r9_pids_ports;
  124. #ifdef HYBRID
  125. Eterm *global_heap;
  126. Eterm *global_hend;
  127. Eterm *global_htop;
  128. Eterm *global_saved_htop;
  129. Eterm *global_old_heap;
  130. Eterm *global_old_hend;
  131. ErlOffHeap erts_global_offheap;
  132. Uint global_heap_sz = SH_DEFAULT_SIZE;
  133. #ifndef INCREMENTAL
  134. Eterm *global_high_water;
  135. Eterm *global_old_htop;
  136. #endif
  137. Uint16 global_gen_gcs;
  138. Uint16 global_max_gen_gcs;
  139. Uint global_gc_flags;
  140. Uint global_heap_min_sz = SH_DEFAULT_SIZE;
  141. #endif
  142. int ignore_break;
  143. int replace_intr;
  144. static ERTS_INLINE int
  145. has_prefix(const char *prefix, const char *string)
  146. {
  147. int i;
  148. for (i = 0; prefix[i]; i++)
  149. if (prefix[i] != string[i])
  150. return 0;
  151. return 1;
  152. }
  153. static char*
  154. progname(char *fullname)
  155. {
  156. int i;
  157. i = strlen(fullname);
  158. while (i >= 0) {
  159. if ((fullname[i] != '/') && (fullname[i] != '\\'))
  160. i--;
  161. else
  162. break;
  163. }
  164. return fullname+i+1;
  165. }
  166. static int
  167. this_rel_num(void)
  168. {
  169. static int this_rel = -1;
  170. if (this_rel < 1) {
  171. int i;
  172. char this_rel_str[] = ERLANG_OTP_RELEASE;
  173. i = 0;
  174. while (this_rel_str[i] && !isdigit((int) this_rel_str[i]))
  175. i++;
  176. this_rel = atoi(&this_rel_str[i]);
  177. if (this_rel < 1)
  178. erl_exit(-1, "Unexpected ERLANG_OTP_RELEASE format\n");
  179. }
  180. return this_rel;
  181. }
  182. /*
  183. * Common error printout function, all error messages
  184. * that don't go to the error logger go through here.
  185. */
  186. void erl_error(char *fmt, va_list args)
  187. {
  188. erts_vfprintf(stderr, fmt, args);
  189. }
  190. static int early_init(int *argc, char **argv);
  191. void
  192. erts_short_init(void)
  193. {
  194. int ncpu = early_init(NULL, NULL);
  195. erl_init(ncpu);
  196. erts_initialized = 1;
  197. }
  198. static void
  199. erl_init(int ncpu)
  200. {
  201. init_benchmarking();
  202. #ifdef ERTS_SMP
  203. erts_system_block_init();
  204. #endif
  205. erts_init_monitors();
  206. erts_init_gc();
  207. erts_init_time();
  208. erts_init_sys_common_misc();
  209. erts_init_process(ncpu);
  210. erts_init_scheduling(use_multi_run_queue,
  211. no_schedulers,
  212. no_schedulers_online);
  213. erts_init_cpu_topology(); /* Must be after init_scheduling */
  214. H_MIN_SIZE = erts_next_heap_size(H_MIN_SIZE, 0);
  215. BIN_VH_MIN_SIZE = erts_next_heap_size(BIN_VH_MIN_SIZE, 0);
  216. erts_init_trace();
  217. erts_init_binary();
  218. erts_init_bits();
  219. erts_init_fun_table();
  220. init_atom_table();
  221. init_export_table();
  222. init_module_table();
  223. init_register_table();
  224. init_message();
  225. erts_bif_info_init();
  226. erts_ddll_init();
  227. init_emulator();
  228. erts_bp_init();
  229. init_db(); /* Must be after init_emulator */
  230. erts_bif_timer_init();
  231. erts_init_node_tables();
  232. init_dist();
  233. erl_drv_thr_init();
  234. init_io();
  235. init_copy();
  236. init_load();
  237. erts_init_bif();
  238. erts_init_bif_chksum();
  239. erts_init_bif_binary();
  240. erts_init_bif_re();
  241. erts_init_unicode(); /* after RE to get access to PCRE unicode */
  242. erts_delay_trap = erts_export_put(am_erlang, am_delay_trap, 2);
  243. erts_late_init_process();
  244. #if HAVE_ERTS_MSEG
  245. erts_mseg_late_init(); /* Must be after timer (erts_init_time()) and thread
  246. initializations */
  247. #endif
  248. #ifdef HIPE
  249. hipe_mode_switch_init(); /* Must be after init_load/beam_catches/init */
  250. #endif
  251. packet_parser_init();
  252. erl_nif_init();
  253. }
  254. static void
  255. init_shared_memory(int argc, char **argv)
  256. {
  257. #ifdef HYBRID
  258. int arg_size = 0;
  259. global_heap_sz = erts_next_heap_size(global_heap_sz,0);
  260. /* Make sure arguments will fit on the heap, no one else will check! */
  261. while (argc--)
  262. arg_size += 2 + strlen(argv[argc]);
  263. if (global_heap_sz < arg_size)
  264. global_heap_sz = erts_next_heap_size(arg_size,1);
  265. #ifndef INCREMENTAL
  266. global_heap = (Eterm *) ERTS_HEAP_ALLOC(ERTS_ALC_T_HEAP,
  267. sizeof(Eterm) * global_heap_sz);
  268. global_hend = global_heap + global_heap_sz;
  269. global_htop = global_heap;
  270. global_high_water = global_heap;
  271. global_old_hend = global_old_htop = global_old_heap = NULL;
  272. #endif
  273. global_gen_gcs = 0;
  274. global_max_gen_gcs = (Uint16) erts_smp_atomic32_read(&erts_max_gen_gcs);
  275. global_gc_flags = erts_default_process_flags;
  276. erts_global_offheap.mso = NULL;
  277. #ifndef HYBRID /* FIND ME! */
  278. erts_global_offheap.funs = NULL;
  279. #endif
  280. erts_global_offheap.overhead = 0;
  281. #endif
  282. #ifdef INCREMENTAL
  283. erts_init_incgc();
  284. #endif
  285. }
  286. static void
  287. erl_first_process_otp(char* modname, void* code, unsigned size, int argc, char** argv)
  288. {
  289. int i;
  290. Eterm start_mod;
  291. Eterm args;
  292. Eterm* hp;
  293. Process parent;
  294. ErlSpawnOpts so;
  295. Eterm env;
  296. start_mod = am_atom_put(modname, sys_strlen(modname));
  297. if (erts_find_function(start_mod, am_start, 2) == NULL) {
  298. erl_exit(5, "No function %s:start/2\n", modname);
  299. }
  300. /*
  301. * We need a dummy parent process to be able to call erl_create_process().
  302. */
  303. erts_init_empty_process(&parent);
  304. erts_smp_proc_lock(&parent, ERTS_PROC_LOCK_MAIN);
  305. hp = HAlloc(&parent, argc*2 + 4);
  306. args = NIL;
  307. for (i = argc-1; i >= 0; i--) {
  308. int len = sys_strlen(argv[i]);
  309. args = CONS(hp, new_binary(&parent, (byte*)argv[i], len), args);
  310. hp += 2;
  311. }
  312. env = new_binary(&parent, code, size);
  313. args = CONS(hp, args, NIL);
  314. hp += 2;
  315. args = CONS(hp, env, args);
  316. so.flags = 0;
  317. (void) erl_create_process(&parent, start_mod, am_start, args, &so);
  318. erts_smp_proc_unlock(&parent, ERTS_PROC_LOCK_MAIN);
  319. erts_cleanup_empty_process(&parent);
  320. }
  321. Eterm
  322. erts_preloaded(Process* p)
  323. {
  324. Eterm previous;
  325. int j;
  326. int need;
  327. Eterm mod;
  328. Eterm* hp;
  329. char* name;
  330. const Preload *preload = sys_preloaded();
  331. j = 0;
  332. while (preload[j].name != NULL) {
  333. j++;
  334. }
  335. previous = NIL;
  336. need = 2*j;
  337. hp = HAlloc(p, need);
  338. j = 0;
  339. while ((name = preload[j].name) != NULL) {
  340. mod = am_atom_put(name, sys_strlen(name));
  341. previous = CONS(hp, mod, previous);
  342. hp += 2;
  343. j++;
  344. }
  345. return previous;
  346. }
  347. /* static variables that must not change (use same values at restart) */
  348. static char* program;
  349. static char* init = "init";
  350. static char* boot = "boot";
  351. static int boot_argc;
  352. static char** boot_argv;
  353. static char *
  354. get_arg(char* rest, char* next, int* ip)
  355. {
  356. if (*rest == '\0') {
  357. if (next == NULL) {
  358. erts_fprintf(stderr, "too few arguments\n");
  359. erts_usage();
  360. }
  361. (*ip)++;
  362. return next;
  363. }
  364. return rest;
  365. }
  366. static void
  367. load_preloaded(void)
  368. {
  369. int i;
  370. int res;
  371. Preload* preload_p;
  372. Eterm module_name;
  373. byte* code;
  374. char* name;
  375. int length;
  376. if ((preload_p = sys_preloaded()) == NULL) {
  377. return;
  378. }
  379. i = 0;
  380. while ((name = preload_p[i].name) != NULL) {
  381. length = preload_p[i].size;
  382. module_name = am_atom_put(name, sys_strlen(name));
  383. if ((code = sys_preload_begin(&preload_p[i])) == 0)
  384. erl_exit(1, "Failed to find preloaded code for module %s\n",
  385. name);
  386. res = erts_load_module(NULL, 0, NIL, &module_name, code, length);
  387. sys_preload_end(&preload_p[i]);
  388. if (res < 0)
  389. erl_exit(1,"Failed loading preloaded module %s\n", name);
  390. i++;
  391. }
  392. }
  393. /* be helpful (or maybe downright rude:-) */
  394. void erts_usage(void)
  395. {
  396. erts_fprintf(stderr, "Usage: %s [flags] [ -- [init_args] ]\n", progname(program));
  397. erts_fprintf(stderr, "The flags are:\n\n");
  398. /* erts_fprintf(stderr, "-# number set the number of items to be used in traces etc\n"); */
  399. erts_fprintf(stderr, "-a size suggested stack size in kilo words for threads\n");
  400. erts_fprintf(stderr, " in the async-thread pool, valid range is [%d-%d]\n",
  401. ERTS_ASYNC_THREAD_MIN_STACK_SIZE,
  402. ERTS_ASYNC_THREAD_MAX_STACK_SIZE);
  403. erts_fprintf(stderr, "-A number set number of threads in async thread pool,\n");
  404. erts_fprintf(stderr, " valid range is [0-%d]\n",
  405. ERTS_MAX_NO_OF_ASYNC_THREADS);
  406. erts_fprintf(stderr, "-B[c|d|i] c to have Ctrl-c interrupt the Erlang shell,\n");
  407. erts_fprintf(stderr, " d (or no extra option) to disable the break\n");
  408. erts_fprintf(stderr, " handler, i to ignore break signals\n");
  409. /* erts_fprintf(stderr, "-b func set the boot function (default boot)\n"); */
  410. erts_fprintf(stderr, "-c disable continuous date/time correction with\n");
  411. erts_fprintf(stderr, " respect to uptime\n");
  412. erts_fprintf(stderr, "-d don't write a crash dump for internally detected errors\n");
  413. erts_fprintf(stderr, " (halt(String) will still produce a crash dump)\n");
  414. erts_fprintf(stderr, "-hms size set minimum heap size in words (default %d)\n",
  415. H_DEFAULT_SIZE);
  416. erts_fprintf(stderr, "-hmbs size set minimum binary virtual heap size in words (default %d)\n",
  417. VH_DEFAULT_SIZE);
  418. /* erts_fprintf(stderr, "-i module set the boot module (default init)\n"); */
  419. erts_fprintf(stderr, "-K boolean enable or disable kernel poll\n");
  420. erts_fprintf(stderr, "-l turn on auto load tracing\n");
  421. erts_fprintf(stderr, "-M<X> <Y> memory allocator switches,\n");
  422. erts_fprintf(stderr, " see the erts_alloc(3) documentation for more info.\n");
  423. erts_fprintf(stderr, "-P number set maximum number of processes on this node,\n");
  424. erts_fprintf(stderr, " valid range is [%d-%d]\n",
  425. ERTS_MIN_PROCESSES, ERTS_MAX_PROCESSES);
  426. erts_fprintf(stderr, "-R number set compatibility release number,\n");
  427. erts_fprintf(stderr, " valid range [%d-%d]\n",
  428. ERTS_MIN_COMPAT_REL, this_rel_num());
  429. erts_fprintf(stderr, "-r force ets memory block to be moved on realloc\n");
  430. erts_fprintf(stderr, "-rg amount set reader groups limit\n");
  431. erts_fprintf(stderr, "-sbt type set scheduler bind type, valid types are:\n");
  432. erts_fprintf(stderr, " u|ns|ts|ps|s|nnts|nnps|tnnps|db\n");
  433. erts_fprintf(stderr, "-sct cput set cpu topology,\n");
  434. erts_fprintf(stderr, " see the erl(1) documentation for more info.\n");
  435. erts_fprintf(stderr, "-swt val set scheduler wakeup threshold, valid values are:\n");
  436. erts_fprintf(stderr, " very_low|low|medium|high|very_high.\n");
  437. erts_fprintf(stderr, "-sss size suggested stack size in kilo words for scheduler threads,\n");
  438. erts_fprintf(stderr, " valid range is [%d-%d]\n",
  439. ERTS_SCHED_THREAD_MIN_STACK_SIZE,
  440. ERTS_SCHED_THREAD_MAX_STACK_SIZE);
  441. erts_fprintf(stderr, "-S n1:n2 set number of schedulers (n1), and number of\n");
  442. erts_fprintf(stderr, " schedulers online (n2), valid range for both\n");
  443. erts_fprintf(stderr, " numbers are [1-%d]\n",
  444. ERTS_MAX_NO_OF_SCHEDULERS);
  445. erts_fprintf(stderr, "-t size set the maximum number of atoms the "
  446. "emulator can handle\n");
  447. erts_fprintf(stderr, " valid range is [%d-%d]\n",
  448. MIN_ATOM_TABLE_SIZE, MAX_ATOM_TABLE_SIZE);
  449. erts_fprintf(stderr, "-T number set modified timing level,\n");
  450. erts_fprintf(stderr, " valid range is [0-%d]\n",
  451. ERTS_MODIFIED_TIMING_LEVELS-1);
  452. erts_fprintf(stderr, "-V print Erlang version\n");
  453. erts_fprintf(stderr, "-v turn on chatty mode (GCs will be reported etc)\n");
  454. erts_fprintf(stderr, "-W<i|w> set error logger warnings mapping,\n");
  455. erts_fprintf(stderr, " see error_logger documentation for details\n");
  456. erts_fprintf(stderr, "-zdbbl size set the distribution buffer busy limit in kilobytes\n");
  457. erts_fprintf(stderr, " valid range is [1-%d]\n", INT_MAX/1024);
  458. erts_fprintf(stderr, "\n");
  459. erts_fprintf(stderr, "Note that if the emulator is started with erlexec (typically\n");
  460. erts_fprintf(stderr, "from the erl script), these flags should be specified with +.\n");
  461. erts_fprintf(stderr, "\n\n");
  462. erl_exit(-1, "");
  463. }
  464. #ifdef USE_THREADS
  465. /*
  466. * allocators for thread lib
  467. */
  468. static void *ethr_std_alloc(size_t size)
  469. {
  470. return erts_alloc_fnf(ERTS_ALC_T_ETHR_STD, (Uint) size);
  471. }
  472. static void *ethr_std_realloc(void *ptr, size_t size)
  473. {
  474. return erts_realloc_fnf(ERTS_ALC_T_ETHR_STD, ptr, (Uint) size);
  475. }
  476. static void ethr_std_free(void *ptr)
  477. {
  478. erts_free(ERTS_ALC_T_ETHR_STD, ptr);
  479. }
  480. static void *ethr_sl_alloc(size_t size)
  481. {
  482. return erts_alloc_fnf(ERTS_ALC_T_ETHR_SL, (Uint) size);
  483. }
  484. static void *ethr_sl_realloc(void *ptr, size_t size)
  485. {
  486. return erts_realloc_fnf(ERTS_ALC_T_ETHR_SL, ptr, (Uint) size);
  487. }
  488. static void ethr_sl_free(void *ptr)
  489. {
  490. erts_free(ERTS_ALC_T_ETHR_SL, ptr);
  491. }
  492. static void *ethr_ll_alloc(size_t size)
  493. {
  494. return erts_alloc_fnf(ERTS_ALC_T_ETHR_LL, (Uint) size);
  495. }
  496. static void *ethr_ll_realloc(void *ptr, size_t size)
  497. {
  498. return erts_realloc_fnf(ERTS_ALC_T_ETHR_LL, ptr, (Uint) size);
  499. }
  500. static void ethr_ll_free(void *ptr)
  501. {
  502. erts_free(ERTS_ALC_T_ETHR_LL, ptr);
  503. }
  504. #endif
  505. static int
  506. early_init(int *argc, char **argv) /*
  507. * Only put things here which are
  508. * really important initialize
  509. * early!
  510. */
  511. {
  512. ErtsAllocInitOpts alloc_opts = ERTS_ALLOC_INIT_DEF_OPTS_INITER;
  513. int ncpu;
  514. int ncpuonln;
  515. int ncpuavail;
  516. int schdlrs;
  517. int schdlrs_onln;
  518. int max_main_threads;
  519. int max_reader_groups;
  520. int reader_groups;
  521. use_multi_run_queue = 1;
  522. erts_printf_eterm_func = erts_printf_term;
  523. erts_disable_tolerant_timeofday = 0;
  524. display_items = 200;
  525. display_loads = 0;
  526. erts_backtrace_depth = DEFAULT_BACKTRACE_SIZE;
  527. erts_async_max_threads = 0;
  528. erts_async_thread_suggested_stack_size = ERTS_ASYNC_THREAD_MIN_STACK_SIZE;
  529. H_MIN_SIZE = H_DEFAULT_SIZE;
  530. BIN_VH_MIN_SIZE = VH_DEFAULT_SIZE;
  531. erts_initialized = 0;
  532. erts_use_sender_punish = 1;
  533. erts_pre_early_init_cpu_topology(&max_reader_groups,
  534. &ncpu,
  535. &ncpuonln,
  536. &ncpuavail);
  537. #ifndef ERTS_SMP
  538. ncpu = 1;
  539. ncpuonln = 1;
  540. ncpuavail = 1;
  541. #endif
  542. ignore_break = 0;
  543. replace_intr = 0;
  544. program = argv[0];
  545. erts_modified_timing_level = -1;
  546. erts_compat_rel = this_rel_num();
  547. erts_use_r9_pids_ports = 0;
  548. erts_sys_pre_init();
  549. #ifdef ERTS_ENABLE_LOCK_CHECK
  550. erts_lc_init();
  551. #endif
  552. #ifdef ERTS_SMP
  553. erts_smp_atomic_init(&erts_writing_erl_crash_dump, 0L);
  554. #else
  555. erts_writing_erl_crash_dump = 0;
  556. #endif
  557. erts_smp_atomic32_init(&erts_max_gen_gcs, (erts_aint32_t) ((Uint16) -1));
  558. erts_pre_init_process();
  559. #if defined(USE_THREADS) && !defined(ERTS_SMP)
  560. main_thread = erts_thr_self();
  561. #endif
  562. /*
  563. * We need to know the number of schedulers to use before we
  564. * can initialize the allocators.
  565. */
  566. no_schedulers = (Uint) (ncpu > 0 ? ncpu : 1);
  567. no_schedulers_online = (ncpuavail > 0
  568. ? ncpuavail
  569. : (ncpuonln > 0 ? ncpuonln : no_schedulers));
  570. schdlrs = no_schedulers;
  571. schdlrs_onln = no_schedulers_online;
  572. if (argc && argv) {
  573. int i = 1;
  574. while (i < *argc) {
  575. if (strcmp(argv[i], "--") == 0) { /* end of emulator options */
  576. i++;
  577. break;
  578. }
  579. if (argv[i][0] == '-') {
  580. switch (argv[i][1]) {
  581. case 'r': {
  582. char *sub_param = argv[i]+2;
  583. if (has_prefix("g", sub_param)) {
  584. char *arg = get_arg(sub_param+1, argv[i+1], &i);
  585. if (sscanf(arg, "%d", &max_reader_groups) != 1) {
  586. erts_fprintf(stderr,
  587. "bad reader groups limit: %s\n", arg);
  588. erts_usage();
  589. }
  590. if (max_reader_groups < 0) {
  591. erts_fprintf(stderr,
  592. "bad reader groups limit: %d\n",
  593. max_reader_groups);
  594. erts_usage();
  595. }
  596. }
  597. break;
  598. }
  599. case 'S' : {
  600. int tot, onln;
  601. char *arg = get_arg(argv[i]+2, argv[i+1], &i);
  602. switch (sscanf(arg, "%d:%d", &tot, &onln)) {
  603. case 0:
  604. switch (sscanf(arg, ":%d", &onln)) {
  605. case 1:
  606. tot = no_schedulers;
  607. goto chk_S;
  608. default:
  609. goto bad_S;
  610. }
  611. case 1:
  612. onln = tot < schdlrs_onln ? tot : schdlrs_onln;
  613. case 2:
  614. chk_S:
  615. if (tot > 0)
  616. schdlrs = tot;
  617. else
  618. schdlrs = no_schedulers + tot;
  619. if (onln > 0)
  620. schdlrs_onln = onln;
  621. else
  622. schdlrs_onln = no_schedulers_online + onln;
  623. if (schdlrs < 1 || ERTS_MAX_NO_OF_SCHEDULERS < schdlrs) {
  624. erts_fprintf(stderr,
  625. "bad amount of schedulers %d\n",
  626. tot);
  627. erts_usage();
  628. }
  629. if (schdlrs_onln < 1 || schdlrs < schdlrs_onln) {
  630. erts_fprintf(stderr,
  631. "bad amount of schedulers online %d "
  632. "(total amount of schedulers %d)\n",
  633. schdlrs_onln, schdlrs);
  634. erts_usage();
  635. }
  636. break;
  637. default:
  638. bad_S:
  639. erts_fprintf(stderr,
  640. "bad amount of schedulers %s\n",
  641. arg);
  642. erts_usage();
  643. break;
  644. }
  645. VERBOSE(DEBUG_SYSTEM,
  646. ("using %d:%d scheduler(s)\n", tot, onln));
  647. break;
  648. }
  649. default:
  650. break;
  651. }
  652. }
  653. i++;
  654. }
  655. }
  656. #ifdef ERTS_SMP
  657. no_schedulers = schdlrs;
  658. no_schedulers_online = schdlrs_onln;
  659. erts_no_schedulers = (Uint) no_schedulers;
  660. #endif
  661. erts_alloc_init(argc, argv, &alloc_opts); /* Handles (and removes)
  662. -M flags. */
  663. /* Require allocators */
  664. erts_early_init_scheduling();
  665. erts_init_utils();
  666. erts_early_init_cpu_topology(no_schedulers,
  667. &max_main_threads,
  668. max_reader_groups,
  669. &reader_groups);
  670. #ifdef USE_THREADS
  671. {
  672. erts_thr_late_init_data_t elid = ERTS_THR_LATE_INIT_DATA_DEF_INITER;
  673. elid.mem.std.alloc = ethr_std_alloc;
  674. elid.mem.std.realloc = ethr_std_realloc;
  675. elid.mem.std.free = ethr_std_free;
  676. elid.mem.sl.alloc = ethr_sl_alloc;
  677. elid.mem.sl.realloc = ethr_sl_realloc;
  678. elid.mem.sl.free = ethr_sl_free;
  679. elid.mem.ll.alloc = ethr_ll_alloc;
  680. elid.mem.ll.realloc = ethr_ll_realloc;
  681. elid.mem.ll.free = ethr_ll_free;
  682. elid.main_threads = max_main_threads;
  683. elid.reader_groups = reader_groups;
  684. erts_thr_late_init(&elid);
  685. }
  686. #endif
  687. #ifdef ERTS_ENABLE_LOCK_CHECK
  688. erts_lc_late_init();
  689. #endif
  690. #ifdef ERTS_ENABLE_LOCK_COUNT
  691. erts_lcnt_late_init();
  692. #endif
  693. #if defined(HIPE)
  694. hipe_signal_init(); /* must be done very early */
  695. #endif
  696. erl_sys_init();
  697. erl_sys_args(argc, argv);
  698. erts_ets_realloc_always_moves = 0;
  699. erts_ets_always_compress = 0;
  700. erts_dist_buf_busy_limit = ERTS_DE_BUSY_LIMIT;
  701. return ncpu;
  702. }
  703. #ifndef ERTS_SMP
  704. static void set_main_stack_size(void)
  705. {
  706. if (erts_sched_thread_suggested_stack_size > 0) {
  707. # if HAVE_DECL_GETRLIMIT && HAVE_DECL_SETRLIMIT && HAVE_DECL_RLIMIT_STACK
  708. struct rlimit rl;
  709. int bytes = erts_sched_thread_suggested_stack_size * sizeof(Uint) * 1024;
  710. if (getrlimit(RLIMIT_STACK, &rl) != 0 ||
  711. (rl.rlim_cur = bytes, setrlimit(RLIMIT_STACK, &rl) != 0)) {
  712. erts_fprintf(stderr, "failed to set stack size for scheduler "
  713. "thread to %d bytes\n", bytes);
  714. erts_usage();
  715. }
  716. # else
  717. erts_fprintf(stderr, "no OS support for dynamic stack size limit\n");
  718. erts_usage();
  719. # endif
  720. }
  721. }
  722. #endif
  723. void
  724. erl_start(int argc, char **argv)
  725. {
  726. int i = 1;
  727. char* arg=NULL;
  728. char* Parg = NULL;
  729. int have_break_handler = 1;
  730. char envbuf[21]; /* enough for any 64-bit integer */
  731. size_t envbufsz;
  732. int async_max_threads = erts_async_max_threads;
  733. int ncpu = early_init(&argc, argv);
  734. envbufsz = sizeof(envbuf);
  735. if (erts_sys_getenv(ERL_MAX_ETS_TABLES_ENV, envbuf, &envbufsz) == 0)
  736. user_requested_db_max_tabs = atoi(envbuf);
  737. else
  738. user_requested_db_max_tabs = 0;
  739. envbufsz = sizeof(envbuf);
  740. if (erts_sys_getenv("ERL_FULLSWEEP_AFTER", envbuf, &envbufsz) == 0) {
  741. Uint16 max_gen_gcs = atoi(envbuf);
  742. erts_smp_atomic32_set(&erts_max_gen_gcs, (erts_aint32_t) max_gen_gcs);
  743. }
  744. envbufsz = sizeof(envbuf);
  745. if (erts_sys_getenv("ERL_THREAD_POOL_SIZE", envbuf, &envbufsz) == 0) {
  746. async_max_threads = atoi(envbuf);
  747. }
  748. #if (defined(__APPLE__) && defined(__MACH__)) || defined(__DARWIN__)
  749. /*
  750. * The default stack size on MacOS X is too small for pcre.
  751. */
  752. erts_sched_thread_suggested_stack_size = 256;
  753. #endif
  754. #ifdef DEBUG
  755. verbose = DEBUG_DEFAULT;
  756. #endif
  757. erts_error_logger_warnings = am_error;
  758. while (i < argc) {
  759. if (argv[i][0] != '-') {
  760. erts_usage();
  761. }
  762. if (strcmp(argv[i], "--") == 0) { /* end of emulator options */
  763. i++;
  764. break;
  765. }
  766. switch (argv[i][1]) {
  767. /*
  768. * NOTE: -M flags are handled (and removed from argv) by
  769. * erts_alloc_init().
  770. *
  771. * The -d, -m, -S, -t, and -T flags was removed in
  772. * Erlang 5.3/OTP R9C.
  773. *
  774. * -S, and -T has been reused in Erlang 5.5/OTP R11B.
  775. *
  776. * -d has been reused in a patch R12B-4.
  777. */
  778. case '#' :
  779. arg = get_arg(argv[i]+2, argv[i+1], &i);
  780. if ((display_items = atoi(arg)) == 0) {
  781. erts_fprintf(stderr, "bad display items%s\n", arg);
  782. erts_usage();
  783. }
  784. VERBOSE(DEBUG_SYSTEM,
  785. ("using display items %d\n",display_items));
  786. break;
  787. case 'f':
  788. if (!strncmp(argv[i],"-fn",3)) {
  789. arg = get_arg(argv[i]+3, argv[i+1], &i);
  790. switch (*arg) {
  791. case 'u':
  792. erts_set_user_requested_filename_encoding(ERL_FILENAME_UTF8);
  793. break;
  794. case 'l':
  795. erts_set_user_requested_filename_encoding(ERL_FILENAME_LATIN1);
  796. break;
  797. case 'a':
  798. erts_set_user_requested_filename_encoding(ERL_FILENAME_UNKNOWN);
  799. default:
  800. erts_fprintf(stderr, "bad filename encoding %s, can be (l,u or a)\n", arg);
  801. erts_usage();
  802. }
  803. break;
  804. } else {
  805. erts_fprintf(stderr, "%s unknown flag %s\n", argv[0], argv[i]);
  806. erts_usage();
  807. }
  808. case 'l':
  809. display_loads++;
  810. break;
  811. case 'v':
  812. #ifdef DEBUG
  813. if (argv[i][2] == '\0') {
  814. verbose |= DEBUG_SYSTEM;
  815. } else {
  816. char *ch;
  817. for (ch = argv[i]+2; *ch != '\0'; ch++) {
  818. switch (*ch) {
  819. case 's': verbose |= DEBUG_SYSTEM; break;
  820. case 'g': verbose |= DEBUG_PRIVATE_GC; break;
  821. case 'h': verbose |= DEBUG_HYBRID_GC; break;
  822. case 'M': verbose |= DEBUG_MEMORY; break;
  823. case 'a': verbose |= DEBUG_ALLOCATION; break;
  824. case 't': verbose |= DEBUG_THREADS; break;
  825. case 'p': verbose |= DEBUG_PROCESSES; break;
  826. case 'm': verbose |= DEBUG_MESSAGES; break;
  827. default : erts_fprintf(stderr,"Unknown verbose option: %c\n",*ch);
  828. }
  829. }
  830. }
  831. erts_printf("Verbose level: ");
  832. if (verbose & DEBUG_SYSTEM) erts_printf("SYSTEM ");
  833. if (verbose & DEBUG_PRIVATE_GC) erts_printf("PRIVATE_GC ");
  834. if (verbose & DEBUG_HYBRID_GC) erts_printf("HYBRID_GC ");
  835. if (verbose & DEBUG_MEMORY) erts_printf("PARANOID_MEMORY ");
  836. if (verbose & DEBUG_ALLOCATION) erts_printf("ALLOCATION ");
  837. if (verbose & DEBUG_THREADS) erts_printf("THREADS ");
  838. if (verbose & DEBUG_PROCESSES) erts_printf("PROCESSES ");
  839. if (verbose & DEBUG_MESSAGES) erts_printf("MESSAGES ");
  840. erts_printf("\n");
  841. #else
  842. erts_fprintf(stderr, "warning: -v (only in debug compiled code)\n");
  843. #endif
  844. break;
  845. case 'V' :
  846. {
  847. char tmp[256];
  848. tmp[0] = tmp[1] = '\0';
  849. #ifdef DEBUG
  850. strcat(tmp, ",DEBUG");
  851. #endif
  852. #ifdef ERTS_SMP
  853. strcat(tmp, ",SMP");
  854. #endif
  855. #ifdef USE_THREADS
  856. strcat(tmp, ",ASYNC_THREADS");
  857. #endif
  858. #ifdef HIPE
  859. strcat(tmp, ",HIPE");
  860. #endif
  861. #ifdef INCREMENTAL
  862. strcat(tmp, ",INCREMENTAL_GC");
  863. #endif
  864. #ifdef HYBRID
  865. strcat(tmp, ",HYBRID");
  866. #endif
  867. erts_fprintf(stderr, "Erlang ");
  868. if (tmp[1]) {
  869. erts_fprintf(stderr, "(%s) ", tmp+1);
  870. }
  871. erts_fprintf(stderr, "(" EMULATOR ") emulator version "
  872. ERLANG_VERSION "\n");
  873. erl_exit(0, "");
  874. }
  875. break;
  876. case 'H': /* undocumented */
  877. fprintf(stderr, "The undocumented +H option has been removed (R10B-6).\n\n");
  878. break;
  879. case 'h': {
  880. char *sub_param = argv[i]+2;
  881. /* set default heap size
  882. *
  883. * h|ms - min_heap_size
  884. * h|mbs - min_bin_vheap_size
  885. *
  886. */
  887. if (has_prefix("mbs", sub_param)) {
  888. arg = get_arg(sub_param+3, argv[i+1], &i);
  889. if ((BIN_VH_MIN_SIZE = atoi(arg)) <= 0) {
  890. erts_fprintf(stderr, "bad heap size %s\n", arg);
  891. erts_usage();
  892. }
  893. VERBOSE(DEBUG_SYSTEM, ("using minimum binary virtual heap size %d\n", BIN_VH_MIN_SIZE));
  894. } else if (has_prefix("ms", sub_param)) {
  895. arg = get_arg(sub_param+2, argv[i+1], &i);
  896. if ((H_MIN_SIZE = atoi(arg)) <= 0) {
  897. erts_fprintf(stderr, "bad heap size %s\n", arg);
  898. erts_usage();
  899. }
  900. VERBOSE(DEBUG_SYSTEM, ("using minimum heap size %d\n", H_MIN_SIZE));
  901. } else {
  902. /* backward compatibility */
  903. arg = get_arg(argv[i]+2, argv[i+1], &i);
  904. if ((H_MIN_SIZE = atoi(arg)) <= 0) {
  905. erts_fprintf(stderr, "bad heap size %s\n", arg);
  906. erts_usage();
  907. }
  908. VERBOSE(DEBUG_SYSTEM, ("using minimum heap size %d\n", H_MIN_SIZE));
  909. }
  910. break;
  911. }
  912. case 'd':
  913. /*
  914. * Never produce crash dumps for internally detected
  915. * errors; only produce a core dump. (Generation of
  916. * crash dumps is destructive and makes it impossible
  917. * to inspect the contents of process heaps in the
  918. * core dump.)
  919. */
  920. erts_no_crash_dump = 1;
  921. break;
  922. case 'e':
  923. if (sys_strcmp("c", argv[i]+2) == 0) {
  924. erts_ets_always_compress = 1;
  925. }
  926. else {
  927. /* set maximum number of ets tables */
  928. arg = get_arg(argv[i]+2, argv[i+1], &i);
  929. if (( user_requested_db_max_tabs = atoi(arg) ) < 0) {
  930. erts_fprintf(stderr, "bad maximum number of ets tables %s\n", arg);
  931. erts_usage();
  932. }
  933. VERBOSE(DEBUG_SYSTEM,
  934. ("using maximum number of ets tables %d\n",
  935. user_requested_db_max_tabs));
  936. }
  937. break;
  938. case 'i':
  939. /* define name of module for initial function */
  940. init = get_arg(argv[i]+2, argv[i+1], &i);
  941. break;
  942. case 'b':
  943. /* define name of initial function */
  944. boot = get_arg(argv[i]+2, argv[i+1], &i);
  945. break;
  946. case 'B':
  947. if (argv[i][2] == 'i') /* +Bi */
  948. ignore_break = 1;
  949. else if (argv[i][2] == 'c') /* +Bc */
  950. replace_intr = 1;
  951. else if (argv[i][2] == 'd') /* +Bd */
  952. have_break_handler = 0;
  953. else if (argv[i+1][0] == 'i') { /* +B i */
  954. get_arg(argv[i]+2, argv[i+1], &i);
  955. ignore_break = 1;
  956. }
  957. else if (argv[i+1][0] == 'c') { /* +B c */
  958. get_arg(argv[i]+2, argv[i+1], &i);
  959. replace_intr = 1;
  960. }
  961. else if (argv[i+1][0] == 'd') { /* +B d */
  962. get_arg(argv[i]+2, argv[i+1], &i);
  963. have_break_handler = 0;
  964. }
  965. else /* +B */
  966. have_break_handler = 0;
  967. break;
  968. case 'K':
  969. /* If kernel poll support is present,
  970. erl_sys_args() will remove the K parameter
  971. and value */
  972. get_arg(argv[i]+2, argv[i+1], &i);
  973. erts_fprintf(stderr,
  974. "kernel-poll not supported; \"K\" parameter ignored\n",
  975. arg);
  976. break;
  977. case 'P':
  978. /* set maximum number of processes */
  979. Parg = get_arg(argv[i]+2, argv[i+1], &i);
  980. erts_max_processes = atoi(Parg);
  981. /* Check of result is delayed until later. This is because +R
  982. may be given after +P. */
  983. break;
  984. case 'S' : /* Was handled in early_init() just read past it */
  985. (void) get_arg(argv[i]+2, argv[i+1], &i);
  986. break;
  987. case 's' : {
  988. char *estr;
  989. int res;
  990. char *sub_param = argv[i]+2;
  991. if (has_prefix("bt", sub_param)) {
  992. arg = get_arg(sub_param+2, argv[i+1], &i);
  993. res = erts_init_scheduler_bind_type_string(arg);
  994. if (res != ERTS_INIT_SCHED_BIND_TYPE_SUCCESS) {
  995. switch (res) {
  996. case ERTS_INIT_SCHED_BIND_TYPE_NOT_SUPPORTED:
  997. estr = "not supported";
  998. break;
  999. case ERTS_INIT_SCHED_BIND_TYPE_ERROR_NO_CPU_TOPOLOGY:
  1000. estr = "no cpu topology available";
  1001. break;
  1002. case ERTS_INIT_SCHED_BIND_TYPE_ERROR_NO_BAD_TYPE:
  1003. estr = "invalid type";
  1004. break;
  1005. default:
  1006. estr = "undefined error";
  1007. break;
  1008. }
  1009. erts_fprintf(stderr,
  1010. "setting scheduler bind type '%s' failed: %s\n",
  1011. arg,
  1012. estr);
  1013. erts_usage();
  1014. }
  1015. }
  1016. else if (has_prefix("ct", sub_param)) {
  1017. arg = get_arg(sub_param+2, argv[i+1], &i);
  1018. res = erts_init_cpu_topology_string(arg);
  1019. if (res != ERTS_INIT_CPU_TOPOLOGY_OK) {
  1020. switch (res) {
  1021. case ERTS_INIT_CPU_TOPOLOGY_INVALID_ID:
  1022. estr = "invalid identifier";
  1023. break;
  1024. case ERTS_INIT_CPU_TOPOLOGY_INVALID_ID_RANGE:
  1025. estr = "invalid identifier range";
  1026. break;
  1027. case ERTS_INIT_CPU_TOPOLOGY_INVALID_HIERARCHY:
  1028. estr = "invalid hierarchy";
  1029. break;
  1030. case ERTS_INIT_CPU_TOPOLOGY_INVALID_ID_TYPE:
  1031. estr = "invalid identifier type";
  1032. break;
  1033. case ERTS_INIT_CPU_TOPOLOGY_INVALID_NODES:
  1034. estr = "invalid nodes declaration";
  1035. break;
  1036. case ERTS_INIT_CPU_TOPOLOGY_MISSING_LID:
  1037. estr = "missing logical identifier";
  1038. break;
  1039. case ERTS_INIT_CPU_TOPOLOGY_NOT_UNIQUE_LIDS:
  1040. estr = "not unique logical identifiers";
  1041. break;
  1042. case ERTS_INIT_CPU_TOPOLOGY_NOT_UNIQUE_ENTITIES:
  1043. estr = "not unique entities";
  1044. break;
  1045. case ERTS_INIT_CPU_TOPOLOGY_MISSING:
  1046. estr = "missing cpu topology";
  1047. break;
  1048. default:
  1049. estr = "undefined error";
  1050. break;
  1051. }
  1052. erts_fprintf(stderr,
  1053. "bad cpu topology '%s': %s\n",
  1054. arg,
  1055. estr);
  1056. erts_usage();
  1057. }
  1058. }
  1059. else if (sys_strcmp("mrq", sub_param) == 0)
  1060. use_multi_run_queue = 1;
  1061. else if (sys_strcmp("nsp", sub_param) == 0)
  1062. erts_use_sender_punish = 0;
  1063. else if (sys_strcmp("srq", sub_param) == 0)
  1064. use_multi_run_queue = 0;
  1065. else if (sys_strcmp("wt", sub_param) == 0) {
  1066. arg = get_arg(sub_param+2, argv[i+1], &i);
  1067. if (erts_sched_set_wakeup_limit(arg) != 0) {
  1068. erts_fprintf(stderr, "scheduler wakeup threshold: %s\n",
  1069. arg);
  1070. erts_usage();
  1071. }
  1072. VERBOSE(DEBUG_SYSTEM,
  1073. ("scheduler wakup threshold: %s\n", arg));
  1074. }
  1075. else if (has_prefix("ss", sub_param)) {
  1076. /* suggested stack size (Kilo Words) for scheduler threads */
  1077. arg = get_arg(sub_param+2, argv[i+1], &i);
  1078. erts_sched_thread_suggested_stack_size = atoi(arg);
  1079. if ((erts_sched_thread_suggested_stack_size
  1080. < ERTS_SCHED_THREAD_MIN_STACK_SIZE)
  1081. || (erts_sched_thread_suggested_stack_size >
  1082. ERTS_SCHED_THREAD_MAX_STACK_SIZE)) {
  1083. erts_fprintf(stderr, "bad stack size for scheduler threads %s\n",
  1084. arg);
  1085. erts_usage();
  1086. }
  1087. VERBOSE(DEBUG_SYSTEM,
  1088. ("suggested scheduler thread stack size %d kilo words\n",
  1089. erts_sched_thread_suggested_stack_size));
  1090. }
  1091. else {
  1092. erts_fprintf(stderr, "bad scheduling option %s\n", argv[i]);
  1093. erts_usage();
  1094. }
  1095. break;
  1096. }
  1097. case 't':
  1098. /* set atom table size */
  1099. arg = get_arg(argv[i]+2, argv[i+1], &i);
  1100. errno = 0;
  1101. erts_atom_table_size = strtol(arg, NULL, 10);
  1102. if (errno != 0 ||
  1103. erts_atom_table_size < MIN_ATOM_TABLE_SIZE ||
  1104. erts_atom_table_size > MAX_ATOM_TABLE_SIZE) {
  1105. erts_fprintf(stderr, "bad atom table size %s\n", arg);
  1106. erts_usage();
  1107. }
  1108. VERBOSE(DEBUG_SYSTEM,
  1109. ("setting maximum number of atoms to %d\n",
  1110. erts_atom_table_size));
  1111. break;
  1112. case 'T' :
  1113. arg = get_arg(argv[i]+2, argv[i+1], &i);
  1114. errno = 0;
  1115. erts_modified_timing_level = atoi(arg);
  1116. if ((erts_modified_timing_level == 0 && errno != 0)
  1117. || erts_modified_timing_level < 0
  1118. || erts_modified_timing_level >= ERTS_MODIFIED_TIMING_LEVELS) {
  1119. erts_fprintf(stderr, "bad modified timing level %s\n", arg);
  1120. erts_usage();
  1121. }
  1122. else {
  1123. VERBOSE(DEBUG_SYSTEM,
  1124. ("using modified timing level %d\n",
  1125. erts_modified_timing_level));
  1126. }
  1127. break;
  1128. case 'R': {
  1129. /* set compatibility release */
  1130. arg = get_arg(argv[i]+2, argv[i+1], &i);
  1131. erts_compat_rel = atoi(arg);
  1132. if (erts_compat_rel < ERTS_MIN_COMPAT_REL
  1133. || erts_compat_rel > this_rel_num()) {
  1134. erts_fprintf(stderr, "bad compatibility release number %s\n", arg);
  1135. erts_usage();
  1136. }
  1137. ASSERT(ERTS_MIN_COMPAT_REL >= 7);
  1138. switch (erts_compat_rel) {
  1139. case 7:
  1140. case 8:
  1141. case 9:
  1142. erts_use_r9_pids_ports = 1;
  1143. default:
  1144. break;
  1145. }
  1146. break;
  1147. }
  1148. case 'A':
  1149. /* set number of threads in thread pool */
  1150. arg = get_arg(argv[i]+2, argv[i+1], &i);
  1151. if (((async_max_threads = atoi(arg)) < 0) ||
  1152. (async_max_threads > ERTS_MAX_NO_OF_ASYNC_THREADS)) {
  1153. erts_fprintf(stderr, "bad number of async threads %s\n", arg);
  1154. erts_usage();
  1155. }
  1156. VERBOSE(DEBUG_SYSTEM, ("using %d async-threads\n",
  1157. async_max_threads));
  1158. break;
  1159. case 'a':
  1160. /* suggested stack size (Kilo Words) for threads in thread pool */
  1161. arg = get_arg(argv[i]+2, argv[i+1], &i);
  1162. erts_async_thread_suggested_stack_size = atoi(arg);
  1163. if ((erts_async_thread_suggested_stack_size
  1164. < ERTS_ASYNC_THREAD_MIN_STACK_SIZE)
  1165. || (erts_async_thread_suggested_stack_size >
  1166. ERTS_ASYNC_THREAD_MAX_STACK_SIZE)) {
  1167. erts_fprintf(stderr, "bad stack size for async threads %s\n",
  1168. arg);
  1169. erts_usage();
  1170. }
  1171. VERBOSE(DEBUG_SYSTEM,
  1172. ("suggested async-thread stack size %d kilo words\n",
  1173. erts_async_thread_suggested_stack_size));
  1174. break;
  1175. case 'r': {
  1176. char *sub_param = argv[i]+2;
  1177. if (has_prefix("g", sub_param)) {
  1178. get_arg(sub_param+1, argv[i+1], &i);
  1179. /* already handled */
  1180. }
  1181. else {
  1182. erts_ets_realloc_always_moves = 1;
  1183. }
  1184. break;
  1185. }
  1186. case 'n': /* XXX obsolete */
  1187. break;
  1188. case 'c':
  1189. if (argv[i][2] == 0) { /* -c: documented option */
  1190. erts_disable_tolerant_timeofday = 1;
  1191. }
  1192. #ifdef ERTS_OPCODE_COUNTER_SUPPORT
  1193. else if (argv[i][2] == 'i') { /* -ci: undcoumented option*/
  1194. count_instructions = 1;
  1195. }
  1196. #endif
  1197. break;
  1198. case 'W':
  1199. arg = get_arg(argv[i]+2, argv[i+1], &i);
  1200. switch (arg[0]) {
  1201. case 'i':
  1202. erts_error_logger_warnings = am_info;
  1203. break;
  1204. case 'w':
  1205. erts_error_logger_warnings = am_warning;
  1206. break;
  1207. case 'e': /* The default */
  1208. erts_error_logger_warnings = am_error;
  1209. default:
  1210. erts_fprintf(stderr, "unrecognized warning_map option %s\n", arg);
  1211. erts_usage();
  1212. }
  1213. break;
  1214. case 'z': {
  1215. char *sub_param = argv[i]+2;
  1216. int new_limit;
  1217. if (has_prefix("dbbl", sub_param)) {
  1218. arg = get_arg(sub_param+4, argv[i+1], &i);
  1219. new_limit = atoi(arg);
  1220. if (new_limit < 1 || INT_MAX/1024 < new_limit) {
  1221. erts_fprintf(stderr, "Invalid dbbl limit: %d\n", new_limit);
  1222. erts_usage();
  1223. } else {
  1224. erts_dist_buf_busy_limit = new_limit*1024;
  1225. }
  1226. } else {
  1227. erts_fprintf(stderr, "bad -z option %s\n", argv[i]);
  1228. erts_usage();
  1229. }
  1230. break;
  1231. }
  1232. default:
  1233. erts_fprintf(stderr, "%s unknown flag %s\n", argv[0], argv[i]);
  1234. erts_usage();
  1235. }
  1236. i++;
  1237. }
  1238. #ifdef USE_THREADS
  1239. erts_async_max_threads = async_max_threads;
  1240. #endif
  1241. /* Delayed check of +P flag */
  1242. if (erts_max_processes < ERTS_MIN_PROCESSES
  1243. || erts_max_processes > ERTS_MAX_PROCESSES
  1244. || (erts_use_r9_pids_ports
  1245. && erts_max_processes > ERTS_MAX_R9_PROCESSES)) {
  1246. erts_fprintf(stderr, "bad number of processes %s\n", Parg);
  1247. erts_usage();
  1248. }
  1249. /* Restart will not reinstall the break handler */
  1250. #ifdef __WIN32__
  1251. if (ignore_break)
  1252. erts_set_ignore_break();
  1253. else if (replace_intr)
  1254. erts_replace_intr();
  1255. else
  1256. init_break_handler();
  1257. #else
  1258. if (ignore_break)
  1259. erts_set_ignore_break();
  1260. else if (have_break_handler)
  1261. init_break_handler();
  1262. if (replace_intr)
  1263. erts_replace_intr();
  1264. #endif
  1265. boot_argc = argc - i; /* Number of arguments to init */
  1266. boot_argv = &argv[i];
  1267. erl_init(ncpu);
  1268. init_shared_memory(boot_argc, boot_argv);
  1269. load_preloaded();
  1270. erts_initialized = 1;
  1271. erl_first_process_otp("otp_ring0", NULL, 0, boot_argc, boot_argv);
  1272. #ifdef ERTS_SMP
  1273. erts_start_schedulers();
  1274. /* Let system specific code decide what to do with the main thread... */
  1275. erts_sys_main_thread(); /* May or may not return! */
  1276. #else
  1277. erts_thr_set_main_status(1, 1);
  1278. set_main_stack_size();
  1279. process_main();
  1280. #endif
  1281. }
  1282. #ifdef USE_THREADS
  1283. __decl_noreturn void erts_thr_fatal_error(int err, char *what)
  1284. {
  1285. char *errstr = err ? strerror(err) : NULL;
  1286. erts_fprintf(stderr,
  1287. "Failed to %s: %s%s(%d)\n",
  1288. what,
  1289. errstr ? errstr : "",
  1290. errstr ? " " : "",
  1291. err);
  1292. abort();
  1293. }
  1294. #endif
  1295. static void
  1296. system_cleanup(int exit_code)
  1297. {
  1298. /* No cleanup wanted if ...
  1299. * 1. we are about to do an abnormal exit
  1300. * 2. we haven't finished initializing, or
  1301. * 3. another thread than the main thread is performing the exit
  1302. * (in threaded non smp case).
  1303. */
  1304. if (exit_code != 0
  1305. || !erts_initialized
  1306. #if defined(USE_THREADS) && !defined(ERTS_SMP)
  1307. || !erts_equal_tids(main_thread, erts_thr_self())
  1308. #endif
  1309. )
  1310. return;
  1311. #ifdef ERTS_SMP
  1312. #ifdef ERTS_ENABLE_LOCK_CHECK
  1313. erts_lc_check_exact(NULL, 0);
  1314. #endif
  1315. erts_smp_block_system(ERTS_BS_FLG_ALLOW_GC); /* We never release it... */
  1316. #endif
  1317. #ifdef HYBRID
  1318. if (ma_src_stack) erts_free(ERTS_ALC_T_OBJECT_STACK,
  1319. (void *)ma_src_stack);
  1320. if (ma_dst_stack) erts_free(ERTS_ALC_T_OBJECT_STACK,
  1321. (void *)ma_dst_stack);
  1322. if (ma_offset_stack) erts_free(ERTS_ALC_T_OBJECT_STACK,
  1323. (void *)ma_offset_stack);
  1324. ma_src_stack = NULL;
  1325. ma_dst_stack = NULL;
  1326. ma_offset_stack = NULL;
  1327. erts_cleanup_offheap(&erts_global_offheap);
  1328. #endif
  1329. #if defined(HYBRID) && !defined(INCREMENTAL)
  1330. if (global_heap) {
  1331. ERTS_HEAP_FREE(ERTS_ALC_T_HEAP,
  1332. (void*) global_heap,
  1333. sizeof(Eterm) * global_heap_sz);
  1334. }
  1335. global_heap = NULL;
  1336. #endif
  1337. #ifdef INCREMENTAL
  1338. erts_cleanup_incgc();
  1339. #endif
  1340. #if defined(USE_THREADS)
  1341. exit_async();
  1342. #endif
  1343. #if HAVE_ERTS_MSEG
  1344. erts_mseg_exit();
  1345. #endif
  1346. /*
  1347. * A lot more cleaning could/should have been done...
  1348. */
  1349. }
  1350. /*
  1351. * Common exit function, all exits from the system go through here.
  1352. * n <= 0 -> normal exit with status n;
  1353. * n = 127 -> Erlang crash dump produced, exit with status 1;
  1354. * other positive n -> Erlang crash dump and core dump produced.
  1355. */
  1356. __decl_noreturn void erl_exit0(char *file, int line, int n, char *fmt,...)
  1357. {
  1358. unsigned int an;
  1359. va_list args;
  1360. va_start(args, fmt);
  1361. save_statistics();
  1362. system_cleanup(n);
  1363. an = abs(n);
  1364. if (erts_mtrace_enabled)
  1365. erts_mtrace_exit((Uint32) an);
  1366. /* Produce an Erlang core dump if error */
  1367. if (n > 0 && erts_initialized &&
  1368. (erts_no_crash_dump == 0 || n == ERTS_DUMP_EXIT)) {
  1369. erl_crash_dump_v(file, line, fmt, args);
  1370. }
  1371. /* need to reinitialize va_args thing */
  1372. va_end(args);
  1373. va_start(args, fmt);
  1374. if (fmt != NULL && *fmt != '\0')
  1375. erl_error(fmt, args); /* Print error message. */
  1376. va_end(args);
  1377. sys_tty_reset(n);
  1378. if (n == ERTS_INTR_EXIT)
  1379. exit(0);
  1380. else if (n == 127)
  1381. ERTS_EXIT_AFTER_DUMP(1);
  1382. else if (n > 0 || n == ERTS_ABORT_EXIT)
  1383. abort();
  1384. exit(an);
  1385. }
  1386. __decl_noreturn void erl_exit(int n, char *fmt,...)
  1387. {
  1388. unsigned int an;
  1389. va_list args;
  1390. va_start(args, fmt);
  1391. save_statistics();
  1392. system_cleanup(n);
  1393. an = abs(n);
  1394. if (erts_mtrace_enabled)
  1395. erts_mtrace_exit((Uint32) an);
  1396. /* Produce an Erlang core dump if error */
  1397. if (n > 0 && erts_initialized &&
  1398. (erts_no_crash_dump == 0 || n == ERTS_DUMP_EXIT)) {
  1399. erl_crash_dump_v((char*) NULL, 0, fmt, args);
  1400. }
  1401. /* need to reinitialize va_args thing */
  1402. va_end(args);
  1403. va_start(args, fmt);
  1404. if (fmt != NULL && *fmt != '\0')
  1405. erl_error(fmt, args); /* Print error message. */
  1406. va_end(args);
  1407. sys_tty_reset(n);
  1408. if (n == ERTS_INTR_EXIT)
  1409. exit(0);
  1410. else if (n == ERTS_DUMP_EXIT)
  1411. ERTS_EXIT_AFTER_DUMP(1);
  1412. else if (n > 0 || n == ERTS_ABORT_EXIT)
  1413. abort();
  1414. exit(an);
  1415. }