20
20
#include < string>
21
21
22
22
#include " cachelib/allocator/KAllocation.h"
23
+ #include " cachelib/allocator/nvmcache/NvmItem.h"
23
24
#include " cachelib/common/EventInterface.h"
24
25
#include " cachelib/common/Throttler.h"
25
26
@@ -36,6 +37,9 @@ struct ObjectCacheConfig {
36
37
using SerializeCb = typename ObjectCache::SerializeCb;
37
38
using DeserializeCb = typename ObjectCache::DeserializeCb;
38
39
using EvictionPolicyConfig = typename ObjectCache::EvictionPolicyConfig;
40
+ using NvmCacheConfig = typename ObjectCache::NvmCacheConfig;
41
+ using ToBlobCb = std::function<std::unique_ptr<folly::IOBuf>(uintptr_t )>;
42
+ using ToPtrCb = std::function<uintptr_t (folly::StringPiece)>;
39
43
40
44
// Set cache name as a string
41
45
ObjectCacheConfig& setCacheName (const std::string& _cacheName);
@@ -147,6 +151,12 @@ struct ObjectCacheConfig {
147
151
// ObjectCache::startCacheWorkers()
148
152
ObjectCacheConfig& setDelayCacheWorkersStart ();
149
153
154
+ /* *
155
+ * Enable NVM cache.
156
+ */
157
+ ObjectCacheConfig& enableNvm (NvmCacheConfig config);
158
+ ObjectCacheConfig& overrideNvmCbs (ToBlobCb blobCb, ToPtrCb ptrCb);
159
+
150
160
// With size controller disabled, above this many entries, L1 will start
151
161
// evicting.
152
162
// With size controller enabled, this is only a hint used for initialization.
@@ -237,6 +247,8 @@ struct ObjectCacheConfig {
237
247
// ObjectCache::startCacheWorkers()
238
248
bool delayCacheWorkersStart{false };
239
249
250
+ std::optional<typename ObjectCache::NvmCacheConfig> nvmConfig{};
251
+
240
252
const ObjectCacheConfig& validate () const ;
241
253
};
242
254
@@ -353,6 +365,7 @@ ObjectCacheConfig<T>& ObjectCacheConfig<T>::enablePersistence(
353
365
" Serialize and deserialize callback must be set to enable cache "
354
366
" persistence" );
355
367
}
368
+ persistenceEnabled = true ;
356
369
persistThreadCount = threadCount;
357
370
persistBaseFilePath = basefilePath;
358
371
serializeCb = std::move (serializeCallback);
@@ -379,6 +392,7 @@ ObjectCacheConfig<T>& ObjectCacheConfig<T>::enablePersistenceWithEvictionOrder(
379
392
" Serialize and deserialize callback must be set to enable cache "
380
393
" persistence" );
381
394
}
395
+ persistenceEnabled = true ;
382
396
persistThreadCount = 1 ;
383
397
persistBaseFilePath = basefilePath;
384
398
serializeCb = std::move (serializeCallback);
@@ -413,6 +427,56 @@ ObjectCacheConfig<T>& ObjectCacheConfig<T>::setDelayCacheWorkersStart() {
413
427
return *this ;
414
428
}
415
429
430
+ template <typename T>
431
+ ObjectCacheConfig<T>& ObjectCacheConfig<T>::enableNvm(NvmCacheConfig config) {
432
+ nvmConfig = std::move (config);
433
+ return *this ;
434
+ }
435
+
436
+ template <typename T>
437
+ ObjectCacheConfig<T>& ObjectCacheConfig<T>::overrideNvmCbs(ToBlobCb blobCb,
438
+ ToPtrCb ptrCb) {
439
+ if (!nvmConfig || nvmConfig->makeBlobCb || nvmConfig->makeObjCb ) {
440
+ throw std::invalid_argument (
441
+ " Do not set makeBlobCb or makeObjCb in nvmConfig before calling "
442
+ " overriceNvmCbs." );
443
+ }
444
+
445
+ nvmConfig->makeBlobCb =
446
+ [blobCb = std::move (blobCb)](
447
+ const typename T::CacheItem& item,
448
+ folly::Range<typename T::NvmCache::ChainedItemIter>) {
449
+ uintptr_t ptr =
450
+ item.template getMemoryAs <typename T::Item>()->objectPtr ;
451
+ auto blob = blobCb (ptr);
452
+ std::vector<BufferedBlob> blobs;
453
+ if (blob == nullptr ) {
454
+ return blobs;
455
+ }
456
+ blobs.emplace_back (BufferedBlob{static_cast <uint32_t >(item.getSize ()),
457
+ std::move (blob)});
458
+ return blobs;
459
+ };
460
+
461
+ nvmConfig->makeObjCb =
462
+ [ptrCb = std::move (ptrCb)](
463
+ const NvmItem& nvmItem, typename T::CacheItem& it,
464
+ folly::Range<typename T::NvmCache::WritableChainedItemIter>) {
465
+ // Create ptr. Do not consider chained item.
466
+ auto pBlob = nvmItem.getBlob (0 );
467
+ uintptr_t ptr = ptrCb (pBlob.data );
468
+ // TODO: Is there a better way to do this?
469
+ if (ptr == reinterpret_cast <uintptr_t >(nullptr )) {
470
+ return false ;
471
+ }
472
+ *it.template getMemoryAs <typename T::Item>() =
473
+ typename T::Item{ptr, pBlob.data .size ()};
474
+
475
+ return true ;
476
+ };
477
+ return *this ;
478
+ }
479
+
416
480
template <typename T>
417
481
const ObjectCacheConfig<T>& ObjectCacheConfig<T>::validate() const {
418
482
// checking missing params
@@ -450,6 +514,7 @@ const ObjectCacheConfig<T>& ObjectCacheConfig<T>::validate() const {
450
514
" Object size tracking has to be enabled to track object size "
451
515
" distribution" );
452
516
}
517
+
453
518
return *this ;
454
519
}
455
520
0 commit comments