This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
/
runtime.h
245 lines (210 loc) · 8.93 KB
/
runtime.h
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
239
240
241
242
243
244
245
#ifndef TRACING_FRAMEWORK_BINDINGS_CPP_INCLUDE_WTF_RUNTIME_H_
#define TRACING_FRAMEWORK_BINDINGS_CPP_INCLUDE_WTF_RUNTIME_H_
#include <iostream>
#include <memory>
#include <vector>
#include "wtf/buffer.h"
#include "wtf/event.h"
#include "wtf/platform.h"
namespace wtf {
// Singleton runtime class, which serves as the main entry point to WTF
// functionality.
//
// All functionality of this class is thread safe.
//
// Saving:
// -------
// The most common interaction that most parties will have with the Runtime
// class will be to save trace files. There are several ways that this can
// be done.
//
// Saving a Full File:
// -------------------
// Save and do not clear:
// assert(Runtime::GetRuntime()::SaveToFile("filename.wtf-trace"));
// Save and and clear:
// assert(Runtime::GetRuntime()::SaveToFile("filename.wtf-trace"),
// Runtime::SaveOptions::ForClear());
//
// Incrementally Saving:
// ---------------------
// In this mode, something in the process is expected to repeatedly call
// SaveToFile in a way that causes it to append new data. Internal state is
// recorded in a SaveCheckpoint so that writing can resume with a minimum
// of repeated data on the next call to save.
//
// In this mode, thread data is destructively cleared upon write and only new
// metadata will be written on each call to save. If using SaveToFile()
// (versus passing an ostream directly), the system will detect if the file
// exists. If so, it assumes that it is continuing a save. If not, it will
// reset its checkpoint, starting a new file. In this way, the file can be
// externally deleted without further intervention.
//
// Example:
// Runtime::SaveCheckpoint checkpoint;
// while (true) {
// assert(Runtime::GetRuntime()::SaveToFile(
// "streaming.wtf-trace",
// Runtime::SaveOptions::ForStreamingFile(&checkpoint)));
// sleep(5);
// }
class Runtime {
public:
// A pointer to a SaveCheckpoint can be set in SaveOptions. Passing the
// SaveCheckpoint from a previous save iteration to a future one will cause
// only updates to be streamed out. This is really only valid when appending
// or concatenating output trace files since subsequent chunks do not have
// all data that they need to function.
class SaveCheckpoint {
public:
SaveCheckpoint() = default;
private:
// Whether this is still the first save operation.
bool needs_file_header = true;
// The index of the first event definition that needs to be written out.
size_t event_definition_from_index_ = 0;
// The index of the first zone registration that needs to be written out.
size_t zone_definition_from_index_ = 0;
friend class Runtime;
};
// Options controlling save.
struct SaveOptions {
static const SaveOptions kDefault;
SaveOptions() = default;
// Creates options configured to clear thread data on save.
static SaveOptions ForClear() {
SaveOptions options;
options.clear_thread_data = true;
return options;
}
// Creates options configured for streaming to a single file that is
// opened for append.
// The checkpoint should be retained until the file is no longer being
// saved to.
static SaveOptions ForStreamingFile(SaveCheckpoint* checkpoint) {
SaveOptions options;
options.checkpoint = checkpoint;
options.clear_thread_data = true;
options.open_mode = std::ios_base::app;
return options;
}
// Creates options configured for streaming to multiple files that will
// be externally concatenated in some way.
// The checkpoint should be retained until the file is no longer being
// saved to.
static SaveOptions ForStreamingMulti(SaveCheckpoint* checkpoint) {
SaveOptions options;
options.checkpoint = checkpoint;
options.clear_thread_data = true;
return options;
}
// If set, a checkpointed save will be done. Only updates from the last
// save will be written and this field will be updated to reflect the
// current state. This implies clear_thread_data and is generally best
// used with open_for_append.
SaveCheckpoint* checkpoint = nullptr;
// Clear saved thread data. Note that this will not clear shared data,
// which currently includes the string table and event registration buffers.
bool clear_thread_data = false;
// The open mode to use if a file is being opened. Defaults to trunc.
// out is implied.
std::ios_base::openmode open_mode = std::ios_base::trunc;
};
// Gets the singleton instance.
// Note that calling through to the instance is reserved for "heavy-weight"
// operations. Logging events happens without involving this instance.
static Runtime* GetInstance();
// Enables the current thread for WTF data collection. This no-ops if
// already enabled.
void EnableCurrentThread(const char* thread_name, const char* type = nullptr,
const char* location = nullptr);
// Registers an external thread returning an EventBuffer that will be
// retained for the life of the Runtime. This is useful for merging
// event-like constructs from entities other than threads (other cores, ISR,
// etc). With great power comes great responsibility: it is up to you to
// obey all of the rules of writing to an EventBuffer, the most important
// of which is to only write from one thread concurrently. You must also
// make sure that any timestamps or event ids that you add are consistent
// with the overall system.
EventBuffer* RegisterExternalThread(const char* thread_name,
const char* type = nullptr,
const char* location = nullptr);
// Disables WTF data collection for this thread. Note that any collected
// data will still be present. This is largely intended for testing.
void DisableCurrentThread();
// Saves the current WTF trace file for all threads. This is mostly non
// disruptive to concurrent operations, but there are a couple of sync
// points that are unavoidable.
// Returns: Whether the trace was saved properly (covers both logical and
// IO errors).
bool Save(std::ostream* out,
const SaveOptions& save_options = SaveOptions::kDefault);
// Shortcut to Save(ostream) that saves to a file.
// Returns: Whether the trace was saved properly (covers both logical and
// IO errors).
bool SaveToFile(const std::string& file_name,
const SaveOptions& save_options = SaveOptions::kDefault);
// Asynchronously clears thread data. This is similar to passing
// a clear_thread_data option to a Save() method, except that when doing it
// at save time, only the saved data is cleared.
void ClearThreadData();
// Resets the WTF runtime state. This is intended for testing and may fail
// or cause crashes if called when asynchronous logging is not quiesced.
void ResetForTesting();
// Pops an idle EventBuffer for the given task and then returns it when
// done. Typically used via the ScopedTask class.
EventBuffer* PopTaskEventBuffer(const std::string& name);
void PushTaskEventBuffer(const std::string& name, EventBuffer* event_buffer);
private:
// Each named task has an info descriptor with the idle event buffers.
struct TaskDefinition {
int next_instance_id = 0;
std::deque<EventBuffer*> idle_event_buffers;
};
Runtime();
Runtime(const Runtime&) = delete;
void operator=(const Runtime&) = delete;
// Creates an EventBuffer bound for a thread local, and adds it to the list
// of owned instances.
EventBuffer* CreateThreadEventBuffer();
platform::mutex mu_;
std::vector<std::unique_ptr<EventBuffer>> thread_event_buffers_;
std::unordered_map<std::string, TaskDefinition> tasks_;
int uniquifier_ = 0;
};
// Represents a temporary assignment of an EventBuffer to a thread.
// The previous state is restored when the ScopedTask goes out of
// scope.
template <bool kEnable>
class ScopedTaskIf {
public:
explicit ScopedTaskIf(std::string name)
: name_(std::move(name)),
previous_event_buffer_(PlatformGetThreadLocalEventBuffer()) {
PlatformSetThreadLocalEventBuffer(
Runtime::GetInstance()->PopTaskEventBuffer(name_));
}
~ScopedTaskIf() {
Runtime::GetInstance()->PushTaskEventBuffer(
name_, PlatformGetThreadLocalEventBuffer());
PlatformSetThreadLocalEventBuffer(previous_event_buffer_);
}
ScopedTaskIf(const ScopedTaskIf&) = delete;
void operator=(const ScopedTaskIf&) = delete;
private:
std::string name_;
EventBuffer* previous_event_buffer_;
};
// Explicit disabled instantiation of ScopedTaskIf.
template <>
class ScopedTaskIf<false> {
public:
explicit ScopedTaskIf(std::string name) {}
~ScopedTaskIf() = default;
ScopedTaskIf(const ScopedTaskIf&) = delete;
void operator=(const ScopedTaskIf&) = delete;
};
using ScopedTask = ScopedTaskIf<kMasterEnable>;
using ScopedTaskEnabled = ScopedTaskIf<true>;
} // namespace wtf
#endif // TRACING_FRAMEWORK_BINDINGS_CPP_INCLUDE_WTF_RUNTIME_H_