diff options
Diffstat (limited to 'erts')
77 files changed, 2882 insertions, 1332 deletions
diff --git a/erts/config.h.in b/erts/config.h.in index ee9390f1b9..d7fecf8631 100644 --- a/erts/config.h.in +++ b/erts/config.h.in @@ -89,6 +89,9 @@ /* Socket address dl length */ #undef ESOCK_SDL_LEN +/* Use extended error info */ +#undef ESOCK_USE_EXTENDED_ERROR_INFO + /* Interface hwaddr supported */ #undef ESOCK_USE_HWADDR diff --git a/erts/configure b/erts/configure index 2c76bc1587..a09c84ff09 100755 --- a/erts/configure +++ b/erts/configure @@ -860,6 +860,7 @@ with_threadnames enable_builtin_zlib enable_esock enable_esock_use_rcvsndtimeo +enable_esock_extended_error_info with_esock_counter_size enable_esock_socket_registry with_clock_resolution @@ -1596,6 +1597,10 @@ Optional Features: --disable-esock-rcvsndtimeo disable use of the option(s) rcvtimeo and sndtimeo (default) + --enable-esock-extended-error-info + enable use of extended error info + --disable-esock-extended-error-info + disable use of extended error info (default) --enable-esock-socket-registry enable use of the socket registry by default (default) @@ -14193,7 +14198,7 @@ int main (void) { - __asm__ __volatile__("isb sy" : : : "memory"); + __asm__ __volatile__("isb sy\n" : : : "memory"); ; return 0; @@ -14227,7 +14232,7 @@ int main (void) { - char data[512]; __asm__ __volatile__("dc cvau, %0" : "r" (data) : : "memory"); + char data[512]; __asm__ __volatile__("dc cvau, %0\n" :: "r" (data) : "memory"); ; return 0; @@ -14261,7 +14266,7 @@ int main (void) { - char data[512]; __asm__ __volatile__("ic ivau, %0" : "r" (data) : : "memory"); + char data[512]; __asm__ __volatile__("ic ivau, %0\n" :: "r" (data) : "memory"); ; return 0; @@ -16070,6 +16075,21 @@ fi +# Check whether --enable-esock_extended_error_info was given. +if test ${enable_esock_extended_error_info+y} +then : + enableval=$enable_esock_extended_error_info; +fi + + +if test "x$enable_esock_extended_error_info" != "xno"; then + +printf "%s\n" "#define ESOCK_USE_EXTENDED_ERROR_INFO 1" >>confdefs.h + +fi + + + # Check whether --with-esock-counter-size was given. if test ${with_esock_counter_size+y} @@ -24539,7 +24559,27 @@ then : JIT_ARCH=x86 ;; arm64) - JIT_ARCH=arm + case "$OPSYS" in + win32|darwin) + # These platforms have dedicated system calls for clearing + # instruction cache, and don't require us to manually issue + # instruction barriers on all threads. + JIT_ARCH=arm + ;; + *) + # We need to use `DC CVAU`, `IC IVAU`, and `ISB SY` to clear + # instruction cache. These have already been tested as part of + # ETHR_CHK_GCC_ATOMIC_OPS([]). + + if test "$ethr_arm_isb_sy_instr_val$ethr_arm_dc_cvau_instr_val$ethr_arm_ic_ivau_instr_val" = "111"; then + JIT_ARCH=arm + else + enable_jit=no + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: WARNING: JIT disabled due to lack of cache-clearing instructions" >&5 +printf "%s\n" "$as_me: WARNING: JIT disabled due to lack of cache-clearing instructions" >&2;} + fi + ;; + esac ;; *) if test ${enable_jit} = yes; then diff --git a/erts/configure.ac b/erts/configure.ac index bedaff3c0b..540cc4b3cb 100644 --- a/erts/configure.ac +++ b/erts/configure.ac @@ -2,7 +2,7 @@ dnl Process this file with autoconf to produce a configure script. -*-Autoconf-* dnl %CopyrightBegin% dnl -dnl Copyright Ericsson AB 1997-2022. All Rights Reserved. +dnl Copyright Ericsson AB 1997-2023. All Rights Reserved. dnl dnl Licensed under the Apache License, Version 2.0 (the "License"); dnl you may not use this file except in compliance with the License. @@ -1356,6 +1356,19 @@ if test "x$enable_esock_rcvsndtimeo" = "xyes"; then fi +dnl *** ESOCK_USE_EXTERNDED_ERROR_INFO *** + +AC_ARG_ENABLE(esock_extended_error_info, +AS_HELP_STRING([--enable-esock-extended-error-info], [enable use of extended error info]) +AS_HELP_STRING([--disable-esock-extended-error-info], [disable use of extended error info (default)])) + +dnl Temporary! Currently we require eei to be *explicitly* +dnl disabled (for debug reasons). +if test "x$enable_esock_extended_error_info" != "xno"; then + AC_DEFINE(ESOCK_USE_EXTENDED_ERROR_INFO, [1], [Use extended error info]) +fi + + dnl *** ESOCK_COUNTER_SIZE *** AC_ARG_WITH(esock-counter-size, @@ -2855,7 +2868,26 @@ AS_IF([test ${enable_jit} != no], JIT_ARCH=x86 ;; arm64) - JIT_ARCH=arm + case "$OPSYS" in + win32|darwin) + # These platforms have dedicated system calls for clearing + # instruction cache, and don't require us to manually issue + # instruction barriers on all threads. + JIT_ARCH=arm + ;; + *) + # We need to use `DC CVAU`, `IC IVAU`, and `ISB SY` to clear + # instruction cache. These have already been tested as part of + # ETHR_CHK_GCC_ATOMIC_OPS([]). + + if test "$ethr_arm_isb_sy_instr_val$ethr_arm_dc_cvau_instr_val$ethr_arm_ic_ivau_instr_val" = "111"; then + JIT_ARCH=arm + else + enable_jit=no + AC_MSG_WARN([JIT disabled due to lack of cache-clearing instructions]) + fi + ;; + esac ;; *) if test ${enable_jit} = yes; then diff --git a/erts/doc/src/notes.xml b/erts/doc/src/notes.xml index f39f020c16..1f372823e8 100644 --- a/erts/doc/src/notes.xml +++ b/erts/doc/src/notes.xml @@ -31,6 +31,102 @@ </header> <p>This document describes the changes made to the ERTS application.</p> +<section><title>Erts 13.2.2</title> + + <section><title>Fixed Bugs and Malfunctions</title> + <list> + <item> + <p> + If a runtime system which was starting the distribution + already had existing pids, ports, or references referring + to a node with the same nodename/creation pair that the + runtime system was about to use, these already existing + pids, ports, or references would not work as expected in + various situations after the node had gone alive. This + could only occur if the runtime system was communicated + such pids, ports, or references prior to the distribution + was started. That is, it was extremely unlikely to happen + unless the distribution was started dynamically and was + even then very unlikely to happen. The runtime system now + checks for already existing pids, ports, and references + with the same nodename/creation pair that it is about to + use. If such are found another creation will be chosen in + order to avoid these issues.</p> + <p> + Own Id: OTP-18570 Aux Id: PR-7190 </p> + </item> + </list> + </section> + +</section> + +<section><title>Erts 13.2.1</title> + + <section><title>Fixed Bugs and Malfunctions</title> + <list> + <item> + <p>Fixed a bug in the loader that prevented certain + modules compiled with <c>no_ssa_opt</c> from being + loaded.</p> + <p> + Own Id: OTP-18519 Aux Id: GH-7024 </p> + </item> + <item> + <p> + Implementations of the <seecref + marker="erts:driver_entry#call"><c>call()</c></seecref> + driver callback that returned a faulty encoded result + could cause a memory leak and could cause invalid data on + the heap of the processes calling <seemfa + marker="erts:erlang#port_call/3"><c>erlang:port_call/3</c></seemfa>.</p> + <p> + Own Id: OTP-18525 Aux Id: PR-7049 </p> + </item> + <item> + <p>Fixed a memory corruption issue when upgrading code. + The bug was introduced in <c>OTP 25.3</c></p> + <p> + Own Id: OTP-18553</p> + </item> + <item> + <p>Fixed configure tests for a few ARM-specific + instructions, which prevented the emulator from being + built on some platforms.</p> + <p> + Own Id: OTP-18554</p> + </item> + <item> + <p> + Aliases created in combination with a monitor using the + <c>{alias, explicit_unalias}</c> option stopped working + from remote nodes when a <c>'DOWN'</c> signal had been + received due to the monitor or if the monitor was removed + using the <c>erlang:demonitor()</c> BIF.</p> + <p> + This bug was introduced in OTP 24.3.4.10 and OTP 25.3.</p> + <p> + Own Id: OTP-18557 Aux Id: PR-7131, OTP-18496 </p> + </item> + <item> + <p>In rare circumstances, bit syntax matching of an + invalid code point for a <c>utf32</c> would crash the + runtime system.</p> + <p> + Own Id: OTP-18560</p> + </item> + <item> + <p> + Building the runtime system failed when native atomic + support was missing. Note that execution on such systems + have only been rudimentary tested.</p> + <p> + Own Id: OTP-18563 Aux Id: GH-7114, PR-7159 </p> + </item> + </list> + </section> + +</section> + <section><title>Erts 13.2</title> <section><title>Fixed Bugs and Malfunctions</title> @@ -1392,6 +1488,64 @@ </section> +<section><title>Erts 12.3.2.11</title> + + <section><title>Fixed Bugs and Malfunctions</title> + <list> + <item> + <p> + Fix bug sometimes causing emulator crash at node shutdown + when there are pending connections. Only seen when + running duel distribution protocols, inet_drv and + inet_tls_dist.</p> + <p> + Own Id: OTP-18243 Aux Id: GH-6247, PR-6258 </p> + </item> + <item> + <p>Fix bug in <c>binary_to_term</c> (and distributed + receive) when decoding a large map (>32 keys) with + unsorted small maps (<= 32) as keys of the large + map.</p> + <p>This was only a problem if the term was encoded by + <c>erl_interface</c>, <c>jinterface</c> or otherwise, as + the VM itself always encodes small maps with sorted + keys.</p> + <p>The "binary_to_term" would appear as successful but + the created large map was internally inconsistent. The + smaller key-maps could not be found with maps:get and + friends. Other operations such as map compare and merge + could probably also give incorrect results.</p> + <p> + Own Id: OTP-18343 Aux Id: GH-6496 </p> + </item> + <item> + <p> + Implementations of the <seecref + marker="erts:driver_entry#call"><c>call()</c></seecref> + driver callback that returned a faulty encoded result + could cause a memory leak and could cause invalid data on + the heap of the processes calling <seemfa + marker="erts:erlang#port_call/3"><c>erlang:port_call/3</c></seemfa>.</p> + <p> + Own Id: OTP-18525 Aux Id: PR-7049 </p> + </item> + <item> + <p> + Aliases created in combination with a monitor using the + <c>{alias, explicit_unalias}</c> option stopped working + from remote nodes when a <c>'DOWN'</c> signal had been + received due to the monitor or if the monitor was removed + using the <c>erlang:demonitor()</c> BIF.</p> + <p> + This bug was introduced in OTP 24.3.4.10 and OTP 25.3.</p> + <p> + Own Id: OTP-18557 Aux Id: PR-7131, OTP-18496 </p> + </item> + </list> + </section> + +</section> + <section><title>Erts 12.3.2.10</title> <section><title>Fixed Bugs and Malfunctions</title> diff --git a/erts/emulator/Makefile.in b/erts/emulator/Makefile.in index d911febb81..5451bdf804 100644 --- a/erts/emulator/Makefile.in +++ b/erts/emulator/Makefile.in @@ -1,7 +1,7 @@ # # %CopyrightBegin% # -# Copyright Ericsson AB 1996-2022. All Rights Reserved. +# Copyright Ericsson AB 1996-2023. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/erts/emulator/beam/beam_file.c b/erts/emulator/beam/beam_file.c index e1215e8640..d210d0fc16 100644 --- a/erts/emulator/beam/beam_file.c +++ b/erts/emulator/beam/beam_file.c @@ -1318,7 +1318,7 @@ int iff_read_chunk(IFF_File *iff, Uint id, IFF_Chunk *chunk) return read_beam_chunks(iff, 1, &id, chunk); } -void beamfile_init() { +void beamfile_init(void) { Eterm suffix; Eterm *hp; diff --git a/erts/emulator/beam/bif.c b/erts/emulator/beam/bif.c index 9ab1be5b90..80eb260153 100644 --- a/erts/emulator/beam/bif.c +++ b/erts/emulator/beam/bif.c @@ -374,6 +374,7 @@ demonitor(Process *c_p, Eterm ref, Eterm *multip) NIL, THE_NON_VALUE); amdp->origin.flags = mon->flags & ERTS_ML_STATE_ALIAS_MASK; + mon->flags &= ~ERTS_ML_STATE_ALIAS_MASK; erts_monitor_tree_replace(&ERTS_P_MONITORS(c_p), mon, &amdp->origin); break; } diff --git a/erts/emulator/beam/code_ix.c b/erts/emulator/beam/code_ix.c index e2e275442e..3888c72363 100644 --- a/erts/emulator/beam/code_ix.c +++ b/erts/emulator/beam/code_ix.c @@ -276,7 +276,7 @@ int erts_try_seize_code_stage_permission(Process* c_p) return try_seize_code_permission(&code_stage_permission, c_p, NULL, NULL); } -void erts_release_code_stage_permission() { +void erts_release_code_stage_permission(void) { release_code_permission(&code_stage_permission); } @@ -304,7 +304,7 @@ int erts_try_seize_code_load_permission(Process* c_p) { return 0; } -void erts_release_code_load_permission() { +void erts_release_code_load_permission(void) { erts_release_code_mod_permission(); erts_release_code_stage_permission(); } @@ -474,7 +474,7 @@ static void schedule_blocking_code_barriers(void *ignored) { } #endif -void erts_blocking_code_barrier() +void erts_blocking_code_barrier(void) { #ifdef DEBUG erts_debug_unrequire_code_barrier(); @@ -487,7 +487,7 @@ void erts_blocking_code_barrier() #endif } -void erts_code_ix_finalize_wait() { +void erts_code_ix_finalize_wait(void) { #ifdef CODE_IX_ISSUE_INSTRUCTION_BARRIERS if (erts_atomic32_read_nob(&outstanding_blocking_code_barriers) != 0) { ERTS_THR_INSTRUCTION_BARRIER; diff --git a/erts/emulator/beam/dist.c b/erts/emulator/beam/dist.c index ca6aa40185..158f7116a2 100644 --- a/erts/emulator/beam/dist.c +++ b/erts/emulator/beam/dist.c @@ -4884,8 +4884,21 @@ BIF_RETTYPE setnode_2(BIF_ALIST_2) erts_thr_progress_block(); success = (!ERTS_PROC_IS_EXITING(net_kernel) - & !ERTS_PROC_GET_DIST_ENTRY(net_kernel)); + && !ERTS_PROC_GET_DIST_ENTRY(net_kernel)); if (success) { + /* + * Ensure we don't use a nodename-creation pair with + * external identifiers existing in the system. + */ + while (!0) { + ErlNode *nep; + if (creation < 4) + creation = 4; + nep = erts_find_node(BIF_ARG_1, creation); + if (!nep || erts_node_refc(nep) == 0) + break; + creation++; + } inc_no_nodes(); erts_set_this_node(BIF_ARG_1, (Uint32) creation); erts_this_dist_entry->creation = creation; @@ -5898,10 +5911,10 @@ BIF_RETTYPE erts_internal_dist_spawn_request_4(BIF_ALIST_4) ok_result = ref; else { Eterm *hp = HAlloc(BIF_P, 3); - Eterm bool = ((monitor_oflags & ERTS_ML_FLG_SPAWN_MONITOR) + Eterm spawns_monitor = ((monitor_oflags & ERTS_ML_FLG_SPAWN_MONITOR) ? am_true : am_false); ASSERT(BIF_ARG_4 == am_spawn_opt); - ok_result = TUPLE2(hp, ref, bool); + ok_result = TUPLE2(hp, ref, spawns_monitor); } code = erts_dsig_prepare(&ctx, dep, diff --git a/erts/emulator/beam/emu/ops.tab b/erts/emulator/beam/emu/ops.tab index 220115cc8a..7c4447d145 100644 --- a/erts/emulator/beam/emu/ops.tab +++ b/erts/emulator/beam/emu/ops.tab @@ -1296,6 +1296,11 @@ bs_skip_utf16 Fail=f Ms=xy Unit=u Flags=u => bs_get_utf16 Fail Ms Unit Flags x i_bs_get_utf16 xy f? t d +bs_get_utf32 Fail=f Ms=xy Live=u Flags=u Dst | equal(Ms, Dst) => + bs_get_integer2 Fail Ms Live i=32 u=1 Flags x | + i_bs_validate_unicode_retract Fail x Ms | + move x Dst + bs_get_utf32 Fail=f Ms=xy Live=u Flags=u Dst=d => bs_get_integer2 Fail Ms Live i=32 u=1 Flags Dst | i_bs_validate_unicode_retract Fail Dst Ms diff --git a/erts/emulator/beam/erl_alloc.c b/erts/emulator/beam/erl_alloc.c index 6e81fafd45..a344b585ae 100644 --- a/erts/emulator/beam/erl_alloc.c +++ b/erts/emulator/beam/erl_alloc.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2002-2022. All Rights Reserved. + * Copyright Ericsson AB 2002-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/erts/emulator/beam/erl_arith.c b/erts/emulator/beam/erl_arith.c index 813f79edaa..9815dd0a6c 100644 --- a/erts/emulator/beam/erl_arith.c +++ b/erts/emulator/beam/erl_arith.c @@ -1310,6 +1310,6 @@ Eterm erts_bnot(Process* p, Eterm arg) } /* Needed to remove compiler optimization */ -double erts_get_positive_zero_float() { +double erts_get_positive_zero_float(void) { return 0.0f; } diff --git a/erts/emulator/beam/erl_async.c b/erts/emulator/beam/erl_async.c index 3e3dc3a29d..1dd6ef18f0 100644 --- a/erts/emulator/beam/erl_async.c +++ b/erts/emulator/beam/erl_async.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2000-2021. All Rights Reserved. + * Copyright Ericsson AB 2000-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -199,7 +199,7 @@ erts_init_async(void) for (i = 0; i < erts_async_max_threads; i++) { ErtsAsyncQ *aq = async_q(i); - erts_snprintf(thr_opts.name, sizeof(thr_name), "async_%d", i+1); + erts_snprintf(thr_opts.name, sizeof(thr_name), "erts_async_%d", i+1); erts_thr_create(&aq->thr_id, async_main, (void*) aq, &thr_opts); } diff --git a/erts/emulator/beam/erl_bif_info.c b/erts/emulator/beam/erl_bif_info.c index 8ba4a7a3ae..4ba368ec7c 100644 --- a/erts/emulator/beam/erl_bif_info.c +++ b/erts/emulator/beam/erl_bif_info.c @@ -4275,6 +4275,13 @@ BIF_RETTYPE erts_debug_get_internal_state_1(BIF_ALIST_1) BIF_RET(am_ok); } #endif + else if (ERTS_IS_ATOM_STR("hashmap_collision_bonanza", BIF_ARG_1)) { +#ifdef DBG_HASHMAP_COLLISION_BONANZA + return am_true; +#else + return am_false; +#endif + } } else if (is_tuple(BIF_ARG_1)) { Eterm* tp = tuple_val(BIF_ARG_1); diff --git a/erts/emulator/beam/erl_db.c b/erts/emulator/beam/erl_db.c index 076a407603..8f5e1a9543 100644 --- a/erts/emulator/beam/erl_db.c +++ b/erts/emulator/beam/erl_db.c @@ -5523,7 +5523,7 @@ erts_db_foreach_thr_prgr_offheap(void (*func)(ErlOffHeap *, void *), /* retrieve max number of ets tables */ Uint -erts_db_get_max_tabs() +erts_db_get_max_tabs(void) { return db_max_tabs; } diff --git a/erts/emulator/beam/erl_db_util.c b/erts/emulator/beam/erl_db_util.c index 38d755f7f6..af5aa09a5c 100644 --- a/erts/emulator/beam/erl_db_util.c +++ b/erts/emulator/beam/erl_db_util.c @@ -947,8 +947,8 @@ static Eterm dmc_lookup_bif_reversed(void *f); static int cmp_uint(void *a, void *b); static int cmp_guard_bif(void *a, void *b); static int match_compact(ErlHeapFragment *expr, DMCErrInfo *err_info); -static Uint my_size_object(Eterm t); -static Eterm my_copy_struct(Eterm t, Eterm **hp, ErlOffHeap* off_heap); +static Uint my_size_object(Eterm t, int is_hashmap_node); +static Eterm my_copy_struct(Eterm t, Eterm **hp, ErlOffHeap* off_heap, int); /* Guard subroutines */ static void @@ -4130,11 +4130,11 @@ static void do_emit_constant(DMCContext *context, DMC_STACK_TYPE(UWord) *text, if (is_immed(t)) { tmp = t; } else { - sz = my_size_object(t); + sz = my_size_object(t, 0); if (sz) { emb = new_message_buffer(sz); hp = emb->mem; - tmp = my_copy_struct(t,&hp,&(emb->off_heap)); + tmp = my_copy_struct(t,&hp,&(emb->off_heap), 0); emb->next = context->save; context->save = emb; } @@ -5672,33 +5672,39 @@ static int match_compact(ErlHeapFragment *expr, DMCErrInfo *err_info) /* ** Simple size object that takes care of function calls and constant tuples */ -static Uint my_size_object(Eterm t) +static Uint my_size_object(Eterm t, int is_hashmap_node) { Uint sum = 0; - Eterm tmp; Eterm *p; switch (t & _TAG_PRIMARY_MASK) { case TAG_PRIMARY_LIST: - sum += 2 + my_size_object(CAR(list_val(t))) + - my_size_object(CDR(list_val(t))); + sum += 2 + my_size_object(CAR(list_val(t)), 0) + + my_size_object(CDR(list_val(t)), 0); break; case TAG_PRIMARY_BOXED: if (is_tuple(t)) { - if (tuple_val(t)[0] == make_arityval(1) && is_tuple(tmp = tuple_val(t)[1])) { - Uint i,n; - p = tuple_val(tmp); - n = arityval(p[0]); - sum += 1 + n; - for (i = 1; i <= n; ++i) - sum += my_size_object(p[i]); - } else if (tuple_val(t)[0] == make_arityval(2) && - is_atom(tmp = tuple_val(t)[1]) && - tmp == am_const) { + Eterm* tpl = tuple_val(t); + Uint i,n; + + if (is_hashmap_node) { + /* hashmap collision node, no matchspec syntax here */ + } + else if (tpl[0] == make_arityval(1) && is_tuple(tpl[1])) { + tpl = tuple_val(tpl[1]); + } + else if (tpl[0] == make_arityval(2) && tpl[1] == am_const) { sum += size_object(tuple_val(t)[2]); - } else { + break; + } + else { erts_exit(ERTS_ERROR_EXIT,"Internal error, sizing unrecognized object in " "(d)ets:match compilation."); } + + n = arityval(tpl[0]); + sum += 1 + n; + for (i = 1; i <= n; ++i) + sum += my_size_object(tpl[i], 0); break; } else if (is_map(t)) { if (is_flatmap(t)) { @@ -5711,7 +5717,7 @@ static Uint my_size_object(Eterm t) n = arityval(p[0]); sum += 1 + n; for (int i = 1; i <= n; ++i) - sum += my_size_object(p[i]); + sum += my_size_object(p[i], 0); /* Calculate size of values */ p = (Eterm *)mp; @@ -5719,18 +5725,19 @@ static Uint my_size_object(Eterm t) sum += n + 3; p += 3; /* hdr + size + keys words */ while (n--) { - sum += my_size_object(*p++); + sum += my_size_object(*p++, 0); } } else { Eterm *head = (Eterm *)hashmap_val(t); Eterm hdr = *head; Uint sz; + sz = hashmap_bitcount(MAP_HEADER_VAL(hdr)); sum += 1 + sz + header_arity(hdr); head += 1 + header_arity(hdr); while(sz-- > 0) { - sum += my_size_object(head[sz]); + sum += my_size_object(head[sz], 1); } } break; @@ -5743,46 +5750,54 @@ static Uint my_size_object(Eterm t) return sum; } -static Eterm my_copy_struct(Eterm t, Eterm **hp, ErlOffHeap* off_heap) +static Eterm my_copy_struct(Eterm t, Eterm **hp, ErlOffHeap* off_heap, + int is_hashmap_node) { Eterm ret = NIL, a, b; Eterm *p; Uint sz; switch (t & _TAG_PRIMARY_MASK) { case TAG_PRIMARY_LIST: - a = my_copy_struct(CAR(list_val(t)), hp, off_heap); - b = my_copy_struct(CDR(list_val(t)), hp, off_heap); + a = my_copy_struct(CAR(list_val(t)), hp, off_heap, 0); + b = my_copy_struct(CDR(list_val(t)), hp, off_heap, 0); ret = CONS(*hp, a, b); *hp += 2; break; case TAG_PRIMARY_BOXED: if (is_tuple(t)) { - if (tuple_val(t)[0] == make_arityval(1) && - is_tuple(a = tuple_val(t)[1])) { - Uint i,n; - p = tuple_val(a); - n = arityval(p[0]); - if (n == 0) { - ret = ERTS_GLOBAL_LIT_EMPTY_TUPLE; - } else { - Eterm *savep = *hp; - ret = make_tuple(savep); - *hp += n + 1; - *savep++ = make_arityval(n); - for(i = 1; i <= n; ++i) - *savep++ = my_copy_struct(p[i], hp, off_heap); - } + Eterm* tpl = tuple_val(t); + Uint i,n; + Eterm *savep; + + if (is_hashmap_node) { + /* hashmap collision node, no matchspec syntax here */ + } + else if (tpl[0] == make_arityval(1) && is_tuple(tpl[1])) { + /* A {{...}} expression */ + tpl = tuple_val(tpl[1]); } - else if (tuple_val(t)[0] == make_arityval(2) && - tuple_val(t)[1] == am_const) { + else if (tpl[0] == make_arityval(2) && tpl[1] == am_const) { /* A {const, XXX} expression */ - b = tuple_val(t)[2]; + b = tpl[2]; sz = size_object(b); ret = copy_struct(b,sz,hp,off_heap); + break; } else { erts_exit(ERTS_ERROR_EXIT, "Trying to constant-copy non constant expression " "0x%bex in (d)ets:match compilation.", t); } + n = arityval(tpl[0]); + if (n == 0) { + ret = ERTS_GLOBAL_LIT_EMPTY_TUPLE; + } else { + savep = *hp; + ret = make_tuple(savep); + *hp += n + 1; + *savep++ = tpl[0]; + for(i = 1; i <= n; ++i) + *savep++ = my_copy_struct(tpl[i], hp, off_heap, 0); + } + } else if (is_map(t)) { if (is_flatmap(t)) { Uint i,n; @@ -5803,7 +5818,7 @@ static Eterm my_copy_struct(Eterm t, Eterm **hp, ErlOffHeap* off_heap) *hp += n + 1; *savep++ = make_arityval(n); for(i = 1; i <= n; ++i) - *savep++ = my_copy_struct(p[i], hp, off_heap); + *savep++ = my_copy_struct(p[i], hp, off_heap, 0); } savep = *hp; ret = make_flatmap(savep); @@ -5815,7 +5830,7 @@ static Eterm my_copy_struct(Eterm t, Eterm **hp, ErlOffHeap* off_heap) *savep++ = keys; p += 3; /* hdr + size + keys words */ for (i = 0; i < n; i++) - *savep++ = my_copy_struct(p[i], hp, off_heap); + *savep++ = my_copy_struct(p[i], hp, off_heap, 0); erts_usort_flatmap((flatmap_t*)flatmap_val(ret)); } else { Eterm *head = hashmap_val(t); @@ -5832,7 +5847,7 @@ static Eterm my_copy_struct(Eterm t, Eterm **hp, ErlOffHeap* off_heap) *savep++ = *head++; /* map size */ for (int i = 0; i < sz; i++) { - *savep++ = my_copy_struct(head[i],hp,off_heap); + *savep++ = my_copy_struct(head[i],hp,off_heap, 1); } } } else { diff --git a/erts/emulator/beam/erl_drv_thread.c b/erts/emulator/beam/erl_drv_thread.c index 949d89232a..c8e6b9906b 100644 --- a/erts/emulator/beam/erl_drv_thread.c +++ b/erts/emulator/beam/erl_drv_thread.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2007-2020. All Rights Reserved. + * Copyright Ericsson AB 2007-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -609,6 +609,7 @@ erl_drv_thread_create(char *name, struct ErlDrvTid_ *dtid; ethr_thr_opts ethr_opts = ETHR_THR_OPTS_DEFAULT_INITER; ethr_thr_opts *use_opts; + char name_buff[ETHR_THR_NAME_MAX + 1]; if (!opts && !name) use_opts = NULL; @@ -616,7 +617,8 @@ erl_drv_thread_create(char *name, if(opts) ethr_opts.suggested_stack_size = opts->suggested_stack_size; - ethr_opts.name = name; + erts_snprintf(name_buff, sizeof(name_buff), "%s", name); + ethr_opts.name = name_buff; use_opts = ðr_opts; } diff --git a/erts/emulator/beam/erl_fun.h b/erts/emulator/beam/erl_fun.h index 43f3ec3b22..a67afaceeb 100644 --- a/erts/emulator/beam/erl_fun.h +++ b/erts/emulator/beam/erl_fun.h @@ -74,14 +74,15 @@ typedef struct erl_fun_thing { /* -- The following may be compound Erlang terms ---------------------- */ Eterm creator; /* Pid of creator process (contains node). */ - Eterm env[1]; /* Environment (free variables). */ + Eterm env[]; /* Environment (free variables). */ } ErlFunThing; #define is_local_fun(FunThing) ((FunThing)->creator != am_external) #define is_external_fun(FunThing) ((FunThing)->creator == am_external) -/* ERL_FUN_SIZE does _not_ include space for the environment */ -#define ERL_FUN_SIZE ((sizeof(ErlFunThing)/sizeof(Eterm))-1) +/* ERL_FUN_SIZE does _not_ include space for the environment which is a + * C99-style flexible array */ +#define ERL_FUN_SIZE ((sizeof(ErlFunThing)/sizeof(Eterm))) ErlFunThing *erts_new_export_fun_thing(Eterm **hpp, Export *exp, int arity); ErlFunThing *erts_new_local_fun_thing(Process *p, diff --git a/erts/emulator/beam/erl_map.c b/erts/emulator/beam/erl_map.c index 8a8265968a..337567406b 100644 --- a/erts/emulator/beam/erl_map.c +++ b/erts/emulator/beam/erl_map.c @@ -95,8 +95,8 @@ static Uint hashmap_subtree_size(Eterm node); static Eterm hashmap_delete(Process *p, Uint32 hx, Eterm key, Eterm node, Eterm *value); static Eterm flatmap_from_validated_list(Process *p, Eterm list, Eterm fill_value, Uint size); static Eterm hashmap_from_unsorted_array(ErtsHeapFactory*, hxnode_t *hxns, Uint n, int reject_dupkeys, ErtsAlcType_t temp_memory_allocator); -static Eterm hashmap_from_sorted_unique_array(ErtsHeapFactory*, hxnode_t *hxns, Uint n, int is_root, ErtsAlcType_t temp_memory_allocator); -static Eterm hashmap_from_chunked_array(ErtsHeapFactory*, hxnode_t *hxns, Uint n, Uint size, int is_root, ErtsAlcType_t temp_memory_allocator); +static Eterm hashmap_from_sorted_unique_array(ErtsHeapFactory*, hxnode_t *hxns, Uint n, ErtsAlcType_t temp_memory_allocator); +static Eterm hashmap_from_chunked_array(ErtsHeapFactory*, hxnode_t *hxns, Uint n, Uint size, ErtsAlcType_t temp_memory_allocator); static Eterm hashmap_info(Process *p, Eterm node); static Eterm hashmap_bld_tuple_uint(Uint **hpp, Uint *szp, Uint n, Uint nums[]); static int hxnodecmp(const void* a, const void* b); @@ -112,8 +112,7 @@ static int hxnodecmpkey(const void* a, const void* b); #define maskval(V,L) (((V) >> ((7 - (L))*4)) & 0xf) #define DBG_PRINT(X) /*erts_printf X*/ -#define HALLOC_EXTRA_HASHMAP_FROM_CHUNKED_ARRAY 200 -#define HALLOC_EXTRA HALLOC_EXTRA_HASHMAP_FROM_CHUNKED_ARRAY +#define HALLOC_EXTRA 200 /* ******************************* * ** Yielding C Fun (YCF) Note ** * ******************************* @@ -135,7 +134,6 @@ static int hxnodecmpkey(const void* a, const void* b); #include "erl_map.ycf.h" #endif #define NOT_YCF_YIELDING_VERSION 1 -#undef HALLOC_EXTRA #define YCF_CONSUME_REDS(X) while(0){} void erts_init_map(void) { @@ -282,6 +280,7 @@ BIF_RETTYPE map_get_2(BIF_ALIST_2) { * means that the code has to follow some restrictions. See note about * YCF near the top of the file for more information. */ + #ifdef INCLUDE_YCF_TRANSFORMED_ONLY_FUNCTIONS static BIF_RETTYPE maps_from_keys_2_helper(Process* p, Eterm* bif_args) { Eterm list = bif_args[0]; @@ -677,33 +676,20 @@ Eterm erts_hashmap_from_array(ErtsHeapFactory* factory, Eterm *leafs, Uint n, static ERTS_INLINE Eterm from_ks_and_vs(ErtsHeapFactory *factory, Eterm *ks, Eterm *vs, - Uint n, Eterm *key_tuple, flatmap_t **fmpp) + Uint n, flatmap_t **fmpp) { if (n <= MAP_SMALL_MAP_LIMIT) { Eterm *hp; flatmap_t *fmp; Eterm keys; - if (key_tuple && is_value(*key_tuple)) { - keys = *key_tuple; - hp = erts_produce_heap(factory, MAP_HEADER_FLATMAP_SZ + n, 0); - ASSERT(is_tuple_arity(keys, n)); - ASSERT(n == 0 || sys_memcmp((void *) (tuple_val(keys) + 1), - (void *) ks, - n * sizeof(Eterm)) == 0); - } - else if (n == 0) { + if (n == 0) { keys = ERTS_GLOBAL_LIT_EMPTY_TUPLE; - if (key_tuple) - *key_tuple = keys; hp = erts_produce_heap(factory, MAP_HEADER_FLATMAP_SZ + n, 0); } else { hp = erts_produce_heap(factory, 1 + MAP_HEADER_FLATMAP_SZ + 2*n, 0); keys = make_tuple(hp); - if (key_tuple) { - *key_tuple = keys; - } *hp++ = make_arityval(n); sys_memcpy((void *) hp, (void *) ks, @@ -720,15 +706,10 @@ from_ks_and_vs(ErtsHeapFactory *factory, Eterm *ks, Eterm *vs, sys_memcpy((void *) hp, (void *) vs, n * sizeof(Eterm)); - if (fmpp) { - *fmpp = fmp; - return THE_NON_VALUE; - } - return make_flatmap(fmp); + *fmpp = fmp; + return THE_NON_VALUE; } else { - if (fmpp) { - *fmpp = NULL; - } + *fmpp = NULL; return erts_hashmap_from_ks_and_vs(factory, ks, vs, n); } } @@ -738,7 +719,7 @@ Eterm erts_map_from_ks_and_vs(ErtsHeapFactory *factory, Eterm *ks, Eterm *vs, Ui Eterm res; flatmap_t *fmp; - res = from_ks_and_vs(factory, ks, vs, n, NULL, &fmp); + res = from_ks_and_vs(factory, ks, vs, n, &fmp); if (fmp) { if (erts_validate_and_sort_flatmap(fmp)) { res = make_flatmap(fmp); @@ -806,14 +787,7 @@ static Eterm hashmap_from_unsorted_array(ErtsHeapFactory* factory, Uint cx; Eterm res; - if (n == 0) { - Eterm *hp; - hp = erts_produce_heap(factory, 2, 0); - hp[0] = MAP_HEADER_HAMT_HEAD_BITMAP(0); - hp[1] = 0; - - return make_hashmap(hp); - } + ASSERT(n > 0); /* sort and compact array (remove non-unique entries) */ erts_qsort(hxns, n, sizeof(hxnode_t), hxnodecmp); @@ -823,7 +797,7 @@ static Eterm hashmap_from_unsorted_array(ErtsHeapFactory* factory, if (hxns[ix].hx == hxns[ix+1].hx) { /* find region of equal hash values */ - jx = ix + 1; + jx = ix + 2; while(jx < n && hxns[ix].hx == hxns[jx].hx) { jx++; } /* find all correct keys from region * (last in list but now hash sorted so we check highest id instead) */ @@ -866,7 +840,8 @@ static Eterm hashmap_from_unsorted_array(ErtsHeapFactory* factory, if (cx > 1) { /* recursive decompose array */ - res = hashmap_from_sorted_unique_array(factory, hxns, cx, 0, temp_memory_allocator); + res = hashmap_from_sorted_unique_array(factory, hxns, cx, + temp_memory_allocator); } else { Eterm *hp; @@ -893,51 +868,49 @@ static Eterm hashmap_from_unsorted_array(ErtsHeapFactory* factory, * YCF near the top of the file for more information. */ static Eterm hashmap_from_sorted_unique_array(ErtsHeapFactory* factory, - hxnode_t *hxns, Uint n, int lvl, + hxnode_t *hxns, Uint n, ErtsAlcType_t temp_memory_allocator) { Eterm res = NIL; - Uint i; Uint ix; - Uint jx; Uint elems; - Uint32 sw; - Uint32 hx; - Eterm val; hxnode_t *tmp = NULL; - ASSERT(lvl < 32); + ix = 0; elems = 1; while (ix < n - 1) { if (hxns[ix].hx == hxns[ix+1].hx) { - jx = ix + 1; - while (jx < n && hxns[ix].hx == hxns[jx].hx) { jx++; } - tmp = (hxnode_t *)erts_alloc(temp_memory_allocator, ((jx - ix)) * sizeof(hxnode_t)); - - for(i = 0; i < jx - ix; i++) { - val = hxns[i + ix].val; - hx = hashmap_restore_hash(lvl + 8, CAR(list_val(val))); - swizzle32(sw,hx); - tmp[i].hx = sw; - tmp[i].val = val; - tmp[i].i = i; - tmp[i].skip = 1; - } + Uint n_colliders; + Eterm* hp; + Eterm collision_node; + Uint jx = ix + 2; + Uint i; + + while (jx < n && hxns[ix].hx == hxns[jx].hx) + jx++; + + n_colliders = jx - ix; + hp = erts_produce_heap(factory, HAMT_COLLISION_NODE_SZ(n_colliders), + HALLOC_EXTRA); + collision_node = make_tuple(hp); + + *hp++ = MAP_HEADER_HAMT_COLLISION_NODE(n_colliders); + for (i = 0; i < n_colliders; i++) { + *hp++ = hxns[ix + i].val; + ASSERT(i == 0 + || CMP_TERM(CAR(list_val(hxns[ix+i-1].val)), + CAR(list_val(hxns[ix+i].val))) < 0); + } - erts_qsort(tmp, jx - ix, sizeof(hxnode_t), hxnodecmp); + hxns[ix].val = collision_node; + hxns[ix].skip = n_colliders; + ix = jx; - hxns[ix].skip = jx - ix; - hxns[ix].val = - hashmap_from_sorted_unique_array(factory, tmp, jx - ix, lvl + 8, temp_memory_allocator); - erts_free(temp_memory_allocator, (void *) tmp); - /* Memory management depend on the statement below */ - tmp = NULL; - ix = jx; - if (ix < n) { elems++; } - continue; + if (ix < n) { elems++; } + continue; } - hxns[ix].skip = 1; - elems++; - ix++; + hxns[ix].skip = 1; + elems++; + ix++; } YCF_SPECIAL_CODE_START(ON_DESTROY_STATE); { @@ -948,14 +921,13 @@ static Eterm hashmap_from_sorted_unique_array(ErtsHeapFactory* factory, } } YCF_SPECIAL_CODE_END(); - res = hashmap_from_chunked_array(factory, hxns, elems, n, !lvl, temp_memory_allocator); + res = hashmap_from_chunked_array(factory, hxns, elems, n, temp_memory_allocator); ERTS_FACTORY_HOLE_CHECK(factory); return res; } -#define HALLOC_EXTRA HALLOC_EXTRA_HASHMAP_FROM_CHUNKED_ARRAY /* **Important Note** * * A yielding version of this function is generated with YCF. This @@ -963,7 +935,7 @@ static Eterm hashmap_from_sorted_unique_array(ErtsHeapFactory* factory, * YCF near the top of the file for more information. */ static Eterm hashmap_from_chunked_array(ErtsHeapFactory *factory, hxnode_t *hxns, Uint n, - Uint size, int is_root, + Uint size, ErtsAlcType_t temp_memory_allocator) { Uint ix; Uint d; @@ -1015,16 +987,11 @@ static Eterm hashmap_from_chunked_array(ErtsHeapFactory *factory, hxnode_t *hxns } slot = maskval(v,0); - hp = erts_produce_heap(factory, (is_root ? 3 : 2), 0); + hp = erts_produce_heap(factory, 3, 0); - if (is_root) { - hp[0] = MAP_HEADER_HAMT_HEAD_BITMAP(1 << slot); - hp[1] = size; - hp[2] = res; - } else { - hp[0] = MAP_HEADER_HAMT_NODE_BITMAP(1 << slot); - hp[1] = res; - } + hp[0] = MAP_HEADER_HAMT_HEAD_BITMAP(1 << slot); + hp[1] = size; + hp[2] = res; return make_hashmap(hp); } @@ -1186,15 +1153,11 @@ static Eterm hashmap_from_chunked_array(ErtsHeapFactory *factory, hxnode_t *hxns bp = 1 << slot; hdr |= bp; sz = hashmap_bitcount(hdr); - hp = erts_produce_heap(factory, sz + /* hdr + item */ (is_root ? 2 : 1), 0); + hp = erts_produce_heap(factory, sz + /* hdr + item */ 2, 0); nhp = hp; - if (is_root) { - *hp++ = (hdr == 0xffff) ? MAP_HEADER_HAMT_HEAD_ARRAY : MAP_HEADER_HAMT_HEAD_BITMAP(hdr); - *hp++ = size; - } else { - *hp++ = MAP_HEADER_HAMT_NODE_BITMAP(hdr); - } + *hp++ = (hdr == 0xffff) ? MAP_HEADER_HAMT_HEAD_ARRAY : MAP_HEADER_HAMT_HEAD_BITMAP(hdr); + *hp++ = size; *hp++ = res; sz--; while (sz--) { *hp++ = ESTACK_POP(stack); } @@ -1206,7 +1169,6 @@ static Eterm hashmap_from_chunked_array(ErtsHeapFactory *factory, hxnode_t *hxns ERTS_FACTORY_HOLE_CHECK(factory); return res; } -#undef HALLOC_EXTRA static int hxnodecmpkey(const void *va, const void *vb) { const hxnode_t *a = (const hxnode_t*) va; @@ -1555,9 +1517,57 @@ BIF_RETTYPE maps_merge_trap_1(BIF_ALIST_1) { (HashmapMergeContext*) ERTS_MAGIC_BIN_DATA(ctx_bin)); } -#define HALLOC_EXTRA 200 #define MAP_MERGE_LOOP_FACTOR 8 +static Eterm merge_collision_node(Process* p, + Eterm* srcA, Uint szA, + Eterm* srcB, Uint szB, + Uint* map_sizep) +{ + Eterm *hp; + Eterm *hdr_ptr; + Eterm *hp_end; + Uint arity; + + ERTS_ASSERT(szA >= 1 && szB >= 1); + arity = szA + szB; + hp = HAlloc(p, HAMT_COLLISION_NODE_SZ(arity)); + hp_end = hp + HAMT_COLLISION_NODE_SZ(arity); + hdr_ptr = hp++; + + while (szA && szB) { + Eterm keyA = CAR(list_val(*srcA)); + Eterm keyB = CAR(list_val(*srcB)); + const Sint key_cmp = CMP_TERM(keyA, keyB); + + if (key_cmp < 0) { + *hp++ = *srcA++; + szA--; + } + else { + *hp++ = *srcB++; + szB--; + if (key_cmp == 0) { + srcA++; + szA--; + arity--; + (*map_sizep)--; + } + } + } + ASSERT(arity >= 2); + + for ( ; szA; szA--) + *hp++ = *srcA++; + for ( ; szB; szB--) + *hp++ = *srcB++; + + HRelease(p, hp_end, hp); + *hdr_ptr = make_arityval(arity); + return make_tuple(hdr_ptr); +} + + static BIF_RETTYPE hashmap_merge(Process *p, Eterm map_A, Eterm map_B, int swap_args, HashmapMergeContext* ctx) { #define PSTACK_TYPE struct HashmapMergePStackType @@ -1571,6 +1581,7 @@ static BIF_RETTYPE hashmap_merge(Process *p, Eterm map_A, Eterm map_B, Eterm trap_ret; Sint initial_reds = (Sint) (ERTS_BIF_REDS_LEFT(p) * MAP_MERGE_LOOP_FACTOR); Sint reds = initial_reds; + Uint coll_szA = 0, coll_szB = 0; /* * Strategy: Do depth-first traversal of both trees (at the same time) @@ -1630,8 +1641,13 @@ recurse: goto merge_nodes; } } - hx = hashmap_restore_hash(ctx->lvl, keyA); - sp->abm = 1 << hashmap_index(hx); + if (ctx->lvl < HAMT_MAX_LEVEL) { + hx = hashmap_restore_hash(ctx->lvl, keyA); + sp->abm = 1 << hashmap_index(hx); + } + else { + coll_szA = 1; + } /* keep srcA pointing at the leaf */ } else { /* A is NODE */ @@ -1640,25 +1656,35 @@ recurse: ASSERT(is_header(hdrA)); switch (hdrA & _HEADER_MAP_SUBTAG_MASK) { case HAMT_SUBTAG_HEAD_ARRAY: { + ASSERT(ctx->lvl < HAMT_MAX_LEVEL); sp->srcA++; sp->abm = 0xffff; break; } case HAMT_SUBTAG_HEAD_BITMAP: sp->srcA++; case HAMT_SUBTAG_NODE_BITMAP: { + ASSERT(ctx->lvl < HAMT_MAX_LEVEL); sp->abm = MAP_HEADER_VAL(hdrA); break; } - default: - erts_exit(ERTS_ABORT_EXIT, "bad header %ld\r\n", hdrA); + default: /* collision node */ + ERTS_ASSERT(is_arity_value(hdrA)); + ASSERT(ctx->lvl == HAMT_MAX_LEVEL); + coll_szA = arityval(hdrA); + ASSERT(coll_szA >= 2); } } if (is_list(sp->nodeB)) { /* B is LEAF */ Eterm keyB = CAR(list_val(sp->nodeB)); - hx = hashmap_restore_hash(ctx->lvl, keyB); - sp->bbm = 1 << hashmap_index(hx); + if (ctx->lvl < HAMT_MAX_LEVEL) { + hx = hashmap_restore_hash(ctx->lvl, keyB); + sp->bbm = 1 << hashmap_index(hx); + } + else { + coll_szB = 1; + } /* keep srcB pointing at the leaf */ } else { /* B is NODE */ @@ -1667,17 +1693,22 @@ recurse: ASSERT(is_header(hdrB)); switch (hdrB & _HEADER_MAP_SUBTAG_MASK) { case HAMT_SUBTAG_HEAD_ARRAY: { + ASSERT(ctx->lvl < HAMT_MAX_LEVEL); sp->srcB++; sp->bbm = 0xffff; break; } case HAMT_SUBTAG_HEAD_BITMAP: sp->srcB++; case HAMT_SUBTAG_NODE_BITMAP: { + ASSERT(ctx->lvl < HAMT_MAX_LEVEL); sp->bbm = MAP_HEADER_VAL(hdrB); break; } - default: - erts_exit(ERTS_ABORT_EXIT, "bad header %ld\r\n", hdrB); + default: /* collision node */ + ERTS_ASSERT(is_arity_value(hdrB)); + ASSERT(ctx->lvl == HAMT_MAX_LEVEL); + coll_szB = arityval(hdrB); + ASSERT(coll_szB >= 2); } } } @@ -1701,13 +1732,22 @@ merge_nodes: sp->srcA++; sp->srcB++; } - } else { /* Start build a node */ + } + else if (ctx->lvl < HAMT_MAX_LEVEL) { /* Start build a node */ sp->ix = 0; sp->rbm = sp->abm | sp->bbm; ASSERT(!(sp->rbm == 0 && ctx->lvl > 0)); } + else { + res = merge_collision_node(p, sp->srcA, coll_szA, + sp->srcB, coll_szB, &ctx->size); + sp->mix = 3; + coll_szA = coll_szB = 0; + continue; + } if (--reds <= 0) { + ASSERT(!coll_szA && !coll_szB); goto trap; } resume_from_trap: @@ -1851,21 +1891,14 @@ static int hash_cmp(Uint32 ha, Uint32 hb) int hashmap_key_hash_cmp(Eterm* ap, Eterm* bp) { - unsigned int lvl = 0; - if (ap && bp) { + Uint32 ha, hb; ASSERT(CMP_TERM(CAR(ap), CAR(bp)) != 0); - for (;;) { - Uint32 ha = hashmap_restore_hash(lvl, CAR(ap)); - Uint32 hb = hashmap_restore_hash(lvl, CAR(bp)); - int cmp = hash_cmp(ha, hb); - if (cmp) { - return cmp; - } - lvl += 8; - } + ha = hashmap_make_hash(CAR(ap)); + hb = hashmap_make_hash(CAR(bp)); + return hash_cmp(ha, hb); } - + ASSERT(ap || bp); return ap ? -1 : 1; } @@ -2300,7 +2333,9 @@ Uint hashmap_node_size(Eterm hdr, Eterm **nodep) ASSERT(sz < 17); break; default: - erts_exit(ERTS_ABORT_EXIT, "bad header"); + ERTS_ASSERT(is_arity_value(hdr)); + sz = arityval(hdr); + break; } return sz; } @@ -2394,7 +2429,7 @@ Eterm* hashmap_iterator_prev(ErtsWStack* s) { const Eterm * erts_hashmap_get(Uint32 hx, Eterm key, Eterm node) { - Eterm *ptr, hdr, *res; + Eterm *ptr, hdr; Uint ix, lvl = 0; Uint32 hval,bp; @@ -2405,15 +2440,16 @@ erts_hashmap_get(Uint32 hx, Eterm key, Eterm node) ASSERT(is_hashmap_header_head(hdr)); ptr++; - for (;;) { + do { + ASSERT(lvl == 0 || is_hashmap_header_node(hdr)); + hval = MAP_HEADER_VAL(hdr); ix = hashmap_index(hx); if (hval != 0xffff) { bp = 1 << ix; if (!(bp & hval)) { /* not occupied */ - res = NULL; - break; + return NULL; } ix = hashmap_bitcount(hval & (bp - 1)); } @@ -2421,8 +2457,7 @@ erts_hashmap_get(Uint32 hx, Eterm key, Eterm node) if (is_list(node)) { /* LEAF NODE [K|V] */ ptr = list_val(node); - res = EQ(CAR(ptr), key) ? &(CDR(ptr)) : NULL; - break; + return EQ(CAR(ptr), key) ? &(CDR(ptr)) : NULL; } hx = hashmap_shift_hash(hx,lvl,key); @@ -2431,10 +2466,17 @@ erts_hashmap_get(Uint32 hx, Eterm key, Eterm node) ptr = boxed_val(node); hdr = *ptr; ASSERT(is_header(hdr)); - ASSERT(!is_hashmap_header_head(hdr)); - } + } while (!is_arity_value(hdr)); - return res; + /* collision node */ + ix = arityval(hdr); + ASSERT(ix > 1); + do { + Eterm* kv = list_val(*(++ptr)); + if (EQ(CAR(kv), key)) + return &(CDR(kv)); + } while (--ix > 0); + return NULL; } Eterm erts_hashmap_insert(Process *p, Uint32 hx, Eterm key, Eterm value, @@ -2448,6 +2490,8 @@ Eterm erts_hashmap_insert(Process *p, Uint32 hx, Eterm key, Eterm value, /* We are putting a new value (under a new or existing key) */ hp = HAlloc(p, size); res = erts_hashmap_insert_up(hp, key, value, upsz, &stack); + ASSERT(hashmap_val(res) + 2 + hashmap_bitcount(MAP_HEADER_VAL(*hashmap_val(res))) + == hp + size); } else { /* We are putting the same key-value */ @@ -2555,9 +2599,34 @@ int erts_hashmap_insert_down(Uint32 hx, Eterm key, Eterm value, Eterm node, Uint } size += HAMT_HEAD_BITMAP_SZ(n+1); goto unroll; - default: - erts_exit(ERTS_ERROR_EXIT, "bad header tag %ld\r\n", hdr & _HEADER_MAP_SUBTAG_MASK); - break; + default: + ERTS_ASSERT(is_arity_value(hdr)); + n = arityval(hdr); + ASSERT(n >= 2); + for (slot = 0; slot < n; slot++) { + Eterm* kv = list_val(ptr[1+slot]); + Sint c; + ckey = CAR(kv); + c = CMP_TERM(key, ckey); + if (c == 0) { + if (CDR(ptr) == value) { + *sz = 0; + return 1; + } + *update_size = 0; + size += HAMT_COLLISION_NODE_SZ(n); + ESTACK_PUSH3(*sp, slot, 0, node); + goto unroll; + } + if (c < 0) + break; + } + if (is_update) { + return 0; + } + size += HAMT_COLLISION_NODE_SZ(n+1); + ESTACK_PUSH3(*sp, slot, 1, node); + goto unroll; } break; default: @@ -2566,21 +2635,25 @@ int erts_hashmap_insert_down(Uint32 hx, Eterm key, Eterm value, Eterm node, Uint } } insert_subnodes: - clvl = lvl; - chx = hashmap_restore_hash(clvl,ckey); - size += HAMT_NODE_BITMAP_SZ(2); - ix = hashmap_index(hx); - cix = hashmap_index(chx); - - while (cix == ix) { - ESTACK_PUSH4(*sp, 0, 1 << ix, 0, MAP_HEADER_HAMT_NODE_BITMAP(0)); - size += HAMT_NODE_BITMAP_SZ(1); - hx = hashmap_shift_hash(hx,lvl,key); - chx = hashmap_shift_hash(chx,clvl,ckey); - ix = hashmap_index(hx); - cix = hashmap_index(chx); - } - ESTACK_PUSH3(*sp, cix, ix, node); + if (lvl < HAMT_MAX_LEVEL) { + clvl = lvl; + chx = hashmap_restore_hash(clvl,ckey); + do { + ix = hashmap_index(hx); + cix = hashmap_index(chx); + if (cix != ix) { + size += HAMT_NODE_BITMAP_SZ(2); + ESTACK_PUSH4(*sp, cix, ix, 0, node); + goto unroll; + } + ESTACK_PUSH4(*sp, 0, 1 << ix, 0, MAP_HEADER_HAMT_NODE_BITMAP(0)); + size += HAMT_NODE_BITMAP_SZ(1); + hx = hashmap_shift_hash(hx,lvl,key); + chx = hashmap_shift_hash(chx,clvl,ckey); + } while (lvl < HAMT_MAX_LEVEL); + } + size += HAMT_COLLISION_NODE_SZ(2); + ESTACK_PUSH2(*sp, 1, node); unroll: *sz = size + /* res cons */ 2; @@ -2594,17 +2667,29 @@ Eterm erts_hashmap_insert_up(Eterm *hp, Eterm key, Eterm value, Eterm *nhp = NULL; Uint32 ix, cix, bp, hval; Uint slot, n; - /* Needed for halfword */ - DeclareTmpHeapNoproc(fake,1); - UseTmpHeapNoproc(1); + Eterm fake; res = CONS(hp, key, value); hp += 2; do { node = ESTACK_POP(*sp); switch(primary_tag(node)) { - case TAG_PRIMARY_LIST: - ix = (Uint32) ESTACK_POP(*sp); + case TAG_PRIMARY_LIST: { + const int is_collision_node = (int) ESTACK_POP(*sp); + if (is_collision_node) { + nhp = hp; + *hp++ = MAP_HEADER_HAMT_COLLISION_NODE(2); + if (CMP_TERM(key, CAR(list_val(node))) < 0){ + *hp++ = res; + *hp++ = node; + } else { + *hp++ = node; + *hp++ = res; + } + res = make_hashmap(nhp); + break; + } + ix = (Uint32)ESTACK_POP(*sp); cix = (Uint32) ESTACK_POP(*sp); nhp = hp; @@ -2618,10 +2703,11 @@ Eterm erts_hashmap_insert_up(Eterm *hp, Eterm key, Eterm value, } res = make_hashmap(nhp); break; + } case TAG_PRIMARY_HEADER: /* subnodes, fake it */ - *fake = node; - node = make_boxed(fake); + fake = node; + node = make_boxed(&fake); case TAG_PRIMARY_BOXED: ptr = boxed_val(node); hdr = *ptr; @@ -2674,9 +2760,27 @@ Eterm erts_hashmap_insert_up(Eterm *hp, Eterm key, Eterm value, } res = make_hashmap(nhp); break; - default: - erts_exit(ERTS_ERROR_EXIT, "bad header tag %x\r\n", hdr & _HEADER_MAP_SUBTAG_MASK); - break; + default: { + int is_insert; + ERTS_ASSERT(is_arity_value(hdr)); + n = arityval(hdr); + ASSERT(n >= 2); + is_insert = (int) ESTACK_POP(*sp); + slot = (Uint) ESTACK_POP(*sp); + nhp = hp; + n += is_insert; + *hp++ = MAP_HEADER_HAMT_COLLISION_NODE(n); ptr++; + ix = 0; + while (ix++ < slot) + *hp++ = *ptr++; + *hp++ = res; + if (!is_insert) + ptr++; + while (ix++ < n) + *hp++ = *ptr++; + res = make_hashmap(nhp); + break; + } } break; default: @@ -2855,9 +2959,22 @@ static Eterm hashmap_delete(Process *p, Uint32 hx, Eterm key, /* not occupied */ res = THE_NON_VALUE; goto not_found; - default: - erts_exit(ERTS_ERROR_EXIT, "bad header tag %ld\r\n", hdr & _HEADER_MAP_SUBTAG_MASK); - break; + default: /* collision node */ + ERTS_ASSERT(is_arity_value(hdr)); + n = arityval(hdr); + ASSERT(n >= 2); + for (slot = 0; slot < n; slot++) { + Eterm* kv = list_val(ptr[1+slot]); + if (EQ(key, CAR(kv))) { + if (value) + *value = CDR(kv); + ESTACK_PUSH2(stack, slot, node); + size += HAMT_COLLISION_NODE_SZ(n); + goto unroll; + } + } + res = THE_NON_VALUE; + goto not_found; } break; default: @@ -3039,8 +3156,25 @@ unroll: } res = make_hashmap(nhp); break; - default: - erts_exit(ERTS_ERROR_EXIT, "bad header tag %x\r\n", hdr & _HEADER_MAP_SUBTAG_MASK); + default: /* collision node */ + ERTS_ASSERT(is_arity_value(hdr)); + n = arityval(hdr); + ASSERT(n >= 2); + slot = (Uint) ESTACK_POP(stack); + ASSERT(slot < n); + if (n > 2) { /* Shrink collision node */ + nhp = hp; + *hp++ = MAP_HEADER_HAMT_COLLISION_NODE(n-1); ptr++; + n -= slot + 1; + while (slot--) { *hp++ = *ptr++; } + ptr++; + while(n--) { *hp++ = *ptr++; } + res = make_hashmap(nhp); + } + else { /* Collapse collision node */ + ASSERT(res == THE_NON_VALUE); + res = ptr[1 + (1-slot)]; + } break; } } while(!ESTACK_ISEMPTY(stack)); @@ -3315,8 +3449,10 @@ BIF_RETTYPE erts_internal_map_hashmap_children_1(BIF_ALIST_1) { sz = 16; ptr += 2; break; - default: - erts_exit(ERTS_ERROR_EXIT, "bad header\r\n"); + default: /* collision node */ + ERTS_ASSERT(is_arity_value(hdr)); + sz = arityval(hdr); + ASSERT(sz >= 2); break; } ASSERT(sz < 17); @@ -3338,15 +3474,17 @@ static Eterm hashmap_info(Process *p, Eterm node) { DECL_AM(leafs); DECL_AM(bitmaps); DECL_AM(arrays); - Uint nleaf=0, nbitmap=0, narray=0; - Uint bitmap_usage[16], leaf_usage[16]; - Uint lvl = 0, clvl; + DECL_AM(collisions); + Uint nleaf=0, nbitmap=0, narray=0, ncollision = 0; + Uint bitmap_usage[16]; + Uint collision_usage[16]; + Uint leaf_usage[HAMT_MAX_LEVEL + 2]; + Uint max_depth = 0, clvl; DECLARE_ESTACK(stack); - for (sz = 0; sz < 16; sz++) { - bitmap_usage[sz] = 0; - leaf_usage[sz] = 0; - } + sys_memzero(bitmap_usage, sizeof(bitmap_usage)); + sys_memzero(collision_usage, sizeof(collision_usage)); + sys_memzero(leaf_usage, sizeof(leaf_usage)); ptr = boxed_val(node); ESTACK_PUSH(stack, 0); @@ -3354,8 +3492,6 @@ static Eterm hashmap_info(Process *p, Eterm node) { do { node = ESTACK_POP(stack); clvl = ESTACK_POP(stack); - if (lvl < clvl) - lvl = clvl; switch(primary_tag(node)) { case TAG_PRIMARY_LIST: nleaf++; @@ -3371,45 +3507,49 @@ static Eterm hashmap_info(Process *p, Eterm node) { sz = hashmap_bitcount(MAP_HEADER_VAL(hdr)); ASSERT(sz < 17); bitmap_usage[sz-1] += 1; - while(sz--) { - ESTACK_PUSH(stack, clvl + 1); - ESTACK_PUSH(stack, ptr[sz+1]); - } break; case HAMT_SUBTAG_HEAD_BITMAP: nbitmap++; sz = hashmap_bitcount(MAP_HEADER_VAL(hdr)); bitmap_usage[sz-1] += 1; - while(sz--) { - ESTACK_PUSH(stack, clvl + 1); - ESTACK_PUSH(stack, ptr[sz+2]); - } + ptr++; break; case HAMT_SUBTAG_HEAD_ARRAY: narray++; sz = 16; - while(sz--) { - ESTACK_PUSH(stack, clvl + 1); - ESTACK_PUSH(stack, ptr[sz+2]); - } - break; - default: - erts_exit(ERTS_ERROR_EXIT, "bad header\r\n"); + ptr++; break; + default: /* collision node */ + ERTS_ASSERT(is_arity_value(hdr)); + ncollision++; + sz = arityval(hdr); + ASSERT(sz >= 2); + collision_usage[(sz > 16 ? 16 : sz) - 1] += 1; + break; } + ASSERT(sz >= 1); + clvl++; + ASSERT(clvl <= HAMT_MAX_LEVEL+1); + if (max_depth < clvl) + max_depth = clvl; + while(sz--) { + ESTACK_PUSH(stack, clvl); + ESTACK_PUSH(stack, ptr[sz+1]); + } } } while(!ESTACK_ISEMPTY(stack)); /* size */ sz = 0; - hashmap_bld_tuple_uint(NULL,&sz,16,leaf_usage); - hashmap_bld_tuple_uint(NULL,&sz,16,bitmap_usage); + hashmap_bld_tuple_uint(NULL, &sz, HAMT_MAX_LEVEL+2, leaf_usage); + hashmap_bld_tuple_uint(NULL, &sz, 16, bitmap_usage); + hashmap_bld_tuple_uint(NULL, &sz, 16, collision_usage); /* alloc */ - hp = HAlloc(p, 2+3 + 3*(2+4) + sz); + hp = HAlloc(p, 2+3 + 4*(2+4) + sz); - info = hashmap_bld_tuple_uint(&hp,NULL,16,leaf_usage); + info = hashmap_bld_tuple_uint(&hp, NULL, HAMT_MAX_LEVEL+2, leaf_usage); tup = TUPLE3(hp, AM_leafs, make_small(nleaf),info); hp += 4; res = CONS(hp, tup, res); hp += 2; @@ -3417,10 +3557,14 @@ static Eterm hashmap_info(Process *p, Eterm node) { tup = TUPLE3(hp, AM_bitmaps, make_small(nbitmap), info); hp += 4; res = CONS(hp, tup, res); hp += 2; + info = hashmap_bld_tuple_uint(&hp, NULL, 16, collision_usage); + tup = TUPLE3(hp, AM_collisions, make_small(ncollision), info); hp += 4; + res = CONS(hp, tup, res); hp += 2; + tup = TUPLE3(hp, AM_arrays, make_small(narray),NIL); hp += 4; res = CONS(hp, tup, res); hp += 2; - tup = TUPLE2(hp, AM_depth, make_small(lvl)); hp += 3; + tup = TUPLE2(hp, AM_depth, make_small(max_depth)); hp += 3; res = CONS(hp, tup, res); hp += 2; DESTROY_ESTACK(stack); @@ -3448,12 +3592,16 @@ static Eterm hashmap_bld_tuple_uint(Uint **hpp, Uint *szp, Uint n, Uint nums[]) * Since each hashmap node can only be up to 16 elements * large we use 4 bits per level in the path. * - * So a Path with value 0x110 will first get the 0:th + * So a Path with value 0x210 will first get the 0:th * slot in the head node, and then the 1:st slot in the - * resulting node and then finally the 1:st slot in the + * resulting node and then finally the 2:st slot in the * node beneath. If that slot is not a leaf, then the path * continues down the 0:th slot until it finds a leaf. * + * Collision nodes may (theoretically and in debug) have more + * than 16 elements. To not complicate the 4-bit path format + * we avoid yielding in collision nodes. + * * Once the leaf has been found, the return value is created * by traversing the tree using the stack that was built * when searching for the first leaf to return. @@ -3596,7 +3744,12 @@ BIF_RETTYPE erts_internal_map_next_3(BIF_ALIST_3) { Uint path_length = 0; Uint *path_rest = NULL; int i, elems, orig_elems; - Eterm node = map, res, *patch_ptr = NULL, *hp; + Eterm node = map, res, *patch_ptr = NULL; + Eterm *hp = NULL; + Eterm *hp_end; + Eterm *ptr; + Uint sz, words_per_elem; + Uint idx; /* A stack WSTACK is used when traversing the hashmap. * It contains: node, idx, sz, ptr @@ -3654,61 +3807,29 @@ BIF_RETTYPE erts_internal_map_next_3(BIF_ALIST_3) { BIF_ERROR(BIF_P, BADARG); } - if (type == iterator) { - /* - * Iterator uses the format {K1, V1, {K2, V2, {K3, V3, [Path | Map]}}}, - * so each element is 4 words large. - * To make iteration order independent of input reductions - * the KV-pairs are here built in DESTRUCTIVE non-reverse order. - */ - hp = HAlloc(BIF_P, 4 * elems); - } else { - /* - * List used the format [Path, Map, {K3,V3}, {K2,V2}, {K1,V1} | BIF_ARG_3], - * so each element is 2+3 words large. - * To make list order independent of input reductions - * the KV-pairs are here built in FUNCTIONAL reverse order - * as this is how the list as a whole is constructed. - */ - hp = HAlloc(BIF_P, (2 + 3) * elems); - } - - orig_elems = elems; - /* First we look for the leaf to start at using the path given. While doing so, we push each map node and the index onto the stack to use later. */ for (i = 1; ; i++) { - Eterm *ptr = hashmap_val(node), - hdr = *ptr++; - Uint sz; + Eterm hdr; + + ptr = hashmap_val(node); + hdr = *ptr++; sz = hashmap_node_size(hdr, &ptr); - if (PATH_ELEM(curr_path) >= sz) + idx = PATH_ELEM(curr_path); + if (idx >= sz) goto badarg; - WSTACK_PUSH4(stack, node, PATH_ELEM(curr_path)+1, sz, (UWord)ptr); - - /* We have found a leaf, return it and the next X elements */ - if (is_list(ptr[PATH_ELEM(curr_path)])) { - Eterm *lst = list_val(ptr[PATH_ELEM(curr_path)]); - if (type == iterator) { - res = make_tuple(hp); - hp[0] = make_arityval(3); - hp[1] = CAR(lst); - hp[2] = CDR(lst); - patch_ptr = &hp[3]; - hp += 4; - } else { - Eterm tup = TUPLE2(hp, CAR(lst), CDR(lst)); hp += 3; - res = CONS(hp, tup, BIF_ARG_3); hp += 2; - } - elems--; + if (is_list(ptr[idx])) { + /* We have found a leaf, return it and the next X elements */ break; } - node = ptr[PATH_ELEM(curr_path)]; + WSTACK_PUSH4(stack, node, idx+1, sz, (UWord)ptr); + + node = ptr[idx]; curr_path >>= PATH_ELEM_SIZE; @@ -3726,12 +3847,50 @@ BIF_RETTYPE erts_internal_map_next_3(BIF_ALIST_3) { } } + if (type == iterator) { + /* + * Iterator uses the format {K1, V1, {K2, V2, {K3, V3, [Path | Map]}}}, + * so each element is 4 words large. + * To make iteration order independent of input reductions + * the KV-pairs are here built in DESTRUCTIVE non-reverse order. + */ + words_per_elem = 4; + patch_ptr = &res; + } else { + /* + * List used the format [Path, Map, {K3,V3}, {K2,V2}, {K1,V1} | BIF_ARG_3], + * so each element is 2+3 words large. + * To make list order independent of input reductions + * the KV-pairs are here built in FUNCTIONAL reverse order + * as this is how the list as a whole is constructed. + */ + words_per_elem = 2 + 3; + res = BIF_ARG_3; + } + hp = HAlloc(BIF_P, words_per_elem * elems); + hp_end = hp + words_per_elem * elems; + + orig_elems = elems; + /* We traverse the hashmap and return at most `elems` elements */ while(1) { - Eterm *ptr = (Eterm*)WSTACK_POP(stack); - Uint sz = (Uint)WSTACK_POP(stack); - Uint idx = (Uint)WSTACK_POP(stack); - Eterm node = (Eterm)WSTACK_POP(stack); + + if (idx == 0) { + if (elems < sz && is_arity_value(*hashmap_val(node))) { + /* + * This is a collision node! + * Make sure 'elems' is large enough not to yield in the + * middle of it. Collision nodes may be larger than 16 + * and that would complicate the 4-bit path format. + */ + elems = sz; + HRelease(BIF_P, hp_end, hp); + hp = HAlloc(BIF_P, words_per_elem * elems); + hp_end = hp + words_per_elem * elems; + } + } + else + ASSERT(!is_arity_value(*hashmap_val(node))); while (idx < sz && elems != 0 && is_list(ptr[idx])) { Eterm *lst = list_val(ptr[idx]); @@ -3750,6 +3909,8 @@ BIF_RETTYPE erts_internal_map_next_3(BIF_ALIST_3) { idx++; } + ASSERT(idx == sz || !is_arity_value(*hashmap_val(node))); + if (elems == 0) { if (idx < sz) { /* There are more elements in this node to explore */ @@ -3768,26 +3929,29 @@ BIF_RETTYPE erts_internal_map_next_3(BIF_ALIST_3) { } } break; - } else { - if (idx < sz) { - Eterm hdr; - /* Push next idx in current node */ - WSTACK_PUSH4(stack, node, idx+1, sz, (UWord)ptr); - - /* Push first idx in child node */ - node = ptr[idx]; - ptr = hashmap_val(ptr[idx]); - hdr = *ptr++; - sz = hashmap_node_size(hdr, &ptr); - WSTACK_PUSH4(stack, node, 0, sz, (UWord)ptr); - } } - - /* There are no more element in the hashmap */ - if (WSTACK_ISEMPTY(stack)) { + else if (idx < sz) { + Eterm hdr; + /* Push next idx in current node */ + WSTACK_PUSH4(stack, node, idx+1, sz, (UWord)ptr); + + /* Continue with first idx in child node */ + node = ptr[idx]; + ptr = hashmap_val(ptr[idx]); + hdr = *ptr++; + sz = hashmap_node_size(hdr, &ptr); + idx = 0; + } + else if (!WSTACK_ISEMPTY(stack)) { + ptr = (Eterm*)WSTACK_POP(stack); + sz = (Uint)WSTACK_POP(stack); + idx = (Uint)WSTACK_POP(stack); + node = (Eterm)WSTACK_POP(stack); + } + else { + /* There are no more element in the hashmap */ break; } - } if (!WSTACK_ISEMPTY(stack)) { @@ -3846,24 +4010,16 @@ BIF_RETTYPE erts_internal_map_next_3(BIF_ALIST_3) { res = CONS(hp, path, res); hp += 2; } } else { - if (type == iterator) { + if (type == iterator) *patch_ptr = am_none; - HRelease(BIF_P, hp + 4 * elems, hp); - } else { - HRelease(BIF_P, hp + (2+3) * elems, hp); - } + HRelease(BIF_P, hp_end, hp); } BIF_P->fcalls -= 4 * (orig_elems - elems); DESTROY_WSTACK(stack); BIF_RET(res); badarg: - if (type == iterator) { - HRelease(BIF_P, hp + 4 * elems, hp); - } else { - HRelease(BIF_P, hp + (2+3) * elems, hp); - } - BIF_P->fcalls -= 4 * (orig_elems - elems); + ASSERT(hp == NULL); DESTROY_WSTACK(stack); BIF_ERROR(BIF_P, BADARG); } diff --git a/erts/emulator/beam/erl_map.h b/erts/emulator/beam/erl_map.h index d3a023bc07..05d09557e7 100644 --- a/erts/emulator/beam/erl_map.h +++ b/erts/emulator/beam/erl_map.h @@ -56,17 +56,15 @@ typedef struct flatmap_s { /* the head-node is a bitmap or array with an untagged size */ #define hashmap_size(x) (((hashmap_head_t*) hashmap_val(x))->size) -#define hashmap_make_hash(Key) make_map_hash(Key, 0) +#define hashmap_make_hash(Key) make_map_hash(Key) #define hashmap_restore_hash(Lvl, Key) \ - (((Lvl) < 8) ? \ - hashmap_make_hash(Key) >> (4*(Lvl)) : \ - make_map_hash(Key, ((Lvl) >> 3)) >> (4 * ((Lvl) & 7))) + (ASSERT(Lvl < 8), \ + hashmap_make_hash(Key) >> (4*(Lvl))) #define hashmap_shift_hash(Hx, Lvl, Key) \ - (((++(Lvl)) & 7) ? \ - (Hx) >> 4 : \ - make_map_hash(Key, ((Lvl) >> 3))) + (++(Lvl), ASSERT(Lvl <= HAMT_MAX_LEVEL), /* we allow one level too much */\ + (Hx) >> 4) /* erl_term.h stuff */ #define flatmap_get_values(x) (((Eterm *)(x)) + sizeof(flatmap_t)/sizeof(Eterm)) @@ -149,7 +147,8 @@ typedef struct hashmap_head_s { /* erl_map.h stuff */ -#define is_hashmap_header_head(x) ((MAP_HEADER_TYPE(x) & (0x2))) +#define is_hashmap_header_head(x) (MAP_HEADER_TYPE(x) & (0x2)) +#define is_hashmap_header_node(x) (MAP_HEADER_TYPE(x) == 1) #define MAKE_MAP_HEADER(Type,Arity,Val) \ (_make_header(((((Uint16)(Val)) << MAP_HEADER_ARITY_SZ) | (Arity)) << MAP_HEADER_TAG_SZ | (Type) , _TAG_HEADER_MAP)) @@ -166,12 +165,21 @@ typedef struct hashmap_head_s { #define MAP_HEADER_HAMT_NODE_BITMAP(Bmp) \ MAKE_MAP_HEADER(MAP_HEADER_TAG_HAMT_NODE_BITMAP,0x0,Bmp) +#define MAP_HEADER_HAMT_COLLISION_NODE(Arity) make_arityval(Arity) + #define MAP_HEADER_FLATMAP_SZ (sizeof(flatmap_t) / sizeof(Eterm)) #define HAMT_NODE_ARRAY_SZ (17) #define HAMT_HEAD_ARRAY_SZ (18) #define HAMT_NODE_BITMAP_SZ(n) (1 + n) #define HAMT_HEAD_BITMAP_SZ(n) (2 + n) +#define HAMT_COLLISION_NODE_SZ(n) (1 + n) +/* + * Collision nodes are used when all hash bits have been exhausted. + * They are normal tuples of arity 2 or larger. The elements of a collision + * node tuple contain key-value cons cells like the other nodes, + * but they are sorted in map-key order. + */ /* 2 bits maps tag + 4 bits subtag + 2 ignore bits */ #define _HEADER_MAP_SUBTAG_MASK (0xfc) @@ -185,11 +193,17 @@ typedef struct hashmap_head_s { #define hashmap_index(hash) (((Uint32)hash) & 0xf) +#define HAMT_MAX_LEVEL 8 + /* hashmap heap size: [one cons cell + one list term in parent node] per key [one header + one boxed term in parent node] per inner node [one header + one size word] for root node Observed average number of nodes per key is about 0.35. + + Amendment: This size estimation does not take collision nodes into account. + It should be good enough though, as collision nodes are rare + and only make the size smaller compared to unlimited HAMT depth. */ #define HASHMAP_WORDS_PER_KEY 3 #define HASHMAP_WORDS_PER_NODE 2 diff --git a/erts/emulator/beam/erl_md5.c b/erts/emulator/beam/erl_md5.c index f4b6301b5c..2a4e026d9c 100644 --- a/erts/emulator/beam/erl_md5.c +++ b/erts/emulator/beam/erl_md5.c @@ -129,10 +129,9 @@ void MD5Init(MD5_CTX* context) * operation, processing another message block, and updating the * context. */ -void MD5Update (context, input, inputLen) - MD5_CTX *context; /* context */ - unsigned char *input; /* input block */ - unsigned int inputLen; /* length of input block */ +void MD5Update (MD5_CTX *context, + unsigned char *input, /* input block */ + unsigned int inputLen) /* length of input block */ { unsigned int i, index, partLen; @@ -175,9 +174,8 @@ void MD5Update (context, input, inputLen) * MD5 finalization. Ends an MD5 message-digest operation, writing the message digest and zeroizing the context. */ -void MD5Final (digest, context) - unsigned char digest[16]; /* message digest */ - MD5_CTX *context; /* context */ +void MD5Final (unsigned char digest[16], /* message digest */ + MD5_CTX *context) /* context */ { unsigned char bits[8]; unsigned int index, padLen; @@ -213,9 +211,7 @@ void MD5Final (digest, context) /* * MD5 basic transformation. Transforms state based on block. */ -static void MD5Transform (state, block) - Uint32 state[4]; - unsigned char block[64]; +static void MD5Transform (Uint32 state[4], unsigned char block[64]) { Uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; @@ -308,10 +304,7 @@ static void MD5Transform (state, block) * Encodes input (Uint32) into output (unsigned char). Assumes len is * a multiple of 4. */ -static void Encode (output, input, len) - unsigned char *output; - Uint32 *input; - unsigned int len; +static void Encode (unsigned char *output, Uint32 *input, unsigned int len) { unsigned int i, j; @@ -327,10 +320,7 @@ static void Encode (output, input, len) * Decodes input (unsigned char) into output (Uint32). Assumes len is * a multiple of 4. */ -static void Decode (output, input, len) - Uint32 *output; - unsigned char *input; - unsigned int len; +static void Decode (Uint32 *output, unsigned char *input, unsigned int len) { unsigned int i, j; diff --git a/erts/emulator/beam/erl_monitor_link.c b/erts/emulator/beam/erl_monitor_link.c index 2c7b765520..0c1eef68f0 100644 --- a/erts/emulator/beam/erl_monitor_link.c +++ b/erts/emulator/beam/erl_monitor_link.c @@ -703,10 +703,15 @@ erts_debug_monitor_tree_destroying_foreach(ErtsMonitor *root, void *arg, void *vysp) { - void *tmp_vysp = erts_alloc(ERTS_ALC_T_ML_YIELD_STATE, - sizeof(ErtsMonLnkYieldState)); + void *tmp_vysp; Sint reds; - sys_memcpy(tmp_vysp, tmp_vysp, sizeof(ErtsMonLnkYieldState)); + if (!vysp) + tmp_vysp = NULL; + else { + tmp_vysp = erts_alloc(ERTS_ALC_T_ML_YIELD_STATE, + sizeof(ErtsMonLnkYieldState)); + sys_memcpy(tmp_vysp, tmp_vysp, sizeof(ErtsMonLnkYieldState)); + } do { reds = ml_rbt_foreach_yielding((ErtsMonLnkNode *) root, (ErtsMonLnkNodeFunc) func, @@ -1338,10 +1343,15 @@ erts_debug_link_tree_destroying_foreach(ErtsLink *root, void *arg, void *vysp) { - void *tmp_vysp = erts_alloc(ERTS_ALC_T_ML_YIELD_STATE, - sizeof(ErtsMonLnkYieldState)); + void *tmp_vysp; Sint reds; - sys_memcpy(tmp_vysp, vysp, sizeof(ErtsMonLnkYieldState)); + if (!vysp) + tmp_vysp = NULL; + else { + tmp_vysp = erts_alloc(ERTS_ALC_T_ML_YIELD_STATE, + sizeof(ErtsMonLnkYieldState)); + sys_memcpy(tmp_vysp, vysp, sizeof(ErtsMonLnkYieldState)); + } do { reds = ml_rbt_foreach_yielding((ErtsMonLnkNode *) root, (ErtsMonLnkNodeFunc) func, diff --git a/erts/emulator/beam/erl_nif.c b/erts/emulator/beam/erl_nif.c index 086dfa95ad..a9661dc780 100644 --- a/erts/emulator/beam/erl_nif.c +++ b/erts/emulator/beam/erl_nif.c @@ -2922,7 +2922,7 @@ void erts_nif_demonitored(ErtsResource* resource) ASSERT(resource->type->fn.down); erts_mtx_lock(&rmp->lock); - free_me = ((rmon_refc_dec_read(rmp) == 0) & !!rmon_is_dying(rmp)); + free_me = ((rmon_refc_dec_read(rmp) == 0) && !!rmon_is_dying(rmp)); erts_mtx_unlock(&rmp->lock); if (free_me) @@ -5265,7 +5265,7 @@ erts_unload_nif(struct erl_module_nif* lib) deref_nifmod(lib); } -void erl_nif_init() +void erl_nif_init(void) { ERTS_CT_ASSERT((offsetof(ErtsResource,data) % 8) == ERTS_MAGIC_BIN_BYTES_TO_ALIGN); diff --git a/erts/emulator/beam/erl_node_tables.c b/erts/emulator/beam/erl_node_tables.c index 2bec8ff20e..dac24d0310 100644 --- a/erts/emulator/beam/erl_node_tables.c +++ b/erts/emulator/beam/erl_node_tables.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2001-2022. All Rights Reserved. + * Copyright Ericsson AB 2001-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -885,6 +885,18 @@ erts_node_table_info(fmtfn_t to, void *to_arg) erts_rwmtx_runlock(&erts_node_table_rwmtx); } +ErlNode *erts_find_node(Eterm sysname, Uint32 creation) +{ + ErlNode *res; + ErlNode ne; + ne.sysname = sysname; + ne.creation = creation; + + erts_rwmtx_rlock(&erts_node_table_rwmtx); + res = hash_get(&erts_node_table, (void *) &ne); + erts_rwmtx_runlock(&erts_node_table_rwmtx); + return res; +} ErlNode *erts_find_or_insert_node(Eterm sysname, Uint32 creation, Eterm book) { diff --git a/erts/emulator/beam/erl_node_tables.h b/erts/emulator/beam/erl_node_tables.h index c12198a23c..56696586c6 100644 --- a/erts/emulator/beam/erl_node_tables.h +++ b/erts/emulator/beam/erl_node_tables.h @@ -255,6 +255,7 @@ void erts_set_dist_entry_not_connected(DistEntry *); void erts_set_dist_entry_pending(DistEntry *); void erts_set_dist_entry_connected(DistEntry *, Eterm, Uint64); ErlNode *erts_find_or_insert_node(Eterm, Uint32, Eterm); +ErlNode *erts_find_node(Eterm, Uint32); void erts_schedule_delete_node(ErlNode *); void erts_set_this_node(Eterm, Uint32); Uint erts_node_table_size(void); @@ -282,6 +283,7 @@ ERTS_GLB_INLINE void erts_deref_node_entry__(ErlNode *np, Eterm term, char *file ERTS_GLB_INLINE erts_aint_t erts_ref_node_entry(ErlNode *np, int min_val, Eterm term); ERTS_GLB_INLINE void erts_deref_node_entry(ErlNode *np, Eterm term); #endif +ERTS_GLB_INLINE erts_aint_t erts_node_refc(ErlNode *np); ERTS_GLB_INLINE void erts_de_rlock(DistEntry *dep); ERTS_GLB_INLINE void erts_de_runlock(DistEntry *dep); ERTS_GLB_INLINE void erts_de_rwlock(DistEntry *dep); @@ -332,6 +334,12 @@ erts_deref_node_entry(ErlNode *np, Eterm term) erts_schedule_delete_node(np); } +ERTS_GLB_INLINE erts_aint_t +erts_node_refc(ErlNode *np) +{ + return erts_refc_read(&np->refc, 0); +} + #endif ERTS_GLB_INLINE void diff --git a/erts/emulator/beam/erl_posix_str.c b/erts/emulator/beam/erl_posix_str.c index 5b515d6e78..c57c269095 100644 --- a/erts/emulator/beam/erl_posix_str.c +++ b/erts/emulator/beam/erl_posix_str.c @@ -47,8 +47,7 @@ */ char * -erl_errno_id(error) - int error; /* Posix error number (as from errno). */ +erl_errno_id(int error /* Posix error number (as from errno). */) { switch (error) { #ifdef E2BIG diff --git a/erts/emulator/beam/erl_printf_term.c b/erts/emulator/beam/erl_printf_term.c index bb8a422343..18858a21bc 100644 --- a/erts/emulator/beam/erl_printf_term.c +++ b/erts/emulator/beam/erl_printf_term.c @@ -722,71 +722,46 @@ print_term(fmtfn_t fn, void* arg, Eterm obj, long *dcount) { } } else { Uint n, mapval; + Eterm* assoc; + Eterm key, val; + mapval = MAP_HEADER_VAL(*head); switch (MAP_HEADER_TYPE(*head)) { case MAP_HEADER_TAG_HAMT_HEAD_ARRAY: case MAP_HEADER_TAG_HAMT_HEAD_BITMAP: PRINT_STRING(res, fn, arg, "#{"); WSTACK_PUSH(s, PRT_CLOSE_TUPLE); - n = hashmap_bitcount(mapval); - ASSERT(n < 17); - head += 2; - if (n > 0) { - Eterm* assoc; - Eterm key, val; - n--; - if (is_list(head[n])) { - assoc = list_val(head[n]); - key = CAR(assoc); - val = CDR(assoc); - WSTACK_PUSH5(s, val, PRT_TERM, PRT_ASSOC, key, PRT_TERM); - } else { - WSTACK_PUSH(s, head[n]); - WSTACK_PUSH(s, PRT_TERM); - } - while (n--) { - if (is_list(head[n])) { - assoc = list_val(head[n]); - key = CAR(assoc); - val = CDR(assoc); - WSTACK_PUSH6(s, PRT_COMMA, val, PRT_TERM, PRT_ASSOC, key, PRT_TERM); - } else { - WSTACK_PUSH(s, PRT_COMMA); - WSTACK_PUSH(s, head[n]); - WSTACK_PUSH(s, PRT_TERM); - } - } - } - break; + head++; + /* fall through */ case MAP_HEADER_TAG_HAMT_NODE_BITMAP: n = hashmap_bitcount(mapval); - head++; - ASSERT(n < 17); - if (n > 0) { - Eterm* assoc; - Eterm key, val; - n--; + ASSERT(0 < n && n < 17); + while (1) { if (is_list(head[n])) { assoc = list_val(head[n]); key = CAR(assoc); val = CDR(assoc); WSTACK_PUSH5(s, val, PRT_TERM, PRT_ASSOC, key, PRT_TERM); - } else { - WSTACK_PUSH(s, head[n]); - WSTACK_PUSH(s, PRT_TERM); } - while (n--) { - if (is_list(head[n])) { - assoc = list_val(head[n]); + else if (is_tuple(head[n])) { /* collision node */ + Eterm *tpl = tuple_val(head[n]); + Uint arity = arityval(tpl[0]); + ASSERT(arity >= 2); + while (1) { + assoc = list_val(tpl[arity]); key = CAR(assoc); val = CDR(assoc); - WSTACK_PUSH6(s, PRT_COMMA, val, PRT_TERM, PRT_ASSOC, key, PRT_TERM); - } else { + WSTACK_PUSH5(s, val, PRT_TERM, PRT_ASSOC, key, PRT_TERM); + if (--arity == 0) + break; WSTACK_PUSH(s, PRT_COMMA); - WSTACK_PUSH(s, head[n]); - WSTACK_PUSH(s, PRT_TERM); } + } else { + WSTACK_PUSH2(s, head[n], PRT_TERM); } + if (--n == 0) + break; + WSTACK_PUSH(s, PRT_COMMA); } break; } diff --git a/erts/emulator/beam/erl_proc_sig_queue.c b/erts/emulator/beam/erl_proc_sig_queue.c index e444a4cdca..ee1d345fb3 100644 --- a/erts/emulator/beam/erl_proc_sig_queue.c +++ b/erts/emulator/beam/erl_proc_sig_queue.c @@ -5626,6 +5626,7 @@ erts_proc_sig_handle_incoming(Process *c_p, erts_aint32_t *statep, mdp->ref, c_p->common.id, NIL, NIL, THE_NON_VALUE); amdp->origin.flags = ERTS_ML_STATE_ALIAS_UNALIAS; + omon->flags &= ~ERTS_ML_STATE_ALIAS_MASK; erts_monitor_tree_replace(&ERTS_P_MONITORS(c_p), omon, &amdp->origin); diff --git a/erts/emulator/beam/erl_process.c b/erts/emulator/beam/erl_process.c index d4502a8b1a..7a89df49e0 100644 --- a/erts/emulator/beam/erl_process.c +++ b/erts/emulator/beam/erl_process.c @@ -7397,7 +7397,7 @@ static ERTS_INLINE int have_dirty_work(void) { return !(ERTS_EMPTY_RUNQ(ERTS_DIRTY_CPU_RUNQ) - | ERTS_EMPTY_RUNQ(ERTS_DIRTY_IO_RUNQ)); + || ERTS_EMPTY_RUNQ(ERTS_DIRTY_IO_RUNQ)); } #define ERTS_MSB_NONE_PRIO_BIT PORT_BIT @@ -8781,7 +8781,7 @@ erts_start_schedulers(void) { ethr_tid tid; int res = 0; - char name[32]; + char name[ETHR_THR_NAME_MAX + 1]; ethr_thr_opts opts = ETHR_THR_OPTS_DEFAULT_INITER; int ix; @@ -8791,7 +8791,7 @@ erts_start_schedulers(void) if (erts_runq_supervision_interval) { opts.suggested_stack_size = 16; - erts_snprintf(opts.name, sizeof(name), "runq_supervisor"); + erts_snprintf(opts.name, sizeof(name), "erts_runq_sup"); erts_atomic_init_nob(&runq_supervisor_sleeping, 0); if (0 != ethr_event_init(&runq_supervision_event)) erts_exit(ERTS_ABORT_EXIT, "Failed to create run-queue supervision event\n"); @@ -8812,7 +8812,7 @@ erts_start_schedulers(void) for (ix = 0; ix < erts_no_schedulers; ix++) { ErtsSchedulerData *esdp = ERTS_SCHEDULER_IX(ix); ASSERT(ix == esdp->no - 1); - erts_snprintf(opts.name, sizeof(name), "%lu_scheduler", ix + 1); + erts_snprintf(opts.name, sizeof(name), "erts_sched_%d", ix + 1); res = ethr_thr_create(&esdp->tid, sched_thread_func, (void*)esdp, &opts); if (res != 0) { erts_exit(ERTS_ABORT_EXIT, "Failed to create scheduler thread %d, error = %d\n", ix, res); @@ -8826,7 +8826,7 @@ erts_start_schedulers(void) { for (ix = 0; ix < erts_no_dirty_cpu_schedulers; ix++) { ErtsSchedulerData *esdp = ERTS_DIRTY_CPU_SCHEDULER_IX(ix); - erts_snprintf(opts.name, sizeof(name), "%d_dirty_cpu_scheduler", ix + 1); + erts_snprintf(opts.name, sizeof(name), "erts_dcpus_%d", ix + 1); opts.suggested_stack_size = erts_dcpu_sched_thread_suggested_stack_size; res = ethr_thr_create(&esdp->tid,sched_dirty_cpu_thread_func,(void*)esdp,&opts); if (res != 0) @@ -8834,7 +8834,7 @@ erts_start_schedulers(void) } for (ix = 0; ix < erts_no_dirty_io_schedulers; ix++) { ErtsSchedulerData *esdp = ERTS_DIRTY_IO_SCHEDULER_IX(ix); - erts_snprintf(opts.name, sizeof(name), "%d_dirty_io_scheduler", ix + 1); + erts_snprintf(opts.name, sizeof(name), "erts_dios_%d", ix + 1); opts.suggested_stack_size = erts_dio_sched_thread_suggested_stack_size; res = ethr_thr_create(&esdp->tid,sched_dirty_io_thread_func,(void*)esdp,&opts); if (res != 0) @@ -8845,7 +8845,7 @@ erts_start_schedulers(void) ix = 0; while (ix < erts_no_aux_work_threads) { int id = ix == 0 ? 1 : ix + 1 - (int) erts_no_schedulers; - erts_snprintf(opts.name, sizeof(name), "%d_aux", id); + erts_snprintf(opts.name, sizeof(name), "erts_aux_%d", id); res = ethr_thr_create(&tid, aux_thread, (void *) (Sint) ix, &opts); if (res != 0) @@ -8872,7 +8872,7 @@ erts_start_schedulers(void) bpt->blocked = 0; bpt->id = ix; - erts_snprintf(opts.name, sizeof(name), "%d_poller", ix); + erts_snprintf(opts.name, sizeof(name), "erts_poll_%d", ix); res = ethr_thr_create(&tid, poll_thread, (void*) bpt, &opts); if (res != 0) diff --git a/erts/emulator/beam/erl_process_lock.h b/erts/emulator/beam/erl_process_lock.h index 3c8428c5b9..76e8616280 100644 --- a/erts/emulator/beam/erl_process_lock.h +++ b/erts/emulator/beam/erl_process_lock.h @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2007-2021. All Rights Reserved. + * Copyright Ericsson AB 2007-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -991,6 +991,7 @@ erts_proc_lock_wait_until_released(Process *p, ErtsProcLocks locks) #if ERTS_PROC_LOCK_OWN_IMPL #if !ERTS_PROC_LOCK_ATOMIC_IMPL Uint32 was_locked; + erts_pix_lock_t *pix_lck = ERTS_PID2PIXLOCK(p->common.id); erts_pix_lock(pix_lck); was_locked = (ERTS_PROC_LOCK_FLGS_READ_(&p->lock) & locks); erts_pix_unlock(pix_lck); diff --git a/erts/emulator/beam/erl_term.h b/erts/emulator/beam/erl_term.h index cff38badf7..03211a6acd 100644 --- a/erts/emulator/beam/erl_term.h +++ b/erts/emulator/beam/erl_term.h @@ -331,7 +331,7 @@ _ET_DECLARE_CHECKED(Uint,header_arity,Eterm) #define is_sane_arity_value(x) ((((x) & _TAG_HEADER_MASK) == _TAG_HEADER_ARITYVAL) && \ (((x) >> _HEADER_ARITY_OFFS) <= MAX_ARITYVAL)) #define is_not_arity_value(x) (!is_arity_value((x))) -#define _unchecked_arityval(x) _unchecked_header_arity((x)) +#define _unchecked_arityval(x) ((x) >> _HEADER_ARITY_OFFS) _ET_DECLARE_CHECKED(Uint,arityval,Eterm) #define arityval(x) _ET_APPLY(arityval,(x)) @@ -1268,15 +1268,12 @@ _ET_DECLARE_CHECKED(struct erl_node_*,external_ref_node,Eterm) #define MAP_SZ(sz) (MAP_HEADER_FLATMAP_SZ + 2*sz + 1) -#define MAP0_SZ MAP_SZ(0) #define MAP1_SZ MAP_SZ(1) #define MAP2_SZ MAP_SZ(2) #define MAP3_SZ MAP_SZ(3) #define MAP4_SZ MAP_SZ(4) #define MAP5_SZ MAP_SZ(5) -#define MAP0(hp) \ - (MAP_HEADER(hp, 0, TUPLE0(hp+MAP_HEADER_FLATMAP_SZ)), \ - make_flatmap(hp)) + #define MAP1(hp, k1, v1) \ (MAP_HEADER(hp, 1, TUPLE1(hp+1+MAP_HEADER_FLATMAP_SZ, k1)), \ (hp)[MAP_HEADER_FLATMAP_SZ+0] = v1, \ diff --git a/erts/emulator/beam/erl_term_hashing.c b/erts/emulator/beam/erl_term_hashing.c index 52c36503ee..848757c2f2 100644 --- a/erts/emulator/beam/erl_term_hashing.c +++ b/erts/emulator/beam/erl_term_hashing.c @@ -1141,7 +1141,18 @@ make_hash2_helper(Eterm term_param, const int can_trap, Eterm* state_mref_write_ } else { ASSERT(is_boxed(*ctx.ptr)); - ESTACK_PUSH(s, *ctx.ptr); + if (is_tuple(*ctx.ptr)) { /* collision node */ + Eterm *coll_ptr = tuple_val(*ctx.ptr); + Uint n = arityval(*coll_ptr); + ASSERT(n >= 2); + coll_ptr++; + for (; n; n--, coll_ptr++) { + Eterm* cons = list_val(*coll_ptr); + ESTACK_PUSH3(s, HASH_MAP_PAIR, CDR(cons), CAR(cons)); + } + } + else + ESTACK_PUSH(s, *ctx.ptr); } ctx.i--; ctx.ptr++; TRAP_LOCATION(map_subtag); @@ -1543,12 +1554,10 @@ trapping_make_hash2(Eterm term, Eterm* state_mref_write_back, Process* p) * with a new ErlNode struct, externals from that node will hash different than * before. * - * One IMPORTANT property must hold (for hamt). - * EVERY BIT of the term that is significant for equality (see EQ) - * MUST BE USED AS INPUT FOR THE HASH. Two different terms must always have a - * chance of hashing different when salted. - * - * This is why we cannot use cached hash values for atoms for example. + * The property "EVERY BIT of the term that is significant for equality + * MUST BE USED AS INPUT FOR THE HASH" is nice but no longer crucial for the + * hashmap implementation that now uses collision nodes at the bottom of + * the HAMT when all hash bits are exhausted. * */ @@ -1716,6 +1725,8 @@ make_internal_hash(Eterm term, Uint32 salt) } else { ASSERT(is_boxed(*ptr)); + /* no special treatment of collision nodes needed, + hash them as the tuples they are */ ESTACK_PUSH(s, *ptr); } i--; ptr++; @@ -1951,15 +1962,43 @@ make_internal_hash(Eterm term, Uint32 salt) } -/* Term hash function for maps, with a separate depth parameter */ -Uint32 make_map_hash(Eterm key, int depth) { - Uint32 hash = 0; - - if (depth > 0) { - UINT32_HASH_2(depth, 1, HCONST_22); +#ifdef DBG_HASHMAP_COLLISION_BONANZA +Uint32 erts_dbg_hashmap_collision_bonanza(Uint32 hash, Eterm key) +{ +/*{ + static Uint32 hashvec[7] = { + 0x02345678, + 0x12345678, + 0xe2345678, + 0xf2345678, + 0x12abcdef, + 0x13abcdef, + 0xcafebabe + }; + hash = hashvec[hash % (sizeof(hashvec) / sizeof(hashvec[0]))]; + }*/ + const Uint32 bad_hash = (hash & 0x12482481) * 1442968193; + const Uint32 bad_bits = hash % 67; + if (bad_bits < 32) { + /* Mix in a number of high good bits to get "randomly" close + to the collision nodes */ + const Uint32 bad_mask = (1 << bad_bits) - 1; + return (hash & ~bad_mask) | (bad_hash & bad_mask); } + return bad_hash; +} +#endif - return make_internal_hash(key, hash); +/* Term hash function for hashmaps */ +Uint32 make_map_hash(Eterm key) { + Uint32 hash; + + hash = make_internal_hash(key, 0); + +#ifdef DBG_HASHMAP_COLLISION_BONANZA + hash = erts_dbg_hashmap_collision_bonanza(hash, key); +#endif + return hash; } #undef CONST_HASH diff --git a/erts/emulator/beam/erl_term_hashing.h b/erts/emulator/beam/erl_term_hashing.h index ec4a49359d..8a898b7c52 100644 --- a/erts/emulator/beam/erl_term_hashing.h +++ b/erts/emulator/beam/erl_term_hashing.h @@ -53,7 +53,13 @@ Uint32 make_hash2(Eterm); Uint32 trapping_make_hash2(Eterm, Eterm*, struct process*); Uint32 make_hash(Eterm); Uint32 make_internal_hash(Eterm, Uint32 salt); -Uint32 make_map_hash(Eterm key, int level); +#ifdef DEBUG +# define DBG_HASHMAP_COLLISION_BONANZA +#endif +#ifdef DBG_HASHMAP_COLLISION_BONANZA +Uint32 erts_dbg_hashmap_collision_bonanza(Uint32 hash, Eterm key); +#endif +Uint32 make_map_hash(Eterm key); void erts_block_hash_init(ErtsBlockHashState *state, const byte *ptr, Uint len, diff --git a/erts/emulator/beam/erl_threads.h b/erts/emulator/beam/erl_threads.h index d3639e1135..ec43772bca 100644 --- a/erts/emulator/beam/erl_threads.h +++ b/erts/emulator/beam/erl_threads.h @@ -430,6 +430,7 @@ ERTS_GLB_INLINE void erts_thr_exit(void *res); ERTS_GLB_INLINE void erts_thr_install_exit_handler(void (*exit_handler)(void)); ERTS_GLB_INLINE erts_tid_t erts_thr_self(void); ERTS_GLB_INLINE int erts_thr_getname(erts_tid_t tid, char *buf, size_t len); +ERTS_GLB_INLINE void erts_thr_setname(char *buf); ERTS_GLB_INLINE int erts_equal_tids(erts_tid_t x, erts_tid_t y); ERTS_GLB_INLINE void erts_mtx_init(erts_mtx_t *mtx, const char *name, @@ -1623,6 +1624,13 @@ erts_thr_getname(erts_tid_t tid, char *buf, size_t len) return ethr_getname(tid, buf, len); } +ERTS_GLB_INLINE void +erts_thr_setname(char *buf) +{ + if (strlen(buf) > ETHR_THR_NAME_MAX) + erts_thr_fatal_error(EINVAL, "too long thread name"); + ethr_setname(buf); +} ERTS_GLB_INLINE int erts_equal_tids(erts_tid_t x, erts_tid_t y) diff --git a/erts/emulator/beam/erl_trace.c b/erts/emulator/beam/erl_trace.c index 82d4cf728c..2dd6c99d4c 100644 --- a/erts/emulator/beam/erl_trace.c +++ b/erts/emulator/beam/erl_trace.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 1999-2022. All Rights Reserved. + * Copyright Ericsson AB 1999-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -2473,7 +2473,7 @@ init_sys_msg_dispatcher(void) { erts_thr_opts_t thr_opts = ERTS_THR_OPTS_DEFAULT_INITER; thr_opts.detached = 1; - thr_opts.name = "sys_msg_dispatcher"; + thr_opts.name = "erts_smsg_disp"; init_smq_element_alloc(); sys_message_queue = NULL; sys_message_queue_end = NULL; @@ -3079,7 +3079,7 @@ erts_tracer_update(ErtsTracer *tracer, const ErtsTracer new_tracer) } } -static void init_tracer_nif() +static void init_tracer_nif(void) { erts_rwmtx_opt_t rwmtx_opt = ERTS_RWMTX_OPT_DEFAULT_INITER; rwmtx_opt.type = ERTS_RWMTX_TYPE_EXTREMELY_FREQUENT_READ; @@ -3092,7 +3092,7 @@ static void init_tracer_nif() } -int erts_tracer_nif_clear() +int erts_tracer_nif_clear(void) { erts_rwmtx_rlock(&tracer_mtx); diff --git a/erts/emulator/beam/erl_vm.h b/erts/emulator/beam/erl_vm.h index 3c7a6054c5..6d5b7c6e1b 100644 --- a/erts/emulator/beam/erl_vm.h +++ b/erts/emulator/beam/erl_vm.h @@ -323,7 +323,7 @@ extern void** beam_ops; #if ERTS_GLB_INLINE_INCL_FUNC_DEF ERTS_GLB_INLINE -int erts_cp_size() +int erts_cp_size(void) { if (erts_frame_layout == ERTS_FRAME_LAYOUT_RA) { return 1; diff --git a/erts/emulator/beam/external.c b/erts/emulator/beam/external.c index a380454699..081ce23e49 100644 --- a/erts/emulator/beam/external.c +++ b/erts/emulator/beam/external.c @@ -3116,7 +3116,6 @@ dec_pid(ErtsDistExternal *edep, ErtsHeapFactory* factory, const byte* ep, #define ENC_BIN_COPY ((Eterm) 3) #define ENC_MAP_PAIR ((Eterm) 4) #define ENC_HASHMAP_NODE ((Eterm) 5) -#define ENC_STORE_MAP_ELEMENT ((Eterm) 6) #define ENC_START_SORTING_MAP ((Eterm) 7) #define ENC_CONTINUE_SORTING_MAP ((Eterm) 8) #define ENC_PUSH_SORTED_MAP ((Eterm) 9) @@ -3315,18 +3314,32 @@ enc_term_int(TTBEncodeContext* ctx, ErtsAtomCacheMap *acmp, Eterm obj, byte* ep, case ENC_HASHMAP_NODE: if (is_list(obj)) { /* leaf node [K|V] */ ptr = list_val(obj); + if (dflags & DFLAG_DETERMINISTIC) { + *next_map_element++ = CAR(ptr); + *next_map_element++ = CDR(ptr); + goto outer_loop; + } WSTACK_PUSH2(s, ENC_TERM, CDR(ptr)); obj = CAR(ptr); } - break; - case ENC_STORE_MAP_ELEMENT: /* option `deterministic` */ - if (is_list(obj)) { /* leaf node [K|V] */ - ptr = list_val(obj); - *next_map_element++ = CAR(ptr); - *next_map_element++ = CDR(ptr); + else if (is_tuple(obj)) { /* collision node */ + Uint tpl_sz; + ptr = tuple_val(obj); + tpl_sz = arityval(*ptr); + ASSERT(tpl_sz >= 2); + ptr++; + WSTACK_RESERVE(s, tpl_sz * 2); + while(tpl_sz--) { + ASSERT(is_list(*ptr)); + WSTACK_FAST_PUSH(s, ENC_HASHMAP_NODE); + WSTACK_FAST_PUSH(s, *ptr++); + } goto outer_loop; - } - break; + } + else + ASSERT((*boxed_val(obj) & _HEADER_MAP_SUBTAG_MASK) + == HAMT_SUBTAG_NODE_BITMAP); + break; case ENC_START_SORTING_MAP: /* option `deterministic` */ { long num_reductions = r; @@ -3654,7 +3667,6 @@ enc_term_int(TTBEncodeContext* ctx, ErtsAtomCacheMap *acmp, Eterm obj, byte* ep, } else { Eterm hdr; Uint node_sz; - Eterm node_processor; ptr = boxed_val(obj); hdr = *ptr; ASSERT(is_header(hdr)); @@ -3695,17 +3707,14 @@ enc_term_int(TTBEncodeContext* ctx, ErtsAtomCacheMap *acmp, Eterm obj, byte* ep, node_sz = hashmap_bitcount(MAP_HEADER_VAL(hdr)); ASSERT(node_sz < 17); break; - default: - erts_exit(ERTS_ERROR_EXIT, "bad header\r\n"); - } - + default: + erts_exit(ERTS_ERROR_EXIT, "bad header\r\n"); + } ptr++; - node_processor = (dflags & DFLAG_DETERMINISTIC) ? - ENC_STORE_MAP_ELEMENT : ENC_HASHMAP_NODE; WSTACK_RESERVE(s, node_sz*2); while(node_sz--) { - WSTACK_FAST_PUSH(s, node_processor); - WSTACK_FAST_PUSH(s, *ptr++); + WSTACK_FAST_PUSH(s, ENC_HASHMAP_NODE); + WSTACK_FAST_PUSH(s, *ptr++); } } break; @@ -5459,8 +5468,8 @@ encode_size_struct_int(TTBSizeContext* ctx, ErtsAtomCacheMap *acmp, Eterm obj, node_sz = hashmap_bitcount(MAP_HEADER_VAL(hdr)); ASSERT(node_sz < 17); break; - default: - erts_exit(ERTS_ERROR_EXIT, "bad header\r\n"); + default: + erts_exit(ERTS_ERROR_EXIT, "bad header\r\n"); } ptr++; diff --git a/erts/emulator/beam/hash.c b/erts/emulator/beam/hash.c index 434ba25098..5c8b43e6e2 100644 --- a/erts/emulator/beam/hash.c +++ b/erts/emulator/beam/hash.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 1996-2021. All Rights Reserved. + * Copyright Ericsson AB 1996-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,6 +64,7 @@ void hash_get_info(HashInfo *hi, Hash *h) } } ASSERT(objects == h->nobjs); + (void)objects; hi->name = h->name; hi->size = hash_get_slots(h); diff --git a/erts/emulator/beam/io.c b/erts/emulator/beam/io.c index b024782073..a79bce3d77 100644 --- a/erts/emulator/beam/io.c +++ b/erts/emulator/beam/io.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 1996-2022. All Rights Reserved. + * Copyright Ericsson AB 1996-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -3126,8 +3126,7 @@ void erts_lcnt_update_port_locks(int enable) { * Parameters: * bufsiz - The (maximum) size of the line buffer. */ -LineBuf *allocate_linebuf(bufsiz) -int bufsiz; +LineBuf *allocate_linebuf(int bufsiz) { int ovsiz = (bufsiz < LINEBUF_INITIAL) ? bufsiz : LINEBUF_INITIAL; LineBuf *lb = (LineBuf *) erts_alloc(ERTS_ALC_T_LINEBUF, diff --git a/erts/emulator/beam/jit/arm/instr_map.cpp b/erts/emulator/beam/jit/arm/instr_map.cpp index b751436881..8d7ad6f45f 100644 --- a/erts/emulator/beam/jit/arm/instr_map.cpp +++ b/erts/emulator/beam/jit/arm/instr_map.cpp @@ -30,13 +30,10 @@ extern "C" } static const Uint32 INTERNAL_HASH_SALT = 3432918353; -static const Uint32 HCONST_22 = 0x98C475E6UL; static const Uint32 HCONST = 0x9E3779B9; -/* ARG3 = incoming hash - * ARG6 = lower 32 +/* ARG6 = lower 32 * ARG7 = upper 32 - * ARG8 = type constant * * Helper function for calculating the internal hash of keys before looking * them up in a map. @@ -50,6 +47,9 @@ void BeamGlobalAssembler::emit_internal_hash_helper() { a64::Gp hash = ARG3.w(), lower = ARG6.w(), upper = ARG7.w(), constant = ARG8.w(); + mov_imm(hash, INTERNAL_HASH_SALT); + mov_imm(constant, HCONST); + a.add(lower, lower, constant); a.add(upper, upper, constant); @@ -82,6 +82,24 @@ void BeamGlobalAssembler::emit_internal_hash_helper() { } #endif +#ifdef DBG_HASHMAP_COLLISION_BONANZA + emit_enter_runtime_frame(); + emit_enter_runtime(); + + a.stp(ARG1, ARG2, TMP_MEM1q); + a.str(ARG4, TMP_MEM3q); + + a.mov(ARG1, ARG3); + runtime_call<2>(erts_dbg_hashmap_collision_bonanza); + a.mov(ARG3, ARG1); + + a.ldp(ARG1, ARG2, TMP_MEM1q); + a.ldr(ARG4, TMP_MEM3q); + + emit_leave_runtime(); + emit_leave_runtime_frame(); +#endif + a.ret(a64::x30); } @@ -95,7 +113,7 @@ void BeamGlobalAssembler::emit_hashmap_get_element() { Label node_loop = a.newLabel(); arm::Gp node = ARG1, key = ARG2, key_hash = ARG3, header_val = ARG4, - depth = TMP4, index = TMP5; + depth = TMP5, index = TMP6; const int header_shift = (_HEADER_ARITY_OFFS + MAP_HEADER_TAG_SZ + MAP_HEADER_ARITY_SZ); @@ -107,8 +125,9 @@ void BeamGlobalAssembler::emit_hashmap_get_element() { a.bind(node_loop); { - Label fail = a.newLabel(), leaf_node = a.newLabel(), - skip_index_adjustment = a.newLabel(), update_hash = a.newLabel(); + Label done = a.newLabel(), leaf_node = a.newLabel(), + skip_index_adjustment = a.newLabel(), + collision_node = a.newLabel(); /* Find out which child we should follow, and shift the hash for the * next round. */ @@ -128,7 +147,7 @@ void BeamGlobalAssembler::emit_hashmap_get_element() { * Note that we jump directly to the return sequence as ZF is clear * at this point. */ a.lsr(TMP1, header_val, index); - a.tbz(TMP1, imm(0), fail); + a.tbz(TMP1, imm(0), done); /* The actual offset of our entry is the number of bits set (in * essence "entries present") before our index in the bitmap. Clear @@ -152,11 +171,11 @@ void BeamGlobalAssembler::emit_hashmap_get_element() { * word. */ a.ldr(header_val, arm::Mem(node).post(sizeof(Eterm))); - /* After 8 nodes we've run out of the 32 bits we started with, so we - * need to update the hash to keep going. */ - a.tst(depth, imm(0x7)); - a.b_eq(update_hash); - a.b(node_loop); + /* After 8 nodes we've run out of the 32 bits we started with + * and we end up in a collision node. */ + a.cmp(depth, imm(HAMT_MAX_LEVEL)); + a.b_ne(node_loop); + a.b(collision_node); a.bind(leaf_node); { @@ -166,36 +185,33 @@ void BeamGlobalAssembler::emit_hashmap_get_element() { a.cmp(TMP1, key); /* See comment at the jump. */ - a.bind(fail); + a.bind(done); a.ret(a64::x30); } - /* After 8 nodes we've run out of the 32 bits we started with, so we - * must calculate a new hash to continue. - * - * This is a manual expansion `make_map_hash` from utils.c, and all - * changes to that function must be mirrored here. */ - a.bind(update_hash); + /* A collision node is a tuple of leafs where we do linear search.*/ + a.bind(collision_node); { - emit_enter_runtime_frame(); + Label linear_loop = a.newLabel(); + + a.lsr(TMP1, header_val, imm(_HEADER_ARITY_OFFS - 3)); - /* NOTE: ARG3 (key_hash) is always 0 at this point. */ - a.lsr(ARG6, depth, imm(3)); - mov_imm(ARG7, 1); - mov_imm(ARG8, HCONST_22); - a.bl(labels[internal_hash_helper]); + a.bind(linear_loop); + { + a.sub(TMP1, TMP1, imm(8)); - mov_imm(TMP1, INTERNAL_HASH_SALT); - a.eor(ARG3, ARG3, TMP1); + a.ldr(TMP2, arm::Mem(node, TMP1)); - a.mov(ARG6.w(), key.w()); - a.lsr(ARG7, key, imm(32)); - mov_imm(ARG8, HCONST); - a.bl(labels[internal_hash_helper]); + emit_untag_ptr(TMP2, TMP2); + a.ldp(TMP3, TMP4, arm::Mem(TMP2)); + a.cmp(key, TMP3); + a.csel(ARG1, node, TMP4, imm(arm::CondCode::kNE)); + a.b_eq(done); - emit_leave_runtime_frame(); + a.cbnz(TMP1, linear_loop); + } - a.b(node_loop); + a.ret(a64::x30); } } } @@ -362,10 +378,8 @@ void BeamGlobalAssembler::emit_i_get_map_element_shared() { emit_enter_runtime_frame(); /* Calculate the internal hash of ARG2 before diving into the HAMT. */ - mov_imm(ARG3, INTERNAL_HASH_SALT); a.mov(ARG6.w(), ARG2.w()); a.lsr(ARG7, ARG2, imm(32)); - mov_imm(ARG8, HCONST); a.bl(labels[internal_hash_helper]); emit_leave_runtime_frame(); diff --git a/erts/emulator/beam/jit/arm/ops.tab b/erts/emulator/beam/jit/arm/ops.tab index 3389ec4459..6d27a18c2d 100644 --- a/erts/emulator/beam/jit/arm/ops.tab +++ b/erts/emulator/beam/jit/arm/ops.tab @@ -1042,6 +1042,10 @@ bs_skip_utf16 Fail=f Ms=xy u Flags=u => skip_utf16(Fail, Ms, Flags) i_bs_get_utf16 S f t d i_bs_skip_utf16 S f t +bs_get_utf32 Fail=f Ms=xy Live=u Flags=u Dst | equal(Ms, Dst) => + bs_get_integer2 Fail Ms Live i=32 u=1 Flags x | + i_bs_validate_unicode_retract Fail x Ms | + move x Dst bs_get_utf32 Fail=f Ms=xy Live=u Flags=u Dst=d => bs_get_integer2 Fail Ms Live i=32 u=1 Flags Dst | i_bs_validate_unicode_retract Fail Dst Ms diff --git a/erts/emulator/beam/jit/beam_jit_common.cpp b/erts/emulator/beam/jit/beam_jit_common.cpp index a0bb07ce56..3200f75407 100644 --- a/erts/emulator/beam/jit/beam_jit_common.cpp +++ b/erts/emulator/beam/jit/beam_jit_common.cpp @@ -369,10 +369,11 @@ void BeamModuleAssembler::register_metadata(const BeamCodeHeader *header) { ranges.reserve(functions.size() + 2); ASSERT((ErtsCodePtr)getBaseAddress() == (ErtsCodePtr)header); + ASSERT(functions.size() == header->num_functions); /* Push info about the header */ ranges.push_back({.start = (ErtsCodePtr)getBaseAddress(), - .stop = getCode(functions[0]), + .stop = (ErtsCodePtr)&header->functions[functions.size()], .name = module_name + "::codeHeader"}); for (unsigned i = 0; i < functions.size(); i++) { diff --git a/erts/emulator/beam/jit/beam_jit_main.cpp b/erts/emulator/beam/jit/beam_jit_main.cpp index 7cd8dbf2aa..0cd732039f 100644 --- a/erts/emulator/beam/jit/beam_jit_main.cpp +++ b/erts/emulator/beam/jit/beam_jit_main.cpp @@ -421,9 +421,9 @@ extern "C" /* Issues full memory/instruction barriers on all threads for us. */ sys_icache_invalidate((char *)address, size); #elif defined(__aarch64__) && defined(__GNUC__) && \ - defined(ETHR_HAVE_GCC_ASM_ARM_IC_IVAU_INSTRUCTION) && \ - defined(ETHR_HAVE_GCC_ASM_ARM_DC_CVAU_INSTRUCTION) && \ - defined(ERTS_THR_INSTRUCTION_BARRIER) + defined(ERTS_THR_INSTRUCTION_BARRIER) && \ + ETHR_HAVE_GCC_ASM_ARM_IC_IVAU_INSTRUCTION && \ + ETHR_HAVE_GCC_ASM_ARM_DC_CVAU_INSTRUCTION /* Note that we do not issue any barriers here, whether instruction or * memory. This is on purpose as we must issue those on all schedulers * and not just the calling thread, and the chances of us forgetting to diff --git a/erts/emulator/beam/jit/beam_jit_metadata.cpp b/erts/emulator/beam/jit/beam_jit_metadata.cpp index 780fd97c97..10e82fdf37 100644 --- a/erts/emulator/beam/jit/beam_jit_metadata.cpp +++ b/erts/emulator/beam/jit/beam_jit_metadata.cpp @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2020-2021. All Rights Reserved. + * Copyright Ericsson AB 2020-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/erts/emulator/beam/jit/x86/instr_map.cpp b/erts/emulator/beam/jit/x86/instr_map.cpp index 679b2a6609..5f89077ba6 100644 --- a/erts/emulator/beam/jit/x86/instr_map.cpp +++ b/erts/emulator/beam/jit/x86/instr_map.cpp @@ -30,13 +30,11 @@ extern "C" } static const Uint32 INTERNAL_HASH_SALT = 3432918353; -static const Uint32 HCONST_22 = 0x98C475E6UL; static const Uint32 HCONST = 0x9E3779B9; -/* ARG3 = incoming hash +/* * ARG4 = lower 32 * ARG5 = upper 32 - * ARG6 = type constant * * Helper function for calculating the internal hash of keys before looking * them up in a map. @@ -47,15 +45,16 @@ static const Uint32 HCONST = 0x9E3779B9; * * Result is returned in ARG3. */ void BeamGlobalAssembler::emit_internal_hash_helper() { - x86::Gp hash = ARG3d, lower = ARG4d, upper = ARG5d, constant = ARG6d; + x86::Gp hash = ARG3d, lower = ARG4d, upper = ARG5d; - a.add(lower, constant); - a.add(upper, constant); + a.mov(hash, imm(INTERNAL_HASH_SALT)); + a.add(lower, imm(HCONST)); + a.add(upper, imm(HCONST)); #if defined(ERL_INTERNAL_HASH_CRC32C) - a.mov(constant, hash); + a.mov(ARG6d, hash); a.crc32(hash, lower); - a.add(hash, constant); + a.add(hash, ARG6d); a.crc32(hash, upper); #else using rounds = @@ -88,6 +87,23 @@ void BeamGlobalAssembler::emit_internal_hash_helper() { } #endif +#ifdef DBG_HASHMAP_COLLISION_BONANZA + a.mov(TMP_MEM1q, ARG1); + a.mov(TMP_MEM2q, ARG2); + a.mov(TMP_MEM3q, RET); + + a.mov(ARG1, ARG3); + emit_enter_runtime(); + runtime_call<2>(erts_dbg_hashmap_collision_bonanza); + emit_leave_runtime(); + + a.mov(ARG3d, RETd); + + a.mov(ARG1, TMP_MEM1q); + a.mov(ARG2, TMP_MEM2q); + a.mov(RET, TMP_MEM3q); +#endif + a.ret(); } @@ -109,8 +125,9 @@ void BeamGlobalAssembler::emit_hashmap_get_element() { a.bind(node_loop); { - Label fail = a.newLabel(), leaf_node = a.newLabel(), - skip_index_adjustment = a.newLabel(), update_hash = a.newLabel(); + Label done = a.newLabel(), leaf_node = a.newLabel(), + skip_index_adjustment = a.newLabel(), + collision_node = a.newLabel(); /* Find out which child we should follow, and shift the hash for the * next round. */ @@ -131,7 +148,7 @@ void BeamGlobalAssembler::emit_hashmap_get_element() { * Note that we jump directly to a `RET` instruction, as `BT` only * affects CF, and ZF ("not found") is clear at this point. */ a.bt(header_val, index); - a.short_().jnc(fail); + a.short_().jnc(done); /* The actual offset of our entry is the number of bits set (in * essence "entries present") before our index in the bitmap. */ @@ -154,11 +171,11 @@ void BeamGlobalAssembler::emit_hashmap_get_element() { /* Nope, we have to search another node. */ a.mov(header_val, emit_boxed_val(node, 0, sizeof(Uint32))); - /* After 8 nodes we've run out of the 32 bits we started with, so we - * need to update the hash to keep going. */ - a.test(depth, imm(0x7)); - a.short_().jz(update_hash); - a.short_().jmp(node_loop); + /* After 8 nodes we've run out of the 32 bits we started with + * and we end up in a collision node. */ + a.test(depth, imm(HAMT_MAX_LEVEL - 1)); + a.short_().jnz(node_loop); + a.short_().jmp(collision_node); a.bind(leaf_node); { @@ -168,36 +185,33 @@ void BeamGlobalAssembler::emit_hashmap_get_element() { a.mov(RET, getCDRRef(node)); /* See comment at the jump. */ - a.bind(fail); + a.bind(done); a.ret(); } - /* After 8 nodes we've run out of the 32 bits we started with, so we - * must calculate a new hash to continue. - * - * This is a manual expansion `make_map_hash` from utils.c, and all - * changes to that function must be mirrored here. */ - a.bind(update_hash); + /* A collision node is a tuple of leafs where we do linear search.*/ + a.bind(collision_node); { - a.mov(TMP_MEM1d, depth); + Label linear_loop = a.newLabel(); - /* NOTE: ARG3d is always 0 at this point. */ - a.mov(ARG4d, depth); - a.shr(ARG4d, imm(3)); - mov_imm(ARG5d, 1); - a.mov(ARG6d, imm(HCONST_22)); - a.call(labels[internal_hash_helper]); + a.shr(header_val, imm(_HEADER_ARITY_OFFS)); + a.lea(ARG6d, x86::qword_ptr(header_val, -1)); - a.xor_(ARG3d, imm(INTERNAL_HASH_SALT)); - a.mov(ARG4d, key.r32()); - a.mov(ARG5, key); - a.shr(ARG5, imm(32)); - a.mov(ARG6d, imm(HCONST)); - a.call(labels[internal_hash_helper]); + a.bind(linear_loop); + { + a.mov(ARG3, + x86::qword_ptr(node, ARG6, 3, 8 - TAG_PRIMARY_BOXED)); - a.mov(depth, TMP_MEM1d); + emit_ptr_val(ARG3, ARG3); + a.cmp(key, getCARRef(ARG3)); + a.mov(RET, getCDRRef(ARG3)); + a.short_().jz(done); - a.jmp(node_loop); + a.dec(ARG6d); + a.short_().jns(linear_loop); + } + + a.ret(); } } } @@ -381,8 +395,6 @@ void BeamGlobalAssembler::emit_i_get_map_element_shared() { a.shr(ARG5, imm(32)); a.mov(ARG4d, ARG2d); - a.mov(ARG3d, imm(INTERNAL_HASH_SALT)); - a.mov(ARG6d, imm(HCONST)); a.call(labels[internal_hash_helper]); emit_hashmap_get_element(); diff --git a/erts/emulator/beam/jit/x86/ops.tab b/erts/emulator/beam/jit/x86/ops.tab index 307c3d057e..ef57021d83 100644 --- a/erts/emulator/beam/jit/x86/ops.tab +++ b/erts/emulator/beam/jit/x86/ops.tab @@ -1000,6 +1000,11 @@ bs_skip_utf16 Fail=f Ms=xy u Flags=u => skip_utf16(Fail, Ms, Flags) i_bs_get_utf16 S f t d i_bs_skip_utf16 S f t +bs_get_utf32 Fail=f Ms=xy Live=u Flags=u Dst | equal(Ms, Dst) => + bs_get_integer2 Fail Ms Live i=32 u=1 Flags x | + i_bs_validate_unicode_retract Fail x Ms | + move x Dst + bs_get_utf32 Fail=f Ms=xy Live=u Flags=u Dst=d => bs_get_integer2 Fail Ms Live i=32 u=1 Flags Dst | i_bs_validate_unicode_retract Fail Dst Ms diff --git a/erts/emulator/beam/packet_parser.c b/erts/emulator/beam/packet_parser.c index 164994a98f..a349c3ff84 100644 --- a/erts/emulator/beam/packet_parser.c +++ b/erts/emulator/beam/packet_parser.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2008-2021. All Rights Reserved. + * Copyright Ericsson AB 2008-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -230,7 +230,7 @@ struct tpkt_head { unsigned char packet_length[2]; /* size incl header, big-endian (?) */ }; -void packet_parser_init() +void packet_parser_init(void) { static int done = 0; if (!done) { diff --git a/erts/emulator/beam/utils.c b/erts/emulator/beam/utils.c index 647f980e62..8e2b13136b 100644 --- a/erts/emulator/beam/utils.c +++ b/erts/emulator/beam/utils.c @@ -1441,8 +1441,8 @@ tailrecur_ne: sz = hashmap_bitcount(MAP_HEADER_VAL(hdr)); ASSERT(sz > 0 && sz < 17); break; - default: - erts_exit(ERTS_ERROR_EXIT, "Unknown hashmap subsubtag\n"); + default: + erts_exit(ERTS_ERROR_EXIT, "bad header"); } goto term_array; } @@ -1983,6 +1983,11 @@ tailrecur_ne: A minimal key can only be candidate as tie-breaker if we have passed that hash value in the other tree (which means the key did not exist in the other tree). + + Collision node amendment: + The leafs in collision nodes are sorted in map-key order. + If keys are different but hashes are equal we advance the + one lagging behind key-wise. */ sp = PSTACK_PUSH(map_stack); @@ -2395,12 +2400,19 @@ pop_next: ASSERT(sp->is_hashmap); if (j) { /* Key diff found, enter phase 2 */ - if (hashmap_key_hash_cmp(sp->ap, sp->bp) < 0) { + int hash_cmp = hashmap_key_hash_cmp(sp->ap, sp->bp); + if (hash_cmp == 0) { + /* Hash collision. Collision nodes are sorted by map key + * order, so we advance the one with the lesser key */ + hash_cmp = j; + } + if (hash_cmp < 0) { sp->min_key = CAR(sp->ap); sp->cmp_res = -1; sp->ap = hashmap_iterator_next(&stack); } else { + ASSERT(hash_cmp > 0); sp->min_key = CAR(sp->bp); sp->cmp_res = 1; sp->bp = hashmap_iterator_next(&b_stack); @@ -2452,7 +2464,7 @@ pop_next: sp->bp = hashmap_iterator_next(&b_stack); if (!sp->ap) { /* end of maps with identical keys */ - ASSERT(!sp->bp); + ASSERT(!sp->bp); /* as we assume indentical map sizes */ j = sp->cmp_res; exact = sp->was_exact; (void) PSTACK_POP(map_stack); @@ -2488,14 +2500,21 @@ pop_next: /* fall through */ case_HASHMAP_PHASE2_NEXT_STEP: if (sp->ap || sp->bp) { - if (hashmap_key_hash_cmp(sp->ap, sp->bp) < 0) { + int hash_cmp = hashmap_key_hash_cmp(sp->ap, sp->bp); + if (hash_cmp == 0) { + /* Hash collision. Collision nodes are sorted by map key + * order, so we advance the one with the lesser key */ + hash_cmp = j; + } + if (hash_cmp < 0) { ASSERT(sp->ap); a = CAR(sp->ap); b = sp->min_key; ASSERT(exact); WSTACK_PUSH(stack, OP_WORD(HASHMAP_PHASE2_IS_MIN_KEY_A)); } - else { /* hash_cmp > 0 */ + else { + ASSERT(hash_cmp > 0); ASSERT(sp->bp); a = CAR(sp->bp); b = sp->min_key; @@ -3749,7 +3768,7 @@ erts_ptr_id(void *ptr) return ptr; } -const void *erts_get_stacklimit() { +const void *erts_get_stacklimit(void) { return ethr_get_stacklimit(); } diff --git a/erts/emulator/drivers/common/inet_drv.c b/erts/emulator/drivers/common/inet_drv.c index a902fd27ad..329c48172c 100644 --- a/erts/emulator/drivers/common/inet_drv.c +++ b/erts/emulator/drivers/common/inet_drv.c @@ -4421,7 +4421,7 @@ static void inet_init_sctp(void) { } #endif /* HAVE_SCTP */ -static int inet_init() +static int inet_init(void) { if (!sock_init()) goto error; @@ -13875,7 +13875,7 @@ static udp_descriptor* sctp_inet_copy(udp_descriptor* desc, SOCKET s, #ifdef HAVE_UDP -static int packet_inet_init() +static int packet_inet_init(void) { sys_memzero((char *)&disassoc_sa, sizeof(disassoc_sa)); #ifdef AF_UNSPEC @@ -15060,8 +15060,7 @@ static MultiTimerData *add_multi_timer(tcp_descriptor *desc, ErlDrvPort port, -----------------------------------------------------------------------------*/ static int -save_subscriber(subs, subs_pid) -subs_list *subs; ErlDrvTermData subs_pid; +save_subscriber(subs_list *subs, ErlDrvTermData subs_pid) { subs_list *tmp; @@ -15083,8 +15082,7 @@ subs_list *subs; ErlDrvTermData subs_pid; } static void -free_subscribers(subs) -subs_list *subs; +free_subscribers(subs_list *subs) { subs_list *this; subs_list *next; diff --git a/erts/emulator/internal_doc/BeamAsm.md b/erts/emulator/internal_doc/BeamAsm.md index b9a739d897..4df4634d2d 100644 --- a/erts/emulator/internal_doc/BeamAsm.md +++ b/erts/emulator/internal_doc/BeamAsm.md @@ -385,8 +385,9 @@ not need the symbols in the executable. Using the same data we can also produce a graph where the scheduler profile data has been merged by using `sed`: - ## Strip [0-9]+_ from all scheduler names - sed -e 's/^[0-9]\+_//' out.folded > out.folded_sched + ## Strip [0-9]+_ and/or _[0-9]+ from all scheduler names + ## scheduler names changed in OTP26, hence two expressions + sed -e 's/^[0-9]\+_//' -e 's/^erts_\([^_]\+\)_[0-9]\+/erts_\1/' out.folded > out.folded_sched ## Create the svg flamegraph.pl out.folded_sched > out_sched.svg diff --git a/erts/emulator/nifs/common/prim_socket_int.h b/erts/emulator/nifs/common/prim_socket_int.h index 90937d308e..9f753bf80b 100644 --- a/erts/emulator/nifs/common/prim_socket_int.h +++ b/erts/emulator/nifs/common/prim_socket_int.h @@ -352,6 +352,7 @@ typedef struct { /* XXX Should be locked but too awkward and small gain */ BOOLEAN_T dbg; BOOLEAN_T useReg; + BOOLEAN_T eei; /* Registry stuff */ ErlNifPid regPid; /* Constant - not locked */ diff --git a/erts/emulator/nifs/common/prim_socket_nif.c b/erts/emulator/nifs/common/prim_socket_nif.c index a46e44d53b..9d6755387d 100644 --- a/erts/emulator/nifs/common/prim_socket_nif.c +++ b/erts/emulator/nifs/common/prim_socket_nif.c @@ -1955,6 +1955,7 @@ static const struct in6_addr in6addr_loopback = GLOBAL_ATOM_DECL(cmsg_cloexec); \ GLOBAL_ATOM_DECL(command); \ GLOBAL_ATOM_DECL(completion); \ + GLOBAL_ATOM_DECL(completion_status); \ GLOBAL_ATOM_DECL(confirm); \ GLOBAL_ATOM_DECL(congestion); \ GLOBAL_ATOM_DECL(connect); \ @@ -2291,6 +2292,7 @@ ERL_NIF_TERM esock_atom_socket_tag; // This has a "special" name ('$socket') LOCAL_ATOM_DECL(do); \ LOCAL_ATOM_DECL(dont); \ LOCAL_ATOM_DECL(dtor); \ + LOCAL_ATOM_DECL(eei); \ LOCAL_ATOM_DECL(exclude); \ LOCAL_ATOM_DECL(false); \ LOCAL_ATOM_DECL(frag_needed); \ @@ -3844,7 +3846,7 @@ ERL_NIF_TERM esock_global_info(ErlNifEnv* env) numBits, numSockets, numTypeDGrams, numTypeStreams, numTypeSeqPkgs, numDomLocal, numDomInet, numDomInet6, numProtoIP, numProtoTCP, numProtoUDP, numProtoSCTP, - sockDbg, iovMax, dbg, useReg, iow; + sockDbg, iovMax, dbg, useReg, iow, eei; MLOCK(data.cntMtx); numBits = MKUI(env, ESOCK_COUNTER_SIZE); @@ -3860,6 +3862,7 @@ ERL_NIF_TERM esock_global_info(ErlNifEnv* env) numProtoUDP = MKUI(env, data.numProtoUDP); numProtoSCTP = MKUI(env, data.numProtoSCTP); sockDbg = BOOL2ATOM(data.sockDbg); + eei = BOOL2ATOM(data.eei); MUNLOCK(data.cntMtx); iovMax = MKI(env, data.iov_max); @@ -3891,6 +3894,7 @@ ERL_NIF_TERM esock_global_info(ErlNifEnv* env) ERL_NIF_TERM keys[] = {esock_atom_debug, atom_socket_debug, + atom_eei, esock_atom_use_registry, atom_iow, esock_atom_counters, @@ -3898,6 +3902,7 @@ ERL_NIF_TERM esock_global_info(ErlNifEnv* env) atom_io_backend}, vals[] = {dbg, sockDbg, + eei, useReg, iow, gcnt, @@ -13141,6 +13146,13 @@ int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) atom_iow, ESOCK_NIF_IOW_DEFAULT); + /* --enable-extended-error-info */ +#if defined(ESOCK_USE_EXTENDED_ERROR_INFO) + data.eei = TRUE; +#else + data.eei = FALSE; +#endif + /* --esock-debug-file=<filename> */ { char *debug_filename; diff --git a/erts/emulator/nifs/common/socket_int.h b/erts/emulator/nifs/common/socket_int.h index 2613fe5a1e..9c3381f975 100644 --- a/erts/emulator/nifs/common/socket_int.h +++ b/erts/emulator/nifs/common/socket_int.h @@ -242,6 +242,7 @@ typedef long ssize_t; GLOBAL_ATOM_DEF(cmsg_cloexec); \ GLOBAL_ATOM_DEF(command); \ GLOBAL_ATOM_DEF(completion); \ + GLOBAL_ATOM_DEF(completion_status); \ GLOBAL_ATOM_DEF(confirm); \ GLOBAL_ATOM_DEF(congestion); \ GLOBAL_ATOM_DEF(connect); \ diff --git a/erts/emulator/nifs/common/socket_util.c b/erts/emulator/nifs/common/socket_util.c index 8eeb98dba7..a1c3e7eddc 100644 --- a/erts/emulator/nifs/common/socket_util.c +++ b/erts/emulator/nifs/common/socket_util.c @@ -2371,6 +2371,18 @@ ERL_NIF_TERM esock_errno_to_term(ErlNifEnv* env, int err) break; #endif +#if defined(ERROR_INVALID_NETNAME) + case ERROR_INVALID_NETNAME: + return MKA(env, "invalid_netname"); + break; +#endif + +#if defined(ERROR_MORE_DATA) + case ERROR_MORE_DATA: + return MKA(env, "more_data"); + break; +#endif + default: { char* str = erl_errno_id(err); @@ -2666,7 +2678,7 @@ MSG_FUNCS */ extern -ErlNifTime esock_timestamp() +ErlNifTime esock_timestamp(void) { ErlNifTime monTime = enif_monotonic_time(ERL_NIF_USEC); ErlNifTime offTime = enif_time_offset(ERL_NIF_USEC); diff --git a/erts/emulator/nifs/common/socket_util.h b/erts/emulator/nifs/common/socket_util.h index 04f0cc3d94..c16803bbdc 100644 --- a/erts/emulator/nifs/common/socket_util.h +++ b/erts/emulator/nifs/common/socket_util.h @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2018-2022. All Rights Reserved. + * Copyright Ericsson AB 2018-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,8 +53,7 @@ __LINE__, \ (RI), (I)) -#define ESOCK_VERBOSE_ERRNO 1 -#if defined(ESOCK_VERBOSE_ERRNO) +#if defined(ESOCK_USE_EXTENDED_ERROR_INFO) #define ENO2T(E, ENO) MKEEI((E), \ MKI((E), (ENO)), \ esock_errno_to_term((E), (ENO))) diff --git a/erts/emulator/nifs/unix/unix_socket_syncio.c b/erts/emulator/nifs/unix/unix_socket_syncio.c index 3e3cfcaeef..644400aeb5 100644 --- a/erts/emulator/nifs/unix/unix_socket_syncio.c +++ b/erts/emulator/nifs/unix/unix_socket_syncio.c @@ -29,6 +29,8 @@ # include "config.h" #endif +#ifdef ESOCK_ENABLE + #ifdef HAVE_SENDFILE #if defined(__linux__) || (defined(__sun) && defined(__SVR4)) #include <sys/sendfile.h> @@ -7385,3 +7387,4 @@ void essio_down_reader(ErlNifEnv* env, } +#endif diff --git a/erts/emulator/nifs/win32/win_socket_asyncio.c b/erts/emulator/nifs/win32/win_socket_asyncio.c index f91e7edc48..b7d1f424fd 100644 --- a/erts/emulator/nifs/win32/win_socket_asyncio.c +++ b/erts/emulator/nifs/win32/win_socket_asyncio.c @@ -83,6 +83,8 @@ # include "config.h" #endif +#ifdef ESOCK_ENABLE + // #include <Ws2def.h> // #include <winsock2.h> // #include <windows.h> @@ -719,6 +721,21 @@ static BOOLEAN_T esaio_completion_send(ESAIOThreadData* dataP, ErlNifPid* opCaller, ESAIOOpDataSend* opDataP, int error); +static void esaio_completion_send_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataSend* opDataP); +static void esaio_completion_send_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSend* opDataP); +static void esaio_completion_send_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSend* opDataP, + int error); static void esaio_completion_send_completed(ErlNifEnv* env, ESockDescriptor* descP, OVERLAPPED* ovl, @@ -748,6 +765,21 @@ static BOOLEAN_T esaio_completion_sendto(ESAIOThreadData* dataP, ErlNifPid* opCaller, ESAIOOpDataSendTo* opDataP, int error); +static void esaio_completion_sendto_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataSendTo* opDataP); +static void esaio_completion_sendto_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSendTo* opDataP); +static void esaio_completion_sendto_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSendTo* opDataP, + int error); static void esaio_completion_sendto_fail(ErlNifEnv* env, ESockDescriptor* descP, int error, @@ -759,6 +791,21 @@ static BOOLEAN_T esaio_completion_sendmsg(ESAIOThreadData* dataP, ErlNifPid* opCaller, ESAIOOpDataSendMsg* opDataP, int error); +static void esaio_completion_sendmsg_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataSendMsg* opDataP); +static void esaio_completion_sendmsg_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSendMsg* opDataP); +static void esaio_completion_sendmsg_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSendMsg* opDataP, + int error); static void esaio_completion_sendmsg_fail(ErlNifEnv* env, ESockDescriptor* descP, int error, @@ -770,6 +817,21 @@ static BOOLEAN_T esaio_completion_recv(ESAIOThreadData* dataP, ErlNifPid* opCaller, ESAIOOpDataRecv* opDataP, int error); +static void esaio_completion_recv_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataRecv* opDataP); +static void esaio_completion_recv_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecv* opDataP); +static void esaio_completion_recv_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecv* opDataP, + int error); static void esaio_completion_recv_completed(ErlNifEnv* env, ESockDescriptor* descP, OVERLAPPED* ovl, @@ -815,6 +877,27 @@ static BOOLEAN_T esaio_completion_recvfrom(ESAIOThreadData* dataP, ErlNifPid* opCaller, ESAIOOpDataRecvFrom* opDataP, int error); +static void esaio_completion_recvfrom_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataRecvFrom* opDataP); +static void esaio_completion_recvfrom_more_data(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataRecvFrom* opDataP, + int error); +static void esaio_completion_recvfrom_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecvFrom* opDataP); +static void esaio_completion_recvfrom_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecvFrom* opDataP, + int error); static void esaio_completion_recvfrom_completed(ErlNifEnv* env, ESockDescriptor* descP, OVERLAPPED* ovl, @@ -845,6 +928,21 @@ static BOOLEAN_T esaio_completion_recvmsg(ESAIOThreadData* dataP, ErlNifPid* opCaller, ESAIOOpDataRecvMsg* opDataP, int error); +static void esaio_completion_recvmsg_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataRecvMsg* opDataP); +static void esaio_completion_recvmsg_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecvMsg* opDataP); +static void esaio_completion_recvmsg_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecvMsg* opDataP, + int error); static void esaio_completion_recvmsg_completed(ErlNifEnv* env, ESockDescriptor* descP, OVERLAPPED* ovl, @@ -5719,7 +5817,7 @@ void esaio_completion_connect_failure(ErlNifEnv* env, if (descP->connectorP != NULL) { /* Figure out the reason */ ERL_NIF_TERM reason = MKT2(env, - esock_atom_get_overlapped_result, + esock_atom_completion_status, ENO2T(env, error)); /* Inform the user waiting for a reply */ @@ -6007,6 +6105,12 @@ void esaio_completion_accept_success(ErlNifEnv* env, /* *Maybe* update socket (read) state * (depends on if the queue is now empty) */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_accept_success(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->acceptorsQ.first == NULL)), descP->readState) ); if (descP->acceptorsQ.first == NULL) descP->readState &= ~ESOCK_STATE_SELECTED; } @@ -6029,7 +6133,6 @@ void esaio_completion_accept_aborted(ErlNifEnv* env, ESAIOOpDataAccept* opDataP) { ESockRequestor req; - ERL_NIF_TERM reason; if (esock_acceptor_get(env, descP, &opDataP->accRef, @@ -6069,6 +6172,12 @@ void esaio_completion_accept_aborted(ErlNifEnv* env, /* *Maybe* update socket (read) state * (depends on if the queue is now empty) */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_accept_aborted(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->acceptorsQ.first == NULL)), descP->readState) ); if (descP->acceptorsQ.first == NULL) { descP->readState &= ~ESOCK_STATE_SELECTED; } @@ -6095,7 +6204,7 @@ void esaio_completion_accept_failure(ErlNifEnv* env, &req)) { reason = MKT2(env, - esock_atom_get_overlapped_result, + esock_atom_completion_status, ENO2T(env, error)); /* Inform the user waiting for a reply */ @@ -6109,6 +6218,12 @@ void esaio_completion_accept_failure(ErlNifEnv* env, /* *Maybe* update socket (read) state * (depends on if the queue is now empty) */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_accept_failure(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->acceptorsQ.first == NULL)), descP->readState) ); if (descP->acceptorsQ.first == NULL) { descP->readState &= ~ESOCK_STATE_SELECTED; } @@ -6370,40 +6485,9 @@ BOOLEAN_T esaio_completion_send(ESAIOThreadData* dataP, ("WIN-ESAIO", "esaio_completion_send(%d) -> no error" "\r\n", descP->sock) ); MLOCK(descP->writeMtx); - if (esock_writer_get(env, descP, - &opDataP->sendRef, - opCaller, - &req)) { - if (IS_OPEN(descP->writeState)) { - esaio_completion_send_completed(env, descP, - ovl, - opEnv, - opCaller, - opDataP->sockRef, - opDataP->sendRef, - opDataP->wbuf.len, - &req); - } else { - /* A completed (active) request for a socket that is not open. - * Is this even possible? - * A race (completed just as the socket was closed). - */ - esaio_completion_send_not_active(descP); - } - } else { - /* Request was actually completed directly - * (and was therefor not put into the "queue") - * => Nothing to do here, other than cleanup (see below). - */ - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->writersQ.first == NULL) { - descP->writeState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_send_success(env, descP, ovl, opEnv, + opCaller, opDataP); MUNLOCK(descP->writeMtx); break; @@ -6416,55 +6500,9 @@ BOOLEAN_T esaio_completion_send(ESAIOThreadData* dataP, /* *** SAME MTX LOCK ORDER FOR ALL OPs *** */ MLOCK(descP->readMtx); MLOCK(descP->writeMtx); - /* The only thing *we* do that could cause an abort is the - * 'CancelIoEx' call, which we do when closing the socket - * (or cancel a request). - * But if we have done that; - * - Socket state will not be 'open' and - * - we have also set closer (pid and ref). - */ - - if (esock_writer_get(env, descP, - &opDataP->sendRef, - opCaller, - &req)) { - - reason = esock_atom_closed, - - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - - /* The socket not being open (assumed closing), - * means we are in the closing phase... - */ - if (! IS_OPEN(descP->writeState)) { - - /* We can only send the 'close' message to the closer - * when all requests has been processed! - */ - - /* Check "our" queue */ - if (descP->writersQ.first == NULL) { - - /* Check "other" queue(s) and if there is a closer pid */ - if ((descP->readersQ.first == NULL) && - (descP->acceptorsQ.first == NULL)) { - - esaio_stop(env, descP); - } - } - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->writersQ.first == NULL) { - descP->writeState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_send_aborted(env, descP, opCaller, opDataP); - } MUNLOCK(descP->writeMtx); MUNLOCK(descP->readMtx); break; @@ -6475,36 +6513,9 @@ BOOLEAN_T esaio_completion_send(ESAIOThreadData* dataP, "esaio_completion_send(%d) -> operation unknown failure" "\r\n", descP->sock) ); MLOCK(descP->writeMtx); - /* We do not know what this is - * but we can "assume" that the request failed so we need to - * remove it from the "queue" if its still there... - * And cleanup... - */ - if (esock_writer_get(env, descP, - &opDataP->sendRef, - opCaller, - &req)) { - - reason = MKT2(env, - esock_atom_get_overlapped_result, - ENO2T(env, error)); - - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - esaio_completion_send_fail(env, descP, error, FALSE); - - } else { - esaio_completion_send_fail(env, descP, error, TRUE); - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->writersQ.first == NULL) { - descP->writeState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_send_failure(env, descP, opCaller, opDataP, error); + MUNLOCK(descP->writeMtx); break; } @@ -6528,6 +6539,184 @@ BOOLEAN_T esaio_completion_send(ESAIOThreadData* dataP, } + +/* *** esaio_completion_send_success *** + * The 'send' operation was successful. + */ +static +void esaio_completion_send_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataSend* opDataP) +{ + ESockRequestor req; + + if (esock_writer_get(env, descP, + &opDataP->sendRef, + opCaller, + &req)) { + if (IS_OPEN(descP->writeState)) { + esaio_completion_send_completed(env, descP, ovl, opEnv, + opCaller, + opDataP->sockRef, + opDataP->sendRef, + opDataP->wbuf.len, + &req); + } else { + /* A completed (active) request for a socket that is not open. + * Is this even possible? + * A race (completed just as the socket was closed). + */ + esaio_completion_send_not_active(descP); + } + + } else { + /* Request was actually completed directly + * (and was therefor not put into the "queue") + * => Nothing to do here, other than cleanup (see below). + */ + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_send_success(%d) -> " + "maybe (%s) update (write) state (ox%X)\r\n", + descP->sock, + B2S((descP->writersQ.first == NULL)), descP->writeState) ); + if (descP->writersQ.first == NULL) { + descP->writeState &= ~ESOCK_STATE_SELECTED; + } + +} + + + +/* *** esaio_completion_send_aborted *** + * The only thing *we* do that could cause an abort is the + * 'CancelIoEx' call, which we do when closing the socket + * (or cancel a request). + * But if we have done that; + * - Socket state will not be 'open' and + * - we have also set closer (pid and ref). + */ + +static +void esaio_completion_send_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSend* opDataP) +{ + ESockRequestor req; + + if (esock_writer_get(env, descP, + &opDataP->sendRef, + opCaller, + &req)) { + + ERL_NIF_TERM reason = esock_atom_closed; + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + + /* The socket not being open (assumed closing), + * means we are in the closing phase... + */ + if (! IS_OPEN(descP->writeState)) { + + /* We can only send the 'close' message to the closer + * when all requests has been processed! + */ + + /* Check "our" queue */ + if (descP->writersQ.first == NULL) { + + /* Check "other" queue(s) and if there is a closer pid */ + if ((descP->readersQ.first == NULL) && + (descP->acceptorsQ.first == NULL)) { + + esaio_stop(env, descP); + + } + } + } + + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_send_aborted(%d) -> " + "maybe (%s) update (write) state (ox%X)\r\n", + descP->sock, + B2S((descP->writersQ.first == NULL)), descP->writeState) ); + if (descP->writersQ.first == NULL) { + descP->writeState &= ~ESOCK_STATE_SELECTED; + } + +} + + + +/* *** esaio_completion_send_failure * + * A "general" failure happened while performing the 'send' operation. + */ +static +void esaio_completion_send_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSend* opDataP, + int error) +{ + ESockRequestor req; + ERL_NIF_TERM reason; + + /* We do not know what this is + * but we can "assume" that the request failed so we need to + * remove it from the "queue" if its still there... + * And cleanup... + */ + if (esock_writer_get(env, descP, + &opDataP->sendRef, + opCaller, + &req)) { + + reason = MKT2(env, + esock_atom_completion_status, + ENO2T(env, error)); + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + esaio_completion_send_fail(env, descP, error, FALSE); + + } else { + esaio_completion_send_fail(env, descP, error, TRUE); + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_send_failure(%d) -> " + "maybe (%s) update (write) state (ox%X)\r\n", + descP->sock, + B2S((descP->writersQ.first == NULL)), descP->writeState) ); + if (descP->writersQ.first == NULL) { + descP->writeState &= ~ESOCK_STATE_SELECTED; + } + +} + + /* *** esaio_completion_send_completed *** * The send request has completed. */ @@ -6785,40 +6974,9 @@ BOOLEAN_T esaio_completion_sendto(ESAIOThreadData* dataP, ("WIN-ESAIO", "esaio_completion_sendto(%d) -> no error" "\r\n", descP->sock) ); MLOCK(descP->writeMtx); - if (esock_writer_get(env, descP, - &opDataP->sendRef, - opCaller, - &req)) { - if (IS_OPEN(descP->writeState)) { - esaio_completion_send_completed(env, descP, - ovl, - opEnv, - opCaller, - opDataP->sockRef, - opDataP->sendRef, - opDataP->wbuf.len, - &req); - } else { - /* A completed (active) request for a socket that is not open. - * Is this even possible? - * A race (completed just as the socket was closed). - */ - esaio_completion_send_not_active(descP); - } - } else { - /* Request was actually completed directly - * (and was therefor not put into the "queue") - * => Nothing to do here, other than cleanup (see below). - */ - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->writersQ.first == NULL) { - descP->writeState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_sendto_success(env, descP, ovl, opEnv, + opCaller, opDataP); MUNLOCK(descP->writeMtx); break; @@ -6831,55 +6989,8 @@ BOOLEAN_T esaio_completion_sendto(ESAIOThreadData* dataP, /* *** SAME MTX LOCK ORDER FOR ALL OPs *** */ MLOCK(descP->readMtx); MLOCK(descP->writeMtx); - /* The only thing *we* do that could cause an abort is the - * 'CancelIoEx' call, which we do when closing the socket - * (or cancel a request). - * But if we have done that; - * - Socket state will not be 'open' and - * - we have also set closer (pid and ref). - */ - - if (esock_writer_get(env, descP, - &opDataP->sendRef, - opCaller, - &req)) { - - reason = esock_atom_closed, - - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - - /* The socket not being open (assumed closing), - * means we are in the closing phase... - */ - if (! IS_OPEN(descP->writeState)) { - - /* We can only send the 'close' message to the closer - * when all requests has been processed! - */ - - /* Check "our" queue */ - if (descP->writersQ.first == NULL) { - - /* Check "other" queue(s) and if there is a closer pid */ - if ((descP->readersQ.first == NULL) && - (descP->acceptorsQ.first == NULL)) { - - esaio_stop(env, descP); - - } - } - } - - } - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->writersQ.first == NULL) { - descP->writeState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_sendto_aborted(env, descP, opCaller, opDataP); MUNLOCK(descP->writeMtx); MUNLOCK(descP->readMtx); @@ -6891,35 +7002,8 @@ BOOLEAN_T esaio_completion_sendto(ESAIOThreadData* dataP, "esaio_completion_sendto(%d) -> operation unknown failure" "\r\n", descP->sock) ); MLOCK(descP->writeMtx); - /* We do not know what this is - * but we can "assume" that the request failed so we need to - * remove it from the "queue" if its still there... - * And cleanup... - */ - if (esock_writer_get(env, descP, - &opDataP->sendRef, - opCaller, - &req)) { - reason = MKT2(env, - esock_atom_get_overlapped_result, - ENO2T(env, error)); - - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - esaio_completion_sendto_fail(env, descP, error, FALSE); - - } else { - esaio_completion_sendto_fail(env, descP, error, TRUE); - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->writersQ.first == NULL) { - descP->writeState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_sendto_failure(env, descP, opCaller, opDataP, error); MUNLOCK(descP->writeMtx); break; @@ -6945,6 +7029,180 @@ BOOLEAN_T esaio_completion_sendto(ESAIOThreadData* dataP, +/* *** esaio_completion_sendto_suuccess *** */ +static +void esaio_completion_sendto_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataSendTo* opDataP) +{ + ESockRequestor req; + + if (esock_writer_get(env, descP, + &opDataP->sendRef, + opCaller, + &req)) { + if (IS_OPEN(descP->writeState)) { + esaio_completion_send_completed(env, descP, ovl, opEnv, + opCaller, + opDataP->sockRef, + opDataP->sendRef, + opDataP->wbuf.len, + &req); + } else { + /* A completed (active) request for a socket that is not open. + * Is this even possible? + * A race (completed just as the socket was closed). + */ + esaio_completion_send_not_active(descP); + } + + } else { + /* Request was actually completed directly + * (and was therefor not put into the "queue") + * => Nothing to do here, other than cleanup (see below). + */ + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_sendto_success(%d) -> " + "maybe (%s) update (write) state (ox%X)\r\n", + descP->sock, + B2S((descP->writersQ.first == NULL)), descP->writeState) ); + if (descP->writersQ.first == NULL) { + descP->writeState &= ~ESOCK_STATE_SELECTED; + } + +} + + + +/* *** esaio_completion_sendto_aborted *** + * The only thing *we* do that could cause an abort is the + * 'CancelIoEx' call, which we do when closing the socket + * (or cancel a request). + * But if we have done that; + * - Socket state will not be 'open' and + * - we have also set closer (pid and ref). + */ + +static +void esaio_completion_sendto_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSendTo* opDataP) +{ + ESockRequestor req; + + if (esock_writer_get(env, descP, + &opDataP->sendRef, + opCaller, + &req)) { + + ERL_NIF_TERM reason = esock_atom_closed; + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + + /* The socket not being open (assumed closing), + * means we are in the closing phase... + */ + if (! IS_OPEN(descP->writeState)) { + + /* We can only send the 'close' message to the closer + * when all requests has been processed! + */ + + /* Check "our" queue */ + if (descP->writersQ.first == NULL) { + + /* Check "other" queue(s) and if there is a closer pid */ + if ((descP->readersQ.first == NULL) && + (descP->acceptorsQ.first == NULL)) { + + esaio_stop(env, descP); + + } + } + } + + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_sendto_aborted(%d) -> " + "maybe (%s) update (write) state (ox%X)\r\n", + descP->sock, + B2S((descP->writersQ.first == NULL)), descP->writeState) ); + if (descP->writersQ.first == NULL) { + descP->writeState &= ~ESOCK_STATE_SELECTED; + } +} + + +/* *** esaio_completion_sendto_failure * + * A "general" failure happened while performing the 'sendto' operation. + */ +static +void esaio_completion_sendto_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSendTo* opDataP, + int error) +{ + ESockRequestor req; + ERL_NIF_TERM reason; + + /* We do not know what this is + * but we can "assume" that the request failed so we need to + * remove it from the "queue" if its still there... + * And cleanup... + */ + if (esock_writer_get(env, descP, + &opDataP->sendRef, + opCaller, + &req)) { + + reason = MKT2(env, + esock_atom_completion_status, + ENO2T(env, error)); + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + esaio_completion_sendto_fail(env, descP, error, FALSE); + + } else { + esaio_completion_sendto_fail(env, descP, error, TRUE); + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_sendto_failure(%d) -> " + "maybe (%s) update (write) state (ox%X)\r\n", + descP->sock, + B2S((descP->writersQ.first == NULL)), descP->writeState) ); + if (descP->writersQ.first == NULL) { + descP->writeState &= ~ESOCK_STATE_SELECTED; + } + +} + + + /* *** esaio_completion_sendto_fail *** * Unknown operation failure. */ @@ -7007,50 +7265,10 @@ BOOLEAN_T esaio_completion_sendmsg(ESAIOThreadData* dataP, ("WIN-ESAIO", "esaio_completion_sendmsg(%d) -> no error" "\r\n", descP->sock) ); MLOCK(descP->writeMtx); - if (esock_writer_get(env, descP, - &opDataP->sendRef, - opCaller, - &req)) { - if (IS_OPEN(descP->writeState)) { - - DWORD toWrite = 0; - - /* Calculate how much data *in total* - * we was supposed to write */ - for (int i = 0; i < opDataP->iovec->iovcnt; i++) { - toWrite += opDataP->iovec->iov[i].iov_len; - } - esaio_completion_send_completed(env, descP, - ovl, - opEnv, - opCaller, - opDataP->sockRef, - opDataP->sendRef, - toWrite, - &req); + esaio_completion_sendmsg_success(env, descP, ovl, opEnv, + opCaller, opDataP); - } else { - /* A completed (active) request for a socket that is not open. - * Is this even possible? - * A race (completed just as the socket was closed). - */ - esaio_completion_send_not_active(descP); - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->writersQ.first == NULL) { - descP->writeState &= ~ESOCK_STATE_SELECTED; - } - - } else { - /* Request was actually completed directly - * (and was therefor not put into the "queue") - * => Nothing to do here, other than cleanup (see below). - */ - } MUNLOCK(descP->writeMtx); break; @@ -7062,55 +7280,9 @@ BOOLEAN_T esaio_completion_sendmsg(ESAIOThreadData* dataP, /* *** SAME MTX LOCK ORDER FOR ALL OPs *** */ MLOCK(descP->readMtx); MLOCK(descP->writeMtx); - /* The only thing *we* do that could cause an abort is the - * 'CancelIoEx' call, which we do when closing the socket - * (or cancel a request). - * But if we have done that; - * - Socket state will not be 'open' and - * - we have also set closer (pid and ref). - */ - - if (esock_writer_get(env, descP, - &opDataP->sendRef, - opCaller, - &req)) { - - reason = esock_atom_closed, - - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - - /* The socket not being open (assumed closing), - * means we are in the closing phase... - */ - if (! IS_OPEN(descP->writeState)) { - - /* We can only send the 'close' message to the closer - * when all requests has been processed! - */ - - /* Check "our" queue */ - if (descP->writersQ.first == NULL) { - - /* Check "other" queue(s) and if there is a closer pid */ - if ((descP->readersQ.first == NULL) && - (descP->acceptorsQ.first == NULL)) { - - esaio_stop(env, descP); - } - } - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->writersQ.first == NULL) { - descP->writeState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_sendmsg_aborted(env, descP, opCaller, opDataP); - } MUNLOCK(descP->writeMtx); MUNLOCK(descP->readMtx); break; @@ -7121,35 +7293,9 @@ BOOLEAN_T esaio_completion_sendmsg(ESAIOThreadData* dataP, "esaio_completion_sendmsg(%d) -> operation unknown failure" "\r\n", descP->sock) ); MLOCK(descP->writeMtx); - /* We do not know what this is - * but we can "assume" that the request failed so we need to - * remove it from the "queue" if its still there... - * And cleanup... - */ - if (esock_writer_get(env, descP, - &opDataP->sendRef, - opCaller, - &req)) { - - reason = MKT2(env, - esock_atom_get_overlapped_result, - ENO2T(env, error)); - - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - esaio_completion_sendmsg_fail(env, descP, error, FALSE); - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->writersQ.first == NULL) { - descP->writeState &= ~ESOCK_STATE_SELECTED; - } - } else { - esaio_completion_sendmsg_fail(env, descP, error, TRUE); - } + esaio_completion_sendmsg_failure(env, descP, opCaller, opDataP, error); + MUNLOCK(descP->writeMtx); break; } @@ -7176,6 +7322,188 @@ BOOLEAN_T esaio_completion_sendmsg(ESAIOThreadData* dataP, } +/* *** esaio_completion_sendmsg_suuccess *** */ +static +void esaio_completion_sendmsg_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataSendMsg* opDataP) +{ + ESockRequestor req; + + if (esock_writer_get(env, descP, + &opDataP->sendRef, + opCaller, + &req)) { + if (IS_OPEN(descP->writeState)) { + + DWORD toWrite = 0; + + /* Calculate how much data *in total* + * we was supposed to write */ + for (int i = 0; i < opDataP->iovec->iovcnt; i++) { + toWrite += opDataP->iovec->iov[i].iov_len; + } + + esaio_completion_send_completed(env, descP, ovl, opEnv, + opCaller, + opDataP->sockRef, + opDataP->sendRef, + toWrite, + &req); + + } else { + /* A completed (active) request for a socket that is not open. + * Is this even possible? + * A race (completed just as the socket was closed). + */ + esaio_completion_send_not_active(descP); + } + + } else { + /* Request was actually completed directly + * (and was therefor not put into the "queue") + * => Nothing to do here, other than cleanup (see below). + */ + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_sendmsg_success(%d) -> " + "maybe (%s) update (write) state (ox%X)\r\n", + descP->sock, + B2S((descP->writersQ.first == NULL)), descP->writeState) ); + if (descP->writersQ.first == NULL) { + descP->writeState &= ~ESOCK_STATE_SELECTED; + } + +} + + +/* *** esaio_completion_sendmsg_aborted *** + * The only thing *we* do that could cause an abort is the + * 'CancelIoEx' call, which we do when closing the socket + * (or cancel a request). + * But if we have done that; + * - Socket state will not be 'open' and + * - we have also set closer (pid and ref). + */ + +static +void esaio_completion_sendmsg_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSendMsg* opDataP) +{ + ESockRequestor req; + + if (esock_writer_get(env, descP, + &opDataP->sendRef, + opCaller, + &req)) { + + ERL_NIF_TERM reason = esock_atom_closed; + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + + /* The socket not being open (assumed closing), + * means we are in the closing phase... + */ + if (! IS_OPEN(descP->writeState)) { + + /* We can only send the 'close' message to the closer + * when all requests has been processed! + */ + + /* Check "our" queue */ + if (descP->writersQ.first == NULL) { + + /* Check "other" queue(s) and if there is a closer pid */ + if ((descP->readersQ.first == NULL) && + (descP->acceptorsQ.first == NULL)) { + + esaio_stop(env, descP); + + } + } + } + + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_sendmsg_aborted(%d) -> " + "maybe (%s) update (write) state (ox%X)\r\n", + descP->sock, + B2S((descP->writersQ.first == NULL)), descP->writeState) ); + if (descP->writersQ.first == NULL) { + descP->writeState &= ~ESOCK_STATE_SELECTED; + } + +} + + +/* *** esaio_completion_sendmsg_failure * + * A "general" failure happened while performing the 'sendmsg' operation. + */ +static +void esaio_completion_sendmsg_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataSendMsg* opDataP, + int error) +{ + ESockRequestor req; + ERL_NIF_TERM reason; + + /* We do not know what this is + * but we can "assume" that the request failed so we need to + * remove it from the "queue" if its still there... + * And cleanup... + */ + if (esock_writer_get(env, descP, + &opDataP->sendRef, + opCaller, + &req)) { + + reason = MKT2(env, + esock_atom_completion_status, + ENO2T(env, error)); + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + esaio_completion_sendmsg_fail(env, descP, error, FALSE); + + } else { + esaio_completion_sendmsg_fail(env, descP, error, TRUE); + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_sendmsg_success(%d) -> " + "maybe (%s) update (write) state (ox%X)\r\n", + descP->sock, + B2S((descP->writersQ.first == NULL)), descP->writeState) ); + if (descP->writersQ.first == NULL) { + descP->writeState &= ~ESOCK_STATE_SELECTED; + } + +} + /* *** esaio_completion_sendmsg_fail *** * Unknown operation failure. @@ -7238,39 +7566,10 @@ BOOLEAN_T esaio_completion_recv(ESAIOThreadData* dataP, ("WIN-ESAIO", "esaio_completion_recv(%d) -> no error" "\r\n", descP->sock) ); MLOCK(descP->readMtx); - if (esock_reader_get(env, descP, - &opDataP->recvRef, - opCaller, - &req)) { - if (IS_OPEN(descP->readState)) { - esaio_completion_recv_completed(env, descP, - ovl, - opEnv, opCaller, opDataP, - &req); - } else { - /* A completed (active) request for a socket that is not open. - * Is this even possible? - * A race (completed just as the socket was closed). - */ - esaio_completion_recv_not_active(descP); - FREE_BIN( &opDataP->buf ); - } - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->readersQ.first == NULL) { - descP->readState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_recv_success(env, descP, ovl, opEnv, + opCaller, opDataP); - } else { - /* Request was actually completed directly - * (and was therefor not put into the "queue") - * => Nothing to do here, other than cleanup (see below). - * => But we do not free the "buffer" since it was "used up" - * when we (as assumed) got the result (directly)... - */ - } MUNLOCK(descP->readMtx); break; @@ -7282,55 +7581,9 @@ BOOLEAN_T esaio_completion_recv(ESAIOThreadData* dataP, /* *** SAME MTX LOCK ORDER FOR ALL OPs *** */ MLOCK(descP->readMtx); MLOCK(descP->writeMtx); - /* The only thing *we* do that could cause an abort is the - * 'CancelIoEx' call, which we do when closing the socket - * (or cancel a request). - * But if we have done that; - * - Socket state will not be 'open' and - * - we have also set closer (pid and ref). - */ - - if (esock_reader_get(env, descP, - &opDataP->recvRef, - opCaller, - &req)) { - reason = esock_atom_closed, + esaio_completion_recv_aborted(env, descP, opCaller, opDataP); - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - - /* The socket not being open (assumed closing), - * means we are in the closing phase... - */ - if (! IS_OPEN(descP->readState)) { - - /* We can only send the 'close' message to the closer - * when all requests has been processed! - */ - - /* Check "our" queue */ - if (descP->readersQ.first == NULL) { - - /* Check "other" queue(s) and if there is a closer pid */ - if ((descP->writersQ.first == NULL) && - (descP->acceptorsQ.first == NULL)) { - - esaio_stop(env, descP); - - } - } - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->readersQ.first == NULL) { - descP->readState &= ~ESOCK_STATE_SELECTED; - } - } - FREE_BIN( &opDataP->buf ); MUNLOCK(descP->writeMtx); MUNLOCK(descP->readMtx); break; @@ -7341,36 +7594,9 @@ BOOLEAN_T esaio_completion_recv(ESAIOThreadData* dataP, "esaio_completion_recv(%d) -> operation unknown failure" "\r\n", descP->sock) ); MLOCK(descP->readMtx); - /* We do not know what this is - * but we can "assume" that the request failed so we need to - * remove it from the "queue" if its still there... - * And cleanup... - */ - if (esock_reader_get(env, descP, - &opDataP->recvRef, - opCaller, - &req)) { - /* Figure out the reason */ - reason = MKT2(env, - esock_atom_get_overlapped_result, - ENO2T(env, error)); - - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - esaio_completion_recv_fail(env, descP, error, FALSE); - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->readersQ.first == NULL) { - descP->readState &= ~ESOCK_STATE_SELECTED; - } - - } else { - esaio_completion_recv_fail(env, descP, error, TRUE); - } - FREE_BIN( &opDataP->buf ); + + esaio_completion_recv_failure(env, descP, opCaller, opDataP, error); + MUNLOCK(descP->readMtx); break; } @@ -7385,13 +7611,187 @@ BOOLEAN_T esaio_completion_recv(ESAIOThreadData* dataP, esock_free_env("esaio_completion_recv - op cleanup", opEnv); SSDBG( descP, - ("WIN-ESAIO", "esaio_completion_recv {%d} -> done\r\n", + ("WIN-ESAIO", "esaio_completion_recv(%d) -> done\r\n", descP->sock) ); return FALSE; } +static +void esaio_completion_recv_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataRecv* opDataP) +{ + ESockRequestor req; + + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + if (IS_OPEN(descP->readState)) { + esaio_completion_recv_completed(env, descP, ovl, opEnv, + opCaller, opDataP, + &req); + } else { + /* A completed (active) request for a socket that is not open. + * Is this even possible? + * A race (completed just as the socket was closed). + */ + esaio_completion_recv_not_active(descP); + FREE_BIN( &opDataP->buf ); + } + + } else { + /* Request was actually completed directly + * (and was therefor not put into the "queue") + * => Nothing to do here, other than cleanup (see below). + * => But we do not free the "buffer" since it was "used up" + * when we (as assumed) got the result (directly)... + */ + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recv_success(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + + +/* *** esaio_completion_recv_aborted *** + * The only thing *we* do that could cause an abort is the + * 'CancelIoEx' call, which we do when closing the socket + * (or cancel a request). + * But if we have done that; + * - Socket state will not be 'open' and + * - we have also set closer (pid and ref). + */ + +static +void esaio_completion_recv_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecv* opDataP) +{ + ESockRequestor req; + + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + + ERL_NIF_TERM reason = esock_atom_closed; + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + + /* The socket not being open (assumed closing), + * means we are in the closing phase... + */ + if (! IS_OPEN(descP->readState)) { + + /* We can only send the 'close' message to the closer + * when all requests has been processed! + */ + + /* Check "our" queue */ + if (descP->readersQ.first == NULL) { + + /* Check "other" queue(s) and if there is a closer pid */ + if ((descP->writersQ.first == NULL) && + (descP->acceptorsQ.first == NULL)) { + + esaio_stop(env, descP); + + } + } + } + } + + FREE_BIN( &opDataP->buf ); + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recv_aborted(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + + +/* *** esaio_completion_recv_failure * + * A "general" failure happened while performing the 'recv' operation. + */ +static +void esaio_completion_recv_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecv* opDataP, + int error) +{ + ESockRequestor req; + ERL_NIF_TERM reason; + + /* We do not know what this is + * but we can "assume" that the request failed so we need to + * remove it from the "queue" if its still there... + * And cleanup... + */ + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + /* Figure out the reason */ + reason = MKT2(env, + esock_atom_completion_status, + ENO2T(env, error)); + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + esaio_completion_recv_fail(env, descP, error, FALSE); + + } else { + esaio_completion_recv_fail(env, descP, error, TRUE); + } + + FREE_BIN( &opDataP->buf ); + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recv_failure(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + /* *** esaio_completion_recv_completed *** * The recv request has completed. @@ -7863,38 +8263,24 @@ BOOLEAN_T esaio_completion_recvfrom(ESAIOThreadData* dataP, ("WIN-ESAIO", "esaio_completion_recvfrom(%d) -> no error" "\r\n", descP->sock) ); MLOCK(descP->readMtx); - if (esock_reader_get(env, descP, - &opDataP->recvRef, - opCaller, - &req)) { - if (IS_OPEN(descP->readState)) { - esaio_completion_recvfrom_completed(env, descP, - ovl, opEnv, opCaller, - opDataP, &req); - } else { - /* A completed (active) request for a socket that is not open. - * Is this even possible? - * A race (completed just as the socket was closed). - */ - esaio_completion_recv_not_active(descP); - FREE_BIN( &opDataP->buf ); - } - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->readersQ.first == NULL) { - descP->readState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_recvfrom_success(env, descP, ovl, opEnv, + opCaller, opDataP); + + MUNLOCK(descP->readMtx); + break; + + case ERROR_MORE_DATA: + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recvfrom(%d) -> more data" + "\r\n", descP->sock) ); + MLOCK(descP->readMtx); + + esaio_completion_recvfrom_more_data(env, descP, + opEnv, opCaller, opDataP, + error); - } else { - /* Request was actually completed directly - * (and was therefor not put into the "queue") - * => Nothing to do here, other than cleanup (see below). - * => But we do not free the "buffer" since it was "used up" - * when we (as assumed) got the result (directly)... - */ - } MUNLOCK(descP->readMtx); break; @@ -7906,56 +8292,9 @@ BOOLEAN_T esaio_completion_recvfrom(ESAIOThreadData* dataP, /* *** SAME MTX LOCK ORDER FOR ALL OPs *** */ MLOCK(descP->readMtx); MLOCK(descP->writeMtx); - /* The only thing *we* do that could cause an abort is the - * 'CancelIoEx' call, which we do when closing the socket - * (or cancel a request). - * But if we have done that; - * - Socket state will not be 'open' and - * - we have also set closer (pid and ref). - */ - - if (esock_reader_get(env, descP, - &opDataP->recvRef, - opCaller, - &req)) { - - reason = esock_atom_closed, - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); + esaio_completion_recvfrom_aborted(env, descP, opCaller, opDataP); - /* The socket not being open (assumed closing), - * means we are in the closing phase... - */ - if (! IS_OPEN(descP->readState)) { - - /* We can only send the 'close' message to the closer - * when all requests has been processed! - */ - - /* Check "our" queue */ - if (descP->readersQ.first == NULL) { - - /* Check "other" queue(s) and if there is a closer pid */ - if ((descP->writersQ.first == NULL) && - (descP->acceptorsQ.first == NULL)) { - - esaio_stop(env, descP); - - } - } - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->readersQ.first == NULL) { - descP->readState &= ~ESOCK_STATE_SELECTED; - } - - } - FREE_BIN( &opDataP->buf ); MUNLOCK(descP->writeMtx); MUNLOCK(descP->readMtx); break; @@ -7966,36 +8305,9 @@ BOOLEAN_T esaio_completion_recvfrom(ESAIOThreadData* dataP, "esaio_completion_recvfrom(%d) -> operation unknown failure" "\r\n", descP->sock) ); MLOCK(descP->readMtx); - /* We do not know what this is - * but we can "assume" that the request failed so we need to - * remove it from the "queue" if its still there... - * And cleanup... - */ - if (esock_reader_get(env, descP, - &opDataP->recvRef, - opCaller, - &req)) { - - reason = MKT2(env, - esock_atom_get_overlapped_result, - ENO2T(env, error)); - - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - esaio_completion_recvfrom_fail(env, descP, error, FALSE); - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->readersQ.first == NULL) { - descP->readState &= ~ESOCK_STATE_SELECTED; - } - } else { - esaio_completion_recvfrom_fail(env, descP, error, TRUE); - } - FREE_BIN( &opDataP->buf ); + esaio_completion_recvfrom_failure(env, descP, opCaller, opDataP, error); + MUNLOCK(descP->readMtx); break; } @@ -8015,6 +8327,250 @@ BOOLEAN_T esaio_completion_recvfrom(ESAIOThreadData* dataP, } +static +void esaio_completion_recvfrom_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataRecvFrom* opDataP) +{ + ESockRequestor req; + + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + if (IS_OPEN(descP->readState)) { + esaio_completion_recvfrom_completed(env, descP, + ovl, opEnv, opCaller, + opDataP, &req); + } else { + /* A completed (active) request for a socket that is not open. + * Is this even possible? + * A race (completed just as the socket was closed). + */ + esaio_completion_recv_not_active(descP); + FREE_BIN( &opDataP->buf ); + } + + } else { + /* Request was actually completed directly + * (and was therefor not put into the "queue") + * => Nothing to do here, other than cleanup (see below). + * => But we do not free the "buffer" since it was "used up" + * when we (as assumed) got the result (directly)... + */ + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recvfrom_success(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + + +static +void esaio_completion_recvfrom_more_data(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataRecvFrom* opDataP, + int error) +{ + ESockRequestor req; + + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + if (IS_OPEN(descP->readState)) { + /* We do not actually need to call this function + * since we already know its 'more_data', but just + * get the same format... + */ + ERL_NIF_TERM reason = MKT2(env, + esock_atom_completion_status, + ENO2T(env, error)); + ERL_NIF_TERM completionStatus = esock_make_error(env, reason); + ERL_NIF_TERM completionInfo = MKT2(opEnv, + opDataP->recvRef, + completionStatus); + + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recvfrom_more_data(%d) -> " + "send completion message: " + "\r\n Completion Status: %T" + "\r\n", descP->sock, completionStatus) ); + + /* Send a 'recvfrom' completion message */ + esaio_send_completion_msg(env, // Send env + descP, // Descriptor + opCaller, // Msg destination + opEnv, // Msg env + opDataP->sockRef, // Dest socket + completionInfo); // Info + + } + + FREE_BIN( &opDataP->buf ); + + } else { + /* Request was actually completed directly + * (and was therefor not put into the "queue") + * => Nothing to do here, other than cleanup (see below). + * => But we do not free the "buffer" since it was "used up" + * when we (as assumed) got the result (directly)... + */ + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recvfrom_more_data(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + + +/* *** esaio_completion_recvfrom_aborted *** + * The only thing *we* do that could cause an abort is the + * 'CancelIoEx' call, which we do when closing the socket + * (or cancel a request). + * But if we have done that; + * - Socket state will not be 'open' and + * - we have also set closer (pid and ref). + */ +static +void esaio_completion_recvfrom_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecvFrom* opDataP) +{ + ESockRequestor req; + + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + + ERL_NIF_TERM reason = esock_atom_closed; + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + + /* The socket not being open (assumed closing), + * means we are in the closing phase... + */ + if (! IS_OPEN(descP->readState)) { + + /* We can only send the 'close' message to the closer + * when all requests has been processed! + */ + + /* Check "our" queue */ + if (descP->readersQ.first == NULL) { + + /* Check "other" queue(s) and if there is a closer pid */ + if ((descP->writersQ.first == NULL) && + (descP->acceptorsQ.first == NULL)) { + + esaio_stop(env, descP); + + } + } + } + } + + FREE_BIN( &opDataP->buf ); + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recvfrom_aborted(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + + +/* *** esaio_completion_recvfrom_failure * + * A "general" failure happened while performing the 'recvfrom' operation. + */ +static +void esaio_completion_recvfrom_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecvFrom* opDataP, + int error) +{ + ESockRequestor req; + ERL_NIF_TERM reason; + + /* We do not know what this is + * but we can "assume" that the request failed so we need to + * remove it from the "queue" if its still there... + * And cleanup... + */ + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + + reason = MKT2(env, + esock_atom_completion_status, + ENO2T(env, error)); + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + esaio_completion_recvfrom_fail(env, descP, error, FALSE); + + } else { + esaio_completion_recvfrom_fail(env, descP, error, TRUE); + } + + FREE_BIN( &opDataP->buf ); + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recvfrom_failure(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + /* *** esaio_completion_recvfrom_completed *** * The recvfrom request has completed. @@ -8329,40 +8885,10 @@ BOOLEAN_T esaio_completion_recvmsg(ESAIOThreadData* dataP, descP->sock, opDataP->recvRef, MKPID(env, opCaller)) ); MLOCK(descP->readMtx); - if (esock_reader_get(env, descP, - &opDataP->recvRef, - opCaller, - &req)) { - if (IS_OPEN(descP->readState)) { - esaio_completion_recvmsg_completed(env, descP, - ovl, opEnv, opCaller, - opDataP, - &req); - } else { - /* A completed (active) request for a socket that is not open. - * Is this even possible? - * A race (completed just as the socket was closed). - */ - esaio_completion_recv_not_active(descP); - FREE_BIN( &opDataP->data[0] ); - FREE_BIN( &opDataP->ctrl ); - } - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->readersQ.first == NULL) { - descP->readState &= ~ESOCK_STATE_SELECTED; - } + esaio_completion_recvmsg_success(env, descP, ovl, opEnv, + opCaller, opDataP); - } else { - /* Request was actually completed directly - * (and was therefor not put into the "queue") - * => Nothing to do here, other than cleanup (see below). - * => But we do not free the "buffer" since it was "used up" - * when we (as assumed) got the result (directly)... - */ - } MUNLOCK(descP->readMtx); break; @@ -8374,57 +8900,9 @@ BOOLEAN_T esaio_completion_recvmsg(ESAIOThreadData* dataP, /* *** SAME MTX LOCK ORDER FOR ALL OPs *** */ MLOCK(descP->readMtx); MLOCK(descP->writeMtx); - /* The only thing *we* do that could cause an abort is the - * 'CancelIoEx' call, which we do when closing the socket - * (or cancel a request). - * But if we have done that; - * - Socket state will not be 'open' and - * - we have also set closer (pid and ref). - */ - - if (esock_reader_get(env, descP, - &opDataP->recvRef, - opCaller, - &req)) { - - reason = esock_atom_closed, - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); + esaio_completion_recvmsg_aborted(env, descP, opCaller, opDataP); - /* The socket not being open (assumed closing), - * means we are in the closing phase... - */ - if (! IS_OPEN(descP->readState)) { - - /* We can only send the 'close' message to the closer - * when all requests has been processed! - */ - - /* Check "our" queue */ - if (descP->readersQ.first == NULL) { - - /* Check "other" queue(s) and if there is a closer pid */ - if ((descP->writersQ.first == NULL) && - (descP->acceptorsQ.first == NULL)) { - - esaio_stop(env, descP); - - } - } - } - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->readersQ.first == NULL) { - descP->readState &= ~ESOCK_STATE_SELECTED; - } - - } - FREE_BIN( &opDataP->data[0] ); - FREE_BIN( &opDataP->ctrl ); MUNLOCK(descP->writeMtx); MUNLOCK(descP->readMtx); break; @@ -8432,40 +8910,12 @@ BOOLEAN_T esaio_completion_recvmsg(ESAIOThreadData* dataP, default: SSDBG( descP, ("WIN-ESAIO", - "esaio_completion_recvmsg(%d) -> operation unknown failure" + "esaio_completion_recvmsg(%d) -> unknown operation failure" "\r\n", descP->sock) ); MLOCK(descP->readMtx); - /* We do not know what this is - * but we can "assume" that the request failed so we need to - * remove it from the "queue" if its still there... - * And cleanup... - */ - if (esock_reader_get(env, descP, - &opDataP->recvRef, - opCaller, - &req)) { - - reason = MKT2(env, - esock_atom_get_overlapped_result, - ENO2T(env, error)); - - /* Inform the user waiting for a reply */ - esock_send_abort_msg(env, descP, opDataP->sockRef, - &req, reason); - esaio_completion_recvmsg_fail(env, descP, error, FALSE); - - /* *Maybe* update socket (write) state - * (depends on if the queue is now empty) - */ - if (descP->readersQ.first == NULL) { - descP->readState &= ~ESOCK_STATE_SELECTED; - } - } else { - esaio_completion_recvmsg_fail(env, descP, error, TRUE); - } - FREE_BIN( &opDataP->data[0] ); - FREE_BIN( &opDataP->ctrl ); + esaio_completion_recvmsg_failure(env, descP, opCaller, opDataP, error); + MUNLOCK(descP->readMtx); break; } @@ -8487,6 +8937,184 @@ BOOLEAN_T esaio_completion_recvmsg(ESAIOThreadData* dataP, } +static +void esaio_completion_recvmsg_success(ErlNifEnv* env, + ESockDescriptor* descP, + OVERLAPPED* ovl, + ErlNifEnv* opEnv, + ErlNifPid* opCaller, + ESAIOOpDataRecvMsg* opDataP) +{ + ESockRequestor req; + + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + if (IS_OPEN(descP->readState)) { + esaio_completion_recvmsg_completed(env, descP, ovl, opEnv, + opCaller, opDataP, + &req); + } else { + /* A completed (active) request for a socket that is not open. + * Is this even possible? + * A race (completed just as the socket was closed). + */ + esaio_completion_recv_not_active(descP); + FREE_BIN( &opDataP->data[0] ); + FREE_BIN( &opDataP->ctrl ); + } + + } else { + /* Request was actually completed directly + * (and was therefor not put into the "queue") + * => Nothing to do here, other than cleanup (see below). + * => But we do not free the "buffer" since it was "used up" + * when we (as assumed) got the result (directly)... + */ + } + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recvmsg_success(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + + +/* *** esaio_completion_recvmsg_aborted *** + * The only thing *we* do that could cause an abort is the + * 'CancelIoEx' call, which we do when closing the socket + * (or cancel a request). + * But if we have done that; + * - Socket state will not be 'open' and + * - we have also set closer (pid and ref). + */ + +static +void esaio_completion_recvmsg_aborted(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecvMsg* opDataP) +{ + ESockRequestor req; + + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + + ERL_NIF_TERM reason = esock_atom_closed; + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + + /* The socket not being open (assumed closing), + * means we are in the closing phase... + */ + if (! IS_OPEN(descP->readState)) { + + /* We can only send the 'close' message to the closer + * when all requests has been processed! + */ + + /* Check "our" queue */ + if (descP->readersQ.first == NULL) { + + /* Check "other" queue(s) and if there is a closer pid */ + if ((descP->writersQ.first == NULL) && + (descP->acceptorsQ.first == NULL)) { + + esaio_stop(env, descP); + + } + } + } + + } + + FREE_BIN( &opDataP->data[0] ); + FREE_BIN( &opDataP->ctrl ); + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recvmsg_aborted(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + + +/* *** esaio_completion_recvmsg_failure * + * A "general" failure happened while performing the 'recvmsg' operation. + */ +static +void esaio_completion_recvmsg_failure(ErlNifEnv* env, + ESockDescriptor* descP, + ErlNifPid* opCaller, + ESAIOOpDataRecvMsg* opDataP, + int error) +{ + ESockRequestor req; + ERL_NIF_TERM reason; + + /* We do not know what this is + * but we can "assume" that the request failed so we need to + * remove it from the "queue" if its still there... + * And cleanup... + */ + if (esock_reader_get(env, descP, + &opDataP->recvRef, + opCaller, + &req)) { + + reason = MKT2(env, + esock_atom_completion_status, + ENO2T(env, error)); + + /* Inform the user waiting for a reply */ + esock_send_abort_msg(env, descP, opDataP->sockRef, + &req, reason); + esaio_completion_recvmsg_fail(env, descP, error, FALSE); + + } else { + esaio_completion_recvmsg_fail(env, descP, error, TRUE); + } + + FREE_BIN( &opDataP->data[0] ); + FREE_BIN( &opDataP->ctrl ); + + /* *Maybe* update socket (write) state + * (depends on if the queue is now empty) + */ + SSDBG( descP, + ("WIN-ESAIO", + "esaio_completion_recvmsg_failure(%d) -> " + "maybe (%s) update (read) state (ox%X)\r\n", + descP->sock, + B2S((descP->readersQ.first == NULL)), descP->readState) ); + if (descP->readersQ.first == NULL) { + descP->readState &= ~ESOCK_STATE_SELECTED; + } + +} + /* *** esaio_completion_recvmsg_completed *** * The recvmsg request has completed. @@ -8802,7 +9430,7 @@ void esaio_completion_fail(ErlNifEnv* env, if (inform) esock_warning_msg("[WIN-ESAIO] Unknown (%s) operation failure: " "\r\n Descriptor: %d" - "\r\n Errno: %T" + "\r\n Error: %T" "\r\n", opStr, descP->sock, ENO2T(env, error)); @@ -8865,10 +9493,12 @@ void esaio_dtor(ErlNifEnv* env, * so we must have closed it properly to get here */ if (! IS_CLOSED(descP->readState) ) - esock_warning_msg("Socket Read State not CLOSED at dtor\r\n"); + esock_warning_msg("Socket Read State not CLOSED (0x%X) " + "at dtor\r\n", descP->readState); if (! IS_CLOSED(descP->writeState) ) - esock_warning_msg("Socket Write State not CLOSED at dtor\r\n"); + esock_warning_msg("Socket Write State not CLOSED (0x%X) " + "at dtor\r\n", descP->writeState); if ( descP->sock != INVALID_SOCKET ) esock_warning_msg("Socket %d still valid\r\n", descP->sock); @@ -8949,7 +9579,21 @@ void esaio_stop(ErlNifEnv* env, err = esock_close_socket(env, descP, FALSE); - if (err != 0) + switch (err) { + case NO_ERROR: + break; + case WSAENOTSOCK: + if (descP->sock != INVALID_SOCKET) + esock_warning_msg("[WIN-ESAIO] Attempt to close an " + "already closed socket" + "\r\n(without a closer process): " + "\r\n Controlling Process: %T" + "\r\n socket fd: %d" + "\r\n", + descP->ctrlPid, descP->sock); + break; + + default: esock_warning_msg("[WIN-ESAIO] Failed closing socket without " "closer process: " "\r\n Controlling Process: %T" @@ -8957,6 +9601,9 @@ void esaio_stop(ErlNifEnv* env, "\r\n Errno: %T" "\r\n", descP->ctrlPid, descP->sock, ENO2T(env, err)); + break; + } + } SSDBG( descP, @@ -9598,3 +10245,6 @@ ERL_NIF_TERM mk_completion_msg(ErlNifEnv* env, return esock_mk_socket_msg(env, sockRef, esock_atom_completion, info); } + + +#endif diff --git a/erts/emulator/sys/unix/erl_unix_sys.h b/erts/emulator/sys/unix/erl_unix_sys.h index c93c0dc5cc..3614aecd17 100644 --- a/erts/emulator/sys/unix/erl_unix_sys.h +++ b/erts/emulator/sys/unix/erl_unix_sys.h @@ -281,7 +281,7 @@ ERTS_GLB_INLINE ErtsSysPerfCounter erts_sys_perf_counter(void); #if ERTS_GLB_INLINE_INCL_FUNC_DEF ERTS_GLB_FORCE_INLINE ErtsSysPerfCounter -erts_sys_perf_counter() +erts_sys_perf_counter(void) { return (*erts_sys_time_data__.r.o.perf_counter)(); } diff --git a/erts/emulator/sys/unix/sys.c b/erts/emulator/sys/unix/sys.c index 3bed966864..210d7a5543 100644 --- a/erts/emulator/sys/unix/sys.c +++ b/erts/emulator/sys/unix/sys.c @@ -1061,7 +1061,7 @@ init_smp_sig_notify(void) { erts_thr_opts_t thr_opts = ERTS_THR_OPTS_DEFAULT_INITER; thr_opts.detached = 1; - thr_opts.name = "sys_sig_dispatcher"; + thr_opts.name = "erts_ssig_disp"; if (pipe(sig_notify_fds) < 0) { erts_exit(ERTS_ABORT_EXIT, @@ -1111,7 +1111,7 @@ erts_sys_main_thread(void) #else /* Become signal receiver thread... */ #ifdef ERTS_ENABLE_LOCK_CHECK - erts_lc_set_thread_name("signal_receiver"); + erts_lc_set_thread_name("main"); #endif #endif smp_sig_notify(0); /* Notify initialized */ diff --git a/erts/emulator/sys/unix/sys_env.c b/erts/emulator/sys/unix/sys_env.c index 4d8301f985..eb98e7b635 100644 --- a/erts/emulator/sys/unix/sys_env.c +++ b/erts/emulator/sys/unix/sys_env.c @@ -16,7 +16,7 @@ extern char **environ; static void import_initial_env(void); -void erts_sys_env_init() { +void erts_sys_env_init(void) { erts_rwmtx_init(&sysenv_rwmtx, "environ", NIL, ERTS_LOCK_FLAGS_PROPERTY_STATIC | ERTS_LOCK_FLAGS_CATEGORY_GENERIC); @@ -24,21 +24,21 @@ void erts_sys_env_init() { import_initial_env(); } -const erts_osenv_t *erts_sys_rlock_global_osenv() { +const erts_osenv_t *erts_sys_rlock_global_osenv(void) { erts_rwmtx_rlock(&sysenv_rwmtx); return &sysenv_global_env; } -erts_osenv_t *erts_sys_rwlock_global_osenv() { +erts_osenv_t *erts_sys_rwlock_global_osenv(void) { erts_rwmtx_rwlock(&sysenv_rwmtx); return &sysenv_global_env; } -void erts_sys_rwunlock_global_osenv() { +void erts_sys_rwunlock_global_osenv(void) { erts_rwmtx_rwunlock(&sysenv_rwmtx); } -void erts_sys_runlock_global_osenv() { +void erts_sys_runlock_global_osenv(void) { erts_rwmtx_runlock(&sysenv_rwmtx); } diff --git a/erts/emulator/test/bs_utf_SUITE.erl b/erts/emulator/test/bs_utf_SUITE.erl index 9e2699119b..4a16ef44ab 100644 --- a/erts/emulator/test/bs_utf_SUITE.erl +++ b/erts/emulator/test/bs_utf_SUITE.erl @@ -356,6 +356,10 @@ utf32_illegal_sequences(Config) when is_list(Config) -> utf32_fail_range(16#10FFFF+1, 16#10FFFF+512), %Too large. utf32_fail_range(16#D800, 16#DFFF), %Reserved for UTF-16. utf32_fail_range(-100, -1), + + <<>> = id(<< 0 || <<X/utf32>> <= <<"àxxx">>, _ = X >>), + <<>> = id(<< 0 || <<X/little-utf32>> <= <<"àxxx">>, _ = X >>), + ok. utf32_fail_range(Char, End) when Char =< End -> diff --git a/erts/emulator/test/decode_packet_SUITE.erl b/erts/emulator/test/decode_packet_SUITE.erl index 306c1193b0..55bc6eb06b 100644 --- a/erts/emulator/test/decode_packet_SUITE.erl +++ b/erts/emulator/test/decode_packet_SUITE.erl @@ -1,7 +1,7 @@ %% %% %CopyrightBegin% %% -%% Copyright Ericsson AB 2008-2022. All Rights Reserved. +%% Copyright Ericsson AB 2008-2023. All Rights Reserved. %% %% Licensed under the Apache License, Version 2.0 (the "License"); %% you may not use this file except in compliance with the License. diff --git a/erts/emulator/test/distribution_SUITE.erl b/erts/emulator/test/distribution_SUITE.erl index f7fa527bd5..d577f2a1e5 100644 --- a/erts/emulator/test/distribution_SUITE.erl +++ b/erts/emulator/test/distribution_SUITE.erl @@ -84,7 +84,9 @@ dyn_node_name_monitor/1, async_dist_flag/1, async_dist_port_dctrlr/1, - async_dist_proc_dctrlr/1]). + async_dist_proc_dctrlr/1, + creation_selection/1, + creation_selection_test/1]). %% Internal exports. -export([sender/3, receiver2/2, dummy_waiter/0, dead_process/0, @@ -118,7 +120,7 @@ all() -> start_epmd_false, no_epmd, epmd_module, system_limit, hopefull_data_encoding, hopefull_export_fun_bug, huge_iovec, is_alive, dyn_node_name_monitor_node, dyn_node_name_monitor, - {group, async_dist}]. + {group, async_dist}, creation_selection]. groups() -> [{bulk_send, [], [bulk_send_small, bulk_send_big, bulk_send_bigbig]}, @@ -520,9 +522,6 @@ nodes2(Config) when is_list(Config) -> end, ok. -id(X) -> - X. - %% Test optimistic distribution flags toward pending connections (DFLAG_DIST_HOPEFULLY) optimistic_dflags(Config) when is_list(Config) -> {ok, PeerSender, _Sender} = ?CT_PEER(#{connection => 0, args => ["-setcookie", "NONE"]}), @@ -3631,8 +3630,126 @@ async_dist_test(Node) -> ok. +creation_selection(Config) when is_list(Config) -> + register(creation_selection_test_supervisor, self()), + Name = atom_to_list(?FUNCTION_NAME) ++ "-" + ++ integer_to_list(erlang:system_time()), + Host = hostname(), + Cmd = lists:append( + [ct:get_progname(), + " -noshell", + " -setcookie ", atom_to_list(erlang:get_cookie()), + " -pa ", filename:dirname(code:which(?MODULE)), + " -s ", atom_to_list(?MODULE), " ", + " creation_selection_test ", atom_to_list(node()), " ", + atom_to_list(net_kernel:longnames()), " ", Name, " ", Host]), + ct:pal("Node command: ~p~n", [Cmd]), + Port = open_port({spawn, Cmd}, [exit_status]), + Node = list_to_atom(lists:append([Name, "@", Host])), + ok = receive_creation_selection_info(Port, Node). + +receive_creation_selection_info(Port, Node) -> + receive + {creation_selection_test, Node, Creations, InvalidCreation, + ClashResolvedCreation} = Msg -> + ct:log("Test result: ~p~n", [Msg]), + %% Verify that creation values are created as expected. The + %% list of creations is in reverse start order... + MaxC = (1 bsl 32) - 1, + MinC = 4, + StartOrderCreations = lists:reverse(Creations), + InvalidCreation = lists:foldl(fun (C, C) when is_integer(C), + MinC =< C, + C =< MaxC -> + %% Return next expected + %% creation... + if C == MaxC -> MinC; + true -> C+1 + end + end, + hd(StartOrderCreations), + StartOrderCreations), + false = lists:member(ClashResolvedCreation, [InvalidCreation + | Creations]), + receive + {Port, {exit_status, 0}} -> + Port ! {self(), close}, + ok; + {Port, {exit_status, EStat}} -> + ct:fail({"node exited abnormally: ", EStat}) + end; + {Port, {exit_status, EStat}} -> + ct:fail({"node prematurely exited: ", EStat}); + {Port, {data, Data}} -> + ct:log("~ts", [Data]), + receive_creation_selection_info(Port, Node) + end, + ok. + +creation_selection_test([TestSupNode, LongNames, Name, Host]) -> + try + StartArgs = [Name, + case LongNames of + true -> longnames; + false -> shortnames + end], + Node = list_to_atom(lists:append([atom_to_list(Name), + "@", atom_to_list(Host)])), + GoDistributed = fun (F) -> + {ok, _} = net_kernel:start(StartArgs), + Node = node(), + Creation = erlang:system_info(creation), + _ = F(Creation), + net_kernel:stop(), + Creation + end, + %% We start multiple times to verify that the creation values + %% we get from epmd are delivered in sequence. This is a + %% must for the test case such as it is written now, but can be + %% changed. If changed, this test case must be updated... + {Creations, + LastCreation} = lists:foldl(fun (_, {Cs, _LC}) -> + CFun = fun (X) -> X end, + C = GoDistributed(CFun), + {[C|Cs], C} + end, {[], 0}, lists:seq(1, 5)), + %% We create a pid with the creation that epmd will offer us the next + %% time we start the distribution and then start the distribution + %% once more. The node should avoid this creation, since this would + %% cause external identifiers in the system with same + %% nodename/creation pair as used by the local node, which in turn + %% would cause these identifers not to work as expected. That is, the + %% node should silently reject this creation and chose another one when + %% starting the distribution. + InvalidCreation = LastCreation+1, + Pid = erts_test_utils:mk_ext_pid({Node, InvalidCreation}, 4711, 0), + true = erts_debug:size(Pid) > 0, %% External pid + ResultFun = fun (ClashResolvedCreation) -> + pong = net_adm:ping(TestSupNode), + Msg = {creation_selection_test, node(), Creations, + InvalidCreation, ClashResolvedCreation}, + {creation_selection_test_supervisor, TestSupNode} + ! Msg, + %% Wait a bit so the message have time to get + %% through before we take down the distribution... + receive after 500 -> ok end + end, + _ = GoDistributed(ResultFun), + %% Ensure Pid is not garbage collected before starting the + %% distribution... + _ = id(Pid), + erlang:halt(0) + catch + Class:Reason:StackTrace -> + erlang:display({Class, Reason, StackTrace}), + erlang:halt(17) + end. + %%% Utilities +id(X) -> + X. + wait_until(Fun) -> wait_until(Fun, 24*60*60*1000). diff --git a/erts/emulator/test/map_SUITE.erl b/erts/emulator/test/map_SUITE.erl index 030ad5ee13..5d7546c1a4 100644 --- a/erts/emulator/test/map_SUITE.erl +++ b/erts/emulator/test/map_SUITE.erl @@ -276,6 +276,8 @@ t_build_and_match_literals_large(Config) when is_list(Config) -> 60 = map_size(M0), 60 = maps:size(M0), + 60 = apply(erlang, id(map_size), [M0]), + 60 = apply(maps, id(size), [M0]), % with repeating M1 = id(#{ 10=>a0,20=>b0,30=>"c0","40"=>"d0",<<"50">>=>"e0",{["00"]}=>"10", @@ -312,6 +314,8 @@ t_build_and_match_literals_large(Config) when is_list(Config) -> 60 = map_size(M1), 60 = maps:size(M1), + 60 = apply(erlang, id(map_size), [M1]), + 60 = apply(maps, id(size), [M1]), % with floats @@ -365,6 +369,8 @@ t_build_and_match_literals_large(Config) when is_list(Config) -> 90 = map_size(M2), 90 = maps:size(M2), + 90 = apply(erlang, id(map_size), [M2]), + 90 = apply(maps, id(size), [M2]), % with bignums M3 = id(#{ 10=>a0,20=>b0,30=>"c0","40"=>"d0",<<"50">>=>"e0",{["00"]}=>"10", @@ -428,6 +434,8 @@ t_build_and_match_literals_large(Config) when is_list(Config) -> 98 = map_size(M3), 98 = maps:size(M3), + 98 = apply(erlang, id(map_size), [M3]), + 98 = apply(maps, id(size), [M3]), %% with maps @@ -548,6 +556,8 @@ t_build_and_match_literals_large(Config) when is_list(Config) -> 95 = map_size(M4), 95 = maps:size(M4), + 95 = apply(erlang, id(map_size), [M4]), + 95 = apply(maps, id(size), [M4]), % call for value @@ -645,6 +655,8 @@ t_build_and_match_literals_large(Config) when is_list(Config) -> 95 = map_size(M5), 95 = maps:size(M5), + 95 = apply(erlang, id(map_size), [M5]), + 95 = apply(maps, id(size), [M5]), %% remember @@ -2220,6 +2232,10 @@ t_bif_map_merge(Config) when is_list(Config) -> {'EXIT',{{badmap,T},[{maps,merge,_,_}|_]}} = (catch maps:merge(T, #{})), {'EXIT',{{badmap,T},[{maps,merge,_,_}|_]}} = + (catch maps:merge(M11, T)), + {'EXIT',{{badmap,T},[{maps,merge,_,_}|_]}} = + (catch maps:merge(T, M11)), + {'EXIT',{{badmap,T},[{maps,merge,_,_}|_]}} = (catch maps:merge(T, T)) end), ok. @@ -2462,6 +2478,16 @@ t_bif_erlang_phash2() -> 70249457 = erlang:phash2(M0), % 118679416 59617982 = erlang:phash2(M1), % 51612236 70249457 = erlang:phash2(M2), % 118679416 + + M1000 = maps:from_list([{K,K} || K <- lists:seq(1,1000)]), + 66609305 = erlang:phash2(M1000), + + Mnested1 = #{flatmap => M0, M0 => flatmap, hashmap => M1000, M1000 => hashmap}, + 113689339 = erlang:phash2(Mnested1), + + Mnested2 = maps:merge(Mnested1, M1000), + 29167443 = erlang:phash2(Mnested2), + ok. t_bif_erlang_phash() -> @@ -2482,6 +2508,16 @@ t_bif_erlang_phash() -> 2620391445 = erlang:phash(M0,Sz), % 3590546636 1670235874 = erlang:phash(M1,Sz), % 4066388227 2620391445 = erlang:phash(M2,Sz), % 3590546636 + + M1000 = maps:from_list([{K,K} || K <- lists:seq(1,1000)]), + 1945662653 = erlang:phash(M1000, Sz), + + Mnested1 = #{flatmap => M0, M0 => flatmap, hashmap => M1000, M1000 => hashmap}, + 113694495 = erlang:phash(Mnested1, Sz), + + Mnested2 = maps:merge(Mnested1, M1000), + 431825783 = erlang:phash(Mnested2, Sz), + ok. t_map_encode_decode(Config) when is_list(Config) -> @@ -2957,25 +2993,31 @@ t_maps_without(_Config) -> %% Verify that the the number of nodes in hashmaps %% of different types and sizes does not deviate too %% much from the theoretical model. +%% For debug with DBG_HASHMAP_COLLISION_BONANZA the test will expect +%% the hashmaps to NOT be well balanced. t_hashmap_balance(_Config) -> + erts_debug:set_internal_state(available_internal_state, true), + ExpectBalance = not erts_debug:get_internal_state(hashmap_collision_bonanza), + hashmap_balance(ExpectBalance), + erts_debug:set_internal_state(available_internal_state, false), + ok. + +hashmap_balance(EB) -> io:format("Integer keys\n", []), - hashmap_balance(fun(I) -> I end), + hashmap_balance(fun(I) -> I end, EB), io:format("Float keys\n", []), - hashmap_balance(fun(I) -> float(I) end), + hashmap_balance(fun(I) -> float(I) end, EB), io:format("String keys\n", []), - hashmap_balance(fun(I) -> integer_to_list(I) end), + hashmap_balance(fun(I) -> integer_to_list(I) end, EB), io:format("Binary (big) keys\n", []), - hashmap_balance(fun(I) -> <<I:16/big>> end), + hashmap_balance(fun(I) -> <<I:16/big>> end, EB), io:format("Binary (little) keys\n", []), - hashmap_balance(fun(I) -> <<I:16/little>> end), + hashmap_balance(fun(I) -> <<I:16/little>> end, EB), io:format("Atom keys\n", []), - erts_debug:set_internal_state(available_internal_state, true), - hashmap_balance(fun(I) -> erts_debug:get_internal_state({atom,I}) end), - erts_debug:set_internal_state(available_internal_state, false), - + hashmap_balance(fun(I) -> erts_debug:get_internal_state({atom,I}) end, EB), ok. -hashmap_balance(KeyFun) -> +hashmap_balance(KeyFun, ExpectBalance) -> %% For uniformly distributed hash values, the average number of nodes N %% in a hashmap varies between 0.3*K and 0.4*K where K is number of keys. %% The standard deviation of N is about sqrt(K)/3. @@ -3019,9 +3061,10 @@ hashmap_balance(KeyFun) -> erts_debug:flat_size(MaxMap)]) end, - true = (MaxDiff < 6), % The probability of this line failing is about 0.000000001 - % for a uniform hash. I've set the probability this "high" for now - % to detect flaws in our make_internal_hash. + %% The probability of this line failing is about 0.000000001 + %% for a uniform hash. I've set the probability this "high" for now + %% to detect flaws in our make_internal_hash. + ExpectBalance = (MaxDiff < 6), ok. hashmap_nodes(M) -> @@ -3030,6 +3073,7 @@ hashmap_nodes(M) -> case element(1,Tpl) of bitmaps -> Acc + element(2,Tpl); arrays -> Acc + element(2,Tpl); + collisions -> Acc + element(2,Tpl); _ -> Acc end end, @@ -3232,7 +3276,7 @@ t_dets(_Config) -> t_tracing(_Config) -> - dbg:stop_clear(), + dbg:stop(), {ok,Tracer} = dbg:tracer(process,{fun trace_collector/2, self()}), dbg:p(self(),c), @@ -3285,7 +3329,7 @@ t_tracing(_Config) -> %% Check to extra messages timeout = getmsg(Tracer), - dbg:stop_clear(), + dbg:stop(), ok. getmsg(_Tracer) -> diff --git a/erts/emulator/test/process_SUITE.erl b/erts/emulator/test/process_SUITE.erl index f6c6526f54..d28c4d15db 100644 --- a/erts/emulator/test/process_SUITE.erl +++ b/erts/emulator/test/process_SUITE.erl @@ -95,6 +95,8 @@ alias_bif/1, monitor_alias/1, spawn_monitor_alias/1, + demonitor_aliasmonitor/1, + down_aliasmonitor/1, monitor_tag/1, no_pid_wrap/1]). @@ -184,7 +186,8 @@ groups() -> gc_request_when_gc_disabled, gc_request_blast_when_gc_disabled, otp_16436, otp_16642]}, {alias, [], - [alias_bif, monitor_alias, spawn_monitor_alias]}]. + [alias_bif, monitor_alias, spawn_monitor_alias, + demonitor_aliasmonitor, down_aliasmonitor]}]. init_per_suite(Config) -> A0 = case application:start(sasl) of @@ -5025,6 +5028,51 @@ spawn_monitor_alias_test(Peer, Node, SpawnType, ExitReason) -> ok end. +demonitor_aliasmonitor(Config) when is_list(Config) -> + {ok, Peer, Node} = ?CT_PEER(), + Fun = fun () -> + receive + {alias, Alias} -> + Alias ! {alias_reply, Alias, self()} + end + end, + LPid = spawn(Fun), + RPid = spawn(Node, Fun), + AliasMonitor = erlang:monitor(process, LPid, [{alias, explicit_unalias}]), + erlang:demonitor(AliasMonitor), + LPid ! {alias, AliasMonitor}, + receive {alias_reply, AliasMonitor, LPid} -> ok end, + %% Demonitor signal has been received and cleaned up. Cleanup of + %% it erroneously removed it from the alias table which caused + %% remote use of the alias to stop working... + RPid ! {alias, AliasMonitor}, + receive {alias_reply, AliasMonitor, RPid} -> ok end, + exit(LPid, kill), + peer:stop(Peer), + false = is_process_alive(LPid), + ok. + +down_aliasmonitor(Config) when is_list(Config) -> + {ok, Peer, Node} = ?CT_PEER(), + LPid = spawn(fun () -> receive infinty -> ok end end), + RPid = spawn(Node, + fun () -> + receive + {alias, Alias} -> + Alias ! {alias_reply, Alias, self()} + end + end), + AliasMonitor = erlang:monitor(process, LPid, [{alias, explicit_unalias}]), + exit(LPid, bye), + receive {'DOWN', AliasMonitor, process, LPid, bye} -> ok end, + %% Down signal has been received and cleaned up. Cleanup of + %% it erroneously removed it from the alias table which caused + %% remote use of the alias to stop working... + RPid ! {alias, AliasMonitor}, + receive {alias_reply, AliasMonitor, RPid} -> ok end, + peer:stop(Peer), + ok. + monitor_tag(Config) when is_list(Config) -> %% Exit signals with immediate exit reasons are sent %% in a different manner than compound exit reasons, and diff --git a/erts/emulator/utils/make_driver_tab b/erts/emulator/utils/make_driver_tab index 4ed9cb0ce8..0749df1cbe 100755 --- a/erts/emulator/utils/make_driver_tab +++ b/erts/emulator/utils/make_driver_tab @@ -107,7 +107,7 @@ foreach (@static_drivers) { } print " {NULL}\n};\n"; -print "void erts_init_static_drivers() {\n"; +print "void erts_init_static_drivers(void) {\n"; my $index = 0; foreach (@static_drivers) { diff --git a/erts/epmd/src/epmd_srv.c b/erts/epmd/src/epmd_srv.c index 18a589ac27..7613533bdd 100644 --- a/erts/epmd/src/epmd_srv.c +++ b/erts/epmd/src/epmd_srv.c @@ -745,12 +745,7 @@ static unsigned int get_creation(Node* node) /* buf is actually one byte larger than bsize, giving place for null termination */ -static void do_request(g, fd, s, buf, bsize) - EpmdVars *g; - int fd; - Connection *s; - char *buf; - int bsize; +static void do_request(EpmdVars *g, int fd, Connection *s, char *buf, int bsize) { char wbuf[OUTBUF_SIZE]; /* Buffer for writing */ int i; diff --git a/erts/etc/common/escript.c b/erts/etc/common/escript.c index fe474338fb..c7418b2ace 100644 --- a/erts/etc/common/escript.c +++ b/erts/etc/common/escript.c @@ -286,7 +286,7 @@ find_prog(char *origpath) beg = end + 1; continue; } - strncpy(dir, beg, sz); + memcpy(dir, beg, sz); dir[sz] = '\0'; beg = end + 1; diff --git a/erts/etc/common/heart.c b/erts/etc/common/heart.c index 297e41dca2..f45c18f3e6 100644 --- a/erts/etc/common/heart.c +++ b/erts/etc/common/heart.c @@ -354,8 +354,7 @@ int main(int argc, char **argv) { * message loop */ static int -message_loop(erlin_fd, erlout_fd) - int erlin_fd, erlout_fd; +message_loop(int erlin_fd, int erlout_fd) { int i; time_t now, last_received; @@ -776,8 +775,7 @@ int wait_until_close_write_or_env_tmo(int tmo) { * Sends an HEART_ACK. */ static int -notify_ack(fd) - int fd; +notify_ack(int fd) { struct msg m; @@ -824,9 +822,7 @@ heart_cmd_reply(int fd, char *s) * FIXME. */ static int -write_message(fd, mp) - int fd; - struct msg *mp; +write_message(int fd, struct msg *mp) { int len = ntohs(mp->len); @@ -853,9 +849,7 @@ write_message(fd, mp) * message. */ static int -read_message(fd, mp) - int fd; - struct msg *mp; +read_message(int fd, struct msg *mp) { int rlen, i; unsigned char* tmp; @@ -891,9 +885,7 @@ read_message(fd, mp) * bytes read (i.e. len) , 0 if eof, or < 0 if error. len must be > 0. */ static int -read_fill(fd, buf, len) - int fd, len; - char *buf; +read_fill(int fd, char *buf, int len) { int i, got = 0; @@ -914,9 +906,7 @@ read_fill(fd, buf, len) * 0 if eof, or < 0 if error. len > maxlen > 0 must hold. */ static int -read_skip(fd, buf, maxlen, len) - int fd, maxlen, len; - char *buf; +read_skip(int fd, char *buf, int maxlen, int len) { int i, got = 0; char c; diff --git a/erts/include/internal/ethread.h b/erts/include/internal/ethread.h index 460849ef2f..e1aa6d4126 100644 --- a/erts/include/internal/ethread.h +++ b/erts/include/internal/ethread.h @@ -497,10 +497,11 @@ typedef struct { typedef struct { int detached; /* boolean (default false) */ int suggested_stack_size; /* kilo words (default sys dependent) */ - char *name; /* max 14 char long (default no-name) */ + char *name; /* max 15 char long (default no-name) */ } ethr_thr_opts; #define ETHR_THR_OPTS_DEFAULT_INITER {0, -1, NULL} +#define ETHR_THR_NAME_MAX 15 #if !defined(ETHR_TRY_INLINE_FUNCS) || defined(ETHR_AUX_IMPL__) # define ETHR_NEED_SPINLOCK_PROTOTYPES__ diff --git a/erts/lib_src/pthread/ethread.c b/erts/lib_src/pthread/ethread.c index b17aa3a17f..da4f1af11d 100644 --- a/erts/lib_src/pthread/ethread.c +++ b/erts/lib_src/pthread/ethread.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2010-2021. All Rights Reserved. + * Copyright Ericsson AB 2010-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,7 +81,7 @@ typedef struct { void *prep_func_res; size_t stacksize; char *name; - char name_buff[32]; + char name_buff[ETHR_THR_NAME_MAX + 1]; } ethr_thr_wrap_data__; static void *thr_wrapper(void *vtwd) @@ -334,21 +334,9 @@ ethr_thr_create(ethr_tid *tid, void * (*func)(void *), void *arg, twd.stacksize = 0; if (opts && opts->name) { - size_t nlen = sizeof(twd.name_buff); -#ifdef __HAIKU__ - if (nlen > B_OS_NAME_LENGTH) - nlen = B_OS_NAME_LENGTH; -#else - /* - * Length of 16 is known to work. At least pthread_setname_np() - * is documented to fail on too long name string, but documentation - * does not say what the limit is. Do not have the time to dig - * further into that now... - */ - if (nlen > 16) - nlen = 16; -#endif - snprintf(twd.name_buff, nlen, "%s", opts->name); + if (strlen(opts->name) >= sizeof(twd.name_buff)) + return EINVAL; + strcpy(twd.name_buff, opts->name); twd.name = twd.name_buff; } else twd.name = NULL; @@ -506,6 +494,8 @@ ethr_getname(ethr_tid tid, char *buf, size_t len) void ethr_setname(char *name) { + if (strlen(name) > ETHR_THR_NAME_MAX) + return; #if defined(ETHR_HAVE_PTHREAD_SETNAME_NP_2) pthread_setname_np(ethr_self(), name); #elif defined(ETHR_HAVE_PTHREAD_SET_NAME_NP_2) diff --git a/erts/preloaded/ebin/prim_socket.beam b/erts/preloaded/ebin/prim_socket.beam Binary files differindex ba50f42a01..45f5ab71d9 100644 --- a/erts/preloaded/ebin/prim_socket.beam +++ b/erts/preloaded/ebin/prim_socket.beam diff --git a/erts/preloaded/src/prim_socket.erl b/erts/preloaded/src/prim_socket.erl index fd4432cbc9..8da47bba82 100644 --- a/erts/preloaded/src/prim_socket.erl +++ b/erts/preloaded/src/prim_socket.erl @@ -512,8 +512,10 @@ send(SockRef, Bin, EFlags, SendRef) when is_integer(EFlags) -> {select, Written} -> <<_:Written/binary, RestBin/binary>> = Bin, {select, RestBin, EFlags}; - completion -> - completion; + + completion = C -> + C; + {error, _Reason} = Result -> Result end; @@ -560,11 +562,13 @@ sendto(SockRef, Bin, To, Flags, SendRef) -> sockaddr -> {error, {invalid, {Cause, To}}} end; + ok -> ok; {ok, Written} -> <<_:Written/binary, RestBin/binary>> = Bin, {ok, RestBin}; + select -> Cont = {To, ETo, EFlags}, {select, Cont}; @@ -572,6 +576,10 @@ sendto(SockRef, Bin, To, Flags, SendRef) -> <<_:Written/binary, RestBin/binary>> = Bin, Cont = {To, ETo, EFlags}, {select, RestBin, Cont}; + + completion = C-> + C; + {error, _Reason} = Result -> Result end diff --git a/erts/test/ethread_SUITE.erl b/erts/test/ethread_SUITE.erl index 19f738c572..4c2c1139df 100644 --- a/erts/test/ethread_SUITE.erl +++ b/erts/test/ethread_SUITE.erl @@ -1,7 +1,7 @@ %% %% %CopyrightBegin% %% -%% Copyright Ericsson AB 2004-2016. All Rights Reserved. +%% Copyright Ericsson AB 2004-2023. All Rights Reserved. %% %% Licensed under the Apache License, Version 2.0 (the "License"); %% you may not use this file except in compliance with the License. @@ -43,7 +43,8 @@ rwspinlock/1, rwmutex/1, atomic/1, - dw_atomic_massage/1]). + dw_atomic_massage/1, + thread_name/1]). -include_lib("common_test/include/ct.hrl"). @@ -65,7 +66,8 @@ all() -> rwspinlock, rwmutex, atomic, - dw_atomic_massage]. + dw_atomic_massage, + thread_name]. init_per_testcase(Case, Config) -> case inet:gethostname() of @@ -158,6 +160,10 @@ atomic(Config) -> dw_atomic_massage(Config) -> run_case(Config, "dw_atomic_massage", ""). +%% Tests thread names. +thread_name(Config) -> + run_case(Config, "thread_name", ""). + %% %% %% Auxiliary functions diff --git a/erts/test/ethread_SUITE_data/ethread_tests.c b/erts/test/ethread_SUITE_data/ethread_tests.c index 048acd6a95..377aabac24 100644 --- a/erts/test/ethread_SUITE_data/ethread_tests.c +++ b/erts/test/ethread_SUITE_data/ethread_tests.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2004-2021. All Rights Reserved. + * Copyright Ericsson AB 2004-2023. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,7 +41,7 @@ #define PRINT_VA_LIST(FRMT) \ do { \ - if (FRMT && FRMT != '\0') { \ + if (FRMT && *(FRMT) != '\0') { \ va_list args; \ va_start(args, FRMT); \ vfprintf(stderr, FRMT, args); \ @@ -1757,6 +1757,7 @@ at_dw_thr(void *vval) break; } } + return NULL; } static void @@ -1783,6 +1784,120 @@ dw_atomic_massage_test(void) } } +static ethr_mutex thread_name_mutex; +static ethr_cond thread_name_cond; +static int thread_name_state; + +static void * +thread_name_thread(void *my_tid) +{ + int res; + + ethr_mutex_lock(&thread_name_mutex); + thread_name_state = 1; + while (thread_name_state == 1) { + res = ethr_cond_wait(&thread_name_cond, &thread_name_mutex); + ASSERT(res == 0); + } + ethr_mutex_unlock(&thread_name_mutex); + return NULL; +} + +static void +thread_name(void) +{ + static const ethr_thr_opts default_thr_opts = ETHR_THR_OPTS_DEFAULT_INITER; + ethr_tid tid; + ethr_thr_opts thr_opts; + int res; + char buf[ETHR_THR_NAME_MAX + 1]; + + res = ethr_mutex_init(&thread_name_mutex); + ASSERT(res == 0); + res = ethr_cond_init(&thread_name_cond); + ASSERT(res == 0); + + if (ethr_getname(ethr_self(), buf, sizeof(buf)) == ENOSYS) { + skip("thread names are not supported"); + return; + } + + /* create a thread with the minimum name length */ + thread_name_state = 0; + + memcpy(&thr_opts, &default_thr_opts, sizeof(thr_opts)); + thr_opts.name = ""; + res = ethr_thr_create(&tid, thread_name_thread, NULL, &thr_opts); + ASSERT(res == 0); + + memset(buf, 0xFF, sizeof(buf)); + res = ethr_getname(tid, buf, sizeof(buf)); + ASSERT(res == 0); + + res = strcmp(buf, ""); + ASSERT(res == 0); + + ethr_mutex_lock(&thread_name_mutex); + thread_name_state = 0; + ethr_cond_signal(&thread_name_cond); + ethr_mutex_unlock(&thread_name_mutex); + + res = ethr_thr_join(tid, NULL); + ASSERT(res == 0); + + /* create a thread with a middling name length */ + thread_name_state = 0; + + memcpy(&thr_opts, &default_thr_opts, sizeof(thr_opts)); + thr_opts.name = "123456789"; + res = ethr_thr_create(&tid, thread_name_thread, NULL, &thr_opts); + ASSERT(res == 0); + + memset(buf, 0xFF, sizeof(buf)); + res = ethr_getname(tid, buf, sizeof(buf)); + ASSERT(res == 0); + + res = strcmp(buf, "123456789"); + ASSERT(res == 0); + + ethr_mutex_lock(&thread_name_mutex); + thread_name_state = 0; + ethr_cond_signal(&thread_name_cond); + ethr_mutex_unlock(&thread_name_mutex); + + res = ethr_thr_join(tid, NULL); + ASSERT(res == 0); + + /* create a thread with the maximum name length */ + thread_name_state = 0; + + memcpy(&thr_opts, &default_thr_opts, sizeof(thr_opts)); + thr_opts.name = "123456789012345"; + res = ethr_thr_create(&tid, thread_name_thread, NULL, &thr_opts); + ASSERT(res == 0); + + memset(buf, 0xFF, sizeof(buf)); + res = ethr_getname(tid, buf, sizeof(buf)); + ASSERT(res == 0); + + res = strcmp(buf, "123456789012345"); + ASSERT(res == 0); + + ethr_mutex_lock(&thread_name_mutex); + thread_name_state = 0; + ethr_cond_signal(&thread_name_cond); + ethr_mutex_unlock(&thread_name_mutex); + + res = ethr_thr_join(tid, NULL); + ASSERT(res == 0); + + /* create a thread with an over-sized name length */ + memcpy(&thr_opts, &default_thr_opts, sizeof(thr_opts)); + thr_opts.name = "1234567890123456"; + res = ethr_thr_create(&tid, thread_name_thread, NULL, &thr_opts); + ASSERT(res == EINVAL); +} + void * at_thread(void *unused) { @@ -1958,6 +2073,8 @@ main(int argc, char *argv[]) atomic_test(); else if (strcmp(testcase, "dw_atomic_massage") == 0) dw_atomic_massage_test(); + else if (strcmp(testcase, "thread_name") == 0) + thread_name(); else skip("Test case \"%s\" not implemented yet", testcase); diff --git a/erts/test/otp_SUITE.erl b/erts/test/otp_SUITE.erl index 40e2be0a85..a8424ea46f 100644 --- a/erts/test/otp_SUITE.erl +++ b/erts/test/otp_SUITE.erl @@ -542,7 +542,7 @@ ignore_undefs() -> %% The following functions are optional dependencies for diameter #{{dbg,ctp,0} => true, {dbg,p,2} => true, - {dbg,stop_clear,0} => true, + {dbg,stop,0} => true, {dbg,trace_port,2} => true, {dbg,tracer,2} => true, {erl_prettypr,format,1} => true, diff --git a/erts/vsn.mk b/erts/vsn.mk index c9cabd73d4..67df301d2e 100644 --- a/erts/vsn.mk +++ b/erts/vsn.mk @@ -18,7 +18,7 @@ # %CopyrightEnd% # -VSN = 13.2 +VSN = 13.2.2 # Port number 4365 in 4.2 # Port number 4366 in 4.3 |