Changeset: dbf9882c7d69 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/dbf9882c7d69
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk.h
        gdk/gdk_private.h
        gdk/gdk_utils.c
        monetdb5/mal/mal_interpreter.c
        monetdb5/modules/atoms/json.c
Branch: resource_management
Log Message:

capture more than blk index and offset in allocator state


diffs (truncated from 310 to 300 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -577,14 +577,14 @@ ssize_t ptrToStr(allocator *ma, str *dst
 const ptr ptr_nil;
 void *sa_alloc(allocator *sa, size_t sz);
 void sa_close(allocator *sa);
-void sa_close_to(allocator *sa, uint64_t);
+void sa_close_to(allocator *sa, allocator_state *);
 void sa_destroy(allocator *sa);
 void sa_free(allocator *sa, void *);
 exception_buffer *sa_get_eb(allocator *sa) __attribute__((__pure__));
 allocator *sa_get_parent(const allocator *sa);
 allocator *sa_get_ta(allocator *sa);
 const char *sa_name(allocator *sa);
-uint64_t sa_open(allocator *sa);
+allocator_state *sa_open(allocator *sa);
 void *sa_realloc(allocator *sa, void *ptr, size_t sz, size_t osz);
 allocator *sa_reset(allocator *sa);
 void sa_set_ta(allocator *sa, allocator *ta);
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -62,6 +62,7 @@ gdk_export _Noreturn void GDKfatal(_In_z
        __attribute__((__format__(__printf__, 1, 2)));
 
 typedef struct allocator allocator;
+typedef struct allocator_state allocator_state;
 #include "gdk_system.h"
 #include "gdk_posix.h"
 #include "stream.h"
@@ -1746,9 +1747,9 @@ gdk_export char *sa_strdup(allocator *sa
 gdk_export char *sa_strconcat(allocator *sa, const char *s1, const char *s2);
 gdk_export size_t sa_size(allocator *sa);
 gdk_export const char* sa_name(allocator *sa);
-gdk_export uint64_t sa_open(allocator *sa);  /* open new frame of tempory 
allocations */
+gdk_export allocator_state* sa_open(allocator *sa);  /* open new frame of 
tempory allocations */
 gdk_export void sa_close(allocator *sa); /* close temporary frame, reset to 
initial state */
-gdk_export void sa_close_to(allocator *sa, uint64_t); /* close temporary 
frame, reset to old state */
+gdk_export void sa_close_to(allocator *sa, allocator_state *); /* close 
temporary frame, reset to old state */
 gdk_export void sa_free(allocator *sa, void *);
 gdk_export exception_buffer *sa_get_eb(allocator *sa)
        __attribute__((__pure__));
diff --git a/gdk/gdk_private.h b/gdk/gdk_private.h
--- a/gdk/gdk_private.h
+++ b/gdk/gdk_private.h
@@ -52,18 +52,29 @@ struct allocator {
        size_t objects;  /* number of objects */
        size_t inuse;    /* number of objects in use*/
        size_t free_obj_hits; /* number of object reuse*/
-       void *freelist; /* list of freed objects */
-       void *freelist_blks;    /* list of freed blks */
+       void *freelist; /* first free object */
+       void *freelist_blks;    /* first free blk */
        size_t frees;
        size_t free_blk_hits;
+       size_t tmp_used; /* counter for temp usage */
 
-       size_t tmp_used; /* keeps track of temp usage via sa_open/sa_close */
+       int refcount;
        exception_buffer eb;
        MT_Lock lock;    /* lock for thread-safe allocations */
        bool use_lock;
        char name [MT_NAME_LEN]; /* Name (only for display!) */
 };
 
+/* checkpoint or snapshot of allocator internal state we can use
+ * to restore to a point in time */
+struct allocator_state {
+       size_t nr;
+       size_t used;
+       size_t usedmem;  /* total used memory */
+       size_t objects;
+       size_t inuse;
+};
+
 bool ATOMisdescendant(int id, int parentid)
        __attribute__((__visibility__("hidden")));
 int ATOMunknown_find(const char *nme)
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -2349,6 +2349,12 @@ sa_reallocated(allocator *sa)
        return sa->blks != (char **)sa->first_blk;
 }
 
+static inline bool
+sa_has_dependencies(allocator *sa)
+{
+       return (sa->refcount > 0) && !sa->pa;
+}
+
 
 static inline void
 _sa_free_blks(allocator *sa, size_t start_idx)
@@ -2373,6 +2379,12 @@ static inline void
 allocator *sa_reset(allocator *sa)
 {
        COND_LOCK_ALLOCATOR(sa);
+       assert(!sa_has_dependencies(sa));
+       if (sa_has_dependencies(sa)) {
+               if (sa->eb.enabled)
+                       eb_error(&sa->eb, "reset failed, allocator has 
dependencies", 1000);
+               return sa;
+       }
        // 1st block is where we live, free the rest
        _sa_free_blks(sa, 1);
 
@@ -2401,6 +2413,7 @@ allocator *sa_reset(allocator *sa)
        sa->blk_size = SA_BLOCK_SIZE;
        sa->objects = 0;
        sa->inuse = 0;
+       sa->tmp_used = 0;
        COND_UNLOCK_ALLOCATOR(sa);
        return sa;
 }
@@ -2415,7 +2428,7 @@ sa_realloc(allocator *sa, void *p, size_
 
        if (r)
                memcpy(r, p, oldsz);
-       if (oldsz >= sa->blk_size) {
+       if (oldsz >= sa->blk_size && !sa_tmp_active(sa)) {
                char* ptr = (char *) p - SA_HEADER_SIZE;
                COND_LOCK_ALLOCATOR(sa);
                sa_free_blk(sa, ptr);
@@ -2471,7 +2484,7 @@ static void *
 {
        assert(sz > 0);
        sz = round16(sz);
-       char *r = sa_use_freed(sa, sz);
+       char *r = sa_tmp_active(sa) ? NULL : sa_use_freed(sa, sz);
        if (r)
                return r;
        COND_LOCK_ALLOCATOR(sa);
@@ -2605,11 +2618,14 @@ create_allocator(allocator *pa, const ch
        sa->free_obj_hits = 0;
        sa->free_blk_hits = 0;
        sa->tmp_used = 0;
+       sa->refcount = 0;
        sa->use_lock = use_lock;
        MT_lock_init(&sa->lock, "allocator_lock");
        if (name)
                snprintf(sa->name, sizeof(sa->name),
                                "%s", name);
+       if (sa->pa)
+               sa->pa->refcount++;
        return sa;
 }
 
@@ -2640,6 +2656,8 @@ sa_destroy(allocator *sa)
                                sa_free_blk(sa->pa, next);
                        }
                }
+               if (sa->pa)
+                       sa->pa->refcount--;
                MT_lock_destroy(&sa->lock);
                if (root_allocator) {
                        if (blks_relocated)
@@ -2709,64 +2727,65 @@ sa_get_eb(allocator *sa)
        return &sa->eb;
 }
 
-#define SA_PACK_INT32(hi, lo)\
-    ((((uint64_t)(hi)) << 32) | ((uint64_t)(lo)))
-#define SA_UNPACK_HI(v)\
-       ((uint32_t)((uint64_t)(v) >> 32))
-#define SA_UNPACK_LO(v)\
-               ((uint32_t)((uint64_t)(v) & 0xFFFFFFFFULL))
-
-uint64_t
+allocator_state *
 sa_open(allocator *sa)
 {
-       //assert(sa->pa); // only child allocators are tmp used
+       allocator_state *res = sa_alloc(sa,
+                       sizeof(allocator_state));
+       if (!res) {
+               if (sa->eb.enabled)
+                       eb_error(&sa->eb, "out of memory", 1000);
+               return NULL;
+       }
+
        COND_LOCK_ALLOCATOR(sa);
        sa->tmp_used += 1;
-       uint64_t offset = SA_PACK_INT32(sa->nr, sa->used);
+       res->nr = sa->nr;
+       res->used = sa->used;
+       res->usedmem = sa->usedmem;
+       res->objects = sa->objects;
+       res->inuse = sa->inuse;
        COND_UNLOCK_ALLOCATOR(sa);
-       return offset;
+       return res;
 }
 
 void
 sa_close(allocator *sa)
 {
+       COND_LOCK_ALLOCATOR(sa);
        assert(sa_tmp_active(sa));
-       sa_reset(sa);
-       COND_LOCK_ALLOCATOR(sa);
-       sa->tmp_used -= 1;
+       if (sa->tmp_used > 0)
+               sa->tmp_used -= 1;
+       if (!sa_tmp_active(sa) && !sa_has_dependencies(sa)) {
+               COND_UNLOCK_ALLOCATOR(sa);
+               sa_reset(sa);
+               return;
+       }
        COND_UNLOCK_ALLOCATOR(sa);
 }
 
 void
-sa_close_to(allocator *sa, uint64_t offset)
+sa_close_to(allocator *sa, allocator_state *state)
 {
-       // only child allocators for now as
-       // allocator statistics get affected
-       // and complications if there are children
-       assert(sa->pa);
-       if(sa->pa)
-               return;
+       COND_LOCK_ALLOCATOR(sa);
+       assert(state);
        assert(sa_tmp_active(sa));
-       assert(offset);
-       if (offset) {
-               uint32_t blk_idx = SA_UNPACK_HI(offset);
-               uint32_t blk_offset = SA_UNPACK_LO(offset);
-               assert((blk_idx > 0) && (blk_idx <= sa->nr));
-               assert(blk_offset < SA_BLOCK_SIZE);
-               if (blk_idx != sa->nr || blk_offset != sa->used) {
-                       COND_LOCK_ALLOCATOR(sa);
-                       _sa_free_blks(sa, blk_idx);
-                       sa->nr = blk_idx;
-                       sa->used = blk_offset;
-                       sa->freelist = NULL;
-                       COND_UNLOCK_ALLOCATOR(sa);
+       if (state && !sa_has_dependencies(sa)) {
+               assert((state->nr > 0) && (state->nr <= sa->nr));
+               assert(state->used <= SA_BLOCK_SIZE);
+               if (state->nr != sa->nr || state->used != sa->used) {
+                       _sa_free_blks(sa, state->nr);
+                       sa->nr = state->nr;
+                       sa->used = state->used;
+                       sa->usedmem = state->usedmem;
+                       sa->objects = state->objects;
+                       sa->inuse = state->inuse;
                }
        }
        if (sa->tmp_used > 0) {
-               COND_LOCK_ALLOCATOR(sa);
                sa->tmp_used -= 1;
-               COND_UNLOCK_ALLOCATOR(sa);
        }
+       COND_UNLOCK_ALLOCATOR(sa);
 }
 
 bool
diff --git a/monetdb5/mal/mal_interpreter.c b/monetdb5/mal/mal_interpreter.c
--- a/monetdb5/mal/mal_interpreter.c
+++ b/monetdb5/mal/mal_interpreter.c
@@ -317,6 +317,10 @@ runMAL(Client cntxt, MalBlkPtr mb, MalBl
        ValPtr lhs, rhs;
        str ret;
        (void) mbcaller;
+       allocator *ta = MT_thread_getallocator();
+       if(!ta)
+               ta = mb->ta;
+       assert(ta);
 
        /* Prepare a new interpreter call. This involves two steps, (1)
         * allocate the minimum amount of stack space needed, some slack
@@ -366,12 +370,9 @@ runMAL(Client cntxt, MalBlkPtr mb, MalBl
                 * been observed due the small size of the function).
                 */
        }
-       // FIX use tls allocator
-       allocator *ta = mb->ta;
-       assert(ta);
-       uint64_t offset = ma_open(ta);
+       allocator_state *ta_state = ma_open(ta);
        ret = copyException(mb->ma, runMALsequence(ta, cntxt, mb, 1, 0, stk, 
env, 0));
-       ma_close_to(ta, offset);
+       ma_close_to(ta, ta_state);
 
        if (!stk->keepAlive && garbageControl(getInstrPtr(mb, 0)))
                garbageCollector(cntxt, mb, stk, env != stk);
diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -518,7 +518,7 @@ JSONstr2json_intern(allocator *ma, json 
        size_t ln = strlen(*j)+1;
        size_t out_size = 0;
        allocator *ta = sa_get_ta(ma);
-       uint64_t ta_offset = ma_open(ta);
+       allocator_state *ta_state = ma_open(ta);
 
        JSON *jt = NULL;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to