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"
31 #include "os_time.hpp"
47 void Profiler::setup(bool cpuTimes_, bool gpuTimes_, bool pixelsDrawn_)
51 pixelsDrawn = pixelsDrawn_;
53 std::cout << "# frame_begin no gpu_start cpu_start" << std::endl;
54 std::cout << "# frame_end no gpu_end gpu_dura cpu_end cpu_dura" << std::endl;
55 std::cout << "# call no gpu_start gpu_dura cpu_start cpu_dura pixels program name" << std::endl;
58 void Profiler::setBaseTimes(int64_t gpuStart, int64_t cpuStart)
60 baseCpuTime = cpuStart;
61 baseGpuTime = gpuStart;
64 bool Profiler::hasBaseTimes()
66 return baseCpuTime != 0 || baseGpuTime != 0;
69 void Profiler::addCall(unsigned no,
73 int64_t gpuStart, int64_t gpuDuration,
74 int64_t cpuStart, int64_t cpuDuration)
76 if (gpuTimes && gpuStart) {
77 gpuStart -= baseGpuTime;
83 if (cpuTimes && cpuStart) {
84 double cpuTimeScale = 1.0E9 / os::timeFrequency;
85 cpuStart = (cpuStart - baseCpuTime) * cpuTimeScale;
86 cpuDuration = cpuDuration * cpuTimeScale;
101 << " " << cpuDuration
108 void Profiler::addFrameStart(unsigned no, int64_t gpuStart, int64_t cpuStart)
111 lastFrame.gpuStart = gpuStart;
112 lastFrame.cpuStart = cpuStart;
115 gpuStart = gpuStart - baseGpuTime;
121 double cpuTimeScale = 1.0E9 / os::timeFrequency;
122 cpuStart = (cpuStart - baseCpuTime) * cpuTimeScale;
127 std::cout << "frame_begin"
134 void Profiler::addFrameEnd(int64_t gpuEnd, int64_t cpuEnd)
136 int64_t gpuDuration, cpuDuration;
139 gpuDuration = gpuEnd - lastFrame.gpuStart;
140 gpuEnd = gpuEnd - baseGpuTime;
147 double cpuTimeScale = 1.0E9 / os::timeFrequency;
148 cpuDuration = (cpuEnd - lastFrame.cpuStart) * cpuTimeScale;
149 cpuEnd = (cpuEnd - baseCpuTime) * cpuTimeScale;
155 std::cout << "frame_end"
156 << " " << lastFrame.no
158 << " " << gpuDuration
160 << " " << cpuDuration
164 void Profiler::parseLine(const char* in, Profile* profile)
166 std::stringstream line(in, std::ios_base::in);
169 if (in[0] == '#' || strlen(in) < 12)
174 if (type.compare("call") == 0) {
175 assert(profile->frames.size());
187 if (call.pixels >= 0) {
188 profile->frames.back().calls.push_back(call);
190 } else if (type.compare("frame_begin") == 0) {
191 Profile::Frame frame;
192 frame.gpuDuration = 0;
193 frame.cpuDuration = 0;
199 profile->frames.push_back(frame);
200 } else if (type.compare("frame_end") == 0) {
201 assert(profile->frames.size());
202 Profile::Frame& frame = profile->frames.back();
209 >> frame.cpuDuration;