LCOV - code coverage report
Current view: top level - monetdb5/mal - mal.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 58 72.4 %
Date: 2021-09-14 22:17:06 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /* (author) M. Kersten */
      10             : #include "monetdb_config.h"
      11             : #include "mal.h"
      12             : 
      13             : char    monet_cwd[FILENAME_MAX] = { 0 };
      14             : char    monet_characteristics[4096];
      15             : stream *maleventstream = 0;
      16             : 
      17             : /* The compile time debugging flags are turned into bit masks, akin to GDK */
      18             : lng MALdebug;
      19             : 
      20             : #include "mal_stack.h"
      21             : #include "mal_linker.h"
      22             : #include "mal_authorize.h"
      23             : #include "mal_session.h"
      24             : #include "mal_scenario.h"
      25             : #include "mal_parser.h"
      26             : #include "mal_interpreter.h"
      27             : #include "mal_namespace.h"  /* for initNamespace() */
      28             : #include "mal_profiler.h"
      29             : #include "mal_client.h"
      30             : #include "msabaoth.h"
      31             : #include "mal_dataflow.h"
      32             : #include "mal_private.h"
      33             : #include "mal_runtime.h"
      34             : #include "mal_resource.h"
      35             : #include "mal_atom.h"
      36             : 
      37             : MT_Lock     mal_contextLock = MT_LOCK_INITIALIZER(mal_contextLock);
      38             : MT_Lock     mal_remoteLock = MT_LOCK_INITIALIZER(mal_remoteLock);
      39             : MT_Lock     mal_profileLock = MT_LOCK_INITIALIZER(mal_profileLock);
      40             : MT_Lock     mal_copyLock = MT_LOCK_INITIALIZER(mal_copyLock);
      41             : MT_Lock     mal_delayLock = MT_LOCK_INITIALIZER(mal_delayLock);
      42             : MT_Lock     mal_oltpLock = MT_LOCK_INITIALIZER(mal_oltpLock);
      43             : 
      44             : const char *
      45         256 : mal_version(void)
      46             : {
      47         256 :         return MONETDB5_VERSION;
      48             : }
      49             : 
      50             : /*
      51             :  * Initialization of the MAL context
      52             :  */
      53             : 
      54             : int
      55         265 : mal_init(char *modules[], int embedded)
      56             : {
      57             : /* Any error encountered here terminates the process
      58             :  * with a message sent to stderr
      59             :  */
      60             :         str err;
      61             : 
      62             :         /* check that library that we're linked against is compatible with
      63             :          * the one we were compiled with */
      64             :         int maj, min, patch;
      65         265 :         const char *version = GDKlibversion();
      66         265 :         sscanf(version, "%d.%d.%d", &maj, &min, &patch);
      67         265 :         if (maj != GDK_VERSION_MAJOR || min < GDK_VERSION_MINOR) {
      68           0 :                 TRC_CRITICAL(MAL_SERVER, "Linked GDK library not compatible with the one this was compiled with\n");
      69           0 :                 TRC_CRITICAL(MAL_SERVER, "Linked version: %s, compiled version: %s\n",
      70             :                                          version, GDK_VERSION);
      71           0 :                 return -1;
      72             :         }
      73             : 
      74         265 :         if ((err = AUTHinitTables(NULL)) != MAL_SUCCEED) {
      75           0 :                 freeException(err);
      76           0 :                 return -1;
      77             :         }
      78             : 
      79         265 :         if (!MCinit())
      80             :                 return -1;
      81             : #ifndef NDEBUG
      82         265 :         if (!mdbInit()) {
      83           0 :                 mal_client_reset();
      84           0 :                 return -1;
      85             :         }
      86             : #endif
      87         265 :         initNamespace();
      88         265 :         initParser();
      89         265 :         initHeartbeat();
      90             : 
      91         265 :         err = malBootstrap(modules, embedded);
      92         264 :         if (err != MAL_SUCCEED) {
      93           0 :                 mal_client_reset();
      94             : #ifndef NDEBUG
      95           0 :                 mdbExit();
      96             : #endif
      97           0 :                 TRC_CRITICAL(MAL_SERVER, "%s\n", err);
      98           0 :                 freeException(err);
      99           0 :                 return -1;
     100             :         }
     101         264 :         initProfiler();
     102         264 :         return 0;
     103             : }
     104             : 
     105             : /*
     106             :  * Upon exit we should attempt to remove all allocated memory explicitly.
     107             :  * This seemingly superflous action is necessary to simplify analyis of
     108             :  * memory leakage problems later ons and to allow an embedded server to
     109             :  * restart the server properly.
     110             :  *
     111             :  * It is the responsibility of the enclosing application to finish/cease all
     112             :  * activity first.
     113             :  * This function should be called after you have issued sql_reset();
     114             :  */
     115         263 : void mal_reset(void)
     116             : {
     117         263 :         GDKprepareExit();
     118         263 :         MCstopClients(0);
     119         263 :         setHeartbeat(-1);
     120         263 :         stopProfiler(0);
     121         263 :         AUTHreset();
     122         263 :         if (!GDKinmemory(0) && !GDKembedded()) {
     123             :                 str err = 0;
     124             : 
     125         253 :                 if ((err = msab_wildRetreat()) != NULL) {
     126           0 :                         TRC_ERROR(MAL_SERVER, "%s\n", err);
     127           0 :                         free(err);
     128             :                 }
     129         253 :                 if ((err = msab_registerStop()) != NULL) {
     130           0 :                         TRC_ERROR(MAL_SERVER, "%s\n", err);
     131           0 :                         free(err);
     132             :                 }
     133             :         }
     134         263 :         mal_factory_reset();
     135         263 :         mal_dataflow_reset();
     136         263 :         mal_client_reset();
     137         263 :         mal_linker_reset();
     138         263 :         mal_resource_reset();
     139         263 :         mal_runtime_reset();
     140         263 :         mal_module_reset();
     141         263 :         mal_atom_reset();
     142             : #ifndef NDEBUG
     143         263 :         mdbExit();
     144             : #endif
     145             : 
     146         263 :         memset((char*)monet_cwd, 0, sizeof(monet_cwd));
     147         263 :         memset((char*)monet_characteristics,0, sizeof(monet_characteristics));
     148         263 :         mal_namespace_reset();
     149             :         /* No need to clean up the namespace, it will simply be extended
     150             :          * upon restart mal_namespace_reset(); */
     151         263 :         GDKreset(0);    // terminate all other threads
     152         263 : }
     153             : 
     154             : 
     155             : /* stopping clients should be done with care, as they may be in the mids of
     156             :  * transactions. One safe place is between MAL instructions, which would
     157             :  * abort the transaction by raising an exception. All sessions are
     158             :  * terminate this way.
     159             :  * We should also ensure that no new client enters the scene while shutting down.
     160             :  * For this we mark the client records as BLOCKCLIENT.
     161             :  *
     162             :  * Beware, mal_exit is also called during a SIGTERM from the monetdb tool
     163             :  */
     164             : 
     165         253 : void mal_exit(int status)
     166             : {
     167         253 :         mal_reset();
     168         253 :         exit(status);                           /* properly end GDK */
     169             : }

Generated by: LCOV version 1.14