| 720 | |
| 721 | template <typename AllocatorT> |
| 722 | void ObjectCache<AllocatorT>::init() { |
| 723 | // Compute variables required to size the cache and placeholders |
| 724 | DCHECK_GE(config_.l1EntriesLimit, config_.l1NumShards); |
| 725 | auto l1AllocSize = getL1AllocSize(config_.maxKeySizeBytes); |
| 726 | const size_t allocsPerSlab = Slab::kSize / l1AllocSize; |
| 727 | const size_t allocsPerShard = |
| 728 | util::getDivCeiling(config_.l1EntriesLimit, config_.l1NumShards); |
| 729 | const size_t slabsPerShard = |
| 730 | util::getDivCeiling(allocsPerShard, allocsPerSlab); |
| 731 | const size_t perPoolSize = slabsPerShard * Slab::kSize; |
| 732 | const size_t l1SizeRequired = perPoolSize * config_.l1NumShards; |
| 733 | auto cacheSize = l1SizeRequired + Slab::kSize; |
| 734 | |
| 735 | typename AllocatorT::Config l1Config; |
| 736 | l1Config.setCacheName(config_.cacheName) |
| 737 | .setCacheSize(cacheSize) |
| 738 | .setAccessConfig({config_.l1HashTablePower, config_.l1LockPower}) |
| 739 | .setDefaultAllocSizes({l1AllocSize}) |
| 740 | .enableItemReaperInBackground(config_.reaperInterval) |
| 741 | .setEventTracker(std::move(config_.legacyEventTracker)) |
| 742 | .setEvictionSearchLimit(config_.evictionSearchLimit); |
| 743 | if (config_.itemDestructor) { |
| 744 | l1Config.setItemDestructor( |
| 745 | [this](typename AllocatorT::DestructorData data) { |
| 746 | ObjectCacheDestructorContext ctx; |
| 747 | if (data.context == DestructorContext::kEvictedFromRAM) { |
| 748 | evictions_.inc(); |
| 749 | ctx = ObjectCacheDestructorContext::kEvicted; |
| 750 | } else if (data.context == DestructorContext::kRemovedFromRAM) { |
| 751 | ctx = ObjectCacheDestructorContext::kRemoved; |
| 752 | } else { // should not enter here |
| 753 | ctx = ObjectCacheDestructorContext::kUnknown; |
| 754 | } |
| 755 | |
| 756 | auto& item = data.item; |
| 757 | |
| 758 | auto itemPtr = getAlignedItemPtr(item.getMemory()); |
| 759 | |
| 760 | SCOPE_EXIT { |
| 761 | if (config_.objectSizeTrackingEnabled) { |
| 762 | // update total object size |
| 763 | totalObjectSizeBytes_.fetch_sub(itemPtr->objectSize, |
| 764 | std::memory_order_relaxed); |
| 765 | totalKeyPaddingBytes_.fetch_sub( |
| 766 | getPaddingBytes(itemPtr, item.getMemory())); |
| 767 | } |
| 768 | // execute user defined item destructor |
| 769 | config_.itemDestructor(ObjectCacheDestructorData( |
| 770 | ctx, itemPtr->objectPtr, item.getKey(), item.getExpiryTime(), |
| 771 | item.getCreationTime(), item.getLastAccessTime())); |
| 772 | }; |
| 773 | }); |
| 774 | } else { |
| 775 | l1Config.setRemoveCallback([this](typename AllocatorT::RemoveCbData data) { |
| 776 | ObjectCacheDestructorContext ctx; |
| 777 | if (data.context == RemoveContext::kEviction) { |
| 778 | evictions_.inc(); |
| 779 | ctx = ObjectCacheDestructorContext::kEvicted; |
no test coverage detected