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,
25 {$IF DEFINED(LINUX)}
26 {$DEFINE STOPWATCH_IS_HERE}
27 unixtype, linux
28 {$ELSEIF DEFINED(WINDOWS)}
29 {$DEFINE STOPWATCH_IS_HERE}
30 Windows
31 {$ELSE}
32 {$IFDEF STOPWATCH_IS_HERE}
33 {$UNDEF STOPWATCH_IS_HERE}
34 {$ENDIF}
35 {$WARNING You suck!}
36 {$ENDIF}
37 ;
39 {$IF DEFINED(STOPWATCH_IS_HERE)}
40 type
42 strict private
47 strict private
53 public
57 public
61 // the following is like start/stop, but doesn't reset elapsed time
69 {$ENDIF}
72 const
75 type
77 private
78 //const FilterFadeoff = 0.05; // 5%
80 private
83 //curval: Single;
89 private
95 public
106 private
107 {$IF DEFINED(STOPWATCH_IS_HERE)}
108 type
117 var
122 {$ENDIF}
124 public
129 public
133 // call this on frame start
135 // call this on frame end
141 // this will reuse the section with the given name (if there is any); use `sectionEnd()` to end it as usual
146 implementation
148 {$IF DEFINED(LINUX)}
150 {$ELSE}
152 {$ENDIF}
154 var
159 // ////////////////////////////////////////////////////////////////////////// //
161 var
163 begin
165 begin
166 {$IF DEFINED(LINUX)}
167 if (clock_getres(CLOCK_MONOTONIC, @r) <> 0) then raise Exception.Create('profiler error: cannot get timer resolution');
172 {$ELSE}
176 {$ENDIF}
182 var
184 begin
186 {$IF DEFINED(LINUX)}
189 {$ELSE}
192 {$ENDIF}
196 // ////////////////////////////////////////////////////////////////////////// //
198 begin
204 begin
211 var
213 begin
222 begin
229 begin
236 begin
244 begin
253 begin
261 begin
267 begin
273 // ////////////////////////////////////////////////////////////////////////// //
274 procedure TProfilerBar.initialize (aHistSize: Integer); begin SetLength(history, aHistSize); hisLast := -1; curAccum := 0; curAccumCount := 0; end;
277 var
279 begin
281 if (hisLast = -1) then begin hisLast := High(history); curAccum := 0; curAccumCount := 0; for idx := 0 to High(history) do history[idx] := val; end;
282 if (curAccumCount = Length(history)) then Dec(curAccum, UInt64(history[(hisLast+1) mod Length(history)])) else Inc(curAccumCount);
287 //curval := FilterFadeoff*val+(1.0-FilterFadeoff)*curval;
291 {$IFDEF XPROFILER_SLOW_AVERAGE}
293 {$ENDIF}
294 begin
295 {$IFDEF XPROFILER_SLOW_AVERAGE}
299 {$ELSE}
300 //result := round(curval);
302 {$ENDIF}
308 begin
309 if (idx < 0) or (idx >= Length(history)) then result := 0 else result := history[(hisLast-idx+Length(history)*2) mod Length(history)];
313 // ////////////////////////////////////////////////////////////////////////// //
315 begin
321 {$IF DEFINED(STOPWATCH_IS_HERE)}
326 {$ENDIF}
331 var
333 begin
336 {$IF DEFINED(STOPWATCH_IS_HERE)}
338 {$ENDIF}
344 begin
345 {$IF DEFINED(STOPWATCH_IS_HERE)}
350 {$ENDIF}
354 {$IF DEFINED(STOPWATCH_IS_HERE)}
355 var
360 var
362 begin
364 begin
366 begin
374 {$ENDIF}
375 begin
376 {$IF DEFINED(STOPWATCH_IS_HERE)}
380 begin
381 // first time?
383 begin
384 //if (length(bars) <> 0) then raise Exception.Create('FUUUUUUUUUUUUUUU');
387 begin
396 // update bars
399 begin
403 //bars[0].update(xptimer.elapsedMicro);
405 end
406 else
407 begin
409 begin
417 {$ENDIF}
421 {$IF DEFINED(STOPWATCH_IS_HERE)}
422 var
425 {$ENDIF}
426 begin
427 {$IF DEFINED(STOPWATCH_IS_HERE)}
437 // calculate level
441 {$ENDIF}
445 {$IF DEFINED(STOPWATCH_IS_HERE)}
446 var
448 {$ENDIF}
449 begin
450 {$IF DEFINED(STOPWATCH_IS_HERE)}
453 begin
455 begin
457 begin
459 if (xpsecs[idx].prevAct <> -1) then raise Exception.Create('profiler error(1): dobule resume: "'+name+'"');
463 exit;
468 {$ENDIF}
472 {$IF DEFINED(STOPWATCH_IS_HERE)}
473 var
475 {$ENDIF}
476 begin
477 {$IF DEFINED(STOPWATCH_IS_HERE)}
482 // go back to parent
485 {$ENDIF}