Commit 98ad3e0f authored by Pádraig Ó Conbhuí's avatar Pádraig Ó Conbhuí
Browse files

Add support for compiling with stricter warnings

parent ee8425c9
Pipeline #1560 failed with stages
in 25 seconds
......@@ -3,16 +3,24 @@
#include <algorithm>
#include <atomic>
#include <cassert>
#include <chrono>
#include <cinttypes>
#include <cstddef>
#include <iostream>
#include <iterator>
#include <memory>
#include <utility>
#include <vector>
#include <cassert>
#include <cinttypes>
#include <cstddef>
// Optional C++ version specific bits
#if __cplusplus >= 201703L
#define PERFORATE_NODISCARD [[nodiscard]]
#else
#define PERFORATE_NODISCARD
#endif
namespace perforate {
namespace detail {
......@@ -102,7 +110,7 @@ public:
int64_t call_count() const { return m_call_count.load(); }
void add_range(std::chrono::nanoseconds time, size_t calls) {
void add_range(std::chrono::nanoseconds time, int64_t calls) {
m_accumulated_ns += time.count();
m_call_count += calls;
}
......@@ -120,11 +128,15 @@ public:
template <typename Stream, typename Stats>
void print_range_stats(Stream &stream, const Stats &stats) {
const std::chrono::nanoseconds accumulated_ns = stats.accumulated_time();
const double accumulated_seconds = accumulated_ns.count() * 1e-9;
const double accumulated_seconds =
static_cast<double>(accumulated_ns.count()) * 1e-9;
const double seconds_per_call =
accumulated_seconds / static_cast<double>(stats.call_count());
stream << stats.range_name() << ": " << accumulated_seconds << " s, "
<< (accumulated_seconds / stats.call_count()) << " s/call, "
<< stats.call_count() << " call(s)\n";
<< seconds_per_call << " s/call, " << stats.call_count()
<< " call(s)\n";
}
template <typename Stream, typename StatsPtrList>
......@@ -176,7 +188,7 @@ class per_tag_range_stats : public per_tag<range_stats_print_on_exit_policy> {};
} // namespace detail
template <typename Tag, size_t AggregateCount = 1,
template <typename Tag, int64_t AggregateCount = 1,
typename Clock = std::chrono::high_resolution_clock>
class scoped_trace {
static_assert((AggregateCount & (AggregateCount - 1)) == 0,
......@@ -185,7 +197,15 @@ class scoped_trace {
using time_point = typename Clock::time_point;
time_point m_t0 = Clock::now();
bool m_active = true;
static constexpr time_point t_disabled() {
using duration = typename Clock::duration;
using rep = typename duration::rep;
constexpr auto rep_max = std::numeric_limits<rep>::max();
constexpr auto time_max = time_point(duration(rep_max));
return time_max;
}
static detail::range_stats &stats() {
return detail::per_tag_range_stats::value<Tag>();
......@@ -204,19 +224,14 @@ public:
// Move constructor marks other as inactive
scoped_trace(scoped_trace &&other)
: m_t0{std::move(other.m_t0)}, m_active{std::exchange(other.m_active,
false)} {}
scoped_trace &operator=(scoped_trace &&other) {
if (this != &other) {
m_t0 = std::move(other.m_t0);
m_active = std::exchange(other.m_active, false);
}
return *this;
: m_t0{std::exchange(other.m_t0, t_disabled())} {
assert(other.m_t0 == t_disabled());
}
scoped_trace &operator=(scoped_trace &&other) = delete;
~scoped_trace() {
if (m_active) {
if (m_t0 != t_disabled()) {
const auto now = Clock::now();
const auto delta = now - m_t0;
......@@ -230,13 +245,13 @@ public:
}
};
template <typename Tag, size_t AggregateCount = 0>
[[nodiscard]] auto make_scoped_trace(const char *range_name) {
template <typename Tag, int64_t AggregateCount = 0>
PERFORATE_NODISCARD auto make_scoped_trace(const char *range_name) {
return scoped_trace<Tag, AggregateCount>(range_name);
}
template <size_t AggregateCount = 0, typename Tag = void>
[[nodiscard]] auto make_scoped_trace(const char *range_name, Tag &&) {
template <int64_t AggregateCount = 0, typename Tag = void>
PERFORATE_NODISCARD auto make_scoped_trace(const char *range_name, Tag &&) {
return make_scoped_trace<Tag, AggregateCount>(range_name);
}
......@@ -255,7 +270,7 @@ template <size_t AggregateCount = 0, typename Tag = void>
PERFORATE_SCOPED_TRACE_V(RANGE_NAME)
// End a scoped_trace early by "std::move"ing into this sink function
template <typename Tag, size_t AggregateCount>
template <typename Tag, int64_t AggregateCount>
void end_scoped_trace(scoped_trace<Tag, AggregateCount>) {}
} // namespace perforate
......
......@@ -30,12 +30,12 @@ public:
using per_tag_index = per_tag_test_helper::type_index;
// Generate some pre-main indices
auto index_1 = per_tag_index::safe_value<class index_1_tag>();
auto index_2 = per_tag_index::safe_value<class index_2_tag>();
static auto index_1 = per_tag_index::safe_value<class index_1_tag>();
static auto index_2 = per_tag_index::safe_value<class index_2_tag>();
// Generate indices using lambdas as unique types
auto index_3 = per_tag_index::safe_value([] {});
auto index_4 = per_tag_index::safe_value([] {});
static auto index_3 = per_tag_index::safe_value([] {});
static auto index_4 = per_tag_index::safe_value([] {});
// Generate indices inside a templated context
template <typename Tag> struct templated_per_tag_index {
......@@ -117,9 +117,9 @@ TEST_CASE("detail::per_tag should create unique instances for unique types",
}
}
auto &rs_1 =
static auto &rs_1 =
perforate::detail::per_tag_range_stats::safe_value<class rs_1_tag>();
auto &rs_2 =
static auto &rs_2 =
perforate::detail::per_tag_range_stats::safe_value<class rs_2_tag>();
TEST_CASE(
......@@ -250,7 +250,7 @@ TEST_CASE("scoped_trace helper functions and macros", "[scoped_trace]") {
SECTION("make_scoped_trace and end_scoped_trace") {
class mst_1_tag;
const size_t num_calls = 10;
const int64_t num_calls = 10;
for (size_t i = 0; i < num_calls; i++) {
auto mst = perforate::make_scoped_trace<mst_1_tag>("test::mst_1");
}
......@@ -259,8 +259,8 @@ TEST_CASE("scoped_trace helper functions and macros", "[scoped_trace]") {
REQUIRE(stats.call_count() == num_calls);
// scoped_trace should increment call_count on destruction
const size_t extra_calls = 5;
for (size_t i = 0; i < extra_calls; i++) {
const int64_t extra_calls = 5;
for (int64_t i = 0; i < extra_calls; i++) {
REQUIRE(stats.call_count() == num_calls + i);
auto mst = perforate::make_scoped_trace<mst_1_tag>("test::mst_1");
REQUIRE(stats.call_count() == num_calls + i);
......@@ -284,8 +284,8 @@ TEST_CASE("scoped_trace helper functions and macros", "[scoped_trace]") {
}
SECTION("PERFORATE_SCOPED_TRACE_V should return a scoped_trace") {
const size_t num_calls = 33;
for (size_t i = 0; i < num_calls; i++) {
const int64_t num_calls = 33;
for (int64_t i = 0; i < num_calls; i++) {
auto mst = PERFORATE_SCOPED_TRACE_V("test::macro_scoped_trace_v");
const auto &stats = find_stats("test::macro_scoped_trace_v");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment