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 uses
24 SysUtils;
26 {$DEFINE STOPWATCH_IS_HERE}
28 {$IF DEFINED(STOPWATCH_IS_HERE)}
29 type
31 strict private
36 strict private
42 public
46 public
50 // the following is like start/stop, but doesn't reset elapsed time
58 {$ENDIF}
61 const
64 type
66 private
67 //const FilterFadeoff = 0.05; // 5%
69 private
72 //curval: Single;
78 private
84 public
95 private
96 {$IF DEFINED(STOPWATCH_IS_HERE)}
97 type
106 var
111 {$ENDIF}
113 public
118 public
122 // call this on frame start
124 // call this on frame end
130 // this will reuse the section with the given name (if there is any); use `sectionEnd()` to end it as usual
139 implementation
141 uses
142 SDL2;
144 type
147 // ////////////////////////////////////////////////////////////////////////// //
149 begin
150 (* init sdl timers? *)
155 begin
161 begin
166 // ////////////////////////////////////////////////////////////////////////// //
168 begin
174 begin
181 var
183 begin
192 begin
199 begin
206 begin
214 begin
223 begin
231 begin
237 begin
243 // ////////////////////////////////////////////////////////////////////////// //
244 procedure TProfilerBar.initialize (aHistSize: Integer); begin SetLength(history, aHistSize); hisLast := -1; curAccum := 0; curAccumCount := 0; end;
247 var
249 begin
251 if (hisLast = -1) then begin hisLast := High(history); curAccum := 0; curAccumCount := 0; for idx := 0 to High(history) do history[idx] := val; end;
252 if (curAccumCount = Length(history)) then Dec(curAccum, UInt64(history[(hisLast+1) mod Length(history)])) else Inc(curAccumCount);
257 //curval := FilterFadeoff*val+(1.0-FilterFadeoff)*curval;
261 {$IFDEF XPROFILER_SLOW_AVERAGE}
263 {$ENDIF}
264 begin
265 {$IFDEF XPROFILER_SLOW_AVERAGE}
269 {$ELSE}
270 //result := round(curval);
272 {$ENDIF}
278 begin
279 if (idx < 0) or (idx >= Length(history)) then result := 0 else result := history[(hisLast-idx+Length(history)*2) mod Length(history)];
283 // ////////////////////////////////////////////////////////////////////////// //
285 begin
291 {$IF DEFINED(STOPWATCH_IS_HERE)}
296 {$ENDIF}
301 var
303 begin
306 {$IF DEFINED(STOPWATCH_IS_HERE)}
308 {$ENDIF}
314 begin
315 {$IF DEFINED(STOPWATCH_IS_HERE)}
320 {$ENDIF}
324 {$IF DEFINED(STOPWATCH_IS_HERE)}
325 var
330 var
332 begin
334 begin
336 begin
344 {$ENDIF}
345 begin
346 {$IF DEFINED(STOPWATCH_IS_HERE)}
350 begin
351 // first time?
353 begin
354 //if (length(bars) <> 0) then raise Exception.Create('FUUUUUUUUUUUUUUU');
357 begin
366 // update bars
369 begin
373 //bars[0].update(xptimer.elapsedMicro);
375 end
376 else
377 begin
379 begin
387 {$ENDIF}
391 {$IF DEFINED(STOPWATCH_IS_HERE)}
392 var
395 {$ENDIF}
396 begin
397 {$IF DEFINED(STOPWATCH_IS_HERE)}
407 // calculate level
411 {$ENDIF}
415 {$IF DEFINED(STOPWATCH_IS_HERE)}
416 var
418 {$ENDIF}
419 begin
420 {$IF DEFINED(STOPWATCH_IS_HERE)}
423 begin
425 begin
427 begin
429 if (xpsecs[idx].prevAct <> -1) then raise Exception.Create('profiler error(1): double resume: "'+aName+'"');
433 exit;
438 {$ENDIF}
442 {$IF DEFINED(STOPWATCH_IS_HERE)}
443 var
445 {$ENDIF}
446 begin
447 {$IF DEFINED(STOPWATCH_IS_HERE)}
452 // go back to parent
455 {$ENDIF}
459 begin