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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 // stopwatch timer to measure short periods (like frame rendering phases)
17 {$INCLUDE a_modes.inc}
18 {.$DEFINE XPROFILER_SLOW_AVERAGE}
21 interface
23 {$IFNDEF IN_TOOLS}
24 uses
25 SysUtils;
27 {$DEFINE STOPWATCH_IS_HERE}
29 {$ELSE}
30 uses
31 SysUtils
32 {$IF DEFINED(LINUX) OR DEFINED(ANDROID)}
33 {$DEFINE STOPWATCH_IS_HERE}
35 {$ELSEIF DEFINED(WINDOWS)}
36 {$DEFINE STOPWATCH_IS_HERE}
37 , Windows
38 {$ELSEIF DEFINED(HAIKU)}
39 {$DEFINE STOPWATCH_IS_HERE}
40 , unixtype
41 {$ELSE}
42 {$IFDEF STOPWATCH_IS_HERE}
43 {$UNDEF STOPWATCH_IS_HERE}
44 {$ENDIF}
45 {$WARNING You suck!}
46 {$ENDIF}
47 ;
50 {$IF DEFINED(STOPWATCH_IS_HERE)}
51 type
53 strict private
58 strict private
64 public
68 public
72 // the following is like start/stop, but doesn't reset elapsed time
80 {$ENDIF}
83 const
86 type
88 private
89 //const FilterFadeoff = 0.05; // 5%
91 private
94 //curval: Single;
100 private
106 public
117 private
118 {$IF DEFINED(STOPWATCH_IS_HERE)}
119 type
128 var
133 {$ENDIF}
135 public
140 public
144 // call this on frame start
146 // call this on frame end
152 // this will reuse the section with the given name (if there is any); use `sectionEnd()` to end it as usual
161 implementation
163 {$IFNDEF IN_TOOLS}
164 uses
165 SDL2;
167 type
169 {$ELSE}
170 {$IF DEFINED(LINUX)}
172 {$ELSE}
174 {$ENDIF}
176 var
179 {$ENDIF}
182 // ////////////////////////////////////////////////////////////////////////// //
184 {$IFDEF IN_TOOLS}
185 var
187 {$ENDIF}
188 begin
189 {$IFDEF IN_TOOLS}
191 begin
192 {$IF DEFINED(LINUX)}
193 if (clock_getres(CLOCK_MONOTONIC, @r) <> 0) then raise Exception.Create('profiler error: cannot get timer resolution');
198 {$ELSEIF DEFINED(WINDOWS)}
202 {$ENDIF}
204 {$ENDIF}
205 (* init sdl timers? *)
209 {$IFDEF IN_TOOLS}
211 var
213 begin
214 //if (mFrequency = 0) then initTimerIntr();
215 {$IF DEFINED(LINUX)}
218 {$ELSEIF DEFINED(WINDOWS)}
221 {$ENDIF}
223 {$ELSE}
225 begin
228 {$ENDIF}
232 begin
237 // ////////////////////////////////////////////////////////////////////////// //
239 begin
245 begin
252 var
254 begin
263 begin
270 begin
277 begin
285 begin
294 begin
302 begin
308 begin
314 // ////////////////////////////////////////////////////////////////////////// //
315 procedure TProfilerBar.initialize (aHistSize: Integer); begin SetLength(history, aHistSize); hisLast := -1; curAccum := 0; curAccumCount := 0; end;
318 var
320 begin
322 if (hisLast = -1) then begin hisLast := High(history); curAccum := 0; curAccumCount := 0; for idx := 0 to High(history) do history[idx] := val; end;
323 if (curAccumCount = Length(history)) then Dec(curAccum, UInt64(history[(hisLast+1) mod Length(history)])) else Inc(curAccumCount);
328 //curval := FilterFadeoff*val+(1.0-FilterFadeoff)*curval;
332 {$IFDEF XPROFILER_SLOW_AVERAGE}
334 {$ENDIF}
335 begin
336 {$IFDEF XPROFILER_SLOW_AVERAGE}
340 {$ELSE}
341 //result := round(curval);
343 {$ENDIF}
349 begin
350 if (idx < 0) or (idx >= Length(history)) then result := 0 else result := history[(hisLast-idx+Length(history)*2) mod Length(history)];
354 // ////////////////////////////////////////////////////////////////////////// //
356 begin
362 {$IF DEFINED(STOPWATCH_IS_HERE)}
367 {$ENDIF}
372 var
374 begin
377 {$IF DEFINED(STOPWATCH_IS_HERE)}
379 {$ENDIF}
385 begin
386 {$IF DEFINED(STOPWATCH_IS_HERE)}
391 {$ENDIF}
395 {$IF DEFINED(STOPWATCH_IS_HERE)}
396 var
401 var
403 begin
405 begin
407 begin
415 {$ENDIF}
416 begin
417 {$IF DEFINED(STOPWATCH_IS_HERE)}
421 begin
422 // first time?
424 begin
425 //if (length(bars) <> 0) then raise Exception.Create('FUUUUUUUUUUUUUUU');
428 begin
437 // update bars
440 begin
444 //bars[0].update(xptimer.elapsedMicro);
446 end
447 else
448 begin
450 begin
458 {$ENDIF}
462 {$IF DEFINED(STOPWATCH_IS_HERE)}
463 var
466 {$ENDIF}
467 begin
468 {$IF DEFINED(STOPWATCH_IS_HERE)}
478 // calculate level
482 {$ENDIF}
486 {$IF DEFINED(STOPWATCH_IS_HERE)}
487 var
489 {$ENDIF}
490 begin
491 {$IF DEFINED(STOPWATCH_IS_HERE)}
494 begin
496 begin
498 begin
500 if (xpsecs[idx].prevAct <> -1) then raise Exception.Create('profiler error(1): double resume: "'+aName+'"');
504 exit;
509 {$ENDIF}
513 {$IF DEFINED(STOPWATCH_IS_HERE)}
514 var
516 {$ENDIF}
517 begin
518 {$IF DEFINED(STOPWATCH_IS_HERE)}
523 // go back to parent
526 {$ENDIF}
530 begin