]> git.cworth.org Git - apitrace/blob - common/trace_profiler.cpp
Cleaned up trace profiler output.
[apitrace] / common / trace_profiler.cpp
1 /**************************************************************************
2  *
3  * Copyright 2012 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #include "trace_profiler.hpp"
27 #include <iostream>
28
29 namespace trace {
30 Profiler::Profiler()
31     : baseGpuTime(0),
32       baseCpuTime(0),
33       cpuTimes(false),
34       gpuTimes(true),
35       pixelsDrawn(false)
36 {
37 }
38
39 Profiler::~Profiler()
40 {
41 }
42
43 void Profiler::setup(bool cpuTimes_, bool gpuTimes_, bool pixelsDrawn_)
44 {
45     cpuTimes = cpuTimes_;
46     gpuTimes = gpuTimes_;
47     pixelsDrawn = pixelsDrawn_;
48
49     std::cout << "# frame_begin no gpu_start cpu_start" << std::endl;
50     std::cout << "# frame_end no gpu_end gpu_dura cpu_end cpu_dura" << std::endl;
51     std::cout << "# call no gpu_start gpu_dura cpu_start cpu_dura pixels program name" << std::endl;
52 }
53
54 void Profiler::addCall(unsigned no,
55                        const char *name,
56                        unsigned program,
57                        uint64_t pixels,
58                        uint64_t gpuStart, uint64_t gpuDuration,
59                        uint64_t cpuStart, uint64_t cpuDuration)
60 {
61     if (baseGpuTime == 0) {
62         baseGpuTime = gpuStart;
63     }
64
65     if (baseCpuTime == 0) {
66         baseCpuTime = cpuStart;
67     }
68
69     if (gpuTimes) {
70         gpuStart -= baseGpuTime;
71     } else {
72         gpuStart = 0;
73         gpuDuration = 0;
74     }
75
76     if (cpuTimes) {
77         cpuStart -= baseCpuTime;
78     } else {
79         cpuStart = 0;
80         cpuDuration = 0;
81     }
82
83     if (!pixelsDrawn) {
84         pixels = 0;
85     }
86
87     std::cout << "call"
88               << " " << no
89               << " " << gpuStart
90               << " " << gpuDuration
91               << " " << cpuStart
92               << " " << cpuDuration
93               << " " << pixels
94               << " " << program
95               << " " << name
96               << std::endl;
97 }
98
99 void Profiler::addFrameStart(unsigned no, uint64_t gpuStart, uint64_t cpuStart)
100 {
101     if (baseGpuTime == 0) {
102         baseGpuTime = gpuStart;
103     }
104
105     if (baseCpuTime == 0) {
106         baseCpuTime = cpuStart;
107     }
108
109     if (gpuTimes) {
110         lastFrame.gpuStart = gpuStart - baseGpuTime;
111     } else {
112         lastFrame.gpuStart = 0;
113     }
114
115     if (cpuTimes) {
116         lastFrame.cpuStart = cpuStart - baseCpuTime;
117     } else {
118         lastFrame.cpuStart = 0;
119     }
120
121     lastFrame.no = no;
122
123     std::cout << "frame_begin"
124               << " " << lastFrame.no
125               << " " << lastFrame.gpuStart
126               << " " << lastFrame.cpuStart
127               << std::endl;
128 }
129
130 void Profiler::addFrameEnd(uint64_t gpuEnd, uint64_t cpuEnd)
131 {
132     uint64_t gpuDuration, cpuDuration;
133
134     if (gpuTimes) {
135         gpuEnd -= baseGpuTime;
136         gpuDuration = gpuEnd - lastFrame.gpuStart;
137     } else {
138         gpuEnd = 0;
139         gpuDuration = 0;
140     }
141
142     if (cpuTimes) {
143         cpuEnd -= baseCpuTime;
144         cpuDuration = cpuEnd - lastFrame.cpuStart;
145     } else {
146         cpuEnd = 0;
147         cpuDuration = 0;
148     }
149
150     std::cout << "frame_end"
151               << " " << lastFrame.no
152               << " " << gpuEnd
153               << " " << gpuDuration
154               << " " << cpuEnd
155               << " " << cpuDuration
156               << std::endl;
157 }
158 }