[njs] Modules: fixed clear() method of a shared dictionary without timeout.

2024-03-20 Thread Dmitry Volyntsev
details:   https://hg.nginx.org/njs/rev/f632fe16ba05
branches:  
changeset: 2303:f632fe16ba05
user:  Dmitry Volyntsev 
date:  Tue Mar 19 21:05:51 2024 -0700
description:
Modules: fixed clear() method of a shared dictionary without timeout.

Previously, the code did not unlock the rwlock when a dict was empty.

The issue was introduced in 4a15613f4e8b (0.8.3).

This closes issue #699 on Github.

diffstat:

 nginx/ngx_js_shared_dict.c |  4 +++-
 nginx/t/js_shared_dict.t   |  1 +
 2 files changed, 4 insertions(+), 1 deletions(-)

diffs (32 lines):

diff -r 85313a068147 -r f632fe16ba05 nginx/ngx_js_shared_dict.c
--- a/nginx/ngx_js_shared_dict.cMon Mar 18 22:24:57 2024 -0700
+++ b/nginx/ngx_js_shared_dict.cTue Mar 19 21:05:51 2024 -0700
@@ -479,7 +479,7 @@ njs_js_ext_shared_dict_clear(njs_vm_t *v
 rbtree = >sh->rbtree;
 
 if (rbtree->root == rbtree->sentinel) {
-return NJS_OK;
+goto done;
 }
 
 for (rn = ngx_rbtree_min(rbtree->root, rbtree->sentinel);
@@ -494,6 +494,8 @@ njs_js_ext_shared_dict_clear(njs_vm_t *v
 }
 }
 
+done:
+
 ngx_rwlock_unlock(>sh->rwlock);
 
 njs_value_undefined_set(retval);
diff -r 85313a068147 -r f632fe16ba05 nginx/t/js_shared_dict.t
--- a/nginx/t/js_shared_dict.t  Mon Mar 18 22:24:57 2024 -0700
+++ b/nginx/t/js_shared_dict.t  Tue Mar 19 21:05:51 2024 -0700
@@ -266,6 +266,7 @@ EOF
 
 function set_clear(r) {
 var dict = ngx.shared.no_timeout;
+dict.clear();
 dict.set("test", "test value");
 dict.set("test1", "test1 value");
 dict.clear();
___
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel


Re: [PATCH] macOS: detect cache line size at runtime

2024-03-20 Thread Sergey Kandaurov
On Wed, Feb 28, 2024 at 01:24:06AM +, Piotr Sikora via nginx-devel wrote:
> # HG changeset patch
> # User Piotr Sikora 
> # Date 1708977640 0
> #  Mon Feb 26 20:00:40 2024 +
> # Branch patch015
> # Node ID f58bc1041ebca635517b919d58b49923bf24f76d
> # Parent  570e97dddeeddb79c71587aa8a10150b64404beb

Hello,

Note that a compile time cache line size was previously improved
in 8010:35afae4b3dff, to have a reasonable default of 64.

> macOS: detect cache line size at runtime.

I prefer not to introduce more ad-hoc prefixes in the log summary.
Something like moving the "macOS" part to the end should be fine.

> 
> Notably, Apple Silicon CPUs have 128 byte cache line size,
> which is twice the default configured for generic aarch64.
> 
> Signed-off-by: Piotr Sikora 
> 
> diff -r 570e97dddeed -r f58bc1041ebc src/os/unix/ngx_darwin_init.c
> --- a/src/os/unix/ngx_darwin_init.c   Mon Feb 26 20:00:38 2024 +
> +++ b/src/os/unix/ngx_darwin_init.c   Mon Feb 26 20:00:40 2024 +
> @@ -11,6 +11,7 @@
>  
>  charngx_darwin_kern_ostype[16];
>  charngx_darwin_kern_osrelease[128];
> +int64_t ngx_darwin_hw_cachelinesize;
>  int ngx_darwin_hw_ncpu;
>  int ngx_darwin_kern_ipc_somaxconn;
>  u_long  ngx_darwin_net_inet_tcp_sendspace;

style: this breaks a perfect indentation of two spaces after type;
further, it appears to be unsorted by type; I'd put it after u_long

Regarding ngx_darwin_net_inet_tcp_sendspace, which is u_long here, it
appears to be inherited from ngx_freebsd_init.c unmodified in 0.7.7.
However, tcp_sendspace is sysctl-exported using integer OID format
in both XNU and FreeBSD modern versions.  Restoring its type there,
back to before 673:b80f94fa2197, might further affect the sorting.

As originally introduced in the BSD kernel, tcp_sendspace was int.
It was changed to u_long in 4.3BSD-Tahoe by Mike Karels (together
with soreserve), though with introduction of the (old style) sysctl
interface in FreeBSD 2.0.5, it started to be exported as type int
(with appropriate XXX).  It continued to be exported as int using
the new style, until its OID format was change to SYSCTL_ULONG in
FreeBSD 7.0 "to match the type of the variable they are exporting".
Apparently, this was the reason of updating
ngx_freebsd_net_inet_tcp_sendspace to u_long in nginx 0.3.58
(further unsorting declarations with regard to sysctls[] order):
|*) Bugfix: nginx could not run on 64-bit FreeBSD 7.0-CURRENT.
Later, the OID format was changed back to int in FreeBSD 9.2, as
part of the sysctl virtualization work, with the following reason:
|A long is not necessary as the TCP window is limited to 2**30
but ngx_freebsd_net_inet_tcp_sendspace remained u_long, apparently
to keep nginx running on older FreeBSD version.

In XNU, tcp_sendspace interface remained the same as imported from
around FreeBSD 4.x, until in xnu-1456.1.26 (Snow Leopard), its type
was corrected to u_int32_t, and its sysctl OID format was changed
to "unsigned int".

Anyway, ngx_darwin_net_inet_tcp_sendspace is not currently used in
the code, we can leave it for now.  See the relevant discussion on
incomplete work here:

https://mailman.nginx.org/pipermail/nginx-devel/2023-May/PSFQLA6TJSL7B2RKJWL6ORA54IOPO5SM.html

> @@ -44,6 +45,10 @@
>  
>  
>  sysctl_t sysctls[] = {
> +{ "hw.cachelinesize",
> +  _darwin_hw_cachelinesize,
> +  sizeof(ngx_darwin_hw_cachelinesize), 0 },
> +

JFTR, this is well sorted regarding the order of declarations,
but it will need to be adjusted after fixing the sorting above.

>  { "hw.ncpu",
>_darwin_hw_ncpu,
>sizeof(ngx_darwin_hw_ncpu), 0 },
> @@ -155,6 +160,7 @@
>  return NGX_ERROR;
>  }
>  
> +ngx_cacheline_size = ngx_darwin_hw_cachelinesize;
>  ngx_ncpu = ngx_darwin_hw_ncpu;
>  
>  if (ngx_darwin_kern_ipc_somaxconn > 32767) {
> diff -r 570e97dddeed -r f58bc1041ebc src/os/unix/ngx_posix_init.c
> --- a/src/os/unix/ngx_posix_init.cMon Feb 26 20:00:38 2024 +
> +++ b/src/os/unix/ngx_posix_init.cMon Feb 26 20:00:40 2024 +
> @@ -51,7 +51,10 @@
>  }
>  
>  ngx_pagesize = getpagesize();
> -ngx_cacheline_size = NGX_CPU_CACHE_LINE;
> +
> +if (ngx_cacheline_size == 0) {
> +ngx_cacheline_size = NGX_CPU_CACHE_LINE;
> +}
>  
>  for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ }
>  

This makes the following slight update to the patch.
If you're okey with it, I will commit it then.

# HG changeset patch
# User Piotr Sikora 
# Date 1710886608 -14400
#  Wed Mar 20 02:16:48 2024 +0400
# Node ID d6fc8103a60d5fe2ca291a3ee943a93725df420c
# Parent  e773dcc47d1004babe8383b73c0731a734b70ca4
Detect cache line size at runtime on macOS.

Notably, Apple Silicon CPUs have 128 byte cache line size,
which is twice the default configured for generic aarch64.

Signed-off-by: Piotr Sikora 

diff --git a/src/os/unix/ngx_darwin_init.c b/src/os/unix/ngx_darwin_init.c
--- a/src/os/unix/ngx_darwin_init.c
+++ 

Re: [PATCH] Configure: link libcrypt when a feature using it is detected

2024-03-20 Thread Sergey Kandaurov

> On 28 Feb 2024, at 05:23, Piotr Sikora via nginx-devel 
>  wrote:
> 
> # HG changeset patch
> # User Piotr Sikora 
> # Date 1708977638 0
> #  Mon Feb 26 20:00:38 2024 +
> # Branch patch014
> # Node ID 570e97dddeeddb79c71587aa8a10150b64404beb
> # Parent  cdc173477ea99fd6c952a85e5cd11db66452076a
> Configure: link libcrypt when a feature using it is detected.
> 
> Previously, this worked only because libcrypt was added in a separate
> test for crypt() in auto/unix.

Hello, 

The crypt_r() test was added specifically for a missing crypt_r() on
uclibc, which is rare these days: uclibc has just crypt() in libcrypt.
Anyway, crypt_r() is expected to be in the same library as crypt().
That's how it worked in practice: testing crypt_r() uses libcrypt to
pass in all cases, and testing crypt() adds actual -lcrypt as needed.

Currently, it may fail rather theoretically: e.g. crypt_r() is present
in libcrypt, but crypt() is not (it might be in libc, instead).
In this case, given the crypt_r() precedence, linking nginx would
fail due to a missing linking with libcrypt required for crypt_r().

Aside from uclibc, I think the patch is good to improve consistency.
I'd rewrote the description to be more specific:

: Configure: fixed Linux crypt_r() test to add libcrypt.
: 
: Previously, the resulting binary was successfully linked
: because libcrypt was added in a separate test for crypt().

> 
> Signed-off-by: Piotr Sikora 
> 
> diff -r cdc173477ea9 -r 570e97dddeed auto/os/linux
> --- a/auto/os/linux Mon Feb 26 20:00:37 2024 +
> +++ b/auto/os/linux Mon Feb 26 20:00:38 2024 +
> @@ -228,6 +228,10 @@
>   crypt_r(\"key\", \"salt\", );"
> . auto/feature
> 
> +if [ $ngx_found = yes ]; then
> +CRYPT_LIB="-lcrypt"
> +fi
> +
> 
> ngx_include="sys/vfs.h"; . auto/include
> 

-- 
Sergey Kandaurov
___
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel