@@ -83,8 +83,8 @@ namespace cachelib {
83
83
template <typename AllocatorT>
84
84
class FbInternalRuntimeUpdateWrapper ;
85
85
86
- template <typename AllocatorT >
87
- class CacheAllocatorFindApiWrapper ;
86
+ template <typename K, typename V, typename C >
87
+ class ReadOnlyMap ;
88
88
89
89
namespace objcache2 {
90
90
template <typename AllocatorT>
@@ -186,10 +186,11 @@ class CacheAllocator : public CacheBase {
186
186
187
187
// the holder for the item when we hand it to the caller. This ensures
188
188
// that the reference count is maintained when the caller is done with the
189
- // item. The ItemHandle provides a getMemory() and getKey() interface. The
190
- // caller is free to use the result of these two as long as the handle is
191
- // active/alive. Using the result of the above interfaces after destroying
192
- // the ItemHandle is UB. The ItemHandle safely wraps a pointer to the Item.
189
+ // item. The ReadHandle/WriteHandle provides a getMemory() and getKey()
190
+ // interface. The caller is free to use the result of these two as long as the
191
+ // handle is active/alive. Using the result of the above interfaces after
192
+ // destroying the ReadHandle/WriteHandle is UB. The ReadHandle/WriteHandle
193
+ // safely wraps a pointer to the "const Item"/"Item".
193
194
using ReadHandle = typename Item::ReadHandle;
194
195
using WriteHandle = typename Item::WriteHandle;
195
196
using ItemHandle = WriteHandle;
@@ -401,7 +402,7 @@ class CacheAllocator : public CacheBase {
401
402
//
402
403
// @return ChainedItem head if there exists one
403
404
// nullptr otherwise
404
- ItemHandle popChainedItem (ItemHandle & parent);
405
+ WriteHandle popChainedItem (WriteHandle & parent);
405
406
406
407
// Return the key to the parent item.
407
408
//
@@ -427,9 +428,9 @@ class CacheAllocator : public CacheBase {
427
428
// @return handle to the oldItem on return.
428
429
//
429
430
// @throw std::invalid_argument if any of the pre-conditions fails
430
- ItemHandle replaceChainedItem (Item& oldItem,
431
- ItemHandle newItem,
432
- Item& parent);
431
+ WriteHandle replaceChainedItem (Item& oldItem,
432
+ WriteHandle newItem,
433
+ Item& parent);
433
434
434
435
// Transfers the ownership of the chain from the current parent to the new
435
436
// parent and inserts the new parent into the cache. Parent will be unmarked
@@ -450,7 +451,7 @@ class CacheAllocator : public CacheBase {
450
451
// @throw std::invalid_argument if the parent does not have chained item or
451
452
// incorrect state of chained item or if any of the pre-conditions
452
453
// are not met
453
- void transferChainAndReplace (ItemHandle & parent, ItemHandle & newParent);
454
+ void transferChainAndReplace (WriteHandle & parent, WriteHandle & newParent);
454
455
455
456
// Inserts the allocated handle into the AccessContainer, making it
456
457
// accessible for everyone. This needs to be the handle that the caller
@@ -564,7 +565,7 @@ class CacheAllocator : public CacheBase {
564
565
// removes the allocation corresponding to the key, if present in the hash
565
566
// table. The key will not be accessible through find() after this returns
566
567
// success. The allocation for the key will be recycled once all active
567
- // ItemHandles are released.
568
+ // Item handles are released.
568
569
//
569
570
// @param key the key for the allocation.
570
571
// @return kSuccess if the key exists and was successfully removed.
@@ -639,11 +640,11 @@ class CacheAllocator : public CacheBase {
639
640
// Get a random item from memory
640
641
// This is useful for profiling and sampling cachelib managed memory
641
642
//
642
- // @return ItemHandle if an valid item is found
643
+ // @return ReadHandle if an valid item is found
643
644
//
644
645
// nullptr if the randomly chosen memory does not belong
645
646
// to an valid item
646
- ItemHandle getSampleItem ();
647
+ ReadHandle getSampleItem ();
647
648
648
649
// Convert a Read Handle to an IOBuf. The returned IOBuf gives a
649
650
// read-only view to the user. The item's ownership is retained by
@@ -1061,6 +1062,11 @@ class CacheAllocator : public CacheBase {
1061
1062
return accessContainer_->getStats ();
1062
1063
}
1063
1064
1065
+ // Get the total number of keys inserted into the access container
1066
+ uint64_t getAccessContainerNumKeys () const {
1067
+ return accessContainer_->getNumKeys ();
1068
+ }
1069
+
1064
1070
// returns the reaper stats
1065
1071
ReaperStats getReaperStats () const {
1066
1072
auto stats = reaper_ ? reaper_->getStats () : ReaperStats{};
@@ -1272,18 +1278,18 @@ class CacheAllocator : public CacheBase {
1272
1278
1273
1279
// acquires an handle on the item. returns an empty handle if it is null.
1274
1280
// @param it pointer to an item
1275
- // @return ItemHandle return a handle to this item
1281
+ // @return WriteHandle return a handle to this item
1276
1282
// @throw std::overflow_error is the maximum item refcount is execeeded by
1277
1283
// creating this item handle.
1278
- ItemHandle acquire (Item* it);
1284
+ WriteHandle acquire (Item* it);
1279
1285
1280
1286
// creates an item handle with wait context.
1281
- ItemHandle createNvmCacheFillHandle () { return ItemHandle {*this }; }
1287
+ WriteHandle createNvmCacheFillHandle () { return WriteHandle {*this }; }
1282
1288
1283
1289
// acquires the wait context for the handle. This is used by NvmCache to
1284
1290
// maintain a list of waiters
1285
1291
std::shared_ptr<WaitContext<ReadHandle>> getWaitContext (
1286
- ItemHandle & hdl) const {
1292
+ ReadHandle & hdl) const {
1287
1293
return hdl.getItemWaitContext ();
1288
1294
}
1289
1295
@@ -1363,9 +1369,9 @@ class CacheAllocator : public CacheBase {
1363
1369
// @param parentKey key of the item's parent
1364
1370
//
1365
1371
// @return handle to the parent item if the validations pass
1366
- // otherwise, an empty ItemHandle is returned.
1372
+ // otherwise, an empty Handle is returned.
1367
1373
//
1368
- ItemHandle validateAndGetParentHandleForChainedMoveLocked (
1374
+ ReadHandle validateAndGetParentHandleForChainedMoveLocked (
1369
1375
const ChainedItem& item, const Key& parentKey);
1370
1376
1371
1377
// Given an existing item, allocate a new one for the
@@ -1386,7 +1392,7 @@ class CacheAllocator : public CacheBase {
1386
1392
//
1387
1393
// @throw std::overflow_error is the maximum item refcount is execeeded by
1388
1394
// creating this item handle.
1389
- ItemHandle findInternal (Key key) {
1395
+ WriteHandle findInternal (Key key) {
1390
1396
// Note: this method can not be const because we need a non-const
1391
1397
// reference to create the ItemReleaser.
1392
1398
return accessContainer_->find (key);
@@ -1401,7 +1407,7 @@ class CacheAllocator : public CacheBase {
1401
1407
//
1402
1408
// @return the handle for the item or a handle to nullptr if the key does
1403
1409
// not exist.
1404
- FOLLY_ALWAYS_INLINE ItemHandle findFastInternal (Key key, AccessMode mode);
1410
+ FOLLY_ALWAYS_INLINE WriteHandle findFastInternal (Key key, AccessMode mode);
1405
1411
1406
1412
// look up an item by its key across the nvm cache as well if enabled.
1407
1413
//
@@ -1411,7 +1417,7 @@ class CacheAllocator : public CacheBase {
1411
1417
//
1412
1418
// @return the handle for the item or a handle to nullptr if the key does
1413
1419
// not exist.
1414
- FOLLY_ALWAYS_INLINE ItemHandle findImpl (Key key, AccessMode mode);
1420
+ FOLLY_ALWAYS_INLINE WriteHandle findImpl (Key key, AccessMode mode);
1415
1421
1416
1422
// look up an item by its key. This ignores the nvm cache and only does RAM
1417
1423
// lookup.
@@ -1422,7 +1428,7 @@ class CacheAllocator : public CacheBase {
1422
1428
//
1423
1429
// @return the handle for the item or a handle to nullptr if the key does
1424
1430
// not exist.
1425
- FOLLY_ALWAYS_INLINE ItemHandle findFastImpl (Key key, AccessMode mode);
1431
+ FOLLY_ALWAYS_INLINE WriteHandle findFastImpl (Key key, AccessMode mode);
1426
1432
1427
1433
// Moves a regular item to a different slab. This should only be used during
1428
1434
// slab release after the item's moving bit has been set. The user supplied
@@ -1434,7 +1440,7 @@ class CacheAllocator : public CacheBase {
1434
1440
//
1435
1441
// @return true If the move was completed, and the containers were updated
1436
1442
// successfully.
1437
- bool moveRegularItem (Item& oldItem, ItemHandle & newItemHdl);
1443
+ bool moveRegularItem (Item& oldItem, WriteHandle & newItemHdl);
1438
1444
1439
1445
// template class for viewAsChainedAllocs that takes either ReadHandle or
1440
1446
// WriteHandle
@@ -1461,7 +1467,7 @@ class CacheAllocator : public CacheBase {
1461
1467
//
1462
1468
// @return true If the move was completed, and the containers were updated
1463
1469
// successfully.
1464
- bool moveChainedItem (ChainedItem& oldItem, ItemHandle & newItemHdl);
1470
+ bool moveChainedItem (ChainedItem& oldItem, WriteHandle & newItemHdl);
1465
1471
1466
1472
// Transfers the chain ownership from parent to newParent. Parent
1467
1473
// will be unmarked as having chained allocations. Parent will not be null
@@ -1478,7 +1484,7 @@ class CacheAllocator : public CacheBase {
1478
1484
// @param newParent the new parent for the chain
1479
1485
//
1480
1486
// @throw if any of the conditions for parent or newParent are not met.
1481
- void transferChainLocked (ItemHandle & parent, ItemHandle & newParent);
1487
+ void transferChainLocked (WriteHandle & parent, WriteHandle & newParent);
1482
1488
1483
1489
// replace a chained item in the existing chain. This needs to be called
1484
1490
// with the chained item lock held exclusive
@@ -1488,9 +1494,9 @@ class CacheAllocator : public CacheBase {
1488
1494
// @param parent the parent for the chain
1489
1495
//
1490
1496
// @return handle to the oldItem
1491
- ItemHandle replaceChainedItemLocked (Item& oldItem,
1492
- ItemHandle newItemHdl,
1493
- const Item& parent);
1497
+ WriteHandle replaceChainedItemLocked (Item& oldItem,
1498
+ WriteHandle newItemHdl,
1499
+ const Item& parent);
1494
1500
1495
1501
// Insert an item into MM container. The caller must hold a valid handle for
1496
1502
// the item.
@@ -1586,8 +1592,8 @@ class CacheAllocator : public CacheBase {
1586
1592
//
1587
1593
// @return valid handle to regular item on success. This will be the last
1588
1594
// handle to the item. On failure an empty handle.
1589
- ItemHandle advanceIteratorAndTryEvictRegularItem (MMContainer& mmContainer,
1590
- EvictionIterator& itr);
1595
+ WriteHandle advanceIteratorAndTryEvictRegularItem (MMContainer& mmContainer,
1596
+ EvictionIterator& itr);
1591
1597
1592
1598
// Advance the current iterator and try to evict a chained item
1593
1599
// Iterator may also be reset during the course of this function
@@ -1596,7 +1602,7 @@ class CacheAllocator : public CacheBase {
1596
1602
//
1597
1603
// @return valid handle to the parent item on success. This will be the last
1598
1604
// handle to the item
1599
- ItemHandle advanceIteratorAndTryEvictChainedItem (EvictionIterator& itr);
1605
+ WriteHandle advanceIteratorAndTryEvictChainedItem (EvictionIterator& itr);
1600
1606
1601
1607
// Deserializer CacheAllocatorMetadata and verify the version
1602
1608
//
@@ -1707,7 +1713,7 @@ class CacheAllocator : public CacheBase {
1707
1713
//
1708
1714
// @return true if the item has been moved
1709
1715
// false if we have exhausted moving attempts
1710
- bool tryMovingForSlabRelease (Item& item, ItemHandle & newItemHdl);
1716
+ bool tryMovingForSlabRelease (Item& item, WriteHandle & newItemHdl);
1711
1717
1712
1718
// Evict an item from access and mm containers and
1713
1719
// ensure it is safe for freeing.
@@ -1723,14 +1729,14 @@ class CacheAllocator : public CacheBase {
1723
1729
//
1724
1730
// @return last handle for corresponding to item on success. empty handle on
1725
1731
// failure. caller can retry if needed.
1726
- ItemHandle evictNormalItemForSlabRelease (Item& item);
1732
+ WriteHandle evictNormalItemForSlabRelease (Item& item);
1727
1733
1728
1734
// Helper function to evict a child item for slab release
1729
1735
// As a side effect, the parent item is also evicted
1730
1736
//
1731
1737
// @return last handle to the parent item of the child on success. empty
1732
1738
// handle on failure. caller can retry.
1733
- ItemHandle evictChainedItemForSlabRelease (ChainedItem& item);
1739
+ WriteHandle evictChainedItemForSlabRelease (ChainedItem& item);
1734
1740
1735
1741
// Helper function to remove a item if expired.
1736
1742
//
@@ -1749,7 +1755,8 @@ class CacheAllocator : public CacheBase {
1749
1755
// primitives. So we consciously exempt ourselves here from TSAN data race
1750
1756
// detection.
1751
1757
folly::annotate_ignore_thread_sanitizer_guard g (__FILE__, __LINE__);
1752
- allocator_->forEachAllocation (std::forward<Fn>(f));
1758
+ auto slabsSkipped = allocator_->forEachAllocation (std::forward<Fn>(f));
1759
+ stats ().numSkippedSlabReleases .add (slabsSkipped);
1753
1760
}
1754
1761
1755
1762
// returns true if nvmcache is enabled and we should write this item to
@@ -1870,7 +1877,7 @@ class CacheAllocator : public CacheBase {
1870
1877
// @return true if successfully recorded in MMContainer
1871
1878
bool recordAccessInMMContainer (Item& item, AccessMode mode);
1872
1879
1873
- ItemHandle findChainedItem (const Item& parent) const ;
1880
+ WriteHandle findChainedItem (const Item& parent) const ;
1874
1881
1875
1882
// Get the thread local version of the Stats
1876
1883
detail::Stats& stats () const noexcept { return stats_; }
@@ -1982,7 +1989,13 @@ class CacheAllocator : public CacheBase {
1982
1989
mutable std::mutex workersMutex_;
1983
1990
1984
1991
// time when the ram cache was first created
1985
- const time_t cacheCreationTime_{0 };
1992
+ const uint32_t cacheCreationTime_{0 };
1993
+
1994
+ // time when CacheAllocator structure is created. Whenever a process restarts
1995
+ // and even if cache content is persisted, this will be reset. It's similar
1996
+ // to process uptime. (But alternatively if user explicitly shuts down and
1997
+ // re-attach cache, this will be reset as well)
1998
+ const uint32_t cacheInstanceCreationTime_{0 };
1986
1999
1987
2000
// thread local accumulation of handle counts
1988
2001
mutable util::FastStats<int64_t > handleCount_{};
@@ -2010,9 +2023,10 @@ class CacheAllocator : public CacheBase {
2010
2023
friend ReaperAPIWrapper<CacheT>;
2011
2024
friend class CacheAPIWrapperForNvm <CacheT>;
2012
2025
friend class FbInternalRuntimeUpdateWrapper <CacheT>;
2013
- friend class CacheAllocatorFindApiWrapper <CacheT>;
2014
2026
friend class objcache2 ::ObjectCache<CacheT>;
2015
2027
friend class objcache2 ::ObjectCacheBase<CacheT>;
2028
+ template <typename K, typename V, typename C>
2029
+ friend class ReadOnlyMap ;
2016
2030
2017
2031
// tests
2018
2032
friend class facebook ::cachelib::tests::NvmCacheTest;
0 commit comments