1 /**************************************************************************
3 * Copyright 2012 VMware, Inc.
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 **************************************************************************/
26 #include "trace_profiler.hpp"
27 #include "os_time.hpp"
47 void Profiler::setup(bool cpuTimes_, bool gpuTimes_, bool pixelsDrawn_)
51 pixelsDrawn = pixelsDrawn_;
53 std::cout << "# call no gpu_start gpu_dura cpu_start cpu_dura pixels program name" << std::endl;
56 int64_t Profiler::getBaseCpuTime()
61 int64_t Profiler::getBaseGpuTime()
66 void Profiler::setBaseCpuTime(int64_t cpuStart)
68 baseCpuTime = cpuStart;
71 void Profiler::setBaseGpuTime(int64_t gpuStart)
73 baseGpuTime = gpuStart;
76 bool Profiler::hasBaseTimes()
78 return baseCpuTime != 0 || baseGpuTime != 0;
81 void Profiler::addCall(unsigned no,
85 int64_t gpuStart, int64_t gpuDuration,
86 int64_t cpuStart, int64_t cpuDuration)
88 if (gpuTimes && gpuStart) {
89 gpuStart -= baseGpuTime;
95 if (cpuTimes && cpuStart) {
96 double cpuTimeScale = 1.0E9 / os::timeFrequency;
97 cpuStart = (cpuStart - baseCpuTime) * cpuTimeScale;
98 cpuDuration = cpuDuration * cpuTimeScale;
100 if (cpuDuration < minCpuTime) {
115 << " " << gpuDuration
117 << " " << cpuDuration
124 void Profiler::addFrameEnd()
126 std::cout << "frame_end" << std::endl;
129 void Profiler::parseLine(const char* in, Profile* profile)
131 std::stringstream line(in, std::ios_base::in);
133 static int64_t lastGpuTime;
134 static int64_t lastCpuTime;
136 if (in[0] == '#' || strlen(in) < 4)
139 if (profile->programs.size() == 0 && profile->calls.size() == 0 && profile->frames.size() == 0) {
146 if (type.compare("call") == 0) {
158 if (lastGpuTime < call.gpuStart + call.gpuDuration) {
159 lastGpuTime = call.gpuStart + call.gpuDuration;
162 if (lastCpuTime < call.cpuStart + call.cpuDuration) {
163 lastCpuTime = call.cpuStart + call.cpuDuration;
166 profile->calls.push_back(call);
168 if (call.pixels >= 0) {
169 if (profile->programs.size() <= call.program) {
170 profile->programs.resize(call.program + 1);
173 Profile::Program& program = profile->programs[call.program];
174 program.cpuTotal += call.cpuDuration;
175 program.gpuTotal += call.gpuDuration;
176 program.pixelTotal += call.pixels;
177 program.calls.push_back(profile->calls.size() - 1);
179 } else if (type.compare("frame_end") == 0) {
180 Profile::Frame frame;
181 frame.no = profile->frames.size();
186 frame.calls.begin = 0;
188 frame.gpuStart = profile->frames.back().gpuStart + profile->frames.back().gpuDuration;
189 frame.cpuStart = profile->frames.back().cpuStart + profile->frames.back().cpuDuration;
190 frame.calls.begin = profile->frames.back().calls.end + 1;
193 frame.gpuDuration = lastGpuTime - frame.gpuStart;
194 frame.cpuDuration = lastCpuTime - frame.cpuStart;
195 frame.calls.end = profile->calls.size() - 1;
197 profile->frames.push_back(frame);