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
9// To enable stats tracking, compile in RelWithAssert mode.
10// cmake --preset $PRESET -DCMAKE_BUILD_TYPE=RelWithAssert
11// TODO(fcarreiro): This should be possible to disable.
12#define AVM_TRACK_STATS
13
14#ifdef AVM_TRACK_STATS
15// For tracking time spent in a block of code.
16#define AVM_TRACK_TIME(key, body) ::bb::avm2::Stats::get().time(key, [&]() { body; });
17// For tracking time spent in a block of code and returning a value.
18#define AVM_TRACK_TIME_V(key, body) ::bb::avm2::Stats::get().time_r(key, [&]() { return body; });
19#else
20#define AVM_TRACK_TIME(key, body) body
21#define AVM_TRACK_TIME_V(key, body) body
22#endif
23
24namespace bb::avm2 {
25
26class Stats {
27 public:
28 static Stats& get();
29 void reset();
30 void increment(const std::string& key, uint64_t value);
31 void time(const std::string& key, const std::function<void()>& f);
32
33 template <typename F> auto time_r(const std::string& key, F&& f)
34 {
35 auto start = std::chrono::system_clock::now();
36 auto result = f();
37 auto elapsed = std::chrono::system_clock::now() - start;
38 increment(key + "_ms",
39 static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()));
40 return result;
41 }
42
43 // Returns a string representation of the stats.
44 // E.g., if depth = 2, it will show the top 2 levels of the stats.
45 // That is, prove/logderiv_ms will be shown but
46 // prove/logderiv/relation_ms will not be shown.
47 std::string to_string(int depth = 2) const;
48
49 private:
50 Stats() = default;
51
52 std::unordered_map<std::string, uint64_t> stats;
53 mutable std::mutex stats_mutex;
54};
55
56} // namespace bb::avm2
void increment(const std::string &key, uint64_t value)
Definition stats.cpp:21
std::string to_string(int depth=2) const
Definition stats.cpp:36
auto time_r(const std::string &key, F &&f)
Definition stats.hpp:33
static Stats & get()
Definition stats.cpp:10
std::unordered_map< std::string, uint64_t > stats
Definition stats.hpp:52
Stats()=default
void time(const std::string &key, const std::function< void()> &f)
Definition stats.cpp:27
void reset()
Definition stats.cpp:16
std::mutex stats_mutex
Definition stats.hpp:53