|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 TU Dresden |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Authors: |
| 6 | + * Christian Menard |
| 7 | + */ |
| 8 | + |
| 9 | +#ifndef REACTOR_CPP_STATISTICS_HH |
| 10 | +#define REACTOR_CPP_STATISTICS_HH |
| 11 | + |
| 12 | +#include <atomic> |
| 13 | + |
| 14 | +#include "reactor-cpp/config.hh" |
| 15 | +#include "reactor-cpp/logging.hh" |
| 16 | + |
| 17 | +namespace reactor { |
| 18 | + |
| 19 | +class Statistics { |
| 20 | +private: |
| 21 | +#ifdef REACTOR_CPP_PRINT_STATISTICS |
| 22 | + constexpr static bool enabled_{true}; |
| 23 | +#else |
| 24 | + constexpr static bool enabled_{false}; |
| 25 | +#endif |
| 26 | + |
| 27 | + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 28 | + inline static std::atomic_size_t reactor_instances_{0}; |
| 29 | + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 30 | + inline static std::atomic_size_t connections_{0}; |
| 31 | + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 32 | + inline static std::atomic_size_t reactions_{0}; |
| 33 | + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 34 | + inline static std::atomic_size_t actions_{0}; |
| 35 | + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 36 | + inline static std::atomic_size_t ports_{0}; |
| 37 | + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 38 | + inline static std::atomic_size_t processed_events_{0}; |
| 39 | + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 40 | + inline static std::atomic_size_t processed_reactions_{0}; |
| 41 | + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 42 | + inline static std::atomic_size_t triggered_actions_{0}; |
| 43 | + |
| 44 | + inline static void increment(std::atomic_size_t& counter) { |
| 45 | + if constexpr (enabled_) { |
| 46 | + counter.fetch_add(1, std::memory_order_release); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | +public: |
| 51 | + inline static void increment_reactor_instances() { increment(reactor_instances_); } |
| 52 | + inline static void increment_connections() { increment(connections_); } |
| 53 | + inline static void increment_reactions() { increment(reactions_); } |
| 54 | + inline static void increment_actions() { increment(actions_); } |
| 55 | + inline static void increment_ports() { increment(ports_); } |
| 56 | + inline static void increment_processed_events() { increment(processed_events_); } |
| 57 | + inline static void increment_processed_reactions() { increment(processed_reactions_); } |
| 58 | + inline static void increment_triggered_actions() { increment(triggered_actions_); } |
| 59 | + |
| 60 | + inline static auto reactor_instances() { return reactor_instances_.load(std::memory_order_acquire); } |
| 61 | + inline static auto connections() { return connections_.load(std::memory_order_acquire); } |
| 62 | + inline static auto reactions() { return reactions_.load(std::memory_order_acquire); } |
| 63 | + inline static auto actions() { return actions_.load(std::memory_order_acquire); } |
| 64 | + inline static auto ports() { return ports_.load(std::memory_order_acquire); } |
| 65 | + inline static auto processed_events() { return processed_events_.load(std::memory_order_acquire); } |
| 66 | + inline static auto processed_reactions() { return processed_reactions_.load(std::memory_order_acquire); } |
| 67 | + inline static auto triggered_actions() { return triggered_actions_.load(std::memory_order_acquire); } |
| 68 | + |
| 69 | + inline static void print() { |
| 70 | + if constexpr (enabled_) { |
| 71 | + reactor::log::Info() << "-----------------------------------------------------------"; |
| 72 | + reactor::log::Info() << "Program statistics:"; |
| 73 | + reactor::log::Info() << " - number of reactors: " << reactor_instances(); |
| 74 | + reactor::log::Info() << " - number of connections: " << connections(); |
| 75 | + reactor::log::Info() << " - number of reactions " << reactions(); |
| 76 | + reactor::log::Info() << " - number of actions: " << actions(); |
| 77 | + reactor::log::Info() << " - number of ports: " << ports(); |
| 78 | + reactor::log::Info() << "Execution statistics:"; |
| 79 | + reactor::log::Info() << " - processed events: " << processed_events(); |
| 80 | + reactor::log::Info() << " - triggered actions: " << triggered_actions(); |
| 81 | + reactor::log::Info() << " - processed reactions: " << processed_reactions(); |
| 82 | + reactor::log::Info() << "-----------------------------------------------------------"; |
| 83 | + } |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +} // namespace reactor |
| 88 | + |
| 89 | +#endif // REACTOR_CPP_STATISTICS_HH |
0 commit comments