1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
31 #include "os_binary.hpp"
32 #include "os_time.hpp"
33 #include "os_workqueue.hpp"
35 #include "trace_callset.hpp"
36 #include "trace_dump.hpp"
37 #include "retrace.hpp"
40 static bool waitOnFinish = false;
41 static bool use_threads;
43 static const char *comparePrefix = NULL;
44 static const char *snapshotPrefix = NULL;
45 static trace::CallSet snapshotFrequency;
46 static trace::CallSet compareFrequency;
48 static unsigned dumpStateCallNo = ~0;
50 retrace::Retracer retracer;
57 trace::Profiler profiler;
59 static std::map<unsigned long, os::WorkQueue *> thread_wq_map;
63 bool dumpingState = false;
65 bool doubleBuffer = true;
66 bool coreProfile = false;
68 bool profiling = false;
69 bool profilingGpuTimes = false;
70 bool profilingCpuTimes = false;
71 bool profilingPixelsDrawn = false;
75 static bool state_dumped;
77 class RenderWork : public os::WorkQueueWork
82 RenderWork(trace::Call *_call) { call = _call; }
83 ~RenderWork(void) { delete call; }
86 class FlushGLWork : public os::WorkQueueWork
89 void run(void) { flushRendering(); }
93 frameComplete(trace::Call &call) {
99 takeSnapshot(unsigned call_no) {
100 assert(snapshotPrefix || comparePrefix);
102 image::Image *ref = NULL;
105 os::String filename = os::String::format("%s%010u.png", comparePrefix, call_no);
106 ref = image::readPNG(filename);
110 if (retrace::verbosity >= 0) {
111 std::cout << "Read " << filename << "\n";
115 image::Image *src = getSnapshot();
120 if (snapshotPrefix) {
121 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
123 snprintf(comment, sizeof comment, "%u", call_no);
124 src->writePNM(std::cout, comment);
126 os::String filename = os::String::format("%s%010u.png", snapshotPrefix, call_no);
127 if (src->writePNG(filename) && retrace::verbosity >= 0) {
128 std::cout << "Wrote " << filename << "\n";
134 std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
143 void RenderWork::run(void)
145 bool swapRenderTarget = call->flags &
146 trace::CALL_FLAG_SWAP_RENDERTARGET;
147 bool doSnapshot = snapshotFrequency.contains(*call) ||
148 compareFrequency.contains(*call);
153 // For calls which cause rendertargets to be swaped, we take the
154 // snapshot _before_ swapping the rendertargets.
155 if (doSnapshot && swapRenderTarget) {
156 if (call->flags & trace::CALL_FLAG_END_FRAME) {
157 // For swapbuffers/presents we still use this
158 // call number, spite not have been executed yet.
159 takeSnapshot(call->no);
161 // Whereas for ordinate fbo/rendertarget changes we
162 // use the previous call's number.
163 takeSnapshot(call->no - 1);
168 retracer.retrace(*call);
170 if (doSnapshot && !swapRenderTarget)
171 takeSnapshot(call->no);
173 if (call->no >= dumpStateCallNo && dumpState(std::cout))
177 static os::WorkQueue *get_work_queue(unsigned long thread_id)
179 os::WorkQueue *thread;
180 std::map<unsigned long, os::WorkQueue *>::iterator it;
182 it = thread_wq_map.find(thread_id);
183 if (it == thread_wq_map.end()) {
184 thread = new os::WorkQueue();
185 thread_wq_map[thread_id] = thread;
193 static void exit_work_queues(void)
195 std::map<unsigned long, os::WorkQueue *>::iterator it;
197 it = thread_wq_map.begin();
198 while (it != thread_wq_map.end()) {
199 os::WorkQueue *thread_wq = it->second;
201 thread_wq->queue_work(new FlushGLWork);
203 thread_wq->destroy();
204 thread_wq_map.erase(it++);
208 static void do_all_calls(void)
211 int prev_thread_id = -1;
212 os::WorkQueue *thread_wq = NULL;
214 while ((call = parser.parse_call())) {
215 RenderWork *render_work = new RenderWork(call);
218 if (prev_thread_id != call->thread_id) {
221 thread_wq = get_work_queue(call->thread_id);
222 prev_thread_id = call->thread_id;
225 thread_wq->queue_work(render_work);
241 addCallbacks(retracer);
243 long long startTime = 0;
246 startTime = os::getTime();
252 * Reached the end of trace; if using threads we do the flush
253 * when exiting the threads.
257 long long endTime = os::getTime();
258 float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
260 if ((retrace::verbosity >= -1) || (retrace::profiling)) {
262 "Rendered " << frameNo << " frames"
263 " in " << timeInterval << " secs,"
264 " average of " << (frameNo/timeInterval) << " fps\n";
275 } /* namespace retrace */
279 usage(const char *argv0) {
281 "Usage: " << argv0 << " [OPTION] TRACE [...]\n"
284 " -b benchmark mode (no error checking or warning messages)\n"
285 " -pcpu cpu profiling (cpu times per call)\n"
286 " -pgpu gpu profiling (gpu times per draw call)\n"
287 " -ppd pixels drawn profiling (pixels drawn per draw call)\n"
288 " -c PREFIX compare against snapshots\n"
289 " -C CALLSET calls to compare (default is every frame)\n"
290 " -core use core profile\n"
291 " -db use a double buffer visual (default)\n"
292 " -sb use a single buffer visual\n"
293 " -s PREFIX take snapshots; `-` for PNM stdout output\n"
294 " -S CALLSET calls to snapshot (default is every frame)\n"
295 " -v increase output verbosity\n"
296 " -D CALLNO dump state at specific call no\n"
297 " -w waitOnFinish on final frame\n"
298 " -t enable threading\n";
303 int main(int argc, char **argv)
305 using namespace retrace;
307 assert(compareFrequency.empty());
308 assert(snapshotFrequency.empty());
311 for (i = 1; i < argc; ++i) {
312 const char *arg = argv[i];
318 if (!strcmp(arg, "--")) {
320 } else if (!strcmp(arg, "-b")) {
321 retrace::debug = false;
322 retrace::verbosity = -1;
323 } else if (!strcmp(arg, "-c")) {
324 comparePrefix = argv[++i];
325 if (compareFrequency.empty()) {
326 compareFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
328 } else if (!strcmp(arg, "-C")) {
329 compareFrequency = trace::CallSet(argv[++i]);
330 if (comparePrefix == NULL) {
333 } else if (!strcmp(arg, "-D")) {
334 dumpStateCallNo = atoi(argv[++i]);
336 retrace::verbosity = -2;
337 } else if (!strcmp(arg, "-core")) {
338 retrace::coreProfile = true;
339 } else if (!strcmp(arg, "-db")) {
340 retrace::doubleBuffer = true;
341 } else if (!strcmp(arg, "-sb")) {
342 retrace::doubleBuffer = false;
343 } else if (!strcmp(arg, "--help")) {
346 } else if (!strcmp(arg, "-s")) {
347 snapshotPrefix = argv[++i];
348 if (snapshotFrequency.empty()) {
349 snapshotFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
351 if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
352 os::setBinaryMode(stdout);
353 retrace::verbosity = -2;
355 } else if (!strcmp(arg, "-S")) {
356 snapshotFrequency = trace::CallSet(argv[++i]);
357 if (snapshotPrefix == NULL) {
360 } else if (!strcmp(arg, "-v")) {
361 ++retrace::verbosity;
362 } else if (!strcmp(arg, "-w")) {
364 } else if (arg[1] == 'p') {
365 retrace::debug = false;
366 retrace::profiling = true;
367 retrace::verbosity = -1;
369 if (!strcmp(arg, "-pcpu")) {
370 retrace::profilingCpuTimes = true;
371 } else if (!strcmp(arg, "-pgpu")) {
372 retrace::profilingGpuTimes = true;
373 } else if (!strcmp(arg, "-ppd")) {
374 retrace::profilingPixelsDrawn = true;
376 } else if (!strcmp(arg, "-t")) {
379 std::cerr << "error: unknown option " << arg << "\n";
386 if (retrace::profiling) {
387 retrace::profiler.setup(retrace::profilingCpuTimes, retrace::profilingGpuTimes, retrace::profilingPixelsDrawn);
390 for ( ; i < argc; ++i) {
391 if (!retrace::parser.open(argv[i])) {
392 std::cerr << "error: failed to open " << argv[i] << "\n";
398 retrace::parser.close();
401 // XXX: X often hangs on XCloseDisplay
402 //retrace::cleanUp();