X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=retrace%2Fretrace_main.cpp;h=9b029c8b5a10e4c0d6c5c9794257b1f74d6b281a;hb=f74ad0d7b108d0b628e2290a4b6c597e9a13ee8e;hp=f91928beb2ee7244e5fa7c3447748dd41e8ef55e;hpb=06555614b7385c49964d27aaeec68dec6ff31744;p=apitrace diff --git a/retrace/retrace_main.cpp b/retrace/retrace_main.cpp index f91928b..9b029c8 100644 --- a/retrace/retrace_main.cpp +++ b/retrace/retrace_main.cpp @@ -29,13 +29,15 @@ #include "os_binary.hpp" #include "os_time.hpp" +#include "os_workqueue.hpp" #include "image.hpp" #include "trace_callset.hpp" #include "trace_dump.hpp" #include "retrace.hpp" -static bool wait = false; +static bool waitOnFinish = false; +static bool use_threads; static const char *comparePrefix = NULL; static const char *snapshotPrefix = NULL; @@ -44,24 +46,47 @@ static trace::CallSet compareFrequency; static unsigned dumpStateCallNo = ~0; +retrace::Retracer retracer; + namespace retrace { trace::Parser parser; +trace::Profiler profiler; +static std::map thread_wq_map; int verbosity = 0; bool debug = true; -bool profiling = false; - +bool dumpingState = false; bool doubleBuffer = true; bool coreProfile = false; +bool profiling = false; +bool profilingGpuTimes = false; +bool profilingCpuTimes = false; +bool profilingPixelsDrawn = false; -static unsigned frameNo = 0; +unsigned frameNo = 0; +unsigned callNo = 0; +static bool state_dumped; +class RenderWork : public os::WorkQueueWork +{ + trace::Call *call; +public: + void run(void); + RenderWork(trace::Call *_call) { call = _call; } + ~RenderWork(void) { delete call; } +}; + +class FlushGLWork : public os::WorkQueueWork +{ +public: + void run(void) { flushRendering(); } +}; void frameComplete(trace::Call &call) { @@ -114,55 +139,123 @@ takeSnapshot(unsigned call_no) { return; } +void RenderWork::run(void) +{ + bool swapRenderTarget = call->flags & + trace::CALL_FLAG_SWAP_RENDERTARGET; + bool doSnapshot = snapshotFrequency.contains(*call) || + compareFrequency.contains(*call); -static void -mainLoop() { - retrace::Retracer retracer; + if (state_dumped) + return; - addCallbacks(retracer); + // For calls which cause rendertargets to be swaped, we take the + // snapshot _before_ swapping the rendertargets. + if (doSnapshot && swapRenderTarget) { + if (call->flags & trace::CALL_FLAG_END_FRAME) { + // For swapbuffers/presents we still use this + // call number, spite not have been executed yet. + takeSnapshot(call->no); + } else { + // Whereas for ordinate fbo/rendertarget changes we + // use the previous call's number. + takeSnapshot(call->no - 1); + } + } - long long startTime = 0; + callNo = call->no; + retracer.retrace(*call); - startTime = os::getTime(); - trace::Call *call; + if (doSnapshot && !swapRenderTarget) + takeSnapshot(call->no); - while ((call = retrace::parser.parse_call())) { - bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET; - bool doSnapshot = - snapshotFrequency.contains(*call) || - compareFrequency.contains(*call) - ; - - // For calls which cause rendertargets to be swaped, we take the - // snapshot _before_ swapping the rendertargets. - if (doSnapshot && swapRenderTarget) { - if (call->flags & trace::CALL_FLAG_END_FRAME) { - // For swapbuffers/presents we still use this call number, - // spite not have been executed yet. - takeSnapshot(call->no); - } else { - // Whereas for ordinate fbo/rendertarget changes we use the - // previous call's number. - takeSnapshot(call->no - 1); - } - } + if (call->no >= dumpStateCallNo && dumpState(std::cout)) + state_dumped = true; +} - retracer.retrace(*call); +static os::WorkQueue *get_work_queue(unsigned long thread_id) +{ + os::WorkQueue *thread; + std::map::iterator it; - if (doSnapshot && !swapRenderTarget) { - takeSnapshot(call->no); - } + it = thread_wq_map.find(thread_id); + if (it == thread_wq_map.end()) { + thread = new os::WorkQueue(); + thread_wq_map[thread_id] = thread; + } else { + thread = it->second; + } + + return thread; +} + +static void exit_work_queues(void) +{ + std::map::iterator it; + + it = thread_wq_map.begin(); + while (it != thread_wq_map.end()) { + os::WorkQueue *thread_wq = it->second; + + thread_wq->queue_work(new FlushGLWork); + thread_wq->flush(); + thread_wq->destroy(); + thread_wq_map.erase(it++); + } +} + +static void do_all_calls(void) +{ + trace::Call *call; + int prev_thread_id = -1; + os::WorkQueue *thread_wq = NULL; + + while ((call = parser.parse_call())) { + RenderWork *render_work = new RenderWork(call); + + if (use_threads) { + if (prev_thread_id != call->thread_id) { + if (thread_wq) + thread_wq->flush(); + thread_wq = get_work_queue(call->thread_id); + prev_thread_id = call->thread_id; + } - if (call->no >= dumpStateCallNo && - dumpState(std::cout)) { - exit(0); + thread_wq->queue_work(render_work); + + // XXX: Flush immediately to avoid race conditions on unprotected + // static/global variables. + thread_wq->flush(); + } else { + render_work->run(); + delete render_work; } - delete call; + if (state_dumped) + break; } - // Reached the end of trace - flushRendering(); + exit_work_queues(); +} + + +static void +mainLoop() { + addCallbacks(retracer); + + long long startTime = 0; + frameNo = 0; + + startTime = os::getTime(); + + do_all_calls(); + + if (!use_threads) + /* + * Reached the end of trace; if using threads we do the flush + * when exiting the threads. + */ + flushRendering(); long long endTime = os::getTime(); float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency); @@ -174,10 +267,10 @@ mainLoop() { " average of " << (frameNo/timeInterval) << " fps\n"; } - if (wait) { + if (waitOnFinish) { waitForInput(); } else { - exit(0); + return; } } @@ -188,11 +281,13 @@ mainLoop() { static void usage(const char *argv0) { std::cout << - "Usage: " << argv0 << " [OPTION] TRACE\n" + "Usage: " << argv0 << " [OPTION] TRACE [...]\n" "Replay TRACE.\n" "\n" " -b benchmark mode (no error checking or warning messages)\n" - " -p profiling mode (run whole trace, dump profiling info)\n" + " -pcpu cpu profiling (cpu times per call)\n" + " -pgpu gpu profiling (gpu times per draw call)\n" + " -ppd pixels drawn profiling (pixels drawn per draw call)\n" " -c PREFIX compare against snapshots\n" " -C CALLSET calls to compare (default is every frame)\n" " -core use core profile\n" @@ -202,13 +297,16 @@ usage(const char *argv0) { " -S CALLSET calls to snapshot (default is every frame)\n" " -v increase output verbosity\n" " -D CALLNO dump state at specific call no\n" - " -w wait on final frame\n"; + " -w waitOnFinish on final frame\n" + " -t enable threading\n"; } extern "C" int main(int argc, char **argv) { + using namespace retrace; + assert(compareFrequency.empty()); assert(snapshotFrequency.empty()); @@ -225,10 +323,6 @@ int main(int argc, char **argv) } else if (!strcmp(arg, "-b")) { retrace::debug = false; retrace::verbosity = -1; - } else if (!strcmp(arg, "-p")) { - retrace::debug = false; - retrace::profiling = true; - retrace::verbosity = -1; } else if (!strcmp(arg, "-c")) { comparePrefix = argv[++i]; if (compareFrequency.empty()) { @@ -241,6 +335,7 @@ int main(int argc, char **argv) } } else if (!strcmp(arg, "-D")) { dumpStateCallNo = atoi(argv[++i]); + dumpingState = true; retrace::verbosity = -2; } else if (!strcmp(arg, "-core")) { retrace::coreProfile = true; @@ -268,7 +363,21 @@ int main(int argc, char **argv) } else if (!strcmp(arg, "-v")) { ++retrace::verbosity; } else if (!strcmp(arg, "-w")) { - wait = true; + waitOnFinish = true; + } else if (arg[1] == 'p') { + retrace::debug = false; + retrace::profiling = true; + retrace::verbosity = -1; + + if (!strcmp(arg, "-pcpu")) { + retrace::profilingCpuTimes = true; + } else if (!strcmp(arg, "-pgpu")) { + retrace::profilingGpuTimes = true; + } else if (!strcmp(arg, "-ppd")) { + retrace::profilingPixelsDrawn = true; + } + } else if (!strcmp(arg, "-t")) { + use_threads = true; } else { std::cerr << "error: unknown option " << arg << "\n"; usage(argv[0]); @@ -277,6 +386,9 @@ int main(int argc, char **argv) } retrace::setUp(); + if (retrace::profiling) { + retrace::profiler.setup(retrace::profilingCpuTimes, retrace::profilingGpuTimes, retrace::profilingPixelsDrawn); + } for ( ; i < argc; ++i) { if (!retrace::parser.open(argv[i])) { @@ -289,7 +401,8 @@ int main(int argc, char **argv) retrace::parser.close(); } - retrace::cleanUp(); + // XXX: X often hangs on XCloseDisplay + //retrace::cleanUp(); return 0; }