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;
79 bool singleThread = false;
86 frameComplete(trace::Call &call) {
91 static Dumper defaultDumper;
93 Dumper *dumper = &defaultDumper;
97 * Take/compare snapshots.
100 takeSnapshot(unsigned call_no) {
101 static unsigned snapshot_no = 0;
103 assert(snapshotPrefix || comparePrefix);
105 image::Image *ref = NULL;
108 os::String filename = os::String::format("%s%010u.png", comparePrefix, call_no);
109 ref = image::readPNG(filename);
113 if (retrace::verbosity >= 0) {
114 std::cout << "Read " << filename << "\n";
118 image::Image *src = dumper->getSnapshot();
120 std::cout << "Failed to get snapshot\n";
124 if (snapshotPrefix) {
125 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
127 snprintf(comment, sizeof comment, "%u",
128 useCallNos ? call_no : snapshot_no);
129 src->writePNM(std::cout, comment);
131 os::String filename = os::String::format("%s%010u.png",
133 useCallNos ? call_no : snapshot_no);
134 if (src->writePNG(filename) && retrace::verbosity >= 0) {
135 std::cout << "Wrote " << filename << "\n";
141 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
156 * Take snapshots before/after retracing (as appropriate) and dispatch it to
157 * the respective handler.
160 retraceCall(trace::Call *call) {
161 bool swapRenderTarget = call->flags &
162 trace::CALL_FLAG_SWAP_RENDERTARGET;
163 bool doSnapshot = snapshotFrequency.contains(*call) ||
164 compareFrequency.contains(*call);
166 // For calls which cause rendertargets to be swaped, we take the
167 // snapshot _before_ swapping the rendertargets.
168 if (doSnapshot && swapRenderTarget) {
169 if (call->flags & trace::CALL_FLAG_END_FRAME) {
170 // For swapbuffers/presents we still use this
171 // call number, spite not have been executed yet.
172 takeSnapshot(call->no);
174 // Whereas for ordinate fbo/rendertarget changes we
175 // use the previous call's number.
176 takeSnapshot(call->no - 1);
181 retracer.retrace(*call);
183 if (doSnapshot && !swapRenderTarget)
184 takeSnapshot(call->no);
186 if (call->no >= dumpStateCallNo &&
187 dumper->dumpState(std::cout)) {
197 * Implement multi-threading by mimicking a relay race.
203 * Runners indexed by the leg they run (i.e, the thread_ids from the
206 std::vector<RelayRunner*> runners;
214 getRunner(unsigned leg);
225 passBaton(trace::Call *call);
236 * Each runner is a thread.
238 * The fore runner doesn't have its own thread, but instead uses the thread
239 * where the race started.
244 friend class RelayRace;
251 os::condition_variable wake_cond;
254 * There are protected by the mutex.
262 runnerThread(RelayRunner *_this);
265 RelayRunner(RelayRace *race, unsigned _leg) :
271 /* The fore runner does not need a new thread */
273 thread = os::thread(runnerThread, this);
282 os::unique_lock<os::mutex> lock(mutex);
285 while (!finished && !baton) {
286 wake_cond.wait(lock);
294 trace::Call *call = baton;
300 if (0) std::cerr << "leg " << leg << " actually finishing\n";
308 * Interpret successive calls.
311 runLeg(trace::Call *call) {
312 /* Consume successive calls for this thread. */
315 assert(call->thread_id == leg);
318 call = parser.parse_call();
319 } while (call && call->thread_id == leg);
323 assert(call->thread_id != leg);
325 race->passBaton(call);
327 /* Reached the finish line */
328 if (0) std::cerr << "finished on leg " << leg << "\n";
330 /* Notify the fore runner */
333 /* We are the fore runner */
340 * Called by other threads when relinquishing the baton.
343 receiveBaton(trace::Call *call) {
344 assert (call->thread_id == leg);
354 * Called by the fore runner when the race is over.
358 if (0) std::cerr << "notify finish to leg " << leg << "\n";
370 RelayRunner::runnerThread(RelayRunner *_this) {
376 RelayRace::RelayRace() {
377 runners.push_back(new RelayRunner(this, 0));
381 RelayRace::~RelayRace() {
382 assert(runners.size() >= 1);
383 std::vector<RelayRunner*>::const_iterator it;
384 for (it = runners.begin(); it != runners.end(); ++it) {
385 RelayRunner* runner = *it;
394 * Get (or instantiate) a runner for the specified leg.
397 RelayRace::getRunner(unsigned leg) {
400 if (leg >= runners.size()) {
401 runners.resize(leg + 1);
404 runner = runners[leg];
407 runner = new RelayRunner(this, leg);
408 runners[leg] = runner;
418 RelayRace::run(void) {
420 call = parser.parse_call();
426 RelayRunner *foreRunner = getForeRunner();
427 if (call->thread_id == 0) {
428 /* We are the forerunner thread, so no need to pass baton */
429 foreRunner->baton = call;
434 /* Start the forerunner thread */
435 foreRunner->runRace();
440 * Pass the baton (i.e., the call) to the appropriate thread.
443 RelayRace::passBaton(trace::Call *call) {
444 if (0) std::cerr << "switching to thread " << call->thread_id << "\n";
445 RelayRunner *runner = getRunner(call->thread_id);
446 runner->receiveBaton(call);
451 * Called when a runner other than the forerunner reaches the finish line.
453 * Only the fore runner can finish the race, so inform him that the race is
457 RelayRace::finishLine(void) {
458 RelayRunner *foreRunner = getForeRunner();
459 foreRunner->finishRace();
464 * Called by the fore runner after finish line to stop all other runners.
467 RelayRace::stopRunners(void) {
468 std::vector<RelayRunner*>::const_iterator it;
469 for (it = runners.begin() + 1; it != runners.end(); ++it) {
470 RelayRunner* runner = *it;
472 runner->finishRace();
480 addCallbacks(retracer);
482 long long startTime = 0;
485 startTime = os::getTime();
489 while ((call = parser.parse_call())) {
499 long long endTime = os::getTime();
500 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
502 if ((retrace::verbosity >= -1) || (retrace::profiling)) {
504 "Rendered " << frameNo << " frames"
505 " in " << timeInterval << " secs,"
506 " average of " << (frameNo/timeInterval) << " fps\n";
517 } /* namespace retrace */
521 usage(const char *argv0) {
523 "Usage: " << argv0 << " [OPTION] TRACE [...]\n"
526 " -b, --benchmark benchmark mode (no error checking or warning messages)\n"
527 " --pcpu cpu profiling (cpu times per call)\n"
528 " --pgpu gpu profiling (gpu times per draw call)\n"
529 " --ppd pixels drawn profiling (pixels drawn per draw call)\n"
530 " --pmem memory usage profiling (vsize rss per call)\n"
531 " -c, --compare=PREFIX compare against snapshots with given PREFIX\n"
532 " -C, --calls=CALLSET calls to compare (default is every frame)\n"
533 " --call-nos[=BOOL] use call numbers in snapshot filenames\n"
534 " --core use core profile\n"
535 " --db use a double buffer visual (default)\n"
536 " --driver=DRIVER force driver type (`hw`, `sw`, `ref`, `null`, or driver module name)\n"
537 " --sb use a single buffer visual\n"
538 " -s, --snapshot-prefix=PREFIX take snapshots; `-` for PNM stdout output\n"
539 " -S, --snapshot=CALLSET calls to snapshot (default is every frame)\n"
540 " -v, --verbose increase output verbosity\n"
541 " -D, --dump-state=CALL dump state at specific call no\n"
542 " -w, --wait waitOnFinish on final frame\n"
543 " --singlethread use a single thread to replay command stream\n";
547 CALL_NOS_OPT = CHAR_MAX + 1,
560 shortOptions = "bc:C:D:hs:S:vw";
562 const static struct option
564 {"benchmark", no_argument, 0, 'b'},
565 {"call-nos", optional_argument, 0, CALL_NOS_OPT },
566 {"calls", required_argument, 0, 'C'},
567 {"compare", required_argument, 0, 'c'},
568 {"core", no_argument, 0, CORE_OPT},
569 {"db", no_argument, 0, DB_OPT},
570 {"driver", required_argument, 0, DRIVER_OPT},
571 {"dump-state", required_argument, 0, 'D'},
572 {"help", no_argument, 0, 'h'},
573 {"pcpu", no_argument, 0, PCPU_OPT},
574 {"pgpu", no_argument, 0, PGPU_OPT},
575 {"ppd", no_argument, 0, PPD_OPT},
576 {"pmem", no_argument, 0, PMEM_OPT},
577 {"sb", no_argument, 0, SB_OPT},
578 {"snapshot-prefix", required_argument, 0, 's'},
579 {"snapshot", required_argument, 0, 'S'},
580 {"verbose", no_argument, 0, 'v'},
581 {"wait", no_argument, 0, 'w'},
582 {"singlethread", no_argument, 0, SINGLETHREAD_OPT},
587 static void exceptionCallback(void)
589 std::cerr << retrace::callNo << ": error: caught an unhandled exception\n";
594 int main(int argc, char **argv)
596 using namespace retrace;
599 assert(compareFrequency.empty());
600 assert(snapshotFrequency.empty());
603 while ((opt = getopt_long_only(argc, argv, shortOptions, longOptions, NULL)) != -1) {
609 retrace::debug = false;
610 retrace::verbosity = -1;
613 useCallNos = trace::boolOption(optarg);
616 comparePrefix = optarg;
617 if (compareFrequency.empty()) {
618 compareFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
622 compareFrequency = trace::CallSet(optarg);
623 if (comparePrefix == NULL) {
628 dumpStateCallNo = atoi(optarg);
630 retrace::verbosity = -2;
633 retrace::coreProfile = true;
636 retrace::doubleBuffer = true;
639 if (strcasecmp(optarg, "hw") == 0) {
640 driver = DRIVER_HARDWARE;
641 } else if (strcasecmp(optarg, "sw") == 0) {
642 driver = DRIVER_SOFTWARE;
643 } else if (strcasecmp(optarg, "ref") == 0) {
644 driver = DRIVER_REFERENCE;
645 } else if (strcasecmp(optarg, "null") == 0) {
646 driver = DRIVER_NULL;
648 driver = DRIVER_MODULE;
649 driverModule = optarg;
653 retrace::doubleBuffer = false;
655 case SINGLETHREAD_OPT:
656 retrace::singleThread = true;
659 snapshotPrefix = optarg;
660 if (snapshotFrequency.empty()) {
661 snapshotFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
663 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
664 os::setBinaryMode(stdout);
665 retrace::verbosity = -2;
669 snapshotFrequency = trace::CallSet(optarg);
670 if (snapshotPrefix == NULL) {
675 ++retrace::verbosity;
681 retrace::debug = false;
682 retrace::profiling = true;
683 retrace::verbosity = -1;
685 retrace::profilingGpuTimes = true;
688 retrace::debug = false;
689 retrace::profiling = true;
690 retrace::verbosity = -1;
692 retrace::profilingCpuTimes = true;
695 retrace::debug = false;
696 retrace::profiling = true;
697 retrace::verbosity = -1;
699 retrace::profilingPixelsDrawn = true;
702 retrace::debug = false;
703 retrace::profiling = true;
704 retrace::verbosity = -1;
706 retrace::profilingMemoryUsage = true;
709 std::cerr << "error: unknown option " << opt << "\n";
716 if (retrace::profiling) {
717 retrace::profiler.setup(retrace::profilingCpuTimes, retrace::profilingGpuTimes, retrace::profilingPixelsDrawn, retrace::profilingMemoryUsage);
720 os::setExceptionCallback(exceptionCallback);
722 for (i = optind; i < argc; ++i) {
723 if (!retrace::parser.open(argv[i])) {
729 retrace::parser.close();
732 os::resetExceptionCallback();
734 // XXX: X often hangs on XCloseDisplay
735 //retrace::cleanUp();