2 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
8 * 1. Redistributions of source code must retain the above copyright
\r
9 * notice, this list of conditions and the following disclaimer.
\r
10 * 2. Redistributions in binary form must reproduce the above copyright
\r
11 * notice, this list of conditions and the following disclaimer in the
\r
12 * documentation and/or other materials provided with the distribution.
\r
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
\r
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
\r
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
24 * POSSIBILITY OF SUCH DAMAGE.
\r
28 * Adapted from Herb Marselas
\r
29 "Profiling, Data Analysis, Scalability, and Magic Numbers: Meeting the Minimum System Requirements for AGE OF EMPIRES 2: THE AGE OF KINGS"
\r
30 Game Developer magazine
\r
34 #include "profile.h"
\r
38 //==============================================================================
\r
39 static OPJ_PROFILE_LIST group_list [PGROUP_LASTGROUP];
\r
41 //==============================================================================
\r
42 static void GetTimeStamp(OPJ_UINT32 *pdwtime);
\r
44 //==============================================================================
\r
45 #define SetMajorSection(entry, major) \
\r
46 { group_list[ entry ].section = entry ; \
\r
47 group_list[ entry ].sectionName = #major ; }
\r
49 //==============================================================================
\r
50 void _ProfInit(void)
\r
52 // clear everything out
\r
53 memset(group_list, 0, sizeof(group_list));
\r
55 // set groups and parents for timing
\r
56 SetMajorSection(PGROUP_DWT,PGROUP_DWT);
\r
57 SetMajorSection(PGROUP_T1, PGROUP_T1);
\r
58 SetMajorSection(PGROUP_T2, PGROUP_T2);
\r
61 //==============================================================================
\r
62 void _ProfStart (OPJ_PROFILE_GROUP group)
\r
64 // make sure this hasn't been incorrectly started twice
\r
65 if (group_list[group].start)
\r
70 // get the start time
\r
71 GetTimeStamp(&(group_list[group].start));
\r
75 //==============================================================================
\r
76 void _ProfStop(OPJ_PROFILE_GROUP group)
\r
78 // make sure we called start first
\r
79 if (!group_list[group].start)
\r
85 GetTimeStamp(&(group_list[group].end));
\r
87 // calculate this latest elapsed interval
\r
88 group_list[group].total_time += group_list[group].end - group_list[group].start;
\r
90 // reset starting time
\r
91 group_list[group].start = 0;
\r
93 // incr the number of calls made
\r
94 ++group_list[group].totalCalls;
\r
98 //==============================================================================
\r
99 #define proftracef(id,totalTime) \
\r
100 fprintf(p, #id "\t%u\t\t%6.6f\t\t%12.6f\t%2.2f%%\n", \
\r
101 group_list[ id ].totalCalls, \
\r
102 (OPJ_FLOAT64) group_list[ id ].total_time / CLOCKS_PER_SEC, \
\r
103 ((OPJ_FLOAT64) group_list[ id ].total_time / (group_list[ id ].totalCalls ? group_list[ id ].totalCalls : 1)), \
\r
104 ((OPJ_FLOAT64) group_list[ id ].total_time / totalTime * 100))
\r
106 #define proftracep(id,totalTime) \
\r
107 printf(#id "\t%u\t\t%6.6f\t\t%12.6f\t%2.2f%%\n", \
\r
108 group_list[ id ].totalCalls, \
\r
109 (OPJ_FLOAT64) group_list[ id ].total_time / CLOCKS_PER_SEC, \
\r
110 ((OPJ_FLOAT64) group_list[ id ].total_time / (group_list[ id ].totalCalls ? group_list[ id ].totalCalls : 1)), \
\r
111 ((OPJ_FLOAT64) group_list[ id ].total_time / totalTime * 100))
\r
113 //==============================================================================
\r
114 void _ProfSave(const OPJ_CHAR * pFileName)
\r
116 FILE *p = fopen(pFileName, "wt");
\r
117 OPJ_FLOAT64 totalTime = 0.;
\r
126 (i=0;i<PGROUP_LASTGROUP;++i)
\r
128 totalTime += group_list[i].total_time;
\r
131 fputs("\n\nProfile Data:\n", p);
\r
132 fputs("description\tnb calls\ttotal time (sec)\ttime per call\t%% of section\n", p);
\r
134 proftracef(PGROUP_DWT,totalTime);
\r
135 proftracef(PGROUP_T1,totalTime);
\r
136 proftracef(PGROUP_T2,totalTime);
\r
138 fputs("=== end of profile list ===\n\n", p);
\r
144 //==============================================================================
\r
145 void _ProfPrint(void)
\r
147 OPJ_FLOAT64 totalTime = 0.;
\r
151 (i=0;i<PGROUP_LASTGROUP;++i)
\r
153 totalTime += group_list[i].total_time;
\r
156 printf("\n\nProfile Data:\n");
\r
157 printf("description\tnb calls\ttotal time (sec)\ttime per call\t%% of section\n");
\r
159 proftracep(PGROUP_RATE, totalTime);
\r
160 proftracep(PGROUP_DC_SHIFT, totalTime);
\r
161 proftracep(PGROUP_MCT, totalTime);
\r
162 proftracep(PGROUP_DWT, totalTime);
\r
163 proftracep(PGROUP_T1, totalTime);
\r
164 proftracep(PGROUP_T2, totalTime);
\r
166 printf("\nTotal time: %6.3f second(s)\n", totalTime / CLOCKS_PER_SEC);
\r
168 printf("=== end of profile list ===\n\n");
\r
172 //==============================================================================
\r
173 static void GetTimeStamp(unsigned *time)
\r