2020
2121#include " cachelib/allocator/memory/MemoryAllocatorStats.h"
2222#include " cachelib/allocator/memory/Slab.h"
23+ #include " cachelib/cachebench/cache/StatsBase.h"
2324#include " cachelib/common/PercentileStats.h"
2425
2526DECLARE_bool (report_api_latency);
@@ -51,7 +52,8 @@ struct BackgroundPromotionStats {
5152 uint64_t nTraversals{0 };
5253};
5354
54- struct Stats {
55+ class Stats : public StatsBase {
56+ public:
5557 BackgroundEvictionStats backgndEvicStats;
5658 BackgroundPromotionStats backgndPromoStats;
5759
@@ -169,7 +171,10 @@ struct Stats {
169171
170172 // Aggregate throughput stats from another instance. DOES NOT HANDLE
171173 // LATENCY STATS!
172- Stats& operator +=(const Stats& other) {
174+ Stats& operator +=(const StatsBase& otherBase) override {
175+ aggregated_ = true ;
176+ auto & other = otherBase.as <Stats>();
177+
173178 backgndEvicStats.nEvictedItems += other.backgndEvicStats .nEvictedItems ;
174179 backgndEvicStats.nTraversals += other.backgndEvicStats .nTraversals ;
175180 backgndEvicStats.nClasses += other.backgndEvicStats .nClasses ;
@@ -241,7 +246,21 @@ struct Stats {
241246 return *this ;
242247 }
243248
244- void render (std::ostream& out, bool isAggregate = false ) const {
249+ std::string progress (const StatsBase& prevStatsBase) const override {
250+ const auto & prevStats = prevStatsBase.as <Stats>();
251+ auto hitRates = getHitRatios (prevStats);
252+ return folly::sformat (
253+ " {} items in cache. {} items in nvm cache. {} items evicted from nvm "
254+ " cache. Hit Ratio {:6.2f}% (RAM {:6.2f}%, NVM {:6.2f}%)." ,
255+ numItems,
256+ numNvmItems,
257+ numNvmEvictions,
258+ hitRates[" overall" ],
259+ hitRates[" ram" ],
260+ hitRates[" nvm" ]);
261+ }
262+
263+ void render (std::ostream& out) const override {
245264 auto totalMisses = getTotalMisses ();
246265 const double overallHitRatio = invertPctFn (totalMisses, numCacheGets);
247266 out << folly::sformat (" Items in RAM : {:,}" , numItems) << std::endl;
@@ -265,7 +284,7 @@ struct Stats {
265284 }
266285 };
267286
268- if (!isAggregate ) {
287+ if (!aggregated_ ) {
269288 for (auto pid = 0U ; pid < poolUsageFraction.size (); pid++) {
270289 out << folly::sformat (" Fraction of pool {:,} used : {:.2f}" , pid,
271290 poolUsageFraction[pid])
@@ -334,7 +353,7 @@ struct Stats {
334353 out << folly::sformat (" Hit Ratio : {:6.2f}%" , overallHitRatio)
335354 << std::endl;
336355
337- if (FLAGS_report_api_latency && !isAggregate ) {
356+ if (FLAGS_report_api_latency && !aggregated_ ) {
338357 auto printLatencies =
339358 [&out](folly::StringPiece cat,
340359 const util::PercentileStats::Estimates& latency) {
@@ -362,7 +381,7 @@ struct Stats {
362381 if (!backgroundEvictionClasses.empty () &&
363382 backgndEvicStats.nEvictedItems > 0 ) {
364383 out << " == Class Background Eviction Counters Map ==" << std::endl;
365- if (!isAggregate ) {
384+ if (!aggregated_ ) {
366385 foreachAC (backgroundEvictionClasses,
367386 [&](auto pid, auto cid, auto evicted) {
368387 out << folly::sformat (" pid{:2} cid{:4} evicted: {:4}" , pid,
@@ -382,7 +401,7 @@ struct Stats {
382401 if (!backgroundPromotionClasses.empty () &&
383402 backgndPromoStats.nPromotedItems > 0 ) {
384403 out << " == Class Background Promotion Counters Map ==" << std::endl;
385- if (!isAggregate ) {
404+ if (!aggregated_ ) {
386405 foreachAC (backgroundPromotionClasses,
387406 [&](auto pid, auto cid, auto promoted) {
388407 out << folly::sformat (" pid{:2} cid{:4} promoted: {:4}" , pid,
@@ -413,7 +432,7 @@ struct Stats {
413432 " {:,}\n " ,
414433 numNvmRejectsByExpiry, numNvmRejectsByClean);
415434
416- if (!isAggregate ) {
435+ if (!aggregated_ ) {
417436 folly::StringPiece readCat = " NVM Read Latency" ;
418437 folly::StringPiece writeCat = " NVM Write Latency" ;
419438 auto fmtLatency = [&](folly::StringPiece cat, folly::StringPiece pct,
@@ -575,51 +594,55 @@ struct Stats {
575594 return numNvmGets > 0 ? numNvmGetMiss : numCacheGetMiss;
576595 }
577596
578- std::tuple<double , double , double > getHitRatios (
579- const Stats& prevStats) const {
580- double overallHitRatio = 0.0 ;
581- double ramHitRatio = 0.0 ;
582- double nvmHitRatio = 0.0 ;
597+ std::map<std::string, double > getHitRatios (
598+ const StatsBase& prevStatsBase) const override {
599+ auto & prevStats = prevStatsBase.as <Stats>();
600+ std::map<std::string, double > rates;
601+ rates[" overall" ] = 0.0 ;
602+ rates[" ram" ] = 0.0 ;
603+ rates[" nvm" ] = 0.0 ;
583604
584605 if (numCacheGets > prevStats.numCacheGets ) {
585606 auto totalMisses = getTotalMisses ();
586607 auto prevTotalMisses = prevStats.getTotalMisses ();
587608
588- overallHitRatio = invertPctFn (totalMisses - prevTotalMisses,
589- numCacheGets - prevStats.numCacheGets );
609+ rates[ " overall " ] = invertPctFn (totalMisses - prevTotalMisses,
610+ numCacheGets - prevStats.numCacheGets );
590611
591- ramHitRatio = invertPctFn (numCacheGetMiss - prevStats.numCacheGetMiss ,
592- numCacheGets - prevStats.numCacheGets );
612+ rates[ " ram " ] = invertPctFn (numCacheGetMiss - prevStats.numCacheGetMiss ,
613+ numCacheGets - prevStats.numCacheGets );
593614 }
594615
595616 if (numNvmGets > prevStats.numNvmGets ) {
596- nvmHitRatio = invertPctFn (numNvmGetMiss - prevStats.numNvmGetMiss ,
597- numNvmGets - prevStats.numNvmGets );
617+ rates[ " nvm " ] = invertPctFn (numNvmGetMiss - prevStats.numNvmGetMiss ,
618+ numNvmGets - prevStats.numNvmGets );
598619 }
599620
600- return std::make_tuple (overallHitRatio, ramHitRatio, nvmHitRatio) ;
621+ return rates ;
601622 }
602623
603624 // Render the stats based on the delta between overall stats and previous
604625 // stats. It can be used to render the stats in the last time period.
605- void render (const Stats& prevStats, std::ostream& out) const {
626+ void render (const StatsBase& prevStatsBase,
627+ std::ostream& out) const override {
628+ auto & prevStats = prevStatsBase.as <Stats>();
629+
606630 if (numCacheGets > prevStats.numCacheGets ) {
607- auto [overallHitRatio, ramHitRatio, nvmHitRatio] =
608- getHitRatios (prevStats);
631+ auto rates = getHitRatios (prevStatsBase);
609632 out << folly::sformat (" Cache Gets : {:,}" ,
610633 numCacheGets - prevStats.numCacheGets )
611634 << std::endl;
612- out << folly::sformat (" Hit Ratio : {:6.2f}%" , overallHitRatio )
635+ out << folly::sformat (" Hit Ratio : {:6.2f}%" , rates[ " overall " ] )
613636 << std::endl;
614637
615638 out << folly::sformat (
616639 " RAM Hit Ratio : {:6.2f}%\n "
617640 " NVM Hit Ratio : {:6.2f}%\n " ,
618- ramHitRatio, nvmHitRatio );
641+ rates[ " ram " ], rates[ " nvm " ] );
619642 }
620643 }
621644
622- void render (folly::UserCounters& counters) {
645+ void render (folly::UserCounters& counters) const override {
623646 auto calcInvertPctFn = [](uint64_t ops, uint64_t total) {
624647 return static_cast <int64_t >(invertPctFn (ops, total) * 100 );
625648 };
@@ -657,7 +680,7 @@ struct Stats {
657680 counters[" nvm_dev_write_amp" ] = static_cast <int64_t >(devWriteAmp);
658681 }
659682
660- bool renderIsTestPassed (std::ostream& out) {
683+ bool renderIsTestPassed (std::ostream& out) const override {
661684 bool pass = true ;
662685
663686 if (isNvmCacheDisabled) {
@@ -697,6 +720,8 @@ struct Stats {
697720 }
698721
699722 private:
723+ bool aggregated_{false };
724+
700725 static double pctFn (uint64_t ops, uint64_t total) {
701726 return total == 0
702727 ? 0
@@ -706,8 +731,7 @@ struct Stats {
706731 static double invertPctFn (uint64_t ops, uint64_t total) {
707732 return 100 - pctFn (ops, total);
708733 }
709-
710- }; // namespace cachebench
734+ };
711735
712736} // namespace cachebench
713737} // namespace cachelib
0 commit comments