- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathGarbageCollector.cpp
219 lines (169 loc) · 5.08 KB
/
GarbageCollector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Vlad Khorsun
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2011 Vlad Khorsun <hvlad@users.sourceforge.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include"../common/classes/alloc.h"
#include"../jrd/GarbageCollector.h"
#include"../jrd/tra.h"
usingnamespaceJrd;
usingnamespaceFirebird;
namespaceJrd {
voidGarbageCollector::RelationData::clear()
{
m_pages.clear();
}
TraNumber GarbageCollector::RelationData::findPage(const ULONG pageno, const TraNumber tranid)
{
PageTranMap::Accessor pages(&m_pages);
if (!pages.locate(pageno))
return MAX_TRA_NUMBER;
// hvlad: this routine could be guarded by shared sync - therefore comparison
// and assignment below should be atomic operation. But we don't require
// exact precision here.
if (pages.current().tranid > tranid)
pages.current().tranid = tranid;
return pages.current().tranid;
}
TraNumber GarbageCollector::RelationData::addPage(const ULONG pageno, const TraNumber tranid)
{
TraNumber findTran = findPage(pageno, tranid);
if (findTran != MAX_TRA_NUMBER)
return findTran;
m_pages.add(PageTran(pageno, tranid));
return tranid;
}
voidGarbageCollector::RelationData::swept(const TraNumber oldest_snapshot, PageBitmap** bm)
{
PageTranMap::Accessor pages(&m_pages);
bool next = pages.getFirst();
while (next)
{
if (pages.current().tranid < oldest_snapshot)
{
if (bm)
{
PBM_SET(&m_pool, bm, pages.current().pageno);
}
next = pages.fastRemove();
}
else
next = pages.getNext();
}
}
GarbageCollector::~GarbageCollector()
{
SyncLockGuard exGuard(&m_sync, SYNC_EXCLUSIVE, "GarbageCollector::~GarbageCollector");
for (FB_SIZE_T pos = 0; pos < m_relations.getCount(); pos++)
{
RelationData* relData = m_relations[pos];
Sync sync(&relData->m_sync, "GarbageCollector::~GarbageCollector");
sync.lock(SYNC_EXCLUSIVE);
m_relations[pos] = NULL;
sync.unlock();
delete relData;
}
m_relations.clear();
}
TraNumber GarbageCollector::addPage(const USHORT relID, const ULONG pageno, const TraNumber tranid)
{
Sync syncGC(&m_sync, "GarbageCollector::addPage");
RelationData* relData = getRelData(syncGC, relID, true);
SyncLockGuard syncData(&relData->m_sync, SYNC_SHARED, "GarbageCollector::addPage");
TraNumber minTraID = relData->findPage(pageno, tranid);
if (minTraID != MAX_TRA_NUMBER)
return minTraID;
syncData.unlock();
syncData.lock(SYNC_EXCLUSIVE, "GarbageCollector::addPage");
syncGC.unlock();
return relData->addPage(pageno, tranid);
}
PageBitmap* GarbageCollector::getPages(const TraNumber oldest_snapshot, USHORT &relID)
{
SyncLockGuard shGuard(&m_sync, SYNC_SHARED, "GarbageCollector::getPages");
if (m_relations.isEmpty())
{
m_nextRelID = 0;
returnNULL;
}
FB_SIZE_T pos;
if (!m_relations.find(m_nextRelID, pos) && (pos == m_relations.getCount()))
pos = 0;
for (; pos < m_relations.getCount(); pos++)
{
RelationData* relData = m_relations[pos];
SyncLockGuard syncData(&relData->m_sync, SYNC_EXCLUSIVE, "GarbageCollector::getPages");
PageBitmap* bm = NULL;
relData->swept(oldest_snapshot, &bm);
if (bm)
{
relID = relData->getRelID();
m_nextRelID = relID + 1;
return bm;
}
}
m_nextRelID = 0;
returnNULL;
}
voidGarbageCollector::removeRelation(const USHORT relID)
{
Sync syncGC(&m_sync, "GarbageCollector::removeRelation");
syncGC.lock(SYNC_EXCLUSIVE);
FB_SIZE_T pos;
if (!m_relations.find(relID, pos))
return;
RelationData* relData = m_relations[pos];
Sync syncData(&relData->m_sync, "GarbageCollector::removeRelation");
syncData.lock(SYNC_EXCLUSIVE);
m_relations.remove(pos);
syncGC.unlock();
syncData.unlock();
delete relData;
}
voidGarbageCollector::sweptRelation(const TraNumber oldest_snapshot, const USHORT relID)
{
Sync syncGC(&m_sync, "GarbageCollector::sweptRelation");
RelationData* relData = getRelData(syncGC, relID, false);
if (relData)
{
SyncLockGuard syncData(&relData->m_sync, SYNC_EXCLUSIVE, "GarbageCollector::sweptRelation");
syncGC.unlock();
relData->swept(oldest_snapshot);
}
}
GarbageCollector::RelationData* GarbageCollector::getRelData(Sync &sync, const USHORT relID,
bool allowCreate)
{
FB_SIZE_T pos;
sync.lock(SYNC_SHARED);
if (!m_relations.find(relID, pos))
{
if (!allowCreate)
returnNULL;
sync.unlock();
sync.lock(SYNC_EXCLUSIVE);
if (!m_relations.find(relID, pos))
{
m_relations.insert(pos, FB_NEW_POOL(m_pool) RelationData(m_pool, relID));
}
sync.downgrade(SYNC_SHARED);
}
return m_relations[pos];
}
} // namespace Jrd