1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/profiling/counterstrace.h>
#if ZEN_WITH_TRACE
# include <zencore/timer.h>
# include <zencore/trace.h>
# include <atomic>
# include <cstring>
namespace {
// Wire-compatible with UE's CountersTrace events (Counters.Spec /
// SetValueInt / SetValueFloat). Channel name matches UE so users can mix &
// match analyzers.
UE_TRACE_CHANNEL_DEFINE(CountersChannel)
UE_TRACE_EVENT_BEGIN(Counters, Spec, NoSync | Important)
UE_TRACE_EVENT_FIELD(uint16_t, Id)
UE_TRACE_EVENT_FIELD(uint8_t, Type)
UE_TRACE_EVENT_FIELD(uint8_t, DisplayHint)
UE_TRACE_EVENT_FIELD(UE::Trace::AnsiString, Name)
UE_TRACE_EVENT_END()
UE_TRACE_EVENT_BEGIN(Counters, SetValueInt)
UE_TRACE_EVENT_FIELD(uint64_t, Cycle)
UE_TRACE_EVENT_FIELD(int64_t, Value)
UE_TRACE_EVENT_FIELD(uint16_t, CounterId)
UE_TRACE_EVENT_END()
UE_TRACE_EVENT_BEGIN(Counters, SetValueFloat)
UE_TRACE_EVENT_FIELD(uint64_t, Cycle)
UE_TRACE_EVENT_FIELD(double, Value)
UE_TRACE_EVENT_FIELD(uint16_t, CounterId)
UE_TRACE_EVENT_END()
} // namespace
namespace zen::counters_detail {
uint16_t
OutputInitCounter(const char* Name, TraceCounterType Type, TraceCounterDisplayHint Hint)
{
if (!UE_TRACE_CHANNELEXPR_IS_ENABLED(CountersChannel) || Name == nullptr)
{
return 0;
}
// Counter ids are uint16; tourist's analyzer truncates anyway. Wrapping
// past 0xFFFF would re-use ids, so we cap allocation -- in practice no
// real-world trace uses anywhere near 65k distinct counters.
static std::atomic<uint32_t> g_NextId{0};
uint32_t Allocated = g_NextId.fetch_add(1, std::memory_order_relaxed) + 1;
if (Allocated > 0xFFFFu)
{
return 0;
}
uint16_t Id = uint16_t(Allocated);
uint16_t NameLen = uint16_t(std::strlen(Name));
UE_TRACE_LOG(Counters, Spec, CountersChannel, NameLen * sizeof(char))
<< Spec.Id(Id) << Spec.Type(uint8_t(Type)) << Spec.DisplayHint(uint8_t(Hint)) << Spec.Name(Name, NameLen);
return Id;
}
void
OutputSetValueInt(uint16_t Id, int64_t Value)
{
if (Id == 0 || !UE_TRACE_CHANNELEXPR_IS_ENABLED(CountersChannel))
{
return;
}
UE_TRACE_LOG(Counters, SetValueInt, CountersChannel)
<< SetValueInt.Cycle(zen::GetHifreqTimerValue()) << SetValueInt.Value(Value) << SetValueInt.CounterId(Id);
}
void
OutputSetValueFloat(uint16_t Id, double Value)
{
if (Id == 0 || !UE_TRACE_CHANNELEXPR_IS_ENABLED(CountersChannel))
{
return;
}
UE_TRACE_LOG(Counters, SetValueFloat, CountersChannel)
<< SetValueFloat.Cycle(zen::GetHifreqTimerValue()) << SetValueFloat.Value(Value) << SetValueFloat.CounterId(Id);
}
bool
IsCountersChannelEnabled()
{
return UE_TRACE_CHANNELEXPR_IS_ENABLED(CountersChannel);
}
} // namespace zen::counters_detail
#endif // ZEN_WITH_TRACE
|