int GDKinit(opt *set, int setlen) { char *dbpath = mo_find_option(set, setlen, "gdk_dbpath"); char *p; opt *n; int i, j, nlen = 0; char buf[16]; /* some sanity checks (should also find if symbols are not defined) */ assert(sizeof(char) == SIZEOF_CHAR); assert(sizeof(short) == SIZEOF_SHORT); assert(sizeof(int) == SIZEOF_INT); assert(sizeof(long) == SIZEOF_LONG); assert(sizeof(lng) == SIZEOF_LNG); assert(sizeof(oid) == SIZEOF_OID); assert(sizeof(void *) == SIZEOF_VOID_P); assert(sizeof(wrd) == SIZEOF_WRD); assert(sizeof(size_t) == SIZEOF_SIZE_T); assert(sizeof(ptrdiff_t) == SIZEOF_PTRDIFF_T); assert(SIZEOF_OID == SIZEOF_INT || SIZEOF_OID == SIZEOF_LNG); #ifdef NEED_MT_LOCK_INIT MT_lock_init(&MT_system_lock,"MT_system_lock"); ATOMIC_INIT(GDKstoppedLock, "GDKstoppedLock"); ATOMIC_INIT(mbyteslock, "mbyteslock"); MT_lock_init(&GDKnameLock, "GDKnameLock"); MT_lock_init(&GDKthreadLock, "GDKthreadLock"); MT_lock_init(&GDKtmLock, "GDKtmLock"); #endif for (i = 0; i <= BBP_BATMASK; i++) { MT_lock_init(&GDKbatLock[i].swap, "GDKswapLock"); MT_lock_init(&GDKbatLock[i].hash, "GDKhashLock"); MT_lock_init(&GDKbatLock[i].imprints, "GDKimprintsLock"); } for (i = 0; i <= BBP_THREADMASK; i++) { MT_lock_init(&GDKbbpLock[i].alloc, "GDKcacheLock"); MT_lock_init(&GDKbbpLock[i].trim, "GDKtrimLock"); GDKbbpLock[i].free = 0; } errno = 0; if (!GDKenvironment(dbpath)) return 0; if ((p = mo_find_option(set, setlen, "gdk_debug"))) GDKdebug = strtol(p, NULL, 10); if ((p = mo_find_option(set, setlen, "gdk_mem_pagebits"))) GDK_mem_pagebits = (int) strtol(p, NULL, 10); mnstr_init(); MT_init_posix(); THRinit(); #ifndef NATIVE_WIN32 BATSIGinit(); #endif #ifdef WIN32 (void) signal(SIGABRT, BATSIGabort); _set_abort_behavior(0, _CALL_REPORTFAULT | _WRITE_ABORT_MSG); _set_error_mode(_OUT_TO_STDERR); #endif GDKlockHome(); /* Mserver by default takes 80% of all memory as a default */ GDK_mem_maxsize = GDK_mem_maxsize_max = (size_t) ((double) MT_npages() * (double) MT_pagesize() * 0.815); #ifdef NATIVE_WIN32 GDK_mmap_minsize = GDK_mem_maxsize_max; #else GDK_mmap_minsize = MIN( 1<<30 , GDK_mem_maxsize_max/6 ); /* per op: 2 args + 1 res, each with head & tail => (2+1)*2 = 6 ^ */ #endif GDK_mem_bigsize = 1024*1024; GDKremovedir(DELDIR); BBPinit(); HEAPcacheInit(); GDKkey = BATnew(TYPE_void, TYPE_str, 100); GDKval = BATnew(TYPE_void, TYPE_str, 100); if (GDKkey == NULL) GDKfatal("GDKinit: Could not create environment BAT"); if (GDKval == NULL) GDKfatal("GDKinit: Could not create environment BAT"); BATseqbase(GDKkey,0); BATkey(GDKkey, BOUND2BTRUE); BATrename(GDKkey, "environment_key"); BATmode(GDKkey, TRANSIENT); BATseqbase(GDKval,0); BATkey(GDKval, BOUND2BTRUE); BATrename(GDKval, "environment_val"); BATmode(GDKval, TRANSIENT); n = (opt *) malloc(setlen * sizeof(opt)); for (i = 0; i < setlen; i++) { int done = 0; for (j = 0; j < nlen; j++) { if (strcmp(n[j].name, set[i].name) == 0) { if (n[j].kind < set[i].kind) { n[j] = set[i]; } done = 1; break; } } if (!done) { n[nlen] = set[i]; nlen++; } } for (i = 0; i < nlen; i++) GDKsetenv(n[i].name, n[i].value); free(n); if ((p = GDKgetenv("gdk_dbpath")) != NULL && (p = strrchr(p, DIR_SEP)) != NULL) { GDKsetenv("gdk_dbname", p + 1); #if DIR_SEP != '/' /* on Windows look for different separator */ } else if ((p = GDKgetenv("gdk_dbpath")) != NULL && (p = strrchr(p, '/')) != NULL) { GDKsetenv("gdk_dbname", p + 1); #endif } if ((p = GDKgetenv("gdk_mem_maxsize"))) { GDK_mem_maxsize = MAX(1 << 26, (size_t) strtoll(p, NULL, 10)); } if ((p = GDKgetenv("gdk_vm_maxsize"))) { GDK_vm_maxsize = MAX(1 << 30, (size_t) strtoll(p, NULL, 10)); } if ((p = GDKgetenv("gdk_mem_bigsize"))) { /* when allocating >6% of all RAM; do so using * vmalloc() iso malloc() */ lng max_mem_bigsize = GDK_mem_maxsize_max / 16; /* sanity check to avoid memory fragmentation */ GDK_mem_bigsize = (size_t) MIN(max_mem_bigsize, strtoll(p, NULL, 10)); } if ((p = GDKgetenv("gdk_mmap_minsize"))) { GDK_mmap_minsize = MAX(REMAP_PAGE_MAXSIZE, (size_t) strtoll(p, NULL, 10)); } if (GDKgetenv("gdk_mem_pagebits") == NULL) { snprintf(buf, sizeof(buf), "%d", GDK_mem_pagebits); GDKsetenv("gdk_mem_pagebits", buf); } if (GDKgetenv("gdk_mem_bigsize") == NULL) { snprintf(buf, sizeof(buf), SZFMT, GDK_mem_bigsize); GDKsetenv("gdk_mem_bigsize", buf); } if (GDKgetenv("monet_pid") == NULL) { snprintf(buf, sizeof(buf), "%d", (int) getpid()); GDKsetenv("monet_pid", buf); } GDKnr_threads = GDKgetenv_int("gdk_nr_threads", 0); if (GDKnr_threads == 0) GDKnr_threads = MT_check_nr_cores(); #ifdef NATIVE_WIN32 GDK_mmap_minsize /= (GDKnr_threads ? GDKnr_threads : 1); #else /* WARNING: This unconditionally overwrites above settings, */ /* incl. setting via MonetDB env. var. "gdk_mmap_minsize" ! */ GDK_mmap_minsize = MIN( 1<<30 , (GDK_mem_maxsize_max/6) / (GDKnr_threads ? GDKnr_threads : 1) ); /* per op: 2 args + 1 res, each with head & tail => (2+1)*2 = 6 ^ */ #endif if ((p = mo_find_option(set, setlen, "gdk_vmtrim")) == NULL || strcasecmp(p, "yes") == 0) MT_create_thread(&GDKvmtrim_id, GDKvmtrim, &GDK_mem_maxsize, MT_THR_JOINABLE); return 1; }
int mvc_init(int debug, store_type store, int ro, int su, backend_stack stk) { int first = 0; logger_settings *log_settings = (struct logger_settings *) GDKmalloc(sizeof(struct logger_settings)); /* Set the default WAL directory. "sql_logs" by default */ log_settings->logdir = "sql_logs"; /* Get and pass on the WAL directory location, if set */ if (GDKgetenv("gdk_logdir") != NULL) { log_settings->logdir = GDKgetenv("gdk_logdir"); } /* Get and pass on the shared WAL directory location, if set */ log_settings->shared_logdir = GDKgetenv("gdk_shared_logdir"); /* Get and pass on the shared WAL drift threshold, if set. * -1 by default, meaning it should be ignored, since it is not set */ log_settings->shared_drift_threshold = GDKgetenv_int("gdk_shared_drift_threshold", -1); /* Get and pass on the flag how many WAL files should be preserved. * 0 by default - keeps only the current WAL file. */ log_settings->keep_persisted_log_files = GDKgetenv_int("gdk_keep_persisted_log_files", 0); mvc_debug = debug&4; if (mvc_debug) { fprintf(stderr, "#mvc_init logdir %s\n", log_settings->logdir); fprintf(stderr, "#mvc_init keep_persisted_log_files %d\n", log_settings->keep_persisted_log_files); if (log_settings->shared_logdir != NULL) { fprintf(stderr, "#mvc_init shared_logdir %s\n", log_settings->shared_logdir); } fprintf(stderr, "#mvc_init shared_drift_threshold %d\n", log_settings->shared_drift_threshold); } keyword_init(); scanner_init_keywords(); if ((first = store_init(debug, store, ro, su, log_settings, stk)) < 0) { fprintf(stderr, "!mvc_init: unable to create system tables\n"); return -1; } if (first || catalog_version) { sql_schema *s; sql_table *t; mvc *m = mvc_create(0, stk, 0, NULL, NULL); m->sa = sa_create(); /* disable caching */ m->caching = 0; /* disable history */ m->history = 0; /* disable size header */ m->sizeheader = 0; mvc_trans(m); s = m->session->schema = mvc_bind_schema(m, "sys"); assert(m->session->schema != NULL); if (!first) { t = mvc_bind_table(m, s, "tables"); mvc_drop_table(m, s, t, 0); t = mvc_bind_table(m, s, "columns"); mvc_drop_table(m, s, t, 0); } t = mvc_create_view(m, s, "tables", SQL_PERSIST, "SELECT \"id\", \"name\", \"schema_id\", \"query\", CAST(CASE WHEN \"system\" THEN \"type\" + 10 /* system table/view */ ELSE (CASE WHEN \"commit_action\" = 0 THEN \"type\" /* table/view */ ELSE \"type\" + 20 /* global temp table */ END) END AS SMALLINT) AS \"type\", \"system\", \"commit_action\", \"access\", CASE WHEN (NOT \"system\" AND \"commit_action\" > 0) THEN 1 ELSE 0 END AS \"temporary\" FROM \"sys\".\"_tables\" WHERE \"type\" <> 2 UNION ALL SELECT \"id\", \"name\", \"schema_id\", \"query\", CAST(\"type\" + 30 /* local temp table */ AS SMALLINT) AS \"type\", \"system\", \"commit_action\", \"access\", 1 AS \"temporary\" FROM \"tmp\".\"_tables\";", 1); mvc_create_column_(m, t, "id", "int", 32); mvc_create_column_(m, t, "name", "varchar", 1024); mvc_create_column_(m, t, "schema_id", "int", 32); mvc_create_column_(m, t, "query", "varchar", 2048); mvc_create_column_(m, t, "type", "smallint", 16); mvc_create_column_(m, t, "system", "boolean", 1); mvc_create_column_(m, t, "commit_action", "smallint", 16); mvc_create_column_(m, t, "access", "smallint", 16); mvc_create_column_(m, t, "temporary", "smallint", 16); if (!first) { int pub = ROLE_PUBLIC; int p = PRIV_SELECT; int zero = 0; sql_table *privs = find_sql_table(s, "privileges"); table_funcs.table_insert(m->session->tr, privs, &t->base.id, &pub, &p, &zero, &zero); } t = mvc_create_view(m, s, "columns", SQL_PERSIST, "SELECT * FROM (SELECT p.* FROM \"sys\".\"_columns\" AS p UNION ALL SELECT t.* FROM \"tmp\".\"_columns\" AS t) AS columns;", 1); mvc_create_column_(m, t, "id", "int", 32); mvc_create_column_(m, t, "name", "varchar", 1024); mvc_create_column_(m, t, "type", "varchar", 1024); mvc_create_column_(m, t, "type_digits", "int", 32); mvc_create_column_(m, t, "type_scale", "int", 32); mvc_create_column_(m, t, "table_id", "int", 32); mvc_create_column_(m, t, "default", "varchar", 2048); mvc_create_column_(m, t, "null", "boolean", 1); mvc_create_column_(m, t, "number", "int", 32); mvc_create_column_(m, t, "storage", "varchar", 2048); if (!first) { int pub = ROLE_PUBLIC; int p = PRIV_SELECT; int zero = 0; sql_table *privs = find_sql_table(s, "privileges"); table_funcs.table_insert(m->session->tr, privs, &t->base.id, &pub, &p, &zero, &zero); } else { sql_create_env(m, s); sql_create_privileges(m, s); } s = m->session->schema = mvc_bind_schema(m, "tmp"); assert(m->session->schema != NULL); if (mvc_commit(m, 0, NULL) < 0) { fprintf(stderr, "!mvc_init: unable to commit system tables\n"); return -1; } mvc_destroy(m); } return first; }