@@ -27,8 +27,75 @@ namespace Perf {
2727 * Collection of collected performance counters.
2828 */
2929struct Counters {
30+ /* *
31+ * Wrapper class for individual counter entires to
32+ */
33+ template <typename T>
34+ struct Stat : private std ::atomic<T> {
35+ /* *
36+ * Passthrough constructor.
37+ */
38+ template <typename ... Args>
39+ Stat (Args&&... args)
40+ : std::atomic<T>(static_cast <Args&&>(args)...)
41+ {}
42+
43+ /* *
44+ * Add the value of another Stat to this Stat.
45+ */
46+ void add (const Stat<T>& other)
47+ {
48+ this ->fetch_add (other.load (std::memory_order_relaxed),
49+ std::memory_order_relaxed);
50+ }
51+
52+ /* *
53+ * Add the given value to this Stat.
54+ */
55+ void add (T val)
56+ {
57+ this ->fetch_add (val, std::memory_order_relaxed);
58+ }
59+
60+ /* *
61+ * Return the stat value.
62+ */
63+ T get () const
64+ {
65+ return this ->load (std::memory_order_relaxed);
66+ }
67+ };
68+
69+ /* *
70+ * Default constructor.
71+ */
72+ Counters ()
73+ : active_cycles(0 )
74+ {}
75+
76+ /* *
77+ * Default destructor.
78+ */
79+ ~Counters () = default ;
80+
81+ /* *
82+ * Add the values in other to the corresponding counters in this object.
83+ */
84+ void add (const Counters* other)
85+ {
86+ active_cycles.add (other->active_cycles );
87+ }
88+
89+ /* *
90+ * Export this object's counter values to a Stats structure.
91+ */
92+ void dumpStats (Stats* stats)
93+ {
94+ stats->active_cycles = active_cycles.get ();
95+ }
96+
3097 // / CPU time spent actively processing Homa messages in cycles.
31- std::atomic <uint64_t > active_cycles;
98+ Stat <uint64_t > active_cycles;
3299};
33100
34101/* *
@@ -42,7 +109,7 @@ struct ThreadCounters : public Counters {
42109/* *
43110 * Per thread counters.
44111 */
45- extern thread_local ThreadCounters threadCounters ;
112+ extern thread_local ThreadCounters counters ;
46113
47114} // namespace Perf
48115} // namespace Homa
0 commit comments