| 1 |
Synchronization in the Locking Subsystem |
| 2 |
|
| 3 |
This is a document that describes how we implemented fine-grain locking |
| 4 |
in the lock manager (that is, locking on a hash bucket level instead of |
| 5 |
locking the entire region). We found that the increase in concurrency |
| 6 |
was not sufficient to warrant the increase in complexity or the additional |
| 7 |
cost of performing each lock operation. Therefore, we don't use this |
| 8 |
any more. Should we have to do fine-grain locking in a future release, |
| 9 |
this would be a reasonable starting point. |
| 10 |
|
| 11 |
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 12 |
1. Data structures |
| 13 |
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 14 |
|
| 15 |
The lock manager maintains 3 different structures: |
| 16 |
|
| 17 |
Objects (__db_lockobj): |
| 18 |
Describes an object that is locked. When used with DB, this consists |
| 19 |
of a __db_ilock (a file identifier and a page number). |
| 20 |
|
| 21 |
Lockers (__db_locker): |
| 22 |
Identifies a specific locker ID and maintains the head of a list of |
| 23 |
locks held by a locker (for using during transaction commit/abort). |
| 24 |
|
| 25 |
Locks (__db_lock): |
| 26 |
Describes a particular object lock held on behalf of a particular |
| 27 |
locker id. |
| 28 |
|
| 29 |
Objects and Lockers reference Locks. |
| 30 |
|
| 31 |
These structures are organized via two synchronized hash tables. Each |
| 32 |
hash table consists of two physical arrays: the array of actual hash |
| 33 |
buckets and an array of mutexes so we can lock individual buckets, rather |
| 34 |
than the whole table. |
| 35 |
|
| 36 |
One hash table contains Objects and the other hash table contains Lockers. |
| 37 |
Objects contain two lists of locks, waiters and holders: holders currently |
| 38 |
hold a lock on the Object, waiters are lock waiting to be granted. |
| 39 |
Lockers are a single linked list that connects the Locks held on behalf |
| 40 |
of the specific locker ID. |
| 41 |
|
| 42 |
In the diagram below: |
| 43 |
|
| 44 |
Locker ID #1 holds a lock on Object #1 (L1) and Object #2 (L5), and is |
| 45 |
waiting on a lock on Object #1 (L3). |
| 46 |
|
| 47 |
Locker ID #2 holds a lock on Object #1 (L2) and is waiting on a lock for |
| 48 |
Object #2 (L7). |
| 49 |
|
| 50 |
Locker ID #3 is waiting for a lock on Object #2 (L6). |
| 51 |
|
| 52 |
OBJECT ----------------------- |
| 53 |
HASH | | |
| 54 |
----|------------- | |
| 55 |
________ _______ | | ________ | | |
| 56 |
| |-->| O1 |--|---|-->| O2 | | | |
| 57 |
|_______| |_____| | | |______| V | |
| 58 |
| | W H--->L1->L2 W H--->L5 | holders |
| 59 |
|_______| | | | | V |
| 60 |
| | ------->L3 \ ------->L6------>L7 waiters |
| 61 |
|_______| / \ \ |
| 62 |
. . / \ \ |
| 63 |
. . | \ \ |
| 64 |
. . | \ ----------- |
| 65 |
|_______| | -------------- | |
| 66 |
| | ____|____ ___|_____ _|______ |
| 67 |
|_______| | | | | | | |
| 68 |
| | | LID1 | | LID2 | | LID3 | |
| 69 |
|_______| |_______| |_______| |______| |
| 70 |
^ ^ ^ |
| 71 |
| | | |
| 72 |
___|________________________|________|___ |
| 73 |
LOCKER | | | | | | | | | |
| 74 |
HASH | | | | | | | | | |
| 75 |
| | | | | | | | | |
| 76 |
|____|____|____|____|____|____|____|____| |
| 77 |
|
| 78 |
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 79 |
2. Synchronization |
| 80 |
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 81 |
|
| 82 |
There are four types of mutexes in the subsystem. |
| 83 |
|
| 84 |
Object mutexes; |
| 85 |
These map one-to-one to each bucket in the Object hash table. |
| 86 |
Holding a mutex on an Object bucket secures all the Objects in |
| 87 |
that bucket as well as the Lock structures linked from those |
| 88 |
Objects. All fields in the Locks EXCEPT the Locker links (the |
| 89 |
links that attach Locks by Locker ID) are protected by these |
| 90 |
mutexes. |
| 91 |
|
| 92 |
Locker mutexes: |
| 93 |
These map one-to-one to each bucket in the Locker hash table. |
| 94 |
Holding a mutex on a Locker bucket secures the Locker structures |
| 95 |
and the Locker links in the Locks. |
| 96 |
|
| 97 |
Memory mutex: |
| 98 |
This mutex allows calls to allocate/free memory, i.e. calls to |
| 99 |
__db_shalloc and __db_shalloc_free, as well as manipulation of |
| 100 |
the Object, Locker and Lock free lists. |
| 101 |
|
| 102 |
Region mutex: |
| 103 |
This mutex is currently only used to protect the locker ids. |
| 104 |
It may also be needed later to provide exclusive access to |
| 105 |
the region for deadlock detection. |
| 106 |
|
| 107 |
Creating or removing a Lock requires locking both the Object lock and the |
| 108 |
Locker lock (and eventually the shalloc lock to return the item to the |
| 109 |
free list). |
| 110 |
|
| 111 |
The locking hierarchy is as follows: |
| 112 |
|
| 113 |
The Region mutex may never be acquired after any other mutex. |
| 114 |
|
| 115 |
The Object mutex may be acquired after the Region mutex. |
| 116 |
|
| 117 |
The Locker mutex may be acquired after the Region and Object |
| 118 |
mutexes. |
| 119 |
|
| 120 |
The Memory mutex may be acquired after any mutex. |
| 121 |
|
| 122 |
So, if both and Object mutex and a Locker mutex are going to be acquired, |
| 123 |
the Object mutex must be acquired first. |
| 124 |
|
| 125 |
The Memory mutex may be acquired after any other mutex, but no other mutexes |
| 126 |
can be acquired once the Memory mutex is held. |
| 127 |
|
| 128 |
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 129 |
3. The algorithms: |
| 130 |
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 131 |
The locking subsystem supports four basic operations: |
| 132 |
Get a Lock (lock_get) |
| 133 |
|
| 134 |
Release a Lock (lock_put) |
| 135 |
|
| 136 |
Release all the Locks on a specific Object (lock_vec) |
| 137 |
|
| 138 |
Release all the Locks for a specific Locker (lock_vec) |
| 139 |
|
| 140 |
Get a lock: |
| 141 |
Acquire Object bucket mutex. |
| 142 |
Acquire Locker bucket mutex. |
| 143 |
|
| 144 |
Acquire Memory mutex. |
| 145 |
If the Object does not exist |
| 146 |
Take an Object off the freelist. |
| 147 |
If the Locker doesn't exist |
| 148 |
Take a Locker off the freelist. |
| 149 |
Take a Lock off the free list. |
| 150 |
Release Memory mutex. |
| 151 |
|
| 152 |
Add Lock to the Object list. |
| 153 |
Add Lock to the Locker list. |
| 154 |
Release Locker bucket mutex |
| 155 |
|
| 156 |
If the lock cannot be granted |
| 157 |
Release Object bucket mutex |
| 158 |
Acquire lock mutex (blocks) |
| 159 |
|
| 160 |
Acquire Object bucket mutex |
| 161 |
If lock acquisition did not succeed (e.g, deadlock) |
| 162 |
Acquire Locker bucket mutex |
| 163 |
If locker should be destroyed |
| 164 |
Remove locker from hash table |
| 165 |
Acquire Memory mutex |
| 166 |
Return locker to free list |
| 167 |
Release Memory mutex |
| 168 |
Release Locker bucket mutex |
| 169 |
|
| 170 |
If object should be released |
| 171 |
Acquire Memory mutex |
| 172 |
Return object to free list |
| 173 |
Release Memory mutex |
| 174 |
|
| 175 |
Release Object bucket mutex |
| 176 |
|
| 177 |
Release a lock: |
| 178 |
Acquire Object bucket mutex. |
| 179 |
(Requires that we be able to find the Object hash bucket |
| 180 |
without looking inside the Lock itself.) |
| 181 |
|
| 182 |
If releasing a single lock and the user provided generation number |
| 183 |
doesn't match the Lock's generation number, the Lock has been reused |
| 184 |
and we return failure. |
| 185 |
|
| 186 |
Enter lock_put_internal: |
| 187 |
if the Lock is still on the Object's lists: |
| 188 |
Increment Lock's generation number. |
| 189 |
Remove Lock from the Object's list (NULL link fields). |
| 190 |
Promote locks for the Object. |
| 191 |
|
| 192 |
Enter locker_list_removal |
| 193 |
Acquire Locker bucket mutex. |
| 194 |
If Locker doesn't exist: |
| 195 |
Release Locker bucket mutex |
| 196 |
Release Object bucket mutex |
| 197 |
Return error. |
| 198 |
Else if Locker marked as deleted: |
| 199 |
dont_release = TRUE |
| 200 |
Else |
| 201 |
Remove Lock from Locker list. |
| 202 |
If Locker has no more locks |
| 203 |
Remove Locker from table. |
| 204 |
Acquire Memory mutex. |
| 205 |
Return Locker to free list |
| 206 |
Release Memory mutex |
| 207 |
Release Locker bucket mutex. |
| 208 |
Exit locker_list_removal |
| 209 |
|
| 210 |
If (!dont_release) |
| 211 |
Acquire Memory mutex |
| 212 |
Return Lock to free list |
| 213 |
Release Memory mutex |
| 214 |
|
| 215 |
Exit lock_put_internal |
| 216 |
|
| 217 |
Release Object bucket mutex |
| 218 |
|
| 219 |
Release all the Locks on a specific Object (lock_vec, DB_PUT_ALL_OBJ): |
| 220 |
|
| 221 |
Acquire Object bucket mutex. |
| 222 |
|
| 223 |
For each lock on the waiter list: |
| 224 |
lock_put_internal |
| 225 |
For each lock on the holder list: |
| 226 |
lock_put_internal |
| 227 |
|
| 228 |
Release Object bucket mutex. |
| 229 |
|
| 230 |
Release all the Locks for a specific Locker (lock_vec, DB_PUT_ALL): |
| 231 |
|
| 232 |
Acquire Locker bucket mutex. |
| 233 |
Mark Locker deleted. |
| 234 |
Release Locker mutex. |
| 235 |
|
| 236 |
For each lock on the Locker's list: |
| 237 |
Remove from locker's list |
| 238 |
(The lock could get put back on the free list in |
| 239 |
lock_put and then could get reallocated and the |
| 240 |
act of setting its locker links could clobber us.) |
| 241 |
Perform "Release a Lock" above: skip locker_list_removal. |
| 242 |
|
| 243 |
Acquire Locker bucket mutex. |
| 244 |
Remove Locker |
| 245 |
Release Locker mutex. |
| 246 |
|
| 247 |
Acquire Memory mutex |
| 248 |
Return Locker to free list |
| 249 |
Release Memory mutex |
| 250 |
|
| 251 |
Deadlock detection (lock_detect): |
| 252 |
|
| 253 |
For each bucket in Object table |
| 254 |
Acquire the Object bucket mutex. |
| 255 |
create waitsfor |
| 256 |
|
| 257 |
For each bucket in Object table |
| 258 |
Release the Object mutex. |
| 259 |
|
| 260 |
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 261 |
FAQ: |
| 262 |
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 263 |
Q: Why do you need generation numbers? |
| 264 |
A: If a lock has been released due to a transaction abort (potentially in a |
| 265 |
different process), and then lock is released by a thread of control |
| 266 |
unaware of the abort, the lock might have potentially been re-allocated |
| 267 |
to a different object. The generation numbers detect this problem. |
| 268 |
|
| 269 |
Note, we assume that reads/writes of lock generation numbers are atomic, |
| 270 |
if they are not, it is theoretically possible that a re-allocated lock |
| 271 |
could be mistaken for another lock. |
| 272 |
|
| 273 |
Q: Why is is safe to walk the Locker list without holding any mutexes at |
| 274 |
all? |
| 275 |
A: Locks are created with both the Object and Locker bucket mutexes held. |
| 276 |
Once created, they removed in two ways: |
| 277 |
|
| 278 |
a) when a specific Lock is released, in which case, the Object and |
| 279 |
Locker bucket mutexes are again held, and |
| 280 |
|
| 281 |
b) when all Locks for a specific Locker Id is released. |
| 282 |
|
| 283 |
In case b), the Locker bucket mutex is held while the Locker chain is |
| 284 |
marked as "destroyed", which blocks any further access to the Locker |
| 285 |
chain. Then, each individual Object bucket mutex is acquired when each |
| 286 |
individual Lock is removed. |
| 287 |
|
| 288 |
Q: What are the implications of doing fine grain locking? |
| 289 |
|
| 290 |
A: Since we no longer globally lock the entire region, lock_vec will no |
| 291 |
longer be atomic. We still execute the items in a lock_vec in order, |
| 292 |
so things like lock-coupling still work, but you can't make any |
| 293 |
guarantees about atomicity. |
| 294 |
|
| 295 |
Q: How do I configure for FINE_GRAIN locking? |
| 296 |
|
| 297 |
A: We currently do not support any automatic configuration for FINE_GRAIN |
| 298 |
locking. When we do, will need to document that atomicity discussion |
| 299 |
listed above (it is bug-report #553). |
| 300 |
|
| 301 |
Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved. |