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"
46 void Profiler::setup(bool cpuTimes_, bool gpuTimes_, bool pixelsDrawn_)
50 pixelsDrawn = pixelsDrawn_;
52 std::cout << "# frame_begin no gpu_start cpu_start" << std::endl;
53 std::cout << "# frame_end no gpu_end gpu_dura cpu_end cpu_dura" << std::endl;
54 std::cout << "# call no gpu_start gpu_dura cpu_start cpu_dura pixels program name" << std::endl;
57 void Profiler::addCall(unsigned no,
61 uint64_t gpuStart, uint64_t gpuDuration,
62 uint64_t cpuStart, uint64_t cpuDuration)
64 if (baseGpuTime == 0) {
65 baseGpuTime = gpuStart;
68 if (baseCpuTime == 0) {
69 baseCpuTime = cpuStart;
73 gpuStart -= baseGpuTime;
80 cpuStart -= baseCpuTime;
102 void Profiler::addFrameStart(unsigned no, uint64_t gpuStart, uint64_t cpuStart)
104 if (baseGpuTime == 0) {
105 baseGpuTime = gpuStart;
108 if (baseCpuTime == 0) {
109 baseCpuTime = cpuStart;
113 lastFrame.gpuStart = gpuStart - baseGpuTime;
115 lastFrame.gpuStart = 0;
119 lastFrame.cpuStart = cpuStart - baseCpuTime;
121 lastFrame.cpuStart = 0;
126 std::cout << "frame_begin"
127 << " " << lastFrame.no
128 << " " << lastFrame.gpuStart
129 << " " << lastFrame.cpuStart
133 void Profiler::addFrameEnd(uint64_t gpuEnd, uint64_t cpuEnd)
135 uint64_t gpuDuration, cpuDuration;
138 gpuEnd -= baseGpuTime;
139 gpuDuration = gpuEnd - lastFrame.gpuStart;
146 cpuEnd -= baseCpuTime;
147 cpuDuration = cpuEnd - lastFrame.cpuStart;
153 std::cout << "frame_end"
154 << " " << lastFrame.no
156 << " " << gpuDuration
158 << " " << cpuDuration
162 void Profiler::parseLine(const char* in, Profile* profile)
164 std::stringstream line(in, std::ios_base::in);
167 if (in[0] == '#' || strlen(in) < 12)
172 if (type.compare("call") == 0) {
173 assert(profile->frames.size());
185 profile->frames.back().calls.push_back(call);
186 } else if (type.compare("frame_begin") == 0) {
187 Profile::Frame frame;
188 frame.gpuDuration = 0;
189 frame.cpuDuration = 0;
195 profile->frames.push_back(frame);
196 } else if (type.compare("frame_end") == 0) {
197 assert(profile->frames.size());
198 Profile::Frame& frame = profile->frames.back();
205 >> frame.cpuDuration;