]> git.cworth.org Git - apitrace/blobdiff - common/trace_profiler.cpp
Rescale/rebase the times just before writing it.
[apitrace] / common / trace_profiler.cpp
index 6fc00395cd1d3de808d8ae999f05f9540f10a86a..38ad6dd93534671ac4c6b7fe78d46f2b5227d317 100644 (file)
 
 #include "trace_profiler.hpp"
 #include <iostream>
+#include <string.h>
+#include <assert.h>
+#include <sstream>
+#include "os_time.hpp"
 
 namespace trace {
 Profiler::Profiler()
-    : lastProgram(0),
-      baseGpuTime(0),
+    : baseGpuTime(0),
       baseCpuTime(0),
       cpuTimes(false),
       gpuTimes(true),
@@ -47,10 +50,9 @@ void Profiler::setup(bool cpuTimes_, bool gpuTimes_, bool pixelsDrawn_)
     gpuTimes = gpuTimes_;
     pixelsDrawn = pixelsDrawn_;
 
-    std::cout << "# frame begin <no> <gpu_start> <cpu_start>" << std::endl;
-    std::cout << "# frame end <no> <gpu_end> <gpu_dura> <cpu_end> <cpu_dura>" << std::endl;
-    std::cout << "# call <no> <gpu_start> <gpu_dura> <cpu_start> <cpu_dura> <pixels> <function>" << std::endl;
-    std::cout << "# use shader program <no>" << std::endl;
+    std::cout << "# frame_begin no gpu_start cpu_start" << std::endl;
+    std::cout << "# frame_end no gpu_end gpu_dura cpu_end cpu_dura" << std::endl;
+    std::cout << "# call no gpu_start gpu_dura cpu_start cpu_dura pixels program name" << std::endl;
 }
 
 void Profiler::addCall(unsigned no,
@@ -60,96 +62,154 @@ void Profiler::addCall(unsigned no,
                        uint64_t gpuStart, uint64_t gpuDuration,
                        uint64_t cpuStart, uint64_t cpuDuration)
 {
-    if (baseGpuTime == 0)
+    if (baseGpuTime == 0) {
         baseGpuTime = gpuStart;
+    }
 
-    if (baseCpuTime == 0)
+    if (baseCpuTime == 0) {
         baseCpuTime = cpuStart;
-
-    if (program != lastProgram) {
-        std::cout << "use shader program " << program << std::endl;
-        lastProgram = program;
     }
 
-    std::cout << "call " << no;
-
     if (gpuTimes) {
-        std::cout << " "
-                  << (gpuStart - baseGpuTime) << " "
-                  << gpuDuration;
+        gpuStart -= baseGpuTime;
     } else {
-        std::cout << " _ _";
+        gpuStart = 0;
+        gpuDuration = 0;
     }
 
     if (cpuTimes) {
-        std::cout << " "
-                  << (cpuStart - baseCpuTime) << " "
-                  << cpuDuration;
+        double cpuTimeScale = 1.0E9 / os::timeFrequency;
+        cpuStart = (cpuStart - baseCpuTime) * cpuTimeScale;
+        cpuDuration = cpuDuration * cpuTimeScale;
     } else {
-        std::cout << " _ _";
+        cpuStart = 0;
+        cpuDuration = 0;
     }
 
-    if (pixelsDrawn) {
-        std::cout << " " << pixels;
-    } else {
-        std::cout << " _";
+    if (!pixelsDrawn) {
+        pixels = 0;
     }
 
-    std::cout << " " << name << std::endl;
+    std::cout << "call"
+              << " " << no
+              << " " << gpuStart
+              << " " << gpuDuration
+              << " " << cpuStart
+              << " " << cpuDuration
+              << " " << pixels
+              << " " << program
+              << " " << name
+              << std::endl;
 }
 
 void Profiler::addFrameStart(unsigned no, uint64_t gpuStart, uint64_t cpuStart)
 {
-    if (baseGpuTime == 0)
+    if (baseGpuTime == 0) {
         baseGpuTime = gpuStart;
+    }
 
-    if (baseCpuTime == 0)
+    if (baseCpuTime == 0) {
         baseCpuTime = cpuStart;
-
+    }
+        
     lastFrame.no = no;
-    lastFrame.gpuStart = gpuStart - baseGpuTime;
-
-    std::cout << "frame begin " << lastFrame.no;
+    lastFrame.gpuStart = gpuStart;
+    lastFrame.cpuStart = cpuStart;
 
     if (gpuTimes) {
-        std::cout << " " << lastFrame.gpuStart;
+        gpuStart = gpuStart - baseGpuTime;
     } else {
-        std::cout << " _";
+        gpuStart = 0;
     }
 
-    if (gpuTimes) {
-        std::cout << " " << lastFrame.cpuStart;
+    if (cpuTimes) {
+        double cpuTimeScale = 1.0E9 / os::timeFrequency;
+        cpuStart = (cpuStart - baseCpuTime) * cpuTimeScale;
     } else {
-        std::cout << " _";
+        cpuStart = 0;
     }
 
-    std::cout << std::endl;
+    std::cout << "frame_begin"
+              << " " << no
+              << " " << gpuStart
+              << " " << cpuStart
+              << std::endl;
 }
 
 void Profiler::addFrameEnd(uint64_t gpuEnd, uint64_t cpuEnd)
 {
-    if (baseGpuTime == 0)
-        baseGpuTime = gpuEnd;
-
-    if (baseCpuTime == 0)
-        baseCpuTime = cpuEnd;
-
-    lastFrame.gpuEnd = gpuEnd - baseGpuTime;
-
-    std::cout << "frame end " << lastFrame.no;
+    uint64_t gpuDuration, cpuDuration;
 
     if (gpuTimes) {
-        std::cout << " " << lastFrame.gpuEnd << " " << (lastFrame.gpuEnd - lastFrame.gpuStart);
+        gpuDuration = gpuEnd - lastFrame.gpuStart;
+        gpuEnd = gpuEnd - baseGpuTime;
     } else {
-        std::cout << " _ _";
+        gpuEnd = 0;
+        gpuDuration = 0;
     }
 
     if (cpuTimes) {
-        std::cout << " " << lastFrame.cpuEnd << " " << (lastFrame.cpuEnd - lastFrame.cpuStart);
+        double cpuTimeScale = 1.0E9 / os::timeFrequency;
+        cpuDuration = (cpuEnd - lastFrame.cpuStart) * cpuTimeScale;
+        cpuEnd = (cpuEnd - baseCpuTime) * cpuTimeScale;
     } else {
-        std::cout << " _ _";
+        cpuEnd = 0;
+        cpuDuration = 0;
     }
 
-    std::cout << std::endl;
+    std::cout << "frame_end"
+              << " " << lastFrame.no
+              << " " << gpuEnd
+              << " " << gpuDuration
+              << " " << cpuEnd
+              << " " << cpuDuration
+              << std::endl;
+}
+
+void Profiler::parseLine(const char* in, Profile* profile)
+{
+    std::stringstream line(in, std::ios_base::in);
+    std::string type;
+
+    if (in[0] == '#' || strlen(in) < 12)
+        return;
+
+    line >> type;
+
+    if (type.compare("call") == 0) {
+        assert(profile->frames.size());
+        Profile::Call call;
+
+        line >> call.no
+             >> call.gpuStart
+             >> call.gpuDuration
+             >> call.cpuStart
+             >> call.cpuDuration
+             >> call.pixels
+             >> call.program
+             >> call.name;
+
+        profile->frames.back().calls.push_back(call);
+    } else if (type.compare("frame_begin") == 0) {
+        Profile::Frame frame;
+        frame.gpuDuration = 0;
+        frame.cpuDuration = 0;
+
+        line >> frame.no
+             >> frame.gpuStart
+             >> frame.cpuStart;
+
+        profile->frames.push_back(frame);
+    } else if (type.compare("frame_end") == 0) {
+        assert(profile->frames.size());
+        Profile::Frame& frame = profile->frames.back();
+        int64_t skipi64;
+
+        line >> frame.no
+             >> skipi64
+             >> frame.gpuDuration
+             >> skipi64
+             >> frame.cpuDuration;
+    }
 }
 }