Changeset: 6fc691c72af6 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/6fc691c72af6
Modified Files:
        clients/Tests/exports.stable.out
        monetdb5/mal/mal.c
        monetdb5/mal/mal_client.c
        monetdb5/mal/mal_import.c
        monetdb5/mal/mal_interpreter.h
        monetdb5/mal/mal_private.h
        monetdb5/mal/mal_profiler.c
        monetdb5/mal/mal_profiler.h
        monetdb5/mal/mal_runtime.c
        monetdb5/mal/mal_runtime.h
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_scenario.c
        sql/server/sql_mvc.c
        sql/storage/sql_storage.h
        sql/storage/store.c
Branch: Jul2021_prof_ext
Log Message:

Patch with non-mal events for profiler.


diffs (truncated from 728 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
@@ -1315,6 +1315,7 @@ void runtimeProfileBegin(Client cntxt, M
 void runtimeProfileExit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci, RuntimeProfile prof);
 void runtimeProfileFinish(Client cntxt, MalBlkPtr mb, MalStkPtr stk);
 void runtimeProfileInit(Client cntxt, MalBlkPtr mb, MalStkPtr stk);
+oid runtimeProfileSetTag(Client cntxt);
 const char *sampleRef;
 const char *schedulerRef;
 const char *selectNotNilRef;
diff --git a/monetdb5/mal/mal.c b/monetdb5/mal/mal.c
--- a/monetdb5/mal/mal.c
+++ b/monetdb5/mal/mal.c
@@ -41,6 +41,42 @@ MT_Lock     mal_copyLock = MT_LOCK_INITI
 MT_Lock     mal_delayLock = MT_LOCK_INITIALIZER(mal_delayLock);
 MT_Lock     mal_oltpLock = MT_LOCK_INITIALIZER(mal_oltpLock);
 
+static pthread_key_t tl_client_key;
+
+static int
+initialize_tl_client_key(void)
+{
+       static bool initialized = false;
+       if (initialized)
+               return 0;
+
+       if (pthread_key_create(&tl_client_key, NULL) != 0)
+               return -1;
+
+       initialized = true;
+       return 0;
+}
+
+Client
+getClientContext(void)
+{
+       if (initialize_tl_client_key())
+               return NULL;
+       return (Client) pthread_getspecific(tl_client_key);
+}
+
+/* declared in mal_private.h so only the MAL interpreter core can access it */
+Client
+setClientContext(Client cntxt)
+{
+       Client old = getClientContext();
+
+       if (pthread_setspecific(tl_client_key, cntxt) != 0)
+               GDKfatal("Failed to set thread local Client context");
+
+       return old;
+}
+
 const char *
 mal_version(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
@@ -46,6 +46,7 @@
 #include "mal_private.h"
 #include "mal_runtime.h"
 #include "mal_authorize.h"
+#include "mal_profiler.h"
 #include "mapi_prompt.h"
 
 int MAL_MAXCLIENTS = 0;
@@ -201,6 +202,11 @@ MCexitClient(Client c)
                c->fdout = NULL;
                c->fdin = NULL;
        }
+       if(malProfileMode > 0)
+               generic_event("client_connection",
+                                         (struct GenericEvent) { &c->idx, 
NULL, NULL, NULL, 0 },
+                                         1);
+       setClientContext(NULL);
 }
 
 static Client
@@ -290,8 +296,16 @@ MCinitClient(oid user, bstream *fin, str
 
        MT_lock_set(&mal_contextLock);
        c = MCnewClient();
-       if (c)
+       if (c) {
+               Client c_old = setClientContext(c);
+               (void) c_old;
+               assert(NULL == c_old);
                c = MCinitClientRecord(c, user, fin, fout);
+               if(malProfileMode > 0)
+                       generic_event("client_connection",
+                                                 (struct GenericEvent) { 
&c->idx, NULL, NULL, NULL, 0 },
+                                                 0);
+       }
        MT_lock_unset(&mal_contextLock);
        return c;
 }
diff --git a/monetdb5/mal/mal_import.c b/monetdb5/mal/mal_import.c
--- a/monetdb5/mal/mal_import.c
+++ b/monetdb5/mal/mal_import.c
@@ -274,7 +274,7 @@ malInclude(Client c, const char *name, i
 str
 evalFile(str fname, int listing)
 {
-       Client c;
+       Client c, c_old;
        stream *fd;
        str filename;
        str msg = MAL_SUCCEED;
@@ -295,13 +295,15 @@ evalFile(str fname, int listing)
                        close_stream(fd);
                throw(MAL,"mal.eval",SQLSTATE(HY013) MAL_MALLOC_FAIL);
        }
-       c= MCinitClient(MAL_ADMIN, bs, 0);
+       c_old = setClientContext(NULL); // save context
+       c = MCinitClient(MAL_ADMIN, bs, 0);
        if( c == NULL){
                throw(MAL,"mal.eval","Can not create user context");
        }
        c->curmodule = c->usermodule = userModule();
        if(c->curmodule == NULL) {
                MCcloseClient(c);
+               setClientContext(c_old); // save context
                throw(MAL,"mal.eval",SQLSTATE(HY013) MAL_MALLOC_FAIL);
        }
        c->promptlength = 0;
@@ -309,15 +311,18 @@ evalFile(str fname, int listing)
 
        if ( (msg = defaultScenario(c)) ) {
                MCcloseClient(c);
+               setClientContext(c_old); // save context
                return msg;
        }
        if((msg = MSinitClientPrg(c, "user", "main")) != MAL_SUCCEED) {
                MCcloseClient(c);
+               setClientContext(c_old); // save context
                return msg;
        }
 
        msg = runScenario(c,0);
        MCcloseClient(c);
+       setClientContext(c_old); // save context
        return msg;
 }
 
@@ -341,7 +346,7 @@ mal_cmdline(char *s, size_t *len)
 str
 compileString(Symbol *fcn, Client cntxt, str s)
 {
-       Client c;
+       Client c, c_old;
        size_t len = strlen(s);
        buffer *b;
        str msg = MAL_SUCCEED;
@@ -380,6 +385,7 @@ compileString(Symbol *fcn, Client cntxt,
        }
        strncpy(fdin->buf, qry, len+1);
 
+       c_old = setClientContext(NULL); // save context
        // compile in context of called for
        c= MCinitClient(MAL_ADMIN, fdin, 0);
        if( c == NULL){
@@ -396,6 +402,7 @@ compileString(Symbol *fcn, Client cntxt,
                GDKfree(b);
                c->usermodule= 0;
                MCcloseClient(c);
+               setClientContext(c_old);
                return msg;
        }
 
@@ -410,6 +417,7 @@ compileString(Symbol *fcn, Client cntxt,
        c->usermodule= 0;
        /* restore IO channel */
        MCcloseClient(c);
+       setClientContext(c_old);
        GDKfree(qry);
        GDKfree(b);
        return msg;
@@ -418,7 +426,7 @@ compileString(Symbol *fcn, Client cntxt,
 str
 callString(Client cntxt, str s, int listing)
 {
-       Client c;
+       Client c, c_old;
        int i;
        size_t len = strlen(s);
        buffer *b;
@@ -448,7 +456,8 @@ callString(Client cntxt, str s, int list
                GDKfree(qry);
                throw(MAL,"callstring", SQLSTATE(HY013) MAL_MALLOC_FAIL);
        }
-       c= MCinitClient(MAL_ADMIN, bs, cntxt->fdout);
+       c_old = setClientContext(NULL);
+       c = MCinitClient(MAL_ADMIN, bs, cntxt->fdout);
        if( c == NULL){
                GDKfree(b);
                GDKfree(qry);
@@ -464,6 +473,7 @@ callString(Client cntxt, str s, int list
                GDKfree(b);
                GDKfree(qry);
                MCcloseClient(c);
+               setClientContext(c_old);
                return msg;
        }
 
@@ -473,6 +483,7 @@ callString(Client cntxt, str s, int list
                GDKfree(qry);
                c->fdout = GDKstdout;
                MCcloseClient(c);
+               setClientContext(c_old);
                return msg;
        }
        msg = runScenario(c,1);
@@ -482,6 +493,7 @@ callString(Client cntxt, str s, int list
                GDKfree(b);
                GDKfree(qry);
                MCcloseClient(c);
+               setClientContext(c_old);
                return msg;
        }
        // The command may have changed the environment of the calling client.
@@ -507,6 +519,7 @@ callString(Client cntxt, str s, int list
        bstream_destroy(c->fdin);
        c->fdin = 0;
        MCcloseClient(c);
+       setClientContext(c_old);
        GDKfree(qry);
        GDKfree(b);
        return msg;
diff --git a/monetdb5/mal/mal_interpreter.h b/monetdb5/mal/mal_interpreter.h
--- a/monetdb5/mal/mal_interpreter.h
+++ b/monetdb5/mal/mal_interpreter.h
@@ -33,6 +33,9 @@ mal_export void garbageCollector(Client 
 mal_export str malCommandCall(MalStkPtr stk, InstrPtr pci);
 mal_export int isNotUsedIn(InstrPtr p, int start, int a);
 
+/* defined in mal.c */
+mal_export Client getClientContext(void);
+
 mal_export ptr getArgReference(MalStkPtr stk, InstrPtr pci, int k);
 #if !defined(NDEBUG) && defined(__GNUC__)
 /* for ease of programming and debugging (assert reporting a useful
diff --git a/monetdb5/mal/mal_private.h b/monetdb5/mal/mal_private.h
--- a/monetdb5/mal/mal_private.h
+++ b/monetdb5/mal/mal_private.h
@@ -41,6 +41,9 @@ str yieldFactory(MalBlkPtr mb, InstrPtr 
        __attribute__((__visibility__("hidden")));
 str callFactory(Client cntxt, MalBlkPtr mb, ValPtr argv[],char flag)
        __attribute__((__visibility__("hidden")));
+
+Client setClientContext(Client cntxt)
+       __attribute__((__visibility__("hidden")));
 #endif
 
 str malAtomDefinition(const char *name,int tpe)
diff --git a/monetdb5/mal/mal_profiler.c b/monetdb5/mal/mal_profiler.c
--- a/monetdb5/mal/mal_profiler.c
+++ b/monetdb5/mal/mal_profiler.c
@@ -75,24 +75,29 @@ static void logjsonInternal(char *logbuf
 }
 
 /*
- * We use a buffer (`logbuffer`) where we incrementally create the output JSON 
object. Initially we allocate LOGLEN (8K)
- * bytes and we keep the capacity of the buffer (`logcap`) and the length of 
the current string (`loglen`).
+ * We use a buffer (`logbuffer`) where we incrementally create the output JSON
+ * object. Initially we allocate LOGLEN (8K)
+ * bytes and we keep the capacity of the buffer (`logcap`) and the length of 
the
+ * current string (`loglen`).
  *
- * We use the `logadd` function to add data to our buffer (usually key-value 
pairs). This macro offers an interface similar
- * to printf.
+ * We use the `logadd` function to add data to our buffer (usually key-value
+ * pairs). This macro offers an interface similar to printf.
  *
- * The first snprintf bellow happens in a statically allocated buffer that 
might be much smaller than logcap. We do not
- * care. We only need to perform this snprintf to get the actual length of the 
string that is to be produced.
+ * The first snprintf bellow happens in a statically allocated buffer that 
might
+ * be much smaller than logcap. We do not care. We only need to perform this
+ * snprintf to get the actual length of the string that is to be produced.
  *
  * There are three cases:
  *
  * 1. The new string fits in the current buffer -> we just update the buffer
  *
- * 2. The new string does not fit in the current buffer, but is smaller than 
the capacity of the buffer -> we output the
- * current contents of the buffer and start at the beginning.
+ * 2. The new string does not fit in the current buffer, but is smaller than 
the
+ * capacity of the buffer -> we output the current contents of the buffer and
+ * start at the beginning.
  *
- * 3. The new string exceeds the current capacity of the buffer -> we output 
the current contents and reallocate the
- * buffer. The new capacity is 1.5 times the length of the new string.
+ * 3. The new string exceeds the current capacity of the buffer -> we output 
the
+ * current contents and reallocate the buffer. The new capacity is 1.5 times 
the
+ * length of the new string.
  */
 struct logbuf {
        char *logbuffer;
@@ -168,6 +173,74 @@ logadd(struct logbuf *logbuf, const char
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to