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
150 implementation
152 {$IF DEFINED(LINUX)}
154 {$ELSE}
156 {$ENDIF}
158 var
163 // ////////////////////////////////////////////////////////////////////////// //
165 var
167 begin
169 begin
170 {$IF DEFINED(LINUX)}
171 if (clock_getres(CLOCK_MONOTONIC, @r) <> 0) then raise Exception.Create('profiler error: cannot get timer resolution');
176 {$ELSE}
180 {$ENDIF}
186 var
188 begin
189 //if (mFrequency = 0) then initTimerIntr();
190 {$IF DEFINED(LINUX)}
193 {$ELSE}
196 {$ENDIF}
201 begin
206 // ////////////////////////////////////////////////////////////////////////// //
208 begin
214 begin
221 var
223 begin
232 begin
239 begin
246 begin
254 begin
263 begin
271 begin
277 begin
283 // ////////////////////////////////////////////////////////////////////////// //
284 procedure TProfilerBar.initialize (aHistSize: Integer); begin SetLength(history, aHistSize); hisLast := -1; curAccum := 0; curAccumCount := 0; end;
287 var
289 begin
291 if (hisLast = -1) then begin hisLast := High(history); curAccum := 0; curAccumCount := 0; for idx := 0 to High(history) do history[idx] := val; end;
292 if (curAccumCount = Length(history)) then Dec(curAccum, UInt64(history[(hisLast+1) mod Length(history)])) else Inc(curAccumCount);
297 //curval := FilterFadeoff*val+(1.0-FilterFadeoff)*curval;
301 {$IFDEF XPROFILER_SLOW_AVERAGE}
303 {$ENDIF}
304 begin
305 {$IFDEF XPROFILER_SLOW_AVERAGE}
309 {$ELSE}
310 //result := round(curval);
312 {$ENDIF}
318 begin
319 if (idx < 0) or (idx >= Length(history)) then result := 0 else result := history[(hisLast-idx+Length(history)*2) mod Length(history)];
323 // ////////////////////////////////////////////////////////////////////////// //
325 begin
331 {$IF DEFINED(STOPWATCH_IS_HERE)}
336 {$ENDIF}
341 var
343 begin
346 {$IF DEFINED(STOPWATCH_IS_HERE)}
348 {$ENDIF}
354 begin
355 {$IF DEFINED(STOPWATCH_IS_HERE)}
360 {$ENDIF}
364 {$IF DEFINED(STOPWATCH_IS_HERE)}
365 var
370 var
372 begin
374 begin
376 begin
384 {$ENDIF}
385 begin
386 {$IF DEFINED(STOPWATCH_IS_HERE)}
390 begin
391 // first time?
393 begin
394 //if (length(bars) <> 0) then raise Exception.Create('FUUUUUUUUUUUUUUU');
397 begin
406 // update bars
409 begin
413 //bars[0].update(xptimer.elapsedMicro);
415 end
416 else
417 begin
419 begin
427 {$ENDIF}
431 {$IF DEFINED(STOPWATCH_IS_HERE)}
432 var
435 {$ENDIF}
436 begin
437 {$IF DEFINED(STOPWATCH_IS_HERE)}
447 // calculate level
451 {$ENDIF}
455 {$IF DEFINED(STOPWATCH_IS_HERE)}
456 var
458 {$ENDIF}
459 begin
460 {$IF DEFINED(STOPWATCH_IS_HERE)}
463 begin
465 begin
467 begin
469 if (xpsecs[idx].prevAct <> -1) then raise Exception.Create('profiler error(1): dobule resume: "'+aName+'"');
473 exit;
478 {$ENDIF}
482 {$IF DEFINED(STOPWATCH_IS_HERE)}
483 var
485 {$ENDIF}
486 begin
487 {$IF DEFINED(STOPWATCH_IS_HERE)}
492 // go back to parent
495 {$ENDIF}
499 begin