LCOV - code coverage report
Current view: top level - gdk - gdk_bbp.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1610 2720 59.2 %
Date: 2024-10-03 20:03:20 Functions: 78 92 84.8 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : /*
      14             :  * @a M. L. Kersten, P. Boncz, N. J. Nes
      15             :  * @* BAT Buffer Pool (BBP)
      16             :  * The BATs created and loaded are collected in a BAT buffer pool.
      17             :  * The Bat Buffer Pool has a number of functions:
      18             :  * @table @code
      19             :  *
      20             :  * @item administration and lookup
      21             :  * The BBP is a directory which contains status information about all
      22             :  * known BATs.  This interface may be used very heavily, by
      23             :  * data-intensive applications.  To eliminate all overhead, read-only
      24             :  * access to the BBP may be done by table-lookups. The integer index
      25             :  * type for these lookups is @emph{bat}, as retrieved by
      26             :  * @emph{b->batCacheid}. The @emph{bat} zero is reserved for the nil
      27             :  * bat.
      28             :  *
      29             :  * @item persistence
      30             :  * The BBP is made persistent by saving it to the dictionary file
      31             :  * called @emph{BBP.dir} in the database.
      32             :  *
      33             :  * When the number of BATs rises, having all files in one directory
      34             :  * becomes a bottleneck.  The BBP therefore implements a scheme that
      35             :  * distributes all BATs in a growing directory tree with at most 64
      36             :  * BATs stored in one node.
      37             :  *
      38             :  * @item buffer management
      39             :  * The BBP is responsible for loading and saving of BATs to disk. It
      40             :  * also contains routines to unload BATs from memory when memory
      41             :  * resources get scarce. For this purpose, it administers BAT memory
      42             :  * reference counts (to know which BATs can be unloaded) and BAT usage
      43             :  * statistics (it unloads the least recently used BATs).
      44             :  *
      45             :  * @item recovery
      46             :  * When the database is closed or during a run-time syncpoint, the
      47             :  * system tables must be written to disk in a safe way, that is immune
      48             :  * for system failures (like disk full). To do so, the BBP implements
      49             :  * an atomic commit and recovery protocol: first all files to be
      50             :  * overwritten are moved to a BACKUP/ dir. If that succeeds, the
      51             :  * writes are done. If that also fully succeeds the BACKUP/ dir is
      52             :  * renamed to DELETE_ME/ and subsequently deleted.  If not, all files
      53             :  * in BACKUP/ are moved back to their original location.
      54             :  *
      55             :  * @item unloading
      56             :  * Bats which have a logical reference (ie. a lrefs > 0) but no memory
      57             :  * reference (refcnt == 0) can be unloaded. Unloading dirty bats
      58             :  * means, moving the original (committed version) to the BACKUP/ dir
      59             :  * and saving the bat. This complicates the commit and recovery/abort
      60             :  * issues.  The commit has to check if the bat is already moved. And
      61             :  * The recovery has to always move back the files from the BACKUP/
      62             :  * dir.
      63             :  *
      64             :  * @item reference counting
      65             :  * Bats use have two kinds of references: logical and physical
      66             :  * (pointer) ones.  The logical references are administered by
      67             :  * BBPretain/BBPrelease, the physical ones by BBPfix/BBPunfix.
      68             :  */
      69             : 
      70             : #include "monetdb_config.h"
      71             : #include "gdk.h"
      72             : #include "gdk_private.h"
      73             : #include "mutils.h"
      74             : #ifdef HAVE_FCNTL_H
      75             : #include <fcntl.h>
      76             : #endif
      77             : 
      78             : #ifndef F_OK
      79             : #define F_OK 0
      80             : #endif
      81             : #ifndef S_ISDIR
      82             : #define S_ISDIR(mode)   (((mode) & _S_IFMT) == _S_IFDIR)
      83             : #endif
      84             : #ifndef O_CLOEXEC
      85             : #ifdef _O_NOINHERIT
      86             : #define O_CLOEXEC _O_NOINHERIT  /* Windows */
      87             : #else
      88             : #define O_CLOEXEC 0
      89             : #endif
      90             : #endif
      91             : #ifndef O_BINARY
      92             : #define O_BINARY 0
      93             : #endif
      94             : 
      95             : /*
      96             :  * The BBP has a fixed address, so re-allocation due to a growing BBP
      97             :  * caused by one thread does not disturb reads to the old entries by
      98             :  * another.  This is implemented using anonymous virtual memory;
      99             :  * extensions on the same address are guaranteed because a large
     100             :  * non-committed VM area is requested initially. New slots in the BBP
     101             :  * are found in O(1) by keeping a freelist that uses the 'next' field
     102             :  * in the BBPrec records.
     103             :  */
     104             : static BBPrec BBP0[BBPINIT];
     105             : BBPrec *BBP[N_BBPINIT] = {[0] = BBP0}; /* fixed base VM address of BBP array */
     106             : bat BBPlimit = BBPINIT;         /* current committed VM BBP array */
     107             : static ATOMIC_TYPE BBPsize = ATOMIC_VAR_INIT(0); /* current used size of BBP array */
     108             : 
     109             : struct BBPfarm_t BBPfarms[MAXFARMS];
     110             : 
     111             : #define KITTENNAP 1             /* used to suspend processing */
     112             : #define BBPNONAME "."         /* filler for no name in BBP.dir */
     113             : /*
     114             :  * The hash index uses a bucket index (int array) of size mask that is
     115             :  * tuned for perfect hashing (1 lookup). The bucket chain uses the
     116             :  * 'next' field in the BBPrec records.
     117             :  */
     118             : static MT_Lock BBPnameLock = MT_LOCK_INITIALIZER(BBPnameLock);
     119             : #define BBP_mask        1023            /* number of buckets = & mask */
     120             : static bat BBP_hash[BBP_mask+1];        /* BBP logical name hash buckets */
     121             : static MT_Lock GDKcacheLock = MT_LOCK_INITIALIZER(GDKcacheLock);
     122             : static bat BBP_free;
     123             : static uint32_t BBP_nfree;
     124             : #define BBP_FREE_LOWATER        10
     125             : #define BBP_FREE_HIWATER        50
     126             : 
     127             : static gdk_return BBPfree(BAT *b);
     128             : static void BBPdestroy(BAT *b);
     129             : static void BBPuncacheit(bat bid, bool unloaddesc);
     130             : static gdk_return BBPprepare(bool subcommit);
     131             : static BAT *getBBPdescriptor(bat i);
     132             : static gdk_return BBPbackup(BAT *b, bool subcommit);
     133             : static gdk_return BBPdir_init(void);
     134             : static void BBPcallbacks(void);
     135             : 
     136             : /* two lngs of extra info in BBP.dir */
     137             : /* these two are atomic because of their use in log_new() */
     138             : static ATOMIC_TYPE BBPlogno = ATOMIC_VAR_INIT(0);
     139             : 
     140             : #define BBPtmpcheck(s)  (strncmp(s, "tmp_", 4) == 0)
     141             : 
     142             : #define BBPnamecheck(s) (BBPtmpcheck(s) ? strtol((s) + 4, NULL, 8) : 0)
     143             : 
     144             : #define BATno_shared_heap(b) \
     145             :         (!VIEWtparent(b) && (ATOMIC_GET(&(b)->theap->refs) & HEAPREFS) == 1)
     146             : 
     147             : #define BATshared(b) \
     148             :         ((!VIEWtparent(b) && (ATOMIC_GET(&(b)->theap->refs) & HEAPREFS) > 1) || \
     149             :          ((b)->tvheap && !VIEWvtparent(b) && (ATOMIC_GET(&(b)->tvheap->refs) & HEAPREFS) > 1))
     150             : 
     151             : static void
     152       26251 : BBP_insert(bat i)
     153             : {
     154       26251 :         bat idx = (bat) (strHash(BBP_logical(i)) & BBP_mask);
     155             : 
     156       26251 :         BBP_next(i) = BBP_hash[idx];
     157       26251 :         BBP_hash[idx] = i;
     158       26251 : }
     159             : 
     160             : static void
     161       14534 : BBP_delete(bat i)
     162             : {
     163       14534 :         const char *s = BBP_logical(i);
     164       14534 :         bat idx = (bat) (strHash(s) & BBP_mask);
     165             : 
     166       14534 :         for (bat *h = &BBP_hash[idx]; (i = *h) != 0; h = &BBP_next(i)) {
     167       14534 :                 if (strcmp(BBP_logical(i), s) == 0) {
     168       14534 :                         *h = BBP_next(i);
     169       14534 :                         break;
     170             :                 }
     171             :         }
     172       14534 : }
     173             : 
     174             : bat
     175   389255289 : getBBPsize(void)
     176             : {
     177   389255289 :         return (bat) ATOMIC_GET(&BBPsize);
     178             : }
     179             : 
     180             : lng
     181         361 : getBBPlogno(void)
     182             : {
     183         361 :         return (lng) ATOMIC_GET(&BBPlogno);
     184             : }
     185             : 
     186             : 
     187             : /*
     188             :  * @+ BBP Consistency and Concurrency
     189             :  * While GDK provides the basic building blocks for an ACID system, in
     190             :  * itself it is not such a system, as we this would entail too much
     191             :  * overhead that is often not needed. Hence, some consistency control
     192             :  * is left to the user. The first important user constraint is that if
     193             :  * a user updates a BAT, (s)he himself must assure that no-one else
     194             :  * accesses this BAT.
     195             :  *
     196             :  * Concerning buffer management, the BBP carries out a swapping
     197             :  * policy.  BATs are kept in memory till the memory is full. If the
     198             :  * memory is full, the malloc functions initiate BBP trim actions,
     199             :  * that unload the coldest BATs that have a zero reference count. The
     200             :  * second important user constraint is therefore that a user may only
     201             :  * manipulate live BAT data in memory if it is sure that there is at
     202             :  * least one reference count to that BAT.
     203             :  *
     204             :  * The main BBP array is protected by two locks:
     205             :  * @table @code
     206             :  * @item GDKcacheLock]
     207             :  * this lock guards the free slot management in the BBP array.  The
     208             :  * BBP operations that allocate a new slot for a new BAT
     209             :  * (@emph{BBPinit},@emph{BBPcacheit}), delete the slot of a destroyed
     210             :  * BAT (@emph{BBPreclaim}), or rename a BAT (@emph{BBPrename}), hold
     211             :  * this lock. It also protects all BAT (re)naming actions include
     212             :  * (read and write) in the hash table with BAT names.
     213             :  * @item GDKswapLock
     214             :  * this lock guards the swap (loaded/unloaded) status of the
     215             :  * BATs. Hence, all BBP routines that influence the swapping policy,
     216             :  * or actually carry out the swapping policy itself, acquire this lock
     217             :  * (e.g. @emph{BBPfix},@emph{BBPunfix}).  Note that this also means
     218             :  * that updates to the BBP_status indicator array must be protected by
     219             :  * GDKswapLock.
     220             :  *
     221             :  * To reduce contention GDKswapLock was split into multiple locks; it
     222             :  * is now an array of lock pointers which is accessed by
     223             :  * GDKswapLock(bat)
     224             :  * @end table
     225             :  *
     226             :  * Routines that need both locks should first acquire the locks in the
     227             :  * GDKswapLock array (in ascending order) and then GDKcacheLock (and
     228             :  * release them in reverse order).
     229             :  *
     230             :  * To obtain maximum speed, read operations to existing elements in
     231             :  * the BBP are unguarded. As said, it is the users responsibility that
     232             :  * the BAT that is being read is not being modified. BBP update
     233             :  * actions that modify the BBP data structure itself are locked by the
     234             :  * BBP functions themselves. Hence, multiple concurrent BBP read
     235             :  * operations may be ongoing while at the same time at most one BBP
     236             :  * write operation @strong{on a different BAT} is executing.  This
     237             :  * holds for accesses to the public (quasi-) arrays @emph{BBPcache},
     238             :  * @emph{BBPstatus} and @emph{BBPrefs}.
     239             :  * These arrays are called quasi as now they are
     240             :  * actually stored together in one big BBPrec array called BBP, that
     241             :  * is allocated in anonymous VM space, so we can reallocate this
     242             :  * structure without changing the base address (a crucial feature if
     243             :  * read actions are to go on unlocked while other entries in the BBP
     244             :  * may be modified).
     245             :  */
     246             : static volatile MT_Id locked_by = 0;
     247             : 
     248             : /* use a lock instead of atomic instructions so that we wait for
     249             :  * BBPlock/BBPunlock */
     250             : #define BBP_unload_inc()                        \
     251             :         do {                                    \
     252             :                 MT_lock_set(&GDKunloadLock);        \
     253             :                 BBPunloadCnt++;                 \
     254             :                 MT_lock_unset(&GDKunloadLock);      \
     255             :         } while (0)
     256             : 
     257             : #define BBP_unload_dec()                        \
     258             :         do {                                    \
     259             :                 MT_lock_set(&GDKunloadLock);        \
     260             :                 --BBPunloadCnt;                 \
     261             :                 assert(BBPunloadCnt >= 0);   \
     262             :                 MT_lock_unset(&GDKunloadLock);      \
     263             :         } while (0)
     264             : 
     265             : static int BBPunloadCnt = 0;
     266             : static MT_Lock GDKunloadLock = MT_LOCK_INITIALIZER(GDKunloadLock);
     267             : 
     268             : void
     269           8 : BBPlock(void)
     270             : {
     271           8 :         int i;
     272             : 
     273             :         /* wait for all pending unloads to finish */
     274           8 :         MT_lock_set(&GDKunloadLock);
     275           8 :         while (BBPunloadCnt > 0) {
     276           0 :                 MT_lock_unset(&GDKunloadLock);
     277           0 :                 MT_sleep_ms(1);
     278           8 :                 MT_lock_set(&GDKunloadLock);
     279             :         }
     280             : 
     281           8 :         BBPtmlock();
     282           8 :         MT_lock_set(&GDKcacheLock);
     283       65552 :         for (i = 0; i <= BBP_BATMASK; i++)
     284       65536 :                 MT_lock_set(&GDKswapLock(i));
     285           8 :         locked_by = MT_getpid();
     286             : 
     287           8 :         MT_lock_unset(&GDKunloadLock);
     288           8 : }
     289             : 
     290             : void
     291           8 : BBPunlock(void)
     292             : {
     293           8 :         int i;
     294             : 
     295       65544 :         for (i = BBP_BATMASK; i >= 0; i--)
     296       65536 :                 MT_lock_unset(&GDKswapLock(i));
     297           8 :         MT_lock_unset(&GDKcacheLock);
     298           8 :         locked_by = 0;
     299           8 :         BBPtmunlock();
     300           8 : }
     301             : 
     302             : int
     303    11783842 : BBPselectfarm(role_t role, int type, enum heaptype hptype)
     304             : {
     305    11783842 :         int i;
     306             : 
     307    11783842 :         (void) type;            /* may use in future */
     308    11783842 :         (void) hptype;          /* may use in future */
     309             : 
     310    11783842 :         if (GDKinmemory(0))
     311             :                 return 0;
     312             : 
     313             : #ifndef PERSISTENTHASH
     314             :         if (hptype == hashheap)
     315             :                 role = TRANSIENT;
     316             : #endif
     317             : #ifndef PERSISTENTIDX
     318             :         if (hptype == orderidxheap)
     319             :                 role = TRANSIENT;
     320             : #endif
     321    23212333 :         for (i = 0; i < MAXFARMS; i++)
     322    23212333 :                 if (BBPfarms[i].roles & (1U << (int) role))
     323    11764064 :                         return i;
     324             :         /* must be able to find farms for TRANSIENT and PERSISTENT */
     325           0 :         assert(role != TRANSIENT && role != PERSISTENT);
     326             :         return -1;
     327             : }
     328             : 
     329             : static gdk_return
     330         327 : BBPextend(bat newsize)
     331             : {
     332         327 :         if (newsize > N_BBPINIT * BBPINIT) {
     333           0 :                 GDKerror("trying to extend BAT pool beyond the "
     334             :                          "limit (%d)\n", N_BBPINIT * BBPINIT);
     335           0 :                 return GDK_FAIL;
     336             :         }
     337             : 
     338             :         /* make sure the new size is at least BBPsize large */
     339         327 :         while (BBPlimit < newsize) {
     340           0 :                 BUN limit = BBPlimit >> BBPINITLOG;
     341           0 :                 assert(BBP[limit] == NULL);
     342           0 :                 BBP[limit] = GDKzalloc(BBPINIT * sizeof(BBPrec));
     343           0 :                 if (BBP[limit] == NULL) {
     344           0 :                         GDKerror("failed to extend BAT pool\n");
     345           0 :                         return GDK_FAIL;
     346             :                 }
     347           0 :                 for (BUN i = 0; i < BBPINIT; i++) {
     348           0 :                         ATOMIC_INIT(&BBP[limit][i].status, 0);
     349           0 :                         BBP[limit][i].pid = ~(MT_Id)0;
     350             :                 }
     351           0 :                 BBPlimit += BBPINIT;
     352             :         }
     353             : 
     354             :         return GDK_SUCCEED;
     355             : }
     356             : 
     357             : static gdk_return
     358         101 : recover_dir(int farmid, bool direxists)
     359             : {
     360         101 :         if (direxists) {
     361             :                 /* just try; don't care about these non-vital files */
     362           0 :                 if (GDKunlink(farmid, BATDIR, "BBP", "bak") != GDK_SUCCEED)
     363           0 :                         GDKwarning("unlink of BBP.bak failed\n");
     364           0 :                 if (GDKmove(farmid, BATDIR, "BBP", "dir", BATDIR, "BBP", "bak", false) != GDK_SUCCEED)
     365           0 :                         GDKwarning("rename of BBP.dir to BBP.bak failed\n");
     366             :         }
     367         101 :         return GDKmove(farmid, BAKDIR, "BBP", "dir", BATDIR, "BBP", "dir", true);
     368             : }
     369             : 
     370             : static inline str
     371      493147 : BBPsubdir_recursive(char *s, bat i)
     372             : {
     373      493147 :         i >>= 6;
     374      493147 :         if (i >= 0100) {
     375       67490 :                 s = BBPsubdir_recursive(s, i);
     376       67490 :                 *s++ = DIR_SEP;
     377             :         }
     378      493147 :         i &= 077;
     379      493147 :         *s++ = '0' + (i >> 3);
     380      493147 :         *s++ = '0' + (i & 7);
     381      493147 :         return s;
     382             : }
     383             : 
     384             : static inline void
     385           0 : BBPgetsubdir(char *s, bat i)
     386             : {
     387           0 :         if (i >= 0100) {
     388           0 :                 s = BBPsubdir_recursive(s, i);
     389             :         }
     390           0 :         *s = 0;
     391           0 : }
     392             : 
     393             : static inline void
     394      446349 : BBPgetfilename(char *s, size_t len, bat i)
     395             : {
     396      446349 :         if (i >= 0100) {
     397      425681 :                 char *p = BBPsubdir_recursive(s, i);
     398      425637 :                 *p++ = DIR_SEP;
     399      425637 :                 len -= (p - s);
     400      425637 :                 s = p;
     401             :         }
     402      446305 :         if (snprintf(s, len, "%o", i) >= (int) len)
     403           0 :                 TRC_CRITICAL(BAT_, "impossible error\n");
     404      446305 : }
     405             : 
     406             : static gdk_return BBPrecover(int farmid);
     407             : static gdk_return BBPrecover_subdir(void);
     408             : static bool BBPdiskscan(const char *, size_t);
     409             : 
     410             : static int
     411        7685 : vheapinit(BAT *b, const char *buf, unsigned bbpversion, const char *filename, int lineno)
     412             : {
     413        7685 :         int n = 0;
     414        7685 :         uint64_t free, size;
     415        7685 :         uint16_t storage;
     416             : 
     417        7685 :         (void) bbpversion;      /* could be used to implement compatibility */
     418             : 
     419        7685 :         size = 0;                             /* for GDKLIBRARY_HSIZE case */
     420        7685 :         storage = STORE_INVALID;              /* for GDKLIBRARY_HSIZE case */
     421       15370 :         if (bbpversion <= GDKLIBRARY_HSIZE ?
     422           0 :             sscanf(buf,
     423             :                    " %" SCNu64 " %" SCNu64 " %" SCNu16
     424             :                    "%n",
     425             :                    &free, &size, &storage, &n) < 3 :
     426        7685 :             sscanf(buf,
     427             :                    " %" SCNu64
     428             :                    "%n",
     429             :                    &free, &n) < 1) {
     430           0 :                 TRC_CRITICAL(GDK, "invalid format for BBP.dir on line %d", lineno);
     431           0 :                 return -1;
     432             :         }
     433        7685 :         if (b->batCount == 0)
     434        2098 :                 free = 0;
     435        7685 :         if (b->ttype >= 0 &&
     436        7471 :             ATOMstorage(b->ttype) == TYPE_str &&
     437        7417 :             free < GDK_STRHASHTABLE * sizeof(stridx_t) + BATTINY * GDK_VARALIGN)
     438        6372 :                 size = GDK_STRHASHTABLE * sizeof(stridx_t) + BATTINY * GDK_VARALIGN;
     439        1313 :         else if (free < 512)
     440         132 :                 size = 512;
     441             :         else
     442        1181 :                 size = free;
     443       15370 :         *b->tvheap = (Heap) {
     444        7685 :                 .free = (size_t) free,
     445        7685 :                 .size = (size_t) size,
     446             :                 .base = NULL,
     447             :                 .storage = STORE_INVALID,
     448             :                 .cleanhash = true,
     449             :                 .newstorage = STORE_INVALID,
     450             :                 .dirty = false,
     451        7685 :                 .parentid = b->batCacheid,
     452        7685 :                 .farmid = BBPselectfarm(PERSISTENT, b->ttype, varheap),
     453        7685 :                 .hasfile = free > 0,
     454             :         };
     455        7685 :         strconcat_len(b->tvheap->filename, sizeof(b->tvheap->filename),
     456             :                       filename, ".theap", NULL);
     457        7685 :         return n;
     458             : }
     459             : 
     460             : static int
     461       28741 : heapinit(BAT *b, const char *buf,
     462             : #ifdef GDKLIBRARY_HASHASH
     463             :          int *hashash,
     464             : #endif
     465             :          unsigned bbpversion, const char *filename, int lineno)
     466             : {
     467       28741 :         int t;
     468       28741 :         char type[33];
     469       28741 :         uint16_t width;
     470       28741 :         uint16_t var;
     471       28741 :         uint16_t properties;
     472       28741 :         uint64_t nokey0;
     473       28741 :         uint64_t nokey1;
     474       28741 :         uint64_t nosorted;
     475       28741 :         uint64_t norevsorted;
     476       28741 :         uint64_t base;
     477       28741 :         uint64_t free;
     478       28741 :         uint64_t size;
     479       28741 :         uint16_t storage;
     480       28741 :         uint64_t minpos, maxpos;
     481       28741 :         int n;
     482             : 
     483       28741 :         (void) bbpversion;      /* could be used to implement compatibility */
     484             : 
     485       28741 :         size = 0;                             /* for GDKLIBRARY_HSIZE case */
     486       28741 :         storage = STORE_INVALID;              /* for GDKLIBRARY_HSIZE case */
     487       57482 :         if (bbpversion <= GDKLIBRARY_HSIZE ?
     488           0 :             sscanf(buf,
     489             :                    " %10s %" SCNu16 " %" SCNu16 " %" SCNu16 " %" SCNu64
     490             :                    " %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64
     491             :                    " %" SCNu64 " %" SCNu64 " %" SCNu16 " %" SCNu64 " %" SCNu64
     492             :                    "%n",
     493             :                    type, &width, &var, &properties, &nokey0,
     494             :                    &nokey1, &nosorted, &norevsorted, &base,
     495             :                    &free, &size, &storage, &minpos, &maxpos,
     496             :                    &n) < 14 :
     497       28741 :             sscanf(buf,
     498             :                    " %10s %" SCNu16 " %" SCNu16 " %" SCNu16 " %" SCNu64
     499             :                    " %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64
     500             :                    " %" SCNu64 " %" SCNu64 " %" SCNu64
     501             :                    "%n",
     502             :                    type, &width, &var, &properties, &nokey0,
     503             :                    &nokey1, &nosorted, &norevsorted, &base,
     504             :                    &free, &minpos, &maxpos,
     505             :                    &n) < 12) {
     506           0 :                 TRC_CRITICAL(GDK, "invalid format for BBP.dir on line %d", lineno);
     507           0 :                 return -1;
     508             :         }
     509             : 
     510       28741 :         if (strcmp(type, "wkba") == 0)
     511           0 :                 GDKwarning("type wkba (SQL name: GeometryA) is deprecated\n");
     512             : 
     513       28741 :         if (properties & ~0x1F81) {
     514           0 :                 TRC_CRITICAL(GDK, "unknown properties are set: incompatible database on line %d of BBP.dir\n", lineno);
     515           0 :                 return -1;
     516             :         }
     517             : #ifdef GDKLIBRARY_HASHASH
     518       28741 :         *hashash = var & 2;
     519             : #endif
     520       28741 :         var &= ~2;
     521       28741 :         if ((t = ATOMindex(type)) < 0) {
     522         241 :                 if ((t = ATOMunknown_find(type)) == 0) {
     523           0 :                         TRC_CRITICAL(GDK, "no space for atom %s", type);
     524           0 :                         return -1;
     525             :                 }
     526       35971 :         } else if (var != (t == TYPE_void || BATatoms[t].atomPut != NULL)) {
     527           0 :                 TRC_CRITICAL(GDK, "inconsistent entry in BBP.dir: tvarsized mismatch for BAT %d on line %d\n", (int) b->batCacheid, lineno);
     528           0 :                 return -1;
     529       28500 :         } else if (var && t != 0 ?
     530        7471 :                    ATOMsize(t) < width ||
     531        7471 :                    (width != 1 && width != 2 && width != 4
     532             : #if SIZEOF_VAR_T == 8
     533          54 :                     && width != 8
     534             : #endif
     535             :                            ) :
     536       21029 :                    ATOMsize(t) != width) {
     537           0 :                 TRC_CRITICAL(GDK, "inconsistent entry in BBP.dir: tsize mismatch for BAT %d on line %d\n", (int) b->batCacheid, lineno);
     538           0 :                 return -1;
     539             :         }
     540       28741 :         b->ttype = t;
     541       28741 :         b->twidth = width;
     542       28741 :         b->tshift = ATOMelmshift(width);
     543       28741 :         assert_shift_width(b->tshift,b->twidth);
     544       28741 :         b->tnokey[0] = (BUN) nokey0;
     545       28741 :         b->tnokey[1] = (BUN) nokey1;
     546       28741 :         b->tsorted = (bit) ((properties & 0x0001) != 0);
     547       28741 :         b->trevsorted = (bit) ((properties & 0x0080) != 0);
     548       28741 :         b->tkey = (properties & 0x0100) != 0;
     549       28741 :         b->tnonil = (properties & 0x0400) != 0;
     550       28741 :         b->tnil = (properties & 0x0800) != 0;
     551       28741 :         b->tascii = (properties & 0x1000) != 0;
     552       28741 :         b->tnosorted = (BUN) nosorted;
     553       28741 :         b->tnorevsorted = (BUN) norevsorted;
     554       28741 :         b->tunique_est = 0.0;
     555             :         /* (properties & 0x0200) is the old tdense flag */
     556       28741 :         b->tseqbase = (properties & 0x0200) == 0 || base >= (uint64_t) oid_nil ? oid_nil : (oid) base;
     557       28741 :         b->theap->free = (size_t) free;
     558       28741 :         b->theap->hasfile = free > 0;
     559             :         /* set heap size to match capacity */
     560       28741 :         if (b->ttype == TYPE_msk) {
     561             :                 /* round up capacity to multiple of 32 */
     562        4398 :                 b->batCapacity = (b->batCapacity + 31) & ~((BUN) 31);
     563        4398 :                 b->theap->size = b->batCapacity / 8;
     564             :         } else {
     565       24343 :                 b->theap->size = (size_t) b->batCapacity << b->tshift;
     566             :         }
     567       28741 :         b->theap->base = NULL;
     568       28741 :         settailname(b->theap, filename, t, width);
     569       28741 :         b->theap->storage = STORE_INVALID;
     570       28741 :         b->theap->newstorage = STORE_INVALID;
     571       28741 :         b->theap->farmid = BBPselectfarm(PERSISTENT, b->ttype, offheap);
     572       28741 :         b->theap->dirty = false;
     573       28741 :         b->theap->parentid = b->batCacheid;
     574       28741 :         if (minpos < b->batCount)
     575       10423 :                 b->tminpos = (BUN) minpos;
     576             :         else
     577       18318 :                 b->tminpos = BUN_NONE;
     578       28741 :         if (maxpos < b->batCount)
     579       10482 :                 b->tmaxpos = (BUN) maxpos;
     580             :         else
     581       18259 :                 b->tmaxpos = BUN_NONE;
     582       28741 :         if (t && var) {
     583        7685 :                 t = vheapinit(b, buf + n, bbpversion, filename, lineno);
     584        7685 :                 if (t < 0)
     585             :                         return t;
     586        7685 :                 n += t;
     587             :         } else {
     588       21056 :                 b->tvheap = NULL;
     589             :         }
     590       28741 :         return n;
     591             : }
     592             : 
     593             : /* read a single line from the BBP.dir file (file pointer fp) and fill
     594             :  * in the structure pointed to by bn and extra information through the
     595             :  * other pointers; this function does not allocate any memory; return 0
     596             :  * on end of file, 1 on success, and -1 on failure */
     597             : /* set to true during initialization, else always false; if false, do
     598             :  * not return any options (set pointer to NULL as if there aren't any);
     599             :  * if true and there are options, return them in freshly allocated
     600             :  * memory through *options */
     601             : static bool return_options = false;
     602             : int
     603       29072 : BBPreadBBPline(FILE *fp, unsigned bbpversion, int *lineno, BAT *bn,
     604             : #ifdef GDKLIBRARY_HASHASH
     605             :                int *hashash,
     606             : #endif
     607             :                char *batname, char *filename, char **options)
     608             : {
     609       29072 :         char buf[4096];
     610       29072 :         uint64_t batid;
     611       29072 :         unsigned int properties;
     612       29072 :         int nread, n;
     613       29072 :         char *s;
     614       29072 :         uint64_t count, base = 0;
     615             : 
     616       29072 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
     617         331 :                 if (ferror(fp)) {
     618           0 :                         TRC_CRITICAL(GDK, "error reading BBP.dir on line %d\n", *lineno);
     619           0 :                         return -1;
     620             :                 }
     621             :                 return 0;       /* end of file */
     622             :         }
     623       28741 :         (*lineno)++;
     624       28741 :         if ((s = strpbrk(buf, "\r\n")) != NULL) {
     625       28741 :                 if (s[0] == '\r' && s[1] != '\n') {
     626           0 :                         TRC_CRITICAL(GDK, "invalid format for BBP.dir on line %d", *lineno);
     627           0 :                         return -1;
     628             :                 }
     629             :                 /* zap the newline */
     630       28741 :                 *s = '\0';
     631             :         } else {
     632           0 :                 TRC_CRITICAL(GDK, "invalid format for BBP.dir on line %d: line too long\n", *lineno);
     633           0 :                 return -1;
     634             :         }
     635             : 
     636       57482 :         if (bbpversion <= GDKLIBRARY_HSIZE ?
     637           0 :             sscanf(buf,
     638             :                    "%" SCNu64 " %*u %128s %*s %u %" SCNu64 " %*u %" SCNu64
     639             :                    "%n",
     640             :                    &batid, batname,
     641             :                    &properties, &count, &base,
     642             :                    &nread) < 5 :
     643             :             bbpversion <= GDKLIBRARY_STATUS ?
     644        2534 :             sscanf(buf,
     645             :                    "%" SCNu64 " %*u %128s %*s %u %" SCNu64 " %" SCNu64
     646             :                    "%n",
     647             :                    &batid, batname,
     648             :                    &properties, &count, &base,
     649             :                    &nread) < 5 :
     650       26207 :             sscanf(buf,
     651             :                    "%" SCNu64 " %128s %u %" SCNu64 " %" SCNu64
     652             :                    "%n",
     653             :                    &batid, batname,
     654             :                    &properties, &count, &base,
     655             :                    &nread) < 5) {
     656           0 :                 TRC_CRITICAL(GDK, "invalid format for BBP.dir on line %d", *lineno);
     657           0 :                 return -1;
     658             :         }
     659             : 
     660       28741 :         if (batid >= N_BBPINIT * BBPINIT) {
     661           0 :                 TRC_CRITICAL(GDK, "bat ID (%" PRIu64 ") too large to accommodate (max %d), on line %d.", batid, N_BBPINIT * BBPINIT - 1, *lineno);
     662           0 :                 return -1;
     663             :         }
     664             : 
     665       28741 :         BBPgetfilename(filename, sizeof(BBP_physical(0)), (bat) batid);
     666             : 
     667       28741 :         bn->batCacheid = (bat) batid;
     668       28741 :         bn->batTransient = false;
     669       28741 :         bn->batCopiedtodisk = true;
     670       28741 :         switch ((properties & 0x06) >> 1) {
     671        1040 :         case 0:
     672        1040 :                 bn->batRestricted = BAT_WRITE;
     673        1040 :                 break;
     674       27701 :         case 1:
     675       27701 :                 bn->batRestricted = BAT_READ;
     676       27701 :                 break;
     677           0 :         case 2:
     678           0 :                 bn->batRestricted = BAT_APPEND;
     679           0 :                 break;
     680           0 :         default:
     681           0 :                 TRC_CRITICAL(GDK, "incorrect batRestricted value");
     682           0 :                 return -1;
     683             :         }
     684       28741 :         bn->batCount = (BUN) count;
     685       28741 :         bn->batInserted = bn->batCount;
     686             :         /* set capacity to at least count */
     687       28741 :         bn->batCapacity = (BUN) count <= BATTINY ? BATTINY : (BUN) count;
     688             : 
     689       28741 :         if (base > (uint64_t) GDK_oid_max) {
     690           0 :                 TRC_CRITICAL(GDK, "head seqbase out of range (ID = %" PRIu64 ", seq = %" PRIu64 ") on line %d.", batid, base, *lineno);
     691           0 :                 return -1;
     692             :         }
     693       28741 :         bn->hseqbase = (oid) base;
     694       28741 :         n = heapinit(bn, buf + nread,
     695             : #ifdef GDKLIBRARY_HASHASH
     696             :                      hashash,
     697             : #endif
     698             :                      bbpversion, filename, *lineno);
     699       28741 :         if (n < 0) {
     700             :                 return -1;
     701             :         }
     702       28741 :         nread += n;
     703             : 
     704       28741 :         if (nread >= (int) sizeof(buf) || (buf[nread] != '\0' && buf[nread] != ' ')) {
     705           0 :                 TRC_CRITICAL(GDK, "invalid format for BBP.dir on line %d", *lineno);
     706           0 :                 return -1;
     707             :         }
     708       28741 :         if (options) {
     709       28741 :                 if (return_options && buf[nread] == ' ') {
     710           0 :                         if ((*options = GDKstrdup(buf + nread + 1)) == NULL) {
     711           0 :                                 TRC_CRITICAL(GDK, "GDKstrdup failed\n");
     712           0 :                                 return -1;
     713             :                         }
     714             :                 } else {
     715       28741 :                         *options = NULL;
     716             :                 }
     717             :         }
     718             :         return 1;
     719             : }
     720             : 
     721             : static gdk_return
     722         326 : BBPreadEntries(FILE *fp, unsigned bbpversion, int lineno
     723             : #ifdef GDKLIBRARY_HASHASH
     724             :                , bat **hashbats, bat *nhashbats
     725             : #endif
     726             :         )
     727             : {
     728             : #ifdef GDKLIBRARY_HASHASH
     729         326 :         bat *hbats = NULL;
     730         326 :         bat nhbats = 0;
     731             : #endif
     732             : 
     733             :         /* read the BBP.dir and insert the BATs into the BBP */
     734         326 :         return_options = true;
     735         326 :         MT_lock_set(&BBPnameLock);
     736       27481 :         for (;;) {
     737       27807 :                 BAT b;
     738       27807 :                 Heap h;
     739       27807 :                 Heap vh;
     740       27807 :                 vh = h = (Heap) {
     741             :                         .free = 0,
     742             :                 };
     743       27807 :                 b = (BAT) {
     744             :                         .theap = &h,
     745             :                         .tvheap = &vh,
     746             :                 };
     747       27807 :                 char *options;
     748       27807 :                 char headname[129];
     749       27807 :                 char filename[sizeof(BBP_physical(0))];
     750       27807 :                 char logical[1024];
     751             : #ifdef GDKLIBRARY_HASHASH
     752       27807 :                 int Thashash;
     753             : #endif
     754             : 
     755       27807 :                 switch (BBPreadBBPline(fp, bbpversion, &lineno, &b,
     756             : #ifdef GDKLIBRARY_HASHASH
     757             :                                        &Thashash,
     758             : #endif
     759             :                                        headname, filename, &options)) {
     760         326 :                 case 0:
     761             :                         /* end of file */
     762             : #ifdef GDKLIBRARY_HASHASH
     763         326 :                         *hashbats = hbats;
     764         326 :                         *nhashbats = nhbats;
     765             : #endif
     766         326 :                         return_options = false;
     767         326 :                         MT_lock_unset(&BBPnameLock);
     768         326 :                         return GDK_SUCCEED;
     769             :                 case 1:
     770             :                         /* successfully read an entry */
     771       27481 :                         break;
     772           0 :                 default:
     773             :                         /* error */
     774           0 :                         goto bailout;
     775             :                 }
     776             : 
     777       27481 :                 if (b.batCacheid >= N_BBPINIT * BBPINIT) {
     778           0 :                         GDKfree(options);
     779           0 :                         TRC_CRITICAL(GDK, "bat ID (%d) too large to accommodate (max %d), on line %d.", b.batCacheid, N_BBPINIT * BBPINIT - 1, lineno);
     780           0 :                         goto bailout;
     781             :                 }
     782             : 
     783       27481 :                 if (b.batCacheid >= (bat) ATOMIC_GET(&BBPsize)) {
     784           0 :                         if ((bat) ATOMIC_GET(&BBPsize) + 1 >= BBPlimit &&
     785           0 :                             BBPextend(b.batCacheid + 1) != GDK_SUCCEED) {
     786           0 :                                 GDKfree(options);
     787           0 :                                 goto bailout;
     788             :                         }
     789           0 :                         ATOMIC_SET(&BBPsize, b.batCacheid + 1);
     790             :                 }
     791       27481 :                 BAT *bn = BBP_desc(b.batCacheid);
     792       27481 :                 if (bn->batCacheid != 0) {
     793           0 :                         GDKfree(options);
     794           0 :                         TRC_CRITICAL(GDK, "duplicate entry in BBP.dir (ID = "
     795             :                                      "%d) on line %d.", b.batCacheid, lineno);
     796           0 :                         goto bailout;
     797             :                 }
     798             : 
     799             : #ifdef GDKLIBRARY_HASHASH
     800       27481 :                 if (Thashash) {
     801           0 :                         assert(bbpversion <= GDKLIBRARY_HASHASH);
     802           0 :                         bat *sb = GDKrealloc(hbats, ++nhbats * sizeof(bat));
     803           0 :                         if (sb == NULL) {
     804           0 :                                 GDKfree(options);
     805           0 :                                 goto bailout;
     806             :                         }
     807           0 :                         hbats = sb;
     808           0 :                         hbats[nhbats - 1] = b.batCacheid;
     809             :                 }
     810             : #endif
     811             : 
     812       27481 :                 Heap *hn;
     813       27481 :                 if ((hn = GDKmalloc(sizeof(Heap))) == NULL) {
     814           0 :                         GDKfree(options);
     815           0 :                         TRC_CRITICAL(GDK, "cannot allocate memory for BAT.");
     816           0 :                         goto bailout;
     817             :                 }
     818       27481 :                 *bn = b;
     819       27481 :                 *hn = h;
     820       27481 :                 bn->theap = hn;
     821       27481 :                 if (b.tvheap) {
     822        7345 :                         Heap *vhn;
     823        7345 :                         assert(b.tvheap == &vh);
     824        7345 :                         if ((vhn = GDKmalloc(sizeof(Heap))) == NULL) {
     825           0 :                                 GDKfree(hn);
     826           0 :                                 GDKfree(options);
     827           0 :                                 TRC_CRITICAL(GDK, "cannot allocate memory for BAT.");
     828           0 :                                 goto bailout;
     829             :                         }
     830        7345 :                         *vhn = vh;
     831        7345 :                         bn->tvheap = vhn;
     832        7345 :                         ATOMIC_INIT(&bn->tvheap->refs, 1);
     833             :                 }
     834             : 
     835       27481 :                 char name[MT_NAME_LEN];
     836       27481 :                 snprintf(name, sizeof(name), "heaplock%d", bn->batCacheid); /* fits */
     837       27481 :                 MT_lock_init(&bn->theaplock, name);
     838       27481 :                 snprintf(name, sizeof(name), "BATlock%d", bn->batCacheid); /* fits */
     839       27481 :                 MT_lock_init(&bn->batIdxLock, name);
     840       27481 :                 snprintf(name, sizeof(name), "hashlock%d", bn->batCacheid); /* fits */
     841       27481 :                 MT_rwlock_init(&bn->thashlock, name);
     842       27481 :                 ATOMIC_INIT(&bn->theap->refs, 1);
     843             : 
     844       27481 :                 if (snprintf(BBP_bak(b.batCacheid), sizeof(BBP_bak(b.batCacheid)), "tmp_%o", (unsigned) b.batCacheid) >= (int) sizeof(BBP_bak(b.batCacheid))) {
     845           0 :                         BATdestroy(bn);
     846           0 :                         GDKfree(options);
     847           0 :                         TRC_CRITICAL(GDK, "BBP logical filename directory is too large, on line %d\n", lineno);
     848           0 :                         goto bailout;
     849             :                 }
     850       27481 :                 char *s;
     851       27481 :                 if ((s = strchr(headname, '~')) != NULL && s == headname) {
     852             :                         /* sizeof(logical) > sizeof(BBP_bak(b.batCacheid)), so
     853             :                          * this fits */
     854           0 :                         strcpy(logical, BBP_bak(b.batCacheid));
     855             :                 } else {
     856           0 :                         if (s)
     857           0 :                                 *s = 0;
     858       27481 :                         strcpy_len(logical, headname, sizeof(logical));
     859             :                 }
     860       27481 :                 if (strcmp(logical, BBP_bak(b.batCacheid)) == 0) {
     861       26811 :                         BBP_logical(b.batCacheid) = BBP_bak(b.batCacheid);
     862             :                 } else {
     863         670 :                         BBP_logical(b.batCacheid) = GDKstrdup(logical);
     864         670 :                         if (BBP_logical(b.batCacheid) == NULL) {
     865           0 :                                 BATdestroy(bn);
     866           0 :                                 GDKfree(options);
     867           0 :                                 TRC_CRITICAL(GDK, "GDKstrdup failed\n");
     868           0 :                                 goto bailout;
     869             :                         }
     870             :                 }
     871       27481 :                 strcpy_len(BBP_physical(b.batCacheid), filename, sizeof(BBP_physical(b.batCacheid)));
     872             : #ifdef __COVERITY__
     873             :                 /* help coverity */
     874             :                 BBP_physical(b.batCacheid)[sizeof(BBP_physical(b.batCacheid)) - 1] = 0;
     875             : #endif
     876       27481 :                 BBP_options(b.batCacheid) = options;
     877       27481 :                 BBP_refs(b.batCacheid) = 0;
     878       27481 :                 BBP_lrefs(b.batCacheid) = 1;    /* any BAT we encounter here is persistent, so has a logical reference */
     879       27481 :                 BBP_pid(b.batCacheid) = 0;
     880       27481 :                 BBP_status_set(b.batCacheid, BBPEXISTING);
     881       27481 :                 if (BBPnamecheck(BBP_logical(b.batCacheid)) == 0)
     882         670 :                         BBP_insert(b.batCacheid);
     883             :         }
     884             : 
     885           0 :   bailout:
     886           0 :         MT_lock_unset(&BBPnameLock);
     887           0 :         return_options = false;
     888             : #ifdef GDKLIBRARY_HASHASH
     889           0 :         GDKfree(hbats);
     890             : #endif
     891           0 :         return GDK_FAIL;
     892             : }
     893             : 
     894             : /* check that the necessary files for all BATs exist and are large
     895             :  * enough */
     896             : static gdk_return
     897         327 : BBPcheckbats(unsigned bbpversion)
     898             : {
     899         327 :         (void) bbpversion;
     900       71278 :         for (bat bid = 1, size = (bat) ATOMIC_GET(&BBPsize); bid < size; bid++) {
     901       70951 :                 struct stat statb;
     902       70951 :                 BAT *b;
     903       70951 :                 char *path;
     904             : 
     905       70951 :                 b = BBP_desc(bid);
     906       70951 :                 if (b->batCacheid == 0 || b->ttype == TYPE_void) {
     907             :                         /* no files needed */
     908       43470 :                         continue;
     909             :                 }
     910       27481 :                 if (b->theap->free > 0) {
     911       18466 :                         path = GDKfilepath(0, BATDIR, b->theap->filename, NULL);
     912       18466 :                         if (path == NULL)
     913           0 :                                 return GDK_FAIL;
     914             :                         /* first check string offset heap with width,
     915             :                          * then without */
     916       18466 :                         if (MT_stat(path, &statb) < 0) {
     917           0 :                                 GDKsyserror("cannot stat file %s (expected size %zu)\n",
     918             :                                             path, b->theap->free);
     919           0 :                                 GDKfree(path);
     920           0 :                                 return GDK_FAIL;
     921             :                         }
     922       18466 :                         if ((size_t) statb.st_size < b->theap->free) {
     923           0 :                                 GDKerror("file %s too small (expected %zu, actual %zu)\n", path, b->theap->free, (size_t) statb.st_size);
     924           0 :                                 GDKfree(path);
     925           0 :                                 return GDK_FAIL;
     926             :                         }
     927       18466 :                         size_t hfree = b->theap->free;
     928       18466 :                         hfree = (hfree + GDK_mmap_pagesize - 1) & ~(GDK_mmap_pagesize - 1);
     929       18466 :                         if (hfree == 0)
     930           0 :                                 hfree = GDK_mmap_pagesize;
     931       18466 :                         if (statb.st_size > (off_t) hfree) {
     932           9 :                                 int fd;
     933           9 :                                 if ((fd = MT_open(path, O_RDWR | O_CLOEXEC | O_BINARY)) >= 0) {
     934           9 :                                         if (ftruncate(fd, hfree) == -1)
     935           0 :                                                 perror("ftruncate");
     936           9 :                                         (void) close(fd);
     937             :                                 }
     938             :                         }
     939       18466 :                         GDKfree(path);
     940             :                 }
     941       27481 :                 if (b->tvheap != NULL && b->tvheap->free > 0) {
     942        5352 :                         path = GDKfilepath(0, BATDIR, BBP_physical(b->batCacheid), "theap");
     943        5352 :                         if (path == NULL)
     944             :                                 return GDK_FAIL;
     945        5352 :                         if (MT_stat(path, &statb) < 0) {
     946           0 :                                 GDKsyserror("cannot stat file %s\n",
     947             :                                             path);
     948           0 :                                 GDKfree(path);
     949           0 :                                 return GDK_FAIL;
     950             :                         }
     951        5352 :                         if ((size_t) statb.st_size < b->tvheap->free) {
     952           0 :                                 GDKerror("file %s too small (expected %zu, actual %zu)\n", path, b->tvheap->free, (size_t) statb.st_size);
     953           0 :                                 GDKfree(path);
     954           0 :                                 return GDK_FAIL;
     955             :                         }
     956        5352 :                         size_t hfree = b->tvheap->free;
     957        5352 :                         hfree = (hfree + GDK_mmap_pagesize - 1) & ~(GDK_mmap_pagesize - 1);
     958        5352 :                         if (hfree == 0)
     959           0 :                                 hfree = GDK_mmap_pagesize;
     960        5352 :                         if (statb.st_size > (off_t) hfree) {
     961          10 :                                 int fd;
     962          10 :                                 if ((fd = MT_open(path, O_RDWR | O_CLOEXEC | O_BINARY)) >= 0) {
     963          10 :                                         if (ftruncate(fd, hfree) == -1)
     964           0 :                                                 perror("ftruncate");
     965          10 :                                         (void) close(fd);
     966             :                                 }
     967             :                         }
     968        5352 :                         GDKfree(path);
     969             :                 }
     970             :         }
     971             :         return GDK_SUCCEED;
     972             : }
     973             : 
     974             : #ifdef HAVE_HGE
     975             : #define SIZEOF_MAX_INT SIZEOF_HGE
     976             : #else
     977             : #define SIZEOF_MAX_INT SIZEOF_LNG
     978             : #endif
     979             : 
     980             : unsigned
     981         331 : BBPheader(FILE *fp, int *lineno, bat *bbpsize, lng *logno, bool allow_hge_upgrade)
     982             : {
     983         331 :         char buf[BUFSIZ];
     984         331 :         int sz, ptrsize, oidsize, intsize;
     985         331 :         unsigned bbpversion;
     986             : 
     987         331 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
     988           0 :                 TRC_CRITICAL(GDK, "BBP.dir is empty");
     989           0 :                 return 0;
     990             :         }
     991         331 :         ++*lineno;
     992         331 :         if (sscanf(buf, "BBP.dir, GDKversion %u\n", &bbpversion) != 1) {
     993           0 :                 GDKerror("old BBP without version number; "
     994             :                          "dump the database using a compatible version, "
     995             :                          "then restore into new database using this version.\n");
     996           0 :                 return 0;
     997             :         }
     998         331 :         if (bbpversion != GDKLIBRARY &&
     999             :             bbpversion != GDKLIBRARY_STATUS &&
    1000             :             bbpversion != GDKLIBRARY_JSON &&
    1001         331 :             bbpversion != GDKLIBRARY_HSIZE &&
    1002             :             bbpversion != GDKLIBRARY_HASHASH) {
    1003           0 :                 TRC_CRITICAL(GDK, "incompatible BBP version: expected 0%o, got 0%o. "
    1004             :                              "This database was probably created by a %s version of MonetDB.",
    1005             :                              GDKLIBRARY, bbpversion,
    1006             :                              bbpversion > GDKLIBRARY ? "newer" : "too old");
    1007           0 :                 return 0;
    1008             :         }
    1009         331 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
    1010           0 :                 TRC_CRITICAL(GDK, "short BBP");
    1011           0 :                 return 0;
    1012             :         }
    1013         331 :         ++*lineno;
    1014         331 :         if (sscanf(buf, "%d %d %d", &ptrsize, &oidsize, &intsize) != 3) {
    1015           0 :                 TRC_CRITICAL(GDK, "BBP.dir has incompatible format: pointer, OID, and max. integer sizes are missing on line %d", *lineno);
    1016           0 :                 return 0;
    1017             :         }
    1018         331 :         if (ptrsize != SIZEOF_SIZE_T || oidsize != SIZEOF_OID) {
    1019           0 :                 TRC_CRITICAL(GDK, "database created with incompatible server: "
    1020             :                              "expected pointer size %d, got %d, expected OID size %d, got %d.",
    1021             :                              SIZEOF_SIZE_T, ptrsize, SIZEOF_OID, oidsize);
    1022           0 :                 return 0;
    1023             :         }
    1024         331 :         if (intsize > SIZEOF_MAX_INT) {
    1025           0 :                 TRC_CRITICAL(GDK, "database created with incompatible server: "
    1026             :                              "expected max. integer size %d, got %d.",
    1027             :                              SIZEOF_MAX_INT, intsize);
    1028           0 :                 return 0;
    1029             :         }
    1030         331 :         if (intsize < SIZEOF_MAX_INT && !allow_hge_upgrade) {
    1031           0 :                 TRC_CRITICAL(GDK, "database created with incompatible server: "
    1032             :                              "expected max. integer size %d, got %d; "
    1033             :                              "use --set allow_hge_upgrade=yes to upgrade.",
    1034             :                              SIZEOF_MAX_INT, intsize);
    1035           0 :                 return 0;
    1036             :         }
    1037         331 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
    1038           0 :                 TRC_CRITICAL(GDK, "short BBP");
    1039           0 :                 return 0;
    1040             :         }
    1041         331 :         ++*lineno;
    1042         331 :         if (sscanf(buf, "BBPsize=%d", &sz) != 1) {
    1043           0 :                 TRC_CRITICAL(GDK, "no BBPsize value found\n");
    1044           0 :                 return 0;
    1045             :         }
    1046         331 :         if (sz > *bbpsize)
    1047         106 :                 *bbpsize = sz;
    1048         331 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
    1049           0 :                 TRC_CRITICAL(GDK, "short BBP");
    1050           0 :                 return 0;
    1051             :         }
    1052         662 :         if (bbpversion <= GDKLIBRARY_STATUS ?
    1053           8 :             sscanf(buf, "BBPinfo=" LLSCN " %*d", logno) != 1 :
    1054         323 :             sscanf(buf, "BBPinfo=" LLSCN, logno) != 1) {
    1055           0 :                 TRC_CRITICAL(GDK, "no info value found\n");
    1056           0 :                 return 0;
    1057             :         }
    1058         331 :         return bbpversion;
    1059             : }
    1060             : 
    1061             : bool
    1062    76277198 : GDKinmemory(int farmid)
    1063             : {
    1064    76277198 :         if (farmid == NOFARM)
    1065             :                 farmid = 0;
    1066    75037907 :         assert(farmid >= 0 && farmid < MAXFARMS);
    1067    76277198 :         return BBPfarms[farmid].dirname == NULL;
    1068             : }
    1069             : 
    1070             : /* all errors are fatal */
    1071             : gdk_return
    1072         975 : BBPaddfarm(const char *dirname, uint32_t rolemask, bool logerror)
    1073             : {
    1074         975 :         struct stat st;
    1075         975 :         int i;
    1076             : 
    1077         975 :         if (dirname == NULL) {
    1078           1 :                 assert(BBPfarms[0].dirname == NULL);
    1079           1 :                 assert(rolemask & 1);
    1080           1 :                 assert(BBPfarms[0].roles == 0);
    1081           1 :                 BBPfarms[0].roles = rolemask;
    1082           1 :                 return GDK_SUCCEED;
    1083             :         }
    1084         974 :         if (strchr(dirname, '\n') != NULL) {
    1085           0 :                 if (logerror)
    1086           0 :                         GDKerror("no newline allowed in directory name\n");
    1087           0 :                 return GDK_FAIL;
    1088             :         }
    1089         974 :         if (rolemask == 0 || (rolemask & 1 && BBPfarms[0].roles != 0)) {
    1090           0 :                 if (logerror)
    1091           0 :                         GDKerror("bad rolemask\n");
    1092           0 :                 return GDK_FAIL;
    1093             :         }
    1094         974 :         if (strcmp(dirname, "in-memory") == 0 ||
    1095         973 :             /* backward compatibility: */ strcmp(dirname, ":memory:") == 0) {
    1096             :                 dirname = NULL;
    1097         973 :         } else if (MT_mkdir(dirname) < 0) {
    1098         885 :                 if (errno == EEXIST) {
    1099         885 :                         if (MT_stat(dirname, &st) == -1 || !S_ISDIR(st.st_mode)) {
    1100           0 :                                 if (logerror)
    1101           0 :                                         GDKerror("%s: not a directory\n", dirname);
    1102           0 :                                 return GDK_FAIL;
    1103             :                         }
    1104             :                 } else {
    1105           0 :                         if (logerror)
    1106           0 :                                 GDKsyserror("%s: cannot create directory\n", dirname);
    1107           0 :                         return GDK_FAIL;
    1108             :                 }
    1109             :         }
    1110        1937 :         for (i = 0; i < MAXFARMS; i++) {
    1111        1937 :                 if (BBPfarms[i].roles == 0) {
    1112         974 :                         if (dirname) {
    1113         973 :                                 BBPfarms[i].dirname = GDKstrdup(dirname);
    1114         973 :                                 if (BBPfarms[i].dirname == NULL)
    1115             :                                         return GDK_FAIL;
    1116             :                         }
    1117         974 :                         BBPfarms[i].roles = rolemask;
    1118         974 :                         if ((rolemask & 1) == 0 && dirname != NULL) {
    1119             :                                 char *bbpdir;
    1120             :                                 int j;
    1121             : 
    1122         969 :                                 for (j = 0; j < i; j++)
    1123         807 :                                         if (BBPfarms[j].dirname != NULL &&
    1124         807 :                                             strcmp(BBPfarms[i].dirname,
    1125             :                                                    BBPfarms[j].dirname) == 0)
    1126             :                                                 return GDK_SUCCEED;
    1127             :                                 /* if an extra farm, make sure we
    1128             :                                  * don't find a BBP.dir there that
    1129             :                                  * might belong to an existing
    1130             :                                  * database */
    1131         162 :                                 bbpdir = GDKfilepath(i, BATDIR, "BBP", "dir");
    1132         162 :                                 if (bbpdir == NULL) {
    1133             :                                         return GDK_FAIL;
    1134             :                                 }
    1135         162 :                                 if (MT_stat(bbpdir, &st) != -1 || errno != ENOENT) {
    1136           0 :                                         GDKfree(bbpdir);
    1137           0 :                                         if (logerror)
    1138           0 :                                                 GDKerror("%s is a database\n", dirname);
    1139           0 :                                         return GDK_FAIL;
    1140             :                                 }
    1141         162 :                                 GDKfree(bbpdir);
    1142         162 :                                 bbpdir = GDKfilepath(i, BAKDIR, "BBP", "dir");
    1143         162 :                                 if (bbpdir == NULL) {
    1144             :                                         return GDK_FAIL;
    1145             :                                 }
    1146         162 :                                 if (MT_stat(bbpdir, &st) != -1 || errno != ENOENT) {
    1147           0 :                                         GDKfree(bbpdir);
    1148           0 :                                         if (logerror)
    1149           0 :                                                 GDKerror("%s is a database\n", dirname);
    1150           0 :                                         return GDK_FAIL;
    1151             :                                 }
    1152         162 :                                 GDKfree(bbpdir);
    1153             :                         }
    1154         491 :                         return GDK_SUCCEED;
    1155             :                 }
    1156             :         }
    1157           0 :         if (logerror)
    1158           0 :                 GDKerror("too many farms\n");
    1159             :         return GDK_FAIL;
    1160             : }
    1161             : 
    1162             : gdk_return
    1163         329 : BBPchkfarms(void)
    1164             : {
    1165         329 :         const char *dir = NULL;
    1166         329 :         uint32_t rolemask = 0;
    1167         329 :         if ((BBPfarms[0].roles & 1) == 0) {
    1168           0 :                 GDKerror("Must call BBPaddfarms at least once for persistent data\n");
    1169           0 :                 return GDK_FAIL;
    1170             :         }
    1171       10857 :         for (int i = 0; i < MAXFARMS; i++) {
    1172       10528 :                 if (BBPfarms[i].roles != 0) {
    1173         646 :                         dir = BBPfarms[i].dirname;
    1174         646 :                         rolemask |= BBPfarms[i].roles;
    1175             :                 }
    1176             :         }
    1177         329 :         if (dir == NULL)
    1178           1 :                 dir = "in-memory";
    1179         329 :         if ((rolemask & (1U << TRANSIENT)) == 0) {
    1180           0 :                 gdk_return rc = BBPaddfarm(dir, 1U << TRANSIENT, true);
    1181           0 :                 if (rc != GDK_SUCCEED)
    1182             :                         return rc;
    1183             :         }
    1184         329 :         if ((rolemask & (1U << SYSTRANS)) == 0) {
    1185         329 :                 gdk_return rc = BBPaddfarm(dir, 1U << SYSTRANS, true);
    1186         329 :                 if (rc != GDK_SUCCEED)
    1187             :                         return rc;
    1188             :         }
    1189             :         return GDK_SUCCEED;
    1190             : }
    1191             : 
    1192             : #ifdef GDKLIBRARY_HASHASH
    1193             : static gdk_return
    1194           0 : fixhashashbat(BAT *b)
    1195             : {
    1196           0 :         const char *nme = BBP_physical(b->batCacheid);
    1197           0 :         char *srcdir = GDKfilepath(NOFARM, BATDIR, nme, NULL);
    1198           0 :         if (srcdir == NULL) {
    1199           0 :                 TRC_CRITICAL(GDK, "GDKfilepath failed\n");
    1200           0 :                 return GDK_FAIL;
    1201             :         }
    1202           0 :         char *s;
    1203           0 :         if ((s = strrchr(srcdir, DIR_SEP)) != NULL)
    1204           0 :                 *s = 0;
    1205           0 :         const char *bnme;
    1206           0 :         if ((bnme = strrchr(nme, DIR_SEP)) != NULL)
    1207           0 :                 bnme++;
    1208             :         else
    1209             :                 bnme = nme;
    1210           0 :         long_str filename;
    1211           0 :         snprintf(filename, sizeof(filename), "BACKUP%c%s", DIR_SEP, bnme);
    1212             : 
    1213             :         /* we don't maintain index structures */
    1214           0 :         HASHdestroy(b);
    1215           0 :         IMPSdestroy(b);
    1216           0 :         OIDXdestroy(b);
    1217           0 :         PROPdestroy(b);
    1218           0 :         STRMPdestroy(b);
    1219           0 :         RTREEdestroy(b);
    1220             : 
    1221             :         /* make backup of heaps */
    1222           0 :         const char *t;
    1223           0 :         if (GDKmove(b->theap->farmid, srcdir, bnme, "tail1",
    1224             :                     BAKDIR, bnme, "tail1", false) == GDK_SUCCEED)
    1225             :                 t = "tail1";
    1226           0 :         else if (GDKmove(b->theap->farmid, srcdir, bnme, "tail2",
    1227             :                          BAKDIR, bnme, "tail2", false) == GDK_SUCCEED)
    1228             :                 t = "tail2";
    1229             : #if SIZEOF_VAR_T == 8
    1230           0 :         else if (GDKmove(b->theap->farmid, srcdir, bnme, "tail4",
    1231             :                          BAKDIR, bnme, "tail4", false) == GDK_SUCCEED)
    1232             :                 t = "tail4";
    1233             : #endif
    1234           0 :         else if (GDKmove(b->theap->farmid, srcdir, bnme, "tail",
    1235             :                          BAKDIR, bnme, "tail", true) == GDK_SUCCEED)
    1236             :                 t = "tail";
    1237             :         else {
    1238           0 :                 GDKfree(srcdir);
    1239           0 :                 TRC_CRITICAL(GDK, "cannot make backup of %s.tail\n", nme);
    1240           0 :                 return GDK_FAIL;
    1241             :         }
    1242           0 :         GDKclrerr();
    1243           0 :         if (GDKmove(b->theap->farmid, srcdir, bnme, "theap",
    1244             :                     BAKDIR, bnme, "theap", true) != GDK_SUCCEED) {
    1245           0 :                 GDKfree(srcdir);
    1246           0 :                 TRC_CRITICAL(GDK, "cannot make backup of %s.theap\n", nme);
    1247           0 :                 return GDK_FAIL;
    1248             :         }
    1249             :         /* load old heaps */
    1250           0 :         Heap h1 = *b->theap; /* old heap */
    1251           0 :         h1.base = NULL;
    1252           0 :         h1.dirty = false;
    1253           0 :         strconcat_len(h1.filename, sizeof(h1.filename), filename, ".", t, NULL);
    1254           0 :         if (HEAPload(&h1, filename, t, false) != GDK_SUCCEED) {
    1255           0 :                 GDKfree(srcdir);
    1256           0 :                 TRC_CRITICAL(GDK, "loading old tail heap "
    1257             :                              "for BAT %d failed\n", b->batCacheid);
    1258           0 :                 return GDK_FAIL;
    1259             :         }
    1260           0 :         Heap vh1 = *b->tvheap;       /* old heap */
    1261           0 :         vh1.base = NULL;
    1262           0 :         vh1.dirty = false;
    1263           0 :         strconcat_len(vh1.filename, sizeof(vh1.filename), filename, ".theap", NULL);
    1264           0 :         if (HEAPload(&vh1, filename, "theap", false) != GDK_SUCCEED) {
    1265           0 :                 GDKfree(srcdir);
    1266           0 :                 HEAPfree(&h1, false);
    1267           0 :                 TRC_CRITICAL(GDK, "loading old string heap "
    1268             :                              "for BAT %d failed\n", b->batCacheid);
    1269           0 :                 return GDK_FAIL;
    1270             :         }
    1271             : 
    1272             :         /* create new heaps */
    1273           0 :         Heap *h2 = GDKmalloc(sizeof(Heap));
    1274           0 :         Heap *vh2 = GDKmalloc(sizeof(Heap));
    1275           0 :         if (h2 == NULL || vh2 == NULL) {
    1276           0 :                 GDKfree(h2);
    1277           0 :                 GDKfree(vh2);
    1278           0 :                 GDKfree(srcdir);
    1279           0 :                 HEAPfree(&h1, false);
    1280           0 :                 HEAPfree(&vh1, false);
    1281           0 :                 TRC_CRITICAL(GDK, "allocating new heaps "
    1282             :                              "for BAT %d failed\n", b->batCacheid);
    1283           0 :                 return GDK_FAIL;
    1284             :         }
    1285           0 :         *h2 = *b->theap;
    1286           0 :         h2->base = NULL;
    1287           0 :         if (HEAPalloc(h2, b->batCapacity, b->twidth) != GDK_SUCCEED) {
    1288           0 :                 GDKfree(h2);
    1289           0 :                 GDKfree(vh2);
    1290           0 :                 GDKfree(srcdir);
    1291           0 :                 HEAPfree(&h1, false);
    1292           0 :                 HEAPfree(&vh1, false);
    1293           0 :                 TRC_CRITICAL(GDK, "allocating new tail heap "
    1294             :                              "for BAT %d failed\n", b->batCacheid);
    1295           0 :                 return GDK_FAIL;
    1296             :         }
    1297           0 :         h2->dirty = true;
    1298           0 :         h2->free = h1.free;
    1299             : 
    1300           0 :         *vh2 = *b->tvheap;
    1301           0 :         strconcat_len(vh2->filename, sizeof(vh2->filename), nme, ".theap", NULL);
    1302           0 :         strHeap(vh2, b->batCapacity);
    1303           0 :         if (vh2->base == NULL) {
    1304           0 :                 GDKfree(srcdir);
    1305           0 :                 HEAPfree(&h1, false);
    1306           0 :                 HEAPfree(&vh1, false);
    1307           0 :                 HEAPfree(h2, false);
    1308           0 :                 GDKfree(h2);
    1309           0 :                 GDKfree(vh2);
    1310           0 :                 TRC_CRITICAL(GDK, "allocating new string heap "
    1311             :                              "for BAT %d failed\n", b->batCacheid);
    1312           0 :                 return GDK_FAIL;
    1313             :         }
    1314           0 :         vh2->dirty = true;
    1315           0 :         ATOMIC_INIT(&h2->refs, 1);
    1316           0 :         ATOMIC_INIT(&vh2->refs, 1);
    1317           0 :         Heap *ovh = b->tvheap;
    1318           0 :         b->tvheap = vh2;
    1319           0 :         vh2 = NULL;             /* no longer needed */
    1320           0 :         for (BUN i = 0; i < b->batCount; i++) {
    1321           0 :                 var_t o;
    1322           0 :                 switch (b->twidth) {
    1323           0 :                 case 1:
    1324           0 :                         o = (var_t) ((uint8_t *) h1.base)[i] + GDK_VAROFFSET;
    1325           0 :                         break;
    1326           0 :                 case 2:
    1327           0 :                         o = (var_t) ((uint16_t *) h1.base)[i] + GDK_VAROFFSET;
    1328           0 :                         break;
    1329             : #if SIZEOF_VAR_T == 8
    1330           0 :                 case 4:
    1331           0 :                         o = (var_t) ((uint32_t *) h1.base)[i];
    1332           0 :                         break;
    1333             : #endif
    1334           0 :                 default:
    1335           0 :                         o = ((var_t *) h1.base)[i];
    1336           0 :                         break;
    1337             :                 }
    1338           0 :                 const char *s = vh1.base + o;
    1339           0 :                 var_t no = strPut(b, &o, s);
    1340           0 :                 if (no == 0) {
    1341           0 :                         HEAPfree(&h1, false);
    1342           0 :                         HEAPfree(&vh1, false);
    1343           0 :                         HEAPdecref(h2, false);
    1344           0 :                         HEAPdecref(b->tvheap, false);
    1345           0 :                         b->tvheap = ovh;
    1346           0 :                         GDKfree(srcdir);
    1347           0 :                         TRC_CRITICAL(GDK, "storing string value "
    1348             :                                      "for BAT %d failed\n", b->batCacheid);
    1349           0 :                         return GDK_FAIL;
    1350             :                 }
    1351           0 :                 assert(no >= GDK_VAROFFSET);
    1352           0 :                 switch (b->twidth) {
    1353           0 :                 case 1:
    1354           0 :                         no -= GDK_VAROFFSET;
    1355           0 :                         assert(no <= 0xFF);
    1356           0 :                         ((uint8_t *) h2->base)[i] = (uint8_t) no;
    1357           0 :                         break;
    1358           0 :                 case 2:
    1359           0 :                         no -= GDK_VAROFFSET;
    1360           0 :                         assert(no <= 0xFFFF);
    1361           0 :                         ((uint16_t *) h2->base)[i] = (uint16_t) no;
    1362           0 :                         break;
    1363             : #if SIZEOF_VAR_T == 8
    1364           0 :                 case 4:
    1365           0 :                         assert(no <= 0xFFFFFFFF);
    1366           0 :                         ((uint32_t *) h2->base)[i] = (uint32_t) no;
    1367           0 :                         break;
    1368             : #endif
    1369           0 :                 default:
    1370           0 :                         ((var_t *) h2->base)[i] = no;
    1371           0 :                         break;
    1372             :                 }
    1373             :         }
    1374             : 
    1375             :         /* cleanup */
    1376           0 :         HEAPfree(&h1, false);
    1377           0 :         HEAPfree(&vh1, false);
    1378           0 :         if (HEAPsave(h2, nme, BATtailname(b), true, h2->free, NULL) != GDK_SUCCEED) {
    1379           0 :                 HEAPdecref(h2, false);
    1380           0 :                 HEAPdecref(b->tvheap, false);
    1381           0 :                 b->tvheap = ovh;
    1382           0 :                 GDKfree(srcdir);
    1383           0 :                 TRC_CRITICAL(GDK, "saving heap failed\n");
    1384           0 :                 return GDK_FAIL;
    1385             :         }
    1386           0 :         if (HEAPsave(b->tvheap, nme, "theap", true, b->tvheap->free, &b->theaplock) != GDK_SUCCEED) {
    1387           0 :                 HEAPfree(b->tvheap, false);
    1388           0 :                 b->tvheap = ovh;
    1389           0 :                 GDKfree(srcdir);
    1390           0 :                 TRC_CRITICAL(GDK, "saving string heap failed\n");
    1391           0 :                 return GDK_FAIL;
    1392             :         }
    1393           0 :         HEAPdecref(b->theap, false);
    1394           0 :         b->theap = h2;
    1395           0 :         HEAPfree(h2, false);
    1396           0 :         HEAPdecref(ovh, false);
    1397           0 :         HEAPfree(b->tvheap, false);
    1398           0 :         GDKfree(srcdir);
    1399           0 :         return GDK_SUCCEED;
    1400             : }
    1401             : 
    1402             : static gdk_return
    1403           0 : fixhashash(bat *hashbats, bat nhashbats)
    1404             : {
    1405           0 :         for (bat i = 0; i < nhashbats; i++) {
    1406           0 :                 bat bid = hashbats[i];
    1407           0 :                 BAT *b = BBP_desc(bid);
    1408           0 :                 if (b->batCacheid == 0) {
    1409             :                         /* not a valid BAT (shouldn't happen) */
    1410           0 :                         continue;
    1411             :                 }
    1412           0 :                 if (fixhashashbat(b) != GDK_SUCCEED)
    1413             :                         return GDK_FAIL;
    1414             :         }
    1415             :         return GDK_SUCCEED;
    1416             : }
    1417             : #endif
    1418             : 
    1419             : #ifdef GDKLIBRARY_JSON
    1420             : static gdk_return
    1421           0 : jsonupgradebat(BAT *b, json_storage_conversion fixJSONStorage)
    1422             : {
    1423           0 :         const char *nme = BBP_physical(b->batCacheid);
    1424           0 :         char *srcdir = GDKfilepath(NOFARM, BATDIR, nme, NULL);
    1425             : 
    1426           0 :         if (srcdir == NULL) {
    1427           0 :                 TRC_CRITICAL(GDK, "GDKfilepath failed\n");
    1428           0 :                 return GDK_FAIL;
    1429             :         }
    1430             : 
    1431           0 :         char *s;
    1432           0 :         if ((s = strrchr(srcdir, DIR_SEP)) != NULL)
    1433           0 :                 *s = 0;
    1434           0 :         const char *bnme;
    1435           0 :         if ((bnme = strrchr(nme, DIR_SEP)) != NULL) {
    1436           0 :                 bnme++;
    1437             :         } else {
    1438             :                 bnme = nme;
    1439             :         }
    1440             : 
    1441           0 :         long_str filename;
    1442           0 :         snprintf(filename, sizeof(filename), "BACKUP%c%s", DIR_SEP, bnme);
    1443             : 
    1444             :         /* A json column should not normally have any index structures */
    1445           0 :         HASHdestroy(b);
    1446           0 :         IMPSdestroy(b);
    1447           0 :         OIDXdestroy(b);
    1448           0 :         PROPdestroy(b);
    1449           0 :         STRMPdestroy(b);
    1450           0 :         RTREEdestroy(b);
    1451             : 
    1452             :         /* backup the current heaps */
    1453           0 :         if (GDKmove(b->theap->farmid, srcdir, bnme, "tail",
    1454             :                     BAKDIR, bnme, "tail", false) != GDK_SUCCEED) {
    1455           0 :                 GDKfree(srcdir);
    1456           0 :                 TRC_CRITICAL(GDK, "cannot make backup of %s.tail\n", nme);
    1457           0 :                 return GDK_FAIL;
    1458             :         }
    1459           0 :         if (GDKmove(b->theap->farmid, srcdir, bnme, "theap",
    1460             :                     BAKDIR, bnme, "theap", true) != GDK_SUCCEED) {
    1461           0 :                 GDKfree(srcdir);
    1462           0 :                 TRC_CRITICAL(GDK, "cannot make backup of %s.theap\n", nme);
    1463           0 :                 return GDK_FAIL;
    1464             :         }
    1465             : 
    1466             :         /* load the old heaps */
    1467           0 :         Heap h1 = *b->theap;
    1468           0 :         h1.base = NULL;
    1469           0 :         h1.dirty = false;
    1470           0 :         strconcat_len(h1.filename, sizeof(h1.filename), filename, ".tail", NULL);
    1471           0 :         if (HEAPload(&h1, filename, "tail", false) != GDK_SUCCEED) {
    1472           0 :                 GDKfree(srcdir);
    1473           0 :                 TRC_CRITICAL(GDK, "loading old tail heap "
    1474             :                              "for BAT %d failed\n", b->batCacheid);
    1475           0 :                 return GDK_FAIL;
    1476             :         }
    1477             : 
    1478           0 :         Heap vh1 = *b->tvheap;
    1479           0 :         vh1.base = NULL;
    1480           0 :         vh1.dirty = false;
    1481           0 :         strconcat_len(vh1.filename, sizeof(vh1.filename), filename, ".theap", NULL);
    1482           0 :         if (HEAPload(&vh1, filename, "theap", false) != GDK_SUCCEED) {
    1483           0 :                 GDKfree(srcdir);
    1484           0 :                 HEAPfree(&h1, false);
    1485           0 :                 TRC_CRITICAL(GDK, "loading old string heap "
    1486             :                              "for BAT %d failed\n", b->batCacheid);
    1487           0 :                 return GDK_FAIL;
    1488             :         }
    1489             : 
    1490             :         /* create the new heaps */
    1491           0 :         Heap *h2 = GDKmalloc(sizeof(Heap));
    1492           0 :         Heap *vh2 = GDKmalloc(sizeof(Heap));
    1493           0 :         if (h2 == NULL || vh2 == NULL) {
    1494           0 :                 GDKfree(h2);
    1495           0 :                 GDKfree(vh2);
    1496           0 :                 GDKfree(srcdir);
    1497           0 :                 HEAPfree(&h1, false);
    1498           0 :                 HEAPfree(&vh1, false);
    1499           0 :                 TRC_CRITICAL(GDK, "allocating new heaps "
    1500             :                              "for BAT %d failed\n", b->batCacheid);
    1501           0 :                 return GDK_FAIL;
    1502             :         }
    1503           0 :         *h2 = *b->theap;
    1504           0 :         h2->base = NULL;
    1505           0 :         if (HEAPalloc(h2, b->batCapacity, b->twidth) != GDK_SUCCEED) {
    1506           0 :                 GDKfree(h2);
    1507           0 :                 GDKfree(vh2);
    1508           0 :                 GDKfree(srcdir);
    1509           0 :                 HEAPfree(&h1, false);
    1510           0 :                 HEAPfree(&vh1, false);
    1511           0 :                 TRC_CRITICAL(GDK, "allocating new tail heap "
    1512             :                              "for BAT %d failed\n", b->batCacheid);
    1513           0 :                 return GDK_FAIL;
    1514             : 
    1515             :         }
    1516           0 :         h2->dirty = true;
    1517           0 :         h2->free = h1.free;
    1518             : 
    1519           0 :         *vh2 = *b->tvheap;
    1520           0 :         strconcat_len(vh2->filename, sizeof(vh2->filename), nme, ".theap", NULL);
    1521           0 :         strHeap(vh2, b->batCapacity);
    1522           0 :         if (vh2->base == NULL) {
    1523           0 :                 GDKfree(srcdir);
    1524           0 :                 HEAPfree(&h1, false);
    1525           0 :                 HEAPfree(&vh1, false);
    1526           0 :                 HEAPfree(h2, false);
    1527           0 :                 GDKfree(h2);
    1528           0 :                 GDKfree(vh2);
    1529           0 :                 TRC_CRITICAL(GDK, "allocating new string heap "
    1530             :                              "for BAT %d failed\n", b->batCacheid);
    1531           0 :                 return GDK_FAIL;
    1532             :         }
    1533           0 :         vh2->dirty = true;
    1534           0 :         ATOMIC_INIT(&h2->refs, 1);
    1535           0 :         ATOMIC_INIT(&vh2->refs, 1);
    1536           0 :         Heap *ovh = b->tvheap;
    1537           0 :         b->tvheap = vh2;
    1538           0 :         vh2 = NULL;
    1539             : 
    1540           0 :         for (BUN i = 0; i < b->batCount; i++) {
    1541           0 :                 var_t o = ((var_t *) h1.base)[i];
    1542           0 :                 const char *s = vh1.base + o;
    1543           0 :                 char *ns;
    1544           0 :                 if (fixJSONStorage(&ns, &s) != GDK_SUCCEED) {
    1545           0 :                         GDKfree(srcdir);
    1546           0 :                         HEAPfree(&h1, false);
    1547           0 :                         HEAPfree(&vh1, false);
    1548           0 :                         HEAPdecref(h2, false);
    1549           0 :                         HEAPdecref(b->tvheap, false);
    1550           0 :                         b->tvheap = ovh;
    1551           0 :                         TRC_CRITICAL(GDK, "converting value "
    1552             :                                      "in BAT %d failed\n", b->batCacheid);
    1553           0 :                         return GDK_FAIL;
    1554             :                 }
    1555           0 :                 var_t no = strPut(b, &o, ns);
    1556           0 :                 GDKfree(ns);
    1557           0 :                 if (no == 0) {
    1558           0 :                         GDKfree(srcdir);
    1559           0 :                         HEAPfree(&h1, false);
    1560           0 :                         HEAPfree(&vh1, false);
    1561           0 :                         HEAPdecref(h2, false);
    1562           0 :                         HEAPdecref(b->tvheap, false);
    1563           0 :                         b->tvheap = ovh;
    1564           0 :                         TRC_CRITICAL(GDK, "storing new value "
    1565             :                                      "in BAT %d failed\n", b->batCacheid);
    1566           0 :                         return GDK_FAIL;
    1567             : 
    1568             :                 }
    1569           0 :                 ((var_t *)h2->base)[i] = no;
    1570             :         }
    1571             : 
    1572             :         /* cleanup */
    1573           0 :         HEAPfree(&h1, false);
    1574           0 :         HEAPfree(&vh1, false);
    1575           0 :         if (HEAPsave(h2, nme, BATtailname(b), true, h2->free, NULL) !=
    1576             :             GDK_SUCCEED) {
    1577           0 :                 HEAPdecref(h2, false);
    1578           0 :                 HEAPdecref(b->tvheap, false);
    1579           0 :                 b->tvheap = ovh;
    1580           0 :                 GDKfree(srcdir);
    1581           0 :                 TRC_CRITICAL(GDK, "saving heap failed\n");
    1582           0 :                 return GDK_FAIL;
    1583             :         }
    1584             : 
    1585           0 :         if (HEAPsave(b->tvheap, nme, "theap", true, b->tvheap->free,
    1586             :                      &b->theaplock) != GDK_SUCCEED) {
    1587           0 :                 HEAPfree(b->tvheap, false);
    1588           0 :                 b->tvheap = ovh;
    1589           0 :                 GDKfree(srcdir);
    1590           0 :                 TRC_CRITICAL(GDK, "saving string failed\n");
    1591           0 :                 return GDK_FAIL;
    1592             :         }
    1593             : 
    1594           0 :         HEAPdecref(b->theap, false);
    1595           0 :         b->theap = h2;
    1596           0 :         HEAPfree(h2, false);
    1597           0 :         HEAPdecref(ovh, false);
    1598           0 :         HEAPfree(b->tvheap, false);
    1599           0 :         GDKfree(srcdir);
    1600             : 
    1601           0 :         return GDK_SUCCEED;
    1602             : }
    1603             : 
    1604             : gdk_return
    1605           0 : BBPjson_upgrade(json_storage_conversion fixJSONStorage)
    1606             : {
    1607           0 :         bat bid;
    1608           0 :         int JSON_type = ATOMindex("json");
    1609           0 :         bat nbat = (bat) ATOMIC_GET(&BBPsize);
    1610           0 :         bat *upd = GDKmalloc(sizeof(bat) * (size_t) nbat);
    1611           0 :         int nupd = 0;
    1612             : 
    1613           0 :         if (upd == NULL) {
    1614           0 :                 TRC_CRITICAL(GDK, "could not create bat\n");
    1615           0 :                 return GDK_FAIL;
    1616             :         }
    1617           0 :         upd[nupd++] = 0;        /* first entry unused */
    1618             : 
    1619           0 :         BBPlock();
    1620             : 
    1621           0 :         for (bid = 1; bid < nbat; bid++) {
    1622           0 :                 BAT *b = BBP_desc(bid);
    1623           0 :                 if (b->batCacheid == 0) {
    1624             :                         /* not a valid BAT */
    1625           0 :                         continue;
    1626             :                 }
    1627             : 
    1628           0 :                 if (b->ttype < 0) {
    1629           0 :                         const char *nme;
    1630             : 
    1631           0 :                         nme = ATOMunknown_name(b->ttype);
    1632           0 :                         int tt = ATOMindex(nme);
    1633           0 :                         if (tt >= 0)
    1634           0 :                                 b->ttype = tt;
    1635           0 :                         if (strcmp(nme, "json") != 0)
    1636           0 :                                 continue;
    1637           0 :                 } else if (b->ttype != JSON_type) {
    1638           0 :                         continue;
    1639             :                 }
    1640           0 :                 fprintf(stderr, "Upgrading json bat %d\n", bid);
    1641           0 :                 if (jsonupgradebat(b, fixJSONStorage) != GDK_SUCCEED) {
    1642           0 :                         BBPunlock();
    1643           0 :                         GDKfree(upd);
    1644           0 :                         return GDK_FAIL;
    1645             :                 }
    1646           0 :                 upd[nupd++] = bid;
    1647             :         }
    1648           0 :         BBPunlock();
    1649           0 :         if (nupd > 1 &&
    1650           0 :             TMsubcommit_list(upd, NULL, nupd, -1) != GDK_SUCCEED) {
    1651           0 :                 TRC_CRITICAL(GDK, "failed to commit changes\n");
    1652           0 :                 GDKfree(upd);
    1653           0 :                 return GDK_FAIL;
    1654             :         }
    1655           0 :         GDKfree(upd);
    1656           0 :         return GDK_SUCCEED;
    1657             : }
    1658             : #endif
    1659             : 
    1660             : static bool
    1661          98 : BBPtrim(bool aggressive, bat nbat)
    1662             : {
    1663          98 :         int n = 0;
    1664          98 :         int waitctr = 0;
    1665          98 :         bool changed = false;
    1666          98 :         unsigned flag = BBPUNLOADING | BBPSYNCING | BBPSAVING;
    1667          98 :         if (!aggressive)
    1668          98 :                 flag |= BBPHOT;
    1669          98 :         lng t0 = GDKusec();
    1670      133518 :         for (bat bid = 1; bid < nbat && !GDKexiting(); bid++) {
    1671             :                 /* quick check to see if we might possibly have to do
    1672             :                  * work (includes free bats) */
    1673      133420 :                 if ((BBP_status(bid) & BBPLOADED) == 0)
    1674       35995 :                         continue;
    1675             :                 /* don't do this during a (sub)commit */
    1676       97425 :                 BBPtmlock();
    1677       97425 :                 MT_lock_set(&GDKswapLock(bid));
    1678       97425 :                 BAT *b = NULL;
    1679       97425 :                 bool swap = false;
    1680       97425 :                 if ((BBP_status(bid) & (flag | BBPLOADED)) == BBPLOADED &&
    1681       18721 :                     BBP_refs(bid) == 0 &&
    1682       18721 :                     BBP_lrefs(bid) != 0 &&
    1683       18721 :                     (b = BBP_desc(bid))->batCacheid != 0) {
    1684       18721 :                         MT_lock_set(&b->theaplock);
    1685       18721 :                         if (!BATshared(b) &&
    1686       18659 :                             !isVIEW(b) &&
    1687       18479 :                             (!BATdirty(b) ||
    1688           0 :                              (aggressive &&
    1689           0 :                               b->theap->storage == STORE_MMAP &&
    1690           0 :                               (b->tvheap == NULL ||
    1691           0 :                                b->tvheap->storage == STORE_MMAP)) ||
    1692       15055 :                              (b->batRole == PERSISTENT &&
    1693       14737 :                               BBP_lrefs(bid) <= 2))) {
    1694       12067 :                                 BBP_status_on(bid, BBPUNLOADING);
    1695       12067 :                                 swap = true;
    1696       20710 :                                 waitctr += BATdirty(b) ? 9 : 1;
    1697             :                         }
    1698       18721 :                         MT_lock_unset(&b->theaplock);
    1699             :                 }
    1700       97425 :                 MT_lock_unset(&GDKswapLock(bid));
    1701       97425 :                 if (swap) {
    1702       12067 :                         TRC_DEBUG(BAT_, "unload and free bat %d\n", bid);
    1703       12067 :                         if (BBPfree(b) != GDK_SUCCEED)
    1704           0 :                                 GDKerror("unload failed for bat %d", bid);
    1705       12067 :                         n++;
    1706       12067 :                         changed = true;
    1707             :                 }
    1708       97425 :                 BBPtmunlock();
    1709             :                 /* every once in a while, give others a chance */
    1710       97425 :                 if (++waitctr >= 1000) {
    1711         146 :                         waitctr = 0;
    1712         146 :                         MT_sleep_ms(2);
    1713             :                 }
    1714             :         }
    1715          98 :         if (n > 0)
    1716          37 :                 TRC_INFO(BAT_, "unloaded %d bats in "LLFMT" usec%s\n", n, GDKusec() - t0, aggressive ? " (also hot)" : "");
    1717          98 :         return changed;
    1718             : }
    1719             : 
    1720             : static void
    1721         326 : BBPmanager(void *dummy)
    1722             : {
    1723         326 :         (void) dummy;
    1724         326 :         bool changed = true;
    1725             : 
    1726         424 :         for (;;) {
    1727         424 :                 int n = 0;
    1728         424 :                 bat nbat = (bat) ATOMIC_GET(&BBPsize);
    1729         424 :                 MT_thread_setworking("clearing HOT bits");
    1730      322125 :                 for (bat bid = 1; bid < nbat; bid++) {
    1731      321701 :                         MT_lock_set(&GDKswapLock(bid));
    1732      321701 :                         if (BBP_refs(bid) == 0 && BBP_lrefs(bid) != 0) {
    1733      107014 :                                 n += (BBP_status(bid) & BBPHOT) != 0;
    1734      107014 :                                 BBP_status_off(bid, BBPHOT);
    1735             :                         }
    1736      321701 :                         MT_lock_unset(&GDKswapLock(bid));
    1737             :                 }
    1738         424 :                 TRC_DEBUG(BAT_, "cleared HOT bit from %d bats\n", n);
    1739         424 :                 size_t cur = GDKvm_cursize();
    1740         424 :                 MT_thread_setworking("sleeping");
    1741       18791 :                 for (int i = 0, n = changed && cur > GDK_vm_maxsize / 2 ? 1 : cur > GDK_vm_maxsize / 4 ? 10 : 100; i < n; i++) {
    1742       18270 :                         MT_sleep_ms(100);
    1743       18268 :                         if (GDKexiting())
    1744             :                                 return;
    1745             :                 }
    1746          98 :                 MT_thread_setworking("BBPtrim");
    1747          98 :                 changed = BBPtrim(false, nbat);
    1748          98 :                 MT_thread_setworking("BBPcallbacks");
    1749          98 :                 BBPcallbacks();
    1750          98 :                 if (GDKexiting())
    1751             :                         return;
    1752             :         }
    1753             : }
    1754             : 
    1755             : static MT_Id manager;
    1756             : 
    1757             : gdk_return
    1758         327 : BBPinit(bool allow_hge_upgrade)
    1759             : {
    1760         327 :         FILE *fp = NULL;
    1761         327 :         struct stat st;
    1762         327 :         unsigned bbpversion = 0;
    1763         327 :         int i;
    1764         327 :         int lineno = 0;
    1765             : #ifdef GDKLIBRARY_HASHASH
    1766         327 :         bat *hashbats = NULL;
    1767         327 :         bat nhashbats = 0;
    1768         327 :         gdk_return res = GDK_SUCCEED;
    1769             : #endif
    1770         327 :         ATOMIC_BASE_TYPE dbg = ATOMIC_GET(&GDKdebug);
    1771             : 
    1772         327 :         ATOMIC_AND(&GDKdebug, ~TAILCHKMASK);
    1773             : 
    1774             :         /* the maximum number of BATs allowed in the system and the
    1775             :          * size of the "physical" array are linked in a complicated
    1776             :          * manner.  The expression below shows the relationship */
    1777         327 :         static_assert((uint64_t) N_BBPINIT * BBPINIT < (UINT64_C(1) << (3 * ((sizeof(BBP[0][0].physical) + 2) * 2 / 5))), "\"physical\" array in BBPrec is too small");
    1778             :         /* similarly, the maximum number of BATs allowed also has a
    1779             :          * (somewhat simpler) relation with the size of the "bak"
    1780             :          * array */
    1781         327 :         static_assert((uint64_t) N_BBPINIT * BBPINIT < (UINT64_C(1) << (3 * (sizeof(BBP[0][0].bak) - 5))), "\"bak\" array in BBPrec is too small");
    1782             : 
    1783         327 :         if (!GDKinmemory(0)) {
    1784         326 :                 str bbpdirstr, backupbbpdirstr;
    1785             : 
    1786         326 :                 BBPtmlock();
    1787             : 
    1788         326 :                 if ((bbpdirstr = GDKfilepath(0, BATDIR, "BBP", "dir")) == NULL) {
    1789           0 :                         TRC_CRITICAL(GDK, "GDKmalloc failed\n");
    1790           0 :                         BBPtmunlock();
    1791           0 :                         ATOMIC_SET(&GDKdebug, dbg);
    1792           0 :                         return GDK_FAIL;
    1793             :                 }
    1794             : 
    1795         326 :                 if ((backupbbpdirstr = GDKfilepath(0, BAKDIR, "BBP", "dir")) == NULL) {
    1796           0 :                         GDKfree(bbpdirstr);
    1797           0 :                         TRC_CRITICAL(GDK, "GDKmalloc failed\n");
    1798           0 :                         BBPtmunlock();
    1799           0 :                         ATOMIC_SET(&GDKdebug, dbg);
    1800           0 :                         return GDK_FAIL;
    1801             :                 }
    1802             : 
    1803         326 :                 if (GDKremovedir(0, TEMPDIR) != GDK_SUCCEED) {
    1804           0 :                         GDKfree(bbpdirstr);
    1805           0 :                         GDKfree(backupbbpdirstr);
    1806           0 :                         TRC_CRITICAL(GDK, "cannot remove directory %s\n", TEMPDIR);
    1807           0 :                         BBPtmunlock();
    1808           0 :                         ATOMIC_SET(&GDKdebug, dbg);
    1809           0 :                         return GDK_FAIL;
    1810             :                 }
    1811             : 
    1812         326 :                 if (GDKremovedir(0, DELDIR) != GDK_SUCCEED) {
    1813           0 :                         GDKfree(bbpdirstr);
    1814           0 :                         GDKfree(backupbbpdirstr);
    1815           0 :                         TRC_CRITICAL(GDK, "cannot remove directory %s\n", DELDIR);
    1816           0 :                         BBPtmunlock();
    1817           0 :                         ATOMIC_SET(&GDKdebug, dbg);
    1818           0 :                         return GDK_FAIL;
    1819             :                 }
    1820             : 
    1821             :                 /* first move everything from SUBDIR to BAKDIR (its parent) */
    1822         326 :                 if (BBPrecover_subdir() != GDK_SUCCEED) {
    1823           0 :                         GDKfree(bbpdirstr);
    1824           0 :                         GDKfree(backupbbpdirstr);
    1825           0 :                         TRC_CRITICAL(GDK, "cannot properly recover_subdir process %s.", SUBDIR);
    1826           0 :                         BBPtmunlock();
    1827           0 :                         ATOMIC_SET(&GDKdebug, dbg);
    1828           0 :                         return GDK_FAIL;
    1829             :                 }
    1830             : 
    1831             :                 /* try to obtain a BBP.dir from bakdir */
    1832         326 :                 if (MT_stat(backupbbpdirstr, &st) == 0) {
    1833             :                         /* backup exists; *must* use it */
    1834         101 :                         if (recover_dir(0, MT_stat(bbpdirstr, &st) == 0) != GDK_SUCCEED) {
    1835           0 :                                 GDKfree(bbpdirstr);
    1836           0 :                                 GDKfree(backupbbpdirstr);
    1837           0 :                                 BBPtmunlock();
    1838           0 :                                 goto bailout;
    1839             :                         }
    1840         101 :                         if ((fp = GDKfilelocate(0, "BBP", "r", "dir")) == NULL) {
    1841           0 :                                 GDKfree(bbpdirstr);
    1842           0 :                                 GDKfree(backupbbpdirstr);
    1843           0 :                                 TRC_CRITICAL(GDK, "cannot open recovered BBP.dir.");
    1844           0 :                                 BBPtmunlock();
    1845           0 :                                 ATOMIC_SET(&GDKdebug, dbg);
    1846           0 :                                 return GDK_FAIL;
    1847             :                         }
    1848         225 :                 } else if ((fp = GDKfilelocate(0, "BBP", "r", "dir")) == NULL) {
    1849             :                         /* there was no BBP.dir either. Panic! try to use a
    1850             :                          * BBP.bak */
    1851         225 :                         if (MT_stat(backupbbpdirstr, &st) < 0) {
    1852             :                                 /* no BBP.bak (nor BBP.dir or BACKUP/BBP.dir):
    1853             :                                  * create a new one */
    1854         225 :                                 TRC_DEBUG(IO_, "initializing BBP.\n");
    1855         225 :                                 if (BBPdir_init() != GDK_SUCCEED) {
    1856           0 :                                         GDKfree(bbpdirstr);
    1857           0 :                                         GDKfree(backupbbpdirstr);
    1858           0 :                                         BBPtmunlock();
    1859           0 :                                         goto bailout;
    1860             :                                 }
    1861           0 :                         } else if (GDKmove(0, BATDIR, "BBP", "bak", BATDIR, "BBP", "dir", true) == GDK_SUCCEED)
    1862           0 :                                 TRC_DEBUG(IO_, "reverting to dir saved in BBP.bak.\n");
    1863             : 
    1864         225 :                         if ((fp = GDKfilelocate(0, "BBP", "r", "dir")) == NULL) {
    1865           0 :                                 GDKsyserror("cannot open BBP.dir");
    1866           0 :                                 GDKfree(bbpdirstr);
    1867           0 :                                 GDKfree(backupbbpdirstr);
    1868           0 :                                 BBPtmunlock();
    1869           0 :                                 goto bailout;
    1870             :                         }
    1871             :                 }
    1872             :                 assert(fp != NULL);
    1873         326 :                 GDKfree(bbpdirstr);
    1874         326 :                 GDKfree(backupbbpdirstr);
    1875         326 :                 BBPtmunlock();
    1876             :         }
    1877             : 
    1878             :         /* scan the BBP.dir to obtain current size */
    1879         327 :         BBPlimit = BBPINIT;
    1880         327 :         memset(BBP0, 0, sizeof(BBP0));
    1881         327 :         memset(BBP, 0, sizeof(BBP));
    1882         327 :         BBP[0] = BBP0;
    1883             : 
    1884         327 :         bat bbpsize;
    1885         327 :         bbpsize = 1;
    1886         327 :         if (GDKinmemory(0)) {
    1887             :                 bbpversion = GDKLIBRARY;
    1888             :         } else {
    1889         326 :                 lng logno;
    1890         326 :                 bbpversion = BBPheader(fp, &lineno, &bbpsize, &logno, allow_hge_upgrade);
    1891         326 :                 if (bbpversion == 0) {
    1892           0 :                         ATOMIC_SET(&GDKdebug, dbg);
    1893           0 :                         return GDK_FAIL;
    1894             :                 }
    1895         326 :                 ATOMIC_SET(&BBPlogno, logno);
    1896             :         }
    1897             : 
    1898             :         /* allocate BBP records */
    1899         327 :         if (BBPextend(bbpsize) != GDK_SUCCEED) {
    1900           0 :                 ATOMIC_SET(&GDKdebug, dbg);
    1901           0 :                 return GDK_FAIL;
    1902             :         }
    1903         327 :         ATOMIC_SET(&BBPsize, bbpsize);
    1904             : 
    1905         327 :         if (!GDKinmemory(0)) {
    1906         326 :                 if (BBPreadEntries(fp, bbpversion, lineno
    1907             : #ifdef GDKLIBRARY_HASHASH
    1908             :                                    , &hashbats, &nhashbats
    1909             : #endif
    1910             :                             ) != GDK_SUCCEED) {
    1911           0 :                         ATOMIC_SET(&GDKdebug, dbg);
    1912           0 :                         return GDK_FAIL;
    1913             :                 }
    1914         326 :                 fclose(fp);
    1915             :         }
    1916             : 
    1917             :         /* remove trailing free bats from potential free list (they will
    1918             :          * get added when needed) */
    1919       16227 :         for (bat i = (bat) ATOMIC_GET(&BBPsize) - 1; i > 0; i--) {
    1920       16001 :                 if (BBP_desc(i)->batCacheid != 0)
    1921             :                         break;
    1922       15900 :                 bbpsize--;
    1923             :         }
    1924         327 :         ATOMIC_SET(&BBPsize, bbpsize);
    1925             : 
    1926             :         /* add free bats to free list in such a way that low numbered
    1927             :          * ones are at the head of the list */
    1928       71278 :         for (bat i = (bat) ATOMIC_GET(&BBPsize) - 1; i > 0; i--) {
    1929       70951 :                 if (BBP_desc(i)->batCacheid == 0) {
    1930       43470 :                         BBP_next(i) = BBP_free;
    1931       43470 :                         BBP_free = i;
    1932       43470 :                         BBP_nfree++;
    1933             :                 }
    1934             :         }
    1935             : 
    1936             :         /* will call BBPrecover if needed */
    1937         327 :         if (!GDKinmemory(0)) {
    1938         326 :                 BBPtmlock();
    1939         326 :                 gdk_return rc = BBPprepare(false);
    1940         326 :                 BBPtmunlock();
    1941         326 :                 if (rc != GDK_SUCCEED) {
    1942             : #ifdef GDKLIBRARY_HASHASH
    1943           0 :                         GDKfree(hashbats);
    1944             : #endif
    1945           0 :                         TRC_CRITICAL(GDK, "cannot properly prepare process %s.", BAKDIR);
    1946           0 :                         ATOMIC_SET(&GDKdebug, dbg);
    1947           0 :                         return rc;
    1948             :                 }
    1949             :         }
    1950             : 
    1951         327 :         if (BBPcheckbats(bbpversion) != GDK_SUCCEED) {
    1952             : #ifdef GDKLIBRARY_HASHASH
    1953           0 :                 GDKfree(hashbats);
    1954             : #endif
    1955           0 :                 ATOMIC_SET(&GDKdebug, dbg);
    1956           0 :                 return GDK_FAIL;
    1957             :         }
    1958             : 
    1959             : #ifdef GDKLIBRARY_HASHASH
    1960         327 :         if (nhashbats > 0)
    1961           0 :                 res = fixhashash(hashbats, nhashbats);
    1962         327 :         GDKfree(hashbats);
    1963         327 :         if (res != GDK_SUCCEED)
    1964             :                 return res;
    1965             : #endif
    1966             : 
    1967             : #ifdef GDKLIBRARY_JSON
    1968         327 :         if (bbpversion <= GDKLIBRARY_JSON) {
    1969           0 :                 char *jsonupgradestr;
    1970           0 :                 if (GDKinmemory(0)) {
    1971         327 :                         jsonupgradestr = NULL;
    1972             :                 } else {
    1973           0 :                         if ((jsonupgradestr = GDKfilepath(0, BATDIR, "jsonupgradeneeded", NULL)) == NULL) {
    1974           0 :                                 TRC_CRITICAL(GDK, "GDKfilepath failed\n");
    1975           0 :                                 ATOMIC_SET(&GDKdebug, dbg);
    1976           0 :                                 return GDK_FAIL;
    1977             :                         }
    1978             : 
    1979             :                         /* create signal file that we need to upgrade
    1980             :                          * stored json strings. This will be performed
    1981             :                          * by an upgrade function in the GDK that will
    1982             :                          * be called at the end of the json module
    1983             :                          * initialization with a callback that actually
    1984             :                          * knows how to perform the upgrade. */
    1985           0 :                         int fd = MT_open(jsonupgradestr, O_WRONLY | O_CREAT);
    1986           0 :                         GDKfree(jsonupgradestr);
    1987           0 :                         if (fd < 0) {
    1988           0 :                                 TRC_CRITICAL(GDK, "cannot create signal file jsonupgradeneeded");
    1989           0 :                                 ATOMIC_SET(&GDKdebug, dbg);
    1990           0 :                                 return GDK_FAIL;
    1991             :                         }
    1992             : 
    1993           0 :                         close(fd);
    1994             :                 }
    1995             :         }
    1996             : #endif
    1997             : 
    1998         327 :         if (bbpversion < GDKLIBRARY && TMcommit() != GDK_SUCCEED) {
    1999           0 :                 TRC_CRITICAL(GDK, "TMcommit failed\n");
    2000           0 :                 ATOMIC_SET(&GDKdebug, dbg);
    2001           0 :                 return GDK_FAIL;
    2002             :         }
    2003             : 
    2004         327 :         ATOMIC_SET(&GDKdebug, dbg);
    2005             : 
    2006             :         /* cleanup any leftovers (must be done after BBPrecover) */
    2007        1294 :         for (i = 0; i < MAXFARMS && BBPfarms[i].dirname != NULL; i++) {
    2008             :                 int j;
    2009        1291 :                 for (j = 0; j < i; j++) {
    2010             :                         /* don't clean a directory twice */
    2011         803 :                         if (BBPfarms[j].dirname &&
    2012         803 :                             strcmp(BBPfarms[i].dirname,
    2013             :                                    BBPfarms[j].dirname) == 0)
    2014             :                                 break;
    2015             :                 }
    2016         967 :                 if (j == i) {
    2017         488 :                         char *d = GDKfilepath(i, NULL, BATDIR, NULL);
    2018         488 :                         if (d == NULL) {
    2019             :                                 return GDK_FAIL;
    2020             :                         }
    2021         488 :                         BBPdiskscan(d, strlen(d) - strlen(BATDIR));
    2022         488 :                         GDKfree(d);
    2023             :                 }
    2024             :         }
    2025             : 
    2026         327 :         if (!GDKinmemory(0) && MT_create_thread(&manager, BBPmanager, NULL, MT_THR_DETACHED, "BBPmanager") < 0) {
    2027           0 :                 TRC_CRITICAL(GDK, "Could not start BBPmanager thread.");
    2028           0 :                 return GDK_FAIL;
    2029             :         }
    2030             :         return GDK_SUCCEED;
    2031             : 
    2032           0 :   bailout:
    2033             :         /* now it is time for real panic */
    2034           0 :         TRC_CRITICAL(GDK, "could not write %s%cBBP.dir.", BATDIR, DIR_SEP);
    2035           0 :         return GDK_FAIL;
    2036             : }
    2037             : 
    2038             : /*
    2039             :  * During the exit phase all non-persistent BATs are removed.  Upon
    2040             :  * exit the status of the BBP tables is saved on disk.  This function
    2041             :  * is called once and during the shutdown of the server. Since
    2042             :  * shutdown may be issued from any thread (dangerous) it may lead to
    2043             :  * interference in a parallel session.
    2044             :  */
    2045             : 
    2046             : static int backup_files = 0, backup_dir = 0, backup_subdir = 0;
    2047             : static char *lockfile = NULL;
    2048             : 
    2049             : void
    2050         325 : BBPexit(void)
    2051             : {
    2052         325 :         bat i;
    2053         325 :         bool skipped;
    2054             : 
    2055             :         //BBPlock();    /* stop all threads ever touching more descriptors */
    2056             : 
    2057             :         /* free all memory (just for leak-checking in Purify) */
    2058         325 :         do {
    2059         325 :                 skipped = false;
    2060      459063 :                 for (i = 0; i < (bat) ATOMIC_GET(&BBPsize); i++) {
    2061      458738 :                         if (BBPvalid(i)) {
    2062      333442 :                                 BAT *b = BBP_desc(i);
    2063             : 
    2064      333442 :                                 if (b->batCacheid != 0) {
    2065      333442 :                                         if (BATshared(b)) {
    2066           0 :                                                 skipped = true;
    2067           0 :                                                 continue;
    2068             :                                         }
    2069      333442 :                                         MT_lock_set(&b->theaplock);
    2070      333442 :                                         bat tp = VIEWtparent(b);
    2071           0 :                                         if (tp != 0) {
    2072           0 :                                                 --BBP_lrefs(tp);
    2073           0 :                                                 HEAPdecref(b->theap, false);
    2074           0 :                                                 b->theap = NULL;
    2075             :                                         }
    2076      333442 :                                         tp = VIEWvtparent(b);
    2077           0 :                                         if (tp != 0) {
    2078           0 :                                                 --BBP_lrefs(tp);
    2079           0 :                                                 HEAPdecref(b->tvheap, false);
    2080           0 :                                                 b->tvheap = NULL;
    2081             :                                         }
    2082      333442 :                                         if (b->oldtail) {
    2083           8 :                                                 Heap *h = b->oldtail;
    2084           8 :                                                 b->oldtail = NULL;
    2085           8 :                                                 ATOMIC_AND(&h->refs, ~DELAYEDREMOVE);
    2086           8 :                                                 HEAPdecref(h, false);
    2087             :                                         }
    2088      333442 :                                         PROPdestroy_nolock(b);
    2089      333442 :                                         MT_lock_unset(&b->theaplock);
    2090      333442 :                                         BATfree(b);
    2091             :                                 }
    2092      333442 :                                 BBP_pid(i) = 0;
    2093      333442 :                                 BBPuncacheit(i, true);
    2094      333442 :                                 if (BBP_logical(i) != BBP_bak(i))
    2095       11644 :                                         GDKfree(BBP_logical(i));
    2096      333442 :                                 BBP_logical(i) = NULL;
    2097             :                         }
    2098             :                 }
    2099         325 :         } while (skipped);
    2100             :         /* these need to be NULL, otherwise no new ones get created */
    2101         325 :         memset(BBP_hash, 0, sizeof(BBP_hash));
    2102         325 :         backup_files = 0;
    2103         325 :         backup_dir = 0;
    2104         325 :         backup_subdir = 0;
    2105         325 :         if (lockfile) {
    2106         324 :                 GDKfree(lockfile);
    2107         324 :                 lockfile = NULL;
    2108             :         }
    2109         325 : }
    2110             : 
    2111             : /*
    2112             :  * The routine BBPdir creates the BAT pool dictionary file.  It
    2113             :  * includes some information about the current state of affair in the
    2114             :  * pool.  The location in the buffer pool is saved for later use as
    2115             :  * well.  This is merely done for ease of debugging and of no
    2116             :  * importance to front-ends.  The tail of non-used entries is
    2117             :  * reclaimed as well.
    2118             :  */
    2119             : static inline int
    2120     1771461 : heap_entry(FILE *fp, BATiter *bi, BUN size)
    2121             : {
    2122     1771461 :         size_t free = bi->hfree;
    2123     1771461 :         if (size < BUN_NONE) {
    2124     1771461 :                 if ((bi->type >= 0 && ATOMstorage(bi->type) == TYPE_msk))
    2125      279247 :                         free = ((size + 31) / 32) * 4;
    2126     1492214 :                 else if (bi->width > 0)
    2127     1492214 :                         free = size << bi->shift;
    2128             :                 else
    2129             :                         free = 0;
    2130             :         }
    2131             : 
    2132     5653211 :         return fprintf(fp, " %s %d %d %d " BUNFMT " " BUNFMT " " BUNFMT " "
    2133             :                        BUNFMT " " OIDFMT " %zu %" PRIu64" %" PRIu64,
    2134     1771461 :                        bi->type >= 0 ? BATatoms[bi->type].name : ATOMunknown_name(bi->type),
    2135     1771461 :                        bi->width,
    2136     1771461 :                        bi->type == TYPE_void || bi->vh != NULL,
    2137     1771461 :                        (unsigned short) bi->sorted |
    2138     1771461 :                            ((unsigned short) bi->revsorted << 7) |
    2139     3542922 :                            ((unsigned short) bi->key << 8) |
    2140     1771461 :                            ((unsigned short) BATtdensebi(bi) << 9) |
    2141     1771461 :                            ((unsigned short) bi->nonil << 10) |
    2142     1771461 :                            ((unsigned short) bi->nil << 11) |
    2143     1771461 :                            ((unsigned short) bi->ascii << 12),
    2144     1075240 :                        bi->nokey[0] >= size || bi->nokey[1] >= size ? 0 : bi->nokey[0],
    2145     1771461 :                        bi->nokey[0] >= size || bi->nokey[1] >= size ? 0 : bi->nokey[1],
    2146     1771461 :                        bi->nosorted >= size ? 0 : bi->nosorted,
    2147     1771461 :                        bi->norevsorted >= size ? 0 : bi->norevsorted,
    2148             :                        bi->tseq,
    2149             :                        free,
    2150     1771461 :                        bi->minpos < size ? (uint64_t) bi->minpos : (uint64_t) oid_nil,
    2151     1771461 :                        bi->maxpos < size ? (uint64_t) bi->maxpos : (uint64_t) oid_nil);
    2152             : }
    2153             : 
    2154             : static inline int
    2155     1771461 : vheap_entry(FILE *fp, BATiter *bi, BUN size)
    2156             : {
    2157     1771461 :         (void) size;
    2158     1771461 :         if (bi->vh == NULL)
    2159             :                 return 0;
    2160      408470 :         return fprintf(fp, " %zu", size == 0 ? 0 : bi->vhfree);
    2161             : }
    2162             : 
    2163             : static gdk_return
    2164     1771461 : new_bbpentry(FILE *fp, bat i, BUN size, BATiter *bi)
    2165             : {
    2166             : #ifndef NDEBUG
    2167     1771461 :         assert(i > 0);
    2168     1771461 :         assert(i < (bat) ATOMIC_GET(&BBPsize));
    2169     1771461 :         assert(bi->b);
    2170     1771461 :         assert(bi->b->batCacheid == i);
    2171     1771461 :         assert(bi->b->batRole == PERSISTENT);
    2172     1771461 :         assert(0 <= bi->h->farmid && bi->h->farmid < MAXFARMS);
    2173     1771461 :         assert(BBPfarms[bi->h->farmid].roles & (1U << PERSISTENT));
    2174     1771461 :         if (bi->vh) {
    2175      408470 :                 assert(0 <= bi->vh->farmid && bi->vh->farmid < MAXFARMS);
    2176      408470 :                 assert(BBPfarms[bi->vh->farmid].roles & (1U << PERSISTENT));
    2177             :         }
    2178     1771461 :         assert(size <= bi->count || size == BUN_NONE);
    2179     1771461 :         assert(BBP_options(i) == NULL || strpbrk(BBP_options(i), "\r\n") == NULL);
    2180             : #endif
    2181             : 
    2182     1771461 :         if (BBP_options(i) != NULL && strpbrk(BBP_options(i), "\r\n") != NULL) {
    2183           0 :                 GDKerror("options for bat %d contains a newline\n", i);
    2184           0 :                 return GDK_FAIL;
    2185             :         }
    2186     1771461 :         if (size > bi->count)
    2187             :                 size = bi->count;
    2188     1771461 :         if (fprintf(fp, "%d %s %d " BUNFMT " " OIDFMT,
    2189             :                     /* BAT info */
    2190             :                     (int) i,
    2191             :                     BBP_logical(i),
    2192     1771461 :                     (unsigned) bi->restricted << 1,
    2193             :                     size,
    2194     1771461 :                     bi->b->hseqbase) < 0 ||
    2195     3542922 :             heap_entry(fp, bi, size) < 0 ||
    2196     1771461 :             vheap_entry(fp, bi, size) < 0 ||
    2197     3542922 :             (BBP_options(i) && fprintf(fp, " %s", BBP_options(i)) < 0) ||
    2198     1771461 :             fprintf(fp, "\n") < 0) {
    2199           0 :                 GDKsyserror("new_bbpentry: Writing BBP.dir entry failed\n");
    2200           0 :                 return GDK_FAIL;
    2201             :         }
    2202             : 
    2203             :         return GDK_SUCCEED;
    2204             : }
    2205             : 
    2206             : static gdk_return
    2207       12954 : BBPdir_header(FILE *f, int n, lng logno)
    2208             : {
    2209       12954 :         if (fprintf(f, "BBP.dir, GDKversion %u\n%d %d %d\nBBPsize=%d\nBBPinfo=" LLFMT "\n",
    2210             :                     GDKLIBRARY, SIZEOF_SIZE_T, SIZEOF_OID,
    2211             : #ifdef HAVE_HGE
    2212             :                     SIZEOF_HGE
    2213             : #else
    2214             :                     SIZEOF_LNG
    2215             : #endif
    2216       12954 :                     , n, logno) < 0 ||
    2217       12954 :             ferror(f)) {
    2218           0 :                 GDKsyserror("Writing BBP.dir header failed\n");
    2219           0 :                 return GDK_FAIL;
    2220             :         }
    2221             :         return GDK_SUCCEED;
    2222             : }
    2223             : 
    2224             : static gdk_return
    2225       12954 : BBPdir_first(bool subcommit, lng logno, FILE **obbpfp, FILE **nbbpfp)
    2226             : {
    2227       12954 :         FILE *obbpf = NULL, *nbbpf = NULL;
    2228       12954 :         int n = 0;
    2229       12954 :         lng ologno;
    2230             : 
    2231       12954 :         if (obbpfp)
    2232       12729 :                 *obbpfp = NULL;
    2233       12954 :         *nbbpfp = NULL;
    2234             : 
    2235       12954 :         if ((nbbpf = GDKfilelocate(0, "BBP", "w", "dir")) == NULL) {
    2236             :                 return GDK_FAIL;
    2237             :         }
    2238             : 
    2239       12954 :         if (subcommit) {
    2240       12721 :                 char buf[512];
    2241             : 
    2242       12721 :                 assert(obbpfp != NULL);
    2243             :                 /* we need to copy the backup BBP.dir to the new, but
    2244             :                  * replacing the entries for the subcommitted bats */
    2245       12721 :                 if ((obbpf = GDKfileopen(0, SUBDIR, "BBP", "dir", "r")) == NULL &&
    2246           0 :                     (obbpf = GDKfileopen(0, BAKDIR, "BBP", "dir", "r")) == NULL) {
    2247           0 :                         GDKsyserror("subcommit attempted without backup BBP.dir");
    2248           0 :                         goto bailout;
    2249             :                 }
    2250             :                 /* read first three lines */
    2251       25442 :                 if (fgets(buf, sizeof(buf), obbpf) == NULL || /* BBP.dir, GDKversion %d */
    2252       25442 :                     fgets(buf, sizeof(buf), obbpf) == NULL || /* SIZEOF_SIZE_T SIZEOF_OID SIZEOF_MAX_INT */
    2253       12721 :                     fgets(buf, sizeof(buf), obbpf) == NULL) { /* BBPsize=%d */
    2254           0 :                         GDKerror("subcommit attempted with invalid backup BBP.dir.");
    2255           0 :                         goto bailout;
    2256             :                 }
    2257             :                 /* third line contains BBPsize */
    2258       12721 :                 if (sscanf(buf, "BBPsize=%d", &n) != 1) {
    2259           0 :                         GDKerror("cannot read BBPsize in backup BBP.dir.");
    2260           0 :                         goto bailout;
    2261             :                 }
    2262             :                 /* fourth line contains BBPinfo */
    2263       12721 :                 if (fgets(buf, sizeof(buf), obbpf) == NULL ||
    2264       12721 :                     sscanf(buf, "BBPinfo=" LLSCN, &ologno) != 1) {
    2265           0 :                         GDKerror("cannot read BBPinfo in backup BBP.dir.");
    2266           0 :                         goto bailout;
    2267             :                 }
    2268             :         }
    2269             : 
    2270       12954 :         if (n < (bat) ATOMIC_GET(&BBPsize))
    2271        2613 :                 n = (bat) ATOMIC_GET(&BBPsize);
    2272             : 
    2273       12954 :         TRC_DEBUG(IO_, "writing BBP.dir (%d bats).\n", n);
    2274             : 
    2275       12954 :         if (BBPdir_header(nbbpf, n, logno) != GDK_SUCCEED) {
    2276           0 :                 goto bailout;
    2277             :         }
    2278             : 
    2279       12954 :         if (obbpfp)
    2280       12729 :                 *obbpfp = obbpf;
    2281       12954 :         *nbbpfp = nbbpf;
    2282             : 
    2283       12954 :         return GDK_SUCCEED;
    2284             : 
    2285           0 :   bailout:
    2286           0 :         if (obbpf != NULL)
    2287           0 :                 fclose(obbpf);
    2288           0 :         if (nbbpf != NULL)
    2289           0 :                 fclose(nbbpf);
    2290           0 :         return GDK_FAIL;
    2291             : }
    2292             : 
    2293             : static bat
    2294     1964554 : BBPdir_step(bat bid, BUN size, int n, char *buf, size_t bufsize,
    2295             :             FILE **obbpfp, FILE *nbbpf, BATiter *bi)
    2296             : {
    2297     1964554 :         if (n < -1)          /* safety catch */
    2298             :                 return n;
    2299     5218315 :         while (n >= 0 && n < bid) {
    2300     3253761 :                 if (n > 0) {
    2301     1542433 :                         if (fputs(buf, nbbpf) == EOF) {
    2302           0 :                                 GDKerror("Writing BBP.dir file failed.\n");
    2303           0 :                                 goto bailout;
    2304             :                         }
    2305             :                 }
    2306     3253761 :                 if (fgets(buf, (int) bufsize, *obbpfp) == NULL) {
    2307        3230 :                         if (ferror(*obbpfp)) {
    2308           0 :                                 GDKerror("error reading backup BBP.dir.");
    2309           0 :                                 goto bailout;
    2310             :                         }
    2311        3230 :                         n = -1;
    2312        3230 :                         if (fclose(*obbpfp) == EOF) {
    2313           0 :                                 GDKsyserror("Closing backup BBP.dir file failed\n");
    2314           0 :                                 GDKclrerr(); /* ignore error */
    2315             :                         }
    2316        3230 :                         *obbpfp = NULL;
    2317             :                 } else {
    2318     3250531 :                         if (sscanf(buf, "%d", &n) != 1 || n <= 0 || n >= N_BBPINIT * BBPINIT) {
    2319           0 :                                 GDKerror("subcommit attempted with invalid backup BBP.dir.");
    2320           0 :                                 goto bailout;
    2321             :                         }
    2322             :                 }
    2323             :         }
    2324     1964554 :         if (bi) {
    2325     1771461 :                 assert(BBP_status(bid) & BBPPERSISTENT);
    2326     1771461 :                 if (new_bbpentry(nbbpf, bid, size, bi) != GDK_SUCCEED)
    2327           0 :                         goto bailout;
    2328             :         }
    2329     1964554 :         return n == -1 ? -1 : n == bid ? 0 : n;
    2330             : 
    2331           0 :   bailout:
    2332           0 :         if (*obbpfp)
    2333           0 :                 fclose(*obbpfp);
    2334           0 :         fclose(nbbpf);
    2335           0 :         return -2;
    2336             : }
    2337             : 
    2338             : static gdk_return
    2339       12954 : BBPdir_last(int n, char *buf, size_t bufsize, FILE *obbpf, FILE *nbbpf)
    2340             : {
    2341       12954 :         if (n > 0 && fputs(buf, nbbpf) == EOF) {
    2342           0 :                 GDKerror("Writing BBP.dir file failed.\n");
    2343           0 :                 goto bailout;
    2344             :         }
    2345      317019 :         while (obbpf) {
    2346      313556 :                 if (fgets(buf, (int) bufsize, obbpf) == NULL) {
    2347        9491 :                         if (ferror(obbpf)) {
    2348           0 :                                 GDKerror("error reading backup BBP.dir.");
    2349           0 :                                 goto bailout;
    2350             :                         }
    2351        9491 :                         if (fclose(obbpf) == EOF) {
    2352           0 :                                 GDKsyserror("Closing backup BBP.dir file failed\n");
    2353           0 :                                 GDKclrerr(); /* ignore error */
    2354             :                         }
    2355             :                         obbpf = NULL;
    2356             :                 } else {
    2357      304065 :                         if (fputs(buf, nbbpf) == EOF) {
    2358           0 :                                 GDKerror("Writing BBP.dir file failed.\n");
    2359           0 :                                 goto bailout;
    2360             :                         }
    2361             :                 }
    2362             :         }
    2363       12954 :         if (fflush(nbbpf) == EOF ||
    2364       12954 :             (!(ATOMIC_GET(&GDKdebug) & NOSYNCMASK)
    2365             : #if defined(NATIVE_WIN32)
    2366             :              && _commit(_fileno(nbbpf)) < 0
    2367             : #elif defined(HAVE_FDATASYNC)
    2368          10 :              && fdatasync(fileno(nbbpf)) < 0
    2369             : #elif defined(HAVE_FSYNC)
    2370             :              && fsync(fileno(nbbpf)) < 0
    2371             : #endif
    2372             :                     )) {
    2373           0 :                 GDKsyserror("Syncing BBP.dir file failed\n");
    2374           0 :                 goto bailout;
    2375             :         }
    2376       12954 :         if (fclose(nbbpf) == EOF) {
    2377           0 :                 GDKsyserror("Closing BBP.dir file failed\n");
    2378           0 :                 nbbpf = NULL;   /* can't close again */
    2379           0 :                 goto bailout;
    2380             :         }
    2381             : 
    2382       12954 :         TRC_DEBUG(IO_, "end\n");
    2383             : 
    2384             :         return GDK_SUCCEED;
    2385             : 
    2386           0 :   bailout:
    2387           0 :         if (obbpf != NULL)
    2388           0 :                 fclose(obbpf);
    2389           0 :         if (nbbpf != NULL)
    2390           0 :                 fclose(nbbpf);
    2391             :         return GDK_FAIL;
    2392             : }
    2393             : 
    2394             : gdk_return
    2395         225 : BBPdir_init(void)
    2396             : {
    2397         225 :         FILE *fp;
    2398         225 :         gdk_return rc;
    2399             : 
    2400         225 :         rc = BBPdir_first(false, 0, NULL, &fp);
    2401         225 :         if (rc == GDK_SUCCEED)
    2402         225 :                 rc = BBPdir_last(-1, NULL, 0, NULL, fp);
    2403         225 :         return rc;
    2404             : }
    2405             : 
    2406             : /* function used for debugging */
    2407             : void
    2408           0 : BBPdump(void)
    2409             : {
    2410           0 :         size_t mem = 0, vm = 0;
    2411           0 :         int n = 0;
    2412             : 
    2413           0 :         for (bat i = 0; i < (bat) ATOMIC_GET(&BBPsize); i++) {
    2414           0 :                 if (BBP_refs(i) == 0 && BBP_lrefs(i) == 0)
    2415           0 :                         continue;
    2416           0 :                 BAT *b = BBP_desc(i);
    2417           0 :                 unsigned status = BBP_status(i);
    2418           0 :                 printf("# %d: " ALGOOPTBATFMT " refs=%d lrefs=%d status=%u%s",
    2419             :                        i,
    2420           0 :                        ALGOOPTBATPAR(b),
    2421             :                        BBP_refs(i),
    2422           0 :                        BBP_lrefs(i),
    2423             :                        status,
    2424           0 :                        status & BBPLOADED ? "" : " not cached");
    2425           0 :                 if (b->batCacheid == 0) {
    2426           0 :                         printf(", no descriptor\n");
    2427           0 :                         continue;
    2428             :                 }
    2429           0 :                 if (b->theap) {
    2430           0 :                         if (b->theap->parentid != b->batCacheid) {
    2431           0 :                                 printf(" Theap -> %d", b->theap->parentid);
    2432             :                         } else {
    2433           0 :                                 printf(" Theap=[%zu,%zu,f=%d]%s%s",
    2434             :                                        b->theap->free,
    2435             :                                        b->theap->size,
    2436           0 :                                        b->theap->farmid,
    2437           0 :                                        b->theap->base == NULL ? "X" : b->theap->storage == STORE_MMAP ? "M" : "",
    2438           0 :                                        status & BBPSWAPPED ? "(Swapped)" : b->theap->dirty ? "(Dirty)" : "");
    2439           0 :                                 mem += HEAPmemsize(b->theap);
    2440           0 :                                 vm += HEAPvmsize(b->theap);
    2441           0 :                                 n++;
    2442             :                         }
    2443             :                 }
    2444           0 :                 if (b->tvheap) {
    2445           0 :                         if (b->tvheap->parentid != b->batCacheid) {
    2446           0 :                                 printf(" Tvheap -> %d",
    2447             :                                        b->tvheap->parentid);
    2448             :                         } else {
    2449           0 :                                 printf(" Tvheap=[%zu,%zu,f=%d]%s%s",
    2450             :                                        b->tvheap->free,
    2451             :                                        b->tvheap->size,
    2452           0 :                                        b->tvheap->farmid,
    2453           0 :                                        b->tvheap->base == NULL ? "X" : b->tvheap->storage == STORE_MMAP ? "M" : "",
    2454           0 :                                        b->tvheap->dirty ? "(Dirty)" : "");
    2455           0 :                                 mem += HEAPmemsize(b->tvheap);
    2456           0 :                                 vm += HEAPvmsize(b->tvheap);
    2457             :                         }
    2458             :                 }
    2459           0 :                 if (MT_rwlock_rdtry(&b->thashlock)) {
    2460           0 :                         if (b->thash && b->thash != (Hash *) 1) {
    2461           0 :                                 size_t m = HEAPmemsize(&b->thash->heaplink) + HEAPmemsize(&b->thash->heapbckt);
    2462           0 :                                 size_t v = HEAPvmsize(&b->thash->heaplink) + HEAPvmsize(&b->thash->heapbckt);
    2463           0 :                                 printf(" Thash=[%zu,%zu,f=%d/%d]", m, v,
    2464           0 :                                        b->thash->heaplink.farmid,
    2465           0 :                                        b->thash->heapbckt.farmid);
    2466           0 :                                 mem += m;
    2467           0 :                                 vm += v;
    2468             :                         }
    2469           0 :                         MT_rwlock_rdunlock(&b->thashlock);
    2470             :                 }
    2471           0 :                 printf(" role: %s\n",
    2472           0 :                        b->batRole == PERSISTENT ? "persistent" : "transient");
    2473             :         }
    2474           0 :         printf("# %d bats: mem=%zu, vm=%zu\n", n, mem, vm);
    2475           0 :         fflush(stdout);
    2476           0 : }
    2477             : 
    2478             : /*
    2479             :  * @+ BBP Readonly Interface
    2480             :  *
    2481             :  * These interface functions do not change the BBP tables. If they
    2482             :  * only access one specific BAT, the caller must have ensured that no
    2483             :  * other thread is modifying that BAT, therefore such functions do not
    2484             :  * need locking.
    2485             :  *
    2486             :  * BBP index lookup by BAT name:
    2487             :  */
    2488             : static inline bat
    2489       40282 : BBP_find(const char *nme, bool lock)
    2490             : {
    2491       40282 :         bat i = BBPnamecheck(nme);
    2492             : 
    2493       13224 :         if (i != 0) {
    2494             :                 /* for tmp_X BATs, we already know X */
    2495       13224 :                 const char *s;
    2496             : 
    2497       13224 :                 if (i >= (bat) ATOMIC_GET(&BBPsize) || (s = BBP_logical(i)) == NULL || strcmp(s, nme)) {
    2498       13224 :                         i = 0;
    2499             :                 }
    2500       27058 :         } else if (*nme != '.') {
    2501             :                 /* must lock since hash-lookup traverses other BATs */
    2502       27058 :                 if (lock)
    2503        1476 :                         MT_lock_set(&BBPnameLock);
    2504       27413 :                 for (i = BBP_hash[strHash(nme) & BBP_mask]; i; i = BBP_next(i)) {
    2505        1066 :                         if (strcmp(BBP_logical(i), nme) == 0)
    2506             :                                 break;
    2507             :                 }
    2508       27058 :                 if (lock)
    2509        1476 :                         MT_lock_unset(&BBPnameLock);
    2510             :         }
    2511       40282 :         return i;
    2512             : }
    2513             : 
    2514             : bat
    2515        1476 : BBPindex(const char *nme)
    2516             : {
    2517        1476 :         return BBP_find(nme, true);
    2518             : }
    2519             : 
    2520             : /*
    2521             :  * @+ BBP Update Interface
    2522             :  * Operations to insert, delete, clear, and modify BBP entries.
    2523             :  * Our policy for the BBP is to provide unlocked BBP access for
    2524             :  * speed, but still write operations have to be locked.
    2525             :  * #ifdef DEBUG_THREADLOCAL_BATS
    2526             :  * Create the shadow version (reversed) of a bat.
    2527             :  *
    2528             :  * An existing BAT is inserted into the BBP
    2529             :  */
    2530             : 
    2531             : /* The free list is empty.  We create a new entry by either just
    2532             :  * increasing BBPsize (up to BBPlimit) or extending the BBP (which
    2533             :  * increases BBPlimit).
    2534             :  *
    2535             :  * Note that this is the only place in normal, multi-threaded operation
    2536             :  * where BBPsize is assigned a value (never decreasing) and that the
    2537             :  * assignment happens after any necessary memory was allocated and
    2538             :  * initialized. */
    2539             : static gdk_return
    2540       38792 : maybeextend(void)
    2541             : {
    2542       38792 :         bat size = (bat) ATOMIC_GET(&BBPsize);
    2543       38792 :         if (size + BBP_FREE_LOWATER > BBPlimit &&
    2544           0 :             BBPextend(size + BBP_FREE_LOWATER) != GDK_SUCCEED) {
    2545             :                 /* nothing available */
    2546             :                 return GDK_FAIL;
    2547             :         }
    2548       38792 :         ATOMIC_SET(&BBPsize, size + BBP_FREE_LOWATER);
    2549       38792 :         assert(BBP_free == 0);
    2550       38792 :         BBP_free = size;
    2551      387920 :         for (int i = 1; i < BBP_FREE_LOWATER; i++) {
    2552      349128 :                 bat sz = size;
    2553      349128 :                 BBP_next(sz) = ++size;
    2554             :         }
    2555       38792 :         BBP_next(size) = 0;
    2556       38792 :         BBP_nfree += BBP_FREE_LOWATER;
    2557       38792 :         return GDK_SUCCEED;
    2558             : }
    2559             : 
    2560             : /* return new BAT id (> 0); return 0 on failure */
    2561             : bat
    2562    19001845 : BBPallocbat(int tt)
    2563             : {
    2564    19001845 :         MT_Id pid = MT_getpid();
    2565    18999347 :         bool lock = locked_by == 0 || locked_by != pid;
    2566    18999347 :         bat i;
    2567    18999347 :         int len = 0;
    2568    18999347 :         struct freebats *t = MT_thread_getfreebats();
    2569             : 
    2570    18997358 :         if (t->freebats == 0) {
    2571             :                 /* critical section: get a new BBP entry */
    2572      198898 :                 assert(t->nfreebats == 0);
    2573      198898 :                 if (lock) {
    2574      198898 :                         MT_lock_set(&GDKcacheLock);
    2575             :                 }
    2576             : 
    2577             :                 /* get a global bat, perhaps extend */
    2578      198907 :                 if (BBP_free <= 0) {
    2579             :                         /* we need to extend the BBP */
    2580       38792 :                         gdk_return r;
    2581       38792 :                         r = maybeextend();
    2582       38792 :                         if (r != GDK_SUCCEED) {
    2583           0 :                                 if (lock) {
    2584           0 :                                         MT_lock_unset(&GDKcacheLock);
    2585             :                                 }
    2586             :                                 /* failed */
    2587           0 :                                 return 0;
    2588             :                         }
    2589             :                 }
    2590      198907 :                 t->freebats = i = BBP_free;
    2591      198907 :                 bat l = 0;
    2592     2155355 :                 for (int x = 0; x < BBP_FREE_LOWATER && i; x++) {
    2593     1956448 :                         assert(BBP_next(i) == 0 || BBP_next(i) > i);
    2594     1956448 :                         t->nfreebats++;
    2595     1956448 :                         BBP_nfree--;
    2596     1956448 :                         l = i;
    2597     1956448 :                         i = BBP_next(i);
    2598             :                 }
    2599      198907 :                 BBP_next(l) = 0;
    2600      198907 :                 BBP_free = i;
    2601             : 
    2602      198907 :                 if (lock) {
    2603      198907 :                         MT_lock_unset(&GDKcacheLock);
    2604             :                 }
    2605             :                 /* rest of the work outside the lock */
    2606             :         }
    2607    18997367 :         if (t->nfreebats > 0) {
    2608    18997367 :                 assert(t->freebats > 0);
    2609    18997367 :                 i = t->freebats;
    2610    18997367 :                 t->freebats = BBP_next(i);
    2611    18997367 :                 assert(t->freebats == 0 || t->freebats > i);
    2612    18997367 :                 BBP_next(i) = 0;
    2613    18997367 :                 t->nfreebats--;
    2614             :         } else {
    2615           0 :                 assert(t->nfreebats == 0);
    2616           0 :                 assert(t->freebats == 0);
    2617             :                 return 0;
    2618             :         }
    2619             : 
    2620             :         /* fill in basic BBP fields for the new bat */
    2621             : 
    2622    18997367 :         MT_lock_set(&GDKswapLock(i));
    2623    18993474 :         BBP_status_set(i, BBPDELETING|BBPHOT);
    2624    18993474 :         BBP_refs(i) = 1;        /* new bats have 1 pin */
    2625    18993474 :         BBP_lrefs(i) = 0;       /* ie. no logical refs */
    2626    18993474 :         BBP_pid(i) = pid;
    2627    18993474 :         MT_lock_unset(&GDKswapLock(i));
    2628             : 
    2629    19001594 :         if (*BBP_bak(i) == 0)
    2630      417990 :                 len = snprintf(BBP_bak(i), sizeof(BBP_bak(i)), "tmp_%o", (unsigned) i);
    2631    19001594 :         if (len == -1 || len >= FILENAME_MAX) {
    2632           0 :                 GDKerror("impossible error\n");
    2633           0 :                 return 0;
    2634             :         }
    2635    19001594 :         BBP_logical(i) = BBP_bak(i);
    2636             : 
    2637             :         /* Keep the physical location around forever */
    2638    19001594 :         if (!GDKinmemory(0) && *BBP_physical(i) == 0) {
    2639      417555 :                 BBPgetfilename(BBP_physical(i), sizeof(BBP_physical(i)), i);
    2640      417631 :                 TRC_DEBUG(BAT_, "%d = new %s(%s)\n", (int) i, BBP_logical(i), ATOMname(tt));
    2641             :         }
    2642             : 
    2643             :         return i;
    2644             : }
    2645             : 
    2646             : gdk_return
    2647    19016143 : BBPcacheit(BAT *bn, bool lock)
    2648             : {
    2649    19016143 :         bat i = bn->batCacheid;
    2650    19016143 :         unsigned mode;
    2651             : 
    2652    19016143 :         if (lock)
    2653    37991890 :                 lock = locked_by == 0 || locked_by != MT_getpid();
    2654             : 
    2655    19016143 :         assert(i > 0);
    2656             : 
    2657    19016143 :         if (lock)
    2658    18996378 :                 MT_lock_set(&GDKswapLock(i));
    2659    19012767 :         mode = (BBP_status(i) | BBPLOADED) & ~(BBPLOADING | BBPDELETING | BBPSWAPPED);
    2660             : 
    2661             :         /* cache it! */
    2662    19012767 :         BBP_status_set(i, mode);
    2663             : 
    2664    19012767 :         if (lock)
    2665    18997650 :                 MT_lock_unset(&GDKswapLock(i));
    2666    19011514 :         return GDK_SUCCEED;
    2667             : }
    2668             : 
    2669             : /*
    2670             :  * BBPuncacheit changes the BBP status to swapped out.  Currently only
    2671             :  * used in BBPfree (bat swapped out) and BBPclear (bat destroyed
    2672             :  * forever).
    2673             :  */
    2674             : 
    2675             : static void
    2676    19044977 : BBPuncacheit(bat i, bool unloaddesc)
    2677             : {
    2678    19044977 :         if (i < 0)
    2679             :                 i = -i;
    2680    19044977 :         if (BBPcheck(i)) {
    2681    19043132 :                 BAT *b = BBP_desc(i);
    2682             : 
    2683    19043132 :                 assert(unloaddesc || BBP_refs(i) == 0);
    2684             : 
    2685    19043132 :                 if (BBP_status(i) & BBPLOADED) {
    2686    19016024 :                         TRC_DEBUG(BAT_, "uncache %d (%s)\n", (int) i, BBP_logical(i));
    2687             : 
    2688             :                         /* clearing bits can be done without the lock */
    2689    19016024 :                         BBP_status_off(i, BBPLOADED);
    2690             :                 }
    2691    19043132 :                 if (unloaddesc) {
    2692    19030783 :                         BATdestroy(b);
    2693             :                 }
    2694             :         }
    2695    19044746 : }
    2696             : 
    2697             : /*
    2698             :  * @- BBPclear
    2699             :  * BBPclear removes a BAT from the BBP directory forever.
    2700             :  */
    2701             : static inline void
    2702       73205 : BBPhandover(struct freebats *t, uint32_t n)
    2703             : {
    2704       73205 :         bat *p, bid;
    2705             :         /* take one bat from our private free list and hand it over to
    2706             :          * the global free list */
    2707       73205 :         if (n >= t->nfreebats) {
    2708       45881 :                 bid = t->freebats;
    2709       45881 :                 t->freebats = 0;
    2710       45881 :                 BBP_nfree += t->nfreebats;
    2711       45881 :                 t->nfreebats = 0;
    2712             :         } else {
    2713       27324 :                 p = &t->freebats;
    2714      300564 :                 for (uint32_t i = n; i < t->nfreebats; i++)
    2715      273240 :                         p = &BBP_next(*p);
    2716       27324 :                 bid = *p;
    2717       27324 :                 *p = 0;
    2718       27324 :                 BBP_nfree += n;
    2719       27324 :                 t->nfreebats -= n;
    2720             :         }
    2721             :         p = &BBP_free;
    2722     1718961 :         while (bid != 0) {
    2723    12393094 :                 while (*p && *p < bid)
    2724    10747338 :                         p = &BBP_next(*p);
    2725     1645756 :                 bat i = BBP_next(bid);
    2726     1645756 :                 BBP_next(bid) = *p;
    2727     1645756 :                 *p = bid;
    2728     1645756 :                 bid = i;
    2729             :         }
    2730       73205 : }
    2731             : 
    2732             : #ifndef NDEBUG
    2733             : extern void printlist(bat bid) __attribute__((__cold__));
    2734             : /* print a bat free list, pass start of free list as argument
    2735             :  * to be used from the debugger */
    2736             : void
    2737           0 : printlist(bat bid)
    2738             : {
    2739           0 :         int n = 0;
    2740           0 :         while (bid) {
    2741           0 :                 printf("%d ", bid);
    2742           0 :                 bid = BBP_next(bid);
    2743           0 :                 n++;
    2744             :         }
    2745           0 :         printf("(%d)\n", n);
    2746           0 : }
    2747             : #endif
    2748             : 
    2749             : static inline void
    2750    18693099 : bbpclear(bat i, bool lock)
    2751             : {
    2752    18693099 :         struct freebats *t = MT_thread_getfreebats();
    2753             : 
    2754    18691056 :         TRC_DEBUG(BAT_, "clear %d (%s)\n", (int) i, BBP_logical(i));
    2755    18691056 :         BBPuncacheit(i, true);
    2756    18705033 :         TRC_DEBUG(BAT_, "set to unloading %d\n", i);
    2757    18705033 :         if (lock) {
    2758    18698538 :                 MT_lock_set(&GDKswapLock(i));
    2759             :         }
    2760             : 
    2761    18698110 :         BBP_status_set(i, BBPUNLOADING);
    2762    18698110 :         BBP_refs(i) = 0;
    2763    18698110 :         BBP_lrefs(i) = 0;
    2764    18698110 :         if (lock)
    2765    18696337 :                 MT_lock_unset(&GDKswapLock(i));
    2766    18698305 :         if (!BBPtmpcheck(BBP_logical(i))) {
    2767        1310 :                 MT_lock_set(&BBPnameLock);
    2768        1310 :                 BBP_delete(i);
    2769        1310 :                 MT_lock_unset(&BBPnameLock);
    2770             :         }
    2771    18698305 :         if (BBP_logical(i) != BBP_bak(i))
    2772        1310 :                 GDKfree(BBP_logical(i));
    2773    18698881 :         BBP_status_set(i, 0);
    2774    18698881 :         BBP_logical(i) = NULL;
    2775    18698881 :         bat *p;
    2776    54909814 :         for (p = &t->freebats; *p && *p < i; p = &BBP_next(*p))
    2777             :                 ;
    2778    18698881 :         BBP_next(i) = *p;
    2779    18698881 :         *p = i;
    2780    18698881 :         t->nfreebats++;
    2781    18698881 :         BBP_pid(i) = ~(MT_Id)0; /* not zero, not a valid thread id */
    2782    18698881 :         if (t->nfreebats > BBP_FREE_HIWATER) {
    2783       27324 :                 if (lock)
    2784       27324 :                         MT_lock_set(&GDKcacheLock);
    2785       27324 :                 BBPhandover(t, t->nfreebats - BBP_FREE_LOWATER);
    2786       27324 :                 if (lock)
    2787       27324 :                         MT_lock_unset(&GDKcacheLock);
    2788             :         }
    2789    18698881 : }
    2790             : 
    2791             : void
    2792    18696747 : BBPclear(bat i)
    2793             : {
    2794    18696747 :         if (BBPcheck(i)) {
    2795    18693098 :                 bool lock = locked_by == 0 || locked_by != MT_getpid();
    2796    18693098 :                 bbpclear(i, lock);
    2797             :         }
    2798    18695426 : }
    2799             : 
    2800             : void
    2801       50428 : BBPrelinquishbats(void)
    2802             : {
    2803       50428 :         struct freebats *t = MT_thread_getfreebats();
    2804       50428 :         if (t == NULL || t->nfreebats == 0)
    2805             :                 return;
    2806       45881 :         MT_lock_set(&GDKcacheLock);
    2807       91762 :         while (t->nfreebats > 0) {
    2808       45881 :                 BBPhandover(t, t->nfreebats);
    2809             :         }
    2810       45881 :         MT_lock_unset(&GDKcacheLock);
    2811             : }
    2812             : 
    2813             : /*
    2814             :  * @- BBP rename
    2815             :  *
    2816             :  * Each BAT has a logical name that is globally unique.
    2817             :  * The batId is the same as the logical BAT name.
    2818             :  *
    2819             :  * The default logical name of a BAT is tmp_X, where X is the
    2820             :  * batCacheid.  Apart from being globally unique, new logical bat
    2821             :  * names cannot be of the form tmp_X, unless X is the batCacheid.
    2822             :  *
    2823             :  * Physical names consist of a directory name followed by a logical
    2824             :  * name suffix.  The directory name is derived from the batCacheid,
    2825             :  * and is currently organized in a hierarchy that puts max 64 bats in
    2826             :  * each directory (see BBPgetsubdir).
    2827             :  *
    2828             :  * Concerning the physical suffix: it is almost always bat_X. This
    2829             :  * saves us a whole lot of trouble, as bat_X is always unique and no
    2830             :  * conflicts can occur.  Other suffixes are only supported in order
    2831             :  * just for backward compatibility with old repositories (you won't
    2832             :  * see them anymore in new repositories).
    2833             :  */
    2834             : int
    2835       38806 : BBPrename(BAT *b, const char *nme)
    2836             : {
    2837       38806 :         if (b == NULL)
    2838             :                 return 0;
    2839             : 
    2840       38806 :         bat bid = b->batCacheid;
    2841       38806 :         bat tmpid = 0, i;
    2842             : 
    2843       38806 :         if (nme == NULL) {
    2844       13224 :                 if (BBP_bak(bid)[0] == 0 &&
    2845           0 :                     snprintf(BBP_bak(bid), sizeof(BBP_bak(bid)), "tmp_%o", (unsigned) bid) >= (int) sizeof(BBP_bak(bid))) {
    2846             :                         /* cannot happen */
    2847           0 :                         TRC_CRITICAL(GDK, "BBP default filename too long\n");
    2848           0 :                         return BBPRENAME_LONG;
    2849             :                 }
    2850       13224 :                 nme = BBP_bak(bid);
    2851             :         }
    2852             : 
    2853             :         /* If name stays same, do nothing */
    2854       38806 :         if (BBP_logical(bid) && strcmp(BBP_logical(bid), nme) == 0)
    2855             :                 return 0;
    2856             : 
    2857       38806 :         if ((tmpid = BBPnamecheck(nme)) && tmpid != bid) {
    2858           0 :                 GDKerror("illegal temporary name: '%s'\n", nme);
    2859           0 :                 return BBPRENAME_ILLEGAL;
    2860             :         }
    2861       38806 :         if (strLen(nme) >= IDLENGTH) {
    2862           0 :                 GDKerror("illegal temporary name: '%s'\n", nme);
    2863           0 :                 return BBPRENAME_LONG;
    2864             :         }
    2865             : 
    2866       38806 :         MT_lock_set(&BBPnameLock);
    2867       38806 :         i = BBP_find(nme, false);
    2868       38806 :         if (i != 0) {
    2869           1 :                 MT_lock_unset(&BBPnameLock);
    2870           1 :                 GDKerror("name is in use: '%s'.\n", nme);
    2871           1 :                 return BBPRENAME_ALREADY;
    2872             :         }
    2873             : 
    2874       38805 :         char *nnme;
    2875       38805 :         if (nme == BBP_bak(bid) || strcmp(nme, BBP_bak(bid)) == 0) {
    2876       38805 :                 nnme = BBP_bak(bid);
    2877             :         } else {
    2878       25581 :                 nnme = GDKstrdup(nme);
    2879       25581 :                 if (nnme == NULL) {
    2880           0 :                         MT_lock_unset(&BBPnameLock);
    2881           0 :                         return BBPRENAME_MEMORY;
    2882             :                 }
    2883             :         }
    2884             : 
    2885             :         /* carry through the name change */
    2886       38805 :         if (BBP_logical(bid) && !BBPtmpcheck(BBP_logical(bid))) {
    2887       13224 :                 BBP_delete(bid);
    2888             :         }
    2889       38805 :         if (BBP_logical(bid) != BBP_bak(bid))
    2890       13224 :                 GDKfree(BBP_logical(bid));
    2891       38805 :         BBP_logical(bid) = nnme;
    2892       38805 :         if (tmpid == 0) {
    2893       25581 :                 BBP_insert(bid);
    2894             :         }
    2895       38805 :         MT_lock_set(&b->theaplock);
    2896       38805 :         bool transient = b->batTransient;
    2897       38805 :         MT_lock_unset(&b->theaplock);
    2898       38805 :         if (!transient) {
    2899        9568 :                 bool lock = locked_by == 0 || locked_by != MT_getpid();
    2900             : 
    2901        9568 :                 if (lock)
    2902        9568 :                         MT_lock_set(&GDKswapLock(i));
    2903        9568 :                 BBP_status_on(bid, BBPRENAMED);
    2904        9568 :                 if (lock)
    2905        9568 :                         MT_lock_unset(&GDKswapLock(i));
    2906             :         }
    2907       38805 :         MT_lock_unset(&BBPnameLock);
    2908       38805 :         return 0;
    2909             : }
    2910             : 
    2911             : /*
    2912             :  * @+ BBP swapping Policy
    2913             :  * The BAT can be moved back to disk using the routine BBPfree.  It
    2914             :  * frees the storage for other BATs. After this call BAT* references
    2915             :  * maintained for the BAT are wrong.  We should keep track of dirty
    2916             :  * unloaded BATs. They may have to be committed later on, which may
    2917             :  * include reading them in again.
    2918             :  *
    2919             :  * BBPswappable: may this bat be unloaded?  Only real bats without
    2920             :  * memory references can be unloaded.
    2921             :  */
    2922             : static inline void
    2923     3228040 : BBPspin(bat i, const char *s, unsigned event)
    2924             : {
    2925     3228040 :         if (BBPcheck(i) && (BBP_status(i) & event)) {
    2926             :                 lng spin = LL_CONSTANT(0);
    2927             : 
    2928        1269 :                 do {
    2929        1269 :                         MT_sleep_ms(KITTENNAP);
    2930        1269 :                         spin++;
    2931        1269 :                 } while (BBP_status(i) & event);
    2932          34 :                 TRC_DEBUG(BAT_, "%d,%s,%u: " LLFMT " loops\n", (int) i, s, event, spin);
    2933             :         }
    2934     3227875 : }
    2935             : 
    2936             : void
    2937     7063972 : BBPcold(bat i)
    2938             : {
    2939     7063972 :         if (!is_bat_nil(i)) {
    2940     7063983 :                 BAT *b = BBP_desc(i);
    2941     7063983 :                 if (b->batRole == PERSISTENT)
    2942         701 :                         BBP_status_off(i, BBPHOT);
    2943             :         }
    2944     7063972 : }
    2945             : 
    2946             : /* This function can fail if the input parameter (i) is incorrect
    2947             :  * (unlikely). */
    2948             : static inline int
    2949   115967938 : incref(bat i, bool logical, bool lock)
    2950             : {
    2951   115967938 :         int refs;
    2952   115967938 :         BAT *b;
    2953             : 
    2954   115967938 :         if (!BBPcheck(i))
    2955             :                 return 0;
    2956             : 
    2957   115888778 :         if (lock) {
    2958    36282829 :                 for (;;) {
    2959    36282829 :                         MT_lock_set(&GDKswapLock(i));
    2960    36398806 :                         if (!(BBP_status(i) & (BBPUNSTABLE|BBPLOADING)))
    2961             :                                 break;
    2962             :                         /* the BATs is "unstable", try again */
    2963           0 :                         MT_lock_unset(&GDKswapLock(i));
    2964           0 :                         BBPspin(i, __func__, BBPUNSTABLE|BBPLOADING);
    2965             :                 }
    2966             :         }
    2967             :         /* we have the lock */
    2968             : 
    2969   116004755 :         b = BBP_desc(i);
    2970   116004755 :         if (b->batCacheid == 0) {
    2971             :                 /* should not have happened */
    2972           0 :                 if (lock)
    2973           0 :                         MT_lock_unset(&GDKswapLock(i));
    2974           0 :                 return 0;
    2975             :         }
    2976             : 
    2977   116004755 :         assert(BBP_refs(i) + BBP_lrefs(i) ||
    2978             :                BBP_status(i) & (BBPDELETED | BBPSWAPPED));
    2979   116004755 :         if (logical) {
    2980    36287241 :                 refs = ++BBP_lrefs(i);
    2981    36287241 :                 BBP_pid(i) = 0;
    2982             :         } else {
    2983    79717514 :                 refs = ++BBP_refs(i);
    2984    79717514 :                 BBP_status_on(i, BBPHOT);
    2985             :         }
    2986   116004755 :         if (lock)
    2987    36285265 :                 MT_lock_unset(&GDKswapLock(i));
    2988             : 
    2989             :         return refs;
    2990             : }
    2991             : 
    2992             : /* increment the physical reference counter for the given bat
    2993             :  * returns the new reference count
    2994             :  * also increments the physical reference count of the parent bat(s) (if
    2995             :  * any) */
    2996             : int
    2997       55664 : BBPfix(bat i)
    2998             : {
    2999       55664 :         return BATdescriptor(i) ? 1 : 0;
    3000             : }
    3001             : 
    3002             : /* increment the logical reference count for the given bat
    3003             :  * returns the new reference count */
    3004             : int
    3005    31170999 : BBPretain(bat i)
    3006             : {
    3007    31170999 :         bool lock = locked_by == 0 || locked_by != MT_getpid();
    3008             : 
    3009    31170999 :         return incref(i, true, lock);
    3010             : }
    3011             : 
    3012             : static inline int
    3013   142495757 : decref(bat i, bool logical, bool lock, const char *func)
    3014             : {
    3015   142495757 :         int refs = 0, lrefs;
    3016   142495757 :         bool swap = false;
    3017   142495757 :         bool locked = false;
    3018   142495757 :         int farmid = 0;
    3019   142495757 :         BAT *b;
    3020             : 
    3021   142495757 :         if (is_bat_nil(i))
    3022             :                 return -1;
    3023   134276265 :         assert(i > 0);
    3024   134276265 :         if (BBPcheck(i) == 0)
    3025             :                 return -1;
    3026             : 
    3027   134170667 :         if (lock)
    3028   134153825 :                 MT_lock_set(&GDKswapLock(i));
    3029             : 
    3030   134129637 :         while (BBP_status(i) & BBPUNLOADING) {
    3031           0 :                 if (lock)
    3032           0 :                         MT_lock_unset(&GDKswapLock(i));
    3033           0 :                 BBPspin(i, func, BBPUNLOADING);
    3034           0 :                 if (lock)
    3035   134129637 :                         MT_lock_set(&GDKswapLock(i));
    3036             :         }
    3037             : 
    3038   134236078 :         b = (BBP_status(i) & BBPLOADED) ? BBP_desc(i) : NULL;
    3039             : 
    3040             :         /* decrement references by one */
    3041   134236078 :         if (logical) {
    3042    36231251 :                 if (BBP_lrefs(i) == 0) {
    3043           0 :                         GDKerror("%s: %s does not have logical references.\n", func, BBP_logical(i));
    3044           0 :                         assert(0);
    3045             :                 } else {
    3046    36231251 :                         refs = --BBP_lrefs(i);
    3047             :                 }
    3048             :                 /* cannot release last logical ref if still shared */
    3049             :                 // but we could still have a bat iterator on it
    3050             :                 //assert(!BATshared(BBP_desc(i)) || refs > 0);
    3051             :         } else {
    3052    98004827 :                 if (BBP_refs(i) == 0) {
    3053           0 :                         GDKerror("%s: %s does not have pointer fixes.\n", func, BBP_logical(i));
    3054           0 :                         assert(0);
    3055             :                 } else {
    3056    98004827 :                         refs = --BBP_refs(i);
    3057    98004827 :                         if (b && refs == 0) {
    3058    81439756 :                                 MT_lock_set(&b->theaplock);
    3059    81498580 :                                 locked = true;
    3060    81498580 :                                 if (VIEWtparent(b) || VIEWvtparent(b))
    3061    14264558 :                                         BBP_status_on(i, BBPHOT);
    3062             :                         }
    3063             :                 }
    3064             :         }
    3065   134294902 :         if (b) {
    3066   134085727 :                 if (!locked) {
    3067    52804693 :                         MT_lock_set(&b->theaplock);
    3068    52823151 :                         locked = true;
    3069             :                 }
    3070             : #if 0
    3071             :                 if (b->batCount > b->batInserted && !isVIEW(b)) {
    3072             :                         /* if batCount is larger than batInserted and
    3073             :                          * the dirty bits are off, it may be that a
    3074             :                          * (sub)commit happened in parallel to an
    3075             :                          * update; we must undo the turning off of the
    3076             :                          * dirty bits */
    3077             :                         if (b->theap && b->theap->parentid == i)
    3078             :                                 b->theap->dirty = true;
    3079             :                         if (b->tvheap && b->tvheap->parentid == i)
    3080             :                                 b->tvheap->dirty = true;
    3081             :                 }
    3082             : #endif
    3083   134077454 :                 if (b->theap)
    3084   134077454 :                         farmid = b->theap->farmid;
    3085             :         }
    3086             : 
    3087             :         /* we destroy transients asap and unload persistent bats only
    3088             :          * if they have been made cold or are not dirty */
    3089   134286629 :         unsigned chkflag = BBPSYNCING;
    3090   134286629 :         bool swapdirty = false;
    3091   134286629 :         if (b) {
    3092   134085187 :                 size_t cursize;
    3093   134085187 :                 if ((cursize = GDKvm_cursize()) < (size_t) (GDK_vm_maxsize * 0.75)) {
    3094   134090358 :                         if (!locked) {
    3095           0 :                                 MT_lock_set(&b->theaplock);
    3096           0 :                                 locked = true;
    3097             :                         }
    3098   134090358 :                         if (((b->theap ? b->theap->size : 0) + (b->tvheap ? b->tvheap->size : 0)) < (GDK_vm_maxsize - cursize) / 32)
    3099   134098286 :                                 chkflag |= BBPHOT;
    3100           0 :                 } else if (cursize > (size_t) (GDK_vm_maxsize * 0.85))
    3101   134291800 :                         swapdirty = true;
    3102             :         }
    3103             :         /* only consider unloading if refs is 0; if, in addition, lrefs
    3104             :          * is 0, we can definitely unload, else only if some more
    3105             :          * conditions are met */
    3106   231825720 :         if (BBP_refs(i) == 0 &&
    3107   116225325 :             (BBP_lrefs(i) == 0 ||
    3108    97504396 :              (b != NULL && b->theap != NULL
    3109    97507271 :               ? ((swapdirty || !BATdirty(b)) &&
    3110    11798408 :                  !(BBP_status(i) & chkflag) &&
    3111       15710 :                  (BBP_status(i) & BBPPERSISTENT) &&
    3112             :                  /* cannot unload in-memory data */
    3113        7673 :                  !GDKinmemory(farmid) &&
    3114             :                  /* do not unload views or parents of views */
    3115        7674 :                  !BATshared(b) &&
    3116    97511671 :                  b->batCacheid == b->theap->parentid &&
    3117        7275 :                  (b->tvheap == NULL || b->batCacheid == b->tvheap->parentid))
    3118       29524 :               : (BBP_status(i) & BBPTMP)))) {
    3119             :                 /* bat will be unloaded now. set the UNLOADING bit
    3120             :                  * while locked so no other thread thinks it's
    3121             :                  * available anymore */
    3122    18703797 :                 assert((BBP_status(i) & BBPUNLOADING) == 0);
    3123    18703797 :                 TRC_DEBUG(BAT_, "%s set to unloading BAT %d (status %u, lrefs %d)\n", func, i, BBP_status(i), BBP_lrefs(i));
    3124    18703797 :                 BBP_status_on(i, BBPUNLOADING);
    3125    18703797 :                 swap = true;
    3126             :         } /* else: bat cannot be swapped out */
    3127   134291801 :         lrefs = BBP_lrefs(i);
    3128   134291801 :         if (locked)
    3129   134129277 :                 MT_lock_unset(&b->theaplock);
    3130             : 
    3131             :         /* unlock before re-locking in unload; as saving a dirty
    3132             :          * persistent bat may take a long time */
    3133   134425964 :         if (lock)
    3134   134298233 :                 MT_lock_unset(&GDKswapLock(i));
    3135             : 
    3136   134336612 :         if (swap) {
    3137    18703939 :                 if (b != NULL) {
    3138    18685642 :                         if (lrefs == 0 && (BBP_status(i) & BBPDELETED) == 0) {
    3139             :                                 /* free memory (if loaded) and delete from
    3140             :                                  * disk (if transient but saved) */
    3141    18677926 :                                 BBPdestroy(b);
    3142             :                         } else {
    3143        7716 :                                 TRC_DEBUG(BAT_, "%s unload and free bat %d\n", func, i);
    3144             :                                 /* free memory of transient */
    3145        7716 :                                 if (BBPfree(b) != GDK_SUCCEED)
    3146             :                                         return -1;      /* indicate failure */
    3147             :                         }
    3148       18297 :                 } else if (lrefs == 0 && (BBP_status(i) & BBPDELETED) == 0) {
    3149       13180 :                         BATdelete(BBP_desc(i));
    3150       13180 :                         BBPclear(i);
    3151             :                 } else {
    3152        5117 :                         BBP_status_off(i, BBPUNLOADING);
    3153             :                 }
    3154             :         }
    3155             :         return refs;
    3156             : }
    3157             : 
    3158             : int
    3159    94096143 : BBPunfix(bat i)
    3160             : {
    3161    94096143 :         return decref(i, false, true, __func__);
    3162             : }
    3163             : 
    3164             : int
    3165    44446584 : BBPrelease(bat i)
    3166             : {
    3167    44446584 :         return decref(i, true, true, __func__);
    3168             : }
    3169             : 
    3170             : void
    3171     5134441 : BBPkeepref(BAT *b)
    3172             : {
    3173     5134441 :         assert(b != NULL);
    3174     5134441 :         bool lock = locked_by == 0 || locked_by != MT_getpid();
    3175     5134441 :         int i = b->batCacheid;
    3176     5134441 :         int refs = incref(i, true, lock);
    3177     5139146 :         if (refs == 1) {
    3178     4870104 :                 MT_lock_set(&b->theaplock);
    3179     4865369 :                 BATsettrivprop(b);
    3180     4863356 :                 MT_lock_unset(&b->theaplock);
    3181             :         }
    3182     5131133 :         if (ATOMIC_GET(&GDKdebug) & CHECKMASK)
    3183     5093049 :                 BATassertProps(b);
    3184     5132883 :         if (BATsetaccess(b, BAT_READ) == NULL)
    3185             :                 return;         /* already decreffed */
    3186             : 
    3187     4058259 :         refs = decref(i, false, lock, __func__);
    3188     4057252 :         (void) refs;
    3189     4057252 :         assert(refs >= 0);
    3190             : }
    3191             : 
    3192             : BAT *
    3193    79877427 : BATdescriptor(bat i)
    3194             : {
    3195    79877427 :         BAT *b = NULL;
    3196             : 
    3197    79877427 :         if (BBPcheck(i)) {
    3198    79820956 :                 bool lock = locked_by == 0 || locked_by != MT_getpid();
    3199             :                 if (lock) {
    3200    79820956 :                         for (;;) {
    3201    79820956 :                                 MT_lock_set(&GDKswapLock(i));
    3202    79735141 :                                 if (!(BBP_status(i) & (BBPUNSTABLE|BBPLOADING)))
    3203             :                                         break;
    3204             :                                 /* the BATs is "unstable", try again */
    3205           0 :                                 MT_lock_unset(&GDKswapLock(i));
    3206           0 :                                 BBPspin(i, __func__, BBPUNSTABLE|BBPLOADING);
    3207             :                         }
    3208             :                 }
    3209    79735141 :                 if (incref(i, false, false) > 0) {
    3210    79892799 :                         if ((BBP_status(i) & BBPLOADED) == 0) {
    3211       19735 :                                 b = getBBPdescriptor(i);
    3212       19735 :                                 if (b == NULL) {
    3213             :                                         /* if loading failed, we need to
    3214             :                                          * compensate for the incref */
    3215           0 :                                         decref(i, false, false, __func__);
    3216             :                                 }
    3217             :                         } else {
    3218    79873064 :                                 b = BBP_desc(i);
    3219             :                         }
    3220             :                 }
    3221    79892799 :                 if (lock)
    3222    79894665 :                         MT_lock_unset(&GDKswapLock(i));
    3223             :         }
    3224    79862915 :         return b;
    3225             : }
    3226             : 
    3227             : /*
    3228             :  * BBPdescriptor checks whether BAT needs loading and does so if
    3229             :  * necessary. You must have at least one fix on the BAT before calling
    3230             :  * this.
    3231             :  */
    3232             : static BAT *
    3233       19735 : getBBPdescriptor(bat i)
    3234             : {
    3235       19735 :         bool load = false;
    3236       19735 :         BAT *b = NULL;
    3237             : 
    3238       19735 :         assert(i > 0);
    3239       19735 :         if (!BBPcheck(i)) {
    3240           0 :                 GDKerror("BBPcheck failed for bat id %d\n", i);
    3241           0 :                 return NULL;
    3242             :         }
    3243       19735 :         assert(BBP_refs(i));
    3244       19735 :         unsigned status = BBP_status(i);
    3245       19735 :         b = BBP_desc(i);
    3246       19735 :         if ((status & BBPLOADED) == 0 || status & BBPWAITING) {
    3247       19735 :                 while (BBP_status(i) & BBPWAITING) {        /* wait for bat to be loaded by other thread */
    3248           0 :                         MT_lock_unset(&GDKswapLock(i));
    3249           0 :                         BBPspin(i, __func__, BBPWAITING);
    3250       19735 :                         MT_lock_set(&GDKswapLock(i));
    3251             :                 }
    3252       19735 :                 if (BBPvalid(i)) {
    3253       19735 :                         if ((BBP_status(i) & BBPLOADED) == 0) {
    3254       19735 :                                 load = true;
    3255       19735 :                                 TRC_DEBUG(BAT_, "set to loading BAT %d\n", i);
    3256       19735 :                                 BBP_status_on(i, BBPLOADING);
    3257             :                         }
    3258             :                 }
    3259             :         }
    3260       19735 :         if (load) {
    3261       19735 :                 TRC_DEBUG(IO_, "load %s\n", BBP_logical(i));
    3262             : 
    3263       19735 :                 b = BATload_intern(i, false);
    3264             : 
    3265       19735 :                 BBP_status_off(i, BBPLOADING);
    3266       19735 :                 CHECKDEBUG if (b != NULL)
    3267       16688 :                         BATassertProps(b);
    3268             :         }
    3269             :         return b;
    3270             : }
    3271             : 
    3272             : /*
    3273             :  * In BBPsave executes unlocked; it just marks the BBP_status of the
    3274             :  * BAT to BBPsaving, so others that want to save or unload this BAT
    3275             :  * must spin lock on the BBP_status field.
    3276             :  */
    3277             : gdk_return
    3278       19482 : BBPsave(BAT *b)
    3279             : {
    3280       19482 :         bool lock = locked_by == 0 || locked_by != MT_getpid();
    3281       19482 :         bat bid = b->batCacheid;
    3282       19482 :         gdk_return ret = GDK_SUCCEED;
    3283             : 
    3284       19482 :         MT_lock_set(&b->theaplock);
    3285       19482 :         if (BBP_lrefs(bid) == 0 || isVIEW(b) || !BATdirty(b)) {
    3286             :                 /* do nothing */
    3287       10839 :                 MT_lock_unset(&b->theaplock);
    3288       10839 :                 MT_rwlock_rdlock(&b->thashlock);
    3289       10839 :                 if (b->thash && b->thash != (Hash *) 1 &&
    3290         213 :                     (b->thash->heaplink.dirty || b->thash->heapbckt.dirty))
    3291         121 :                         BAThashsave(b, (BBP_status(bid) & BBPPERSISTENT) != 0);
    3292       10839 :                 MT_rwlock_rdunlock(&b->thashlock);
    3293       10839 :                 return GDK_SUCCEED;
    3294             :         }
    3295        8643 :         MT_lock_unset(&b->theaplock);
    3296        8643 :         if (lock)
    3297        8643 :                 MT_lock_set(&GDKswapLock(bid));
    3298             : 
    3299        8643 :         if (BBP_status(bid) & BBPSAVING) {
    3300             :                 /* wait until save in other thread completes */
    3301           0 :                 if (lock)
    3302           0 :                         MT_lock_unset(&GDKswapLock(bid));
    3303           0 :                 BBPspin(bid, __func__, BBPSAVING);
    3304             :         } else {
    3305             :                 /* save it */
    3306        8643 :                 unsigned flags = BBPSAVING;
    3307             : 
    3308        8643 :                 MT_lock_set(&b->theaplock);
    3309        8643 :                 if (DELTAdirty(b)) {
    3310         406 :                         flags |= BBPSWAPPED;
    3311             :                 }
    3312        8643 :                 if (b->batTransient) {
    3313        8638 :                         flags |= BBPTMP;
    3314             :                 }
    3315        8643 :                 MT_lock_unset(&b->theaplock);
    3316        8643 :                 BBP_status_on(bid, flags);
    3317        8643 :                 if (lock)
    3318        8643 :                         MT_lock_unset(&GDKswapLock(bid));
    3319             : 
    3320        8643 :                 TRC_DEBUG(IO_, "save " ALGOBATFMT "\n", ALGOBATPAR(b));
    3321             : 
    3322             :                 /* do the time-consuming work unlocked */
    3323        8643 :                 if (BBP_status(bid) & BBPEXISTING && b->batInserted > 0)
    3324           2 :                         ret = BBPbackup(b, false);
    3325           2 :                 if (ret == GDK_SUCCEED) {
    3326        8643 :                         ret = BATsave(b);
    3327             :                 }
    3328             :                 /* clearing bits can be done without the lock */
    3329        8643 :                 BBP_status_off(bid, BBPSAVING);
    3330             :         }
    3331             :         return ret;
    3332             : }
    3333             : 
    3334             : /*
    3335             :  * TODO merge BBPfree with BATfree? Its function is to prepare a BAT
    3336             :  * for being unloaded (or even destroyed, if the BAT is not
    3337             :  * persistent).
    3338             :  */
    3339             : static void
    3340    18678596 : BBPdestroy(BAT *b)
    3341             : {
    3342    18678596 :         bat tp = VIEWtparent(b);
    3343    18678596 :         bat vtp = VIEWvtparent(b);
    3344             : 
    3345    18678596 :         if (b->theap) {
    3346    18678919 :                 HEAPdecref(b->theap, tp == 0);
    3347    18683844 :                 b->theap = NULL;
    3348    18683844 :                 if (tp != 0)
    3349     9887572 :                         BBPrelease(tp);
    3350             :         }
    3351    18684198 :         if (b->tvheap) {
    3352     3058253 :                 HEAPdecref(b->tvheap, vtp == 0);
    3353     3058263 :                 b->tvheap = NULL;
    3354     3058263 :                 if (vtp != 0)
    3355     2293183 :                         BBPrelease(vtp);
    3356             :         }
    3357    18684178 :         if (b->oldtail) {
    3358           2 :                 ATOMIC_AND(&b->oldtail->refs, ~DELAYEDREMOVE);
    3359           2 :                 HEAPdecref(b->oldtail, true);
    3360           2 :                 b->oldtail = NULL;
    3361             :         }
    3362    18684178 :         BATdelete(b);
    3363             : 
    3364    18682980 :         BBPclear(b->batCacheid);     /* if destroyed; de-register from BBP */
    3365    18681859 : }
    3366             : 
    3367             : static gdk_return
    3368       19482 : BBPfree(BAT *b)
    3369             : {
    3370       19482 :         bat bid = b->batCacheid;
    3371       19482 :         gdk_return ret;
    3372             : 
    3373       19482 :         assert(bid > 0);
    3374       19482 :         assert(BBPswappable(b));
    3375       19482 :         assert(!isVIEW(b));
    3376             : 
    3377       19482 :         BBP_unload_inc();
    3378             :         /* write dirty BATs before unloading */
    3379       19482 :         ret = BBPsave(b);
    3380       19482 :         if (ret == GDK_SUCCEED) {
    3381       19482 :                 if (BBP_status(bid) & BBPLOADED)
    3382       19482 :                         BATfree(b);     /* free memory */
    3383       19482 :                 BBPuncacheit(bid, false);
    3384             :         }
    3385       19482 :         TRC_DEBUG(BAT_, "turn off unloading %d\n", bid);
    3386       19482 :         BBP_status_off(bid, BBPUNLOADING);
    3387       19482 :         BBP_unload_dec();
    3388       19482 :         return ret;
    3389             : }
    3390             : 
    3391             : /*
    3392             :  * BBPquickdesc loads a BAT descriptor without loading the entire BAT,
    3393             :  * of which the result be used only for a *limited* number of
    3394             :  * purposes. Specifically, during the global sync/commit, we do not
    3395             :  * want to load any BATs that are not already loaded, both because
    3396             :  * this costs performance, and because getting into memory shortage
    3397             :  * during a commit is extremely dangerous. Loading a BAT tends not to
    3398             :  * be required, since the commit actions mostly involve moving some
    3399             :  * pointers in the BAT descriptor.
    3400             :  */
    3401             : BAT *
    3402     1272411 : BBPquickdesc(bat bid)
    3403             : {
    3404     1272411 :         BAT *b;
    3405             : 
    3406     1272411 :         if (!BBPcheck(bid)) {
    3407         163 :                 if (!is_bat_nil(bid)) {
    3408           0 :                         GDKerror("called with invalid batid.\n");
    3409           0 :                         assert(0);
    3410             :                 }
    3411             :                 return NULL;
    3412             :         }
    3413     1272053 :         BBPspin(bid, __func__, BBPWAITING);
    3414     1271865 :         b = BBP_desc(bid);
    3415     1271865 :         if (b->ttype < 0) {
    3416         241 :                 const char *aname = ATOMunknown_name(b->ttype);
    3417         241 :                 int tt = ATOMindex(aname);
    3418         241 :                 if (tt < 0) {
    3419           0 :                         GDKwarning("atom '%s' unknown in bat '%s'.\n",
    3420             :                                    aname, BBP_physical(bid));
    3421             :                 } else {
    3422         241 :                         b->ttype = tt;
    3423             :                 }
    3424             :         }
    3425             :         return b;
    3426             : }
    3427             : 
    3428             : /*
    3429             :  * @+ Global Commit
    3430             :  */
    3431             : static BAT *
    3432     1964554 : dirty_bat(bat *i, bool subcommit)
    3433             : {
    3434     1964554 :         if (BBPvalid(*i)) {
    3435     1956029 :                 BAT *b;
    3436     1956029 :                 BBPspin(*i, __func__, BBPSAVING);
    3437     1956029 :                 if (BBP_status(*i) & BBPLOADED) {
    3438     1842666 :                         b = BBP_desc(*i);
    3439     1842666 :                         MT_lock_set(&b->theaplock);
    3440     1965307 :                         if ((BBP_status(*i) & BBPNEW) &&
    3441      122641 :                             BATcheckmodes(b, false) != GDK_SUCCEED) /* check mmap modes */
    3442           0 :                                 *i = -*i;       /* error */
    3443     1842666 :                         else if ((BBP_status(*i) & BBPPERSISTENT) &&
    3444           0 :                                  (subcommit || BATdirty(b))) {
    3445     1663348 :                                 MT_lock_unset(&b->theaplock);
    3446     1663348 :                                 return b;       /* the bat is loaded, persistent and dirty */
    3447             :                         }
    3448      179318 :                         MT_lock_unset(&b->theaplock);
    3449      113363 :                 } else if (subcommit)
    3450      110829 :                         return BBP_desc(*i);
    3451             :         }
    3452             :         return NULL;
    3453             : }
    3454             : 
    3455             : /*
    3456             :  * @- backup-bat
    3457             :  * Backup-bat moves all files of a BAT to a backup directory. Only
    3458             :  * after this succeeds, it may be saved. If some failure occurs
    3459             :  * halfway saving, we can thus always roll back.
    3460             :  */
    3461             : static gdk_return
    3462      221993 : file_move(int farmid, const char *srcdir, const char *dstdir, const char *name, const char *ext)
    3463             : {
    3464      221993 :         if (GDKmove(farmid, srcdir, name, ext, dstdir, name, ext, false) == GDK_SUCCEED) {
    3465             :                 return GDK_SUCCEED;
    3466             :         } else {
    3467           0 :                 char *path;
    3468           0 :                 struct stat st;
    3469             : 
    3470           0 :                 path = GDKfilepath(farmid, srcdir, name, ext);
    3471           0 :                 if (path == NULL)
    3472           0 :                         return GDK_FAIL;
    3473           0 :                 if (MT_stat(path, &st)) {
    3474             :                         /* source file does not exist; the best
    3475             :                          * recovery is to give an error but continue
    3476             :                          * by considering the BAT as not saved; making
    3477             :                          * sure that this time it does get saved.
    3478             :                          */
    3479           0 :                         GDKsyserror("file_move: cannot stat %s\n", path);
    3480           0 :                         GDKfree(path);
    3481           0 :                         return GDK_FAIL;        /* fishy, but not fatal */
    3482             :                 }
    3483           0 :                 GDKfree(path);
    3484             :         }
    3485           0 :         return GDK_FAIL;
    3486             : }
    3487             : 
    3488             : /* returns true if the file exists */
    3489             : static bool
    3490     3137959 : file_exists(int farmid, const char *dir, const char *name, const char *ext)
    3491             : {
    3492     3137959 :         char *path;
    3493     3137959 :         struct stat st;
    3494     3137959 :         int ret = -1;
    3495             : 
    3496     3137959 :         path = GDKfilepath(farmid, dir, name, ext);
    3497     3137959 :         if (path) {
    3498     3137959 :                 ret = MT_stat(path, &st);
    3499     3137959 :                 TRC_DEBUG(IO_, "stat(%s) = %d\n", path, ret);
    3500     3137959 :                 GDKfree(path);
    3501             :         }
    3502     3137959 :         return (ret == 0);
    3503             : }
    3504             : 
    3505             : static gdk_return
    3506      221993 : heap_move(Heap *hp, const char *srcdir, const char *dstdir, const char *nme, const char *ext)
    3507             : {
    3508             :         /* see doc at BATsetaccess()/gdk_bat.c for an expose on mmap
    3509             :          * heap modes */
    3510      221993 :         if (file_exists(hp->farmid, dstdir, nme, ext)) {
    3511             :                 /* dont overwrite heap with the committed state
    3512             :                  * already in dstdir */
    3513             :                 return GDK_SUCCEED;
    3514      221993 :         } else if (hp->newstorage == STORE_PRIV &&
    3515           0 :                    !file_exists(hp->farmid, srcdir, nme, ext)) {
    3516             : 
    3517             :                 /* In order to prevent half-saved X.new files
    3518             :                  * surviving a recover we create a dummy file in the
    3519             :                  * BACKUP(dstdir) whose presence will trigger
    3520             :                  * BBPrecover to remove them.  Thus, X will prevail
    3521             :                  * where it otherwise wouldn't have.  If X already has
    3522             :                  * a saved X.new, that one is backed up as normal.
    3523             :                  */
    3524             : 
    3525           0 :                 FILE *fp;
    3526           0 :                 long_str kill_ext;
    3527           0 :                 char *path;
    3528             : 
    3529           0 :                 strconcat_len(kill_ext, sizeof(kill_ext), ext, ".kill", NULL);
    3530           0 :                 path = GDKfilepath(hp->farmid, dstdir, nme, kill_ext);
    3531           0 :                 if (path == NULL)
    3532             :                         return GDK_FAIL;
    3533           0 :                 fp = MT_fopen(path, "w");
    3534           0 :                 if (fp == NULL)
    3535           0 :                         GDKsyserror("heap_move: cannot open file %s\n", path);
    3536           0 :                 TRC_DEBUG(IO_, "open %s = %d\n", path, fp ? 0 : -1);
    3537           0 :                 GDKfree(path);
    3538             : 
    3539           0 :                 if (fp != NULL) {
    3540           0 :                         fclose(fp);
    3541           0 :                         return GDK_SUCCEED;
    3542             :                 } else {
    3543             :                         return GDK_FAIL;
    3544             :                 }
    3545             :         }
    3546      221993 :         return file_move(hp->farmid, srcdir, dstdir, nme, ext);
    3547             : }
    3548             : 
    3549             : /*
    3550             :  * @- BBPprepare
    3551             :  *
    3552             :  * this routine makes sure there is a BAKDIR/, and initiates one if
    3553             :  * not.  For subcommits, it does the same with SUBDIR.
    3554             :  *
    3555             :  * It is now locked, to get proper file counters, and also to prevent
    3556             :  * concurrent BBPrecovers, etc.
    3557             :  *
    3558             :  * backup_dir == 0 => no backup BBP.dir
    3559             :  * backup_dir == 1 => BBP.dir saved in BACKUP/
    3560             :  * backup_dir == 2 => BBP.dir saved in SUBCOMMIT/
    3561             :  */
    3562             : 
    3563             : static gdk_return
    3564       25784 : BBPprepare(bool subcommit)
    3565             : {
    3566       25784 :         bool start_subcommit;
    3567       25784 :         int set = 1 + subcommit;
    3568       25784 :         gdk_return ret = GDK_SUCCEED;
    3569             : 
    3570       25784 :         start_subcommit = (subcommit && backup_subdir == 0);
    3571       12721 :         if (start_subcommit) {
    3572             :                 /* starting a subcommit. Make sure SUBDIR and DELDIR
    3573             :                  * are clean */
    3574       12721 :                 ret = BBPrecover_subdir();
    3575       12721 :                 if (ret != GDK_SUCCEED)
    3576             :                         return ret;
    3577             :         }
    3578       25784 :         if (backup_files == 0) {
    3579         334 :                 backup_dir = 0;
    3580         334 :                 ret = BBPrecover(0);
    3581         334 :                 if (ret != GDK_SUCCEED)
    3582             :                         return ret;
    3583         334 :                 str bakdirpath = GDKfilepath(0, NULL, BAKDIR, NULL);
    3584         334 :                 if (bakdirpath == NULL) {
    3585             :                         return GDK_FAIL;
    3586             :                 }
    3587             : 
    3588         334 :                 if (MT_mkdir(bakdirpath) < 0 && errno != EEXIST) {
    3589           0 :                         GDKsyserror("cannot create directory %s\n", bakdirpath);
    3590           0 :                         GDKfree(bakdirpath);
    3591           0 :                         return GDK_FAIL;
    3592             :                 }
    3593             :                 /* if BAKDIR already exists, don't signal error */
    3594         334 :                 TRC_DEBUG(IO_, "mkdir %s = %d\n", bakdirpath, (int) ret);
    3595         334 :                 GDKfree(bakdirpath);
    3596             :         }
    3597       25784 :         if (start_subcommit) {
    3598             :                 /* make a new SUBDIR (subdir of BAKDIR) */
    3599       12721 :                 str subdirpath = GDKfilepath(0, NULL, SUBDIR, NULL);
    3600       12721 :                 if (subdirpath == NULL) {
    3601             :                         return GDK_FAIL;
    3602             :                 }
    3603             : 
    3604       12721 :                 if (MT_mkdir(subdirpath) < 0) {
    3605           0 :                         GDKsyserror("cannot create directory %s\n", subdirpath);
    3606           0 :                         GDKfree(subdirpath);
    3607           0 :                         return GDK_FAIL;
    3608             :                 }
    3609       12721 :                 TRC_DEBUG(IO_, "mkdir %s\n", subdirpath);
    3610       12721 :                 GDKfree(subdirpath);
    3611             :         }
    3612       25784 :         if (backup_dir != set) {
    3613             :                 /* a valid backup dir *must* at least contain BBP.dir */
    3614       51886 :                 if ((ret = GDKmove(0, backup_dir ? BAKDIR : BATDIR, "BBP", "dir", subcommit ? SUBDIR : BAKDIR, "BBP", "dir", true)) != GDK_SUCCEED)
    3615             :                         return ret;
    3616       25776 :                 backup_dir = set;
    3617             :         }
    3618             :         /* increase counters */
    3619       25784 :         backup_subdir += subcommit;
    3620       25784 :         backup_files++;
    3621             : 
    3622       25784 :         return ret;
    3623             : }
    3624             : 
    3625             : static gdk_return
    3626     1245863 : do_backup(Heap *h, bool dirty, bool subcommit)
    3627             : {
    3628     1245863 :         gdk_return ret = GDK_SUCCEED;
    3629     1245863 :         char extnew[16];
    3630             : 
    3631     1245863 :         if (h->wasempty) {
    3632             :                 return GDK_SUCCEED;
    3633             :         }
    3634             : 
    3635             :         /* direct mmap is unprotected (readonly usage, or has WAL
    3636             :          * protection) */
    3637     1245863 :         if (h->storage != STORE_MMAP) {
    3638             :                 /* STORE_PRIV saves into X.new files. Two cases could
    3639             :                  * happen. The first is when a valid X.new exists
    3640             :                  * because of an access change or a previous
    3641             :                  * commit. This X.new should be backed up as
    3642             :                  * usual. The second case is when X.new doesn't
    3643             :                  * exist. In that case we could have half written
    3644             :                  * X.new files (after a crash). To protect against
    3645             :                  * these we write X.new.kill files in the backup
    3646             :                  * directory (see heap_move). */
    3647     1235990 :                 gdk_return mvret = GDK_SUCCEED;
    3648             : 
    3649     1235990 :                 char *srcdir = GDKfilepath(NOFARM, BATDIR, h->filename, NULL);
    3650     1235990 :                 if (srcdir == NULL)
    3651             :                         return GDK_FAIL;
    3652     1235990 :                 char *nme = strrchr(srcdir, DIR_SEP);
    3653     1235990 :                 assert(nme != NULL);
    3654     1235990 :                 *nme++ = '\0';
    3655     1235990 :                 char *ext = strchr(nme, '.');
    3656     1235990 :                 assert(ext != NULL);
    3657     1235990 :                 *ext++ = '\0';
    3658             : 
    3659     1235990 :                 strconcat_len(extnew, sizeof(extnew), ext, ".new", NULL);
    3660     1457983 :                 if (dirty &&
    3661      443986 :                     !file_exists(h->farmid, BAKDIR, nme, extnew) &&
    3662      221993 :                     !file_exists(h->farmid, BAKDIR, nme, ext)) {
    3663             :                         /* if the heap is dirty and there is no heap
    3664             :                          * file (with or without .new extension) in
    3665             :                          * the BAKDIR, move the heap (preferably with
    3666             :                          * .new extension) to the correct backup
    3667             :                          * directory */
    3668      221993 :                         if (file_exists(h->farmid, srcdir, nme, extnew)) {
    3669           0 :                                 mvret = heap_move(h, srcdir,
    3670             :                                                   subcommit ? SUBDIR : BAKDIR,
    3671             :                                                   nme, extnew);
    3672      221993 :                         } else if (file_exists(h->farmid, srcdir, nme, ext)) {
    3673      221995 :                                 mvret = heap_move(h, srcdir,
    3674             :                                                   subcommit ? SUBDIR : BAKDIR,
    3675             :                                                   nme, ext);
    3676      221993 :                                 if (mvret == GDK_SUCCEED) {
    3677             :                                         /* file no longer in "standard"
    3678             :                                          * location */
    3679      221993 :                                         h->hasfile = false;
    3680             :                                 }
    3681             :                         }
    3682     1013997 :                 } else if (subcommit) {
    3683             :                         /* if subcommit, we may need to move an
    3684             :                          * already made backup from BAKDIR to
    3685             :                          * SUBDIR */
    3686     1013997 :                         if (file_exists(h->farmid, BAKDIR, nme, extnew))
    3687           0 :                                 mvret = file_move(h->farmid, BAKDIR, SUBDIR, nme, extnew);
    3688     1013997 :                         else if (file_exists(h->farmid, BAKDIR, nme, ext))
    3689           0 :                                 mvret = file_move(h->farmid, BAKDIR, SUBDIR, nme, ext);
    3690             :                 }
    3691             :                 /* there is a situation where the move may fail,
    3692             :                  * namely if this heap was not supposed to be existing
    3693             :                  * before, i.e. after a BATmaterialize on a persistent
    3694             :                  * bat; as a workaround, do not complain about move
    3695             :                  * failure if the source file is nonexistent
    3696             :                  */
    3697      221993 :                 if (mvret != GDK_SUCCEED && file_exists(h->farmid, srcdir, nme, ext)) {
    3698     1235990 :                         ret = GDK_FAIL;
    3699             :                 }
    3700     1235990 :                 if (subcommit &&
    3701     1235988 :                     (h->storage == STORE_PRIV || h->newstorage == STORE_PRIV)) {
    3702           0 :                         long_str kill_ext;
    3703             : 
    3704           0 :                         strconcat_len(kill_ext, sizeof(kill_ext),
    3705             :                                       ext, ".new.kill", NULL);
    3706           0 :                         if (file_exists(h->farmid, BAKDIR, nme, kill_ext) &&
    3707           0 :                             file_move(h->farmid, BAKDIR, SUBDIR, nme, kill_ext) != GDK_SUCCEED) {
    3708           0 :                                 ret = GDK_FAIL;
    3709             :                         }
    3710             :                 }
    3711     1235990 :                 GDKfree(srcdir);
    3712             :         }
    3713             :         return ret;
    3714             : }
    3715             : 
    3716             : static gdk_return
    3717      983254 : BBPbackup(BAT *b, bool subcommit)
    3718             : {
    3719      983254 :         gdk_return rc = GDK_SUCCEED;
    3720             : 
    3721      983254 :         MT_lock_set(&b->theaplock);
    3722      983254 :         BATiter bi = bat_iterator_nolock(b);
    3723      983254 :         if (!bi.copiedtodisk || bi.transient) {
    3724           1 :                 MT_lock_unset(&b->theaplock);
    3725           1 :                 return GDK_SUCCEED;
    3726             :         }
    3727      983253 :         assert(b->theap->parentid == b->batCacheid);
    3728      983253 :         if (b->oldtail && b->oldtail != (Heap *) 1) {
    3729        1715 :                 bi.h = b->oldtail;
    3730        1715 :                 bi.hdirty = b->oldtail->dirty;
    3731             :         }
    3732      983253 :         bat_iterator_incref(&bi);
    3733      983253 :         MT_lock_unset(&b->theaplock);
    3734             : 
    3735             :         /* determine location dir and physical suffix */
    3736      983253 :         if (bi.type != TYPE_void) {
    3737      983253 :                 rc = do_backup(bi.h, bi.hdirty, subcommit);
    3738      983253 :                 if (rc == GDK_SUCCEED && bi.vh != NULL)
    3739      262610 :                         rc = do_backup(bi.vh, bi.vhdirty, subcommit);
    3740             :         }
    3741      983253 :         bat_iterator_end(&bi);
    3742      983253 :         return rc;
    3743             : }
    3744             : 
    3745             : static inline void
    3746           0 : BBPcheckHeap(Heap *h)
    3747             : {
    3748           0 :         struct stat statb;
    3749           0 :         char *path;
    3750             : 
    3751           0 :         char *s = strrchr(h->filename, DIR_SEP);
    3752           0 :         if (s)
    3753           0 :                 s++;
    3754             :         else
    3755             :                 s = h->filename;
    3756           0 :         path = GDKfilepath(0, BAKDIR, s, NULL);
    3757           0 :         if (path == NULL)
    3758           0 :                 return;
    3759           0 :         if (MT_stat(path, &statb) < 0) {
    3760           0 :                 GDKfree(path);
    3761           0 :                 path = GDKfilepath(0, BATDIR, h->filename, NULL);
    3762           0 :                 if (path == NULL)
    3763             :                         return;
    3764           0 :                 if (MT_stat(path, &statb) < 0) {
    3765           0 :                         GDKsyserror("cannot stat file %s (expected size %zu)\n",
    3766             :                                     path, h->free);
    3767           0 :                         assert(0);
    3768             :                         GDKfree(path);
    3769             :                         return;
    3770             :                 }
    3771             :         }
    3772           0 :         assert((statb.st_mode & S_IFMT) == S_IFREG);
    3773           0 :         assert((size_t) statb.st_size >= h->free);
    3774           0 :         if ((size_t) statb.st_size < h->free) {
    3775             :                 GDKerror("file %s too small (expected %zu, actual %zu)\n", path, h->free, (size_t) statb.st_size);
    3776             :                 GDKfree(path);
    3777             :                 return;
    3778             :         }
    3779           0 :         GDKfree(path);
    3780             : }
    3781             : 
    3782             : static void
    3783           0 : BBPcheckBBPdir(void)
    3784             : {
    3785           0 :         FILE *fp;
    3786           0 :         int lineno = 0;
    3787           0 :         bat bbpsize = 0;
    3788           0 :         unsigned bbpversion;
    3789           0 :         lng logno;
    3790             : 
    3791           0 :         fp = GDKfileopen(0, BAKDIR, "BBP", "dir", "r");
    3792           0 :         assert(fp != NULL);
    3793           0 :         if (fp == NULL) {
    3794             :                 fp = GDKfileopen(0, BATDIR, "BBP", "dir", "r");
    3795             :                 assert(fp != NULL);
    3796             :                 if (fp == NULL)
    3797             :                         return;
    3798             :         }
    3799           0 :         bbpversion = BBPheader(fp, &lineno, &bbpsize, &logno, false);
    3800           0 :         if (bbpversion == 0) {
    3801           0 :                 fclose(fp);
    3802           0 :                 return;         /* error reading file */
    3803             :         }
    3804           0 :         assert(bbpversion == GDKLIBRARY);
    3805             : 
    3806           0 :         for (;;) {
    3807           0 :                 BAT b;
    3808           0 :                 Heap h;
    3809           0 :                 Heap vh;
    3810           0 :                 vh = h = (Heap) {
    3811             :                         .free = 0,
    3812             :                 };
    3813           0 :                 b = (BAT) {
    3814             :                         .theap = &h,
    3815             :                         .tvheap = &vh,
    3816             :                 };
    3817           0 :                 char filename[sizeof(BBP_physical(0))];
    3818           0 :                 char batname[129];
    3819             : #ifdef GDKLIBRARY_HASHASH
    3820           0 :                 int hashash;
    3821             : #endif
    3822             : 
    3823           0 :                 switch (BBPreadBBPline(fp, bbpversion, &lineno, &b,
    3824             : #ifdef GDKLIBRARY_HASHASH
    3825             :                                        &hashash,
    3826             : #endif
    3827             :                                        batname, filename, NULL)) {
    3828           0 :                 case 0:
    3829             :                         /* end of file */
    3830           0 :                         fclose(fp);
    3831             :                         /* don't leak errors, this is just debug code */
    3832           0 :                         GDKclrerr();
    3833           0 :                         return;
    3834             :                 case 1:
    3835             :                         /* successfully read an entry */
    3836           0 :                         break;
    3837           0 :                 default:
    3838             :                         /* error */
    3839           0 :                         fclose(fp);
    3840           0 :                         return;
    3841             :                 }
    3842             : #ifdef GDKLIBRARY_HASHASH
    3843           0 :                 assert(hashash == 0);
    3844             : #endif
    3845           0 :                 assert(b.batCacheid < (bat) ATOMIC_GET(&BBPsize));
    3846           0 :                 assert(b.hseqbase <= GDK_oid_max);
    3847           0 :                 if (b.ttype == TYPE_void) {
    3848             :                         /* no files needed */
    3849           0 :                         continue;
    3850             :                 }
    3851           0 :                 if (b.theap->free > 0)
    3852           0 :                         BBPcheckHeap(b.theap);
    3853           0 :                 if (b.tvheap != NULL && b.tvheap->free > 0)
    3854           0 :                         BBPcheckHeap(b.tvheap);
    3855             :         }
    3856             : }
    3857             : 
    3858             : /*
    3859             :  * @+ Atomic Write
    3860             :  * The atomic BBPsync() function first safeguards the old images of
    3861             :  * all files to be written in BAKDIR. It then saves all files. If that
    3862             :  * succeeds fully, BAKDIR is renamed to DELDIR. The rename is
    3863             :  * considered an atomic action. If it succeeds, the DELDIR is removed.
    3864             :  * If something fails, the pre-sync status can be obtained by moving
    3865             :  * back all backed up files; this is done by BBPrecover().
    3866             :  *
    3867             :  * The BBP.dir is also moved into the BAKDIR.
    3868             :  */
    3869             : gdk_return
    3870       12729 : BBPsync(int cnt, bat *restrict subcommit, BUN *restrict sizes, lng logno)
    3871             : {
    3872       12729 :         gdk_return ret = GDK_SUCCEED;
    3873       12729 :         lng t0 = 0, t1 = 0;
    3874       12729 :         str bakdir, deldir;
    3875       12729 :         const bool lock = locked_by == 0 || locked_by != MT_getpid();
    3876       12729 :         char buf[3000];
    3877       12729 :         int n = subcommit ? 0 : -1;
    3878           8 :         FILE *obbpf, *nbbpf;
    3879             : 
    3880       12729 :         if ((bakdir = GDKfilepath(0, NULL, subcommit ? SUBDIR : BAKDIR, NULL)) == NULL)
    3881             :                 return GDK_FAIL;
    3882       12729 :         if ((deldir = GDKfilepath(0, NULL, DELDIR, NULL)) == NULL) {
    3883           0 :                 GDKfree(bakdir);
    3884           0 :                 return GDK_FAIL;
    3885             :         }
    3886             : 
    3887       12729 :         TRC_DEBUG_IF(PERF) t0 = t1 = GDKusec();
    3888             : 
    3889       12729 :         if ((ATOMIC_GET(&GDKdebug) & TAILCHKMASK) && !GDKinmemory(0))
    3890           0 :                 BBPcheckBBPdir();
    3891             : 
    3892       12729 :         ret = BBPprepare(subcommit != NULL);
    3893             : 
    3894       12729 :         if (ret == GDK_SUCCEED) {
    3895       12729 :                 ret = BBPdir_first(subcommit != NULL, logno, &obbpf, &nbbpf);
    3896             :         }
    3897             : 
    3898     1977283 :         for (int idx = 1; ret == GDK_SUCCEED && idx < cnt; idx++) {
    3899     1964554 :                 bat i = subcommit ? subcommit[idx] : idx;
    3900     1964554 :                 BUN size = sizes ? sizes[idx] : BUN_NONE;
    3901     1964554 :                 BATiter bi, *bip;
    3902             : 
    3903     1964554 :                 const bat bid = i;
    3904     1964554 :                 if (lock)
    3905     1953495 :                         MT_lock_set(&GDKswapLock(bid));
    3906             :                 /* set flag that we're syncing, i.e. that we'll
    3907             :                  * be between moving heap to backup dir and
    3908             :                  * saving the new version, in other words, the
    3909             :                  * heap may not exist in the usual location */
    3910     1964554 :                 BBP_status_on(bid, BBPSYNCING);
    3911             :                 /* wait until unloading is finished before
    3912             :                  * attempting to make a backup */
    3913     1964554 :                 while (BBP_status(bid) & BBPUNLOADING) {
    3914           0 :                         if (lock)
    3915           0 :                                 MT_lock_unset(&GDKswapLock(bid));
    3916           0 :                         BBPspin(bid, __func__, BBPUNLOADING);
    3917           0 :                         if (lock)
    3918     1964554 :                                 MT_lock_set(&GDKswapLock(bid));
    3919             :                 }
    3920     1964554 :                 BAT *b = BBP_desc(bid);
    3921     1964554 :                 if (subcommit && b->ttype != TYPE_void) {
    3922             :                         /* move any tail/theap files we find for this bat that
    3923             :                          * are in the BACKUP directory to the SUBCOMMIT
    3924             :                          * directory */
    3925     1953495 :                         assert(b->ttype > 0); /* no unknown types allowed */
    3926     1953495 :                         char fname[16]; /* plenty big enough */
    3927     1953495 :                         if (snprintf(fname, sizeof(fname), "%o", (unsigned) bid) < 16) {
    3928             :                                 /* the snprintf never fails, any of the
    3929             :                                  * below may fail */
    3930     1953495 :                                 uint8_t stpe = ATOMstorage(b->ttype);
    3931     3496545 :                                 if ((b->ttype != TYPE_str || b->twidth >= 8) &&
    3932     1543050 :                                     GDKmove(0, BAKDIR, fname, "tail", SUBDIR, fname, "tail", false) == GDK_SUCCEED)
    3933           2 :                                         TRC_DEBUG(IO_, "moved %s.tail from %s to %s\n",
    3934             :                                                   fname, BAKDIR, SUBDIR);
    3935     2364258 :                                 if (stpe == TYPE_str &&
    3936      410763 :                                     GDKmove(0, BAKDIR, fname, "tail1", SUBDIR, fname, "tail1", false) == GDK_SUCCEED)
    3937           0 :                                         TRC_DEBUG(IO_, "moved %s.tail1 from %s to %s\n",
    3938             :                                                   fname, BAKDIR, SUBDIR);
    3939      519978 :                                 if (stpe == TYPE_str && b->twidth >= 2 &&
    3940      109215 :                                     GDKmove(0, BAKDIR, fname, "tail2", SUBDIR, fname, "tail2", false) == GDK_SUCCEED)
    3941           0 :                                         TRC_DEBUG(IO_, "moved %s.tail2 from %s to %s\n",
    3942             :                                                   fname, BAKDIR, SUBDIR);
    3943             : #if SIZEOF_VAR_T == 8
    3944      428539 :                                 if (stpe == TYPE_str && b->twidth >= 4 &&
    3945       17776 :                                     GDKmove(0, BAKDIR, fname, "tail4", SUBDIR, fname, "tail4", false) == GDK_SUCCEED)
    3946           0 :                                         TRC_DEBUG(IO_, "moved %s.tail4 from %s to %s\n",
    3947             :                                                   fname, BAKDIR, SUBDIR);
    3948             : #endif
    3949     2365268 :                                 if (ATOMvarsized(b->ttype) &&
    3950      411773 :                                     GDKmove(0, BAKDIR, fname, "theap", SUBDIR, fname, "theap", false) == GDK_SUCCEED)
    3951     1953495 :                                         TRC_DEBUG(IO_, "moved %s.theap from %s to %s\n",
    3952             :                                                   fname, BAKDIR, SUBDIR);
    3953             :                         }
    3954             :                 }
    3955     1964554 :                 b = dirty_bat(&i, subcommit != NULL);
    3956     1964554 :                 if (i <= 0)
    3957             :                         ret = GDK_FAIL;
    3958     1964554 :                 else if (BBP_status(bid) & BBPEXISTING &&
    3959     1645847 :                          b != NULL &&
    3960     1645847 :                          b->batInserted > 0)
    3961      983252 :                         ret = BBPbackup(b, subcommit != NULL);
    3962             : 
    3963     1964554 :                 if (lock)
    3964     1953495 :                         MT_lock_unset(&GDKswapLock(bid));
    3965             : 
    3966     1964554 :                 if (ret != GDK_SUCCEED)
    3967             :                         break;
    3968             : 
    3969     1964554 :                 if (BBP_status(i) & BBPPERSISTENT) {
    3970     1771461 :                         MT_lock_set(&BBP_desc(i)->theaplock);
    3971     1771461 :                         bi = bat_iterator_nolock(BBP_desc(i));
    3972     1771461 :                         bat_iterator_incref(&bi);
    3973     1771461 :                         assert(sizes == NULL || size <= bi.count);
    3974     1767827 :                         assert(sizes == NULL || bi.width == 0 || (bi.type == TYPE_msk ? ((size + 31) / 32) * 4 : size << bi.shift) <= bi.hfree);
    3975     1771461 :                         if (size > bi.count) /* includes sizes==NULL */
    3976             :                                 size = bi.count;
    3977     1771461 :                         bi.b->batInserted = size;
    3978     1771461 :                         if (bi.b->ttype >= 0 && ATOMvarsized(bi.b->ttype)) {
    3979             :                                 /* see epilogue() for other part of this */
    3980             :                                 /* remember the tail we're saving */
    3981      408420 :                                 if (BATsetprop_nolock(bi.b, (enum prop_t) 20, TYPE_ptr, &bi.h) == NULL) {
    3982           0 :                                         GDKerror("setprop failed\n");
    3983           0 :                                         ret = GDK_FAIL;
    3984             :                                 } else {
    3985      408420 :                                         if (bi.b->oldtail == NULL)
    3986      406384 :                                                 bi.b->oldtail = (Heap *) 1;
    3987      408420 :                                         HEAPincref(bi.h);
    3988             :                                 }
    3989             :                         }
    3990     1771461 :                         MT_lock_unset(&bi.b->theaplock);
    3991     1771461 :                         if (ret == GDK_SUCCEED && b && size != 0) {
    3992             :                                 /* wait for BBPSAVING so that we
    3993             :                                  * can set it, wait for
    3994             :                                  * BBPUNLOADING before
    3995             :                                  * attempting to save */
    3996     1073410 :                                 for (;;) {
    3997     1073410 :                                         if (lock)
    3998     1073410 :                                                 MT_lock_set(&GDKswapLock(i));
    3999     1073410 :                                         if (!(BBP_status(i) & (BBPSAVING|BBPUNLOADING)))
    4000             :                                                 break;
    4001           0 :                                         if (lock)
    4002           0 :                                                 MT_lock_unset(&GDKswapLock(i));
    4003           0 :                                         BBPspin(i, __func__, BBPSAVING|BBPUNLOADING);
    4004             :                                 }
    4005     1073410 :                                 BBP_status_on(i, BBPSAVING);
    4006     1073410 :                                 if (lock)
    4007     1073410 :                                         MT_lock_unset(&GDKswapLock(i));
    4008     1073410 :                                 ret = BATsave_iter(b, &bi, size);
    4009     1073410 :                                 BBP_status_off(i, BBPSAVING);
    4010             :                         }
    4011             :                         bip = &bi;
    4012             :                 } else {
    4013             :                         bip = NULL;
    4014             :                 }
    4015     1964554 :                 if (ret == GDK_SUCCEED) {
    4016     1964554 :                         n = BBPdir_step(i, size, n, buf, sizeof(buf), &obbpf, nbbpf, bip);
    4017     1964554 :                         if (n < -1)
    4018           0 :                                 ret = GDK_FAIL;
    4019             :                 }
    4020     1964554 :                 if (bip)
    4021     1771461 :                         bat_iterator_end(bip);
    4022             :                 /* we once again have a saved heap */
    4023             :         }
    4024             : 
    4025       12729 :         TRC_DEBUG(PERF, "write time "LLFMT" usec\n", (t0 = GDKusec()) - t1);
    4026             : 
    4027       12729 :         if (ret == GDK_SUCCEED) {
    4028       12729 :                 ret = BBPdir_last(n, buf, sizeof(buf), obbpf, nbbpf);
    4029             :         }
    4030             : 
    4031       12729 :         TRC_DEBUG(PERF, "dir time "LLFMT" usec, %d bats\n", (t1 = GDKusec()) - t0, (bat) ATOMIC_GET(&BBPsize));
    4032             : 
    4033       12729 :         if (ret == GDK_SUCCEED) {
    4034             :                 /* atomic switchover */
    4035             :                 /* this is the big one: this call determines
    4036             :                  * whether the operation of this function
    4037             :                  * succeeded, so no changing of ret after this
    4038             :                  * call anymore */
    4039             : 
    4040       12729 :                 if (MT_rename(bakdir, deldir) < 0 &&
    4041             :                     /* maybe there was an old deldir, so remove and try again */
    4042           0 :                     (GDKremovedir(0, DELDIR) != GDK_SUCCEED ||
    4043           0 :                      MT_rename(bakdir, deldir) < 0))
    4044           0 :                         ret = GDK_FAIL;
    4045           0 :                 if (ret != GDK_SUCCEED)
    4046           0 :                         GDKsyserror("rename(%s,%s) failed\n", bakdir, deldir);
    4047       12729 :                 TRC_DEBUG(IO_, "rename %s %s = %d\n", bakdir, deldir, (int) ret);
    4048             :         }
    4049             : 
    4050             :         /* AFTERMATH */
    4051       12729 :         if (ret == GDK_SUCCEED) {
    4052       12729 :                 ATOMIC_SET(&BBPlogno, logno);       /* the new value */
    4053       12729 :                 backup_files = subcommit ? (backup_files - backup_subdir) : 0;
    4054       12729 :                 backup_dir = backup_subdir = 0;
    4055       12729 :                 if (GDKremovedir(0, DELDIR) != GDK_SUCCEED)
    4056           0 :                         fprintf(stderr, "#BBPsync: cannot remove directory %s\n", DELDIR);
    4057       12729 :                 (void) BBPprepare(false); /* (try to) remove DELDIR and set up new BAKDIR */
    4058       12729 :                 if (backup_files > 1) {
    4059       12721 :                         TRC_DEBUG(PERF, "backup_files %d > 1\n", backup_files);
    4060       12721 :                         backup_files = 1;
    4061             :                 }
    4062             :         }
    4063       12729 :         TRC_DEBUG(PERF, "%s (ready time "LLFMT" usec)\n",
    4064             :                   ret == GDK_SUCCEED ? "" : " failed",
    4065             :                   (t0 = GDKusec()) - t1);
    4066             : 
    4067       12729 :         if (ret != GDK_SUCCEED) {
    4068             :                 /* clean up extra refs we created */
    4069           0 :                 for (int idx = 1; idx < cnt; idx++) {
    4070           0 :                         bat i = subcommit ? subcommit[idx] : idx;
    4071           0 :                         BAT *b = BBP_desc(i);
    4072           0 :                         if (ATOMvarsized(b->ttype)) {
    4073           0 :                                 MT_lock_set(&b->theaplock);
    4074           0 :                                 ValPtr p = BATgetprop_nolock(b, (enum prop_t) 20);
    4075           0 :                                 if (p != NULL) {
    4076           0 :                                         HEAPdecref(p->val.pval, false);
    4077           0 :                                         BATrmprop_nolock(b, (enum prop_t) 20);
    4078             :                                 }
    4079           0 :                                 MT_lock_unset(&b->theaplock);
    4080             :                         }
    4081             :                 }
    4082             :         }
    4083             : 
    4084             :         /* turn off the BBPSYNCING bits for all bats, even when things
    4085             :          * didn't go according to plan (i.e., don't check for ret ==
    4086             :          * GDK_SUCCEED) */
    4087     1977283 :         for (int idx = 1; idx < cnt; idx++) {
    4088     1964554 :                 bat i = subcommit ? subcommit[idx] : idx;
    4089     1964554 :                 BBP_status_off(i, BBPSYNCING);
    4090             :         }
    4091             : 
    4092       12729 :         GDKfree(bakdir);
    4093       12729 :         GDKfree(deldir);
    4094       12729 :         return ret;
    4095             : }
    4096             : 
    4097             : /*
    4098             :  * Recovery just moves all files back to their original location. this
    4099             :  * is an incremental process: if something fails, just stop with still
    4100             :  * files left for moving in BACKUP/.  The recovery process can resume
    4101             :  * later with the left over files.
    4102             :  */
    4103             : static gdk_return
    4104           0 : force_move(int farmid, const char *srcdir, const char *dstdir, const char *name)
    4105             : {
    4106           0 :         const char *p;
    4107           0 :         char *dstpath, *killfile;
    4108           0 :         gdk_return ret = GDK_SUCCEED;
    4109             : 
    4110           0 :         if ((p = strrchr(name, '.')) != NULL && strcmp(p, ".kill") == 0) {
    4111             :                 /* Found a X.new.kill file, ie remove the X.new file */
    4112           0 :                 ptrdiff_t len = p - name;
    4113           0 :                 long_str srcpath;
    4114             : 
    4115           0 :                 strncpy(srcpath, name, len);
    4116           0 :                 srcpath[len] = '\0';
    4117           0 :                 if ((dstpath = GDKfilepath(farmid, dstdir, srcpath, NULL)) == NULL) {
    4118             :                         return GDK_FAIL;
    4119             :                 }
    4120             : 
    4121             :                 /* step 1: remove the X.new file that is going to be
    4122             :                  * overridden by X */
    4123           0 :                 if (MT_remove(dstpath) != 0 && errno != ENOENT) {
    4124             :                         /* if it exists and cannot be removed, all
    4125             :                          * this is going to fail */
    4126           0 :                         GDKsyserror("force_move: remove(%s)\n", dstpath);
    4127           0 :                         GDKfree(dstpath);
    4128           0 :                         return GDK_FAIL;
    4129             :                 }
    4130           0 :                 GDKfree(dstpath);
    4131             : 
    4132             :                 /* step 2: now remove the .kill file. This one is
    4133             :                  * crucial, otherwise we'll never finish recovering */
    4134           0 :                 if ((killfile = GDKfilepath(farmid, srcdir, name, NULL)) == NULL) {
    4135             :                         return GDK_FAIL;
    4136             :                 }
    4137           0 :                 if (MT_remove(killfile) != 0) {
    4138           0 :                         ret = GDK_FAIL;
    4139           0 :                         GDKsyserror("force_move: remove(%s)\n", killfile);
    4140             :                 }
    4141           0 :                 GDKfree(killfile);
    4142           0 :                 return ret;
    4143             :         }
    4144             :         /* try to rename it */
    4145           0 :         ret = GDKmove(farmid, srcdir, name, NULL, dstdir, name, NULL, false);
    4146             : 
    4147           0 :         if (ret != GDK_SUCCEED) {
    4148           0 :                 char *srcpath;
    4149             : 
    4150           0 :                 GDKclrerr();
    4151             :                 /* two legal possible causes: file exists or dir
    4152             :                  * doesn't exist */
    4153           0 :                 if ((dstpath = GDKfilepath(farmid, dstdir, name, NULL)) == NULL)
    4154             :                         return GDK_FAIL;
    4155           0 :                 if ((srcpath = GDKfilepath(farmid, srcdir, name, NULL)) == NULL) {
    4156           0 :                         GDKfree(dstpath);
    4157           0 :                         return GDK_FAIL;
    4158             :                 }
    4159           0 :                 if (MT_remove(dstpath) != 0)    /* clear destination */
    4160           0 :                         ret = GDK_FAIL;
    4161           0 :                 TRC_DEBUG(IO_, "remove %s = %d\n", dstpath, (int) ret);
    4162             : 
    4163           0 :                 (void) GDKcreatedir(dstdir); /* if fails, move will fail */
    4164           0 :                 ret = GDKmove(farmid, srcdir, name, NULL, dstdir, name, NULL, true);
    4165           0 :                 TRC_DEBUG(IO_, "link %s %s = %d\n", srcpath, dstpath, (int) ret);
    4166           0 :                 GDKfree(dstpath);
    4167           0 :                 GDKfree(srcpath);
    4168             :         }
    4169             :         return ret;
    4170             : }
    4171             : 
    4172             : gdk_return
    4173         334 : BBPrecover(int farmid)
    4174             : {
    4175         334 :         str bakdirpath;
    4176         334 :         str leftdirpath;
    4177         334 :         DIR *dirp;
    4178         334 :         struct dirent *dent;
    4179         334 :         long_str path, dstpath;
    4180         334 :         bat i;
    4181         334 :         size_t j = strlen(BATDIR);
    4182         334 :         gdk_return ret = GDK_SUCCEED;
    4183         334 :         bool dirseen = false;
    4184         334 :         str dstdir;
    4185             : 
    4186         334 :         bakdirpath = GDKfilepath(farmid, NULL, BAKDIR, NULL);
    4187         334 :         leftdirpath = GDKfilepath(farmid, NULL, LEFTDIR, NULL);
    4188         334 :         if (bakdirpath == NULL || leftdirpath == NULL) {
    4189           0 :                 GDKfree(bakdirpath);
    4190           0 :                 GDKfree(leftdirpath);
    4191           0 :                 return GDK_FAIL;
    4192             :         }
    4193         334 :         dirp = opendir(bakdirpath);
    4194         334 :         if (dirp == NULL) {
    4195         233 :                 if (errno != ENOENT)
    4196           0 :                         GDKsyserror("cannot open directory %s\n", bakdirpath);
    4197         233 :                 GDKfree(bakdirpath);
    4198         233 :                 GDKfree(leftdirpath);
    4199         233 :                 return GDK_SUCCEED;     /* nothing to do */
    4200             :         }
    4201         101 :         memcpy(dstpath, BATDIR, j);
    4202         101 :         dstpath[j] = DIR_SEP;
    4203         101 :         dstpath[++j] = 0;
    4204         101 :         dstdir = dstpath + j;
    4205         101 :         TRC_DEBUG(IO_, "start\n");
    4206             : 
    4207         101 :         if (MT_mkdir(leftdirpath) < 0 && errno != EEXIST) {
    4208           0 :                 GDKsyserror("cannot create directory %s\n", leftdirpath);
    4209           0 :                 closedir(dirp);
    4210           0 :                 GDKfree(bakdirpath);
    4211           0 :                 GDKfree(leftdirpath);
    4212           0 :                 return GDK_FAIL;
    4213             :         }
    4214             : 
    4215             :         /* move back all files */
    4216         303 :         while ((dent = readdir(dirp)) != NULL) {
    4217         202 :                 const char *q = strchr(dent->d_name, '.');
    4218             : 
    4219         202 :                 if (q == dent->d_name) {
    4220         202 :                         char *fn;
    4221             : 
    4222         202 :                         if (strcmp(dent->d_name, ".") == 0 ||
    4223         101 :                             strcmp(dent->d_name, "..") == 0)
    4224         202 :                                 continue;
    4225           0 :                         fn = GDKfilepath(farmid, BAKDIR, dent->d_name, NULL);
    4226           0 :                         if (fn) {
    4227           0 :                                 int uret = MT_remove(fn);
    4228           0 :                                 TRC_DEBUG(IO_, "remove %s = %d\n",
    4229             :                                           fn, uret);
    4230           0 :                                 GDKfree(fn);
    4231             :                         }
    4232           0 :                         continue;
    4233           0 :                 } else if (strcmp(dent->d_name, "BBP.dir") == 0) {
    4234           0 :                         dirseen = true;
    4235           0 :                         continue;
    4236             :                 }
    4237           0 :                 if (q == NULL)
    4238           0 :                         q = dent->d_name + strlen(dent->d_name);
    4239           0 :                 if ((j = q - dent->d_name) + 1 > sizeof(path)) {
    4240             :                         /* name too long: ignore */
    4241           0 :                         continue;
    4242             :                 }
    4243           0 :                 strncpy(path, dent->d_name, j);
    4244           0 :                 path[j] = 0;
    4245           0 :                 if (GDKisdigit(*path)) {
    4246           0 :                         i = strtol(path, NULL, 8);
    4247             :                 } else {
    4248           0 :                         i = BBP_find(path, false);
    4249           0 :                         if (i < 0)
    4250           0 :                                 i = -i;
    4251             :                 }
    4252           0 :                 if (i == 0 || i >= (bat) ATOMIC_GET(&BBPsize) || !BBPvalid(i)) {
    4253           0 :                         force_move(farmid, BAKDIR, LEFTDIR, dent->d_name);
    4254             :                 } else {
    4255           0 :                         BBPgetsubdir(dstdir, i);
    4256           0 :                         if (force_move(farmid, BAKDIR, dstpath, dent->d_name) != GDK_SUCCEED) {
    4257             :                                 ret = GDK_FAIL;
    4258             :                                 break;
    4259             :                         }
    4260             :                         /* don't trust index files after recovery */
    4261           0 :                         GDKunlink(farmid, dstpath, path, "thashl");
    4262           0 :                         GDKunlink(farmid, dstpath, path, "thashb");
    4263           0 :                         GDKunlink(farmid, dstpath, path, "timprints");
    4264           0 :                         GDKunlink(farmid, dstpath, path, "torderidx");
    4265           0 :                         GDKunlink(farmid, dstpath, path, "tstrimps");
    4266             :                 }
    4267             :         }
    4268         101 :         closedir(dirp);
    4269         101 :         if (dirseen && ret == GDK_SUCCEED) {    /* we have a saved BBP.dir; it should be moved back!! */
    4270           0 :                 struct stat st;
    4271           0 :                 char *fn;
    4272             : 
    4273           0 :                 fn = GDKfilepath(farmid, BATDIR, "BBP", "dir");
    4274           0 :                 if (fn == NULL) {
    4275             :                         ret = GDK_FAIL;
    4276             :                 } else {
    4277           0 :                         ret = recover_dir(farmid, MT_stat(fn, &st) == 0);
    4278           0 :                         GDKfree(fn);
    4279             :                 }
    4280             :         }
    4281             : 
    4282         101 :         if (ret == GDK_SUCCEED) {
    4283         101 :                 if (MT_rmdir(bakdirpath) < 0) {
    4284           0 :                         GDKsyserror("cannot remove directory %s\n", bakdirpath);
    4285           0 :                         ret = GDK_FAIL;
    4286             :                 }
    4287         101 :                 TRC_DEBUG(IO_, "rmdir %s = %d\n", bakdirpath, (int) ret);
    4288             :         }
    4289         101 :         if (ret != GDK_SUCCEED)
    4290           0 :                 GDKerror("recovery failed.\n");
    4291             : 
    4292         101 :         TRC_DEBUG(IO_, "end\n");
    4293         101 :         GDKfree(bakdirpath);
    4294         101 :         GDKfree(leftdirpath);
    4295         101 :         return ret;
    4296             : }
    4297             : 
    4298             : /*
    4299             :  * SUBDIR recovery is quite mindlessly moving all files back to the
    4300             :  * parent (BAKDIR).  We do recognize moving back BBP.dir and set
    4301             :  * backed_up_subdir accordingly.
    4302             :  */
    4303             : gdk_return
    4304       13047 : BBPrecover_subdir(void)
    4305             : {
    4306       13047 :         str subdirpath;
    4307       13047 :         DIR *dirp;
    4308       13047 :         struct dirent *dent;
    4309       13047 :         gdk_return ret = GDK_SUCCEED;
    4310             : 
    4311       13047 :         subdirpath = GDKfilepath(0, NULL, SUBDIR, NULL);
    4312       13047 :         if (subdirpath == NULL)
    4313             :                 return GDK_FAIL;
    4314       13047 :         dirp = opendir(subdirpath);
    4315       13047 :         if (dirp == NULL && errno != ENOENT)
    4316           0 :                 GDKsyserror("cannot open directory %s\n", subdirpath);
    4317       13047 :         GDKfree(subdirpath);
    4318       13047 :         if (dirp == NULL) {
    4319             :                 return GDK_SUCCEED;     /* nothing to do */
    4320             :         }
    4321           0 :         TRC_DEBUG(IO_, "start\n");
    4322             : 
    4323             :         /* move back all files */
    4324           0 :         while ((dent = readdir(dirp)) != NULL) {
    4325           0 :                 if (dent->d_name[0] == '.')
    4326           0 :                         continue;
    4327           0 :                 ret = GDKmove(0, SUBDIR, dent->d_name, NULL, BAKDIR, dent->d_name, NULL, true);
    4328           0 :                 if (ret != GDK_SUCCEED)
    4329             :                         break;
    4330           0 :                 if (strcmp(dent->d_name, "BBP.dir") == 0)
    4331           0 :                         backup_dir = 1;
    4332             :         }
    4333           0 :         closedir(dirp);
    4334             : 
    4335             :         /* delete the directory */
    4336           0 :         if (ret == GDK_SUCCEED) {
    4337           0 :                 ret = GDKremovedir(0, SUBDIR);
    4338           0 :                 if (backup_dir == 2) {
    4339           0 :                         TRC_DEBUG(IO_, "%s%cBBP.dir had disappeared!\n", SUBDIR, DIR_SEP);
    4340           0 :                         backup_dir = 0;
    4341             :                 }
    4342             :         }
    4343           0 :         TRC_DEBUG(IO_, "end = %d\n", (int) ret);
    4344             : 
    4345           0 :         if (ret != GDK_SUCCEED)
    4346           0 :                 GDKerror("recovery failed.\n");
    4347             :         return ret;
    4348             : }
    4349             : 
    4350             : /*
    4351             :  * @- The diskscan
    4352             :  * The BBPdiskscan routine walks through the BAT dir, cleans up
    4353             :  * leftovers, and measures disk occupancy.  Leftovers are files that
    4354             :  * cannot belong to a BAT. in order to establish this for [ht]heap
    4355             :  * files, the BAT descriptor is loaded in order to determine whether
    4356             :  * these files are still required.
    4357             :  *
    4358             :  * The routine gathers all bat sizes in a bat that contains bat-ids
    4359             :  * and bytesizes. The return value is the number of bytes of space
    4360             :  * freed.
    4361             :  */
    4362             : static bool
    4363       24929 : persistent_bat(bat bid)
    4364             : {
    4365       24929 :         if (bid >= 0 && bid < (bat) ATOMIC_GET(&BBPsize) && BBPvalid(bid)) {
    4366       24929 :                 BAT *b = BBP_desc(bid);
    4367       24929 :                 if ((BBP_status(bid) & BBPLOADED) == 0 || b->batCopiedtodisk) {
    4368             :                         return true;
    4369             :                 }
    4370             :         }
    4371             :         return false;
    4372             : }
    4373             : 
    4374             : static BAT *
    4375       24929 : getdesc(bat bid)
    4376             : {
    4377       24929 :         BAT *b = NULL;
    4378             : 
    4379       24929 :         if (is_bat_nil(bid))
    4380             :                 return NULL;
    4381       24929 :         assert(bid > 0);
    4382       24929 :         if (bid < (bat) ATOMIC_GET(&BBPsize) && BBP_logical(bid))
    4383       24929 :                 b = BBP_desc(bid);
    4384       24929 :         if (b == NULL)
    4385           0 :                 BBPclear(bid);
    4386             :         return b;
    4387             : }
    4388             : 
    4389             : static bool
    4390        1722 : BBPdiskscan(const char *parent, size_t baseoff)
    4391             : {
    4392        1722 :         DIR *dirp = opendir(parent);
    4393        1722 :         struct dirent *dent;
    4394        1722 :         char fullname[FILENAME_MAX];
    4395        1722 :         str dst;
    4396        1722 :         size_t dstlen;
    4397        1722 :         const char *src = parent;
    4398             : 
    4399        1722 :         if (dirp == NULL) {
    4400         162 :                 if (errno != ENOENT)
    4401           0 :                         GDKsyserror("cannot open directory %s\n", parent);
    4402         162 :                 return true;    /* nothing to do */
    4403             :         }
    4404             : 
    4405        1560 :         dst = stpcpy(fullname, src);
    4406        1560 :         if (dst > fullname && dst[-1] != DIR_SEP)
    4407        1560 :                 *dst++ = DIR_SEP;
    4408        1560 :         dstlen = sizeof(fullname) - (dst - fullname);
    4409             : 
    4410       32729 :         while ((dent = readdir(dirp)) != NULL) {
    4411       29609 :                 const char *p;
    4412       29609 :                 bat bid;
    4413       29609 :                 bool ok, delete;
    4414             : 
    4415       29609 :                 if (dent->d_name[0] == '.')
    4416        3120 :                         continue;       /* ignore .dot files and directories (. ..) */
    4417             : 
    4418             : #ifdef GDKLIBRARY_JSON
    4419       26489 :                 if (strcmp(dent->d_name, "jsonupgradeneeded") == 0) {
    4420           0 :                         continue; /* ignore json upgrade signal file  */
    4421             :                 }
    4422             : #endif
    4423             : 
    4424       26489 :                 if (strncmp(dent->d_name, "BBP.", 4) == 0 &&
    4425         326 :                     (strcmp(parent + baseoff, BATDIR) == 0 ||
    4426         326 :                      strncmp(parent + baseoff, BAKDIR, strlen(BAKDIR)) == 0 ||
    4427           0 :                      strncmp(parent + baseoff, SUBDIR, strlen(SUBDIR)) == 0))
    4428         326 :                         continue;
    4429             : 
    4430       26163 :                 p = strchr(dent->d_name, '.');
    4431             : 
    4432       26163 :                 if (strlen(dent->d_name) >= dstlen) {
    4433             :                         /* found a file with too long a name
    4434             :                            (i.e. unknown); stop pruning in this
    4435             :                            subdir */
    4436           0 :                         fprintf(stderr, "unexpected file %s, leaving %s.\n", dent->d_name, parent);
    4437           0 :                         break;
    4438             :                 }
    4439       26163 :                 strncpy(dst, dent->d_name, dstlen);
    4440       26163 :                 fullname[sizeof(fullname) - 1] = 0;
    4441             : 
    4442       26163 :                 if (p == NULL && !BBPdiskscan(fullname, baseoff)) {
    4443             :                         /* it was a directory */
    4444        1234 :                         continue;
    4445             :                 }
    4446             : 
    4447       24929 :                 if (p && strcmp(p + 1, "tmp") == 0) {
    4448             :                         delete = true;
    4449             :                         ok = true;
    4450       24929 :                         bid = 0;
    4451             :                 } else {
    4452       24929 :                         bid = strtol(dent->d_name, NULL, 8);
    4453       24929 :                         ok = p && bid;
    4454       24929 :                         delete = false;
    4455             : 
    4456       24929 :                         if (!ok || !persistent_bat(bid)) {
    4457             :                                 delete = true;
    4458       24929 :                         } else if (strncmp(p + 1, "tail", 4) == 0) {
    4459       18468 :                                 BAT *b = getdesc(bid);
    4460       18468 :                                 delete = (b == NULL || !b->ttype || !b->batCopiedtodisk || b->batCount == 0);
    4461       18468 :                                 assert(b == NULL || b->batCount > 0 || b->theap->free == 0);
    4462       18468 :                                 if (!delete) {
    4463       18466 :                                         if (b->ttype == TYPE_str) {
    4464        5084 :                                                 switch (b->twidth) {
    4465        3079 :                                                 case 1:
    4466        3079 :                                                         delete = strcmp(p + 1, "tail1") != 0;
    4467        3079 :                                                         break;
    4468        1653 :                                                 case 2:
    4469        1653 :                                                         delete = strcmp(p + 1, "tail2") != 0;
    4470        1653 :                                                         break;
    4471             : #if SIZEOF_VAR_T == 8
    4472         352 :                                                 case 4:
    4473         352 :                                                         delete = strcmp(p + 1, "tail4") != 0;
    4474         352 :                                                         break;
    4475             : #endif
    4476           0 :                                                 default:
    4477           0 :                                                         delete = strcmp(p + 1, "tail") != 0;
    4478           0 :                                                         break;
    4479             :                                                 }
    4480             :                                         } else {
    4481       13382 :                                                 delete = strcmp(p + 1, "tail") != 0;
    4482             :                                         }
    4483             :                                 }
    4484        6461 :                         } else if (strncmp(p + 1, "theap", 5) == 0) {
    4485        5352 :                                 BAT *b = getdesc(bid);
    4486        5352 :                                 delete = (b == NULL || !b->tvheap || !b->batCopiedtodisk || b->tvheap->free == 0);
    4487        1109 :                         } else if (strncmp(p + 1, "thashl", 6) == 0 ||
    4488         555 :                                    strncmp(p + 1, "thashb", 6) == 0) {
    4489             : #ifdef PERSISTENTHASH
    4490        1108 :                                 BAT *b = getdesc(bid);
    4491        1108 :                                 delete = b == NULL;
    4492        1108 :                                 if (!delete)
    4493        1108 :                                         b->thash = (Hash *) 1;
    4494             : #else
    4495             :                                 delete = true;
    4496             : #endif
    4497           1 :                         } else if (strncmp(p + 1, "thash", 5) == 0) {
    4498             :                                 /* older versions used .thash which we
    4499             :                                  * can simply ignore */
    4500             :                                 delete = true;
    4501           1 :                         } else if (strncmp(p + 1, "thsh", 4) == 0) {
    4502             :                                 /* temporary hash files which we can
    4503             :                                  * simply ignore */
    4504             :                                 delete = true;
    4505           1 :                         } else if (strncmp(p + 1, "timprints", 9) == 0) {
    4506           0 :                                 BAT *b = getdesc(bid);
    4507           0 :                                 delete = b == NULL;
    4508           0 :                                 if (!delete)
    4509           0 :                                         b->timprints = (Imprints *) 1;
    4510           1 :                         } else if (strncmp(p + 1, "torderidx", 9) == 0) {
    4511             : #ifdef PERSISTENTIDX
    4512           0 :                                 BAT *b = getdesc(bid);
    4513           0 :                                 delete = b == NULL;
    4514           0 :                                 if (!delete)
    4515           0 :                                         b->torderidx = (Heap *) 1;
    4516             : #else
    4517             :                                 delete = true;
    4518             : #endif
    4519           1 :                         } else if (strncmp(p + 1, "tstrimps", 8) == 0) {
    4520           1 :                                 BAT *b = getdesc(bid);
    4521           1 :                                 delete = b == NULL;
    4522           1 :                                 if (!delete)
    4523           1 :                                         b->tstrimps = (Strimps *)1;
    4524           0 :                         } else if (strncmp(p + 1, "new", 3) != 0) {
    4525       24929 :                                 ok = false;
    4526             :                         }
    4527             :                 }
    4528       24929 :                 if (!ok) {
    4529             :                         /* found an unknown file; stop pruning in this
    4530             :                          * subdir */
    4531           0 :                         fprintf(stderr, "unexpected file %s, leaving %s.\n", dent->d_name, parent);
    4532           0 :                         break;
    4533             :                 }
    4534       24929 :                 if (delete) {
    4535           2 :                         if (MT_remove(fullname) != 0 && errno != ENOENT) {
    4536           0 :                                 GDKsyserror("remove(%s)", fullname);
    4537           0 :                                 continue;
    4538             :                         }
    4539       31171 :                         TRC_DEBUG(IO_, "remove(%s) = 0\n", fullname);
    4540             :                 }
    4541             :         }
    4542        1560 :         closedir(dirp);
    4543        1560 :         return false;
    4544             : }
    4545             : 
    4546             : void
    4547         325 : gdk_bbp_reset(void)
    4548             : {
    4549         325 :         int i;
    4550             : 
    4551         325 :         BBP_free = 0;
    4552         325 :         BBP_nfree = 0;
    4553         325 :         while (BBPlimit > BBPINIT) {
    4554           0 :                 BBPlimit -= BBPINIT;
    4555           0 :                 assert(BBPlimit >= 0);
    4556           0 :                 GDKfree(BBP[BBPlimit >> BBPINITLOG]);
    4557           0 :                 BBP[BBPlimit >> BBPINITLOG] = NULL;
    4558             :         }
    4559         325 :         ATOMIC_SET(&BBPsize, 0);
    4560         325 :         memset(BBP0, 0, sizeof(BBP0));
    4561       10725 :         for (i = 0; i < MAXFARMS; i++)
    4562       10400 :                 GDKfree((void *) BBPfarms[i].dirname); /* loose "const" */
    4563         325 :         memset(BBPfarms, 0, sizeof(BBPfarms));
    4564         325 :         memset(BBP_hash, 0, sizeof(BBP_hash));
    4565             : 
    4566         325 :         locked_by = 0;
    4567         325 :         BBPunloadCnt = 0;
    4568         325 :         backup_files = 0;
    4569         325 :         backup_dir = 0;
    4570         325 :         backup_subdir = 0;
    4571         325 : }
    4572             : 
    4573             : static MT_Lock GDKCallbackListLock = MT_LOCK_INITIALIZER(GDKCallbackListLock);
    4574             : 
    4575             : static struct {
    4576             :         int cnt;
    4577             :         gdk_callback *head;
    4578             : } callback_list = {
    4579             :         .cnt = 0,
    4580             :         .head = NULL,
    4581             : };
    4582             : 
    4583             : /*
    4584             :  * @- Add a callback
    4585             :  * Adds new callback to the callback list.
    4586             :  */
    4587             : gdk_return
    4588           0 : gdk_add_callback(char *name, gdk_callback_func *f, int argc, void *argv[], int
    4589             :                 interval)
    4590             : {
    4591             : 
    4592           0 :         gdk_callback *callback = NULL;
    4593             : 
    4594           0 :         if (!(callback = GDKmalloc(sizeof(gdk_callback) + sizeof(void *) * argc))) {
    4595           0 :                 TRC_CRITICAL(GDK, "Failed to allocate memory!");
    4596           0 :                 return GDK_FAIL;
    4597             :         }
    4598             : 
    4599           0 :         *callback = (gdk_callback) {
    4600             :                 .name = name,
    4601             :                 .argc = argc,
    4602             :                 .interval = interval,
    4603             :                 .func = f,
    4604             :         };
    4605             : 
    4606           0 :         for (int i=0; i < argc; i++) {
    4607           0 :                 callback->argv[i] = argv[i];
    4608             :         }
    4609             : 
    4610           0 :         MT_lock_set(&GDKCallbackListLock);
    4611           0 :         gdk_callback *p = callback_list.head;
    4612           0 :         if (p) {
    4613             :                 int cnt = 1;
    4614           0 :                 do {
    4615             :                         /* check if already added */
    4616           0 :                         if (strcmp(callback->name, p->name) == 0) {
    4617           0 :                                 MT_lock_unset(&GDKCallbackListLock);
    4618           0 :                                 GDKfree(callback);
    4619           0 :                                 return GDK_FAIL;
    4620             :                         }
    4621           0 :                         if (p->next == NULL) {
    4622           0 :                                 p->next = callback;
    4623           0 :                                 p = callback->next;
    4624             :                         } else {
    4625             :                                 p = p->next;
    4626             :                         }
    4627           0 :                         cnt += 1;
    4628           0 :                 } while(p);
    4629           0 :                 callback_list.cnt = cnt;
    4630             :         } else {
    4631           0 :                 callback_list.cnt = 1;
    4632           0 :                 callback_list.head = callback;
    4633             :         }
    4634           0 :         MT_lock_unset(&GDKCallbackListLock);
    4635           0 :         return GDK_SUCCEED;
    4636             : }
    4637             : 
    4638             : /*
    4639             :  * @- Remove a callback
    4640             :  * Removes a callback from the callback list with a given name as an argument.
    4641             :  */
    4642             : gdk_return
    4643           0 : gdk_remove_callback(char *cb_name, gdk_callback_func *argsfree)
    4644             : {
    4645           0 :         gdk_callback *prev = NULL;
    4646           0 :         gdk_return res = GDK_FAIL;
    4647             : 
    4648           0 :         MT_lock_set(&GDKCallbackListLock);
    4649           0 :         gdk_callback *curr = callback_list.head;
    4650           0 :         while(curr) {
    4651           0 :                 if (strcmp(cb_name, curr->name) == 0) {
    4652           0 :                         if (curr == callback_list.head && prev == NULL) {
    4653           0 :                                 callback_list.head = curr->next;
    4654             :                         } else {
    4655           0 :                                 prev->next = curr->next;
    4656             :                         }
    4657           0 :                         if (argsfree)
    4658           0 :                                 argsfree(curr->argc, curr->argv);
    4659           0 :                         GDKfree(curr);
    4660           0 :                         curr = NULL;
    4661           0 :                         callback_list.cnt -=1;
    4662           0 :                         res = GDK_SUCCEED;
    4663             :                 } else {
    4664           0 :                         prev = curr;
    4665           0 :                         curr = curr->next;
    4666             :                 }
    4667             :         }
    4668           0 :         MT_lock_unset(&GDKCallbackListLock);
    4669           0 :         return res;
    4670             : }
    4671             : 
    4672             : static gdk_return
    4673           0 : do_callback(gdk_callback *cb)
    4674             : {
    4675           0 :         cb->last_called = GDKusec();
    4676           0 :         return cb->func(cb->argc, cb->argv);
    4677             : }
    4678             : 
    4679             : static bool
    4680           0 : should_call(gdk_callback *cb)
    4681             : {
    4682           0 :         if (cb->last_called && cb->interval) {
    4683           0 :                 return (cb->last_called + cb->interval * 1000 * 1000) <
    4684           0 :                         GDKusec();
    4685             :         }
    4686             :         return true;
    4687             : }
    4688             : 
    4689             : static void
    4690          98 : BBPcallbacks(void)
    4691             : {
    4692          98 :         MT_lock_set(&GDKCallbackListLock);
    4693          98 :         gdk_callback *next = callback_list.head;
    4694             : 
    4695          98 :         while (next) {
    4696           0 :                 if(should_call(next))
    4697           0 :                         do_callback(next);
    4698           0 :                 next = next->next;
    4699             :         }
    4700          98 :         MT_lock_unset(&GDKCallbackListLock);
    4701          98 : }
    4702             : 
    4703             : /* GDKtmLock protects all accesses and changes to BAKDIR and SUBDIR.
    4704             :  * MUST use BBPtmlock()/BBPtmunlock() to set/unset the lock.
    4705             :  *
    4706             :  * This is at the end of the file on purpose: we don't want people to
    4707             :  * accidentally use GDKtmLock directly. */
    4708             : static MT_Lock GDKtmLock = MT_LOCK_INITIALIZER(GDKtmLock);
    4709             : static int lockfd;
    4710             : 
    4711             : static void
    4712      110927 : BBPtmlockFinish(void)
    4713             : {
    4714      110927 :         if (!GDKinmemory(0) &&
    4715             :             /* also use an external lock file to synchronize with
    4716             :              * external programs */
    4717      110927 :             (lockfile != NULL ||
    4718         326 :              (lockfile = GDKfilepath(0, NULL, ".tm_lock", NULL)) != NULL)) {
    4719      110927 :                     lockfd = MT_lockf(lockfile, F_LOCK);
    4720             :         }
    4721      110927 : }
    4722             : 
    4723             : void
    4724      110811 : BBPtmlock(void)
    4725             : {
    4726      110811 :         MT_lock_set(&GDKtmLock);
    4727      110811 :         BBPtmlockFinish();
    4728      110811 : }
    4729             : 
    4730             : static bool
    4731         116 : BBPtrytmlock(int ms)
    4732             : {
    4733         116 :         if (!MT_lock_trytime(&GDKtmLock, ms))
    4734           0 :                 return false;
    4735         116 :         BBPtmlockFinish();
    4736         116 :         return true;
    4737             : }
    4738             : 
    4739             : void
    4740      110927 : BBPtmunlock(void)
    4741             : {
    4742      110927 :         if (lockfile && lockfd >= 0) {
    4743      110927 :                 assert(!GDKinmemory(0));
    4744      110927 :                 MT_lockf(lockfile, F_ULOCK);
    4745      110927 :                 close(lockfd);
    4746      110927 :                 lockfd = -1;
    4747             :         }
    4748      110927 :         MT_lock_unset(&GDKtmLock);
    4749      110927 : }
    4750             : 
    4751             : void
    4752         116 : BBPprintinfo(void)
    4753             : {
    4754             :         /* 32 categories for the bats, not all are expected to be filled */
    4755         116 :         struct counters {
    4756             :                 size_t sz;
    4757             :                 size_t vmsz;
    4758             :                 int nr;
    4759         116 :         } bats[2][2][2][2][2] = {0};
    4760         116 :         int nbats = 0;
    4761         116 :         int nskip = 0;
    4762             : 
    4763         116 :         if (!BBPtrytmlock(1000)) {
    4764           0 :                 printf("BBP is currently locked, so no BAT information\n");
    4765           0 :                 return;
    4766             :         }
    4767         116 :         bat sz = (bat) ATOMIC_GET(&BBPsize);
    4768      234156 :         for (bat i = 1; i < sz; i++) {
    4769      234040 :                 if (!MT_lock_trytime(&GDKswapLock(i), 1000)) {
    4770           0 :                         nskip++;
    4771           0 :                         continue;
    4772             :                 }
    4773      234040 :                 int r;
    4774      234040 :                 if ((r = BBP_refs(i)) > 0 || BBP_lrefs(i) > 0) {
    4775      193431 :                         BAT *b = BBP_desc(i);
    4776      193431 :                         if (!MT_lock_trytime(&b->theaplock, 1000)) {
    4777           0 :                                 nskip++;
    4778           0 :                                 b = NULL;
    4779             :                         }
    4780           0 :                         if (b != NULL) {
    4781      193431 :                                 nbats++;
    4782      193431 :                                 ATOMIC_BASE_TYPE status = BBP_status(i);
    4783      193431 :                                 struct counters *bt = &bats[r > 0][BATdirty(b)][(status & BBPPERSISTENT) != 0][(status & BBPLOADED) != 0][(status & BBPHOT) != 0];
    4784      193431 :                                 bt->nr++;
    4785      193431 :                                 if (b->theap && b->batCacheid == b->theap->parentid) {
    4786      193431 :                                         bt->sz += HEAPmemsize(b->theap);
    4787      193431 :                                         bt->vmsz += HEAPvmsize(b->theap);
    4788             :                                 }
    4789      193431 :                                 if (b->tvheap && b->batCacheid == b->tvheap->parentid) {
    4790       11361 :                                         bt->sz += HEAPmemsize(b->tvheap);
    4791       11361 :                                         bt->vmsz += HEAPvmsize(b->tvheap);
    4792             :                                 }
    4793      193431 :                                 MT_lock_unset(&b->theaplock);
    4794             :                         }
    4795             :                 }
    4796      234040 :                 MT_lock_unset(&GDKswapLock(i));
    4797             :         }
    4798         116 :         uint32_t nfree = BBP_nfree;
    4799         116 :         BBPtmunlock();
    4800         116 :         printf("BATs:\n");
    4801         116 :         if (bats[1][1][1][1][1].nr > 0)
    4802         116 :                 printf("fix, dirty, persistent, loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[1][1][1][1][1].nr, bats[1][1][1][1][1].vmsz, bats[1][1][1][1][1].sz);
    4803         116 :         if (bats[1][1][1][1][0].nr > 0)
    4804           0 :                 printf("fix, dirty, persistent, not loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[1][1][1][1][0].nr, bats[1][1][1][1][0].vmsz, bats[1][1][1][1][0].sz);
    4805         116 :         if (bats[1][1][1][0][1].nr > 0)
    4806           0 :                 printf("fix, dirty, persistent, loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[1][1][1][0][1].nr, bats[1][1][1][0][1].vmsz, bats[1][1][1][0][1].sz);
    4807         116 :         if (bats[1][1][1][0][0].nr > 0)
    4808           0 :                 printf("fix, dirty, persistent, not loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[1][1][1][0][0].nr, bats[1][1][1][0][0].vmsz, bats[1][1][1][0][0].sz);
    4809         116 :         if (bats[1][1][0][1][1].nr > 0)
    4810         116 :                 printf("fix, dirty, transient, loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[1][1][0][1][1].nr, bats[1][1][0][1][1].vmsz, bats[1][1][0][1][1].sz);
    4811         116 :         if (bats[1][1][0][1][0].nr > 0)
    4812           1 :                 printf("fix, dirty, transient, not loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[1][1][0][1][0].nr, bats[1][1][0][1][0].vmsz, bats[1][1][0][1][0].sz);
    4813         116 :         if (bats[1][1][0][0][1].nr > 0)
    4814           0 :                 printf("fix, dirty, transient, loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[1][1][0][0][1].nr, bats[1][1][0][0][1].vmsz, bats[1][1][0][0][1].sz);
    4815         116 :         if (bats[1][1][0][0][0].nr > 0)
    4816           0 :                 printf("fix, dirty, transient, not loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[1][1][0][0][0].nr, bats[1][1][0][0][0].vmsz, bats[1][1][0][0][0].sz);
    4817         116 :         if (bats[1][0][1][1][1].nr > 0)
    4818         107 :                 printf("fix, clean, persistent, loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[1][0][1][1][1].nr, bats[1][0][1][1][1].vmsz, bats[1][0][1][1][1].sz);
    4819         116 :         if (bats[1][0][1][1][0].nr > 0)
    4820           0 :                 printf("fix, clean, persistent, not loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[1][0][1][1][0].nr, bats[1][0][1][1][0].vmsz, bats[1][0][1][1][0].sz);
    4821         116 :         if (bats[1][0][1][0][1].nr > 0)
    4822           0 :                 printf("fix, clean, persistent, loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[1][0][1][0][1].nr, bats[1][0][1][0][1].vmsz, bats[1][0][1][0][1].sz);
    4823         116 :         if (bats[1][0][1][0][0].nr > 0)
    4824           0 :                 printf("fix, clean, persistent, not loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[1][0][1][0][0].nr, bats[1][0][1][0][0].vmsz, bats[1][0][1][0][0].sz);
    4825         116 :         if (bats[1][0][0][1][1].nr > 0)
    4826           0 :                 printf("fix, clean, transient, loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[1][0][0][1][1].nr, bats[1][0][0][1][1].vmsz, bats[1][0][0][1][1].sz);
    4827         116 :         if (bats[1][0][0][1][0].nr > 0)
    4828           0 :                 printf("fix, clean, transient, not loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[1][0][0][1][0].nr, bats[1][0][0][1][0].vmsz, bats[1][0][0][1][0].sz);
    4829         116 :         if (bats[1][0][0][0][1].nr > 0)
    4830           0 :                 printf("fix, clean, transient, loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[1][0][0][0][1].nr, bats[1][0][0][0][1].vmsz, bats[1][0][0][0][1].sz);
    4831         116 :         if (bats[1][0][0][0][0].nr > 0)
    4832           0 :                 printf("fix, clean, transient, not loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[1][0][0][0][0].nr, bats[1][0][0][0][0].vmsz, bats[1][0][0][0][0].sz);
    4833         116 :         if (bats[0][1][1][1][1].nr > 0)
    4834         112 :                 printf("no fix, dirty, persistent, loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[0][1][1][1][1].nr, bats[0][1][1][1][1].vmsz, bats[0][1][1][1][1].sz);
    4835         116 :         if (bats[0][1][1][1][0].nr > 0)
    4836          29 :                 printf("no fix, dirty, persistent, not loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[0][1][1][1][0].nr, bats[0][1][1][1][0].vmsz, bats[0][1][1][1][0].sz);
    4837         116 :         if (bats[0][1][1][0][1].nr > 0)
    4838           0 :                 printf("no fix, dirty, persistent, loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[0][1][1][0][1].nr, bats[0][1][1][0][1].vmsz, bats[0][1][1][0][1].sz);
    4839         116 :         if (bats[0][1][1][0][0].nr > 0)
    4840           1 :                 printf("no fix, dirty, persistent, not loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[0][1][1][0][0].nr, bats[0][1][1][0][0].vmsz, bats[0][1][1][0][0].sz);
    4841         116 :         if (bats[0][1][0][1][1].nr > 0)
    4842          92 :                 printf("no fix, dirty, transient, loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[0][1][0][1][1].nr, bats[0][1][0][1][1].vmsz, bats[0][1][0][1][1].sz);
    4843         116 :         if (bats[0][1][0][1][0].nr > 0)
    4844          14 :                 printf("no fix, dirty, transient, not loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[0][1][0][1][0].nr, bats[0][1][0][1][0].vmsz, bats[0][1][0][1][0].sz);
    4845         116 :         if (bats[0][1][0][0][1].nr > 0)
    4846           0 :                 printf("no fix, dirty, transient, loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[0][1][0][0][1].nr, bats[0][1][0][0][1].vmsz, bats[0][1][0][0][1].sz);
    4847         116 :         if (bats[0][1][0][0][0].nr > 0)
    4848          17 :                 printf("no fix, dirty, transient, not loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[0][1][0][0][0].nr, bats[0][1][0][0][0].vmsz, bats[0][1][0][0][0].sz);
    4849         116 :         if (bats[0][0][1][1][1].nr > 0)
    4850         116 :                 printf("no fix, clean, persistent, loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[0][0][1][1][1].nr, bats[0][0][1][1][1].vmsz, bats[0][0][1][1][1].sz);
    4851         116 :         if (bats[0][0][1][1][0].nr > 0)
    4852          19 :                 printf("no fix, clean, persistent, not loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[0][0][1][1][0].nr, bats[0][0][1][1][0].vmsz, bats[0][0][1][1][0].sz);
    4853         116 :         if (bats[0][0][1][0][1].nr > 0)
    4854           0 :                 printf("no fix, clean, persistent, loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[0][0][1][0][1].nr, bats[0][0][1][0][1].vmsz, bats[0][0][1][0][1].sz);
    4855         116 :         if (bats[0][0][1][0][0].nr > 0)
    4856          18 :                 printf("no fix, clean, persistent, not loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[0][0][1][0][0].nr, bats[0][0][1][0][0].vmsz, bats[0][0][1][0][0].sz);
    4857         116 :         if (bats[0][0][0][1][1].nr > 0)
    4858           4 :                 printf("no fix, clean, transient, loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[0][0][0][1][1].nr, bats[0][0][0][1][1].vmsz, bats[0][0][0][1][1].sz);
    4859         116 :         if (bats[0][0][0][1][0].nr > 0)
    4860           0 :                 printf("no fix, clean, transient, not loaded, hot: %d bats, %zu virtual, %zu malloc\n", bats[0][0][0][1][0].nr, bats[0][0][0][1][0].vmsz, bats[0][0][0][1][0].sz);
    4861         116 :         if (bats[0][0][0][0][1].nr > 0)
    4862           0 :                 printf("no fix, clean, transient, loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[0][0][0][0][1].nr, bats[0][0][0][0][1].vmsz, bats[0][0][0][0][1].sz);
    4863         116 :         if (bats[0][0][0][0][0].nr > 0)
    4864           0 :                 printf("no fix, clean, transient, not loaded, cold: %d bats, %zu virtual, %zu malloc\n", bats[0][0][0][0][0].nr, bats[0][0][0][0][0].vmsz, bats[0][0][0][0][0].sz);
    4865             : 
    4866         116 :         printf("%d bats total, %d in use, %"PRIu32" free bats in common shared list\n",
    4867             :                sz - 1, nbats, nfree);
    4868         116 :         if (nskip > 0)
    4869           0 :                 printf("%d bat slots unaccounted for because of locking\n", nskip);
    4870             : }

Generated by: LCOV version 1.14