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;
45 static bool loopOnFinish = false;
47 static const char *comparePrefix = NULL;
48 static const char *snapshotPrefix = NULL;
52 } snapshotFormat = PNM_FMT;
54 static trace::CallSet snapshotFrequency;
55 static trace::CallSet compareFrequency;
56 static trace::ParseBookmark lastFrameStart;
58 static unsigned dumpStateCallNo = ~0;
60 retrace::Retracer retracer;
67 trace::Profiler profiler;
72 bool dumpingState = false;
74 Driver driver = DRIVER_DEFAULT;
75 const char *driverModule = NULL;
77 bool doubleBuffer = true;
78 bool coreProfile = false;
80 bool profiling = false;
81 bool profilingGpuTimes = false;
82 bool profilingCpuTimes = false;
83 bool profilingPixelsDrawn = false;
84 bool profilingMemoryUsage = false;
85 bool useCallNos = true;
86 bool singleThread = false;
93 frameComplete(trace::Call &call) {
98 static Dumper defaultDumper;
100 Dumper *dumper = &defaultDumper;
104 * Take/compare snapshots.
107 takeSnapshot(unsigned call_no) {
108 static unsigned snapshot_no = 0;
110 assert(snapshotPrefix || comparePrefix);
112 image::Image *ref = NULL;
115 os::String filename = os::String::format("%s%010u.png", comparePrefix, call_no);
116 ref = image::readPNG(filename);
120 if (retrace::verbosity >= 0) {
121 std::cout << "Read " << filename << "\n";
125 image::Image *src = dumper->getSnapshot();
127 std::cerr << call_no << ": warning: failed to get snapshot\n";
131 if (snapshotPrefix) {
132 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
134 snprintf(comment, sizeof comment, "%u",
135 useCallNos ? call_no : snapshot_no);
136 if (snapshotFormat == RAW_RGB)
137 src->writeRAW(std::cout);
139 src->writePNM(std::cout, comment);
141 os::String filename = os::String::format("%s%010u.png",
143 useCallNos ? call_no : snapshot_no);
144 if (src->writePNG(filename) && retrace::verbosity >= 0) {
145 std::cout << "Wrote " << filename << "\n";
151 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
166 * Take snapshots before/after retracing (as appropriate) and dispatch it to
167 * the respective handler.
170 retraceCall(trace::Call *call) {
171 bool swapRenderTarget = call->flags &
172 trace::CALL_FLAG_SWAP_RENDERTARGET;
173 bool doSnapshot = snapshotFrequency.contains(*call) ||
174 compareFrequency.contains(*call);
176 // For calls which cause rendertargets to be swaped, we take the
177 // snapshot _before_ swapping the rendertargets.
178 if (doSnapshot && swapRenderTarget) {
179 if (call->flags & trace::CALL_FLAG_END_FRAME) {
180 // For swapbuffers/presents we still use this
181 // call number, spite not have been executed yet.
182 takeSnapshot(call->no);
184 // Whereas for ordinate fbo/rendertarget changes we
185 // use the previous call's number.
186 takeSnapshot(call->no - 1);
191 retracer.retrace(*call);
193 if (doSnapshot && !swapRenderTarget)
194 takeSnapshot(call->no);
196 if (call->no >= dumpStateCallNo &&
197 dumper->dumpState(std::cout)) {
207 * Implement multi-threading by mimicking a relay race.
213 * Runners indexed by the leg they run (i.e, the thread_ids from the
216 std::vector<RelayRunner*> runners;
224 getRunner(unsigned leg);
235 passBaton(trace::Call *call);
246 * Each runner is a thread.
248 * The fore runner doesn't have its own thread, but instead uses the thread
249 * where the race started.
254 friend class RelayRace;
261 os::condition_variable wake_cond;
264 * There are protected by the mutex.
272 runnerThread(RelayRunner *_this);
275 RelayRunner(RelayRace *race, unsigned _leg) :
281 /* The fore runner does not need a new thread */
283 thread = os::thread(runnerThread, this);
288 if (thread.joinable()) {
298 os::unique_lock<os::mutex> lock(mutex);
301 while (!finished && !baton) {
302 wake_cond.wait(lock);
310 trace::Call *call = baton;
316 if (0) std::cerr << "leg " << leg << " actually finishing\n";
324 * Interpret successive calls.
327 runLeg(trace::Call *call) {
329 /* Consume successive calls for this thread. */
331 bool callEndsFrame = false;
332 static trace::ParseBookmark frameStart;
335 assert(call->thread_id == leg);
337 if (loopOnFinish && call->flags & trace::CALL_FLAG_END_FRAME) {
338 callEndsFrame = true;
339 parser.getBookmark(frameStart);
344 call = parser.parse_call();
346 /* Restart last frame if looping is requested. */
349 parser.setBookmark(lastFrameStart);
350 call = parser.parse_call();
351 } else if (callEndsFrame) {
352 lastFrameStart = frameStart;
356 } while (call && call->thread_id == leg);
360 assert(call->thread_id != leg);
362 race->passBaton(call);
364 /* Reached the finish line */
365 if (0) std::cerr << "finished on leg " << leg << "\n";
367 /* Notify the fore runner */
370 /* We are the fore runner */
377 * Called by other threads when relinquishing the baton.
380 receiveBaton(trace::Call *call) {
381 assert (call->thread_id == leg);
391 * Called by the fore runner when the race is over.
395 if (0) std::cerr << "notify finish to leg " << leg << "\n";
407 RelayRunner::runnerThread(RelayRunner *_this) {
413 RelayRace::RelayRace() {
414 runners.push_back(new RelayRunner(this, 0));
418 RelayRace::~RelayRace() {
419 assert(runners.size() >= 1);
420 std::vector<RelayRunner*>::const_iterator it;
421 for (it = runners.begin(); it != runners.end(); ++it) {
422 RelayRunner* runner = *it;
431 * Get (or instantiate) a runner for the specified leg.
434 RelayRace::getRunner(unsigned leg) {
437 if (leg >= runners.size()) {
438 runners.resize(leg + 1);
441 runner = runners[leg];
444 runner = new RelayRunner(this, leg);
445 runners[leg] = runner;
455 RelayRace::run(void) {
457 call = parser.parse_call();
463 /* If the user wants to loop we need to get a bookmark target. We
464 * usually get this after replaying a call that ends a frame, but
465 * for a trace that has only one frame we need to get it at the
468 parser.getBookmark(lastFrameStart);
471 RelayRunner *foreRunner = getForeRunner();
472 if (call->thread_id == 0) {
473 /* We are the forerunner thread, so no need to pass baton */
474 foreRunner->baton = call;
479 /* Start the forerunner thread */
480 foreRunner->runRace();
485 * Pass the baton (i.e., the call) to the appropriate thread.
488 RelayRace::passBaton(trace::Call *call) {
489 if (0) std::cerr << "switching to thread " << call->thread_id << "\n";
490 RelayRunner *runner = getRunner(call->thread_id);
491 runner->receiveBaton(call);
496 * Called when a runner other than the forerunner reaches the finish line.
498 * Only the fore runner can finish the race, so inform him that the race is
502 RelayRace::finishLine(void) {
503 RelayRunner *foreRunner = getForeRunner();
504 foreRunner->finishRace();
509 * Called by the fore runner after finish line to stop all other runners.
512 RelayRace::stopRunners(void) {
513 std::vector<RelayRunner*>::const_iterator it;
514 for (it = runners.begin() + 1; it != runners.end(); ++it) {
515 RelayRunner* runner = *it;
517 runner->finishRace();
525 addCallbacks(retracer);
527 long long startTime = 0;
530 startTime = os::getTime();
534 while ((call = parser.parse_call())) {
544 long long endTime = os::getTime();
545 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
547 if ((retrace::verbosity >= -1) || (retrace::profiling)) {
549 "Rendered " << frameNo << " frames"
550 " in " << timeInterval << " secs,"
551 " average of " << (frameNo/timeInterval) << " fps\n";
562 } /* namespace retrace */
566 usage(const char *argv0) {
568 "Usage: " << argv0 << " [OPTION] TRACE [...]\n"
571 " -b, --benchmark benchmark mode (no error checking or warning messages)\n"
572 " --pcpu cpu profiling (cpu times per call)\n"
573 " --pgpu gpu profiling (gpu times per draw call)\n"
574 " --ppd pixels drawn profiling (pixels drawn per draw call)\n"
575 " --pmem memory usage profiling (vsize rss per call)\n"
576 " -c, --compare=PREFIX compare against snapshots with given PREFIX\n"
577 " -C, --calls=CALLSET calls to compare (default is every frame)\n"
578 " --call-nos[=BOOL] use call numbers in snapshot filenames\n"
579 " --core use core profile\n"
580 " --db use a double buffer visual (default)\n"
581 " --driver=DRIVER force driver type (`hw`, `sw`, `ref`, `null`, or driver module name)\n"
582 " --sb use a single buffer visual\n"
583 " -s, --snapshot-prefix=PREFIX take snapshots; `-` for PNM stdout output\n"
584 " --snapshot-format=FMT use (PNM or RGB; default is PNM) when writing to stdout output\n"
585 " -S, --snapshot=CALLSET calls to snapshot (default is every frame)\n"
586 " -v, --verbose increase output verbosity\n"
587 " -D, --dump-state=CALL dump state at specific call no\n"
588 " -w, --wait waitOnFinish on final frame\n"
589 " --loop continuously loop, replaying final frame.\n"
590 " --singlethread use a single thread to replay command stream\n";
594 CALL_NOS_OPT = CHAR_MAX + 1,
609 shortOptions = "bc:C:D:hs:S:vw";
611 const static struct option
613 {"benchmark", no_argument, 0, 'b'},
614 {"call-nos", optional_argument, 0, CALL_NOS_OPT },
615 {"calls", required_argument, 0, 'C'},
616 {"compare", required_argument, 0, 'c'},
617 {"core", no_argument, 0, CORE_OPT},
618 {"db", no_argument, 0, DB_OPT},
619 {"driver", required_argument, 0, DRIVER_OPT},
620 {"dump-state", required_argument, 0, 'D'},
621 {"help", no_argument, 0, 'h'},
622 {"pcpu", no_argument, 0, PCPU_OPT},
623 {"pgpu", no_argument, 0, PGPU_OPT},
624 {"ppd", no_argument, 0, PPD_OPT},
625 {"pmem", no_argument, 0, PMEM_OPT},
626 {"sb", no_argument, 0, SB_OPT},
627 {"snapshot-prefix", required_argument, 0, 's'},
628 {"snapshot-format", required_argument, 0, SNAPSHOT_FORMAT_OPT},
629 {"snapshot", required_argument, 0, 'S'},
630 {"verbose", no_argument, 0, 'v'},
631 {"wait", no_argument, 0, 'w'},
632 {"loop", no_argument, 0, LOOP_OPT},
633 {"singlethread", no_argument, 0, SINGLETHREAD_OPT},
638 static void exceptionCallback(void)
640 std::cerr << retrace::callNo << ": error: caught an unhandled exception\n";
645 int main(int argc, char **argv)
647 using namespace retrace;
650 assert(compareFrequency.empty());
651 assert(snapshotFrequency.empty());
654 while ((opt = getopt_long_only(argc, argv, shortOptions, longOptions, NULL)) != -1) {
660 retrace::debug = false;
661 retrace::verbosity = -1;
664 useCallNos = trace::boolOption(optarg);
667 comparePrefix = optarg;
668 if (compareFrequency.empty()) {
669 compareFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
673 compareFrequency = trace::CallSet(optarg);
674 if (comparePrefix == NULL) {
679 dumpStateCallNo = atoi(optarg);
681 retrace::verbosity = -2;
684 retrace::coreProfile = true;
687 retrace::doubleBuffer = true;
690 if (strcasecmp(optarg, "hw") == 0) {
691 driver = DRIVER_HARDWARE;
692 } else if (strcasecmp(optarg, "sw") == 0) {
693 driver = DRIVER_SOFTWARE;
694 } else if (strcasecmp(optarg, "ref") == 0) {
695 driver = DRIVER_REFERENCE;
696 } else if (strcasecmp(optarg, "null") == 0) {
697 driver = DRIVER_NULL;
699 driver = DRIVER_MODULE;
700 driverModule = optarg;
704 retrace::doubleBuffer = false;
706 case SINGLETHREAD_OPT:
707 retrace::singleThread = true;
710 snapshotPrefix = optarg;
711 if (snapshotFrequency.empty()) {
712 snapshotFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
714 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
715 os::setBinaryMode(stdout);
716 retrace::verbosity = -2;
719 * Create the snapshot directory if it does not exist.
721 * We can't just use trimFilename() because when applied to
722 * "/foo/boo/" it would merely return "/foo".
724 * XXX: create nested directories.
726 os::String prefix(snapshotPrefix);
727 os::String::iterator sep = prefix.rfindSep(false);
728 if (sep != prefix.end()) {
729 prefix.erase(sep, prefix.end());
730 if (!prefix.exists() && !os::createDirectory(prefix)) {
731 std::cerr << "error: failed to create `" << prefix.str() << "` directory\n";
736 case SNAPSHOT_FORMAT_OPT:
737 if (strcmp(optarg, "RGB") == 0)
738 snapshotFormat = RAW_RGB;
740 snapshotFormat = PNM_FMT;
743 snapshotFrequency = trace::CallSet(optarg);
744 if (snapshotPrefix == NULL) {
749 ++retrace::verbosity;
758 retrace::debug = false;
759 retrace::profiling = true;
760 retrace::verbosity = -1;
762 retrace::profilingGpuTimes = true;
765 retrace::debug = false;
766 retrace::profiling = true;
767 retrace::verbosity = -1;
769 retrace::profilingCpuTimes = true;
772 retrace::debug = false;
773 retrace::profiling = true;
774 retrace::verbosity = -1;
776 retrace::profilingPixelsDrawn = true;
779 retrace::debug = false;
780 retrace::profiling = true;
781 retrace::verbosity = -1;
783 retrace::profilingMemoryUsage = true;
786 std::cerr << "error: unknown option " << opt << "\n";
793 if (retrace::profiling) {
794 retrace::profiler.setup(retrace::profilingCpuTimes, retrace::profilingGpuTimes, retrace::profilingPixelsDrawn, retrace::profilingMemoryUsage);
797 os::setExceptionCallback(exceptionCallback);
799 for (i = optind; i < argc; ++i) {
800 if (!retrace::parser.open(argv[i])) {
806 retrace::parser.close();
809 os::resetExceptionCallback();
811 // XXX: X often hangs on XCloseDisplay
812 //retrace::cleanUp();