1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
4 * Copyright (C) 2013 Intel Corporation. All rights reversed.
5 * Author: Shuang He <shuang.he@intel.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 **************************************************************************/
30 #include <limits.h> // for CHAR_MAX
34 #include "os_binary.hpp"
35 #include "os_time.hpp"
36 #include "os_thread.hpp"
38 #include "trace_callset.hpp"
39 #include "trace_dump.hpp"
40 #include "trace_option.hpp"
41 #include "retrace.hpp"
44 static bool waitOnFinish = false;
46 static const char *comparePrefix = NULL;
47 static const char *snapshotPrefix = NULL;
48 static trace::CallSet snapshotFrequency;
49 static trace::CallSet compareFrequency;
51 static unsigned dumpStateCallNo = ~0;
53 retrace::Retracer retracer;
60 trace::Profiler profiler;
65 bool dumpingState = false;
67 Driver driver = DRIVER_DEFAULT;
68 const char *driverModule = NULL;
70 bool doubleBuffer = true;
71 bool coreProfile = false;
73 bool profiling = false;
74 bool profilingGpuTimes = false;
75 bool profilingCpuTimes = false;
76 bool profilingPixelsDrawn = false;
77 bool profilingMemoryUsage = false;
78 bool useCallNos = true;
85 frameComplete(trace::Call &call) {
90 static Dumper defaultDumper;
92 Dumper *dumper = &defaultDumper;
96 * Take/compare snapshots.
99 takeSnapshot(unsigned call_no) {
100 static unsigned snapshot_no = 0;
102 assert(snapshotPrefix || comparePrefix);
104 image::Image *ref = NULL;
107 os::String filename = os::String::format("%s%010u.png", comparePrefix, call_no);
108 ref = image::readPNG(filename);
112 if (retrace::verbosity >= 0) {
113 std::cout << "Read " << filename << "\n";
117 image::Image *src = dumper->getSnapshot();
119 std::cout << "Failed to get snapshot\n";
123 if (snapshotPrefix) {
124 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
126 snprintf(comment, sizeof comment, "%u",
127 useCallNos ? call_no : snapshot_no);
128 src->writePNM(std::cout, comment);
130 os::String filename = os::String::format("%s%010u.png",
132 useCallNos ? call_no : snapshot_no);
133 if (src->writePNG(filename) && retrace::verbosity >= 0) {
134 std::cout << "Wrote " << filename << "\n";
140 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
155 * Take snapshots before/after retracing (as appropriate) and dispatch it to
156 * the respective handler.
159 retraceCall(trace::Call *call) {
160 bool swapRenderTarget = call->flags &
161 trace::CALL_FLAG_SWAP_RENDERTARGET;
162 bool doSnapshot = snapshotFrequency.contains(*call) ||
163 compareFrequency.contains(*call);
165 // For calls which cause rendertargets to be swaped, we take the
166 // snapshot _before_ swapping the rendertargets.
167 if (doSnapshot && swapRenderTarget) {
168 if (call->flags & trace::CALL_FLAG_END_FRAME) {
169 // For swapbuffers/presents we still use this
170 // call number, spite not have been executed yet.
171 takeSnapshot(call->no);
173 // Whereas for ordinate fbo/rendertarget changes we
174 // use the previous call's number.
175 takeSnapshot(call->no - 1);
180 retracer.retrace(*call);
182 if (doSnapshot && !swapRenderTarget)
183 takeSnapshot(call->no);
185 if (call->no >= dumpStateCallNo &&
186 dumper->dumpState(std::cout)) {
196 * Implement multi-threading by mimicking a relay race.
202 * Runners indexed by the leg they run (i.e, the thread_ids from the
205 std::vector<RelayRunner*> runners;
213 getRunner(unsigned leg);
224 passBaton(trace::Call *call);
235 * Each runner is a thread.
237 * The fore runner doesn't have its own thread, but instead uses the thread
238 * where the race started.
243 friend class RelayRace;
250 os::condition_variable wake_cond;
253 * There are protected by the mutex.
261 runnerThread(RelayRunner *_this);
264 RelayRunner(RelayRace *race, unsigned _leg) :
270 /* The fore runner does not need a new thread */
272 thread = os::thread(runnerThread, this);
281 os::unique_lock<os::mutex> lock(mutex);
284 while (!finished && !baton) {
285 wake_cond.wait(lock);
293 trace::Call *call = baton;
299 if (0) std::cerr << "leg " << leg << " actually finishing\n";
307 * Interpret successive calls.
310 runLeg(trace::Call *call) {
311 /* Consume successive calls for this thread. */
314 assert(call->thread_id == leg);
317 call = parser.parse_call();
318 } while (call && call->thread_id == leg);
322 assert(call->thread_id != leg);
324 race->passBaton(call);
326 /* Reached the finish line */
327 if (0) std::cerr << "finished on leg " << leg << "\n";
329 /* Notify the fore runner */
332 /* We are the fore runner */
339 * Called by other threads when relinquishing the baton.
342 receiveBaton(trace::Call *call) {
343 assert (call->thread_id == leg);
353 * Called by the fore runner when the race is over.
357 if (0) std::cerr << "notify finish to leg " << leg << "\n";
369 RelayRunner::runnerThread(RelayRunner *_this) {
375 RelayRace::RelayRace() {
376 runners.push_back(new RelayRunner(this, 0));
380 RelayRace::~RelayRace() {
381 assert(runners.size() >= 1);
382 std::vector<RelayRunner*>::const_iterator it;
383 for (it = runners.begin(); it != runners.end(); ++it) {
384 RelayRunner* runner = *it;
393 * Get (or instantiate) a runner for the specified leg.
396 RelayRace::getRunner(unsigned leg) {
399 if (leg >= runners.size()) {
400 runners.resize(leg + 1);
403 runner = runners[leg];
406 runner = new RelayRunner(this, leg);
407 runners[leg] = runner;
417 RelayRace::run(void) {
419 call = parser.parse_call();
425 RelayRunner *foreRunner = getForeRunner();
426 if (call->thread_id == 0) {
427 /* We are the forerunner thread, so no need to pass baton */
428 foreRunner->baton = call;
433 /* Start the forerunner thread */
434 foreRunner->runRace();
439 * Pass the baton (i.e., the call) to the appropriate thread.
442 RelayRace::passBaton(trace::Call *call) {
443 if (0) std::cerr << "switching to thread " << call->thread_id << "\n";
444 RelayRunner *runner = getRunner(call->thread_id);
445 runner->receiveBaton(call);
450 * Called when a runner other than the forerunner reaches the finish line.
452 * Only the fore runner can finish the race, so inform him that the race is
456 RelayRace::finishLine(void) {
457 RelayRunner *foreRunner = getForeRunner();
458 foreRunner->finishRace();
463 * Called by the fore runner after finish line to stop all other runners.
466 RelayRace::stopRunners(void) {
467 std::vector<RelayRunner*>::const_iterator it;
468 for (it = runners.begin() + 1; it != runners.end(); ++it) {
469 RelayRunner* runner = *it;
471 runner->finishRace();
479 addCallbacks(retracer);
481 long long startTime = 0;
484 startTime = os::getTime();
489 long long endTime = os::getTime();
490 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
492 if ((retrace::verbosity >= -1) || (retrace::profiling)) {
494 "Rendered " << frameNo << " frames"
495 " in " << timeInterval << " secs,"
496 " average of " << (frameNo/timeInterval) << " fps\n";
507 } /* namespace retrace */
511 usage(const char *argv0) {
513 "Usage: " << argv0 << " [OPTION] TRACE [...]\n"
516 " -b, --benchmark benchmark mode (no error checking or warning messages)\n"
517 " --pcpu cpu profiling (cpu times per call)\n"
518 " --pgpu gpu profiling (gpu times per draw call)\n"
519 " --ppd pixels drawn profiling (pixels drawn per draw call)\n"
520 " --pmem memory usage profiling (vsize rss per call)\n"
521 " -c, --compare=PREFIX compare against snapshots with given PREFIX\n"
522 " -C, --calls=CALLSET calls to compare (default is every frame)\n"
523 " --call-nos[=BOOL] use call numbers in snapshot filenames\n"
524 " --core use core profile\n"
525 " --db use a double buffer visual (default)\n"
526 " --driver=DRIVER force driver type (`hw`, `sw`, `ref`, `null`, or driver module name)\n"
527 " --sb use a single buffer visual\n"
528 " -s, --snapshot-prefix=PREFIX take snapshots; `-` for PNM stdout output\n"
529 " -S, --snapshot=CALLSET calls to snapshot (default is every frame)\n"
530 " -v, --verbose increase output verbosity\n"
531 " -D, --dump-state=CALL dump state at specific call no\n"
532 " -w, --wait waitOnFinish on final frame\n";
536 CALL_NOS_OPT = CHAR_MAX + 1,
548 shortOptions = "bc:C:D:hs:S:vw";
550 const static struct option
552 {"benchmark", no_argument, 0, 'b'},
553 {"call-nos", optional_argument, 0, CALL_NOS_OPT },
554 {"calls", required_argument, 0, 'C'},
555 {"compare", required_argument, 0, 'c'},
556 {"core", no_argument, 0, CORE_OPT},
557 {"db", no_argument, 0, DB_OPT},
558 {"driver", required_argument, 0, DRIVER_OPT},
559 {"dump-state", required_argument, 0, 'D'},
560 {"help", no_argument, 0, 'h'},
561 {"pcpu", no_argument, 0, PCPU_OPT},
562 {"pgpu", no_argument, 0, PGPU_OPT},
563 {"ppd", no_argument, 0, PPD_OPT},
564 {"pmem", no_argument, 0, PMEM_OPT},
565 {"sb", no_argument, 0, SB_OPT},
566 {"snapshot-prefix", required_argument, 0, 's'},
567 {"snapshot", required_argument, 0, 'S'},
568 {"verbose", no_argument, 0, 'v'},
569 {"wait", no_argument, 0, 'w'},
574 static void exceptionCallback(void)
576 std::cerr << retrace::callNo << ": error: caught an unhandled exception\n";
581 int main(int argc, char **argv)
583 using namespace retrace;
586 assert(compareFrequency.empty());
587 assert(snapshotFrequency.empty());
590 while ((opt = getopt_long_only(argc, argv, shortOptions, longOptions, NULL)) != -1) {
596 retrace::debug = false;
597 retrace::verbosity = -1;
600 useCallNos = trace::boolOption(optarg);
603 comparePrefix = optarg;
604 if (compareFrequency.empty()) {
605 compareFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
609 compareFrequency = trace::CallSet(optarg);
610 if (comparePrefix == NULL) {
615 dumpStateCallNo = atoi(optarg);
617 retrace::verbosity = -2;
620 retrace::coreProfile = true;
623 retrace::doubleBuffer = true;
626 if (strcasecmp(optarg, "hw") == 0) {
627 driver = DRIVER_HARDWARE;
628 } else if (strcasecmp(optarg, "sw") == 0) {
629 driver = DRIVER_SOFTWARE;
630 } else if (strcasecmp(optarg, "ref") == 0) {
631 driver = DRIVER_REFERENCE;
632 } else if (strcasecmp(optarg, "null") == 0) {
633 driver = DRIVER_NULL;
635 driver = DRIVER_MODULE;
636 driverModule = optarg;
640 retrace::doubleBuffer = false;
643 snapshotPrefix = optarg;
644 if (snapshotFrequency.empty()) {
645 snapshotFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
647 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
648 os::setBinaryMode(stdout);
649 retrace::verbosity = -2;
653 snapshotFrequency = trace::CallSet(optarg);
654 if (snapshotPrefix == NULL) {
659 ++retrace::verbosity;
665 retrace::debug = false;
666 retrace::profiling = true;
667 retrace::verbosity = -1;
669 retrace::profilingGpuTimes = true;
672 retrace::debug = false;
673 retrace::profiling = true;
674 retrace::verbosity = -1;
676 retrace::profilingCpuTimes = true;
679 retrace::debug = false;
680 retrace::profiling = true;
681 retrace::verbosity = -1;
683 retrace::profilingPixelsDrawn = true;
686 retrace::debug = false;
687 retrace::profiling = true;
688 retrace::verbosity = -1;
690 retrace::profilingMemoryUsage = true;
693 std::cerr << "error: unknown option " << opt << "\n";
700 if (retrace::profiling) {
701 retrace::profiler.setup(retrace::profilingCpuTimes, retrace::profilingGpuTimes, retrace::profilingPixelsDrawn, retrace::profilingMemoryUsage);
704 os::setExceptionCallback(exceptionCallback);
706 for (i = optind; i < argc; ++i) {
707 if (!retrace::parser.open(argv[i])) {
713 retrace::parser.close();
716 os::resetExceptionCallback();
718 // XXX: X often hangs on XCloseDisplay
719 //retrace::cleanUp();