Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cc4f58f

Browse files
committed
Ensure that all details of the ARC algorithm are hidden within freelist.c.
This refactoring does not change any algorithms or data structures, just remove visibility of the ARC datastructures from other source files.
1 parent ad893a3 commit cc4f58f

File tree

4 files changed

+129
-93
lines changed

4 files changed

+129
-93
lines changed

src/backend/storage/buffer/buf_init.c

+12-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/buffer/buf_init.c,v 1.70 2004/12/31 22:00:49 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/buffer/buf_init.c,v 1.71 2005/02/03 23:29:11 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -73,7 +73,6 @@ long int LocalBufferFlushCount;
7373
* aborts, it should only unpin the buffers exactly the number of times it
7474
* has pinned them, so that it will not blow away buffers of another
7575
* backend.
76-
*
7776
*/
7877

7978

@@ -120,14 +119,17 @@ InitBufferPool(void)
120119
block = BufferBlocks;
121120

122121
/*
123-
* link the buffers into a single linked list. This will become
124-
* the LIFO list of unused buffers returned by
125-
* StrategyGetBuffer().
122+
* Initialize all the buffer headers.
126123
*/
127124
for (i = 0; i < NBuffers; block += BLCKSZ, buf++, i++)
128125
{
129126
Assert(ShmemIsValid((unsigned long) block));
130127

128+
/*
129+
* The bufNext fields link together all totally-unused buffers.
130+
* Subsequent management of this list is done by
131+
* StrategyGetBuffer().
132+
*/
131133
buf->bufNext = i + 1;
132134

133135
CLEAR_BUFFERTAG(buf->tag);
@@ -142,7 +144,7 @@ InitBufferPool(void)
142144
buf->wait_backend_id = 0;
143145
}
144146

145-
/* Correct last entry */
147+
/* Correct last entry of linked list */
146148
BufferDescriptors[NBuffers - 1].bufNext = -1;
147149

148150
LWLockRelease(BufMgrLock);
@@ -178,7 +180,8 @@ InitBufferPoolAccess(void)
178180

179181
/*
180182
* Convert shmem offsets into addresses as seen by this process. This
181-
* is just to speed up the BufferGetBlock() macro.
183+
* is just to speed up the BufferGetBlock() macro. It is OK to do this
184+
* without any lock since the data pointers never change.
182185
*/
183186
for (i = 0; i < NBuffers; i++)
184187
BufferBlockPointers[i] = (Block) MAKE_PTR(BufferDescriptors[i].data);
@@ -201,14 +204,8 @@ BufferShmemSize(void)
201204
/* size of data pages */
202205
size += NBuffers * MAXALIGN(BLCKSZ);
203206

204-
/* size of buffer hash table */
205-
size += hash_estimate_size(NBuffers * 2, sizeof(BufferLookupEnt));
206-
207-
/* size of the shared replacement strategy control block */
208-
size += MAXALIGN(sizeof(BufferStrategyControl));
209-
210-
/* size of the ARC directory blocks */
211-
size += MAXALIGN(NBuffers * 2 * sizeof(BufferStrategyCDB));
207+
/* size of stuff controlled by freelist.c */
208+
size += StrategyShmemSize();
212209

213210
return size;
214211
}

src/backend/storage/buffer/buf_table.c

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*-------------------------------------------------------------------------
22
*
33
* buf_table.c
4-
* routines for finding buffers in the buffer pool.
4+
* routines for mapping BufferTags to buffer indexes.
55
*
6-
* NOTE: these days, what this table actually provides is a mapping from
7-
* BufferTags to CDB indexes, not directly to buffers. The function names
8-
* are thus slight misnomers.
6+
* NOTE: this module is called only by freelist.c, and the "buffer IDs"
7+
* it deals with are whatever freelist.c needs them to be; they may not be
8+
* directly equivalent to Buffer numbers.
99
*
1010
* Note: all routines in this file assume that the BufMgrLock is held
1111
* by the caller, so no synchronization is needed.
@@ -16,7 +16,7 @@
1616
*
1717
*
1818
* IDENTIFICATION
19-
* $PostgreSQL: pgsql/src/backend/storage/buffer/buf_table.c,v 1.38 2004/12/31 22:00:49 pgsql Exp $
19+
* $PostgreSQL: pgsql/src/backend/storage/buffer/buf_table.c,v 1.39 2005/02/03 23:29:11 tgl Exp $
2020
*
2121
*-------------------------------------------------------------------------
2222
*/
@@ -26,12 +26,29 @@
2626
#include "storage/bufmgr.h"
2727

2828

29+
/* entry for buffer lookup hashtable */
30+
typedef struct
31+
{
32+
BufferTag key; /* Tag of a disk page */
33+
int id; /* Associated buffer ID */
34+
} BufferLookupEnt;
35+
2936
static HTAB *SharedBufHash;
3037

3138

39+
/*
40+
* Estimate space needed for mapping hashtable
41+
* size is the desired hash table size (possibly more than NBuffers)
42+
*/
43+
int
44+
BufTableShmemSize(int size)
45+
{
46+
return hash_estimate_size(size, sizeof(BufferLookupEnt));
47+
}
48+
3249
/*
3350
* Initialize shmem hash table for mapping buffers
34-
* size is the desired hash table size (2*NBuffers for ARC algorithm)
51+
* size is the desired hash table size (possibly more than NBuffers)
3552
*/
3653
void
3754
InitBufTable(int size)
@@ -56,7 +73,7 @@ InitBufTable(int size)
5673

5774
/*
5875
* BufTableLookup
59-
* Lookup the given BufferTag; return CDB index, or -1 if not found
76+
* Lookup the given BufferTag; return buffer ID, or -1 if not found
6077
*/
6178
int
6279
BufTableLookup(BufferTag *tagPtr)
@@ -76,10 +93,10 @@ BufTableLookup(BufferTag *tagPtr)
7693

7794
/*
7895
* BufTableInsert
79-
* Insert a hashtable entry for given tag and CDB index
96+
* Insert a hashtable entry for given tag and buffer ID
8097
*/
8198
void
82-
BufTableInsert(BufferTag *tagPtr, int cdb_id)
99+
BufTableInsert(BufferTag *tagPtr, int buf_id)
83100
{
84101
BufferLookupEnt *result;
85102
bool found;
@@ -92,15 +109,15 @@ BufTableInsert(BufferTag *tagPtr, int cdb_id)
92109
(errcode(ERRCODE_OUT_OF_MEMORY),
93110
errmsg("out of shared memory")));
94111

95-
if (found) /* found something else in the table? */
112+
if (found) /* found something already in the table? */
96113
elog(ERROR, "shared buffer hash table corrupted");
97114

98-
result->id = cdb_id;
115+
result->id = buf_id;
99116
}
100117

101118
/*
102119
* BufTableDelete
103-
* Delete the hashtable entry for given tag
120+
* Delete the hashtable entry for given tag (which must exist)
104121
*/
105122
void
106123
BufTableDelete(BufferTag *tagPtr)

src/backend/storage/buffer/freelist.c

+73-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
* freelist.c
44
* routines for manipulating the buffer pool's replacement strategy.
55
*
6+
* The name "freelist.c" is now a bit of a misnomer, since this module
7+
* controls not only the list of free buffers per se, but the entire
8+
* mechanism for looking up existing shared buffers and the strategy
9+
* for choosing replacement victims when needed.
10+
*
611
* Note: all routines in this file assume that the BufMgrLock is held
712
* by the caller, so no synchronization is needed.
813
*
@@ -12,7 +17,7 @@
1217
*
1318
*
1419
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/backend/storage/buffer/freelist.c,v 1.49 2004/12/31 22:00:49 pgsql Exp $
20+
* $PostgreSQL: pgsql/src/backend/storage/buffer/freelist.c,v 1.50 2005/02/03 23:29:11 tgl Exp $
1621
*
1722
*-------------------------------------------------------------------------
1823
*/
@@ -25,6 +30,51 @@
2530
#include "storage/bufmgr.h"
2631

2732

33+
/*
34+
* Definitions for the buffer replacement strategy
35+
*/
36+
#define STRAT_LIST_UNUSED (-1)
37+
#define STRAT_LIST_B1 0
38+
#define STRAT_LIST_T1 1
39+
#define STRAT_LIST_T2 2
40+
#define STRAT_LIST_B2 3
41+
#define STRAT_NUM_LISTS 4
42+
43+
/*
44+
* The Cache Directory Block (CDB) of the Adaptive Replacement Cache (ARC)
45+
*/
46+
typedef struct
47+
{
48+
int prev; /* list links */
49+
int next;
50+
short list; /* ID of list it is currently in */
51+
bool t1_vacuum; /* t => present only because of VACUUM */
52+
TransactionId t1_xid; /* the xid this entry went onto T1 */
53+
BufferTag buf_tag; /* page identifier */
54+
int buf_id; /* currently assigned data buffer, or -1 */
55+
} BufferStrategyCDB;
56+
57+
/*
58+
* The shared ARC control information.
59+
*/
60+
typedef struct
61+
{
62+
int target_T1_size; /* What T1 size are we aiming for */
63+
int listUnusedCDB; /* All unused StrategyCDB */
64+
int listHead[STRAT_NUM_LISTS]; /* ARC lists B1, T1, T2
65+
* and B2 */
66+
int listTail[STRAT_NUM_LISTS];
67+
int listSize[STRAT_NUM_LISTS];
68+
Buffer listFreeBuffers; /* List of unused buffers */
69+
70+
long num_lookup; /* Some hit statistics */
71+
long num_hit[STRAT_NUM_LISTS];
72+
time_t stat_report;
73+
74+
/* Array of CDB's starts here */
75+
BufferStrategyCDB cdb[1]; /* VARIABLE SIZE ARRAY */
76+
} BufferStrategyControl;
77+
2878
/* GUC variable: time in seconds between statistics reports */
2979
int DebugSharedBuffers = 0;
3080

@@ -812,6 +862,28 @@ StrategyDirtyBufferList(BufferDesc **buffers, BufferTag *buftags,
812862
}
813863

814864

865+
/*
866+
* StrategyShmemSize
867+
*
868+
* estimate the size of shared memory used by the freelist-related structures.
869+
*/
870+
int
871+
StrategyShmemSize(void)
872+
{
873+
int size = 0;
874+
875+
/* size of CDB lookup hash table */
876+
size += BufTableShmemSize(NBuffers * 2);
877+
878+
/* size of the shared replacement strategy control block */
879+
size += MAXALIGN(sizeof(BufferStrategyControl));
880+
881+
/* size of the ARC directory blocks */
882+
size += MAXALIGN(NBuffers * 2 * sizeof(BufferStrategyCDB));
883+
884+
return size;
885+
}
886+
815887
/*
816888
* StrategyInitialize -- initialize the buffer cache replacement
817889
* strategy.

0 commit comments

Comments
 (0)