Changeset: 53c3e5d2cf37 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/53c3e5d2cf37
Modified Files:
        gdk/gdk_heap.c
        gdk/gdk_system.h
        monetdb5/mal/mal_client.c
        monetdb5/mal/mal_session.c
Branch: default
Log Message:

Keep track of how much heap space each client (context) uses in the transient 
farm.
We only count the transient farm because the persistent farm is
(potentially) shared among multiple clients.  Counting looks at the heap
capacity, not actual used size, since that is way easier (read: less
overhead).
These counts can potentially be used to limit resource usage.


diffs (224 lines):

diff --git a/gdk/gdk_heap.c b/gdk/gdk_heap.c
--- a/gdk/gdk_heap.c
+++ b/gdk/gdk_heap.c
@@ -177,6 +177,11 @@ HEAPalloc(Heap *h, size_t nitems, size_t
                return GDK_FAIL;
        }
        h->newstorage = h->storage;
+       if (h->farmid == 1) {
+               QryCtx *qc = MT_thread_get_qry_ctx();
+               if (qc)
+                       ATOMIC_ADD(&qc->datasize, h->size);
+       }
        return GDK_SUCCEED;
 }
 
@@ -201,6 +206,8 @@ HEAPalloc(Heap *h, size_t nitems, size_t
 gdk_return
 HEAPextend(Heap *h, size_t size, bool mayshare)
 {
+       size_t osize = h->size;
+
        if (size <= h->size)
                return GDK_SUCCEED;     /* nothing to do */
 
@@ -239,6 +246,11 @@ HEAPextend(Heap *h, size_t size, bool ma
                if (p) {
                        h->size = size;
                        h->base = p;
+                       if (h->farmid == 1) {
+                               QryCtx *qc = MT_thread_get_qry_ctx();
+                               if (qc)
+                                       ATOMIC_ADD(&qc->datasize, size - osize);
+                       }
                        return GDK_SUCCEED; /* success */
                }
                failure = "GDKmremap() failed";
@@ -262,8 +274,14 @@ HEAPextend(Heap *h, size_t size, bool ma
                        h->base = GDKrealloc(h->base, size);
                        TRC_DEBUG(HEAP, "Extending malloced heap %s %zu %zu %p 
%p\n", h->filename, size, h->size, bak.base, h->base);
                        h->size = size;
-                       if (h->base)
+                       if (h->base) {
+                               if (h->farmid == 1) {
+                                       QryCtx *qc = MT_thread_get_qry_ctx();
+                                       if (qc)
+                                               ATOMIC_ADD(&qc->datasize, size 
- osize);
+                               }
                                return GDK_SUCCEED; /* success */
+                       }
                        /* bak.base is still valid and may get restored */
                        failure = "h->storage == STORE_MEM && !must_map && 
!h->base";
                }
@@ -293,6 +311,11 @@ HEAPextend(Heap *h, size_t size, bool ma
                                        if (bak.free > 0)
                                                memcpy(h->base, bak.base, 
bak.free);
                                        HEAPfree(&bak, false);
+                                       if (h->farmid == 1) {
+                                               QryCtx *qc = 
MT_thread_get_qry_ctx();
+                                               if (qc)
+                                                       
ATOMIC_ADD(&qc->datasize, size);
+                                       }
                                        return GDK_SUCCEED;
                                }
                                GDKclrerr();
@@ -391,6 +414,11 @@ HEAPshrink(Heap *h, size_t size)
                          h->filename, h->size, size, h->base, p);
        }
        if (p) {
+               if (h->farmid == 1) {
+                       QryCtx *qc = MT_thread_get_qry_ctx();
+                       if (qc)
+                               ATOMIC_SUB(&qc->datasize, h->size - size);
+               }
                h->size = size;
                h->base = p;
                return GDK_SUCCEED;
@@ -578,6 +606,11 @@ void
 HEAPfree(Heap *h, bool rmheap)
 {
        if (h->base) {
+               if (h->farmid == 1) {
+                       QryCtx *qc = MT_thread_get_qry_ctx();
+                       if (qc)
+                               ATOMIC_SUB(&qc->datasize, h->size);
+               }
                if (h->storage == STORE_MEM) {  /* plain memory */
                        TRC_DEBUG(HEAP, "HEAPfree %s %zu %p\n", h->filename, 
h->size, h->base);
                        GDKfree(h->base);
@@ -764,6 +797,11 @@ HEAPload_intern(Heap *h, const char *nme
                return GDK_FAIL; /* file could  not be read satisfactorily */
 
        h->dirty = false;       /* we just read it, so it's clean */
+       if (h->farmid == 1) {
+               QryCtx *qc = MT_thread_get_qry_ctx();
+               if (qc)
+                       ATOMIC_ADD(&qc->datasize, h->size);
+       }
        return GDK_SUCCEED;
 }
 
diff --git a/gdk/gdk_system.h b/gdk/gdk_system.h
--- a/gdk/gdk_system.h
+++ b/gdk/gdk_system.h
@@ -134,6 +134,8 @@
 #define PTW32 1
 #endif
 
+#include "matomic.h"
+
 /* debug and errno integers */
 gdk_export int GDKdebug;
 gdk_export void GDKsetdebug(int debug);
@@ -163,6 +165,7 @@ typedef int64_t lng;
 typedef struct QryCtx {
        lng starttime;
        lng querytimeout;
+       ATOMIC_TYPE datasize;
 } QryCtx;
 
 gdk_export bool MT_thread_init(void);
diff --git a/monetdb5/mal/mal_client.c b/monetdb5/mal/mal_client.c
--- a/monetdb5/mal/mal_client.c
+++ b/monetdb5/mal/mal_client.c
@@ -55,11 +55,15 @@ ClientRec *mal_clients = NULL;
 void
 mal_client_reset(void)
 {
-       MAL_MAXCLIENTS = 0;
        if (mal_clients) {
+               for (int i = 0; i < MAL_MAXCLIENTS; i++) {
+                       ATOMIC_DESTROY(&mal_clients[i].lastprint);
+                       ATOMIC_DESTROY(&mal_clients[i].qryctx.datasize);
+               }
                GDKfree(mal_clients);
                mal_clients = NULL;
        }
+       MAL_MAXCLIENTS = 0;
 }
 
 bool
@@ -86,6 +90,7 @@ MCinit(void)
        }
        for (int i = 0; i < MAL_MAXCLIENTS; i++){
                ATOMIC_INIT(&mal_clients[i].lastprint, 0);
+               ATOMIC_INIT(&mal_clients[i].qryctx.datasize, 0);
                mal_clients[i].idx = -1; /* indicate it's available */
        }
        return true;
@@ -261,6 +266,7 @@ MCinitClientRecord(Client c, oid user, b
        c->qryctx.querytimeout = 0;
        c->sessiontimeout = 0;
        c->qryctx.starttime = 0;
+       ATOMIC_SET(&c->qryctx.datasize, 0);
        c->itrace = 0;
        c->errbuf = 0;
 
diff --git a/monetdb5/mal/mal_session.c b/monetdb5/mal/mal_session.c
--- a/monetdb5/mal/mal_session.c
+++ b/monetdb5/mal/mal_session.c
@@ -208,6 +208,8 @@ MSscheduleClient(str command, str challe
        bool filetrans = false;
        Client c;
 
+       MT_thread_set_qry_ctx(NULL);
+
        /* decode BIG/LIT:user:{cypher}passwordchal:lang:database: line */
 
        /* byte order */
@@ -315,6 +317,7 @@ MSscheduleClient(str command, str challe
                                cleanUpScheduleClient(NULL, NULL, fin, fout, 
&command, NULL);
                                return;
                        }
+                       MT_thread_set_qry_ctx(&c->qryctx);
                        Scenario scenario = findScenario("sql");
                        if ((msg = scenario->initClientCmd(c)) != MAL_SUCCEED) {
                                mnstr_printf(fout, "!%s\n", msg);
@@ -330,6 +333,7 @@ MSscheduleClient(str command, str challe
                                return;
                        }
                        cleanUpScheduleClient(c, scenario, NULL, NULL, NULL, 
NULL);
+                       MT_thread_set_qry_ctx(NULL);
                }
 
 
@@ -372,6 +376,7 @@ MSscheduleClient(str command, str challe
                        GDKfree(command);
                        return;
                }
+               MT_thread_set_qry_ctx(&c->qryctx);
                c->filetrans = filetrans;
                c->handshake_options = handshake_opts ? strdup(handshake_opts) 
: NULL;
                /* move this back !! */
@@ -381,6 +386,7 @@ MSscheduleClient(str command, str challe
                                mnstr_printf(fout, "!could not allocate 
space\n");
                                exit_streams(fin, fout);
                                GDKfree(command);
+                               MT_thread_set_qry_ctx(NULL);
                                return;
                        }
                }
@@ -398,6 +404,7 @@ MSscheduleClient(str command, str challe
                                                   "run mserver5 with --set 
%s=yes to change this.\n", mal_enableflag);
                        exit_streams(fin, fout);
                        GDKfree(command);
+                       MT_thread_set_qry_ctx(NULL);
                        return;
                }
        }
@@ -407,6 +414,7 @@ MSscheduleClient(str command, str challe
                exit_streams(fin, fout);
                freeException(msg);
                GDKfree(command);
+               MT_thread_set_qry_ctx(NULL);
                return;
        }
 
@@ -439,6 +447,7 @@ MSscheduleClient(str command, str challe
                exit_streams(fin, fout);
                freeException(msg);
        }
+       MT_thread_set_qry_ctx(NULL);
 }
 
 /*
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to