|
49 | 49 | import java.util.Queue; |
50 | 50 | import java.util.concurrent.ConcurrentLinkedQueue; |
51 | 51 | import java.util.concurrent.atomic.AtomicInteger; |
| 52 | +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
52 | 53 | import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; |
53 | 54 | import java.util.concurrent.atomic.LongAdder; |
54 | 55 | import java.util.concurrent.locks.StampedLock; |
@@ -511,7 +512,7 @@ private void freeChunkReuseQueue(Thread ownerThread) { |
511 | 512 | // that reference some thread local chunk |
512 | 513 | threadLocalChunk.ownerThread = null; |
513 | 514 | } |
514 | | - chunk.release(); |
| 515 | + chunk.markToDeallocate(); |
515 | 516 | } |
516 | 517 | } |
517 | 518 | } |
@@ -633,7 +634,7 @@ public boolean offerChunk(Chunk chunk) { |
633 | 634 | break; |
634 | 635 | } |
635 | 636 | if (chunks.remove(key, toDeallocate)) { |
636 | | - toDeallocate.release(); |
| 637 | + toDeallocate.markToDeallocate(); |
637 | 638 | } |
638 | 639 | size = chunks.size(); |
639 | 640 | } |
@@ -1143,6 +1144,10 @@ void releaseSegment(int ignoredSegmentId, int size) { |
1143 | 1144 | release(); |
1144 | 1145 | } |
1145 | 1146 |
|
| 1147 | + void markToDeallocate() { |
| 1148 | + release(); |
| 1149 | + } |
| 1150 | + |
1146 | 1151 | private void retain() { |
1147 | 1152 | RefCnt.retain(refCnt); |
1148 | 1153 | } |
@@ -1249,8 +1254,30 @@ public int size() { |
1249 | 1254 | } |
1250 | 1255 | } |
1251 | 1256 |
|
| 1257 | + /** |
| 1258 | + * Replaces per-allocation retain()/release() ref counting with a segment-count state machine |
| 1259 | + * that only needs atomics on the cold deallocation path. |
| 1260 | + * <p> |
| 1261 | + * State transitions: |
| 1262 | + * <ul> |
| 1263 | + * <li>{@link #AVAILABLE} (-1): chunk is in use, no deallocation tracking needed</li> |
| 1264 | + * <li>0..N: local free list size at the time {@link #markToDeallocate()} was called; |
| 1265 | + * used to track when all segments have been returned</li> |
| 1266 | + * <li>{@link #DEALLOCATED} (Integer.MIN_VALUE): all segments returned, chunk deallocated</li> |
| 1267 | + * </ul> |
| 1268 | + * <p> |
| 1269 | + * Ordering: external {@link #releaseSegment} pushes to the MPSC queue (which has an implicit |
| 1270 | + * StoreLoad barrier via its {@code offer()}), then reads {@code state} — this guarantees |
| 1271 | + * visibility of any preceding {@link #markToDeallocate()} write. |
| 1272 | + */ |
1252 | 1273 | private static final class SizeClassedChunk extends Chunk { |
1253 | 1274 | private static final int FREE_LIST_EMPTY = -1; |
| 1275 | + private static final int AVAILABLE = -1; |
| 1276 | + private static final int DEALLOCATED = Integer.MIN_VALUE; |
| 1277 | + private static final AtomicIntegerFieldUpdater<SizeClassedChunk> STATE = |
| 1278 | + AtomicIntegerFieldUpdater.newUpdater(SizeClassedChunk.class, "state"); |
| 1279 | + private volatile int state; |
| 1280 | + private final int segments; |
1254 | 1281 | private final int segmentSize; |
1255 | 1282 | private final MpscIntQueue externalFreeList; |
1256 | 1283 | private final IntStack localFreeList; |
@@ -1260,6 +1287,8 @@ private static final class SizeClassedChunk extends Chunk { |
1260 | 1287 | SizeClassChunkController controller) { |
1261 | 1288 | super(delegate, magazine, true); |
1262 | 1289 | segmentSize = controller.segmentSize; |
| 1290 | + segments = controller.chunkSize / segmentSize; |
| 1291 | + STATE.lazySet(this, AVAILABLE); |
1263 | 1292 | ownerThread = magazine.group.ownerThread; |
1264 | 1293 | if (ownerThread == null) { |
1265 | 1294 | externalFreeList = controller.createFreeList(); |
@@ -1270,26 +1299,30 @@ private static final class SizeClassedChunk extends Chunk { |
1270 | 1299 | } |
1271 | 1300 | } |
1272 | 1301 |
|
| 1302 | + @Override |
| 1303 | + boolean releaseFromMagazine() { |
| 1304 | + Magazine mag = magazine; |
| 1305 | + detachFromMagazine(); |
| 1306 | + if (!mag.offerToQueue(this)) { |
| 1307 | + markToDeallocate(); |
| 1308 | + } |
| 1309 | + return false; |
| 1310 | + } |
| 1311 | + |
1273 | 1312 | @Override |
1274 | 1313 | public boolean readInitInto(AdaptiveByteBuf buf, int size, int startingCapacity, int maxCapacity) { |
| 1314 | + assert state == AVAILABLE; |
1275 | 1315 | final int startIndex = nextAvailableSegmentOffset(); |
1276 | 1316 | if (startIndex == FREE_LIST_EMPTY) { |
1277 | 1317 | return false; |
1278 | 1318 | } |
1279 | 1319 | allocatedBytes += segmentSize; |
1280 | | - Chunk chunk = this; |
1281 | | - chunk.retain(); |
1282 | 1320 | try { |
1283 | | - buf.init(delegate, chunk, 0, 0, startIndex, size, startingCapacity, maxCapacity); |
1284 | | - chunk = null; |
1285 | | - } finally { |
1286 | | - if (chunk != null) { |
1287 | | - // If chunk is not null we know that buf.init(...) failed and so we need to manually release |
1288 | | - // the chunk again as we retained it before calling buf.init(...). Beside this we also need to |
1289 | | - // restore the old allocatedBytes value. |
1290 | | - allocatedBytes -= segmentSize; |
1291 | | - chunk.releaseSegment(startIndex, startingCapacity); |
1292 | | - } |
| 1321 | + buf.init(delegate, this, 0, 0, startIndex, size, startingCapacity, maxCapacity); |
| 1322 | + } catch (Throwable t) { |
| 1323 | + allocatedBytes -= segmentSize; |
| 1324 | + releaseSegmentOffsetIntoFreeList(startIndex); |
| 1325 | + throw t; |
1293 | 1326 | } |
1294 | 1327 | return true; |
1295 | 1328 | } |
@@ -1348,8 +1381,63 @@ private void releaseSegmentOffsetIntoFreeList(int startIndex) { |
1348 | 1381 |
|
1349 | 1382 | @Override |
1350 | 1383 | void releaseSegment(int startIndex, int size) { |
1351 | | - release(); |
1352 | | - releaseSegmentOffsetIntoFreeList(startIndex); |
| 1384 | + IntStack localFreeList = this.localFreeList; |
| 1385 | + if (localFreeList != null && Thread.currentThread() == ownerThread) { |
| 1386 | + localFreeList.push(startIndex); |
| 1387 | + int state = this.state; |
| 1388 | + if (state != AVAILABLE) { |
| 1389 | + updateStateOnLocalReleaseSegment(state, localFreeList); |
| 1390 | + } |
| 1391 | + } else { |
| 1392 | + boolean segmentReturned = externalFreeList.offer(startIndex); |
| 1393 | + assert segmentReturned; |
| 1394 | + // implicit StoreLoad barrier from MPSC offer() |
| 1395 | + int state = this.state; |
| 1396 | + if (state != AVAILABLE) { |
| 1397 | + handleStateOnExternalReleaseSegment(state); |
| 1398 | + } |
| 1399 | + } |
| 1400 | + } |
| 1401 | + |
| 1402 | + private void handleStateOnExternalReleaseSegment(int localFreeListSize) { |
| 1403 | + // localFreeListSize is the number of segments that were in the local free list when |
| 1404 | + // markToDeallocate() was called. Check if all segments have been returned. |
| 1405 | + int totalFreeSegments = localFreeListSize + externalFreeList.size(); |
| 1406 | + if (totalFreeSegments == segments) { |
| 1407 | + if (STATE.compareAndSet(this, localFreeListSize, DEALLOCATED)) { |
| 1408 | + deallocate(); |
| 1409 | + } |
| 1410 | + } |
| 1411 | + } |
| 1412 | + |
| 1413 | + private void updateStateOnLocalReleaseSegment(int previousLocalSize, IntStack localFreeList) { |
| 1414 | + // Update the state to reflect the new local free list size. |
| 1415 | + int newLocalSize = localFreeList.size(); |
| 1416 | + if (!STATE.compareAndSet(this, previousLocalSize, newLocalSize)) { |
| 1417 | + // State was concurrently changed by another external releaseSegment that deallocated. |
| 1418 | + return; |
| 1419 | + } |
| 1420 | + // Check if all segments have been returned. |
| 1421 | + int totalFreeSegments = newLocalSize + externalFreeList.size(); |
| 1422 | + if (totalFreeSegments == segments) { |
| 1423 | + if (STATE.compareAndSet(this, newLocalSize, DEALLOCATED)) { |
| 1424 | + deallocate(); |
| 1425 | + } |
| 1426 | + } |
| 1427 | + } |
| 1428 | + |
| 1429 | + @Override |
| 1430 | + void markToDeallocate() { |
| 1431 | + IntStack localFreeList = this.localFreeList; |
| 1432 | + int localSize = localFreeList != null ? localFreeList.size() : 0; |
| 1433 | + STATE.set(this, localSize); |
| 1434 | + // Check if all segments have already been returned. |
| 1435 | + int totalFreeSegments = localSize + externalFreeList.size(); |
| 1436 | + if (totalFreeSegments == segments) { |
| 1437 | + if (STATE.compareAndSet(this, localSize, DEALLOCATED)) { |
| 1438 | + deallocate(); |
| 1439 | + } |
| 1440 | + } |
1353 | 1441 | } |
1354 | 1442 | } |
1355 | 1443 |
|
|