This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathevent.cc
238 lines (207 loc) · 7.67 KB
/
event.cc
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "wtf/event.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iterator>
namespace wtf {
platform::atomic<int> EventDefinition::next_event_id_{
StandardEvents::kScopeLeaveEventId + 1};
namespace {
bool IsSepCharOrNull(char c) {
return c <= ' ' || c == ','; // Note: Explicitly matches null.
}
// Scans for the next argument name in a list of identifiers separated by
// separator characters. Returns true if found and sets *out_arg_name and
// *out_len to point to the name and its length (it may not be zero terminated).
// Also advances *arg_names to one past the end of the argument name or nullptr
// if at the end.
bool PeelArgName(const char** arg_names, const char** out_arg_name,
size_t* out_len) {
const char* arg_name = *arg_names;
if (!arg_name || !*arg_name) {
return false;
}
while (IsSepCharOrNull(*arg_name)) {
if (*arg_name == 0) {
// Hit the end of the string searching for end of trailing white-space.
*arg_names = nullptr;
return false;
}
arg_name += 1;
}
// arg_name now points to the beginning of the name.
size_t len = 1;
while (!IsSepCharOrNull(arg_name[len])) {
len += 1;
}
// Output results.
*arg_names = arg_name + len;
*out_arg_name = arg_name;
*out_len = len;
return true;
}
} // namespace
void EventDefinition::AppendName(std::string* output) const {
// Colons are used as separators in WTF's binary format so can't be part of
// identifiers, but '::' commonly appears in auto-generated C++ identifier
// names, as with the __PRETTY_FUNCTION__ built-in macro.
// Replace double colons with '#', which is WTF's class/namespace separator.
//
// A single : in a name_spec separates the name part from arguments,
// if there are arguments.
const char *src = name_spec_;
if (argument_zipper_) {
const char* colon = strchr(src, ':');
while (colon) {
output->append(src, (colon - src));
src = colon + 1;
if (*src == ':') {
// Double colon, just output it and continue.
output->append("::");
src += 1;
colon = strchr(src, ':');
} else {
// This was a single colon. Output no more.
return;
}
}
}
// Append anything remaining in src.
output->append(src);
}
void EventDefinition::AppendArguments(std::string* output) const {
if (argument_zipper_ && name_spec_) {
const char* arg_names = strchr(name_spec_, ':');
while (arg_names) {
// Colon found - advance.
arg_names += 1;
if (*arg_names == ':') {
// Actually a '::' namespace separator, keep looking.
arg_names += 1;
arg_names = strchr(arg_names, ':');
} else {
break;
}
}
argument_zipper_(output, arg_names);
}
}
void EventDefinition::ZipArgument(std::string* output, size_t index,
const char* next_arg_type,
const char** arg_names) {
if (output->size()) {
output->append(", ");
}
output->append(next_arg_type);
output->push_back(' ');
// Output the name.
const char* arg_name;
size_t len;
if (PeelArgName(arg_names, &arg_name, &len)) {
output->append(arg_name, len);
} else {
// Generate one.
char tmp_name[16];
snprintf(tmp_name, sizeof(tmp_name), "a%zu", index);
tmp_name[sizeof(tmp_name) - 1] = 0; // Overflow guard.
output->append(tmp_name);
}
}
EventRegistry::EventRegistry() = default;
EventRegistry* EventRegistry::GetInstance() {
static EventRegistry instance;
return &instance;
}
void EventRegistry::AddEventDefinition(EventDefinition event_definition) {
EventRegistry* instance = GetInstance();
platform::lock_guard<platform::mutex> lock{instance->mu_};
instance->event_definitions_.push_back(event_definition);
}
std::vector<EventDefinition> EventRegistry::GetEventDefinitions(
size_t from_index) {
platform::lock_guard<platform::mutex> lock{mu_};
if (from_index >= event_definitions_.size()) {
return std::vector<EventDefinition>{};
}
std::vector<EventDefinition> r;
r.reserve(event_definitions_.size() - from_index);
std::copy(event_definitions_.begin() + from_index, event_definitions_.end(),
std::back_inserter(r));
return r;
}
ZoneRegistry::ZoneRegistry() = default;
ZoneRegistry* ZoneRegistry::GetInstance() {
static ZoneRegistry instance;
return &instance;
}
int ZoneRegistry::CreateZone(const char* name, const char* type,
const char* location) {
platform::lock_guard<platform::mutex> lock{mu_};
int id = next_zone_id_.fetch_add(1);
zone_definitions_.push_back(ZoneDefinition{
id, name ? name : "", type ? type : "", location ? location : ""});
return id;
}
int ZoneRegistry::EmitZones(EventBuffer* event_buffer, size_t from_index) {
platform::lock_guard<platform::mutex> lock{mu_};
size_t size = zone_definitions_.size();
if (from_index >= size) {
return size;
}
for (size_t i = from_index; i < size; i++) {
auto& definition = zone_definitions_[i];
StandardEvents::CreateZone(event_buffer, definition.id,
definition.name.c_str(), definition.type.c_str(),
definition.location.c_str());
}
return size;
}
StandardEvents::ScopeLeaveEventType& StandardEvents::GetScopeLeaveEvent() {
static ScopeLeaveEventType event{kScopeLeaveEventId, EventClass::kInstance,
EventFlags::kBuiltin | EventFlags::kInternal,
"wtf.scope#leave"};
return event;
}
StandardEvents::CreateZoneEventType& StandardEvents::GetCreateZoneEvent() {
static CreateZoneEventType event{EventClass::kInstance,
EventFlags::kBuiltin | EventFlags::kInternal,
"wtf.zone#create:zoneId,name,type,location"};
return event;
}
void StandardEvents::DefineEvent(EventBuffer* event_buffer, uint16_t wire_id,
uint16_t event_class, uint32_t flags,
const char* name, const char* args) {
static EventEnabled<uint16_t, uint16_t, uint32_t, const char*, const char*>
event{1, EventClass::kInstance,
EventFlags::kBuiltin | EventFlags::kInternal,
"wtf.event#define:wireId,eventClass,flags,name,args"};
event.InvokeSpecific(event_buffer, wire_id, event_class, flags, name, args);
}
void StandardEvents::ScopeLeave(EventBuffer* event_buffer) {
GetScopeLeaveEvent().InvokeSpecific(event_buffer);
}
void StandardEvents::CreateZone(EventBuffer* event_buffer, int zone_id,
const char* name, const char* type,
const char* location) {
GetCreateZoneEvent().InvokeSpecific(event_buffer, zone_id, name, type,
location);
}
void StandardEvents::SetZone(EventBuffer* event_buffer, int zone_id) {
static EventEnabled<uint16_t> event{
EventClass::kInstance, EventFlags::kBuiltin | EventFlags::kInternal,
"wtf.zone#set:zoneId"};
event.InvokeSpecific(event_buffer, zone_id);
}
void StandardEvents::FrameStart(EventBuffer* event_buffer, uint32_t number) {
static EventEnabled<uint32_t> event{EventClass::kInstance,
EventFlags::kInternal,
"wtf.timing#frameStart:number"};
event.InvokeSpecific(event_buffer, number);
}
void StandardEvents::FrameEnd(EventBuffer* event_buffer, uint32_t number) {
static EventEnabled<uint32_t> event{EventClass::kInstance,
EventFlags::kInternal,
"wtf.timing#frameEnd:number"};
event.InvokeSpecific(event_buffer, number);
}
} // namespace wtf