]> git.cworth.org Git - apitrace/blob - common/trace_profiler.cpp
Add gui support for trace profiling.
[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 #include <string.h>
29 #include <assert.h>
30 #include <stdio.h>
31
32 namespace trace {
33 Profiler::Profiler()
34     : baseGpuTime(0),
35       baseCpuTime(0),
36       cpuTimes(false),
37       gpuTimes(true),
38       pixelsDrawn(false)
39 {
40 }
41
42 Profiler::~Profiler()
43 {
44 }
45
46 void Profiler::setup(bool cpuTimes_, bool gpuTimes_, bool pixelsDrawn_)
47 {
48     cpuTimes = cpuTimes_;
49     gpuTimes = gpuTimes_;
50     pixelsDrawn = pixelsDrawn_;
51
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;
55 }
56
57 void Profiler::addCall(unsigned no,
58                        const char *name,
59                        unsigned program,
60                        uint64_t pixels,
61                        uint64_t gpuStart, uint64_t gpuDuration,
62                        uint64_t cpuStart, uint64_t cpuDuration)
63 {
64     if (baseGpuTime == 0) {
65         baseGpuTime = gpuStart;
66     }
67
68     if (baseCpuTime == 0) {
69         baseCpuTime = cpuStart;
70     }
71
72     if (gpuTimes) {
73         gpuStart -= baseGpuTime;
74     } else {
75         gpuStart = 0;
76         gpuDuration = 0;
77     }
78
79     if (cpuTimes) {
80         cpuStart -= baseCpuTime;
81     } else {
82         cpuStart = 0;
83         cpuDuration = 0;
84     }
85
86     if (!pixelsDrawn) {
87         pixels = 0;
88     }
89
90     std::cout << "call"
91               << " " << no
92               << " " << gpuStart
93               << " " << gpuDuration
94               << " " << cpuStart
95               << " " << cpuDuration
96               << " " << pixels
97               << " " << program
98               << " " << name
99               << std::endl;
100 }
101
102 void Profiler::addFrameStart(unsigned no, uint64_t gpuStart, uint64_t cpuStart)
103 {
104     if (baseGpuTime == 0) {
105         baseGpuTime = gpuStart;
106     }
107
108     if (baseCpuTime == 0) {
109         baseCpuTime = cpuStart;
110     }
111
112     if (gpuTimes) {
113         lastFrame.gpuStart = gpuStart - baseGpuTime;
114     } else {
115         lastFrame.gpuStart = 0;
116     }
117
118     if (cpuTimes) {
119         lastFrame.cpuStart = cpuStart - baseCpuTime;
120     } else {
121         lastFrame.cpuStart = 0;
122     }
123
124     lastFrame.no = no;
125
126     std::cout << "frame_begin"
127               << " " << lastFrame.no
128               << " " << lastFrame.gpuStart
129               << " " << lastFrame.cpuStart
130               << std::endl;
131 }
132
133 void Profiler::addFrameEnd(uint64_t gpuEnd, uint64_t cpuEnd)
134 {
135     uint64_t gpuDuration, cpuDuration;
136
137     if (gpuTimes) {
138         gpuEnd -= baseGpuTime;
139         gpuDuration = gpuEnd - lastFrame.gpuStart;
140     } else {
141         gpuEnd = 0;
142         gpuDuration = 0;
143     }
144
145     if (cpuTimes) {
146         cpuEnd -= baseCpuTime;
147         cpuDuration = cpuEnd - lastFrame.cpuStart;
148     } else {
149         cpuEnd = 0;
150         cpuDuration = 0;
151     }
152
153     std::cout << "frame_end"
154               << " " << lastFrame.no
155               << " " << gpuEnd
156               << " " << gpuDuration
157               << " " << cpuEnd
158               << " " << cpuDuration
159               << std::endl;
160 }
161
162 void Profiler::parseLine(const char* line, Profile* profile)
163 {
164     char name[64];
165
166     if (line[0] == '#' || strlen(line) < 12)
167         return;
168
169     if (strncmp(line, "call ", 5) == 0) {
170         assert(profile->frames.size());
171
172         Profile::Call call;
173         sscanf(line, "call %u %li %li %li %li %li %u %s", &call.no, &call.gpuStart, &call.gpuDuration, &call.cpuStart, &call.cpuDuration, &call.pixels, &call.program, name);
174         call.name = name;
175         profile->frames.back().calls.push_back(call);
176     } else if (strncmp(line, "frame_begin ", 12) == 0) {
177         Profile::Frame frame;
178         frame.gpuDuration = 0;
179         frame.gpuDuration = 0;
180         sscanf(line, "frame_begin %u %li %li", &frame.no, &frame.gpuStart, &frame.cpuStart);
181         profile->frames.push_back(frame);
182     } else if (strncmp(line, "frame_end ", 10) == 0) {
183         assert(profile->frames.size());
184
185         Profile::Frame& frame = profile->frames.back();
186         unsigned no;
187         sscanf(line, "frame_end %u %*li %li %*li %li", &no, &frame.gpuDuration, &frame.cpuDuration);
188         assert(no == frame.no);
189     }
190 }
191 }