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
31
#include "
barretenberg/common/compiler_hints.hpp
"
32
#include <algorithm>
33
#include <atomic>
34
#include <cstdlib>
35
#include <map>
36
#include <mutex>
37
#include <optional>
38
#include <string>
39
#include <vector>
40
namespace
bb::detail
{
41
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
42
extern
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/
46
template
<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
59
struct
OpStats
{
60
std::size_t
count
= 0;
61
std::size_t
time
= 0;
62
};
63
64
// Contains all statically known op counts
65
struct
GlobalOpCountContainer
{
66
public
:
67
struct
Entry
{
68
std::string
key
;
69
std::string
thread_id
;
70
std::shared_ptr<OpStats>
count
;
71
};
72
~GlobalOpCountContainer
();
73
std::mutex
mutex
;
74
std::vector<Entry>
counts
;
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);
79
std::map<std::string, std::size_t>
get_aggregate_counts
()
const
;
80
void
print_aggregate_counts
(std::ostream&,
size_t
)
const
;
81
};
82
83
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
84
extern
GlobalOpCountContainer
GLOBAL_OP_COUNTS
;
85
86
template
<OperationLabel Op>
struct
GlobalOpCount
{
87
public
:
88
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
89
static
thread_local
std::shared_ptr<OpStats>
stats
;
90
91
static
OpStats
*
ensure_stats
()
92
{
93
if
(
BB_UNLIKELY
(
stats
==
nullptr
)) {
94
stats
=
std::make_shared<OpStats>
();
95
GLOBAL_OP_COUNTS
.
add_entry
(Op.value,
stats
);
96
}
97
return
stats
.get();
98
}
99
static
constexpr
void
increment_op_count
()
100
{
101
if
(
std::is_constant_evaluated
()) {
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
{
110
if
(
std::is_constant_evaluated
()) {
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)
119
template
<OperationLabel Op>
thread_local
std::shared_ptr<OpStats>
GlobalOpCount<Op>::stats
;
120
121
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
122
struct
OpCountTimeReporter
{
123
OpStats
*
stats
;
124
std::size_t
time
;
125
OpCountTimeReporter
(
OpStats
*
stats
);
126
~OpCountTimeReporter
();
127
};
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
compiler_hints.hpp
BB_UNLIKELY
#define BB_UNLIKELY(x)
Definition
compiler_hints.hpp:25
key
FF key
Definition
indexed_memory_tree.test.cpp:18
bb::detail
Definition
constexpr_utils.hpp:8
bb::detail::use_op_count_time
bool use_op_count_time
Definition
op_count.cpp:11
bb::detail::GLOBAL_OP_COUNTS
GlobalOpCountContainer GLOBAL_OP_COUNTS
Definition
op_count.cpp:87
std::get
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition
tuple.hpp:13
bb::detail::GlobalOpCountContainer::Entry
Definition
op_count.hpp:67
bb::detail::GlobalOpCountContainer::Entry::count
std::shared_ptr< OpStats > count
Definition
op_count.hpp:70
bb::detail::GlobalOpCountContainer::Entry::thread_id
std::string thread_id
Definition
op_count.hpp:69
bb::detail::GlobalOpCountContainer::Entry::key
std::string key
Definition
op_count.hpp:68
bb::detail::GlobalOpCountContainer
Definition
op_count.hpp:65
bb::detail::GlobalOpCountContainer::get_aggregate_counts
std::map< std::string, std::size_t > get_aggregate_counts() const
Definition
op_count.cpp:44
bb::detail::GlobalOpCountContainer::add_entry
void add_entry(const char *key, const std::shared_ptr< OpStats > &count)
Definition
op_count.cpp:21
bb::detail::GlobalOpCountContainer::clear
void clear()
Definition
op_count.cpp:78
bb::detail::GlobalOpCountContainer::counts
std::vector< Entry > counts
Definition
op_count.hpp:74
bb::detail::GlobalOpCountContainer::print_aggregate_counts
void print_aggregate_counts(std::ostream &, size_t) const
Definition
op_count.cpp:58
bb::detail::GlobalOpCountContainer::print
void print() const
Definition
op_count.cpp:29
bb::detail::GlobalOpCountContainer::mutex
std::mutex mutex
Definition
op_count.hpp:73
bb::detail::GlobalOpCountContainer::~GlobalOpCountContainer
~GlobalOpCountContainer()
Definition
op_count.cpp:14
bb::detail::GlobalOpCount
Definition
op_count.hpp:86
bb::detail::GlobalOpCount::increment_op_count
static constexpr void increment_op_count()
Definition
op_count.hpp:99
bb::detail::GlobalOpCount::ensure_stats
static OpStats * ensure_stats()
Definition
op_count.hpp:91
bb::detail::GlobalOpCount::add_clock_time
static constexpr void add_clock_time(std::size_t time)
Definition
op_count.hpp:108
bb::detail::GlobalOpCount::stats
static thread_local std::shared_ptr< OpStats > stats
Definition
op_count.hpp:89
bb::detail::OpCountTimeReporter
Definition
op_count.hpp:122
bb::detail::OpCountTimeReporter::time
std::size_t time
Definition
op_count.hpp:124
bb::detail::OpCountTimeReporter::stats
OpStats * stats
Definition
op_count.hpp:123
bb::detail::OpCountTimeReporter::~OpCountTimeReporter
~OpCountTimeReporter()
Definition
op_count.cpp:96
bb::detail::OpStats
Definition
op_count.hpp:59
bb::detail::OpStats::time
std::size_t time
Definition
op_count.hpp:61
bb::detail::OpStats::count
std::size_t count
Definition
op_count.hpp:60
bb::detail::OperationLabel
Definition
op_count.hpp:46
bb::detail::OperationLabel::OperationLabel
constexpr OperationLabel(const char(&str)[N])
Definition
op_count.hpp:48
bb::detail::OperationLabel::value
char value[N]
Definition
op_count.hpp:56
src
barretenberg
common
op_count.hpp
Generated by
1.9.8