]> git.cworth.org Git - apitrace/blob - retrace/retrace.hpp
Improved profiling capabilities.
[apitrace] / retrace / retrace.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011-2012 Jose Fonseca
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 #ifndef _RETRACE_HPP_
27 #define _RETRACE_HPP_
28
29 #include <assert.h>
30 #include <string.h>
31 #include <stdint.h>
32
33 #include <list>
34 #include <map>
35 #include <ostream>
36
37 #include "trace_model.hpp"
38 #include "trace_parser.hpp"
39 #include "trace_profiler.hpp"
40
41
42 namespace image {
43     class Image;
44 }
45
46
47 namespace retrace {
48
49
50 extern trace::Parser parser;
51 extern trace::Profiler profiler;
52
53
54 /**
55  * Similar to alloca(), but implemented with malloc.
56  */
57 class ScopedAllocator
58 {
59 private:
60     uintptr_t next;
61
62 public:
63     ScopedAllocator() :
64         next(0) {
65     }
66
67     inline void *
68     alloc(size_t size) {
69         /* Always return valid address, even when size is zero */
70         size = std::max(size, sizeof(uintptr_t));
71
72         uintptr_t * buf = static_cast<uintptr_t *>(malloc(sizeof(uintptr_t) + size));
73         if (!buf) {
74             return NULL;
75         }
76
77         *buf = next;
78         next = reinterpret_cast<uintptr_t>(buf);
79         assert((next & 1) == 0);
80
81         return static_cast<void *>(&buf[1]);
82     }
83
84     template< class T >
85     inline T *
86     alloc(size_t n = 1) {
87         return static_cast<T *>(alloc(sizeof(T) * n));
88     }
89
90     /**
91      * Allocate an array with the same dimensions as the specified value.
92      */
93     template< class T >
94     inline T *
95     alloc(const trace::Value *value) {
96         const trace::Array *array = dynamic_cast<const trace::Array *>(value);
97         if (array) {
98             return alloc<T>(array->size());
99         }
100         const trace::Null *null = dynamic_cast<const trace::Null *>(value);
101         if (null) {
102             return NULL;
103         }
104         assert(0);
105         return NULL;
106     }
107
108     /**
109      * Prevent this pointer from being automatically freed.
110      */
111     template< class T >
112     inline void
113     bind(T *ptr) {
114         if (ptr) {
115             reinterpret_cast<uintptr_t *>(ptr)[-1] |= 1;
116         }
117     }
118
119     inline
120     ~ScopedAllocator() {
121         while (next) {
122             uintptr_t temp = *reinterpret_cast<uintptr_t *>(next);
123
124             bool bind = temp & 1;
125             temp &= ~1;
126
127             if (!bind) {
128                 free(reinterpret_cast<void *>(next));
129             }
130
131             next = temp;
132         }
133     }
134 };
135
136
137 /**
138  * Output verbosity when retracing files.
139  */
140 extern int verbosity;
141
142 /**
143  * Debugging checks.
144  */
145 extern bool debug;
146
147 /**
148  * Add profiling data to the dump when retracing.
149  */
150 extern bool profiling;
151 extern bool profilingCpuTimes;
152 extern bool profilingGpuTimes;
153 extern bool profilingPixelsDrawn;
154
155 /**
156  * State dumping.
157  */
158 extern bool dumpingState;
159
160
161 extern bool doubleBuffer;
162 extern bool coreProfile;
163
164 extern unsigned frameNo;
165
166
167 std::ostream &warning(trace::Call &call);
168
169
170 void ignore(trace::Call &call);
171 void unsupported(trace::Call &call);
172
173
174 typedef void (*Callback)(trace::Call &call);
175
176 struct Entry {
177     const char *name;
178     Callback callback;
179 };
180
181
182 struct stringComparer {
183   bool operator() (const char *a, const  char *b) const {
184     return strcmp(a, b) < 0;
185   }
186 };
187
188
189 extern const Entry stdc_callbacks[];
190
191
192 class Retracer
193 {
194     typedef std::map<const char *, Callback, stringComparer> Map;
195     Map map;
196
197     std::vector<Callback> callbacks;
198
199 public:
200     Retracer() {
201         addCallbacks(stdc_callbacks);
202     }
203
204     virtual ~Retracer() {}
205
206     void addCallback(const Entry *entry);
207     void addCallbacks(const Entry *entries);
208
209     void retrace(trace::Call &call);
210 };
211
212
213 void
214 setUp(void);
215
216 void
217 addCallbacks(retrace::Retracer &retracer);
218
219 void
220 frameComplete(trace::Call &call);
221
222 image::Image *
223 getSnapshot(void);
224
225 bool
226 dumpState(std::ostream &os);
227
228 void
229 flushRendering(void);
230
231 void
232 waitForInput(void);
233
234 void
235 cleanUp(void);
236
237
238 } /* namespace retrace */
239
240 #endif /* _RETRACE_HPP_ */