-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfiler.cpp
226 lines (180 loc) · 6.04 KB
/
Profiler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Copyright 2011 Matt DeVore
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "StdAfx.h"
#ifdef _DEBUG
#include "Profiler.h"
using std::vector;
using std::string;
const float INVALID_PERCENTAGE = -1.0f;
LONGLONG start_frame;
float timer_frequency;
struct ProfileSample {
DWORD profile_instances; // number of times ProfileBegin called
DWORD open_profiles; // number of times ProfileBegin called w/o ProfileEnd
LONGLONG start_time;
LONGLONG accumulator; // all samples this frame added together
LONGLONG children_sample_time; // time taken by all children
DWORD num_parents;
DWORD parent; // contains id of parent
float ave;
float min;
float max;
string name;
};
typedef vector<ProfileSample> VCTR_PROFILESAMPLE;
VCTR_PROFILESAMPLE profile_data;
CProfile::CProfile(DWORD id, const char *name) {
if(profile_data.size() <= id) {
// the profile_data array is not large enough to store a new profile data
// sample
ProfileSample x;
x.profile_instances = 0;
x.min = x.max = x.ave = INVALID_PERCENTAGE;
profile_data.resize(id+1,x);
}
// get pointer to the sample in question
ProfileSample *x = &profile_data[id];
if(0 == x->profile_instances++) {
// this is the first time the profile was started
x->accumulator = 0;
x->children_sample_time = 0;
x->name = name; // copy the name
x->open_profiles = 0;
}
QueryPerformanceCounter((LARGE_INTEGER *)&x->start_time);
x->open_profiles++;
assert(1 == x->open_profiles);
this->data = x;
}
CProfile::~CProfile() {
// get back pointer to sample data
ProfileSample *x = this->data;
LONGLONG profile_time;
QueryPerformanceCounter((LARGE_INTEGER *)&profile_time);
profile_time -= x->start_time;
x->open_profiles--;
if(1 == x->profile_instances) {
// this is the first time the profile was being used,
// so figure out our parentage
x->parent = 0-1;
x->num_parents = 0;
// count all parents and find the immediate parent
for(VCTR_PROFILESAMPLE::iterator inner = profile_data.begin();
inner != profile_data.end(); inner++) {
if(inner->open_profiles > 0) {
x->num_parents++;
if((DWORD)-1 == x->parent || inner->start_time
>= profile_data[x->parent].start_time) {
x->parent = inner - profile_data.begin();
}
}
}
}
if((DWORD)-1 != x->parent) {
profile_data[x->parent].children_sample_time += profile_time;
}
// save sample time in accumulater
x->accumulator += profile_time;
}
void InitializeProfiler(DWORD default_size) {
profile_data.clear();
ProfileSample x;
x.profile_instances = 0;
x.min = x.max = x.ave = INVALID_PERCENTAGE;
profile_data.resize(default_size,x);
LONGLONG timer_frequency_longlong;
QueryPerformanceFrequency((LARGE_INTEGER *)&timer_frequency_longlong);
timer_frequency = (float)timer_frequency_longlong;
}
inline void addstring(vector<string>& rows, const char *str) {
// appends a string to the end of a vector
rows.insert(rows.end(),string(str));
}
void GetProfileData(vector<string>& text_rows) {
text_rows.clear();
LONGLONG frame_time;
QueryPerformanceCounter((LARGE_INTEGER *)&frame_time);
frame_time -= start_frame;
float seconds_per_frame = (float)(frame_time) / timer_frequency;
text_rows.reserve(profile_data.size()+2);
addstring(text_rows, " Ave : Min : Max : # : Profile Name");
addstring(text_rows, "------------------------------------------");
for(VCTR_PROFILESAMPLE::iterator i = profile_data.begin();
i != profile_data.end(); i++) {
if(i->profile_instances > 0) {
// we have found some valid profile data
float percentage = float(i->accumulator - i->children_sample_time);
percentage /= (float)(frame_time);
percentage *= 100.0f;
if(percentage > 100.0f) {
i->profile_instances = 0;
continue;
}
// store profile in history :
if(INVALID_PERCENTAGE == i->ave) {
// first time entering into history
i->ave = i->min = i->max = percentage;
} else {
// adding more to history
float new_ratio = 0.8f * seconds_per_frame;
if(new_ratio > 1.0f) {
new_ratio = 1.0f;
}
float old_ratio = 1.0f - new_ratio;
new_ratio *= percentage;
i->ave *= old_ratio;
i->ave += new_ratio;
if(percentage > i->min) {
// we don't have a new min record, so average it in
i->min *= old_ratio;
i->min += new_ratio;
} else {
// we have a new minimum time
i->min = percentage;
}
if(percentage < i->max) {
// we don't have a new max record, so average it in
i->max *= old_ratio;
i->max += new_ratio;
} else {
// we have a new maximum time
i->max = percentage;
}
}
// end of storeing profile in history
// add all this data into a string
string data;
char buffer[16];
float *p = &i->ave;
do {
sprintf(buffer, "%5.1f", *p);
data += buffer;
data += " : ";
} while(p++ != &i->max);
sprintf(buffer, "%3d", i->profile_instances);
data += buffer;
data += " : ";
// add indentation: more for each parent it has
for(DWORD j = 0;j < i->num_parents; j++) {
data += ' ';
}
data += i->name;
addstring(text_rows,data.c_str());
i->profile_instances = 0;
} // endif is valid profile data
} // end for each profile
} // end GetProfileData()
void StartProfileFrame() {
QueryPerformanceCounter((LARGE_INTEGER *)&start_frame);
}
#endif