Changeset: 9aeade4109e5 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/9aeade4109e5
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk.h
        gdk/gdk_private.h
        gdk/gdk_utils.c
        geom/monetdb5/geom_atoms.c
        monetdb5/mal/mal_interpreter.c
        monetdb5/modules/atoms/json.c
        monetdb5/modules/atoms/str.c
        monetdb5/modules/kernel/batstr.c
        monetdb5/modules/mal/mal_mapi.c
        monetdb5/optimizer/opt_aliases.c
        monetdb5/optimizer/opt_coercion.c
        monetdb5/optimizer/opt_commonTerms.c
        monetdb5/optimizer/opt_constants.c
        monetdb5/optimizer/opt_dataflow.c
        monetdb5/optimizer/opt_deadcode.c
        monetdb5/optimizer/opt_dict.c
        monetdb5/optimizer/opt_emptybind.c
        monetdb5/optimizer/opt_evaluate.c
        monetdb5/optimizer/opt_for.c
        monetdb5/optimizer/opt_generator.c
        monetdb5/optimizer/opt_inline.c
        monetdb5/optimizer/opt_mergetable.c
        monetdb5/optimizer/opt_multiplex.c
        monetdb5/optimizer/opt_projectionpath.c
        monetdb5/optimizer/opt_pushselect.c
        monetdb5/optimizer/opt_remap.c
        monetdb5/optimizer/opt_remoteQueries.c
        monetdb5/optimizer/opt_reorder.c
        sql/server/rel_optimize_proj.c
        sql/server/rel_optimize_sel.c
        sql/server/rel_unnest.c
        sql/storage/bat/bat_logger.c
Branch: resource_management
Log Message:

Keep allocator state for open/close on the processor stack.
Also, remove ma_close_to and use only ma_open and ma_close, both of
which have a different interface than before.


diffs (truncated from 2522 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
@@ -566,14 +566,14 @@ gdk_return log_tsequence(logger *lg, int
 gdk_return log_tstart(allocator *ma, logger *lg, bool flushnow, ulng 
*log_file_id);
 ATOMIC_TYPE lvl_per_component[];
 void *ma_alloc(allocator *sa, size_t sz);
-void ma_close(allocator *sa);
-void ma_close_to(allocator *sa, allocator_state *);
+void ma_close(allocator *sa, const allocator_state *);
 void ma_destroy(allocator *sa);
 void ma_free(allocator *sa, void *);
 exception_buffer *ma_get_eb(allocator *sa) __attribute__((__pure__));
 allocator *ma_get_parent(const allocator *sa);
+void ma_info(const allocator *sa, char *buf, size_t buflen);
 const char *ma_name(allocator *sa);
-allocator_state *ma_open(allocator *sa);
+allocator_state ma_open(allocator *sa);
 void *ma_realloc(allocator *sa, void *ptr, size_t sz, size_t osz);
 allocator *ma_reset(allocator *sa);
 size_t ma_size(allocator *sa);
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -62,7 +62,17 @@ gdk_export _Noreturn void GDKfatal(_In_z
        __attribute__((__format__(__printf__, 1, 2)));
 
 typedef struct allocator allocator;
-typedef struct allocator_state allocator_state;
+/* checkpoint or snapshot of allocator internal state we can use
+ * to restore to a point in time */
+typedef struct {
+       size_t nr;
+       size_t used;
+       size_t usedmem;  /* total used memory */
+       size_t objects;
+       size_t inuse;
+       size_t tmp_used;
+} allocator_state;
+
 #include "gdk_system.h"
 #include "gdk_posix.h"
 #include "stream.h"
@@ -1748,9 +1758,8 @@ gdk_export char *ma_strdup(allocator *sa
 gdk_export char *ma_strconcat(allocator *sa, const char *s1, const char *s2);
 gdk_export size_t ma_size(allocator *sa);
 gdk_export const char* ma_name(allocator *sa);
-gdk_export allocator_state* ma_open(allocator *sa);  /* open new frame of 
tempory allocations */
-gdk_export void ma_close(allocator *sa); /* close temporary frame, reset to 
initial state */
-gdk_export void ma_close_to(allocator *sa, allocator_state *); /* close 
temporary frame, reset to old state */
+gdk_export allocator_state ma_open(allocator *sa);  /* open new frame of 
tempory allocations */
+gdk_export void ma_close(allocator *sa, const allocator_state *); /* close 
temporary frame, reset to old state */
 gdk_export void ma_free(allocator *sa, void *);
 gdk_export exception_buffer *ma_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
@@ -39,16 +39,6 @@ enum range_comp_t {
        range_inside,           /* search range inside bat range */
 };
 
-/* 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
@@ -2099,6 +2099,7 @@ ma_alloc(allocator *sa, size_t sz)
 allocator *
 create_allocator(allocator *pa, const char *name, bool use_lock)
 {
+//     assert(pa == NULL || pa->use_lock);
        // allocator lives in the 1st blk
        char *first_blk = (pa)? (char*) _sa_alloc_internal(pa, SA_BLOCK_SIZE) : 
(char*) GDKmalloc(SA_BLOCK_SIZE);
        if (!first_blk)
@@ -2241,53 +2242,29 @@ ma_get_eb(allocator *sa)
        return &sa->eb;
 }
 
-allocator_state *
+allocator_state
 ma_open(allocator *sa)
 {
+       allocator_state st = {0};
        assert(sa);
        if (sa) {
-               allocator_state st;
                COND_LOCK_ALLOCATOR(sa);
-               sa->tmp_used += 1;
                st = (allocator_state) {
                        .nr = sa->nr,
                        .used = sa->used,
                        .usedmem = sa->usedmem,
                        .objects = sa->objects,
                        .inuse = sa->inuse,
+                       .tmp_used = sa->tmp_used,
                };
+               sa->tmp_used += 1;
                COND_UNLOCK_ALLOCATOR(sa);
-               allocator_state *res = ma_alloc(sa,
-                               sizeof(allocator_state));
-               if (!res) {
-                       if (sa->eb.enabled)
-                               eb_error(&sa->eb, "out of memory", 1000);
-                       return NULL;
-               }
-               *res = st;
-
-               return res;
        }
-       return NULL;
+       return st;
 }
 
 void
-ma_close(allocator *sa)
-{
-       COND_LOCK_ALLOCATOR(sa);
-       assert(ma_tmp_active(sa));
-       if (sa->tmp_used > 0)
-               sa->tmp_used -= 1;
-       if (!ma_tmp_active(sa) && !sa_has_dependencies(sa)) {
-               COND_UNLOCK_ALLOCATOR(sa);
-               ma_reset(sa);
-               return;
-       }
-       COND_UNLOCK_ALLOCATOR(sa);
-}
-
-void
-ma_close_to(allocator *sa, allocator_state *state)
+ma_close(allocator *sa, const allocator_state *state)
 {
        assert(sa);
        if (sa) {
@@ -2297,22 +2274,22 @@ ma_close_to(allocator *sa, allocator_sta
                        sa->tmp_used -= 1;
                }
                // check if we can reset to the initial state
-               if (!ma_tmp_active(sa) && !sa_has_dependencies(sa)) {
+               if (state->tmp_used == 0 && !sa_has_dependencies(sa)) {
                        COND_UNLOCK_ALLOCATOR(sa);
                        ma_reset(sa);
                        return;
                }
-               if (state && !sa_has_dependencies(sa)) {
+               if (!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) {
-                               allocator_state state_save = *state;
-                               _sa_free_blks(sa, state_save.nr);
-                               sa->nr = state_save.nr;
-                               sa->used = state_save.used;
-                               sa->usedmem = state_save.usedmem;
-                               sa->objects = state_save.objects;
-                               sa->inuse = state_save.inuse;
+                               _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;
+                               sa->tmp_used = state->tmp_used;
                        }
                }
                COND_UNLOCK_ALLOCATOR(sa);
diff --git a/geom/monetdb5/geom_atoms.c b/geom/monetdb5/geom_atoms.c
--- a/geom/monetdb5/geom_atoms.c
+++ b/geom/monetdb5/geom_atoms.c
@@ -338,7 +338,7 @@ wkbFROMSTR_withSRID(allocator *ma, const
        //continuing. Of course this means that isValid for example does
        //not work correctly.
        allocator *ta = MT_thread_getallocator();
-       allocator_state *state = ma_open(ta);
+       allocator_state state = ma_open(ta);
        if (strncasecmp(geomWKT, polyhedralSurface, strlen(polyhedralSurface)) 
== 0) {
                size_t sizeOfInfo = strlen(geomWKT) - strlen(polyhedralSurface) 
+ strlen(multiPolygon) + 1;
                geomWKT_new = ma_alloc(ta, sizeOfInfo);
@@ -351,20 +351,20 @@ wkbFROMSTR_withSRID(allocator *ma, const
 
        WKT_reader = GEOSWKTReader_create_r(geoshandle);
        if (WKT_reader == NULL) {
-               ma_close_to(ta, state);
+               ma_close(ta, &state);
                throw(MAL, "wkb.FromText", SQLSTATE(38000) "Geos operation 
GEOSWKTReader_create failed");
        }
        geosGeometry = GEOSWKTReader_read_r(geoshandle, WKT_reader, geomWKT);
        GEOSWKTReader_destroy_r(geoshandle, WKT_reader);
 
        if (geosGeometry == NULL) {
-               ma_close_to(ta, state);
+               ma_close(ta, &state);
                throw(MAL, "wkb.FromText", SQLSTATE(38000) "Geos operation 
GEOSWKTReader_read failed");
        }
 
        if (GEOSGeomTypeId_r(geoshandle, geosGeometry) == -1) {
                GEOSGeom_destroy_r(geoshandle, geosGeometry);
-               ma_close_to(ta, state);
+               ma_close(ta, &state);
                throw(MAL, "wkb.FromText", SQLSTATE(38000) "Geos operation 
GEOSGeomTypeId failed");
        }
 
@@ -377,7 +377,7 @@ wkbFROMSTR_withSRID(allocator *ma, const
        *geomWKB = geos2wkb(ma, geomWKB, len, geosGeometry);
        GEOSGeom_destroy_r(geoshandle, geosGeometry);
        if (*geomWKB == NULL) {
-               ma_close_to(ta, state);
+               ma_close(ta, &state);
                throw(MAL, "wkb.FromText", SQLSTATE(38000) "Geos operation 
geos2wkb failed");
        }
 
@@ -385,7 +385,7 @@ wkbFROMSTR_withSRID(allocator *ma, const
        parsedCharacters = strlen(geomWKT);
        assert(parsedCharacters <= GDK_int_max);
 
-       ma_close_to(ta, state);
+       ma_close(ta, &state);
        *nread = parsedCharacters;
        return MAL_SUCCEED;
 }
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
@@ -370,9 +370,9 @@ runMAL(Client cntxt, MalBlkPtr mb, MalBl
                 * been observed due the small size of the function).
                 */
        }
-       allocator_state *ta_state = 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, ta_state);
+       ma_close(ta, &ta_state);
 
        if (!stk->keepAlive && garbageControl(getInstrPtr(mb, 0)))
                garbageCollector(cntxt, mb, stk, env != stk);
@@ -410,9 +410,9 @@ reenterMAL(Client cntxt, MalBlkPtr mb, i
                throw(MAL, "mal.interpreter", MAL_STACK_FAIL);
        keepAlive = stk->keepAlive;
        allocator *ta = MT_thread_getallocator();
-       ma_open(ta);
+       allocator_state ta_state = ma_open(ta);
        ret = copyException(mb->ma, runMALsequence(ta, cntxt, mb, startpc, 
stoppc, stk, 0, 0));
-       ma_close(ta);
+       ma_close(ta, &ta_state);
 
        if (keepAlive == 0 && garbageControl(getInstrPtr(mb, 0)))
                garbageCollector(cntxt, mb, stk, stk != 0);
@@ -469,9 +469,9 @@ callMAL(Client cntxt, MalBlkPtr mb, MalS
                                BBPretain(lhs->val.bval);
                }
                allocator *ta = MT_thread_getallocator();
-               ma_open(ta);
+               allocator_state ta_state = ma_open(ta);
                ret = copyException(mb->ma, runMALsequence(ta, cntxt, mb, 1, 0, 
stk, 0, 0));
-               ma_close(ta);
+               ma_close(ta, &ta_state);
                break;
        case PATcall:
        case CMDcall:
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
@@ -467,7 +467,7 @@ JSONdump(Client ctx, MalBlkPtr mb, MalSt
 {
        (void) mb;
        allocator *ma = MT_thread_getallocator();
-       allocator_state *ma_state = ma_open(ma);
+       allocator_state ma_state = ma_open(ma);
 
        bat *ret = getArgReference_bat(stk, pci, 0);
        const json *val = (json *) getArgReference(stk, pci, 1);
@@ -476,7 +476,7 @@ JSONdump(Client ctx, MalBlkPtr mb, MalSt
        CHECK_JSON(jt);
        BAT *bn = JSONdumpInternal(ma, ctx, jt, 0);
        JSONfree(jt);
-       ma_close_to(ma, ma_state);
+       ma_close(ma, &ma_state);
        if (bn == NULL)
                throw(MAL, "json.dump", SQLSTATE(HY013) MAL_MALLOC_FAIL);
        *ret = bn->batCacheid;
@@ -520,7 +520,7 @@ JSONstr2json_intern(allocator *ma, json 
        size_t ln = strlen(*j)+1;
        size_t out_size = 0;
        allocator *ta = MT_thread_getallocator();
-       allocator_state *ta_state = NULL;
+       allocator_state ta_state = {0};
        if (ta != ma)
                ta_state = ma_open(ta);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to