LCOV - code coverage report
Current view: top level - gdk - gdk_bbp.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1623 2720 59.7 %
Date: 2024-11-12 19:36:54 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       24337 : BBP_insert(bat i)
     153             : {
     154       24337 :         bat idx = (bat) (strHash(BBP_logical(i)) & BBP_mask);
     155             : 
     156       24337 :         BBP_next(i) = BBP_hash[idx];
     157       24337 :         BBP_hash[idx] = i;
     158       24337 : }
     159             : 
     160             : static void
     161       12585 : BBP_delete(bat i)
     162             : {
     163       12585 :         const char *s = BBP_logical(i);
     164       12585 :         bat idx = (bat) (strHash(s) & BBP_mask);
     165             : 
     166       12585 :         for (bat *h = &BBP_hash[idx]; (i = *h) != 0; h = &BBP_next(i)) {
     167       12585 :                 if (strcmp(BBP_logical(i), s) == 0) {
     168       12585 :                         *h = BBP_next(i);
     169       12585 :                         break;
     170             :                 }
     171             :         }
     172       12585 : }
     173             : 
     174             : bat
     175   448239778 : getBBPsize(void)
     176             : {
     177   448239778 :         return (bat) ATOMIC_GET(&BBPsize);
     178             : }
     179             : 
     180             : lng
     181         362 : getBBPlogno(void)
     182             : {
     183         362 :         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    15098934 : BBPselectfarm(role_t role, int type, enum heaptype hptype)
     304             : {
     305    15098934 :         int i;
     306             : 
     307    15098934 :         (void) type;            /* may use in future */
     308    15098934 :         (void) hptype;          /* may use in future */
     309             : 
     310    15098934 :         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    29856076 :         for (i = 0; i < MAXFARMS; i++)
     322    29856076 :                 if (BBPfarms[i].roles & (1U << (int) role))
     323    15094844 :                         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         331 : BBPextend(bat newsize)
     331             : {
     332         331 :         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         334 :         while (BBPlimit < newsize) {
     340           3 :                 BUN limit = BBPlimit >> BBPINITLOG;
     341           3 :                 assert(BBP[limit] == NULL);
     342           3 :                 BBP[limit] = GDKzalloc(BBPINIT * sizeof(BBPrec));
     343           3 :                 if (BBP[limit] == NULL) {
     344           0 :                         GDKerror("failed to extend BAT pool\n");
     345           0 :                         return GDK_FAIL;
     346             :                 }
     347       49155 :                 for (BUN i = 0; i < BBPINIT; i++) {
     348       49152 :                         ATOMIC_INIT(&BBP[limit][i].status, 0);
     349       49152 :                         BBP[limit][i].pid = ~(MT_Id)0;
     350             :                 }
     351           3 :                 BBPlimit += BBPINIT;
     352             :         }
     353             : 
     354             :         return GDK_SUCCEED;
     355             : }
     356             : 
     357             : static gdk_return
     358         102 : recover_dir(int farmid, bool direxists)
     359             : {
     360         102 :         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         102 :         return GDKmove(farmid, BAKDIR, "BBP", "dir", BATDIR, "BBP", "dir", true);
     368             : }
     369             : 
     370             : static inline str
     371      972514 : BBPsubdir_recursive(char *s, bat i)
     372             : {
     373      972514 :         i >>= 6;
     374      972514 :         if (i >= 0100) {
     375      246220 :                 s = BBPsubdir_recursive(s, i);
     376      246216 :                 *s++ = DIR_SEP;
     377             :         }
     378      972510 :         i &= 077;
     379      972510 :         *s++ = '0' + (i >> 3);
     380      972510 :         *s++ = '0' + (i & 7);
     381      972510 :         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      747157 : BBPgetfilename(char *s, size_t len, bat i)
     395             : {
     396      747157 :         if (i >= 0100) {
     397      726638 :                 char *p = BBPsubdir_recursive(s, i);
     398      726628 :                 *p++ = DIR_SEP;
     399      726628 :                 len -= (p - s);
     400      726628 :                 s = p;
     401             :         }
     402      747147 :         if (snprintf(s, len, "%o", i) >= (int) len)
     403           0 :                 TRC_CRITICAL(BAT_, "impossible error\n");
     404      747147 : }
     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        7765 : vheapinit(BAT *b, const char *buf, unsigned bbpversion, const char *filename, int lineno)
     412             : {
     413        7765 :         int n = 0;
     414        7765 :         uint64_t free, size;
     415        7765 :         uint16_t storage;
     416             : 
     417        7765 :         (void) bbpversion;      /* could be used to implement compatibility */
     418             : 
     419        7765 :         size = 0;                             /* for GDKLIBRARY_HSIZE case */
     420        7765 :         storage = STORE_INVALID;              /* for GDKLIBRARY_HSIZE case */
     421       15530 :         if (bbpversion <= GDKLIBRARY_HSIZE ?
     422           0 :             sscanf(buf,
     423             :                    " %" SCNu64 " %" SCNu64 " %" SCNu16
     424             :                    "%n",
     425             :                    &free, &size, &storage, &n) < 3 :
     426        7765 :             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        7765 :         if (b->batCount == 0)
     434        2131 :                 free = 0;
     435        7765 :         if (b->ttype >= 0 &&
     436        7551 :             ATOMstorage(b->ttype) == TYPE_str &&
     437        7497 :             free < GDK_STRHASHTABLE * sizeof(stridx_t) + BATTINY * GDK_VARALIGN)
     438        6443 :                 size = GDK_STRHASHTABLE * sizeof(stridx_t) + BATTINY * GDK_VARALIGN;
     439        1322 :         else if (free < 512)
     440         132 :                 size = 512;
     441             :         else
     442        1190 :                 size = free;
     443       15530 :         *b->tvheap = (Heap) {
     444        7765 :                 .free = (size_t) free,
     445        7765 :                 .size = (size_t) size,
     446             :                 .base = NULL,
     447             :                 .storage = STORE_INVALID,
     448             :                 .cleanhash = true,
     449             :                 .newstorage = STORE_INVALID,
     450             :                 .dirty = false,
     451        7765 :                 .parentid = b->batCacheid,
     452        7765 :                 .farmid = BBPselectfarm(PERSISTENT, b->ttype, varheap),
     453        7765 :                 .hasfile = free > 0,
     454             :         };
     455        7765 :         strconcat_len(b->tvheap->filename, sizeof(b->tvheap->filename),
     456             :                       filename, ".theap", NULL);
     457        7765 :         return n;
     458             : }
     459             : 
     460             : static int
     461       29029 : 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       29029 :         int t;
     468       29029 :         char type[33];
     469       29029 :         uint16_t width;
     470       29029 :         uint16_t var;
     471       29029 :         uint16_t properties;
     472       29029 :         uint64_t nokey0;
     473       29029 :         uint64_t nokey1;
     474       29029 :         uint64_t nosorted;
     475       29029 :         uint64_t norevsorted;
     476       29029 :         uint64_t base;
     477       29029 :         uint64_t free;
     478       29029 :         uint64_t size;
     479       29029 :         uint16_t storage;
     480       29029 :         uint64_t minpos, maxpos;
     481       29029 :         int n;
     482             : 
     483       29029 :         (void) bbpversion;      /* could be used to implement compatibility */
     484             : 
     485       29029 :         size = 0;                             /* for GDKLIBRARY_HSIZE case */
     486       29029 :         storage = STORE_INVALID;              /* for GDKLIBRARY_HSIZE case */
     487       58058 :         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       29029 :             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       29029 :         if (strcmp(type, "wkba") == 0)
     511           0 :                 GDKwarning("type wkba (SQL name: GeometryA) is deprecated\n");
     512             : 
     513       29029 :         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       29029 :         *hashash = var & 2;
     519             : #endif
     520       29029 :         var &= ~2;
     521       29029 :         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       36339 :         } 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       28788 :         } else if (var && t != 0 ?
     530        7551 :                    ATOMsize(t) < width ||
     531        7551 :                    (width != 1 && width != 2 && width != 4
     532             : #if SIZEOF_VAR_T == 8
     533          54 :                     && width != 8
     534             : #endif
     535             :                            ) :
     536       21237 :                    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       29029 :         b->ttype = t;
     541       29029 :         b->twidth = width;
     542       29029 :         b->tshift = ATOMelmshift(width);
     543       29029 :         assert_shift_width(b->tshift,b->twidth);
     544       29029 :         b->tnokey[0] = (BUN) nokey0;
     545       29029 :         b->tnokey[1] = (BUN) nokey1;
     546       29029 :         b->tsorted = (bit) ((properties & 0x0001) != 0);
     547       29029 :         b->trevsorted = (bit) ((properties & 0x0080) != 0);
     548       29029 :         b->tkey = (properties & 0x0100) != 0;
     549       29029 :         b->tnonil = (properties & 0x0400) != 0;
     550       29029 :         b->tnil = (properties & 0x0800) != 0;
     551       29029 :         b->tascii = (properties & 0x1000) != 0;
     552       29029 :         b->tnosorted = (BUN) nosorted;
     553       29029 :         b->tnorevsorted = (BUN) norevsorted;
     554       29029 :         b->tunique_est = 0.0;
     555             :         /* (properties & 0x0200) is the old tdense flag */
     556       29029 :         b->tseqbase = (properties & 0x0200) == 0 || base >= (uint64_t) oid_nil ? oid_nil : (oid) base;
     557       29029 :         b->theap->free = (size_t) free;
     558       29029 :         b->theap->hasfile = free > 0;
     559             :         /* set heap size to match capacity */
     560       29029 :         if (b->ttype == TYPE_msk) {
     561             :                 /* round up capacity to multiple of 32 */
     562        4450 :                 b->batCapacity = (b->batCapacity + 31) & ~((BUN) 31);
     563        4450 :                 b->theap->size = b->batCapacity / 8;
     564             :         } else {
     565       24579 :                 b->theap->size = (size_t) b->batCapacity << b->tshift;
     566             :         }
     567       29029 :         b->theap->base = NULL;
     568       29029 :         settailname(b->theap, filename, t, width);
     569       29029 :         b->theap->storage = STORE_INVALID;
     570       29029 :         b->theap->newstorage = STORE_INVALID;
     571       29029 :         b->theap->farmid = BBPselectfarm(PERSISTENT, b->ttype, offheap);
     572       29029 :         b->theap->dirty = false;
     573       29029 :         b->theap->parentid = b->batCacheid;
     574       29029 :         if (minpos < b->batCount)
     575       10534 :                 b->tminpos = (BUN) minpos;
     576             :         else
     577       18495 :                 b->tminpos = BUN_NONE;
     578       29029 :         if (maxpos < b->batCount)
     579       10594 :                 b->tmaxpos = (BUN) maxpos;
     580             :         else
     581       18435 :                 b->tmaxpos = BUN_NONE;
     582       29029 :         if (t && var) {
     583        7765 :                 t = vheapinit(b, buf + n, bbpversion, filename, lineno);
     584        7765 :                 if (t < 0)
     585             :                         return t;
     586        7765 :                 n += t;
     587             :         } else {
     588       21264 :                 b->tvheap = NULL;
     589             :         }
     590       29029 :         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       29361 : 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       29361 :         char buf[4096];
     610       29361 :         uint64_t batid;
     611       29361 :         unsigned int properties;
     612       29361 :         int nread, n;
     613       29361 :         char *s;
     614       29361 :         uint64_t count, base = 0;
     615             : 
     616       29361 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
     617         332 :                 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       29029 :         (*lineno)++;
     624       29029 :         if ((s = strpbrk(buf, "\r\n")) != NULL) {
     625       29029 :                 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       29029 :                 *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       58058 :         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       26495 :             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       29029 :         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       29029 :         BBPgetfilename(filename, sizeof(BBP_physical(0)), (bat) batid);
     666             : 
     667       29029 :         bn->batCacheid = (bat) batid;
     668       29029 :         bn->batTransient = false;
     669       29029 :         bn->batCopiedtodisk = true;
     670       29029 :         switch ((properties & 0x06) >> 1) {
     671        1044 :         case 0:
     672        1044 :                 bn->batRestricted = BAT_WRITE;
     673        1044 :                 break;
     674       27985 :         case 1:
     675       27985 :                 bn->batRestricted = BAT_READ;
     676       27985 :                 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       29029 :         bn->batCount = (BUN) count;
     685       29029 :         bn->batInserted = bn->batCount;
     686             :         /* set capacity to at least count */
     687       29029 :         bn->batCapacity = (BUN) count <= BATTINY ? BATTINY : (BUN) count;
     688             : 
     689       29029 :         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       29029 :         bn->hseqbase = (oid) base;
     694       29029 :         n = heapinit(bn, buf + nread,
     695             : #ifdef GDKLIBRARY_HASHASH
     696             :                      hashash,
     697             : #endif
     698             :                      bbpversion, filename, *lineno);
     699       29029 :         if (n < 0) {
     700             :                 return -1;
     701             :         }
     702       29029 :         nread += n;
     703             : 
     704       29029 :         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       29029 :         if (options) {
     709       29029 :                 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       29029 :                         *options = NULL;
     716             :                 }
     717             :         }
     718             :         return 1;
     719             : }
     720             : 
     721             : static gdk_return
     722         327 : 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         327 :         bat *hbats = NULL;
     730         327 :         bat nhbats = 0;
     731             : #endif
     732             : 
     733             :         /* read the BBP.dir and insert the BATs into the BBP */
     734         327 :         return_options = true;
     735         327 :         MT_lock_set(&BBPnameLock);
     736       27769 :         for (;;) {
     737       28096 :                 BAT b;
     738       28096 :                 Heap h;
     739       28096 :                 Heap vh;
     740       28096 :                 vh = h = (Heap) {
     741             :                         .free = 0,
     742             :                 };
     743       28096 :                 b = (BAT) {
     744             :                         .theap = &h,
     745             :                         .tvheap = &vh,
     746             :                 };
     747       28096 :                 char *options;
     748       28096 :                 char headname[129];
     749       28096 :                 char filename[sizeof(BBP_physical(0))];
     750       28096 :                 char logical[1024];
     751             : #ifdef GDKLIBRARY_HASHASH
     752       28096 :                 int Thashash;
     753             : #endif
     754             : 
     755       28096 :                 switch (BBPreadBBPline(fp, bbpversion, &lineno, &b,
     756             : #ifdef GDKLIBRARY_HASHASH
     757             :                                        &Thashash,
     758             : #endif
     759             :                                        headname, filename, &options)) {
     760         327 :                 case 0:
     761             :                         /* end of file */
     762             : #ifdef GDKLIBRARY_HASHASH
     763         327 :                         *hashbats = hbats;
     764         327 :                         *nhashbats = nhbats;
     765             : #endif
     766         327 :                         return_options = false;
     767         327 :                         MT_lock_unset(&BBPnameLock);
     768         327 :                         return GDK_SUCCEED;
     769             :                 case 1:
     770             :                         /* successfully read an entry */
     771       27769 :                         break;
     772           0 :                 default:
     773             :                         /* error */
     774           0 :                         goto bailout;
     775             :                 }
     776             : 
     777       27769 :                 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       27769 :                 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       27769 :                 BAT *bn = BBP_desc(b.batCacheid);
     792       27769 :                 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       27769 :                 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       27769 :                 Heap *hn;
     813       27769 :                 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       27769 :                 *bn = b;
     819       27769 :                 *hn = h;
     820       27769 :                 bn->theap = hn;
     821       27769 :                 if (b.tvheap) {
     822        7425 :                         Heap *vhn;
     823        7425 :                         assert(b.tvheap == &vh);
     824        7425 :                         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        7425 :                         *vhn = vh;
     831        7425 :                         bn->tvheap = vhn;
     832        7425 :                         ATOMIC_INIT(&bn->tvheap->refs, 1);
     833             :                 }
     834             : 
     835       27769 :                 char name[MT_NAME_LEN];
     836       27769 :                 snprintf(name, sizeof(name), "heaplock%d", bn->batCacheid); /* fits */
     837       27769 :                 MT_lock_init(&bn->theaplock, name);
     838       27769 :                 snprintf(name, sizeof(name), "BATlock%d", bn->batCacheid); /* fits */
     839       27769 :                 MT_lock_init(&bn->batIdxLock, name);
     840       27769 :                 snprintf(name, sizeof(name), "hashlock%d", bn->batCacheid); /* fits */
     841       27769 :                 MT_rwlock_init(&bn->thashlock, name);
     842       27769 :                 ATOMIC_INIT(&bn->theap->refs, 1);
     843             : 
     844       27769 :                 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       27769 :                 char *s;
     851       27769 :                 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       27769 :                         strcpy_len(logical, headname, sizeof(logical));
     859             :                 }
     860       27769 :                 if (strcmp(logical, BBP_bak(b.batCacheid)) == 0) {
     861       27093 :                         BBP_logical(b.batCacheid) = BBP_bak(b.batCacheid);
     862             :                 } else {
     863         676 :                         BBP_logical(b.batCacheid) = GDKstrdup(logical);
     864         676 :                         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       27769 :                 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       27769 :                 BBP_options(b.batCacheid) = options;
     877       27769 :                 BBP_refs(b.batCacheid) = 0;
     878       27769 :                 BBP_lrefs(b.batCacheid) = 1;    /* any BAT we encounter here is persistent, so has a logical reference */
     879       27769 :                 BBP_pid(b.batCacheid) = 0;
     880       27769 :                 BBP_status_set(b.batCacheid, BBPEXISTING);
     881       27769 :                 if (BBPnamecheck(BBP_logical(b.batCacheid)) == 0)
     882         676 :                         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         328 : BBPcheckbats(unsigned bbpversion)
     898             : {
     899         328 :         (void) bbpversion;
     900       72675 :         for (bat bid = 1, size = (bat) ATOMIC_GET(&BBPsize); bid < size; bid++) {
     901       72347 :                 struct stat statb;
     902       72347 :                 BAT *b;
     903       72347 :                 char *path;
     904             : 
     905       72347 :                 b = BBP_desc(bid);
     906       72347 :                 if (b->batCacheid == 0 || b->ttype == TYPE_void) {
     907             :                         /* no files needed */
     908       44578 :                         continue;
     909             :                 }
     910       27769 :                 if (b->theap->free > 0) {
     911       18623 :                         path = GDKfilepath(0, BATDIR, b->theap->filename, NULL);
     912       18623 :                         if (path == NULL)
     913           0 :                                 return GDK_FAIL;
     914             :                         /* first check string offset heap with width,
     915             :                          * then without */
     916       18623 :                         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       18623 :                         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       18623 :                         size_t hfree = b->theap->free;
     928       18623 :                         hfree = (hfree + GDK_mmap_pagesize - 1) & ~(GDK_mmap_pagesize - 1);
     929       18623 :                         if (hfree == 0)
     930           0 :                                 hfree = GDK_mmap_pagesize;
     931       18623 :                         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       18623 :                         GDKfree(path);
     940             :                 }
     941       27769 :                 if (b->tvheap != NULL && b->tvheap->free > 0) {
     942        5399 :                         path = GDKfilepath(0, BATDIR, BBP_physical(b->batCacheid), "theap");
     943        5399 :                         if (path == NULL)
     944             :                                 return GDK_FAIL;
     945        5399 :                         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        5399 :                         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        5399 :                         size_t hfree = b->tvheap->free;
     957        5399 :                         hfree = (hfree + GDK_mmap_pagesize - 1) & ~(GDK_mmap_pagesize - 1);
     958        5399 :                         if (hfree == 0)
     959           0 :                                 hfree = GDK_mmap_pagesize;
     960        5399 :                         if (statb.st_size > (off_t) hfree) {
     961           6 :                                 int fd;
     962           6 :                                 if ((fd = MT_open(path, O_RDWR | O_CLOEXEC | O_BINARY)) >= 0) {
     963           6 :                                         if (ftruncate(fd, hfree) == -1)
     964           0 :                                                 perror("ftruncate");
     965           6 :                                         (void) close(fd);
     966             :                                 }
     967             :                         }
     968        5399 :                         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         332 : BBPheader(FILE *fp, int *lineno, bat *bbpsize, lng *logno, bool allow_hge_upgrade)
     982             : {
     983         332 :         char buf[BUFSIZ];
     984         332 :         int sz, ptrsize, oidsize, intsize;
     985         332 :         unsigned bbpversion;
     986             : 
     987         332 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
     988           0 :                 TRC_CRITICAL(GDK, "BBP.dir is empty");
     989           0 :                 return 0;
     990             :         }
     991         332 :         ++*lineno;
     992         332 :         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         332 :         if (bbpversion != GDKLIBRARY &&
     999             :             bbpversion != GDKLIBRARY_STATUS &&
    1000             :             bbpversion != GDKLIBRARY_JSON &&
    1001         332 :             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         332 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
    1010           0 :                 TRC_CRITICAL(GDK, "short BBP");
    1011           0 :                 return 0;
    1012             :         }
    1013         332 :         ++*lineno;
    1014         332 :         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         332 :         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         332 :         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         332 :         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         332 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
    1038           0 :                 TRC_CRITICAL(GDK, "short BBP");
    1039           0 :                 return 0;
    1040             :         }
    1041         332 :         ++*lineno;
    1042         332 :         if (sscanf(buf, "BBPsize=%d", &sz) != 1) {
    1043           0 :                 TRC_CRITICAL(GDK, "no BBPsize value found\n");
    1044           0 :                 return 0;
    1045             :         }
    1046         332 :         if (sz > *bbpsize)
    1047         107 :                 *bbpsize = sz;
    1048         332 :         if (fgets(buf, sizeof(buf), fp) == NULL) {
    1049           0 :                 TRC_CRITICAL(GDK, "short BBP");
    1050           0 :                 return 0;
    1051             :         }
    1052         664 :         if (bbpversion <= GDKLIBRARY_STATUS ?
    1053           8 :             sscanf(buf, "BBPinfo=" LLSCN " %*d", logno) != 1 :
    1054         324 :             sscanf(buf, "BBPinfo=" LLSCN, logno) != 1) {
    1055           0 :                 TRC_CRITICAL(GDK, "no info value found\n");
    1056           0 :                 return 0;
    1057             :         }
    1058         332 :         return bbpversion;
    1059             : }
    1060             : 
    1061             : bool
    1062    90896415 : GDKinmemory(int farmid)
    1063             : {
    1064    90896415 :         if (farmid == NOFARM)
    1065             :                 farmid = 0;
    1066    89695348 :         assert(farmid >= 0 && farmid < MAXFARMS);
    1067    90896415 :         return BBPfarms[farmid].dirname == NULL;
    1068             : }
    1069             : 
    1070             : /* all errors are fatal */
    1071             : gdk_return
    1072         978 : BBPaddfarm(const char *dirname, uint32_t rolemask, bool logerror)
    1073             : {
    1074         978 :         struct stat st;
    1075         978 :         int i;
    1076             : 
    1077         978 :         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         977 :         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         977 :         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         977 :         if (strcmp(dirname, "in-memory") == 0 ||
    1095         976 :             /* backward compatibility: */ strcmp(dirname, ":memory:") == 0) {
    1096             :                 dirname = NULL;
    1097         976 :         } else if (MT_mkdir(dirname) < 0) {
    1098         888 :                 if (errno == EEXIST) {
    1099         888 :                         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        1943 :         for (i = 0; i < MAXFARMS; i++) {
    1111        1943 :                 if (BBPfarms[i].roles == 0) {
    1112         977 :                         if (dirname) {
    1113         976 :                                 BBPfarms[i].dirname = GDKstrdup(dirname);
    1114         976 :                                 if (BBPfarms[i].dirname == NULL)
    1115             :                                         return GDK_FAIL;
    1116             :                         }
    1117         977 :                         BBPfarms[i].roles = rolemask;
    1118         977 :                         if ((rolemask & 1) == 0 && dirname != NULL) {
    1119             :                                 char *bbpdir;
    1120             :                                 int j;
    1121             : 
    1122         971 :                                 for (j = 0; j < i; j++)
    1123         809 :                                         if (BBPfarms[j].dirname != NULL &&
    1124         809 :                                             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         492 :                         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         330 : BBPchkfarms(void)
    1164             : {
    1165         330 :         const char *dir = NULL;
    1166         330 :         uint32_t rolemask = 0;
    1167         330 :         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       10890 :         for (int i = 0; i < MAXFARMS; i++) {
    1172       10560 :                 if (BBPfarms[i].roles != 0) {
    1173         648 :                         dir = BBPfarms[i].dirname;
    1174         648 :                         rolemask |= BBPfarms[i].roles;
    1175             :                 }
    1176             :         }
    1177         330 :         if (dir == NULL)
    1178           1 :                 dir = "in-memory";
    1179         330 :         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         330 :         if ((rolemask & (1U << SYSTRANS)) == 0) {
    1185         330 :                 gdk_return rc = BBPaddfarm(dir, 1U << SYSTRANS, true);
    1186         330 :                 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          42 : BBPtrim(bool aggressive, bat nbat)
    1662             : {
    1663          42 :         int n = 0;
    1664          42 :         int waitctr = 0;
    1665          42 :         bool changed = false;
    1666          42 :         unsigned flag = BBPUNLOADING | BBPSYNCING | BBPSAVING;
    1667          42 :         if (!aggressive)
    1668          42 :                 flag |= BBPHOT;
    1669          42 :         lng t0 = GDKusec();
    1670       53892 :         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       53850 :                 if ((BBP_status(bid) & BBPLOADED) == 0)
    1674        9188 :                         continue;
    1675             :                 /* don't do this during a (sub)commit */
    1676       44662 :                 BBPtmlock();
    1677       44662 :                 MT_lock_set(&GDKswapLock(bid));
    1678       44662 :                 BAT *b = NULL;
    1679       44662 :                 bool swap = false;
    1680       44662 :                 if ((BBP_status(bid) & (flag | BBPLOADED)) == BBPLOADED &&
    1681       12489 :                     BBP_refs(bid) == 0 &&
    1682       12489 :                     BBP_lrefs(bid) != 0 &&
    1683       12489 :                     (b = BBP_desc(bid))->batCacheid != 0) {
    1684       12489 :                         MT_lock_set(&b->theaplock);
    1685       12489 :                         if (!BATshared(b) &&
    1686       12464 :                             !isVIEW(b) &&
    1687       12416 :                             (!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       10528 :                              (b->batRole == PERSISTENT &&
    1693       10496 :                               BBP_lrefs(bid) <= 2))) {
    1694       10062 :                                 BBP_status_on(bid, BBPUNLOADING);
    1695       10062 :                                 swap = true;
    1696       18236 :                                 waitctr += BATdirty(b) ? 9 : 1;
    1697             :                         }
    1698       12489 :                         MT_lock_unset(&b->theaplock);
    1699             :                 }
    1700       44662 :                 MT_lock_unset(&GDKswapLock(bid));
    1701       44662 :                 if (swap) {
    1702       10062 :                         TRC_DEBUG(BAT_, "unload and free bat %d\n", bid);
    1703       10062 :                         if (BBPfree(b) != GDK_SUCCEED)
    1704           0 :                                 GDKerror("unload failed for bat %d", bid);
    1705       10062 :                         n++;
    1706       10062 :                         changed = true;
    1707             :                 }
    1708       44662 :                 BBPtmunlock();
    1709             :                 /* every once in a while, give others a chance */
    1710       44662 :                 if (++waitctr >= 1000) {
    1711         108 :                         waitctr = 0;
    1712         108 :                         MT_sleep_ms(2);
    1713             :                 }
    1714             :         }
    1715          42 :         if (n > 0)
    1716          16 :                 TRC_INFO(BAT_, "unloaded %d bats in "LLFMT" usec%s\n", n, GDKusec() - t0, aggressive ? " (also hot)" : "");
    1717          42 :         return changed;
    1718             : }
    1719             : 
    1720             : static void
    1721         327 : BBPmanager(void *dummy)
    1722             : {
    1723         327 :         (void) dummy;
    1724         327 :         bool changed = true;
    1725             : 
    1726         369 :         for (;;) {
    1727         369 :                 int n = 0;
    1728         369 :                 bat nbat = (bat) ATOMIC_GET(&BBPsize);
    1729         369 :                 MT_thread_setworking("clearing HOT bits");
    1730      252776 :                 for (bat bid = 1; bid < nbat; bid++) {
    1731      252407 :                         MT_lock_set(&GDKswapLock(bid));
    1732      252407 :                         if (BBP_refs(bid) == 0 && BBP_lrefs(bid) != 0) {
    1733       93089 :                                 n += (BBP_status(bid) & BBPHOT) != 0;
    1734       93089 :                                 BBP_status_off(bid, BBPHOT);
    1735             :                         }
    1736      252407 :                         MT_lock_unset(&GDKswapLock(bid));
    1737             :                 }
    1738         369 :                 TRC_DEBUG(BAT_, "cleared HOT bit from %d bats\n", n);
    1739         369 :                 size_t cur = GDKvm_cursize();
    1740         369 :                 MT_thread_setworking("sleeping");
    1741        9883 :                 for (int i = 0, n = changed && cur > GDK_vm_maxsize / 2 ? 1 : cur > GDK_vm_maxsize / 4 ? 10 : 100; i < n; i++) {
    1742        9473 :                         MT_sleep_ms(100);
    1743        9471 :                         if (GDKexiting())
    1744             :                                 return;
    1745             :                 }
    1746          42 :                 MT_thread_setworking("BBPtrim");
    1747          42 :                 changed = BBPtrim(false, nbat);
    1748          42 :                 MT_thread_setworking("BBPcallbacks");
    1749          42 :                 BBPcallbacks();
    1750          42 :                 if (GDKexiting())
    1751             :                         return;
    1752             :         }
    1753             : }
    1754             : 
    1755             : static MT_Id manager;
    1756             : 
    1757             : gdk_return
    1758         328 : BBPinit(bool allow_hge_upgrade)
    1759             : {
    1760         328 :         FILE *fp = NULL;
    1761         328 :         struct stat st;
    1762         328 :         unsigned bbpversion = 0;
    1763         328 :         int i;
    1764         328 :         int lineno = 0;
    1765             : #ifdef GDKLIBRARY_HASHASH
    1766         328 :         bat *hashbats = NULL;
    1767         328 :         bat nhashbats = 0;
    1768         328 :         gdk_return res = GDK_SUCCEED;
    1769             : #endif
    1770         328 :         ATOMIC_BASE_TYPE dbg = ATOMIC_GET(&GDKdebug);
    1771             : 
    1772         328 :         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         328 :         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         328 :         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         328 :         if (!GDKinmemory(0)) {
    1784         327 :                 str bbpdirstr, backupbbpdirstr;
    1785             : 
    1786         327 :                 BBPtmlock();
    1787             : 
    1788         327 :                 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         327 :                 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         327 :                 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         327 :                 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         327 :                 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         327 :                 if (MT_stat(backupbbpdirstr, &st) == 0) {
    1833             :                         /* backup exists; *must* use it */
    1834         102 :                         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         102 :                         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         327 :                 GDKfree(bbpdirstr);
    1874         327 :                 GDKfree(backupbbpdirstr);
    1875         327 :                 BBPtmunlock();
    1876             :         }
    1877             : 
    1878             :         /* scan the BBP.dir to obtain current size */
    1879         328 :         BBPlimit = BBPINIT;
    1880         328 :         memset(BBP0, 0, sizeof(BBP0));
    1881         328 :         memset(BBP, 0, sizeof(BBP));
    1882         328 :         BBP[0] = BBP0;
    1883             : 
    1884         328 :         bat bbpsize;
    1885         328 :         bbpsize = 1;
    1886         328 :         if (GDKinmemory(0)) {
    1887             :                 bbpversion = GDKLIBRARY;
    1888             :         } else {
    1889         327 :                 lng logno;
    1890         327 :                 bbpversion = BBPheader(fp, &lineno, &bbpsize, &logno, allow_hge_upgrade);
    1891         327 :                 if (bbpversion == 0) {
    1892           0 :                         ATOMIC_SET(&GDKdebug, dbg);
    1893           0 :                         return GDK_FAIL;
    1894             :                 }
    1895         327 :                 ATOMIC_SET(&BBPlogno, logno);
    1896             :         }
    1897             : 
    1898             :         /* allocate BBP records */
    1899         328 :         if (BBPextend(bbpsize) != GDK_SUCCEED) {
    1900           0 :                 ATOMIC_SET(&GDKdebug, dbg);
    1901           0 :                 return GDK_FAIL;
    1902             :         }
    1903         328 :         ATOMIC_SET(&BBPsize, bbpsize);
    1904             : 
    1905         328 :         if (!GDKinmemory(0)) {
    1906         327 :                 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         327 :                 fclose(fp);
    1915             :         }
    1916             : 
    1917             :         /* remove trailing free bats from potential free list (they will
    1918             :          * get added when needed) */
    1919       29915 :         for (bat i = (bat) ATOMIC_GET(&BBPsize) - 1; i > 0; i--) {
    1920       29689 :                 if (BBP_desc(i)->batCacheid != 0)
    1921             :                         break;
    1922       29587 :                 bbpsize--;
    1923             :         }
    1924         328 :         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       72675 :         for (bat i = (bat) ATOMIC_GET(&BBPsize) - 1; i > 0; i--) {
    1929       72347 :                 if (BBP_desc(i)->batCacheid == 0) {
    1930       44578 :                         BBP_next(i) = BBP_free;
    1931       44578 :                         BBP_free = i;
    1932       44578 :                         BBP_nfree++;
    1933             :                 }
    1934             :         }
    1935             : 
    1936             :         /* will call BBPrecover if needed */
    1937         328 :         if (!GDKinmemory(0)) {
    1938         327 :                 BBPtmlock();
    1939         327 :                 gdk_return rc = BBPprepare(false);
    1940         327 :                 BBPtmunlock();
    1941         327 :                 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         328 :         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         328 :         if (nhashbats > 0)
    1961           0 :                 res = fixhashash(hashbats, nhashbats);
    1962         328 :         GDKfree(hashbats);
    1963         328 :         if (res != GDK_SUCCEED)
    1964             :                 return res;
    1965             : #endif
    1966             : 
    1967             : #ifdef GDKLIBRARY_JSON
    1968         328 :         if (bbpversion <= GDKLIBRARY_JSON) {
    1969           0 :                 char *jsonupgradestr;
    1970           0 :                 if (GDKinmemory(0)) {
    1971         328 :                         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         328 :         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         328 :         ATOMIC_SET(&GDKdebug, dbg);
    2005             : 
    2006             :         /* cleanup any leftovers (must be done after BBPrecover) */
    2007        1298 :         for (i = 0; i < MAXFARMS && BBPfarms[i].dirname != NULL; i++) {
    2008             :                 int j;
    2009        1294 :                 for (j = 0; j < i; j++) {
    2010             :                         /* don't clean a directory twice */
    2011         805 :                         if (BBPfarms[j].dirname &&
    2012         805 :                             strcmp(BBPfarms[i].dirname,
    2013             :                                    BBPfarms[j].dirname) == 0)
    2014             :                                 break;
    2015             :                 }
    2016         970 :                 if (j == i) {
    2017         489 :                         char *d = GDKfilepath(i, NULL, BATDIR, NULL);
    2018         489 :                         if (d == NULL) {
    2019             :                                 return GDK_FAIL;
    2020             :                         }
    2021         489 :                         BBPdiskscan(d, strlen(d) - strlen(BATDIR));
    2022         489 :                         GDKfree(d);
    2023             :                 }
    2024             :         }
    2025             : 
    2026         328 :         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         326 : BBPexit(void)
    2051             : {
    2052         326 :         bat i;
    2053         326 :         bool skipped;
    2054             : 
    2055             :         //BBPlock();    /* stop all threads ever touching more descriptors */
    2056             : 
    2057             :         /* free all memory (just for leak-checking in Purify) */
    2058         326 :         do {
    2059         326 :                 skipped = false;
    2060      762101 :                 for (i = 0; i < (bat) ATOMIC_GET(&BBPsize); i++) {
    2061      761775 :                         if (BBPvalid(i)) {
    2062      553139 :                                 BAT *b = BBP_desc(i);
    2063             : 
    2064      553139 :                                 if (b->batCacheid != 0) {
    2065      553139 :                                         if (BATshared(b)) {
    2066           0 :                                                 skipped = true;
    2067           0 :                                                 continue;
    2068             :                                         }
    2069      553139 :                                         MT_lock_set(&b->theaplock);
    2070      553139 :                                         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      553139 :                                         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      553139 :                                         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      553139 :                                         PROPdestroy_nolock(b);
    2089      553139 :                                         MT_lock_unset(&b->theaplock);
    2090      553139 :                                         BATfree(b);
    2091             :                                 }
    2092      553139 :                                 BBP_pid(i) = 0;
    2093      553139 :                                 BBPuncacheit(i, true);
    2094      553139 :                                 if (BBP_logical(i) != BBP_bak(i))
    2095       11679 :                                         GDKfree(BBP_logical(i));
    2096      553139 :                                 BBP_logical(i) = NULL;
    2097             :                         }
    2098             :                 }
    2099         326 :         } while (skipped);
    2100             :         /* these need to be NULL, otherwise no new ones get created */
    2101         326 :         memset(BBP_hash, 0, sizeof(BBP_hash));
    2102         326 :         backup_files = 0;
    2103         326 :         backup_dir = 0;
    2104         326 :         backup_subdir = 0;
    2105         326 :         if (lockfile) {
    2106         325 :                 GDKfree(lockfile);
    2107         325 :                 lockfile = NULL;
    2108             :         }
    2109         326 : }
    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     1704656 : heap_entry(FILE *fp, BATiter *bi, BUN size)
    2121             : {
    2122     1704656 :         size_t free = bi->hfree;
    2123     1704656 :         if (size < BUN_NONE) {
    2124     1704656 :                 if ((bi->type >= 0 && ATOMstorage(bi->type) == TYPE_msk))
    2125      268929 :                         free = ((size + 31) / 32) * 4;
    2126     1435727 :                 else if (bi->width > 0)
    2127     1435727 :                         free = size << bi->shift;
    2128             :                 else
    2129             :                         free = 0;
    2130             :         }
    2131             : 
    2132     5478412 :         return fprintf(fp, " %s %d %d %d " BUNFMT " " BUNFMT " " BUNFMT " "
    2133             :                        BUNFMT " " OIDFMT " %zu %" PRIu64" %" PRIu64,
    2134     1704656 :                        bi->type >= 0 ? BATatoms[bi->type].name : ATOMunknown_name(bi->type),
    2135     1704656 :                        bi->width,
    2136     1704656 :                        bi->type == TYPE_void || bi->vh != NULL,
    2137     1704656 :                        (unsigned short) bi->sorted |
    2138     1704656 :                            ((unsigned short) bi->revsorted << 7) |
    2139     3409312 :                            ((unsigned short) bi->key << 8) |
    2140     1704656 :                            ((unsigned short) BATtdensebi(bi) << 9) |
    2141     1704656 :                            ((unsigned short) bi->nonil << 10) |
    2142     1704656 :                            ((unsigned short) bi->nil << 11) |
    2143     1704656 :                            ((unsigned short) bi->ascii << 12),
    2144     1017472 :                        bi->nokey[0] >= size || bi->nokey[1] >= size ? 0 : bi->nokey[0],
    2145     1704656 :                        bi->nokey[0] >= size || bi->nokey[1] >= size ? 0 : bi->nokey[1],
    2146     1704656 :                        bi->nosorted >= size ? 0 : bi->nosorted,
    2147     1704656 :                        bi->norevsorted >= size ? 0 : bi->norevsorted,
    2148             :                        bi->tseq,
    2149             :                        free,
    2150     1704656 :                        bi->minpos < size ? (uint64_t) bi->minpos : (uint64_t) oid_nil,
    2151     1704656 :                        bi->maxpos < size ? (uint64_t) bi->maxpos : (uint64_t) oid_nil);
    2152             : }
    2153             : 
    2154             : static inline int
    2155     1704656 : vheap_entry(FILE *fp, BATiter *bi, BUN size)
    2156             : {
    2157     1704656 :         (void) size;
    2158     1704656 :         if (bi->vh == NULL)
    2159             :                 return 0;
    2160      402455 :         return fprintf(fp, " %zu", size == 0 ? 0 : bi->vhfree);
    2161             : }
    2162             : 
    2163             : static gdk_return
    2164     1704656 : new_bbpentry(FILE *fp, bat i, BUN size, BATiter *bi)
    2165             : {
    2166             : #ifndef NDEBUG
    2167     1704656 :         assert(i > 0);
    2168     1704656 :         assert(i < (bat) ATOMIC_GET(&BBPsize));
    2169     1704656 :         assert(bi->b);
    2170     1704656 :         assert(bi->b->batCacheid == i);
    2171     1704656 :         assert(bi->b->batRole == PERSISTENT);
    2172     1704656 :         assert(0 <= bi->h->farmid && bi->h->farmid < MAXFARMS);
    2173     1704656 :         assert(BBPfarms[bi->h->farmid].roles & (1U << PERSISTENT));
    2174     1704656 :         if (bi->vh) {
    2175      402455 :                 assert(0 <= bi->vh->farmid && bi->vh->farmid < MAXFARMS);
    2176      402455 :                 assert(BBPfarms[bi->vh->farmid].roles & (1U << PERSISTENT));
    2177             :         }
    2178     1704656 :         assert(size <= bi->count || size == BUN_NONE);
    2179     1704656 :         assert(BBP_options(i) == NULL || strpbrk(BBP_options(i), "\r\n") == NULL);
    2180             : #endif
    2181             : 
    2182     1704656 :         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     1704656 :         if (size > bi->count)
    2187             :                 size = bi->count;
    2188     1704656 :         if (fprintf(fp, "%d %s %d " BUNFMT " " OIDFMT,
    2189             :                     /* BAT info */
    2190             :                     (int) i,
    2191             :                     BBP_logical(i),
    2192     1704656 :                     (unsigned) bi->restricted << 1,
    2193             :                     size,
    2194     1704656 :                     bi->b->hseqbase) < 0 ||
    2195     3409312 :             heap_entry(fp, bi, size) < 0 ||
    2196     1704656 :             vheap_entry(fp, bi, size) < 0 ||
    2197     3409312 :             (BBP_options(i) && fprintf(fp, " %s", BBP_options(i)) < 0) ||
    2198     1704656 :             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       12288 : BBPdir_header(FILE *f, int n, lng logno)
    2208             : {
    2209       12288 :         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       12288 :                     , n, logno) < 0 ||
    2217       12288 :             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       12288 : BBPdir_first(bool subcommit, lng logno, FILE **obbpfp, FILE **nbbpfp)
    2226             : {
    2227       12288 :         FILE *obbpf = NULL, *nbbpf = NULL;
    2228       12288 :         int n = 0;
    2229       12288 :         lng ologno;
    2230             : 
    2231       12288 :         if (obbpfp)
    2232       12063 :                 *obbpfp = NULL;
    2233       12288 :         *nbbpfp = NULL;
    2234             : 
    2235       12288 :         if ((nbbpf = GDKfilelocate(0, "BBP", "w", "dir")) == NULL) {
    2236             :                 return GDK_FAIL;
    2237             :         }
    2238             : 
    2239       12288 :         if (subcommit) {
    2240       12055 :                 char buf[512];
    2241             : 
    2242       12055 :                 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       12055 :                 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       24110 :                 if (fgets(buf, sizeof(buf), obbpf) == NULL || /* BBP.dir, GDKversion %d */
    2252       24110 :                     fgets(buf, sizeof(buf), obbpf) == NULL || /* SIZEOF_SIZE_T SIZEOF_OID SIZEOF_MAX_INT */
    2253       12055 :                     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       12055 :                 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       12055 :                 if (fgets(buf, sizeof(buf), obbpf) == NULL ||
    2264       12055 :                     sscanf(buf, "BBPinfo=" LLSCN, &ologno) != 1) {
    2265           0 :                         GDKerror("cannot read BBPinfo in backup BBP.dir.");
    2266           0 :                         goto bailout;
    2267             :                 }
    2268             :         }
    2269             : 
    2270       12288 :         if (n < (bat) ATOMIC_GET(&BBPsize))
    2271        2638 :                 n = (bat) ATOMIC_GET(&BBPsize);
    2272             : 
    2273       12288 :         TRC_DEBUG(IO_, "writing BBP.dir (%d bats).\n", n);
    2274             : 
    2275       12288 :         if (BBPdir_header(nbbpf, n, logno) != GDK_SUCCEED) {
    2276           0 :                 goto bailout;
    2277             :         }
    2278             : 
    2279       12288 :         if (obbpfp)
    2280       12063 :                 *obbpfp = obbpf;
    2281       12288 :         *nbbpfp = nbbpf;
    2282             : 
    2283       12288 :         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     1897230 : BBPdir_step(bat bid, BUN size, int n, char *buf, size_t bufsize,
    2295             :             FILE **obbpfp, FILE *nbbpf, BATiter *bi)
    2296             : {
    2297     1897230 :         if (n < -1)          /* safety catch */
    2298             :                 return n;
    2299     4939243 :         while (n >= 0 && n < bid) {
    2300     3042013 :                 if (n > 0) {
    2301     1397772 :                         if (fputs(buf, nbbpf) == EOF) {
    2302           0 :                                 GDKerror("Writing BBP.dir file failed.\n");
    2303           0 :                                 goto bailout;
    2304             :                         }
    2305             :                 }
    2306     3042013 :                 if (fgets(buf, (int) bufsize, *obbpfp) == NULL) {
    2307        3009 :                         if (ferror(*obbpfp)) {
    2308           0 :                                 GDKerror("error reading backup BBP.dir.");
    2309           0 :                                 goto bailout;
    2310             :                         }
    2311        3009 :                         n = -1;
    2312        3009 :                         if (fclose(*obbpfp) == EOF) {
    2313           0 :                                 GDKsyserror("Closing backup BBP.dir file failed\n");
    2314           0 :                                 GDKclrerr(); /* ignore error */
    2315             :                         }
    2316        3009 :                         *obbpfp = NULL;
    2317             :                 } else {
    2318     3039004 :                         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     1897230 :         if (bi) {
    2325     1704656 :                 assert(BBP_status(bid) & BBPPERSISTENT);
    2326     1704656 :                 if (new_bbpentry(nbbpf, bid, size, bi) != GDK_SUCCEED)
    2327           0 :                         goto bailout;
    2328             :         }
    2329     1897230 :         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       12288 : BBPdir_last(int n, char *buf, size_t bufsize, FILE *obbpf, FILE *nbbpf)
    2340             : {
    2341       12288 :         if (n > 0 && fputs(buf, nbbpf) == EOF) {
    2342           0 :                 GDKerror("Writing BBP.dir file failed.\n");
    2343           0 :                 goto bailout;
    2344             :         }
    2345      311851 :         while (obbpf) {
    2346      308609 :                 if (fgets(buf, (int) bufsize, obbpf) == NULL) {
    2347        9046 :                         if (ferror(obbpf)) {
    2348           0 :                                 GDKerror("error reading backup BBP.dir.");
    2349           0 :                                 goto bailout;
    2350             :                         }
    2351        9046 :                         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      299563 :                         if (fputs(buf, nbbpf) == EOF) {
    2358           0 :                                 GDKerror("Writing BBP.dir file failed.\n");
    2359           0 :                                 goto bailout;
    2360             :                         }
    2361             :                 }
    2362             :         }
    2363       12288 :         if (fflush(nbbpf) == EOF ||
    2364       12288 :             (!(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       12288 :         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       12288 :         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       36417 : BBP_find(const char *nme, bool lock)
    2490             : {
    2491       36417 :         bat i = BBPnamecheck(nme);
    2492             : 
    2493       11271 :         if (i != 0) {
    2494             :                 /* for tmp_X BATs, we already know X */
    2495       11271 :                 const char *s;
    2496             : 
    2497       11271 :                 if (i >= (bat) ATOMIC_GET(&BBPsize) || (s = BBP_logical(i)) == NULL || strcmp(s, nme)) {
    2498       11271 :                         i = 0;
    2499             :                 }
    2500       25146 :         } else if (*nme != '.') {
    2501             :                 /* must lock since hash-lookup traverses other BATs */
    2502       25146 :                 if (lock)
    2503        1484 :                         MT_lock_set(&BBPnameLock);
    2504       25502 :                 for (i = BBP_hash[strHash(nme) & BBP_mask]; i; i = BBP_next(i)) {
    2505        1074 :                         if (strcmp(BBP_logical(i), nme) == 0)
    2506             :                                 break;
    2507             :                 }
    2508       25146 :                 if (lock)
    2509        1484 :                         MT_lock_unset(&BBPnameLock);
    2510             :         }
    2511       36417 :         return i;
    2512             : }
    2513             : 
    2514             : bat
    2515        1484 : BBPindex(const char *nme)
    2516             : {
    2517        1484 :         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       68957 : maybeextend(void)
    2541             : {
    2542       68957 :         bat size = (bat) ATOMIC_GET(&BBPsize);
    2543       68960 :         if (size + BBP_FREE_LOWATER > BBPlimit &&
    2544           3 :             BBPextend(size + BBP_FREE_LOWATER) != GDK_SUCCEED) {
    2545             :                 /* nothing available */
    2546             :                 return GDK_FAIL;
    2547             :         }
    2548       68957 :         ATOMIC_SET(&BBPsize, size + BBP_FREE_LOWATER);
    2549       68957 :         assert(BBP_free == 0);
    2550       68957 :         BBP_free = size;
    2551      689570 :         for (int i = 1; i < BBP_FREE_LOWATER; i++) {
    2552      620613 :                 bat sz = size;
    2553      620613 :                 BBP_next(sz) = ++size;
    2554             :         }
    2555       68957 :         BBP_next(size) = 0;
    2556       68957 :         BBP_nfree += BBP_FREE_LOWATER;
    2557       68957 :         return GDK_SUCCEED;
    2558             : }
    2559             : 
    2560             : /* return new BAT id (> 0); return 0 on failure */
    2561             : bat
    2562    23447065 : BBPallocbat(int tt)
    2563             : {
    2564    23447065 :         MT_Id pid = MT_getpid();
    2565    23458549 :         bool lock = locked_by == 0 || locked_by != pid;
    2566    23458549 :         bat i;
    2567    23458549 :         int len = 0;
    2568    23458549 :         struct freebats *t = MT_thread_getfreebats();
    2569             : 
    2570    23491947 :         if (t->freebats == 0) {
    2571             :                 /* critical section: get a new BBP entry */
    2572      246407 :                 assert(t->nfreebats == 0);
    2573      246407 :                 if (lock) {
    2574      246407 :                         MT_lock_set(&GDKcacheLock);
    2575             :                 }
    2576             : 
    2577             :                 /* get a global bat, perhaps extend */
    2578      246524 :                 if (BBP_free <= 0) {
    2579             :                         /* we need to extend the BBP */
    2580       68957 :                         gdk_return r;
    2581       68957 :                         r = maybeextend();
    2582       68957 :                         if (r != GDK_SUCCEED) {
    2583           0 :                                 if (lock) {
    2584           0 :                                         MT_lock_unset(&GDKcacheLock);
    2585             :                                 }
    2586             :                                 /* failed */
    2587           0 :                                 return 0;
    2588             :                         }
    2589             :                 }
    2590      246524 :                 t->freebats = i = BBP_free;
    2591      246524 :                 bat l = 0;
    2592     2676096 :                 for (int x = 0; x < BBP_FREE_LOWATER && i; x++) {
    2593     2429572 :                         assert(BBP_next(i) == 0 || BBP_next(i) > i);
    2594     2429572 :                         t->nfreebats++;
    2595     2429572 :                         BBP_nfree--;
    2596     2429572 :                         l = i;
    2597     2429572 :                         i = BBP_next(i);
    2598             :                 }
    2599      246524 :                 BBP_next(l) = 0;
    2600      246524 :                 BBP_free = i;
    2601             : 
    2602      246524 :                 if (lock) {
    2603      246524 :                         MT_lock_unset(&GDKcacheLock);
    2604             :                 }
    2605             :                 /* rest of the work outside the lock */
    2606             :         }
    2607    23492063 :         if (t->nfreebats > 0) {
    2608    23492063 :                 assert(t->freebats > 0);
    2609    23492063 :                 i = t->freebats;
    2610    23492063 :                 t->freebats = BBP_next(i);
    2611    23492063 :                 assert(t->freebats == 0 || t->freebats > i);
    2612    23492063 :                 BBP_next(i) = 0;
    2613    23492063 :                 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    23492063 :         MT_lock_set(&GDKswapLock(i));
    2623    23514341 :         BBP_status_set(i, BBPDELETING|BBPHOT);
    2624    23514341 :         BBP_refs(i) = 1;        /* new bats have 1 pin */
    2625    23514341 :         BBP_lrefs(i) = 0;       /* ie. no logical refs */
    2626    23514341 :         BBP_pid(i) = pid;
    2627    23514341 :         MT_lock_unset(&GDKswapLock(i));
    2628             : 
    2629    23559004 :         if (*BBP_bak(i) == 0)
    2630      717910 :                 len = snprintf(BBP_bak(i), sizeof(BBP_bak(i)), "tmp_%o", (unsigned) i);
    2631    23559004 :         if (len == -1 || len >= FILENAME_MAX) {
    2632           0 :                 GDKerror("impossible error\n");
    2633           0 :                 return 0;
    2634             :         }
    2635    23559004 :         BBP_logical(i) = BBP_bak(i);
    2636             : 
    2637             :         /* Keep the physical location around forever */
    2638    23559004 :         if (!GDKinmemory(0) && *BBP_physical(i) == 0) {
    2639      718666 :                 BBPgetfilename(BBP_physical(i), sizeof(BBP_physical(i)), i);
    2640      718987 :                 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    23506551 : BBPcacheit(BAT *bn, bool lock)
    2648             : {
    2649    23506551 :         bat i = bn->batCacheid;
    2650    23506551 :         unsigned mode;
    2651             : 
    2652    23506551 :         if (lock)
    2653    46935748 :                 lock = locked_by == 0 || locked_by != MT_getpid();
    2654             : 
    2655    23506551 :         assert(i > 0);
    2656             : 
    2657    23506551 :         if (lock)
    2658    23501416 :                 MT_lock_set(&GDKswapLock(i));
    2659    23508199 :         mode = (BBP_status(i) | BBPLOADED) & ~(BBPLOADING | BBPDELETING | BBPSWAPPED);
    2660             : 
    2661             :         /* cache it! */
    2662    23508199 :         BBP_status_set(i, mode);
    2663             : 
    2664    23508199 :         if (lock)
    2665    23560683 :                 MT_lock_unset(&GDKswapLock(i));
    2666    23501936 :         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    23495675 : BBPuncacheit(bat i, bool unloaddesc)
    2677             : {
    2678    23495675 :         if (i < 0)
    2679             :                 i = -i;
    2680    23495675 :         if (BBPcheck(i)) {
    2681    23492585 :                 BAT *b = BBP_desc(i);
    2682             : 
    2683    23492585 :                 assert(unloaddesc || BBP_refs(i) == 0);
    2684             : 
    2685    23492585 :                 if (BBP_status(i) & BBPLOADED) {
    2686    23465706 :                         TRC_DEBUG(BAT_, "uncache %d (%s)\n", (int) i, BBP_logical(i));
    2687             : 
    2688             :                         /* clearing bits can be done without the lock */
    2689    23465706 :                         BBP_status_off(i, BBPLOADED);
    2690             :                 }
    2691    23492585 :                 if (unloaddesc) {
    2692    23597910 :                         BATdestroy(b);
    2693             :                 }
    2694             :         }
    2695    23407991 : }
    2696             : 
    2697             : /*
    2698             :  * @- BBPclear
    2699             :  * BBPclear removes a BAT from the BBP directory forever.
    2700             :  */
    2701             : static inline void
    2702       76544 : BBPhandover(struct freebats *t, uint32_t n)
    2703             : {
    2704       76544 :         bat *p, bid;
    2705             :         /* take one bat from our private free list and hand it over to
    2706             :          * the global free list */
    2707       76544 :         if (n >= t->nfreebats) {
    2708       42280 :                 bid = t->freebats;
    2709       42280 :                 t->freebats = 0;
    2710       42280 :                 BBP_nfree += t->nfreebats;
    2711       42280 :                 t->nfreebats = 0;
    2712             :         } else {
    2713       34264 :                 p = &t->freebats;
    2714      376904 :                 for (uint32_t i = n; i < t->nfreebats; i++)
    2715      342640 :                         p = &BBP_next(*p);
    2716       34264 :                 bid = *p;
    2717       34264 :                 *p = 0;
    2718       34264 :                 BBP_nfree += n;
    2719       34264 :                 t->nfreebats -= n;
    2720             :         }
    2721             :         p = &BBP_free;
    2722     1975983 :         while (bid != 0) {
    2723    31256895 :                 while (*p && *p < bid)
    2724    29357456 :                         p = &BBP_next(*p);
    2725     1899439 :                 bat i = BBP_next(bid);
    2726     1899439 :                 BBP_next(bid) = *p;
    2727     1899439 :                 *p = bid;
    2728     1899439 :                 bid = i;
    2729             :         }
    2730       76544 : }
    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    22929495 : bbpclear(bat i, bool lock)
    2751             : {
    2752    22929495 :         struct freebats *t = MT_thread_getfreebats();
    2753             : 
    2754    22929587 :         TRC_DEBUG(BAT_, "clear %d (%s)\n", (int) i, BBP_logical(i));
    2755    22929587 :         BBPuncacheit(i, true);
    2756    22854190 :         TRC_DEBUG(BAT_, "set to unloading %d\n", i);
    2757    22854190 :         if (lock) {
    2758    22884951 :                 MT_lock_set(&GDKswapLock(i));
    2759             :         }
    2760             : 
    2761    22948299 :         BBP_status_set(i, BBPUNLOADING);
    2762    22948299 :         BBP_refs(i) = 0;
    2763    22948299 :         BBP_lrefs(i) = 0;
    2764    22948299 :         if (lock)
    2765    23032241 :                 MT_lock_unset(&GDKswapLock(i));
    2766    22954623 :         if (!BBPtmpcheck(BBP_logical(i))) {
    2767        1314 :                 MT_lock_set(&BBPnameLock);
    2768        1314 :                 BBP_delete(i);
    2769        1314 :                 MT_lock_unset(&BBPnameLock);
    2770             :         }
    2771    22954623 :         if (BBP_logical(i) != BBP_bak(i))
    2772        1314 :                 GDKfree(BBP_logical(i));
    2773    23045857 :         BBP_status_set(i, 0);
    2774    23045857 :         BBP_logical(i) = NULL;
    2775    23045857 :         bat *p;
    2776    70831766 :         for (p = &t->freebats; *p && *p < i; p = &BBP_next(*p))
    2777             :                 ;
    2778    23045857 :         BBP_next(i) = *p;
    2779    23045857 :         *p = i;
    2780    23045857 :         t->nfreebats++;
    2781    23045857 :         BBP_pid(i) = ~(MT_Id)0; /* not zero, not a valid thread id */
    2782    23045857 :         if (t->nfreebats > BBP_FREE_HIWATER) {
    2783       34262 :                 if (lock)
    2784       34262 :                         MT_lock_set(&GDKcacheLock);
    2785       34264 :                 BBPhandover(t, t->nfreebats - BBP_FREE_LOWATER);
    2786       34264 :                 if (lock)
    2787       34264 :                         MT_lock_unset(&GDKcacheLock);
    2788             :         }
    2789    23045859 : }
    2790             : 
    2791             : void
    2792    22952297 : BBPclear(bat i)
    2793             : {
    2794    22952297 :         if (BBPcheck(i)) {
    2795    22928956 :                 bool lock = locked_by == 0 || locked_by != MT_getpid();
    2796    22928956 :                 bbpclear(i, lock);
    2797             :         }
    2798    23019833 : }
    2799             : 
    2800             : void
    2801       46489 : BBPrelinquishbats(void)
    2802             : {
    2803       46489 :         struct freebats *t = MT_thread_getfreebats();
    2804       46535 :         if (t == NULL || t->nfreebats == 0)
    2805             :                 return;
    2806       42209 :         MT_lock_set(&GDKcacheLock);
    2807       84560 :         while (t->nfreebats > 0) {
    2808       42280 :                 BBPhandover(t, t->nfreebats);
    2809             :         }
    2810       42280 :         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       34933 : BBPrename(BAT *b, const char *nme)
    2836             : {
    2837       34933 :         if (b == NULL)
    2838             :                 return 0;
    2839             : 
    2840       34933 :         bat bid = b->batCacheid;
    2841       34933 :         bat tmpid = 0, i;
    2842             : 
    2843       34933 :         if (nme == NULL) {
    2844       11271 :                 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       11271 :                 nme = BBP_bak(bid);
    2851             :         }
    2852             : 
    2853             :         /* If name stays same, do nothing */
    2854       34933 :         if (BBP_logical(bid) && strcmp(BBP_logical(bid), nme) == 0)
    2855             :                 return 0;
    2856             : 
    2857       34933 :         if ((tmpid = BBPnamecheck(nme)) && tmpid != bid) {
    2858           0 :                 GDKerror("illegal temporary name: '%s'\n", nme);
    2859           0 :                 return BBPRENAME_ILLEGAL;
    2860             :         }
    2861       34933 :         if (strLen(nme) >= IDLENGTH) {
    2862           0 :                 GDKerror("illegal temporary name: '%s'\n", nme);
    2863           0 :                 return BBPRENAME_LONG;
    2864             :         }
    2865             : 
    2866       34933 :         MT_lock_set(&BBPnameLock);
    2867       34933 :         i = BBP_find(nme, false);
    2868       34933 :         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       34932 :         char *nnme;
    2875       34932 :         if (nme == BBP_bak(bid) || strcmp(nme, BBP_bak(bid)) == 0) {
    2876       34932 :                 nnme = BBP_bak(bid);
    2877             :         } else {
    2878       23661 :                 nnme = GDKstrdup(nme);
    2879       23661 :                 if (nnme == NULL) {
    2880           0 :                         MT_lock_unset(&BBPnameLock);
    2881           0 :                         return BBPRENAME_MEMORY;
    2882             :                 }
    2883             :         }
    2884             : 
    2885             :         /* carry through the name change */
    2886       34932 :         if (BBP_logical(bid) && !BBPtmpcheck(BBP_logical(bid))) {
    2887       11271 :                 BBP_delete(bid);
    2888             :         }
    2889       34932 :         if (BBP_logical(bid) != BBP_bak(bid))
    2890       11271 :                 GDKfree(BBP_logical(bid));
    2891       34932 :         BBP_logical(bid) = nnme;
    2892       34932 :         if (tmpid == 0) {
    2893       23661 :                 BBP_insert(bid);
    2894             :         }
    2895       34932 :         MT_lock_set(&b->theaplock);
    2896       34932 :         bool transient = b->batTransient;
    2897       34932 :         MT_lock_unset(&b->theaplock);
    2898       34932 :         if (!transient) {
    2899        8397 :                 bool lock = locked_by == 0 || locked_by != MT_getpid();
    2900             : 
    2901        8397 :                 if (lock)
    2902        8397 :                         MT_lock_set(&GDKswapLock(i));
    2903        8397 :                 BBP_status_on(bid, BBPRENAMED);
    2904        8397 :                 if (lock)
    2905        8397 :                         MT_lock_unset(&GDKswapLock(i));
    2906             :         }
    2907       34932 :         MT_lock_unset(&BBPnameLock);
    2908       34932 :         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     3970953 : BBPspin(bat i, const char *s, unsigned event)
    2924             : {
    2925     3970953 :         if (BBPcheck(i) && (BBP_status(i) & event)) {
    2926             :                 lng spin = LL_CONSTANT(0);
    2927             : 
    2928          81 :                 do {
    2929          81 :                         MT_sleep_ms(KITTENNAP);
    2930          81 :                         spin++;
    2931          81 :                 } while (BBP_status(i) & event);
    2932           5 :                 TRC_DEBUG(BAT_, "%d,%s,%u: " LLFMT " loops\n", (int) i, s, event, spin);
    2933             :         }
    2934     3970550 : }
    2935             : 
    2936             : void
    2937    11956498 : BBPcold(bat i)
    2938             : {
    2939    11956498 :         if (!is_bat_nil(i)) {
    2940    11967096 :                 BAT *b = BBP_desc(i);
    2941    11967096 :                 if (b->batRole == PERSISTENT)
    2942         701 :                         BBP_status_off(i, BBPHOT);
    2943             :         }
    2944    11956498 : }
    2945             : 
    2946             : /* This function can fail if the input parameter (i) is incorrect
    2947             :  * (unlikely). */
    2948             : static inline int
    2949   136041076 : incref(bat i, bool logical, bool lock)
    2950             : {
    2951   136041076 :         int refs;
    2952   136041076 :         BAT *b;
    2953             : 
    2954   136041076 :         if (!BBPcheck(i))
    2955             :                 return 0;
    2956             : 
    2957   136259791 :         if (lock) {
    2958    42724979 :                 for (;;) {
    2959    42724979 :                         MT_lock_set(&GDKswapLock(i));
    2960    43976653 :                         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   137511465 :         b = BBP_desc(i);
    2970   137511465 :         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   137511465 :         assert(BBP_refs(i) + BBP_lrefs(i) ||
    2978             :                BBP_status(i) & (BBPDELETED | BBPSWAPPED));
    2979   137511465 :         if (logical) {
    2980    42880059 :                 refs = ++BBP_lrefs(i);
    2981    42880059 :                 BBP_pid(i) = 0;
    2982             :         } else {
    2983    94631406 :                 refs = ++BBP_refs(i);
    2984    94631406 :                 BBP_status_on(i, BBPHOT);
    2985             :         }
    2986   137511465 :         if (lock)
    2987    42872741 :                 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      112762 : BBPfix(bat i)
    2998             : {
    2999      112762 :         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    34069670 : BBPretain(bat i)
    3006             : {
    3007    34069670 :         bool lock = locked_by == 0 || locked_by != MT_getpid();
    3008             : 
    3009    34069670 :         return incref(i, true, lock);
    3010             : }
    3011             : 
    3012             : static inline int
    3013   171088463 : decref(bat i, bool logical, bool lock, const char *func)
    3014             : {
    3015   171088463 :         int refs = 0, lrefs;
    3016   171088463 :         bool swap = false;
    3017   171088463 :         bool locked = false;
    3018   171088463 :         int farmid = 0;
    3019   171088463 :         BAT *b;
    3020             : 
    3021   171088463 :         if (is_bat_nil(i))
    3022             :                 return -1;
    3023   158095131 :         assert(i > 0);
    3024   158095131 :         if (BBPcheck(i) == 0)
    3025             :                 return -1;
    3026             : 
    3027   157979539 :         if (lock)
    3028   157924394 :                 MT_lock_set(&GDKswapLock(i));
    3029             : 
    3030   158843905 :         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   158843905 :                         MT_lock_set(&GDKswapLock(i));
    3036             :         }
    3037             : 
    3038   159418876 :         b = (BBP_status(i) & BBPLOADED) ? BBP_desc(i) : NULL;
    3039             : 
    3040             :         /* decrement references by one */
    3041   159418876 :         if (logical) {
    3042    42833830 :                 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    42833830 :                         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   116585046 :                 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   116585046 :                         refs = --BBP_refs(i);
    3057   116585046 :                         if (b && refs == 0) {
    3058    95625155 :                                 MT_lock_set(&b->theaplock);
    3059    95978810 :                                 locked = true;
    3060    95978810 :                                 if (VIEWtparent(b) || VIEWvtparent(b))
    3061    18370618 :                                         BBP_status_on(i, BBPHOT);
    3062             :                         }
    3063             :                 }
    3064             :         }
    3065   159772531 :         if (b) {
    3066   158749468 :                 if (!locked) {
    3067    63860863 :                         MT_lock_set(&b->theaplock);
    3068    64100335 :                         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   158794257 :                 if (b->theap)
    3084   158794257 :                         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   159817320 :         unsigned chkflag = BBPSYNCING;
    3090   159817320 :         bool swapdirty = false;
    3091   159817320 :         if (b) {
    3092   158895041 :                 size_t cursize;
    3093   158895041 :                 if ((cursize = GDKvm_cursize()) < (size_t) (GDK_vm_maxsize * 0.75)) {
    3094   158869066 :                         if (!locked) {
    3095           0 :                                 MT_lock_set(&b->theaplock);
    3096           0 :                                 locked = true;
    3097             :                         }
    3098   158869066 :                         if (((b->theap ? b->theap->size : 0) + (b->tvheap ? b->tvheap->size : 0)) < (GDK_vm_maxsize - cursize) / 32)
    3099   158976113 :                                 chkflag |= BBPHOT;
    3100           0 :                 } else if (cursize > (size_t) (GDK_vm_maxsize * 0.85))
    3101   159791345 :                         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   272802310 :         if (BBP_refs(i) == 0 &&
    3107   136021918 :             (BBP_lrefs(i) == 0 ||
    3108   112984856 :              (b != NULL && b->theap != NULL
    3109   112989974 :               ? ((swapdirty || !BATdirty(b)) &&
    3110    12428791 :                  !(BBP_status(i) & chkflag) &&
    3111       10018 :                  (BBP_status(i) & BBPPERSISTENT) &&
    3112             :                  /* cannot unload in-memory data */
    3113        4922 :                  !GDKinmemory(farmid) &&
    3114             :                  /* do not unload views or parents of views */
    3115        4922 :                  !BATshared(b) &&
    3116   112989743 :                  b->batCacheid == b->theap->parentid &&
    3117        4887 :                  (b->tvheap == NULL || b->batCacheid == b->tvheap->parentid))
    3118       26109 :               : (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    23019291 :                 assert((BBP_status(i) & BBPUNLOADING) == 0);
    3123    23019291 :                 TRC_DEBUG(BAT_, "%s set to unloading BAT %d (status %u, lrefs %d)\n", func, i, BBP_status(i), BBP_lrefs(i));
    3124    23019291 :                 BBP_status_on(i, BBPUNLOADING);
    3125    23019291 :                 swap = true;
    3126             :         } /* else: bat cannot be swapped out */
    3127   159791345 :         lrefs = BBP_lrefs(i);
    3128   159791345 :         if (locked)
    3129   159039085 :                 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   160605378 :         if (lock)
    3134   159933864 :                 MT_lock_unset(&GDKswapLock(i));
    3135             : 
    3136   160633630 :         if (swap) {
    3137    23045964 :                 if (b != NULL) {
    3138    23031542 :                         if (lrefs == 0 && (BBP_status(i) & BBPDELETED) == 0) {
    3139             :                                 /* free memory (if loaded) and delete from
    3140             :                                  * disk (if transient but saved) */
    3141    23028404 :                                 BBPdestroy(b);
    3142             :                         } else {
    3143        3138 :                                 TRC_DEBUG(BAT_, "%s unload and free bat %d\n", func, i);
    3144             :                                 /* free memory of transient */
    3145        3138 :                                 if (BBPfree(b) != GDK_SUCCEED)
    3146             :                                         return -1;      /* indicate failure */
    3147             :                         }
    3148       14422 :                 } else if (lrefs == 0 && (BBP_status(i) & BBPDELETED) == 0) {
    3149       10971 :                         BATdelete(BBP_desc(i));
    3150       10971 :                         BBPclear(i);
    3151             :                 } else {
    3152        3451 :                         BBP_status_off(i, BBPUNLOADING);
    3153             :                 }
    3154             :         }
    3155             :         return refs;
    3156             : }
    3157             : 
    3158             : int
    3159   110394034 : BBPunfix(bat i)
    3160             : {
    3161   110394034 :         return decref(i, false, true, __func__);
    3162             : }
    3163             : 
    3164             : int
    3165    55700211 : BBPrelease(bat i)
    3166             : {
    3167    55700211 :         return decref(i, true, true, __func__);
    3168             : }
    3169             : 
    3170             : void
    3171     8798093 : BBPkeepref(BAT *b)
    3172             : {
    3173     8798093 :         assert(b != NULL);
    3174     8798093 :         bool lock = locked_by == 0 || locked_by != MT_getpid();
    3175     8798093 :         int i = b->batCacheid;
    3176     8798093 :         int refs = incref(i, true, lock);
    3177     8858365 :         if (refs == 1) {
    3178     8534520 :                 MT_lock_set(&b->theaplock);
    3179     8516747 :                 BATsettrivprop(b);
    3180     8509252 :                 MT_lock_unset(&b->theaplock);
    3181             :         }
    3182     8863125 :         if (ATOMIC_GET(&GDKdebug) & CHECKMASK)
    3183     8806088 :                 BATassertProps(b);
    3184     8853116 :         if (BATsetaccess(b, BAT_READ) == NULL)
    3185             :                 return;         /* already decreffed */
    3186             : 
    3187     6457638 :         refs = decref(i, false, lock, __func__);
    3188     6439722 :         (void) refs;
    3189     6439722 :         assert(refs >= 0);
    3190             : }
    3191             : 
    3192             : BAT *
    3193    94472617 : BATdescriptor(bat i)
    3194             : {
    3195    94472617 :         BAT *b = NULL;
    3196             : 
    3197    94472617 :         if (BBPcheck(i)) {
    3198    94448325 :                 bool lock = locked_by == 0 || locked_by != MT_getpid();
    3199             :                 if (lock) {
    3200    94448325 :                         for (;;) {
    3201    94448325 :                                 MT_lock_set(&GDKswapLock(i));
    3202    94634714 :                                 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    94634714 :                 if (incref(i, false, false) > 0) {
    3210    95128695 :                         if ((BBP_status(i) & BBPLOADED) == 0) {
    3211       18918 :                                 b = getBBPdescriptor(i);
    3212       18918 :                                 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    95109777 :                                 b = BBP_desc(i);
    3219             :                         }
    3220             :                 }
    3221    95128695 :                 if (lock)
    3222    95127133 :                         MT_lock_unset(&GDKswapLock(i));
    3223             :         }
    3224    95156715 :         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       18918 : getBBPdescriptor(bat i)
    3234             : {
    3235       18918 :         bool load = false;
    3236       18918 :         BAT *b = NULL;
    3237             : 
    3238       18918 :         assert(i > 0);
    3239       18918 :         if (!BBPcheck(i)) {
    3240           0 :                 GDKerror("BBPcheck failed for bat id %d\n", i);
    3241           0 :                 return NULL;
    3242             :         }
    3243       18918 :         assert(BBP_refs(i));
    3244       18918 :         unsigned status = BBP_status(i);
    3245       18918 :         b = BBP_desc(i);
    3246       18918 :         if ((status & BBPLOADED) == 0 || status & BBPWAITING) {
    3247       18917 :                 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       18918 :                         MT_lock_set(&GDKswapLock(i));
    3251             :                 }
    3252       18917 :                 if (BBPvalid(i)) {
    3253       18917 :                         if ((BBP_status(i) & BBPLOADED) == 0) {
    3254       18917 :                                 load = true;
    3255       18917 :                                 TRC_DEBUG(BAT_, "set to loading BAT %d\n", i);
    3256       18917 :                                 BBP_status_on(i, BBPLOADING);
    3257             :                         }
    3258             :                 }
    3259             :         }
    3260       18917 :         if (load) {
    3261       18917 :                 TRC_DEBUG(IO_, "load %s\n", BBP_logical(i));
    3262             : 
    3263       18917 :                 b = BATload_intern(i, false);
    3264             : 
    3265       18918 :                 BBP_status_off(i, BBPLOADING);
    3266       18918 :                 CHECKDEBUG if (b != NULL)
    3267       16213 :                         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       15089 : BBPsave(BAT *b)
    3279             : {
    3280       15089 :         bool lock = locked_by == 0 || locked_by != MT_getpid();
    3281       15089 :         bat bid = b->batCacheid;
    3282       15089 :         gdk_return ret = GDK_SUCCEED;
    3283             : 
    3284       15089 :         MT_lock_set(&b->theaplock);
    3285       15089 :         if (BBP_lrefs(bid) == 0 || isVIEW(b) || !BATdirty(b)) {
    3286             :                 /* do nothing */
    3287        6915 :                 MT_lock_unset(&b->theaplock);
    3288        6915 :                 MT_rwlock_rdlock(&b->thashlock);
    3289        6915 :                 if (b->thash && b->thash != (Hash *) 1 &&
    3290         191 :                     (b->thash->heaplink.dirty || b->thash->heapbckt.dirty))
    3291         119 :                         BAThashsave(b, (BBP_status(bid) & BBPPERSISTENT) != 0);
    3292        6915 :                 MT_rwlock_rdunlock(&b->thashlock);
    3293        6915 :                 return GDK_SUCCEED;
    3294             :         }
    3295        8174 :         MT_lock_unset(&b->theaplock);
    3296        8174 :         if (lock)
    3297        8174 :                 MT_lock_set(&GDKswapLock(bid));
    3298             : 
    3299        8174 :         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        8174 :                 unsigned flags = BBPSAVING;
    3307             : 
    3308        8174 :                 MT_lock_set(&b->theaplock);
    3309        8174 :                 if (DELTAdirty(b)) {
    3310         477 :                         flags |= BBPSWAPPED;
    3311             :                 }
    3312        8174 :                 if (b->batTransient) {
    3313        8171 :                         flags |= BBPTMP;
    3314             :                 }
    3315        8174 :                 MT_lock_unset(&b->theaplock);
    3316        8174 :                 BBP_status_on(bid, flags);
    3317        8174 :                 if (lock)
    3318        8174 :                         MT_lock_unset(&GDKswapLock(bid));
    3319             : 
    3320        8174 :                 TRC_DEBUG(IO_, "save " ALGOBATFMT "\n", ALGOBATPAR(b));
    3321             : 
    3322             :                 /* do the time-consuming work unlocked */
    3323        8174 :                 if (BBP_status(bid) & BBPEXISTING && b->batInserted > 0)
    3324           0 :                         ret = BBPbackup(b, false);
    3325           0 :                 if (ret == GDK_SUCCEED) {
    3326        8174 :                         ret = BATsave(b);
    3327             :                 }
    3328             :                 /* clearing bits can be done without the lock */
    3329        8174 :                 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    23009538 : BBPdestroy(BAT *b)
    3341             : {
    3342    23009538 :         bat tp = VIEWtparent(b);
    3343    23009538 :         bat vtp = VIEWvtparent(b);
    3344             : 
    3345    23009538 :         if (b->theap) {
    3346    23017074 :                 HEAPdecref(b->theap, tp == 0);
    3347    22999045 :                 b->theap = NULL;
    3348    22999045 :                 if (tp != 0)
    3349    11444467 :                         BBPrelease(tp);
    3350             :         }
    3351    22990006 :         if (b->tvheap) {
    3352     3594021 :                 HEAPdecref(b->tvheap, vtp == 0);
    3353     3593391 :                 b->tvheap = NULL;
    3354     3593391 :                 if (vtp != 0)
    3355     2460815 :                         BBPrelease(vtp);
    3356             :         }
    3357    22989237 :         if (b->oldtail) {
    3358           2 :                 ATOMIC_AND(&b->oldtail->refs, ~DELAYEDREMOVE);
    3359           2 :                 HEAPdecref(b->oldtail, true);
    3360           2 :                 b->oldtail = NULL;
    3361             :         }
    3362    22989237 :         BATdelete(b);
    3363             : 
    3364    22997388 :         BBPclear(b->batCacheid);     /* if destroyed; de-register from BBP */
    3365    23009001 : }
    3366             : 
    3367             : static gdk_return
    3368       15089 : BBPfree(BAT *b)
    3369             : {
    3370       15089 :         bat bid = b->batCacheid;
    3371       15089 :         gdk_return ret;
    3372             : 
    3373       15089 :         assert(bid > 0);
    3374       15089 :         assert(BBPswappable(b));
    3375       15089 :         assert(!isVIEW(b));
    3376             : 
    3377       15089 :         BBP_unload_inc();
    3378             :         /* write dirty BATs before unloading */
    3379       15089 :         ret = BBPsave(b);
    3380       15089 :         if (ret == GDK_SUCCEED) {
    3381       15089 :                 if (BBP_status(bid) & BBPLOADED)
    3382       15089 :                         BATfree(b);     /* free memory */
    3383       15089 :                 BBPuncacheit(bid, false);
    3384             :         }
    3385       15089 :         TRC_DEBUG(BAT_, "turn off unloading %d\n", bid);
    3386       15089 :         BBP_status_off(bid, BBPUNLOADING);
    3387       15089 :         BBP_unload_dec();
    3388       15089 :         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     2082440 : BBPquickdesc(bat bid)
    3403             : {
    3404     2082440 :         BAT *b;
    3405             : 
    3406     2082440 :         if (!BBPcheck(bid)) {
    3407         179 :                 if (!is_bat_nil(bid)) {
    3408           0 :                         GDKerror("called with invalid batid.\n");
    3409           0 :                         assert(0);
    3410             :                 }
    3411             :                 return NULL;
    3412             :         }
    3413     2082060 :         BBPspin(bid, __func__, BBPWAITING);
    3414     2082545 :         b = BBP_desc(bid);
    3415     2082545 :         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     1897230 : dirty_bat(bat *i, bool subcommit)
    3433             : {
    3434     1897230 :         if (BBPvalid(*i)) {
    3435     1888705 :                 BAT *b;
    3436     1888705 :                 BBPspin(*i, __func__, BBPSAVING);
    3437     1888705 :                 if (BBP_status(*i) & BBPLOADED) {
    3438     1819979 :                         b = BBP_desc(*i);
    3439     1819979 :                         MT_lock_set(&b->theaplock);
    3440     1918567 :                         if ((BBP_status(*i) & BBPNEW) &&
    3441       98588 :                             BATcheckmodes(b, false) != GDK_SUCCEED) /* check mmap modes */
    3442           0 :                                 *i = -*i;       /* error */
    3443     1819979 :                         else if ((BBP_status(*i) & BBPPERSISTENT) &&
    3444           0 :                                  (subcommit || BATdirty(b))) {
    3445     1639471 :                                 MT_lock_unset(&b->theaplock);
    3446     1639471 :                                 return b;       /* the bat is loaded, persistent and dirty */
    3447             :                         }
    3448      180508 :                         MT_lock_unset(&b->theaplock);
    3449       68726 :                 } else if (subcommit)
    3450       66192 :                         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      220473 : file_move(int farmid, const char *srcdir, const char *dstdir, const char *name, const char *ext)
    3463             : {
    3464      220473 :         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     3057153 : file_exists(int farmid, const char *dir, const char *name, const char *ext)
    3491             : {
    3492     3057153 :         char *path;
    3493     3057153 :         struct stat st;
    3494     3057153 :         int ret = -1;
    3495             : 
    3496     3057153 :         path = GDKfilepath(farmid, dir, name, ext);
    3497     3057153 :         if (path) {
    3498     3057153 :                 ret = MT_stat(path, &st);
    3499     3057153 :                 TRC_DEBUG(IO_, "stat(%s) = %d\n", path, ret);
    3500     3057153 :                 GDKfree(path);
    3501             :         }
    3502     3057153 :         return (ret == 0);
    3503             : }
    3504             : 
    3505             : static gdk_return
    3506      220473 : 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      220473 :         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      220473 :         } 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      220473 :         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       24453 : BBPprepare(bool subcommit)
    3565             : {
    3566       24453 :         bool start_subcommit;
    3567       24453 :         int set = 1 + subcommit;
    3568       24453 :         gdk_return ret = GDK_SUCCEED;
    3569             : 
    3570       24453 :         start_subcommit = (subcommit && backup_subdir == 0);
    3571       12055 :         if (start_subcommit) {
    3572             :                 /* starting a subcommit. Make sure SUBDIR and DELDIR
    3573             :                  * are clean */
    3574       12055 :                 ret = BBPrecover_subdir();
    3575       12055 :                 if (ret != GDK_SUCCEED)
    3576             :                         return ret;
    3577             :         }
    3578       24453 :         if (backup_files == 0) {
    3579         335 :                 backup_dir = 0;
    3580         335 :                 ret = BBPrecover(0);
    3581         335 :                 if (ret != GDK_SUCCEED)
    3582             :                         return ret;
    3583         335 :                 str bakdirpath = GDKfilepath(0, NULL, BAKDIR, NULL);
    3584         335 :                 if (bakdirpath == NULL) {
    3585             :                         return GDK_FAIL;
    3586             :                 }
    3587             : 
    3588         335 :                 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         335 :                 TRC_DEBUG(IO_, "mkdir %s = %d\n", bakdirpath, (int) ret);
    3595         335 :                 GDKfree(bakdirpath);
    3596             :         }
    3597       24453 :         if (start_subcommit) {
    3598             :                 /* make a new SUBDIR (subdir of BAKDIR) */
    3599       12055 :                 str subdirpath = GDKfilepath(0, NULL, SUBDIR, NULL);
    3600       12055 :                 if (subdirpath == NULL) {
    3601             :                         return GDK_FAIL;
    3602             :                 }
    3603             : 
    3604       12055 :                 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       12055 :                 TRC_DEBUG(IO_, "mkdir %s\n", subdirpath);
    3610       12055 :                 GDKfree(subdirpath);
    3611             :         }
    3612       24453 :         if (backup_dir != set) {
    3613             :                 /* a valid backup dir *must* at least contain BBP.dir */
    3614       49225 :                 if ((ret = GDKmove(0, backup_dir ? BAKDIR : BATDIR, "BBP", "dir", subcommit ? SUBDIR : BAKDIR, "BBP", "dir", true)) != GDK_SUCCEED)
    3615             :                         return ret;
    3616       24445 :                 backup_dir = set;
    3617             :         }
    3618             :         /* increase counters */
    3619       24453 :         backup_subdir += subcommit;
    3620       24453 :         backup_files++;
    3621             : 
    3622       24453 :         return ret;
    3623             : }
    3624             : 
    3625             : static gdk_return
    3626     1207565 : do_backup(Heap *h, bool dirty, bool subcommit)
    3627             : {
    3628     1207565 :         gdk_return ret = GDK_SUCCEED;
    3629     1207565 :         char extnew[16];
    3630             : 
    3631     1207565 :         if (h->wasempty) {
    3632             :                 return GDK_SUCCEED;
    3633             :         }
    3634             : 
    3635             :         /* direct mmap is unprotected (readonly usage, or has WAL
    3636             :          * protection) */
    3637     1207565 :         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     1197867 :                 gdk_return mvret = GDK_SUCCEED;
    3648             : 
    3649     1197867 :                 char *srcdir = GDKfilepath(NOFARM, BATDIR, h->filename, NULL);
    3650     1197867 :                 if (srcdir == NULL)
    3651             :                         return GDK_FAIL;
    3652     1197867 :                 char *nme = strrchr(srcdir, DIR_SEP);
    3653     1197867 :                 assert(nme != NULL);
    3654     1197867 :                 *nme++ = '\0';
    3655     1197867 :                 char *ext = strchr(nme, '.');
    3656     1197867 :                 assert(ext != NULL);
    3657     1197867 :                 *ext++ = '\0';
    3658             : 
    3659     1197867 :                 strconcat_len(extnew, sizeof(extnew), ext, ".new", NULL);
    3660     1418340 :                 if (dirty &&
    3661      440946 :                     !file_exists(h->farmid, BAKDIR, nme, extnew) &&
    3662      220473 :                     !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      220473 :                         if (file_exists(h->farmid, srcdir, nme, extnew)) {
    3669           0 :                                 mvret = heap_move(h, srcdir,
    3670             :                                                   subcommit ? SUBDIR : BAKDIR,
    3671             :                                                   nme, extnew);
    3672      220473 :                         } else if (file_exists(h->farmid, srcdir, nme, ext)) {
    3673      220473 :                                 mvret = heap_move(h, srcdir,
    3674             :                                                   subcommit ? SUBDIR : BAKDIR,
    3675             :                                                   nme, ext);
    3676      220473 :                                 if (mvret == GDK_SUCCEED) {
    3677             :                                         /* file no longer in "standard"
    3678             :                                          * location */
    3679      220473 :                                         h->hasfile = false;
    3680             :                                 }
    3681             :                         }
    3682      977394 :                 } else if (subcommit) {
    3683             :                         /* if subcommit, we may need to move an
    3684             :                          * already made backup from BAKDIR to
    3685             :                          * SUBDIR */
    3686      977394 :                         if (file_exists(h->farmid, BAKDIR, nme, extnew))
    3687           0 :                                 mvret = file_move(h->farmid, BAKDIR, SUBDIR, nme, extnew);
    3688      977394 :                         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      220473 :                 if (mvret != GDK_SUCCEED && file_exists(h->farmid, srcdir, nme, ext)) {
    3698     1197867 :                         ret = GDK_FAIL;
    3699             :                 }
    3700     1197867 :                 if (subcommit &&
    3701     1197867 :                     (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     1197867 :                 GDKfree(srcdir);
    3712             :         }
    3713             :         return ret;
    3714             : }
    3715             : 
    3716             : static gdk_return
    3717      949125 : BBPbackup(BAT *b, bool subcommit)
    3718             : {
    3719      949125 :         gdk_return rc = GDK_SUCCEED;
    3720             : 
    3721      949125 :         MT_lock_set(&b->theaplock);
    3722      949125 :         BATiter bi = bat_iterator_nolock(b);
    3723      949125 :         if (!bi.copiedtodisk || bi.transient) {
    3724           1 :                 MT_lock_unset(&b->theaplock);
    3725           1 :                 return GDK_SUCCEED;
    3726             :         }
    3727      949124 :         assert(b->theap->parentid == b->batCacheid);
    3728      949124 :         if (b->oldtail && b->oldtail != (Heap *) 1) {
    3729        1706 :                 bi.h = b->oldtail;
    3730        1706 :                 bi.hdirty = b->oldtail->dirty;
    3731             :         }
    3732      949124 :         bat_iterator_incref(&bi);
    3733      949124 :         MT_lock_unset(&b->theaplock);
    3734             : 
    3735             :         /* determine location dir and physical suffix */
    3736      949124 :         if (bi.type != TYPE_void) {
    3737      949124 :                 rc = do_backup(bi.h, bi.hdirty, subcommit);
    3738      949124 :                 if (rc == GDK_SUCCEED && bi.vh != NULL)
    3739      258441 :                         rc = do_backup(bi.vh, bi.vhdirty, subcommit);
    3740             :         }
    3741      949124 :         bat_iterator_end(&bi);
    3742      949124 :         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       12063 : BBPsync(int cnt, bat *restrict subcommit, BUN *restrict sizes, lng logno)
    3871             : {
    3872       12063 :         gdk_return ret = GDK_SUCCEED;
    3873       12063 :         lng t0 = 0, t1 = 0;
    3874       12063 :         str bakdir, deldir;
    3875       12063 :         const bool lock = locked_by == 0 || locked_by != MT_getpid();
    3876       12063 :         char buf[3000];
    3877       12063 :         int n = subcommit ? 0 : -1;
    3878           8 :         FILE *obbpf, *nbbpf;
    3879             : 
    3880       12063 :         if ((bakdir = GDKfilepath(0, NULL, subcommit ? SUBDIR : BAKDIR, NULL)) == NULL)
    3881             :                 return GDK_FAIL;
    3882       12063 :         if ((deldir = GDKfilepath(0, NULL, DELDIR, NULL)) == NULL) {
    3883           0 :                 GDKfree(bakdir);
    3884           0 :                 return GDK_FAIL;
    3885             :         }
    3886             : 
    3887       12063 :         TRC_DEBUG_IF(PERF) t0 = t1 = GDKusec();
    3888             : 
    3889       12063 :         if ((ATOMIC_GET(&GDKdebug) & TAILCHKMASK) && !GDKinmemory(0))
    3890           0 :                 BBPcheckBBPdir();
    3891             : 
    3892       12063 :         ret = BBPprepare(subcommit != NULL);
    3893             : 
    3894       12063 :         if (ret == GDK_SUCCEED) {
    3895       12063 :                 ret = BBPdir_first(subcommit != NULL, logno, &obbpf, &nbbpf);
    3896             :         }
    3897             : 
    3898     1909293 :         for (int idx = 1; ret == GDK_SUCCEED && idx < cnt; idx++) {
    3899     1897230 :                 bat i = subcommit ? subcommit[idx] : idx;
    3900     1897230 :                 BUN size = sizes ? sizes[idx] : BUN_NONE;
    3901     1897230 :                 BATiter bi, *bip;
    3902             : 
    3903     1897230 :                 const bat bid = i;
    3904     1897230 :                 if (lock)
    3905     1886171 :                         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     1897230 :                 BBP_status_on(bid, BBPSYNCING);
    3911             :                 /* wait until unloading is finished before
    3912             :                  * attempting to make a backup */
    3913     1897230 :                 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     1897230 :                                 MT_lock_set(&GDKswapLock(bid));
    3919             :                 }
    3920     1897230 :                 BAT *b = BBP_desc(bid);
    3921     1897230 :                 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     1886171 :                         assert(b->ttype > 0); /* no unknown types allowed */
    3926     1886171 :                         char fname[16]; /* plenty big enough */
    3927     1886171 :                         if (snprintf(fname, sizeof(fname), "%o", (unsigned) bid) < 16) {
    3928             :                                 /* the snprintf never fails, any of the
    3929             :                                  * below may fail */
    3930     1886171 :                                 uint8_t stpe = ATOMstorage(b->ttype);
    3931     3367921 :                                 if ((b->ttype != TYPE_str || b->twidth >= 8) &&
    3932     1481750 :                                     GDKmove(0, BAKDIR, fname, "tail", SUBDIR, fname, "tail", false) == GDK_SUCCEED)
    3933           0 :                                         TRC_DEBUG(IO_, "moved %s.tail from %s to %s\n",
    3934             :                                                   fname, BAKDIR, SUBDIR);
    3935     2290906 :                                 if (stpe == TYPE_str &&
    3936      404735 :                                     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      512214 :                                 if (stpe == TYPE_str && b->twidth >= 2 &&
    3940      107479 :                                     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      422257 :                                 if (stpe == TYPE_str && b->twidth >= 4 &&
    3945       17522 :                                     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     2291907 :                                 if (ATOMvarsized(b->ttype) &&
    3950      405736 :                                     GDKmove(0, BAKDIR, fname, "theap", SUBDIR, fname, "theap", false) == GDK_SUCCEED)
    3951     1886171 :                                         TRC_DEBUG(IO_, "moved %s.theap from %s to %s\n",
    3952             :                                                   fname, BAKDIR, SUBDIR);
    3953             :                         }
    3954             :                 }
    3955     1897230 :                 b = dirty_bat(&i, subcommit != NULL);
    3956     1897230 :                 if (i <= 0)
    3957             :                         ret = GDK_FAIL;
    3958     1897230 :                 else if (BBP_status(bid) & BBPEXISTING &&
    3959     1603066 :                          b != NULL &&
    3960     1603066 :                          b->batInserted > 0)
    3961      949125 :                         ret = BBPbackup(b, subcommit != NULL);
    3962             : 
    3963     1897230 :                 if (lock)
    3964     1886171 :                         MT_lock_unset(&GDKswapLock(bid));
    3965             : 
    3966     1897230 :                 if (ret != GDK_SUCCEED)
    3967             :                         break;
    3968             : 
    3969     1897230 :                 if (BBP_status(i) & BBPPERSISTENT) {
    3970     1704656 :                         MT_lock_set(&BBP_desc(i)->theaplock);
    3971     1704656 :                         bi = bat_iterator_nolock(BBP_desc(i));
    3972     1704656 :                         bat_iterator_incref(&bi);
    3973     1704656 :                         assert(sizes == NULL || size <= bi.count);
    3974     1701022 :                         assert(sizes == NULL || bi.width == 0 || (bi.type == TYPE_msk ? ((size + 31) / 32) * 4 : size << bi.shift) <= bi.hfree);
    3975     1704656 :                         if (size > bi.count) /* includes sizes==NULL */
    3976             :                                 size = bi.count;
    3977     1704656 :                         bi.b->batInserted = size;
    3978     1704656 :                         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      402405 :                                 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      402405 :                                         if (bi.b->oldtail == NULL)
    3986      400369 :                                                 bi.b->oldtail = (Heap *) 1;
    3987      402405 :                                         HEAPincref(bi.h);
    3988             :                                 }
    3989             :                         }
    3990     1704656 :                         MT_lock_unset(&bi.b->theaplock);
    3991     1704656 :                         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     1015642 :                                 for (;;) {
    3997     1015642 :                                         if (lock)
    3998     1015642 :                                                 MT_lock_set(&GDKswapLock(i));
    3999     1015642 :                                         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     1015642 :                                 BBP_status_on(i, BBPSAVING);
    4006     1015642 :                                 if (lock)
    4007     1015642 :                                         MT_lock_unset(&GDKswapLock(i));
    4008     1015642 :                                 ret = BATsave_iter(b, &bi, size);
    4009     1015642 :                                 BBP_status_off(i, BBPSAVING);
    4010             :                         }
    4011             :                         bip = &bi;
    4012             :                 } else {
    4013             :                         bip = NULL;
    4014             :                 }
    4015     1897230 :                 if (ret == GDK_SUCCEED) {
    4016     1897230 :                         n = BBPdir_step(i, size, n, buf, sizeof(buf), &obbpf, nbbpf, bip);
    4017     1897230 :                         if (n < -1)
    4018           0 :                                 ret = GDK_FAIL;
    4019             :                 }
    4020     1897230 :                 if (bip)
    4021     1704656 :                         bat_iterator_end(bip);
    4022             :                 /* we once again have a saved heap */
    4023             :         }
    4024             : 
    4025       12063 :         TRC_DEBUG(PERF, "write time "LLFMT" usec\n", (t0 = GDKusec()) - t1);
    4026             : 
    4027       12063 :         if (ret == GDK_SUCCEED) {
    4028       12063 :                 ret = BBPdir_last(n, buf, sizeof(buf), obbpf, nbbpf);
    4029             :         }
    4030             : 
    4031       12063 :         TRC_DEBUG(PERF, "dir time "LLFMT" usec, %d bats\n", (t1 = GDKusec()) - t0, (bat) ATOMIC_GET(&BBPsize));
    4032             : 
    4033       12063 :         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       12063 :                 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       12063 :                 TRC_DEBUG(IO_, "rename %s %s = %d\n", bakdir, deldir, (int) ret);
    4048             :         }
    4049             : 
    4050             :         /* AFTERMATH */
    4051       12063 :         if (ret == GDK_SUCCEED) {
    4052       12063 :                 ATOMIC_SET(&BBPlogno, logno);       /* the new value */
    4053       12063 :                 backup_files = subcommit ? (backup_files - backup_subdir) : 0;
    4054       12063 :                 backup_dir = backup_subdir = 0;
    4055       12063 :                 if (GDKremovedir(0, DELDIR) != GDK_SUCCEED)
    4056           0 :                         fprintf(stderr, "#BBPsync: cannot remove directory %s\n", DELDIR);
    4057       12063 :                 (void) BBPprepare(false); /* (try to) remove DELDIR and set up new BAKDIR */
    4058       12063 :                 if (backup_files > 1) {
    4059       12055 :                         TRC_DEBUG(PERF, "backup_files %d > 1\n", backup_files);
    4060       12055 :                         backup_files = 1;
    4061             :                 }
    4062             :         }
    4063       12063 :         TRC_DEBUG(PERF, "%s (ready time "LLFMT" usec)\n",
    4064             :                   ret == GDK_SUCCEED ? "" : " failed",
    4065             :                   (t0 = GDKusec()) - t1);
    4066             : 
    4067       12063 :         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     1909293 :         for (int idx = 1; idx < cnt; idx++) {
    4088     1897230 :                 bat i = subcommit ? subcommit[idx] : idx;
    4089     1897230 :                 BBP_status_off(i, BBPSYNCING);
    4090             :         }
    4091             : 
    4092       12063 :         GDKfree(bakdir);
    4093       12063 :         GDKfree(deldir);
    4094       12063 :         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         335 : BBPrecover(int farmid)
    4174             : {
    4175         335 :         str bakdirpath;
    4176         335 :         str leftdirpath;
    4177         335 :         DIR *dirp;
    4178         335 :         struct dirent *dent;
    4179         335 :         long_str path, dstpath;
    4180         335 :         bat i;
    4181         335 :         size_t j = strlen(BATDIR);
    4182         335 :         gdk_return ret = GDK_SUCCEED;
    4183         335 :         bool dirseen = false;
    4184         335 :         str dstdir;
    4185             : 
    4186         335 :         bakdirpath = GDKfilepath(farmid, NULL, BAKDIR, NULL);
    4187         335 :         leftdirpath = GDKfilepath(farmid, NULL, LEFTDIR, NULL);
    4188         335 :         if (bakdirpath == NULL || leftdirpath == NULL) {
    4189           0 :                 GDKfree(bakdirpath);
    4190           0 :                 GDKfree(leftdirpath);
    4191           0 :                 return GDK_FAIL;
    4192             :         }
    4193         335 :         dirp = opendir(bakdirpath);
    4194         335 :         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         102 :         memcpy(dstpath, BATDIR, j);
    4202         102 :         dstpath[j] = DIR_SEP;
    4203         102 :         dstpath[++j] = 0;
    4204         102 :         dstdir = dstpath + j;
    4205         102 :         TRC_DEBUG(IO_, "start\n");
    4206             : 
    4207         102 :         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         306 :         while ((dent = readdir(dirp)) != NULL) {
    4217         204 :                 const char *q = strchr(dent->d_name, '.');
    4218             : 
    4219         204 :                 if (q == dent->d_name) {
    4220         204 :                         char *fn;
    4221             : 
    4222         204 :                         if (strcmp(dent->d_name, ".") == 0 ||
    4223         102 :                             strcmp(dent->d_name, "..") == 0)
    4224         204 :                                 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         102 :         closedir(dirp);
    4269         102 :         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         102 :         if (ret == GDK_SUCCEED) {
    4283         102 :                 if (MT_rmdir(bakdirpath) < 0) {
    4284           0 :                         GDKsyserror("cannot remove directory %s\n", bakdirpath);
    4285           0 :                         ret = GDK_FAIL;
    4286             :                 }
    4287         102 :                 TRC_DEBUG(IO_, "rmdir %s = %d\n", bakdirpath, (int) ret);
    4288             :         }
    4289         102 :         if (ret != GDK_SUCCEED)
    4290           0 :                 GDKerror("recovery failed.\n");
    4291             : 
    4292         102 :         TRC_DEBUG(IO_, "end\n");
    4293         102 :         GDKfree(bakdirpath);
    4294         102 :         GDKfree(leftdirpath);
    4295         102 :         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       12382 : BBPrecover_subdir(void)
    4305             : {
    4306       12382 :         str subdirpath;
    4307       12382 :         DIR *dirp;
    4308       12382 :         struct dirent *dent;
    4309       12382 :         gdk_return ret = GDK_SUCCEED;
    4310             : 
    4311       12382 :         subdirpath = GDKfilepath(0, NULL, SUBDIR, NULL);
    4312       12382 :         if (subdirpath == NULL)
    4313             :                 return GDK_FAIL;
    4314       12382 :         dirp = opendir(subdirpath);
    4315       12382 :         if (dirp == NULL && errno != ENOENT)
    4316           0 :                 GDKsyserror("cannot open directory %s\n", subdirpath);
    4317       12382 :         GDKfree(subdirpath);
    4318       12382 :         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       25151 : persistent_bat(bat bid)
    4364             : {
    4365       25151 :         if (bid >= 0 && bid < (bat) ATOMIC_GET(&BBPsize) && BBPvalid(bid)) {
    4366       25151 :                 BAT *b = BBP_desc(bid);
    4367       25151 :                 if ((BBP_status(bid) & BBPLOADED) == 0 || b->batCopiedtodisk) {
    4368             :                         return true;
    4369             :                 }
    4370             :         }
    4371             :         return false;
    4372             : }
    4373             : 
    4374             : static BAT *
    4375       25151 : getdesc(bat bid)
    4376             : {
    4377       25151 :         BAT *b = NULL;
    4378             : 
    4379       25151 :         if (is_bat_nil(bid))
    4380             :                 return NULL;
    4381       25151 :         assert(bid > 0);
    4382       25151 :         if (bid < (bat) ATOMIC_GET(&BBPsize) && BBP_logical(bid))
    4383       25151 :                 b = BBP_desc(bid);
    4384       25151 :         if (b == NULL)
    4385           0 :                 BBPclear(bid);
    4386             :         return b;
    4387             : }
    4388             : 
    4389             : static bool
    4390        1731 : BBPdiskscan(const char *parent, size_t baseoff)
    4391             : {
    4392        1731 :         DIR *dirp = opendir(parent);
    4393        1731 :         struct dirent *dent;
    4394        1731 :         char fullname[FILENAME_MAX];
    4395        1731 :         str dst;
    4396        1731 :         size_t dstlen;
    4397        1731 :         const char *src = parent;
    4398             : 
    4399        1731 :         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        1569 :         dst = stpcpy(fullname, src);
    4406        1569 :         if (dst > fullname && dst[-1] != DIR_SEP)
    4407        1569 :                 *dst++ = DIR_SEP;
    4408        1569 :         dstlen = sizeof(fullname) - (dst - fullname);
    4409             : 
    4410       32996 :         while ((dent = readdir(dirp)) != NULL) {
    4411       29858 :                 const char *p;
    4412       29858 :                 bat bid;
    4413       29858 :                 bool ok, delete;
    4414             : 
    4415       29858 :                 if (dent->d_name[0] == '.')
    4416        3138 :                         continue;       /* ignore .dot files and directories (. ..) */
    4417             : 
    4418             : #ifdef GDKLIBRARY_JSON
    4419       26720 :                 if (strcmp(dent->d_name, "jsonupgradeneeded") == 0) {
    4420           0 :                         continue; /* ignore json upgrade signal file  */
    4421             :                 }
    4422             : #endif
    4423             : 
    4424       26720 :                 if (strncmp(dent->d_name, "BBP.", 4) == 0 &&
    4425         327 :                     (strcmp(parent + baseoff, BATDIR) == 0 ||
    4426         327 :                      strncmp(parent + baseoff, BAKDIR, strlen(BAKDIR)) == 0 ||
    4427           0 :                      strncmp(parent + baseoff, SUBDIR, strlen(SUBDIR)) == 0))
    4428         327 :                         continue;
    4429             : 
    4430       26393 :                 p = strchr(dent->d_name, '.');
    4431             : 
    4432       26393 :                 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       26393 :                 strncpy(dst, dent->d_name, dstlen);
    4440       26393 :                 fullname[sizeof(fullname) - 1] = 0;
    4441             : 
    4442       26393 :                 if (p == NULL && !BBPdiskscan(fullname, baseoff)) {
    4443             :                         /* it was a directory */
    4444        1242 :                         continue;
    4445             :                 }
    4446             : 
    4447       25151 :                 if (p && strcmp(p + 1, "tmp") == 0) {
    4448             :                         delete = true;
    4449             :                         ok = true;
    4450       25151 :                         bid = 0;
    4451             :                 } else {
    4452       25151 :                         bid = strtol(dent->d_name, NULL, 8);
    4453       25151 :                         ok = p && bid;
    4454       25151 :                         delete = false;
    4455             : 
    4456       25151 :                         if (!ok || !persistent_bat(bid)) {
    4457             :                                 delete = true;
    4458       25151 :                         } else if (strncmp(p + 1, "tail", 4) == 0) {
    4459       18625 :                                 BAT *b = getdesc(bid);
    4460       18625 :                                 delete = (b == NULL || !b->ttype || !b->batCopiedtodisk || b->batCount == 0);
    4461       18625 :                                 assert(b == NULL || b->batCount > 0 || b->theap->free == 0);
    4462       18625 :                                 if (!delete) {
    4463       18623 :                                         if (b->ttype == TYPE_str) {
    4464        5131 :                                                 switch (b->twidth) {
    4465        3108 :                                                 case 1:
    4466        3108 :                                                         delete = strcmp(p + 1, "tail1") != 0;
    4467        3108 :                                                         break;
    4468        1668 :                                                 case 2:
    4469        1668 :                                                         delete = strcmp(p + 1, "tail2") != 0;
    4470        1668 :                                                         break;
    4471             : #if SIZEOF_VAR_T == 8
    4472         355 :                                                 case 4:
    4473         355 :                                                         delete = strcmp(p + 1, "tail4") != 0;
    4474         355 :                                                         break;
    4475             : #endif
    4476           0 :                                                 default:
    4477           0 :                                                         delete = strcmp(p + 1, "tail") != 0;
    4478           0 :                                                         break;
    4479             :                                                 }
    4480             :                                         } else {
    4481       13492 :                                                 delete = strcmp(p + 1, "tail") != 0;
    4482             :                                         }
    4483             :                                 }
    4484        6526 :                         } else if (strncmp(p + 1, "theap", 5) == 0) {
    4485        5399 :                                 BAT *b = getdesc(bid);
    4486        5399 :                                 delete = (b == NULL || !b->tvheap || !b->batCopiedtodisk || b->tvheap->free == 0);
    4487        1127 :                         } else if (strncmp(p + 1, "thashl", 6) == 0 ||
    4488         564 :                                    strncmp(p + 1, "thashb", 6) == 0) {
    4489             : #ifdef PERSISTENTHASH
    4490        1126 :                                 BAT *b = getdesc(bid);
    4491        1126 :                                 delete = b == NULL;
    4492        1126 :                                 if (!delete)
    4493        1126 :                                         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       25151 :                                 ok = false;
    4526             :                         }
    4527             :                 }
    4528       25151 :                 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       25151 :                 if (delete) {
    4535           2 :                         if (MT_remove(fullname) != 0 && errno != ENOENT) {
    4536           0 :                                 GDKsyserror("remove(%s)", fullname);
    4537           0 :                                 continue;
    4538             :                         }
    4539       31429 :                         TRC_DEBUG(IO_, "remove(%s) = 0\n", fullname);
    4540             :                 }
    4541             :         }
    4542        1569 :         closedir(dirp);
    4543        1569 :         return false;
    4544             : }
    4545             : 
    4546             : void
    4547         326 : gdk_bbp_reset(void)
    4548             : {
    4549         326 :         int i;
    4550             : 
    4551         326 :         BBP_free = 0;
    4552         326 :         BBP_nfree = 0;
    4553         329 :         while (BBPlimit > BBPINIT) {
    4554           3 :                 BBPlimit -= BBPINIT;
    4555           3 :                 assert(BBPlimit >= 0);
    4556           3 :                 GDKfree(BBP[BBPlimit >> BBPINITLOG]);
    4557           3 :                 BBP[BBPlimit >> BBPINITLOG] = NULL;
    4558             :         }
    4559         326 :         ATOMIC_SET(&BBPsize, 0);
    4560         326 :         memset(BBP0, 0, sizeof(BBP0));
    4561       10758 :         for (i = 0; i < MAXFARMS; i++)
    4562       10432 :                 GDKfree((void *) BBPfarms[i].dirname); /* loose "const" */
    4563         326 :         memset(BBPfarms, 0, sizeof(BBPfarms));
    4564         326 :         memset(BBP_hash, 0, sizeof(BBP_hash));
    4565             : 
    4566         326 :         locked_by = 0;
    4567         326 :         BBPunloadCnt = 0;
    4568         326 :         backup_files = 0;
    4569         326 :         backup_dir = 0;
    4570         326 :         backup_subdir = 0;
    4571         326 : }
    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          42 : BBPcallbacks(void)
    4691             : {
    4692          42 :         MT_lock_set(&GDKCallbackListLock);
    4693          42 :         gdk_callback *next = callback_list.head;
    4694             : 
    4695          42 :         while (next) {
    4696           0 :                 if(should_call(next))
    4697           0 :                         do_callback(next);
    4698           0 :                 next = next->next;
    4699             :         }
    4700          42 :         MT_lock_unset(&GDKCallbackListLock);
    4701          42 : }
    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       57500 : BBPtmlockFinish(void)
    4713             : {
    4714       57500 :         if (!GDKinmemory(0) &&
    4715             :             /* also use an external lock file to synchronize with
    4716             :              * external programs */
    4717       57500 :             (lockfile != NULL ||
    4718         327 :              (lockfile = GDKfilepath(0, NULL, ".tm_lock", NULL)) != NULL)) {
    4719       57500 :                     lockfd = MT_lockf(lockfile, F_LOCK);
    4720             :         }
    4721       57500 : }
    4722             : 
    4723             : void
    4724       57384 : BBPtmlock(void)
    4725             : {
    4726       57384 :         MT_lock_set(&GDKtmLock);
    4727       57384 :         BBPtmlockFinish();
    4728       57384 : }
    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       57500 : BBPtmunlock(void)
    4741             : {
    4742       57500 :         if (lockfile && lockfd >= 0) {
    4743       57500 :                 assert(!GDKinmemory(0));
    4744       57500 :                 MT_lockf(lockfile, F_ULOCK);
    4745       57500 :                 close(lockfd);
    4746       57500 :                 lockfd = -1;
    4747             :         }
    4748       57500 :         MT_lock_unset(&GDKtmLock);
    4749       57500 : }
    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      391876 :         for (bat i = 1; i < sz; i++) {
    4769      391760 :                 if (!MT_lock_trytime(&GDKswapLock(i), 1000)) {
    4770           0 :                         nskip++;
    4771           0 :                         continue;
    4772             :                 }
    4773      391760 :                 int r;
    4774      391760 :                 if ((r = BBP_refs(i)) > 0 || BBP_lrefs(i) > 0) {
    4775      342381 :                         BAT *b = BBP_desc(i);
    4776      342381 :                         if (!MT_lock_trytime(&b->theaplock, 1000)) {
    4777           0 :                                 nskip++;
    4778           0 :                                 b = NULL;
    4779             :                         }
    4780           0 :                         if (b != NULL) {
    4781      342381 :                                 nbats++;
    4782      342381 :                                 ATOMIC_BASE_TYPE status = BBP_status(i);
    4783      342381 :                                 struct counters *bt = &bats[r > 0][BATdirty(b)][(status & BBPPERSISTENT) != 0][(status & BBPLOADED) != 0][(status & BBPHOT) != 0];
    4784      342381 :                                 bt->nr++;
    4785      342381 :                                 if (b->theap && b->batCacheid == b->theap->parentid) {
    4786      342381 :                                         bt->sz += HEAPmemsize(b->theap);
    4787      342381 :                                         bt->vmsz += HEAPvmsize(b->theap);
    4788             :                                 }
    4789      342381 :                                 if (b->tvheap && b->batCacheid == b->tvheap->parentid) {
    4790       11421 :                                         bt->sz += HEAPmemsize(b->tvheap);
    4791       11421 :                                         bt->vmsz += HEAPvmsize(b->tvheap);
    4792             :                                 }
    4793      342381 :                                 MT_lock_unset(&b->theaplock);
    4794             :                         }
    4795             :                 }
    4796      391760 :                 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         104 :                 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         115 :                 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          16 :                 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         106 :                 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          13 :                 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           5 :                 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          11 :                 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           1 :                 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           6 :                 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           3 :                 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           1 :                 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           1 :                 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