]> git.cworth.org Git - apitrace/blobdiff - common/trace_profiler.cpp
CPU Profiling now includes all OpenGL calls (was only draw calls).
[apitrace] / common / trace_profiler.cpp
index 8a3b137bbbe8d94bb32a8dd3b0f943af3b6d1274..403c26099fe4d1a45245b8bd6b1639660d08cf9b 100644 (file)
@@ -27,7 +27,8 @@
 #include <iostream>
 #include <string.h>
 #include <assert.h>
-#include <stdio.h>
+#include <sstream>
+#include "os_time.hpp"
 
 namespace trace {
 Profiler::Profiler()
@@ -54,30 +55,35 @@ void Profiler::setup(bool cpuTimes_, bool gpuTimes_, bool pixelsDrawn_)
     std::cout << "# call no gpu_start gpu_dura cpu_start cpu_dura pixels program name" << std::endl;
 }
 
+void Profiler::setBaseTimes(int64_t gpuStart, int64_t cpuStart)
+{
+    baseCpuTime = cpuStart;
+    baseGpuTime = gpuStart;
+}
+
+bool Profiler::hasBaseTimes()
+{
+    return baseCpuTime != 0 || baseGpuTime != 0;
+}
+
 void Profiler::addCall(unsigned no,
                        const char *name,
                        unsigned program,
-                       uint64_t pixels,
-                       uint64_t gpuStart, uint64_t gpuDuration,
-                       uint64_t cpuStart, uint64_t cpuDuration)
+                       int64_t pixels,
+                       int64_t gpuStart, int64_t gpuDuration,
+                       int64_t cpuStart, int64_t cpuDuration)
 {
-    if (baseGpuTime == 0) {
-        baseGpuTime = gpuStart;
-    }
-
-    if (baseCpuTime == 0) {
-        baseCpuTime = cpuStart;
-    }
-
-    if (gpuTimes) {
+    if (gpuTimes && gpuStart) {
         gpuStart -= baseGpuTime;
     } else {
         gpuStart = 0;
         gpuDuration = 0;
     }
 
-    if (cpuTimes) {
-        cpuStart -= baseCpuTime;
+    if (cpuTimes && cpuStart) {
+        double cpuTimeScale = 1.0E9 / os::timeFrequency;
+        cpuStart = (cpuStart - baseCpuTime) * cpuTimeScale;
+        cpuDuration = cpuDuration * cpuTimeScale;
     } else {
         cpuStart = 0;
         cpuDuration = 0;
@@ -99,52 +105,48 @@ void Profiler::addCall(unsigned no,
               << std::endl;
 }
 
-void Profiler::addFrameStart(unsigned no, uint64_t gpuStart, uint64_t cpuStart)
+void Profiler::addFrameStart(unsigned no, int64_t gpuStart, int64_t cpuStart)
 {
-    if (baseGpuTime == 0) {
-        baseGpuTime = gpuStart;
-    }
-
-    if (baseCpuTime == 0) {
-        baseCpuTime = cpuStart;
-    }
+    lastFrame.no = no;
+    lastFrame.gpuStart = gpuStart;
+    lastFrame.cpuStart = cpuStart;
 
     if (gpuTimes) {
-        lastFrame.gpuStart = gpuStart - baseGpuTime;
+        gpuStart = gpuStart - baseGpuTime;
     } else {
-        lastFrame.gpuStart = 0;
+        gpuStart = 0;
     }
 
     if (cpuTimes) {
-        lastFrame.cpuStart = cpuStart - baseCpuTime;
+        double cpuTimeScale = 1.0E9 / os::timeFrequency;
+        cpuStart = (cpuStart - baseCpuTime) * cpuTimeScale;
     } else {
-        lastFrame.cpuStart = 0;
+        cpuStart = 0;
     }
 
-    lastFrame.no = no;
-
     std::cout << "frame_begin"
-              << " " << lastFrame.no
-              << " " << lastFrame.gpuStart
-              << " " << lastFrame.cpuStart
+              << " " << no
+              << " " << gpuStart
+              << " " << cpuStart
               << std::endl;
 }
 
-void Profiler::addFrameEnd(uint64_t gpuEnd, uint64_t cpuEnd)
+void Profiler::addFrameEnd(int64_t gpuEnd, int64_t cpuEnd)
 {
-    uint64_t gpuDuration, cpuDuration;
+    int64_t gpuDuration, cpuDuration;
 
     if (gpuTimes) {
-        gpuEnd -= baseGpuTime;
         gpuDuration = gpuEnd - lastFrame.gpuStart;
+        gpuEnd = gpuEnd - baseGpuTime;
     } else {
         gpuEnd = 0;
         gpuDuration = 0;
     }
 
     if (cpuTimes) {
-        cpuEnd -= baseCpuTime;
-        cpuDuration = cpuEnd - lastFrame.cpuStart;
+        double cpuTimeScale = 1.0E9 / os::timeFrequency;
+        cpuDuration = (cpuEnd - lastFrame.cpuStart) * cpuTimeScale;
+        cpuEnd = (cpuEnd - baseCpuTime) * cpuTimeScale;
     } else {
         cpuEnd = 0;
         cpuDuration = 0;
@@ -159,33 +161,52 @@ void Profiler::addFrameEnd(uint64_t gpuEnd, uint64_t cpuEnd)
               << std::endl;
 }
 
-void Profiler::parseLine(const char* line, Profile* profile)
+void Profiler::parseLine(const char* in, Profile* profile)
 {
-    char name[64];
+    std::stringstream line(in, std::ios_base::in);
+    std::string type;
 
-    if (line[0] == '#' || strlen(line) < 12)
+    if (in[0] == '#' || strlen(in) < 12)
         return;
 
-    if (strncmp(line, "call ", 5) == 0) {
-        assert(profile->frames.size());
+    line >> type;
 
+    if (type.compare("call") == 0) {
+        assert(profile->frames.size());
         Profile::Call call;
-        sscanf(line, "call %u %li %li %li %li %li %u %s", &call.no, &call.gpuStart, &call.gpuDuration, &call.cpuStart, &call.cpuDuration, &call.pixels, &call.program, name);
-        call.name = name;
-        profile->frames.back().calls.push_back(call);
-    } else if (strncmp(line, "frame_begin ", 12) == 0) {
+
+        line >> call.no
+             >> call.gpuStart
+             >> call.gpuDuration
+             >> call.cpuStart
+             >> call.cpuDuration
+             >> call.pixels
+             >> call.program
+             >> call.name;
+
+        if (call.pixels >= 0) {
+            profile->frames.back().calls.push_back(call);
+        }
+    } else if (type.compare("frame_begin") == 0) {
         Profile::Frame frame;
         frame.gpuDuration = 0;
-        frame.gpuDuration = 0;
-        sscanf(line, "frame_begin %u %li %li", &frame.no, &frame.gpuStart, &frame.cpuStart);
+        frame.cpuDuration = 0;
+
+        line >> frame.no
+             >> frame.gpuStart
+             >> frame.cpuStart;
+
         profile->frames.push_back(frame);
-    } else if (strncmp(line, "frame_end ", 10) == 0) {
+    } else if (type.compare("frame_end") == 0) {
         assert(profile->frames.size());
-
         Profile::Frame& frame = profile->frames.back();
-        unsigned no;
-        sscanf(line, "frame_end %u %*li %li %*li %li", &no, &frame.gpuDuration, &frame.cpuDuration);
-        assert(no == frame.no);
+        int64_t skipi64;
+
+        line >> frame.no
+             >> skipi64
+             >> frame.gpuDuration
+             >> skipi64
+             >> frame.cpuDuration;
     }
 }
 }