buf_internals.h 6.4 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * buf_internals.h--
4
 *	  Internal definitions.
5 6 7 8
 *
 *
 * Copyright (c) 1994, Regents of the University of California
 *
9
 * $Id: buf_internals.h,v 1.21 1998/02/26 04:43:21 momjian Exp $
10 11
 *
 * NOTE
12 13
 *		If BUFFERPAGE0 is defined, then 0 will be used as a
 *		valid buffer page number.
14 15 16
 *
 *-------------------------------------------------------------------------
 */
17
#ifndef BUFMGR_INTERNALS_H
18 19
#define BUFMGR_INTERNALS_H

20 21
#include <storage/lmgr.h>
#include <storage/buf.h>
22 23 24

/* Buf Mgr constants */
/* in bufmgr.c */
25 26 27 28 29
extern int	NBuffers;
extern int	Data_Descriptors;
extern int	Free_List_Descriptor;
extern int	Lookup_List_Descriptor;
extern int	Num_Descriptors;
30 31 32 33

/*
 * Flags for buffer descriptors
 */
34 35 36 37 38 39 40 41
#define BM_DIRTY				(1 << 0)
#define BM_PRIVATE				(1 << 1)
#define BM_VALID				(1 << 2)
#define BM_DELETED				(1 << 3)
#define BM_FREE					(1 << 4)
#define BM_IO_IN_PROGRESS		(1 << 5)
#define BM_IO_ERROR				(1 << 6)
#define BM_JUST_DIRTIED			(1 << 7)
42

43
typedef bits16 BufFlags;
44 45 46 47

typedef struct sbufdesc BufferDesc;
typedef struct sbufdesc BufferHdr;
typedef struct buftag BufferTag;
48

49
/* long * so alignment will be correct */
50
typedef long **BufferBlock;
51

52 53
struct buftag
{
54 55
	LRelId		relId;
	BlockNumber blockNum;		/* blknum relative to begin of reln */
56 57 58 59 60 61 62 63 64 65 66 67
};

#define CLEAR_BUFFERTAG(a)\
  (a)->relId.dbId = InvalidOid; \
  (a)->relId.relId = InvalidOid; \
  (a)->blockNum = InvalidBlockNumber

#define INIT_BUFFERTAG(a,xx_reln,xx_blockNum) \
{ \
  (a)->blockNum = xx_blockNum;\
  (a)->relId = RelationGetLRelId(xx_reln); \
}
68
#ifdef NOT_USED
69 70 71 72 73 74 75 76 77
#define COPY_BUFFERTAG(a,b)\
{ \
  (a)->blockNum = (b)->blockNum;\
  LRelIdAssign(*(a),*(b));\
}

#define EQUAL_BUFFERTAG(a,b) \
  (((a)->blockNum == (b)->blockNum) &&\
   (OID_Equal((a)->relId.relId,(b)->relId.relId)))
78
#endif
79 80 81 82 83

#define BAD_BUFFER_ID(bid) ((bid<1) || (bid>(NBuffers)))
#define INVALID_DESCRIPTOR (-3)

/*
84 85 86 87 88
 *	bletch hack -- anyplace that we declare space for relation or
 *	database names, we just use '16', not a symbolic constant, to
 *	specify their lengths.	BM_NAMESIZE is the length of these names,
 *	and is used in the buffer manager code.  somebody with lots of
 *	spare time should do this for all the other modules, too.
89
 */
90
#define BM_NAMESIZE		16
91 92

/*
93 94
 *	struct sbufdesc -- shared buffer cache metadata for a single
 *					   shared buffer descriptor.
95
 *
96 97 98 99 100 101 102
 *		We keep the name of the database and relation in which this
 *		buffer appears in order to avoid a catalog lookup on cache
 *		flush if we don't have the reldesc in the cache.  It is also
 *		possible that the relation to which this buffer belongs is
 *		not visible to all backends at the time that it gets flushed.
 *		Dbname, relname, dbid, and relid are enough to determine where
 *		to put the buffer, for all storage managers.
103 104
 */

105
#define PADDED_SBUFDESC_SIZE	128
106 107 108 109 110

/* DO NOT CHANGE THIS NEXT STRUCTURE:
   It is used only to get padding information for the real sbufdesc structure
   It should match the sbufdesc structure exactly except for a missing sb_pad
*/
111 112
struct sbufdesc_unpadded
{
113 114 115 116 117 118 119
	Buffer		freeNext;
	Buffer		freePrev;
	SHMEM_OFFSET data;
	BufferTag	tag;
	int			buf_id;
	BufFlags	flags;
	unsigned	refcount;
120
#ifdef HAS_TEST_AND_SET
121
	slock_t		io_in_progress_lock;
122
#endif							/* HAS_TEST_AND_SET */
123
	char		sb_dbname[NAMEDATALEN];
124

125
	/* NOTE NO PADDING OF THE MEMBER HERE */
126
	char		sb_relname[NAMEDATALEN];
127 128 129
};

/* THE REAL STRUCTURE - the structure above must match it, minus sb_pad */
130 131
struct sbufdesc
{
132 133 134
	Buffer		freeNext;		/* link for freelist chain */
	Buffer		freePrev;
	SHMEM_OFFSET data;			/* pointer to data in buf pool */
135

136
	/* tag and id must be together for table lookup to work */
137 138
	BufferTag	tag;			/* file/block identifier */
	int			buf_id;			/* maps global desc to local desc */
139

140 141
	BufFlags	flags;			/* described below */
	unsigned	refcount;		/* # of times buffer is pinned */
142 143

#ifdef HAS_TEST_AND_SET
144
	/* can afford a dedicated lock if test-and-set locks are available */
145
	slock_t		io_in_progress_lock;
146 147
#endif							/* HAS_TEST_AND_SET */

148
	char		sb_dbname[NAMEDATALEN]; /* name of db in which buf belongs */
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

	/*
	 * I padded this structure to a power of 2 (PADDED_SBUFDESC_SIZE)
	 * because BufferDescriptorGetBuffer is called a billion times and it
	 * does an C pointer subtraction (i.e., "x - y" -> array index of x
	 * relative to y, which is calculated using division by struct size).
	 * Integer ".div" hits you for 35 cycles, as opposed to a 1-cycle
	 * "sra" ... this hack cut 10% off of the time to create the Wisconsin
	 * database! It eats up more shared memory, of course, but we're
	 * (allegedly) going to make some of these types bigger soon anyway...
	 * -pma 1/2/93
	 */

	/*
	 * please, don't take the sizeof() this member and use it for
	 * something important
	 */

167 168
	char		sb_relname[NAMEDATALEN +		/* name of reln */
				PADDED_SBUFDESC_SIZE - sizeof(struct sbufdesc_unpadded)];
169 170 171
};

/*
172
 *	mao tracing buffer allocation
173 174 175 176 177
 */

/*#define BMTRACE*/
#ifdef BMTRACE

178 179
typedef struct _bmtrace
{
180 181 182 183 184 185
	int			bmt_pid;
	long		bmt_buf;
	long		bmt_dbid;
	long		bmt_relid;
	int			bmt_blkno;
	int			bmt_op;
186

187
#define BMT_NOTUSED		0
188
#define BMT_ALLOCFND	1
189 190
#define BMT_ALLOCNOTFND 2
#define BMT_DEALLOC		3
191

192
}			bmtrace;
193

194
#endif							/* BMTRACE */
195 196


197
/*
198 199 200 201 202 203
 * Bufmgr Interface:
 */

/* Internal routines: only called by buf.c */

/*freelist.c*/
B
Bruce Momjian 已提交
204 205 206 207
extern void AddBufferToFreelist(BufferDesc *bf);
extern void PinBuffer(BufferDesc *buf);
extern void PinBuffer_Debug(char *file, int line, BufferDesc *buf);
extern void UnpinBuffer(BufferDesc *buf);
208
extern BufferDesc *GetFreeBuffer(void);
209
extern void InitFreeList(bool init);
210 211

/* buf_table.c */
212
extern void InitBufTable(void);
213
extern BufferDesc *BufTableLookup(BufferTag *tagPtr);
B
Bruce Momjian 已提交
214 215
extern bool BufTableDelete(BufferDesc *buf);
extern bool BufTableInsert(BufferDesc *buf);
216 217

/* bufmgr.c */
218 219
extern BufferDesc *BufferDescriptors;
extern BufferBlock BufferBlocks;
220 221 222
extern long *PrivateRefCount;
extern long *LastRefCount;
extern long *CommitInfoNeedsSave;
223
extern SPINLOCK BufMgrLock;
224 225

/* localbuf.c */
226
extern long *LocalRefCount;
227
extern BufferDesc *LocalBufferDescriptors;
228
extern int	NLocBuffer;
229

230 231
extern BufferDesc *
LocalBufferAlloc(Relation reln, BlockNumber blockNum,
232
				 bool *foundPtr);
233 234 235 236 237
extern int	WriteLocalBuffer(Buffer buffer, bool release);
extern int	FlushLocalBuffer(Buffer buffer, bool release);
extern void InitLocalBuffer(void);
extern void LocalBufferSync(void);
extern void ResetLocalBufferPool(void);
238 239

#endif							/* BUFMGR_INTERNALS_H */