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 SysUtils;
26 {$DEFINE STOPWATCH_IS_HERE}
28 {$ELSE}
29 uses
30 SysUtils
31 {$IF DEFINED(LINUX) OR DEFINED(ANDROID)}
32 {$DEFINE STOPWATCH_IS_HERE}
34 {$ELSEIF DEFINED(WINDOWS)}
35 {$DEFINE STOPWATCH_IS_HERE}
36 , Windows
37 {$ELSEIF DEFINED(HAIKU)}
38 {$DEFINE STOPWATCH_IS_HERE}
39 , unixtype
40 {$ELSE}
41 {$IFDEF STOPWATCH_IS_HERE}
42 {$UNDEF STOPWATCH_IS_HERE}
43 {$ENDIF}
44 {$WARNING You suck!}
45 {$ENDIF}
46 ;
49 {$IF DEFINED(STOPWATCH_IS_HERE)}
50 type
52 strict private
57 strict private
63 public
67 public
71 // the following is like start/stop, but doesn't reset elapsed time
79 {$ENDIF}
82 const
85 type
87 private
88 //const FilterFadeoff = 0.05; // 5%
90 private
93 //curval: Single;
99 private
105 public
116 private
117 {$IF DEFINED(STOPWATCH_IS_HERE)}
118 type
127 var
132 {$ENDIF}
134 public
139 public
143 // call this on frame start
145 // call this on frame end
151 // this will reuse the section with the given name (if there is any); use `sectionEnd()` to end it as usual
160 implementation
162 {$IFNDEF IN_TOOLS}
163 uses
164 SDL2;
166 type
168 {$ELSE}
169 {$IF DEFINED(LINUX)}
171 {$ELSE}
173 {$ENDIF}
175 var
178 {$ENDIF}
181 // ////////////////////////////////////////////////////////////////////////// //
183 {$IFDEF IN_TOOLS}
184 var
186 {$ENDIF}
187 begin
188 {$IFDEF IN_TOOLS}
190 begin
191 {$IF DEFINED(LINUX)}
192 if (clock_getres(CLOCK_MONOTONIC, @r) <> 0) then raise Exception.Create('profiler error: cannot get timer resolution');
197 {$ELSEIF DEFINED(WINDOWS)}
201 {$ENDIF}
203 {$ENDIF}
204 (* init sdl timers? *)
208 {$IFDEF IN_TOOLS}
210 var
212 begin
213 //if (mFrequency = 0) then initTimerIntr();
214 {$IF DEFINED(LINUX)}
217 {$ELSEIF DEFINED(WINDOWS)}
220 {$ENDIF}
222 {$ELSE}
224 begin
227 {$ENDIF}
231 begin
236 // ////////////////////////////////////////////////////////////////////////// //
238 begin
244 begin
251 var
253 begin
262 begin
269 begin
276 begin
284 begin
293 begin
301 begin
307 begin
313 // ////////////////////////////////////////////////////////////////////////// //
314 procedure TProfilerBar.initialize (aHistSize: Integer); begin SetLength(history, aHistSize); hisLast := -1; curAccum := 0; curAccumCount := 0; end;
317 var
319 begin
321 if (hisLast = -1) then begin hisLast := High(history); curAccum := 0; curAccumCount := 0; for idx := 0 to High(history) do history[idx] := val; end;
322 if (curAccumCount = Length(history)) then Dec(curAccum, UInt64(history[(hisLast+1) mod Length(history)])) else Inc(curAccumCount);
327 //curval := FilterFadeoff*val+(1.0-FilterFadeoff)*curval;
331 {$IFDEF XPROFILER_SLOW_AVERAGE}
333 {$ENDIF}
334 begin
335 {$IFDEF XPROFILER_SLOW_AVERAGE}
339 {$ELSE}
340 //result := round(curval);
342 {$ENDIF}
348 begin
349 if (idx < 0) or (idx >= Length(history)) then result := 0 else result := history[(hisLast-idx+Length(history)*2) mod Length(history)];
353 // ////////////////////////////////////////////////////////////////////////// //
355 begin
361 {$IF DEFINED(STOPWATCH_IS_HERE)}
366 {$ENDIF}
371 var
373 begin
376 {$IF DEFINED(STOPWATCH_IS_HERE)}
378 {$ENDIF}
384 begin
385 {$IF DEFINED(STOPWATCH_IS_HERE)}
390 {$ENDIF}
394 {$IF DEFINED(STOPWATCH_IS_HERE)}
395 var
400 var
402 begin
404 begin
406 begin
414 {$ENDIF}
415 begin
416 {$IF DEFINED(STOPWATCH_IS_HERE)}
420 begin
421 // first time?
423 begin
424 //if (length(bars) <> 0) then raise Exception.Create('FUUUUUUUUUUUUUUU');
427 begin
436 // update bars
439 begin
443 //bars[0].update(xptimer.elapsedMicro);
445 end
446 else
447 begin
449 begin
457 {$ENDIF}
461 {$IF DEFINED(STOPWATCH_IS_HERE)}
462 var
465 {$ENDIF}
466 begin
467 {$IF DEFINED(STOPWATCH_IS_HERE)}
477 // calculate level
481 {$ENDIF}
485 {$IF DEFINED(STOPWATCH_IS_HERE)}
486 var
488 {$ENDIF}
489 begin
490 {$IF DEFINED(STOPWATCH_IS_HERE)}
493 begin
495 begin
497 begin
499 if (xpsecs[idx].prevAct <> -1) then raise Exception.Create('profiler error(1): double resume: "'+aName+'"');
503 exit;
508 {$ENDIF}
512 {$IF DEFINED(STOPWATCH_IS_HERE)}
513 var
515 {$ENDIF}
516 begin
517 {$IF DEFINED(STOPWATCH_IS_HERE)}
522 // go back to parent
525 {$ENDIF}
529 begin