1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
32 * @author José Fonseca <jrfonseca@tungstengraphics.com>
34 * @sa http://blogs.msdn.com/joshpoley/archive/2008/03/12/poor-man-s-profiler.aspx
35 * @sa http://www.johnpanzer.com/aci_cuj/index.html
38 #include "pipe/p_config.h"
40 #if defined(PROFILE) && defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
45 #include "pipe/p_debug.h"
46 #include "util/u_string.h"
49 #define PROFILE_TABLE_SIZE (1024*1024)
50 #define FILE_NAME_SIZE 256
52 struct debug_profile_entry
59 static unsigned long enabled
= 0;
61 static WCHAR wFileName
[FILE_NAME_SIZE
] = L
"\\??\\c:\\00000000.prof";
62 static ULONG_PTR iFile
= 0;
64 static struct debug_profile_entry
*table
= NULL
;
65 static unsigned long free_table_entries
= 0;
66 static unsigned long max_table_entries
= 0;
68 uint64_t start_stamp
= 0;
69 uint64_t end_stamp
= 0;
73 debug_profile_entry(uintptr_t caller
, uintptr_t callee
, uint64_t samples
)
75 unsigned hash
= ( caller
+ callee
) & PROFILE_TABLE_SIZE
- 1;
78 if(table
[hash
].caller
== 0 && table
[hash
].callee
== 0) {
79 table
[hash
].caller
= caller
;
80 table
[hash
].callee
= callee
;
81 table
[hash
].samples
= samples
;
85 else if(table
[hash
].caller
== caller
&& table
[hash
].callee
== callee
) {
86 table
[hash
].samples
+= samples
;
96 static uintptr_t caller_stack
[1024];
97 static unsigned last_caller
= 0;
100 static int64_t delta(void) {
101 int64_t result
= end_stamp
- start_stamp
;
102 if(result
> UINT64_C(0xffffffff))
109 debug_profile_enter(uintptr_t callee
)
111 uintptr_t caller
= last_caller
? caller_stack
[last_caller
- 1] : 0;
114 debug_profile_entry(caller
, 0, delta());
115 debug_profile_entry(caller
, callee
, 1);
116 caller_stack
[last_caller
++] = callee
;
121 debug_profile_exit(uintptr_t callee
)
123 debug_profile_entry(callee
, 0, delta());
130 * Called at the start of every method or function.
132 * @sa http://msdn.microsoft.com/en-us/library/c63a9b7h.aspx
134 void __declspec(naked
) __cdecl
145 mov dword ptr
[end_stamp
], eax
146 mov dword ptr
[end_stamp
+4], edx
160 call debug_profile_enter
173 mov dword ptr
[start_stamp
], eax
174 mov dword ptr
[start_stamp
+4], edx
185 * Called at the end of Calls the end of every method or function.
187 * @sa http://msdn.microsoft.com/en-us/library/xc11y76y.aspx
189 void __declspec(naked
) __cdecl
200 mov dword ptr
[end_stamp
], eax
201 mov dword ptr
[end_stamp
+4], edx
215 call debug_profile_exit
228 mov dword ptr
[start_stamp
], eax
229 mov dword ptr
[start_stamp
+4], edx
240 * Reference function for calibration.
242 void __declspec(naked
)
243 __debug_profile_reference(void) {
253 debug_profile_start(void)
257 // increment starting from the less significant digit
269 table
= EngMapFile(wFileName
,
270 PROFILE_TABLE_SIZE
*sizeof(struct debug_profile_entry
),
275 free_table_entries
= max_table_entries
= PROFILE_TABLE_SIZE
;
276 memset(table
, 0, PROFILE_TABLE_SIZE
*sizeof(struct debug_profile_entry
));
278 table
[0].caller
= (uintptr_t)&__debug_profile_reference
;
280 table
[0].samples
= 0;
281 --free_table_entries
;
288 mov dword ptr
[start_stamp
], eax
289 mov dword ptr
[start_stamp
+4], edx
299 for(i
= 0; i
< 8; ++i
) {
301 call __debug_profile_reference
309 debug_profile_stop(void)
317 free_table_entries
= max_table_entries
= 0;