Skip to content

Commit e1996d4

Browse files
committed
Add internal Perf interface
1 parent 6608613 commit e1996d4

5 files changed

Lines changed: 80 additions & 12 deletions

File tree

src/Perf.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ std::unordered_set<const Counters*> perThreadCounters;
4343
} // namespace Internal
4444

4545
// Init thread local thread counters
46-
thread_local ThreadCounters threadCounters;
46+
thread_local ThreadCounters counters;
4747

4848
/**
4949
* Construct and register a new per thread set of counters.
5050
*/
5151
ThreadCounters::ThreadCounters()
52+
: Counters()
5253
{
5354
std::lock_guard<std::mutex> lock(Internal::mutex);
5455
Internal::perThreadCounters.insert(this);
@@ -60,7 +61,7 @@ ThreadCounters::ThreadCounters()
6061
ThreadCounters::~ThreadCounters()
6162
{
6263
std::lock_guard<std::mutex> lock(Internal::mutex);
63-
Internal::globalCounters.active_cycles += this->active_cycles;
64+
Internal::globalCounters.add(this);
6465
Internal::perThreadCounters.erase(this);
6566
}
6667

@@ -73,11 +74,14 @@ getStats(Stats* stats)
7374
stats->timestamp = PerfUtils::Cycles::rdtsc();
7475
stats->cycles_per_second = PerfUtils::Cycles::perSecond();
7576

76-
stats->active_cycles = Internal::globalCounters.active_cycles;
77+
Counters output;
78+
output.add(&Internal::globalCounters);
7779

7880
for (const Counters* counters : Internal::perThreadCounters) {
79-
stats->active_cycles += counters->active_cycles;
81+
output.add(counters);
8082
}
83+
84+
output.dumpStats(stats);
8185
}
8286

8387
} // namespace Perf

src/Perf.h

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,75 @@ namespace Perf {
2727
* Collection of collected performance counters.
2828
*/
2929
struct 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

src/Receiver.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,7 @@ Receiver::trySendGrants()
778778

779779
uint64_t elapsed_cycles = PerfUtils::Cycles::rdtsc() - start_tsc;
780780
if (!idle) {
781-
Perf::threadCounters.active_cycles.fetch_add(elapsed_cycles,
782-
std::memory_order_relaxed);
781+
Perf::counters.active_cycles.add(elapsed_cycles);
783782
}
784783
}
785784

src/Sender.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,7 @@ Sender::trySend()
10151015
sending.clear();
10161016
uint64_t elapsed_cycles = PerfUtils::Cycles::rdtsc() - start_tsc;
10171017
if (!idle) {
1018-
Perf::threadCounters.active_cycles.fetch_add(elapsed_cycles,
1019-
std::memory_order_relaxed);
1018+
Perf::counters.active_cycles.add(elapsed_cycles);
10201019
}
10211020
}
10221021

src/TransportImpl.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ TransportImpl::processPackets()
134134
}
135135
uint64_t elapsed_cycles = PerfUtils::Cycles::rdtsc() - start_tsc;
136136
if (!idle) {
137-
Perf::threadCounters.active_cycles.fetch_add(elapsed_cycles,
138-
std::memory_order_relaxed);
137+
Perf::counters.active_cycles.add(elapsed_cycles);
139138
}
140139
}
141140

0 commit comments

Comments
 (0)