8000 fixed up LRU, still not passing tests, need to look into it. Maybe mu… · dlhoang/postgres@86f15d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86f15d6

Browse files
committed
fixed up LRU, still not passing tests, need to look into it. Maybe multithreading issue?
1 parent e59325a commit 86f15d6

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

src/backend/storage/buffer/freelist.c

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,47 @@ void AddToLRUList(BufferDesc *buf)
144144

145145
/*
146146
* RemoveFromLRUList()
147+
* Remove item on list depending on where they are since nodes before them weren't removed
148+
* due to being pinned
147149
*/
148-
void RemoveFromLRUList()
150+
void RemoveFromLRUList(int trycounter)
149151
{
150152
LRUListNode *node;
153+
int loop;
151154
Assert(LRUList != NULL);
155+
152156
node = StrategyControl->LRUListHead;
153-
StrategyControl->LRUListHead = node->next;
154-
StrategyControl->LRUListHead->prev = NULL;
157+
loop = NBuffers - trycounter;
158+
159+
while (loop > 0) {
160+
node = node->next;
161+
loop--;
162+
}
163+
164+
// delete the only element
165+
if (node->prev == NULL && node->next == NULL)
166+
{
167+
StrategyControl->LRUListHead = NULL;
168+
StrategyControl->LRUListTail = NULL;
169+
}
170+
// delete head
171+
else if (node->prev == NULL && node->next != NULL)
172+
{
173+
StrategyControl->LRUListHead = node->next;
174+
StrategyControl->LRUListHead->prev = NULL;
175+
}
176+
// delete tail
177+
else if (node->prev != NULL && node->next == NULL)
178+
{
179+
StrategyControl->LRUListTail = node->prev;
180+
StrategyControl->LRUListTail->next = NULL;
181+
}
182+
// middle elements
183+
else
184+
{
185+
node->prev->next = node->next;
186+
node->next->prev = node->prev;
187+
}
155188
}
156189

157190
/*
@@ -198,10 +231,22 @@ Random(void)
198231
return GetBufferDescriptor(victim);
199232
}
200233

234+
/*
235+
* LRU - Helper routine for StrategyGetBuffer()
236+
* Return in the order they are on the list but don't remove yet because it might be pinned
237+
*/
201238
static inline BufferDesc *
202-
LRU(void)
239+
LRU(int trycounter)
203240
{
241+
int loop = NBuffers - trycounter;
204242
LRUListNode *node = StrategyControl->LRUListHead;
243+
244+
while (loop > 0)
245+
{
246+
node = node->next;
247+
loop--;
248+
}
249+
205250
return node->nodeId;
206251
}
207252

@@ -294,7 +339,7 @@ BufferDesc *doEviction(uint32 local_buf_state, BufferAccessStrategy strategy, ui
294339

295340
for (;;)
296341
{
297-
buf = LRU();
342+
buf = LRU(trycounter);
298343

299344
/*
300345
* If the buffer is pinned or has a nonzero usage_count, we cannot use
@@ -328,7 +373,7 @@ BufferDesc *doEviction(uint32 local_buf_state, BufferAccessStrategy strategy, ui
328373
AddBufferToRing(strategy, buf);
329374
if (LRU_EVICTION)
330375
{
331-
RemoveFromLRUList();
376+
RemoveFromLRUList(trycounter);
332377
}
333378
}
334379
*buf_state = local_buf_state;

0 commit comments

Comments
 (0)
0