Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : /*
19 : * GPG3-style hierarchical profiler
20 : */
21 :
22 : #include "precompiled.h"
23 :
24 : #include "Profile.h"
25 :
26 : #include "lib/secure_crt.h"
27 : #include "lib/timer.h"
28 : #include "ps/CStr.h"
29 : #include "ps/ProfileViewer.h"
30 : #include "ps/ThreadUtil.h"
31 :
32 : #include <cstddef>
33 : #include <numeric>
34 :
35 : ///////////////////////////////////////////////////////////////////////////////////////////////
36 : // CProfileNodeTable
37 :
38 :
39 :
40 : /**
41 : * Class CProfileNodeTable: Implement ProfileViewer's AbstractProfileTable
42 : * interface in order to display profiling data in-game.
43 : */
44 : class CProfileNodeTable : public AbstractProfileTable
45 : {
46 : public:
47 : CProfileNodeTable(CProfileNode* n);
48 :
49 : // Implementation of AbstractProfileTable interface
50 : CStr GetName() override;
51 : CStr GetTitle() override;
52 : size_t GetNumberRows() override;
53 : const std::vector<ProfileColumn>& GetColumns() override;
54 :
55 : CStr GetCellText(size_t row, size_t col) override;
56 : AbstractProfileTable* GetChild(size_t row) override;
57 : bool IsHighlightRow(size_t row) override;
58 :
59 : private:
60 : /**
61 : * struct ColumnDescription: The only purpose of this helper structure
62 : * is to provide the global constructor that sets up the column
63 : * description.
64 : */
65 : struct ColumnDescription
66 : {
67 : std::vector<ProfileColumn> columns;
68 :
69 1 : ColumnDescription()
70 1 : {
71 1 : columns.push_back(ProfileColumn("Name", 230));
72 1 : columns.push_back(ProfileColumn("calls/frame", 80));
73 1 : columns.push_back(ProfileColumn("msec/frame", 80));
74 1 : columns.push_back(ProfileColumn("calls/turn", 80));
75 1 : columns.push_back(ProfileColumn("msec/turn", 80));
76 1 : }
77 : };
78 :
79 : /// The node represented by this table
80 : CProfileNode* node;
81 :
82 : /// Columns description (shared by all instances)
83 : static ColumnDescription columnDescription;
84 : };
85 :
86 : CProfileNodeTable::ColumnDescription CProfileNodeTable::columnDescription;
87 :
88 :
89 : // Constructor/Destructor
90 26 : CProfileNodeTable::CProfileNodeTable(CProfileNode* n)
91 : {
92 26 : node = n;
93 0 : }
94 :
95 : // Short name (= name of profile node)
96 0 : CStr CProfileNodeTable::GetName()
97 : {
98 0 : return node->GetName();
99 : }
100 :
101 : // Title (= explanatory text plus time totals)
102 0 : CStr CProfileNodeTable::GetTitle()
103 : {
104 0 : char buf[512];
105 0 : sprintf_s(buf, ARRAY_SIZE(buf), "Profiling Information for: %s (Time in node: %.3f msec/frame)", node->GetName(), node->GetFrameTime() * 1000.0f );
106 0 : return buf;
107 : }
108 :
109 : // Total number of children
110 0 : size_t CProfileNodeTable::GetNumberRows()
111 : {
112 0 : return node->GetChildren()->size() + node->GetScriptChildren()->size() + 1;
113 : }
114 :
115 : // Column description
116 0 : const std::vector<ProfileColumn>& CProfileNodeTable::GetColumns()
117 : {
118 0 : return columnDescription.columns;
119 : }
120 :
121 : // Retrieve cell text
122 0 : CStr CProfileNodeTable::GetCellText(size_t row, size_t col)
123 : {
124 0 : CProfileNode* child;
125 0 : size_t nrchildren = node->GetChildren()->size();
126 0 : size_t nrscriptchildren = node->GetScriptChildren()->size();
127 0 : char buf[256] = "?";
128 :
129 0 : if (row < nrchildren)
130 0 : child = (*node->GetChildren())[row];
131 0 : else if (row < nrchildren + nrscriptchildren)
132 0 : child = (*node->GetScriptChildren())[row - nrchildren];
133 0 : else if (row > nrchildren + nrscriptchildren)
134 0 : return "!bad row!";
135 : else
136 : {
137 : // "unlogged" row
138 0 : if (col == 0)
139 0 : return "unlogged";
140 0 : else if (col == 1)
141 0 : return "";
142 0 : else if (col == 4)
143 0 : return "";
144 :
145 0 : double unlogged_time_frame = node->GetFrameTime();
146 0 : double unlogged_time_turn = node->GetTurnTime();
147 : CProfileNode::const_profile_iterator it;
148 :
149 0 : for (it = node->GetChildren()->begin(); it != node->GetChildren()->end(); ++it)
150 : {
151 0 : unlogged_time_frame -= (*it)->GetFrameTime();
152 0 : unlogged_time_turn -= (*it)->GetTurnTime();
153 : }
154 0 : for (it = node->GetScriptChildren()->begin(); it != node->GetScriptChildren()->end(); ++it)
155 : {
156 0 : unlogged_time_frame -= (*it)->GetFrameTime();
157 0 : unlogged_time_turn -= (*it)->GetTurnTime();
158 : }
159 :
160 : // The root node can't easily count per-turn values (since Turn isn't called until
161 : // halfway though a frame), so just reset them the zero to prevent weird displays
162 0 : if (!node->GetParent())
163 : {
164 : unlogged_time_turn = 0.0;
165 : }
166 :
167 0 : if (col == 2)
168 0 : sprintf_s(buf, ARRAY_SIZE(buf), "%.3f", unlogged_time_frame * 1000.0f);
169 : else if (col == 4)
170 : sprintf_s(buf, ARRAY_SIZE(buf), "%.3f", unlogged_time_turn * 1000.f);
171 :
172 0 : return CStr(buf);
173 : }
174 :
175 0 : switch(col)
176 : {
177 0 : default:
178 0 : case 0:
179 0 : return child->GetName();
180 :
181 0 : case 1:
182 0 : sprintf_s(buf, ARRAY_SIZE(buf), "%.1f", child->GetFrameCalls());
183 : break;
184 0 : case 2:
185 0 : sprintf_s(buf, ARRAY_SIZE(buf), "%.3f", child->GetFrameTime() * 1000.0f);
186 : break;
187 0 : case 3:
188 0 : sprintf_s(buf, ARRAY_SIZE(buf), "%.1f", child->GetTurnCalls());
189 : break;
190 0 : case 4:
191 0 : sprintf_s(buf, ARRAY_SIZE(buf), "%.3f", child->GetTurnTime() * 1000.0f);
192 : break;
193 : }
194 0 : return CStr(buf);
195 : }
196 :
197 : // Return a pointer to the child table if the child node is expandable
198 0 : AbstractProfileTable* CProfileNodeTable::GetChild(size_t row)
199 : {
200 0 : CProfileNode* child;
201 0 : size_t nrchildren = node->GetChildren()->size();
202 0 : size_t nrscriptchildren = node->GetScriptChildren()->size();
203 :
204 0 : if (row < nrchildren)
205 0 : child = (*node->GetChildren())[row];
206 0 : else if (row < nrchildren + nrscriptchildren)
207 0 : child = (*node->GetScriptChildren())[row - nrchildren];
208 : else
209 : return 0;
210 :
211 0 : if (child->CanExpand())
212 0 : return child->display_table;
213 :
214 : return 0;
215 : }
216 :
217 : // Highlight all script nodes
218 0 : bool CProfileNodeTable::IsHighlightRow(size_t row)
219 : {
220 0 : size_t nrchildren = node->GetChildren()->size();
221 0 : size_t nrscriptchildren = node->GetScriptChildren()->size();
222 :
223 0 : return (row >= nrchildren && row < (nrchildren + nrscriptchildren));
224 : }
225 :
226 : ///////////////////////////////////////////////////////////////////////////////////////////////
227 : // CProfileNode implementation
228 :
229 :
230 : // Note: As with the GPG profiler, name is assumed to be a pointer to a constant string; only pointer equality is checked.
231 130 : CProfileNode::CProfileNode( const char* _name, CProfileNode* _parent )
232 : {
233 26 : name = _name;
234 26 : recursion = 0;
235 :
236 26 : Reset();
237 :
238 26 : parent = _parent;
239 :
240 26 : display_table = new CProfileNodeTable(this);
241 26 : }
242 :
243 26 : CProfileNode::~CProfileNode()
244 : {
245 26 : profile_iterator it;
246 50 : for( it = children.begin(); it != children.end(); ++it )
247 24 : delete( *it );
248 27 : for( it = script_children.begin(); it != script_children.end(); ++it )
249 1 : delete( *it );
250 :
251 26 : delete display_table;
252 27 : }
253 :
254 : template<typename T>
255 0 : static double average(const T& collection)
256 : {
257 0 : if (collection.empty())
258 : return 0.0;
259 0 : return std::accumulate(collection.begin(), collection.end(), 0.0) / collection.size();
260 : }
261 :
262 0 : double CProfileNode::GetFrameCalls() const
263 : {
264 0 : return average(calls_per_frame);
265 : }
266 :
267 0 : double CProfileNode::GetFrameTime() const
268 : {
269 0 : return average(time_per_frame);
270 : }
271 :
272 0 : double CProfileNode::GetTurnCalls() const
273 : {
274 0 : return average(calls_per_turn);
275 : }
276 :
277 0 : double CProfileNode::GetTurnTime() const
278 : {
279 0 : return average(time_per_turn);
280 : }
281 :
282 0 : const CProfileNode* CProfileNode::GetChild( const char* childName ) const
283 : {
284 0 : const_profile_iterator it;
285 0 : for( it = children.begin(); it != children.end(); ++it )
286 0 : if( (*it)->name == childName )
287 : return( *it );
288 :
289 : return( NULL );
290 : }
291 :
292 0 : const CProfileNode* CProfileNode::GetScriptChild( const char* childName ) const
293 : {
294 0 : const_profile_iterator it;
295 0 : for( it = script_children.begin(); it != script_children.end(); ++it )
296 0 : if( (*it)->name == childName )
297 : return( *it );
298 :
299 : return( NULL );
300 : }
301 :
302 16650 : CProfileNode* CProfileNode::GetChild( const char* childName )
303 : {
304 16650 : profile_iterator it;
305 134193 : for( it = children.begin(); it != children.end(); ++it )
306 134169 : if( (*it)->name == childName )
307 : return( *it );
308 :
309 24 : CProfileNode* newNode = new CProfileNode( childName, this );
310 24 : children.push_back( newNode );
311 : return( newNode );
312 : }
313 :
314 3 : CProfileNode* CProfileNode::GetScriptChild( const char* childName )
315 : {
316 3 : profile_iterator it;
317 3 : for( it = script_children.begin(); it != script_children.end(); ++it )
318 2 : if( (*it)->name == childName )
319 : return( *it );
320 :
321 1 : CProfileNode* newNode = new CProfileNode( childName, this );
322 1 : script_children.push_back( newNode );
323 : return( newNode );
324 : }
325 :
326 0 : bool CProfileNode::CanExpand()
327 : {
328 0 : return( !( children.empty() && script_children.empty() ) );
329 : }
330 :
331 26 : void CProfileNode::Reset()
332 : {
333 26 : calls_per_frame.clear();
334 26 : calls_per_turn.clear();
335 26 : calls_frame_current = 0;
336 26 : calls_turn_current = 0;
337 :
338 26 : time_per_frame.clear();
339 26 : time_per_turn.clear();
340 26 : time_frame_current = 0.0;
341 26 : time_turn_current = 0.0;
342 :
343 26 : profile_iterator it;
344 26 : for (it = children.begin(); it != children.end(); ++it)
345 0 : (*it)->Reset();
346 26 : for (it = script_children.begin(); it != script_children.end(); ++it)
347 0 : (*it)->Reset();
348 26 : }
349 :
350 0 : void CProfileNode::Frame()
351 : {
352 0 : calls_per_frame.push_back(calls_frame_current);
353 0 : time_per_frame.push_back(time_frame_current);
354 :
355 0 : calls_frame_current = 0;
356 0 : time_frame_current = 0.0;
357 :
358 0 : profile_iterator it;
359 0 : for (it = children.begin(); it != children.end(); ++it)
360 0 : (*it)->Frame();
361 0 : for (it = script_children.begin(); it != script_children.end(); ++it)
362 0 : (*it)->Frame();
363 0 : }
364 :
365 4613 : void CProfileNode::Turn()
366 : {
367 4613 : calls_per_turn.push_back(calls_turn_current);
368 4613 : time_per_turn.push_back(time_turn_current);
369 :
370 4613 : calls_turn_current = 0;
371 4613 : time_turn_current = 0.0;
372 :
373 4613 : profile_iterator it;
374 8866 : for (it = children.begin(); it != children.end(); ++it)
375 4253 : (*it)->Turn();
376 4793 : for (it = script_children.begin(); it != script_children.end(); ++it)
377 180 : (*it)->Turn();
378 4613 : }
379 :
380 16654 : void CProfileNode::Call()
381 : {
382 16654 : calls_frame_current++;
383 16654 : calls_turn_current++;
384 16654 : if (recursion++ == 0)
385 : {
386 16654 : start = timer_Time();
387 : }
388 16654 : }
389 :
390 16653 : bool CProfileNode::Return()
391 : {
392 16653 : if (--recursion != 0)
393 : return false;
394 :
395 16653 : double now = timer_Time();
396 16653 : time_frame_current += (now - start);
397 16653 : time_turn_current += (now - start);
398 16653 : return true;
399 : }
400 :
401 1 : CProfileManager::CProfileManager() :
402 1 : root(NULL), current(NULL), needs_structural_reset(false)
403 : {
404 1 : PerformStructuralReset();
405 1 : }
406 :
407 1 : CProfileManager::~CProfileManager()
408 : {
409 1 : delete root;
410 1 : }
411 :
412 16650 : void CProfileManager::Start( const char* name )
413 : {
414 16650 : if( name != current->GetName() )
415 16650 : current = current->GetChild( name );
416 16650 : current->Call();
417 16650 : }
418 :
419 3 : void CProfileManager::StartScript( const char* name )
420 : {
421 3 : if( name != current->GetName() )
422 3 : current = current->GetScriptChild( name );
423 3 : current->Call();
424 3 : }
425 :
426 16653 : void CProfileManager::Stop()
427 : {
428 16653 : if (current->Return())
429 16653 : current = current->GetParent();
430 6 : }
431 :
432 0 : void CProfileManager::Reset()
433 : {
434 0 : root->Reset();
435 0 : }
436 :
437 0 : void CProfileManager::Frame()
438 : {
439 0 : root->time_frame_current += (timer_Time() - root->start);
440 :
441 0 : root->Frame();
442 :
443 0 : if (needs_structural_reset)
444 : {
445 0 : PerformStructuralReset();
446 0 : needs_structural_reset = false;
447 : }
448 :
449 0 : root->start = timer_Time();
450 0 : }
451 :
452 180 : void CProfileManager::Turn()
453 : {
454 180 : root->Turn();
455 180 : }
456 :
457 6 : void CProfileManager::StructuralReset()
458 : {
459 : // We can't immediately perform the reset, because we're probably already
460 : // nested inside the profile tree and it will get very confused if we delete
461 : // the tree when we're not currently at the root.
462 : // So just set a flag to perform the reset at the end of the frame.
463 :
464 6 : needs_structural_reset = true;
465 6 : }
466 :
467 1 : void CProfileManager::PerformStructuralReset()
468 : {
469 1 : delete root;
470 1 : root = new CProfileNode("root", NULL);
471 1 : root->Call();
472 1 : current = root;
473 1 : g_ProfileViewer.AddRootTable(root->display_table, true);
474 1 : }
475 :
476 19837 : CProfileSample::CProfileSample(const char* name)
477 : {
478 19837 : if (CProfileManager::IsInitialised())
479 : {
480 : // The profiler is only safe to use on the main thread
481 16647 : if(Threading::IsMainThread())
482 16647 : g_Profiler.Start(name);
483 : }
484 19837 : }
485 :
486 19837 : CProfileSample::~CProfileSample()
487 : {
488 19837 : if (CProfileManager::IsInitialised())
489 16647 : if(Threading::IsMainThread())
490 16647 : g_Profiler.Stop();
491 19837 : }
|