]> git.cworth.org Git - apitrace/blob - common/trace_profiler.cpp
6fc00395cd1d3de808d8ae999f05f9540f10a86a
[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     : lastProgram(0),
32       baseGpuTime(0),
33       baseCpuTime(0),
34       cpuTimes(false),
35       gpuTimes(true),
36       pixelsDrawn(false)
37 {
38 }
39
40 Profiler::~Profiler()
41 {
42 }
43
44 void Profiler::setup(bool cpuTimes_, bool gpuTimes_, bool pixelsDrawn_)
45 {
46     cpuTimes = cpuTimes_;
47     gpuTimes = gpuTimes_;
48     pixelsDrawn = pixelsDrawn_;
49
50     std::cout << "# frame begin <no> <gpu_start> <cpu_start>" << std::endl;
51     std::cout << "# frame end <no> <gpu_end> <gpu_dura> <cpu_end> <cpu_dura>" << std::endl;
52     std::cout << "# call <no> <gpu_start> <gpu_dura> <cpu_start> <cpu_dura> <pixels> <function>" << std::endl;
53     std::cout << "# use shader program <no>" << std::endl;
54 }
55
56 void Profiler::addCall(unsigned no,
57                        const char *name,
58                        unsigned program,
59                        uint64_t pixels,
60                        uint64_t gpuStart, uint64_t gpuDuration,
61                        uint64_t cpuStart, uint64_t cpuDuration)
62 {
63     if (baseGpuTime == 0)
64         baseGpuTime = gpuStart;
65
66     if (baseCpuTime == 0)
67         baseCpuTime = cpuStart;
68
69     if (program != lastProgram) {
70         std::cout << "use shader program " << program << std::endl;
71         lastProgram = program;
72     }
73
74     std::cout << "call " << no;
75
76     if (gpuTimes) {
77         std::cout << " "
78                   << (gpuStart - baseGpuTime) << " "
79                   << gpuDuration;
80     } else {
81         std::cout << " _ _";
82     }
83
84     if (cpuTimes) {
85         std::cout << " "
86                   << (cpuStart - baseCpuTime) << " "
87                   << cpuDuration;
88     } else {
89         std::cout << " _ _";
90     }
91
92     if (pixelsDrawn) {
93         std::cout << " " << pixels;
94     } else {
95         std::cout << " _";
96     }
97
98     std::cout << " " << name << std::endl;
99 }
100
101 void Profiler::addFrameStart(unsigned no, uint64_t gpuStart, uint64_t cpuStart)
102 {
103     if (baseGpuTime == 0)
104         baseGpuTime = gpuStart;
105
106     if (baseCpuTime == 0)
107         baseCpuTime = cpuStart;
108
109     lastFrame.no = no;
110     lastFrame.gpuStart = gpuStart - baseGpuTime;
111
112     std::cout << "frame begin " << lastFrame.no;
113
114     if (gpuTimes) {
115         std::cout << " " << lastFrame.gpuStart;
116     } else {
117         std::cout << " _";
118     }
119
120     if (gpuTimes) {
121         std::cout << " " << lastFrame.cpuStart;
122     } else {
123         std::cout << " _";
124     }
125
126     std::cout << std::endl;
127 }
128
129 void Profiler::addFrameEnd(uint64_t gpuEnd, uint64_t cpuEnd)
130 {
131     if (baseGpuTime == 0)
132         baseGpuTime = gpuEnd;
133
134     if (baseCpuTime == 0)
135         baseCpuTime = cpuEnd;
136
137     lastFrame.gpuEnd = gpuEnd - baseGpuTime;
138
139     std::cout << "frame end " << lastFrame.no;
140
141     if (gpuTimes) {
142         std::cout << " " << lastFrame.gpuEnd << " " << (lastFrame.gpuEnd - lastFrame.gpuStart);
143     } else {
144         std::cout << " _ _";
145     }
146
147     if (cpuTimes) {
148         std::cout << " " << lastFrame.cpuEnd << " " << (lastFrame.cpuEnd - lastFrame.cpuStart);
149     } else {
150         std::cout << " _ _";
151     }
152
153     std::cout << std::endl;
154 }
155 }