X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Ftrace_profiler.cpp;h=402cdacc025954115f3ca9d964c272a3f1904fac;hb=4c4896f8490aca7f32956e402ffdf413d04c36dd;hp=fdc0fd4a2f748c170c464bbd7af12e6852d4f68b;hpb=addf7f90727a50040d79e6da6d59ffb031074674;p=apitrace diff --git a/common/trace_profiler.cpp b/common/trace_profiler.cpp index fdc0fd4..402cdac 100644 --- a/common/trace_profiler.cpp +++ b/common/trace_profiler.cpp @@ -24,11 +24,19 @@ **************************************************************************/ #include "trace_profiler.hpp" +#include "os_time.hpp" #include +#include +#include namespace trace { Profiler::Profiler() - : baseTime(0) + : baseGpuTime(0), + baseCpuTime(0), + minCpuTime(1000), + cpuTimes(false), + gpuTimes(true), + pixelsDrawn(false) { } @@ -36,26 +44,158 @@ Profiler::~Profiler() { } -void Profiler::addCall(const Call& call) +void Profiler::setup(bool cpuTimes_, bool gpuTimes_, bool pixelsDrawn_) { - if (baseTime == 0) - baseTime = call.gpu.start; + cpuTimes = cpuTimes_; + gpuTimes = gpuTimes_; + pixelsDrawn = pixelsDrawn_; - std::cout << "call " - << call.no << " " - << (call.gpu.start - baseTime) << " " - << call.gpu.duration << " " - << call.name << std::endl; + std::cout << "# call no gpu_start gpu_dura cpu_start cpu_dura pixels program name" << std::endl; } -void Profiler::addFrame(const Frame& frame) +int64_t Profiler::getBaseCpuTime() { - if (baseTime == 0) - baseTime = frame.gpu.start; + return baseCpuTime; +} + +int64_t Profiler::getBaseGpuTime() +{ + return baseGpuTime; +} + +void Profiler::setBaseCpuTime(int64_t cpuStart) +{ + baseCpuTime = cpuStart; +} + +void Profiler::setBaseGpuTime(int64_t gpuStart) +{ + baseGpuTime = gpuStart; +} + +bool Profiler::hasBaseTimes() +{ + return baseCpuTime != 0 || baseGpuTime != 0; +} + +void Profiler::addCall(unsigned no, + const char *name, + unsigned program, + int64_t pixels, + int64_t gpuStart, int64_t gpuDuration, + int64_t cpuStart, int64_t cpuDuration) +{ + if (gpuTimes && gpuStart) { + gpuStart -= baseGpuTime; + } else { + gpuStart = 0; + gpuDuration = 0; + } + + if (cpuTimes && cpuStart) { + double cpuTimeScale = 1.0E9 / os::timeFrequency; + cpuStart = (cpuStart - baseCpuTime) * cpuTimeScale; + cpuDuration = cpuDuration * cpuTimeScale; + + if (cpuDuration < minCpuTime) { + return; + } + } else { + cpuStart = 0; + cpuDuration = 0; + } + + if (!pixelsDrawn) { + pixels = 0; + } + + std::cout << "call" + << " " << no + << " " << gpuStart + << " " << gpuDuration + << " " << cpuStart + << " " << cpuDuration + << " " << pixels + << " " << program + << " " << name + << std::endl; +} + +void Profiler::addFrameEnd() +{ + std::cout << "frame_end" << std::endl; +} + +void Profiler::parseLine(const char* in, Profile* profile) +{ + std::stringstream line(in, std::ios_base::in); + std::string type; + static int64_t lastGpuTime; + static int64_t lastCpuTime; + + if (in[0] == '#' || strlen(in) < 4) + return; + + if (profile->programs.size() == 0 && profile->calls.size() == 0 && profile->frames.size() == 0) { + lastGpuTime = 0; + lastCpuTime = 0; + } + + line >> type; + + if (type.compare("call") == 0) { + Profile::Call call; + unsigned programNo; + + line >> call.no + >> call.gpuStart + >> call.gpuDuration + >> call.cpuStart + >> call.cpuDuration + >> call.pixels + >> programNo + >> call.name; + + if (lastGpuTime < call.gpuStart + call.gpuDuration) { + lastGpuTime = call.gpuStart + call.gpuDuration; + } + + if (lastCpuTime < call.cpuStart + call.cpuDuration) { + lastCpuTime = call.cpuStart + call.cpuDuration; + } + + profile->calls.push_back(call); + + if (call.pixels >= 0) { + if (profile->programs.size() <= programNo) { + profile->programs.resize(programNo + 1); + } + + Profile::Program& program = profile->programs[programNo]; + program.cpuTotal += call.cpuDuration; + program.gpuTotal += call.gpuDuration; + program.pixelTotal += call.pixels; + program.calls.push_back(profile->calls.size() - 1); + } + } else if (type.compare("frame_end") == 0) { + Profile::Frame frame; + frame.no = profile->frames.size(); + + if (frame.no == 0) { + frame.gpuStart = 0; + frame.cpuStart = 0; + frame.calls.begin = 0; + } else { + frame.gpuStart = profile->frames.back().gpuStart + profile->frames.back().gpuDuration; + frame.cpuStart = profile->frames.back().cpuStart + profile->frames.back().cpuDuration; + frame.calls.begin = profile->frames.back().calls.end + 1; + } + + frame.gpuDuration = lastGpuTime - frame.gpuStart; + frame.cpuDuration = lastCpuTime - frame.cpuStart; + frame.calls.end = profile->calls.size() - 1; - std::cout << "frame " - << frame.no << " " - << (frame.gpu.start - baseTime) << " " - << frame.gpu.duration << std::endl; + profile->frames.push_back(frame); + } } }