1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 // stopwatch timer to measure short periods (like frame rendering phases)
16 {$INCLUDE a_modes.inc}
17 {.$DEFINE XPROFILER_SLOW_AVERAGE}
20 interface
22 {$IFNDEF IN_TOOLS}
23 uses
24 {$IFDEF USE_SDL}
25 SDL,
26 {$ENDIF}
27 {$IFDEF USE_SDL2}
28 SDL2,
29 {$ENDIF}
30 SysUtils;
32 {$DEFINE STOPWATCH_IS_HERE}
34 {$ELSE}
35 uses
36 SysUtils
37 {$IF DEFINED(LINUX) OR DEFINED(ANDROID)}
38 {$DEFINE STOPWATCH_IS_HERE}
40 {$ELSEIF DEFINED(WINDOWS)}
41 {$DEFINE STOPWATCH_IS_HERE}
42 , Windows
43 {$ELSEIF DEFINED(HAIKU)}
44 {$DEFINE STOPWATCH_IS_HERE}
45 , unixtype
46 {$ELSE}
47 {$IFDEF STOPWATCH_IS_HERE}
48 {$UNDEF STOPWATCH_IS_HERE}
49 {$ENDIF}
50 {$WARNING You suck!}
51 {$ENDIF}
52 ;
55 {$IFDEF USE_SDL}
56 type
58 {$ENDIF}
60 {$IF DEFINED(STOPWATCH_IS_HERE)}
61 type
63 strict private
68 strict private
74 public
78 public
82 // the following is like start/stop, but doesn't reset elapsed time
90 {$ENDIF}
93 const
96 type
98 private
99 //const FilterFadeoff = 0.05; // 5%
101 private
104 //curval: Single;
110 private
116 public
127 private
128 {$IF DEFINED(STOPWATCH_IS_HERE)}
129 type
138 var
143 {$ENDIF}
145 public
150 public
154 // call this on frame start
156 // call this on frame end
162 // this will reuse the section with the given name (if there is any); use `sectionEnd()` to end it as usual
171 implementation
173 {$IFNDEF IN_TOOLS}
174 type
176 {$ELSE}
177 {$IF DEFINED(LINUX)}
179 {$ELSE}
181 {$ENDIF}
183 var
186 {$ENDIF}
189 // ////////////////////////////////////////////////////////////////////////// //
191 {$IFDEF IN_TOOLS}
192 var
194 {$ENDIF}
195 begin
196 {$IFDEF IN_TOOLS}
198 begin
199 {$IF DEFINED(LINUX)}
200 if (clock_getres(CLOCK_MONOTONIC, @r) <> 0) then raise Exception.Create('profiler error: cannot get timer resolution');
205 {$ELSEIF DEFINED(WINDOWS)}
209 {$ENDIF}
211 {$ENDIF}
212 (* init sdl timers? *)
216 {$IF DEFINED(IN_TOOLS)}
218 var
220 begin
221 //if (mFrequency = 0) then initTimerIntr();
222 {$IF DEFINED(LINUX)}
225 {$ELSEIF DEFINED(WINDOWS)}
228 {$ENDIF}
230 {$ELSEIF DEFINED(USE_SDL2)}
232 begin
235 {$ELSE}
237 begin
240 {$ENDIF}
244 begin
249 // ////////////////////////////////////////////////////////////////////////// //
251 begin
257 begin
264 var
266 begin
275 begin
282 begin
289 begin
297 begin
306 begin
314 begin
320 begin
326 // ////////////////////////////////////////////////////////////////////////// //
327 procedure TProfilerBar.initialize (aHistSize: Integer); begin SetLength(history, aHistSize); hisLast := -1; curAccum := 0; curAccumCount := 0; end;
330 var
332 begin
334 if (hisLast = -1) then begin hisLast := High(history); curAccum := 0; curAccumCount := 0; for idx := 0 to High(history) do history[idx] := val; end;
335 if (curAccumCount = Length(history)) then Dec(curAccum, UInt64(history[(hisLast+1) mod Length(history)])) else Inc(curAccumCount);
340 //curval := FilterFadeoff*val+(1.0-FilterFadeoff)*curval;
344 {$IFDEF XPROFILER_SLOW_AVERAGE}
346 {$ENDIF}
347 begin
348 {$IFDEF XPROFILER_SLOW_AVERAGE}
352 {$ELSE}
353 //result := round(curval);
355 {$ENDIF}
361 begin
362 if (idx < 0) or (idx >= Length(history)) then result := 0 else result := history[(hisLast-idx+Length(history)*2) mod Length(history)];
366 // ////////////////////////////////////////////////////////////////////////// //
368 begin
374 {$IF DEFINED(STOPWATCH_IS_HERE)}
379 {$ENDIF}
384 var
386 begin
389 {$IF DEFINED(STOPWATCH_IS_HERE)}
391 {$ENDIF}
397 begin
398 {$IF DEFINED(STOPWATCH_IS_HERE)}
403 {$ENDIF}
407 {$IF DEFINED(STOPWATCH_IS_HERE)}
408 var
413 var
415 begin
417 begin
419 begin
427 {$ENDIF}
428 begin
429 {$IF DEFINED(STOPWATCH_IS_HERE)}
433 begin
434 // first time?
436 begin
437 //if (length(bars) <> 0) then raise Exception.Create('FUUUUUUUUUUUUUUU');
440 begin
449 // update bars
452 begin
456 //bars[0].update(xptimer.elapsedMicro);
458 end
459 else
460 begin
462 begin
470 {$ENDIF}
474 {$IF DEFINED(STOPWATCH_IS_HERE)}
475 var
478 {$ENDIF}
479 begin
480 {$IF DEFINED(STOPWATCH_IS_HERE)}
490 // calculate level
494 {$ENDIF}
498 {$IF DEFINED(STOPWATCH_IS_HERE)}
499 var
501 {$ENDIF}
502 begin
503 {$IF DEFINED(STOPWATCH_IS_HERE)}
506 begin
508 begin
510 begin
512 if (xpsecs[idx].prevAct <> -1) then raise Exception.Create('profiler error(1): double resume: "'+aName+'"');
516 exit;
521 {$ENDIF}
525 {$IF DEFINED(STOPWATCH_IS_HERE)}
526 var
528 {$ENDIF}
529 begin
530 {$IF DEFINED(STOPWATCH_IS_HERE)}
535 // go back to parent
538 {$ENDIF}
542 begin