Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
stats.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <mutex>
6#include <string>
7#include <unordered_map>
8#include <utility>
9#include <vector>
10
11// To enable stats tracking, compile in RelWithAssert mode.
12// cmake --preset $PRESET -DCMAKE_BUILD_TYPE=RelWithAssert
13// TODO(fcarreiro): This should be possible to disable.
14#define AVM_TRACK_STATS
15
16#ifdef AVM_TRACK_STATS
17// For tracking time spent in a block of code.
18#define AVM_TRACK_TIME(key, body) ::bb::avm2::Stats::get().time(key, [&]() { body; });
19// For tracking time spent in a block of code and returning a value.
20#define AVM_TRACK_TIME_V(key, body) ::bb::avm2::Stats::get().time_r(key, [&]() { return body; });
21#else
22#define AVM_TRACK_TIME(key, body) body
23#define AVM_TRACK_TIME_V(key, body) body
24#endif
25
26namespace bb::avm2 {
27
28class Stats {
29 public:
30 static Stats& get();
31 void reset();
32 void increment(const std::string& key, uint64_t value);
33 void time(const std::string& key, const std::function<void()>& f);
34
35 template <typename F> auto time_r(const std::string& key, F&& f)
36 {
37 auto start = std::chrono::system_clock::now();
38 auto result = f();
39 auto elapsed = std::chrono::system_clock::now() - start;
40 increment(key + "_ms",
41 static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()));
42 return result;
43 }
44
45 // Returns a string representation of the stats.
46 // E.g., if depth = 2, it will show the top 2 levels of the stats.
47 // That is, prove/logderiv_ms will be shown but
48 // prove/logderiv/relation_ms will not be shown.
49 std::string to_string(int depth = 2) const;
50
51 // Returns a copy of the stats as (key, value_ms) pairs. Keys retain the "_ms" suffix added by time().
53
54 private:
55 Stats() = default;
56
57 std::unordered_map<std::string, uint64_t> stats;
58 mutable std::mutex stats_mutex;
59};
60
61} // namespace bb::avm2
void increment(const std::string &key, uint64_t value)
Definition stats.cpp:21
std::vector< std::pair< std::string, uint64_t > > snapshot() const
Definition stats.cpp:55
static Stats & get()
Definition stats.cpp:10
auto time_r(const std::string &key, F &&f)
Definition stats.hpp:35
std::string to_string(int depth=2) const
Definition stats.cpp:36
std::unordered_map< std::string, uint64_t > stats
Definition stats.hpp:57
Stats()=default
void reset()
Definition stats.cpp:16
void time(const std::string &key, const std::function< void()> &f)
Definition stats.cpp:27
std::mutex stats_mutex
Definition stats.hpp:58
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13