Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
op_count.cpp
Go to the documentation of this file.
1#ifndef __wasm__
2#include "op_count.hpp"
3#include <iostream>
4#include <ostream>
5#include <sstream>
6#include <thread>
7
8namespace bb::detail {
9
10// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
12 std::getenv("BB_USE_OP_COUNT_TIME") == nullptr ? false : std::string(std::getenv("BB_USE_OP_COUNT_TIME")) == "1";
13
15{
16 // This is useful for printing counts at the end of non-benchmarks.
17 // See op_count_google_bench.hpp for benchmarks.
18 // print();
19}
20
22{
24 std::stringstream ss;
25 ss << std::this_thread::get_id();
26 counts.push_back({ key, ss.str(), count });
27}
28
30{
31 std::cout << "print_op_counts() START" << std::endl;
32 for (const Entry& entry : counts) {
33 if (entry.count->count > 0) {
34 std::cout << entry.key << "\t" << entry.count->count << "\t[thread=" << entry.thread_id << "]" << std::endl;
35 }
36 if (entry.count->time > 0) {
37 std::cout << entry.key << "(t)\t" << static_cast<double>(entry.count->time) / 1000000.0
38 << "ms\t[thread=" << entry.thread_id << "]" << std::endl;
39 }
40 }
41 std::cout << "print_op_counts() END" << std::endl;
42}
43
45{
47 for (const Entry& entry : counts) {
48 if (entry.count->count > 0) {
49 aggregate_counts[entry.key] += entry.count->count;
50 }
51 if (entry.count->time > 0) {
52 aggregate_counts[entry.key + "(t)"] += entry.count->time;
53 }
54 }
55 return aggregate_counts;
56}
57
58void GlobalOpCountContainer::print_aggregate_counts(std::ostream& os, size_t indent) const
59{
60 os << '{';
61 bool first = true;
62 for (const auto& [key, value] : get_aggregate_counts()) {
63 if (!first) {
64 os << ',';
65 }
66 if (indent > 0) {
67 os << std::endl << std::string(indent, ' ');
68 }
69 os << '"' << key << "\":" << value;
70 first = false;
71 }
72 if (indent > 0) {
73 os << std::endl;
74 }
75 os << '}' << std::endl;
76}
77
79{
81 for (Entry& entry : counts) {
82 *entry.count = OpStats();
83 }
84}
85
86// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
88
90 : stats(stats)
91{
92 auto now = std::chrono::high_resolution_clock::now();
93 auto now_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
94 time = static_cast<std::size_t>(now_ns.time_since_epoch().count());
95}
97{
98 auto now = std::chrono::high_resolution_clock::now();
99 auto now_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
100 stats->count += 1;
101 stats->time += static_cast<std::size_t>(now_ns.time_since_epoch().count()) - time;
102}
103} // namespace bb::detail
104#endif
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::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
OpCountTimeReporter(OpStats *stats)
Definition op_count.cpp:89
std::size_t time
Definition op_count.hpp:61
std::size_t count
Definition op_count.hpp:60