Changeset: 105d2030bcea for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/105d2030bcea Modified Files: clients/Tests/exports.stable.out monetdb5/mal/mal.c monetdb5/mal/mal_client.c monetdb5/mal/mal_embedded.c monetdb5/mal/mal_import.c monetdb5/mal/mal_interpreter.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: Jan2022_prof_ext Log Message:
Patch for profiler extension. diffs (truncated from 870 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 @@ -1008,6 +1008,7 @@ void freeVariable(MalBlkPtr mb, int vari void garbageCollector(Client cntxt, MalBlkPtr mb, MalStkPtr stk, int flag); void garbageElement(Client cntxt, ValPtr v); const char *generatorRef; +void generic_event(str face, struct GenericEvent e, int state); MALfcn getAddress(const char *modname, const char *fcnname); str getArgDefault(MalBlkPtr mb, InstrPtr p, int idx); ptr getArgReference(MalStkPtr stk, InstrPtr pci, int k); @@ -1333,6 +1334,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 @@ -40,6 +40,43 @@ MT_Lock mal_profileLock = MT_LOCK_IN MT_Lock mal_copyLock = MT_LOCK_INITIALIZER(mal_copyLock); MT_Lock mal_delayLock = MT_LOCK_INITIALIZER(mal_delayLock); +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; +} + +/* declared in mal_interpreter.h so MAL operators can access it */ +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; @@ -204,6 +205,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 @@ -292,8 +298,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_embedded.c b/monetdb5/mal/mal_embedded.c --- a/monetdb5/mal/mal_embedded.c +++ b/monetdb5/mal/mal_embedded.c @@ -43,7 +43,7 @@ static bool embeddedinitialized = false; str malEmbeddedBoot(int workerlimit, int memorylimit, int querytimeout, int sessiontimeout, int with_mapi_server) { - Client c; + Client c, c_old; str msg = MAL_SUCCEED; if( embeddedinitialized ) @@ -99,7 +99,7 @@ malEmbeddedBoot(int workerlimit, int mem initParser(); initHeartbeat(); // initResource(); - + c_old = setClientContext(NULL); //save context c = MCinitClient((oid) 0, 0, 0); if(c == NULL) throw(MAL, "malEmbeddedBoot", "Failed to initialize client"); @@ -110,31 +110,37 @@ malEmbeddedBoot(int workerlimit, int mem c->curmodule = c->usermodule = userModule(); if(c->usermodule == NULL) { MCcloseClient(c); + setClientContext(c_old); // restore context throw(MAL, "malEmbeddedBoot", "Failed to initialize client MAL module"); } if ( (msg = defaultScenario(c)) ) { MCcloseClient(c); + setClientContext(c_old); // restore context return msg; } if ((msg = MSinitClientPrg(c, "user", "main")) != MAL_SUCCEED) { MCcloseClient(c); + setClientContext(c_old); // restore context return msg; } char *modules[5] = { "embedded", "sql", "generator", "udf" }; if ((msg = malIncludeModules(c, modules, 0, !with_mapi_server)) != MAL_SUCCEED) { MCcloseClient(c); + setClientContext(c_old); // restore context return msg; } pushEndInstruction(c->curprg->def); msg = chkProgram(c->usermodule, c->curprg->def); if ( msg != MAL_SUCCEED || (msg= c->curprg->def->errors) != MAL_SUCCEED ) { MCcloseClient(c); + setClientContext(c_old); // restore context return msg; } msg = MALengine(c); if (msg == MAL_SUCCEED) embeddedinitialized = true; MCcloseClient(c); + setClientContext(c_old); // restore context initProfiler(); return msg; } 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,6 +295,7 @@ evalFile(str fname, int listing) close_stream(fd); throw(MAL,"mal.eval",SQLSTATE(HY013) MAL_MALLOC_FAIL); } + c_old = setClientContext(NULL); // save context c= MCinitClient(MAL_ADMIN, bs, 0); if( c == NULL){ throw(MAL,"mal.eval","Can not create user context"); @@ -302,6 +303,7 @@ evalFile(str fname, int listing) c->curmodule = c->usermodule = userModule(); if(c->curmodule == NULL) { MCcloseClient(c); + setClientContext(c_old); // restore 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); // restore context return msg; } if((msg = MSinitClientPrg(c, "user", "main")) != MAL_SUCCEED) { MCcloseClient(c); + setClientContext(c_old); // restore context return msg; } msg = runScenario(c,0); MCcloseClient(c); + setClientContext(c_old); // restore 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); // restore context return msg; } @@ -410,6 +417,7 @@ compileString(Symbol *fcn, Client cntxt, c->usermodule= 0; /* restore IO channel */ MCcloseClient(c); + setClientContext(c_old); // restore context 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); // save context + 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); // restore context return msg; } @@ -473,6 +483,7 @@ callString(Client cntxt, str s, int list GDKfree(qry); c->fdout = GDKstdout; MCcloseClient(c); + setClientContext(c_old); // restore context 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); // restore context 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); // restore context GDKfree(qry); GDKfree(b); return msg; 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 _______________________________________________ checkin-list mailing list -- checkin-list@monetdb.org To unsubscribe send an email to checkin-list-le...@monetdb.org