From 7c718646cbb3c55e346dca9304d0cbb23edf3d35 Mon Sep 17 00:00:00 2001 From: regnat Date: Fri, 5 Feb 2021 13:35:59 +0100 Subject: [PATCH] Add some stats for the evaluation caching --- src/libexpr/eval.cc | 30 ++++++++++++++++++++++++++++++ src/libexpr/eval.hh | 6 ++++++ 2 files changed, 36 insertions(+) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 77301b75d..5acbf50d7 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -1163,6 +1163,28 @@ std::pair ValueCache::getValue(EvalState & return { cacheResult, ValueCache(resultingCursor) }; } +void EvalState::updateCacheStats(ValueCache::CacheResult cacheResult) +{ + switch (cacheResult.returnCode) { + case ValueCache::CacheHit: + nrCacheHits++; + break; + case ValueCache::CacheMiss: + nrCacheMisses++; + break; + case ValueCache::UnCacheable: + nrUncacheable++; + break; + case ValueCache::NoCacheKey: + nrUncached++; + break; + case ValueCache::Forward: + nrCacheHits++; + break; + }; +} + + bool EvalState::getAttrField(Value & attrs, const std::vector & selector, const Pos & pos, Value & dest) { Pos * pos2 = 0; @@ -1171,6 +1193,7 @@ bool EvalState::getAttrField(Value & attrs, const std::vector & selector if (attrs.type() == nAttrs) { auto eval_cache = attrs.attrs->eval_cache; auto [ cacheResult, resultingCursor ] = eval_cache.getValue(*this, selector, dest); + updateCacheStats(cacheResult); switch (cacheResult.returnCode) { case ValueCache::CacheHit: if (cacheResult.lastQueriedSymbolIfMissing) @@ -2063,6 +2086,13 @@ void EvalState::printStats() topObj.attr("nrLookups", nrLookups); topObj.attr("nrPrimOpCalls", nrPrimOpCalls); topObj.attr("nrFunctionCalls", nrFunctionCalls); + { + auto cache = topObj.object("evalCache"); + cache.attr("nrCacheMisses", nrCacheMisses); + cache.attr("nrCacheHits", nrCacheHits); + cache.attr("nrUncached", nrUncached); + cache.attr("nrUncacheable", nrUncacheable); + } #if HAVE_BOEHMGC { auto gc = topObj.object("gc"); diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 28c153a2d..36a3b5b74 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -314,6 +314,8 @@ public: void realiseContext(const PathSet & context); + void updateCacheStats(ValueCache::CacheResult); + private: unsigned long nrEnvs = 0; @@ -327,6 +329,10 @@ private: unsigned long nrListConcats = 0; unsigned long nrPrimOpCalls = 0; unsigned long nrFunctionCalls = 0; + unsigned long nrCacheHits = 0; + unsigned long nrCacheMisses = 0; + unsigned long nrUncacheable = 0; + unsigned long nrUncached = 0; bool countCalls;