Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
op_count.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <memory>
5#include <ostream>
6#include <tracy/Tracy.hpp>
7
8#ifdef TRACY_INSTRUMENTED
9#define PROFILE_THIS() ZoneScopedN(__func__)
10#define PROFILE_THIS_NAME(name) ZoneScopedN(name)
11#elif defined __wasm__
12#define PROFILE_THIS() (void)0
13#define PROFILE_THIS_NAME(name) (void)0
14#else
15#define PROFILE_THIS() BB_OP_COUNT_TIME_NAME(__func__)
16#define PROFILE_THIS_NAME(name) BB_OP_COUNT_TIME_NAME(name)
17#endif
18
19#ifdef __wasm__
20// require a semicolon to appease formatters
21// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
22#define BB_OP_COUNT_TIME_NAME(name) (void)0
23// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
24#define BB_OP_COUNT_TIME() (void)0
25#else
32#include <algorithm>
33#include <atomic>
34#include <cstdlib>
35#include <map>
36#include <mutex>
37#include <optional>
38#include <string>
39#include <vector>
40namespace bb::detail {
41// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
42extern bool use_op_count_time;
43
44// Compile-time string
45// See e.g. https://www.reddit.com/r/cpp_questions/comments/pumi9r/does_c20_not_support_string_literals_as_template/
46template <std::size_t N> struct OperationLabel {
47 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
48 constexpr OperationLabel(const char (&str)[N])
49 {
50 for (std::size_t i = 0; i < N; ++i) {
51 value[i] = str[i];
52 }
53 }
54
55 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
56 char value[N];
57};
58
63
64// Contains all statically known op counts
66 public:
67 struct Entry {
68 std::string key;
69 std::string thread_id;
71 };
73 std::mutex mutex;
75 void print() const;
76 // NOTE: Should be called when other threads aren't active
77 void clear();
78 void add_entry(const char* key, const std::shared_ptr<OpStats>& count);
80 void print_aggregate_counts(std::ostream&, size_t) const;
81};
82
83// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
85
86template <OperationLabel Op> struct GlobalOpCount {
87 public:
88 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
89 static thread_local std::shared_ptr<OpStats> stats;
90
92 {
93 if (BB_UNLIKELY(stats == nullptr)) {
96 }
97 return stats.get();
98 }
99 static constexpr void increment_op_count()
100 {
102 // We do nothing if the compiler tries to run this
103 return;
104 }
105 ensure_stats();
106 stats->count++;
107 }
108 static constexpr void add_clock_time(std::size_t time)
109 {
111 // We do nothing if the compiler tries to run this
112 return;
113 }
114 ensure_stats();
115 stats->time += time;
116 }
117};
118// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
119template <OperationLabel Op> thread_local std::shared_ptr<OpStats> GlobalOpCount<Op>::stats;
120
121// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
128} // namespace bb::detail
129
130// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
131#define BB_OP_COUNT_TIME_NAME(name) \
132 std::optional<bb::detail::OpCountTimeReporter> __bb_op_count_time; \
133 if (bb::detail::use_op_count_time) \
134 __bb_op_count_time.emplace(bb::detail::GlobalOpCount<name>::ensure_stats())
135// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
136#define BB_OP_COUNT_TIME() BB_OP_COUNT_TIME_NAME(__func__)
137#endif
#define BB_UNLIKELY(x)
bool use_op_count_time
Definition op_count.cpp:11
GlobalOpCountContainer GLOBAL_OP_COUNTS
Definition op_count.cpp:87
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Definition op_count.hpp:67
std::shared_ptr< OpStats > count
Definition op_count.hpp:70
std::string thread_id
Definition op_count.hpp:69
std::string key
Definition op_count.hpp:68
std::map< std::string, std::size_t > get_aggregate_counts() const
Definition op_count.cpp:44
void add_entry(const char *key, const std::shared_ptr< OpStats > &count)
Definition op_count.cpp:21
std::vector< Entry > counts
Definition op_count.hpp:74
void print_aggregate_counts(std::ostream &, size_t) const
Definition op_count.cpp:58
static constexpr void increment_op_count()
Definition op_count.hpp:99
static OpStats * ensure_stats()
Definition op_count.hpp:91
static constexpr void add_clock_time(std::size_t time)
Definition op_count.hpp:108
static thread_local std::shared_ptr< OpStats > stats
Definition op_count.hpp:89
std::size_t time
Definition op_count.hpp:61
std::size_t count
Definition op_count.hpp:60
constexpr OperationLabel(const char(&str)[N])
Definition op_count.hpp:48