[njs] Added njs.memoryStats object.

Dmitry Volyntsev xeioex at nginx.com
Sat Oct 22 01:49:12 UTC 2022


details:   https://hg.nginx.org/njs/rev/ebc48ac9f656
branches:  
changeset: 1980:ebc48ac9f656
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Oct 21 18:48:39 2022 -0700
description:
Added njs.memoryStats object.

njs.memoryStats.size is a number of bytes a VM claimed from the OS.

diffstat:

 src/njs_builtin.c        |  70 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/njs_mp.c             |  24 ++++++++++++++++
 src/njs_mp.h             |   9 ++++++
 src/test/njs_unit_test.c |  16 ++++++++++
 4 files changed, 119 insertions(+), 0 deletions(-)

diffs (173 lines):

diff -r c42cdba1626d -r ebc48ac9f656 src/njs_builtin.c
--- a/src/njs_builtin.c	Fri Oct 21 18:48:39 2022 -0700
+++ b/src/njs_builtin.c	Fri Oct 21 18:48:39 2022 -0700
@@ -912,6 +912,69 @@ njs_ext_on(njs_vm_t *vm, njs_value_t *ar
 
 
 static njs_int_t
+njs_ext_memory_stats(njs_vm_t *vm, njs_object_prop_t *prop,
+    njs_value_t *unused, njs_value_t *unused2, njs_value_t *retval)
+{
+    njs_int_t      ret;
+    njs_value_t    object, value;
+    njs_object_t   *stat;
+    njs_mp_stat_t  mp_stat;
+
+    static const njs_value_t  size_string = njs_string("size");
+    static const njs_value_t  nblocks_string = njs_string("nblocks");
+    static const njs_value_t  page_string = njs_string("page_size");
+    static const njs_value_t  cluster_string = njs_string("cluster_size");
+
+    stat = njs_object_alloc(vm);
+    if (njs_slow_path(stat == NULL)) {
+        return NJS_ERROR;
+    }
+
+    njs_set_object(&object, stat);
+
+    njs_mp_stat(vm->mem_pool, &mp_stat);
+
+    njs_set_number(&value, mp_stat.size);
+
+    ret = njs_value_property_set(vm, &object, njs_value_arg(&size_string),
+                                 &value);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
+    njs_set_number(&value, mp_stat.nblocks);
+
+    ret = njs_value_property_set(vm, &object, njs_value_arg(&nblocks_string),
+                                 &value);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
+    njs_set_number(&value, mp_stat.cluster_size);
+
+    ret = njs_value_property_set(vm, &object, njs_value_arg(&cluster_string),
+                                 &value);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
+    njs_set_number(&value, mp_stat.page_size);
+
+    ret = njs_value_property_set(vm, &object, njs_value_arg(&page_string),
+                                 &value);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
+    njs_set_object(retval, stat);
+
+    return NJS_OK;
+}
+
+
+
+
+static njs_int_t
 njs_global_this_prop_handler(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *global, njs_value_t *setval, njs_value_t *retval)
 {
@@ -1725,6 +1788,13 @@ static const njs_object_prop_t  njs_njs_
         .value = njs_native_function(njs_ext_on, 0),
         .configurable = 1,
     },
+
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("memoryStats"),
+        .value = njs_prop_handler(njs_ext_memory_stats),
+    },
+
 };
 
 
diff -r c42cdba1626d -r ebc48ac9f656 src/njs_mp.c
--- a/src/njs_mp.c	Fri Oct 21 18:48:39 2022 -0700
+++ b/src/njs_mp.c	Fri Oct 21 18:48:39 2022 -0700
@@ -285,6 +285,30 @@ njs_mp_destroy(njs_mp_t *mp)
 }
 
 
+void
+njs_mp_stat(njs_mp_t *mp, njs_mp_stat_t *stat)
+{
+    njs_mp_block_t     *block;
+    njs_rbtree_node_t  *node;
+
+    stat->size = 0;
+    stat->nblocks = 0;
+    stat->cluster_size = mp->cluster_size;
+    stat->page_size = mp->page_size;
+
+    node = njs_rbtree_min(&mp->blocks);
+
+    while (njs_rbtree_is_there_successor(&mp->blocks, node)) {
+        block = (njs_mp_block_t *) node;
+
+        stat->nblocks++;
+        stat->size += block->size;
+
+        node = njs_rbtree_node_successor(&mp->blocks, node);
+    }
+}
+
+
 void *
 njs_mp_alloc(njs_mp_t *mp, size_t size)
 {
diff -r c42cdba1626d -r ebc48ac9f656 src/njs_mp.h
--- a/src/njs_mp.h	Fri Oct 21 18:48:39 2022 -0700
+++ b/src/njs_mp.h	Fri Oct 21 18:48:39 2022 -0700
@@ -20,6 +20,14 @@ struct njs_mp_cleanup_s {
 };
 
 
+typedef struct {
+    size_t                  size;
+    size_t                  nblocks;
+    size_t                  page_size;
+    size_t                  cluster_size;
+} njs_mp_stat_t;
+
+
 NJS_EXPORT njs_mp_t *njs_mp_create(size_t cluster_size, size_t page_alignment,
     size_t page_size, size_t min_chunk_size) NJS_MALLOC_LIKE;
 NJS_EXPORT njs_mp_t * njs_mp_fast_create(size_t cluster_size,
@@ -27,6 +35,7 @@ NJS_EXPORT njs_mp_t * njs_mp_fast_create
     NJS_MALLOC_LIKE;
 NJS_EXPORT njs_bool_t njs_mp_is_empty(njs_mp_t *mp);
 NJS_EXPORT void njs_mp_destroy(njs_mp_t *mp);
+NJS_EXPORT void njs_mp_stat(njs_mp_t *mp, njs_mp_stat_t *stat);
 
 NJS_EXPORT void *njs_mp_alloc(njs_mp_t *mp, size_t size)
     NJS_MALLOC_LIKE;
diff -r c42cdba1626d -r ebc48ac9f656 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Oct 21 18:48:39 2022 -0700
+++ b/src/test/njs_unit_test.c	Fri Oct 21 18:48:39 2022 -0700
@@ -18239,6 +18239,22 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("njs.on('exit', ()=>{}); 1"),
       njs_str("1") },
 
+    /* njs.memoryStats. */
+
+    { njs_str("Object.keys(njs.memoryStats).sort()"),
+      njs_str("cluster_size,nblocks,page_size,size") },
+
+    { njs_str("typeof njs.memoryStats.size"),
+      njs_str("number") },
+
+    { njs_str("njs.memoryStats.size > 4096"),
+      njs_str("true") },
+
+    { njs_str("var size = njs.memoryStats.size;"
+              "new Array(2**15);"
+              "njs.memoryStats.size > size"),
+      njs_str("true") },
+
     /* Built-in methods name. */
 
     { njs_str(



More information about the nginx-devel mailing list