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