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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
#include "tracing/tracing.h"
#include "graphics/2d.h"
#include "parse/parselo.h"
#include "io/timer.h"
#include "TraceEventWriter.h"
#include "MainFrameTimer.h"
#include "FrameProfiler.h"
#include <cinttypes>
#include <fstream>
#include <future>
#include <mutex>
// A function for getting the id of the current thread
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static int64_t get_tid() {
return (int64_t) GetCurrentThreadId();
}
#elif __LINUX__
#include <sys/syscall.h>
static int64_t get_tid() {
return (int64_t) syscall(SYS_gettid);
}
#else
#include <pthread.h>
static int64_t get_tid() {
// This is not a reliable way of getting the tid but it's better than nothing
return (int64_t) pthread_self();
}
#endif
// A function for getting the id of the current process
#ifdef WIN32
static int64_t get_pid() {
return (int64_t)GetCurrentProcessId();
}
#else
#include <unistd.h>
static int64_t get_pid() {
return (int64_t) getpid();
}
#endif
namespace {
using namespace tracing;
std::unique_ptr<ThreadedTraceEventWriter> traceEventWriter;
std::unique_ptr<ThreadedMainFrameTimer> mainFrameTimer;
std::unique_ptr<FrameProfiler> frameProfiler;
SCP_vector<int> query_objects;
// The GPU timestamp queries use an internal free list to reduce the number of graphics API calls
SCP_queue<int> free_query_objects;
bool do_gpu_queries = true;
int get_query_object() {
if (!free_query_objects.empty()) {
auto id = free_query_objects.front();
free_query_objects.pop();
return id;
}
auto id = gr_create_query_object();
query_objects.push_back(id);
return id;
}
int get_gpu_timestamp_query() {
GR_DEBUG_SCOPE("Query tracing timestamp");
auto query = get_query_object();
gr_query_value(query, QueryType::Timestamp);
return query;
}
void free_query_object(int obj) {
free_query_objects.push(obj);
}
struct gpu_trace_event {
trace_event base_evt;
int gpu_begin_query = -1;
int gpu_end_query = -1;
};
SCP_queue<gpu_trace_event> gpu_events;
bool initialized = false;
bool do_trace_events = false;
bool do_async_events = false;
bool do_counter_events = false;
std::int64_t main_thread_id = -1;
int gpu_start_query = -1;
std::uint64_t gpu_start_time = 0;
std::uint64_t cpu_start_time = 0;
std::uint64_t current_id = 0;
void submit_event(trace_event* evt) {
if (evt->pid == GPU_PID) {
evt->timestamp -= gpu_start_time;
} else {
evt->timestamp -= cpu_start_time;
}
if (traceEventWriter) {
// Trace event writer receives all events
traceEventWriter->processEvent(evt);
}
if (mainFrameTimer) {
mainFrameTimer->processEvent(evt);
}
if (frameProfiler) {
frameProfiler->processEvent(evt);
}
}
void process_gpu_events() {
Assertion(get_tid() == main_thread_id, "This function must be called from the main thread!");
if (gpu_start_query >= 0) {
if (gr_query_value_available(gpu_start_query)) {
gpu_start_time = gr_get_query_value(gpu_start_query);
gpu_start_query = -1;
} else {
// Wait until query is finished
return;
}
}
GR_DEBUG_SCOPE("Query GPU timestamps");
while (!gpu_events.empty()) {
auto& first = gpu_events.front();
auto result_available = true;
if (first.gpu_begin_query != -1) {
if (gr_query_value_available(first.gpu_begin_query)) {
first.base_evt.timestamp = gr_get_query_value(first.gpu_begin_query);
free_query_object(first.gpu_begin_query);
first.gpu_begin_query = -1;
} else {
// Query not processed yet, try again later...
result_available = false;
}
}
switch (first.base_evt.type) {
case EventType::Complete:
// For complete events, check the end query
if (gr_query_value_available(first.gpu_end_query)) {
// All queries are finished, get the values and submit the event
auto finished_evt = first.base_evt;
auto val = gr_get_query_value(first.gpu_end_query);
finished_evt.duration = val - finished_evt.timestamp;
free_query_object(first.gpu_end_query);
submit_event(&finished_evt);
gpu_events.pop();
} else {
result_available = false;
}
break;
case EventType::Begin:
case EventType::End:
if (result_available) {
submit_event(&first.base_evt);
gpu_events.pop();
}
break;
default:
UNREACHABLE("Invalid event type!");
gpu_events.pop();
break;
}
if (!result_available) {
// GPU result not available, try again next frame
break;
}
}
}
void init_event(const Category& category, trace_event* evt) {
evt->category = &category;
evt->timestamp = timer_get_nanoseconds();
evt->pid = get_pid();
evt->tid = get_tid();
}
}
namespace tracing {
void init() {
do_trace_events = false;
do_async_events = false;
do_counter_events = false;
if (Cmdline_json_profiling) {
traceEventWriter.reset(new ThreadedTraceEventWriter());
do_trace_events = true;
do_async_events = true;
do_counter_events = true;
}
if (Cmdline_profile_write_file) {
mainFrameTimer.reset(new ThreadedMainFrameTimer());
do_async_events = true;
}
if (Cmdline_frame_profile) {
frameProfiler.reset(new FrameProfiler());
do_trace_events = true;
}
do_gpu_queries = gr_is_capable(CAPABILITY_TIMESTAMP_QUERY);
if (do_gpu_queries) {
gpu_start_query = get_gpu_timestamp_query();
}
cpu_start_time = timer_get_nanoseconds();
main_thread_id = get_tid();
initialized = true;
}
void process_events() {
if (do_gpu_queries) {
// Process pending GPU events
process_gpu_events();
}
}
void frame_profile_process_frame() {
Assertion(frameProfiler, "Frame profiling must be enabled for this function!");
return frameProfiler->processFrame();
}
SCP_string get_frame_profile_output() {
Assertion(frameProfiler, "Frame profiling must be enabled for this function!");
return frameProfiler->getContent();
}
void shutdown() {
while (!gpu_events.empty()) {
process_events();
// Don't do busy waiting...
os_sleep(5);
}
for (auto query : query_objects) {
gr_delete_query_object(query);
}
query_objects.clear();
mainFrameTimer = nullptr;
traceEventWriter = nullptr;
initialized = false;
}
namespace complete {
void start(const Category& category, trace_event* evt) {
if (!do_trace_events) {
// No one to process the event is here
return;
}
if (!initialized) {
return;
}
init_event(category, evt);
evt->duration = 0;
evt->type = EventType::Complete;
evt->event_id = ++current_id;
if (do_gpu_queries && category.usesGPUCounter()) {
Assertion(get_tid() == main_thread_id, "This function must be called from the main thread!");
gpu_trace_event gpu_event;
gpu_event.base_evt.category = &category;
gpu_event.base_evt.tid = 1;
gpu_event.base_evt.pid = GPU_PID;
gpu_event.base_evt.type = EventType::Begin;
gpu_event.gpu_begin_query = get_gpu_timestamp_query();
// This does not need to be synchronized since GPU queries are only allowed on the main thread.
gpu_events.push(gpu_event);
}
}
void end(trace_event* evt) {
if (!do_trace_events) {
// No one to process the event is here
return;
}
if (!initialized) {
return;
}
Assertion(evt->pid == get_pid(), "Complete events must be generated from the same process!");
Assertion(evt->tid == get_tid(), "Complete events must be generated from the same thread!");
evt->duration = timer_get_nanoseconds() - evt->timestamp;
evt->end_event_id = ++current_id;
// Process CPU events
submit_event(evt);
// Create GPU events
if (do_gpu_queries && evt->category->usesGPUCounter()) {
Assertion(get_tid() == main_thread_id, "This function must be called from the main thread!");
gpu_trace_event gpu_event;
gpu_event.base_evt.category = evt->category;
gpu_event.base_evt.tid = 1;
gpu_event.base_evt.pid = GPU_PID;
gpu_event.base_evt.type = EventType::End;
gpu_event.gpu_begin_query = get_gpu_timestamp_query();
// This does not need to be synchronized since GPU queries are only allowed on the main thread.
gpu_events.push(gpu_event);
}
}
}
namespace async {
void begin(const Category& category, const Scope& async_scope) {
if (!do_async_events) {
return;
}
trace_event evt;
init_event(category, &evt);
evt.type = EventType::AsyncBegin;
evt.scope = &async_scope;
evt.event_id = ++current_id;
submit_event(&evt);
}
void step(const Category& category, const Scope& async_scope) {
if (!do_async_events) {
return;
}
trace_event evt;
init_event(category, &evt);
evt.type = EventType::AsyncStep;
evt.scope = &async_scope;
evt.event_id = ++current_id;
submit_event(&evt);
}
void end(const Category& category, const Scope& async_scope) {
if (!do_async_events) {
return;
}
trace_event evt;
init_event(category, &evt);
evt.type = EventType::AsyncEnd;
evt.scope = &async_scope;
evt.event_id = ++current_id;
submit_event(&evt);
}
}
namespace counter {
void value(const Category& category, float value) {
if (!do_counter_events) {
return;
}
trace_event evt;
init_event(category, &evt);
evt.type = EventType::Counter;
evt.value = value;
evt.event_id = ++current_id;
submit_event(&evt);
}
}
}
|