⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdk_bbp.c

📁 这个是内存数据库中的一个管理工具
💻 C
📖 第 1 页 / 共 5 页
字号:
	}	if (i < 0)		i = -i;	if (BBPcheck(i, "BBPincref")) {		if (lock) {			gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPincref");			while (BBP_status(i) & BBPUNSTABLE) {				gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPincref spin wait");				MT_sleep_ms(1);				gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPincref spin wait");			}		}		/* got the lock */		if (logical) {			refs = ++BBP_lrefs(i);		} else {			refs = ++BBP_refs(i);		}		if (lock)			gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPfix");	}	return refs;}intBBPincref(bat i, int logical){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	return incref(i, logical, lock);}voidBBPshare(bat parent){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	int fix = 0;	if (parent < 0)		parent = -parent;	if (lock)		gdk_set_lock(GDKswapLock[parent & BBPLOCKMASK], "BBPshare");	if (++BBP_cache(parent)->batSharecnt == 1)		fix = 1;	if (lock)		gdk_unset_lock(GDKswapLock[parent & BBPLOCKMASK], "BBPshare");	if (fix)		BBPincref(parent, FALSE);}static INLINE intdecref(bat i, int logical, int lock){	int refs = 0, swap = 0, swap_unloading = 0;	BAT *b;	/* decrement references by one */	if (logical) {		if (BBP_lrefs(i) == 0) {			GDKerror("BBPdecref: %s does not have logical references.\n", BBPname(i));		} else {			refs = --BBP_lrefs(i);		}	} else {		if (BBP_refs(i) == 0) {			GDKerror("BBPdecref: %s does not have pointer fixes.\n", BBPname(i));		} else {			refs = --BBP_refs(i);		}	}	/* we destroy transients asap and unload persistent bats only if they have been made cold */	b = BBP_cache(i);	if (BBP_refs(i) > 0 || (BBP_lrefs(i) > 0 && BBP_lastused(i) != 0)) {		/* bat cannot be swapped out. renew its last usage stamp for the BBP LRU policy */		int sec = BBPLASTUSED(BBPstamp());		if (sec > BBPLASTUSED(BBP_lastused(i)))			BBP_lastused(i) = sec;	} else if (b || (BBP_status(i) & BBPTMP)) {		/* bat will be unloaded now. set the UNLOADING bit while locked so no other thread thinks its available anymore */                if (BBP_status(i) & BBPUNLOADING) {                        /* BBPtrim was unloading this bat that just now lost its last refcnt. */                        incref(i, logical, FALSE); /* cannot destroy it now; back off */                        swap_unloading = TRUE;                 } else {		        BBP_status_on(i, BBPUNLOADING, "BBPdecref");		        swap = TRUE;                }	}	/* unlock before re-locking in unload; as saving a dirty persistent bat may take a long time */	if (lock)		gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPdecref");        if (swap_unloading) {		BBPspin(i, "BBPdecref", BBPUNLOADING);  /* wait for unload to complete */		decref(i, logical, lock);  /* and just try again */	} else if (swap) {		int destroy = BBP_lrefs(i) == 0 && (BBP_status(i) & BBPDELETED) == 0;		b = BBPquickdesc(i, TRUE);		if (destroy) {			BBPdestroy(b);	/* free memory (if loaded) and delete from disk (if transient but saved) */		} else if (b) {			BBP_unload_inc(i, "BBPdecref");			BBPfree(b, "BBPdecref" );	/* free memory of transient */		}	}	return refs;}intBBPdecref(bat i, int logical){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	if (BBPcheck(i, "BBPdecref") == 0) {		return -1;	}	if (i < 0)		i = -i;	if (lock)		gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPdecref");	return decref(i, logical, lock);}#line 1368 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bbp.mx"voidBBPkeepref(bat i){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	if (i == bat_nil) 		return ;	if (i < 0)		i = -i;	if (BBPcheck(i, "BBPkeepref")) {		if (lock)			gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPkeepref");		while (BBP_status(i) & BBPUNLOADING) {			if (lock)				gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPincref spin wait");			MT_sleep_ms(1);			if (lock)				gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPincref spin wait");		}		/* got the lock */		++BBP_lrefs(i);		--BBP_refs(i);		if (lock)			gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPfix"); 		/* decref(i, FALSE, lock);*/	}}voidBBPreleaselref(bat i){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	if (i == bat_nil || BBP_lrefs(i)<= 0) 		return ;	if (i < 0)		i = -i;	if (BBPcheck(i, "BBPreleaselref")) {		if (lock)			gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPreleaselref");		while (BBP_status(i) & BBPUNLOADING) {			if (lock)				gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPincref spin wait");			MT_sleep_ms(1);			if (lock)				gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPincref spin wait");		}		/* got the lock */		--BBP_lrefs(i);		if (lock)			gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPfix"); 	}}voidBBPreleaseref(bat i){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	if (i == bat_nil || BBP_refs(i)<=0)		return ;	if (i < 0)		i = -i;	if (BBPcheck(i, "BBPreleaseref")) {		if (lock)			gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPreleaseref");		while (BBP_status(i) & BBPUNLOADING) {			if (lock)				gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPincref spin wait");			MT_sleep_ms(1);			if (lock)				gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPincref spin wait");		}		/* got the lock */		--BBP_refs(i);		if (lock)			gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPfix"); 	}}static INLINE voidunshare(bat parent){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	if (parent < 0)		parent = -parent;	if (lock)		gdk_set_lock(GDKswapLock[parent & BBPLOCKMASK], "BBPunshare");	if (--BBP_cache(parent)->batSharecnt == 0) { 		(void) decref(parent, FALSE, lock);	} else if (lock) {		gdk_unset_lock(GDKswapLock[parent & BBPLOCKMASK], "BBPunshare");	}}voidBBPunshare(bat parent) {	unshare(parent);}#line 1478 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bbp.mx"intBBPreclaim(BAT *b){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	bat i = ABS(b->batCacheid);	int reclaim_while_unloading = 0;	int ret = 0;	if (lock)		gdk_set_lock(GDKswapLock[i & BBPLOCKMASK], "BBPreclaim");	BATDEBUG THRprintf(GDKout, "#BBPreclaim: bat(%d) view=%d lrefs=%d ref=%d stat=%d\n", (int) b->batCacheid, b->batSharecnt, BBP_lrefs(b->batCacheid), BBP_refs(b->batCacheid), BBP_status(b->batCacheid));	if (BBP_refs(b->batCacheid) > 1) {		GDKerror("BBPreclaim: %d refs > 1 (%d)\n", i, BBP_refs(i));		ret = -1;	} else if (BBP_status(i) & BBPUNLOADING) {                /* BBPtrim was unloading this bat that just now is getting killed. */                reclaim_while_unloading = TRUE;         } else {		/* unload whatever the LRU in the BBP */		BBP_refs(b->batCacheid) = 0;		BBP_status_on(i, BBPUNLOADING, "BBPreclaim");	}	if (lock)		gdk_unset_lock(GDKswapLock[i & BBPLOCKMASK], "BBPreclaim");        if (reclaim_while_unloading) {		BBPspin(i, "BBPreclaim", BBPUNLOADING);  /* wait for unload to complete */		return BBPreclaim(b);	}	/* BBPfree potentially saves the BAT. Do this after releasing the short-term lock */	if (ret == 0) {		int destroy = BBP_lrefs(i) == 0 && (BBP_status(i) & BBPDELETED) == 0;		if (destroy) {			ret = BBPdestroy(BBP_cache(i));		} else {			BBP_unload_inc(i, "BBPreclaim");			ret = BBPfree(b, "BBPreclaim" );		}	}	return ret;}#line 1527 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bbp.mx"BAT *BBPdescriptor(bat i){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	int load = FALSE;	bat j = ABS(i);	BAT *b = NULL;	if (!BBPcheck(i, "BBPdescriptor")) {		return NULL;	}	assert(BBP_refs(i));	if ((b = BBP_cache(i)) == NULL) {		if (lock)			gdk_set_lock(GDKswapLock[j & BBPLOCKMASK], "BBPdescriptor");		while (BBP_status(j) & BBPWAITING) {	/* wait for bat to be loaded by other thread */			if (lock)				gdk_unset_lock(GDKswapLock[j & BBPLOCKMASK], "BBPdescriptor");			MT_sleep_ms(1);			if (lock)				gdk_set_lock(GDKswapLock[j & BBPLOCKMASK], "BBPdescriptor");		}		if (BBPvalid(j)) {			b = BBP_cache(i);			if (b == NULL) {				load = TRUE;				BBP_status_on(j, BBPLOADING, "BBPdescriptor");			}		}		if (lock)			gdk_unset_lock(GDKswapLock[j & BBPLOCKMASK], "BBPdescriptor");	}	if (load) {		IODEBUG THRprintf(GDKout, "#load %s\n", BBPname(i));		b = BATload_intern(i);		BBPin++;		/* clearing bits can be done without the lock */		BBP_status_off(j, BBPLOADING, "BBPdescriptor");	}	return b;}#line 1578 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bbp.mx"intBBPsave(BAT *b){	int lock = locked_by ? BBP_getpid() != locked_by : 1;	bat bid = ABS(b->batCacheid);	int ret = 0;	if (BBP_lrefs(bid) == 0 || !BATdirty(b)) 		/* do nothing */		return 0;	if (lock)		gdk_set_lock(GDKswapLock[bid & BBPLOCKMASK], "BBPsave");	if (BBP_status(bid) & BBPSAVING) {		/* wait until save in other thread completes */		BBPspin(bid, "BBPsave", BBPSAVING);		if (lock)			gdk_unset_lock(GDKswapLock[bid & BBPLOCKMASK], "BBPsave");	} else {		/* save it */		int flags = BBPSAVING;		if (DELTAdirty(b)) {			flags |= BBPSWAPPED;			BBPdirty(1);		}		if (b->batPersistence != PERSISTENT) {			flags |= BBPTMP;		}		BBP_status_on(bid, flags, "BBPsave");		if (lock)			gdk_unset_lock(GDKswapLock[bid & BBPLOCKMASK], "BBPsave");		IODEBUG THRprintf(GDKout, "#save %s\n", BATgetId(b));		/* do the time-consuming work unlocked */         	if (BBP_status(bid) & BBPEXISTING) 			ret = BBPbackup(b, FALSE);		if (ret == 0) {			BBPout++;			ret = (BATsave(b) == NULL);		}		/* clearing bits can be done without the lock */		BBP_status_off(bid, BBPSAVING, "BBPsave");	}	return ret;}#line 1632 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bbp.mx"static int BBPaddtobin(BAT *b);static intBBPdestroy(BAT *b) {	int clear = 1;	bat parent = b->batParentid; 	if (VIEWparent(b)) { /* a physical view */		VIEWdestroy(b);	} else {		/* bats that get destroyed must unfix their atoms */		int (*hunfix) (ptr) = BATatoms[b->htype].atomUnfix;		int (*tunfix) (ptr) = BATatoms[b->ttype].atomUnfix;		BUN p, q;		int xx;		assert(b->batSharecnt == 0);		if (hunfix) {			DELloop(b, p, q, xx) {				(*hunfix) (BUNhead(b, p));			}			BATloopFast(b, p, q, xx) {				(*hunfix) (BUNhead(b, p));			}		}		if (tunfix) {			DELloop(b, p, q, xx) {				(*tunfix) (BUNtail(b, p));			}			BATloopFast(b, p, q, xx) {				(*tunfix) (BUNtail(b, p));			}		}		clear = BBPaddtobin(b);	/* plan for re-use */	}	if (clear)		BBPclear(b->batCacheid);	/* if destroyed; de-register from BBP */	/* parent released when completely done with child */	if (parent) unshare(parent);	return 0;}static intBBPfree(BAT *b, str calledFrom ){	bat bid = ABS(b->batCacheid), parent = b->batParentid;	int ret;	assert(BBPswappable(b));	/* write dirty BATs before being unloaded */	ret = BBPsave(b);        if (ret == 0) {                if (VIEWparent(b)) { /* physical view */		        VIEWdestroy(b);	        } else {	   	        assert(b->batSharecnt == 0); 		        if (BBP_cache(bid)) BATfree(b);	/* free memory */	        }	        BBPuncacheit_(bid, FALSE);	        if (b->batMapdirty) {		        DESCsetmodes(b, b);		        b->batMapdirty = 0;	        }	}	/* clearing bits can be done without the lock */	BBP_status_off(bid, BBPUNLOADING, calledFrom);	BBP_unload_dec(bid, calledFrom);		/* parent released when completely done with child */	if (ret == 0 && parent) unshare(parent);	return ret;}#line 1707 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bbp.mx"#line 1762 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bbp.mx"#define BBPMAXTRIM 40000#define BBPSMALLBAT 1000typedef struct {	int lastused;		/* bat lastused stamp; sort on this field */	bat bid;		/* bat id */	ssize_t cnt;		/* bat count */	int next;		/* next position in list */} bbptrim_t;bbptrim_t bbptrim[BBPMAXTRIM];int bbptrimfirst = BBPMAXTRIM, bbptrimlast = 0, bbpunloadtail, bbpunload, bbptrimmax = BBPMAXTRIM, bbpscanstart = 1;static batBBPtrim_scan(int mem, int vm, bat bbppos, bat bbplim){	bbptrimlast = 0;	bbptrimmax = BBPMAXTRIM;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -