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
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
128 public
132 // call this on frame start
134 // call this on frame end
140 // this will reuse the section with the given name (if there is any); use `sectionEnd()` to end it as usual
145 implementation
147 {$IF DEFINED(LINUX)}
149 {$ELSE}
151 {$ENDIF}
153 var
158 // ////////////////////////////////////////////////////////////////////////// //
160 var
162 begin
164 begin
165 {$IF DEFINED(LINUX)}
166 if (clock_getres(CLOCK_MONOTONIC, @r) <> 0) then raise Exception.Create('profiler error: cannot get timer resolution');
171 {$ELSE}
175 {$ENDIF}
181 var
183 begin
185 {$IF DEFINED(LINUX)}
188 {$ELSE}
191 {$ENDIF}
195 // ////////////////////////////////////////////////////////////////////////// //
197 begin
203 begin
210 var
212 begin
221 begin
228 begin
235 begin
243 begin
252 begin
260 begin
266 begin
272 // ////////////////////////////////////////////////////////////////////////// //
273 procedure TProfilerBar.initialize (); begin hisLast := -1; curAccum := 0; curAccumCount := 0; end;
276 var
278 begin
280 if (hisLast = -1) then begin hisLast := TProfHistorySize-1; curAccum := 0; curAccumCount := 0; for idx := 0 to TProfHistorySize-1 do history[idx] := val; end;
281 if (curAccumCount = TProfHistorySize) then Dec(curAccum, UInt64(history[(hisLast+1) mod TProfHistorySize])) else Inc(curAccumCount);
286 //curval := FilterFadeoff*val+(1.0-FilterFadeoff)*curval;
290 {$IFDEF XPROFILER_SLOW_AVERAGE}
292 {$ENDIF}
293 begin
294 {$IFDEF XPROFILER_SLOW_AVERAGE}
298 {$ELSE}
299 //result := round(curval);
301 {$ENDIF}
307 begin
308 if (idx < 0) or (idx >= TProfHistorySize) then result := 0 else result := history[(hisLast-idx+TProfHistorySize*2) mod TProfHistorySize];
312 // ////////////////////////////////////////////////////////////////////////// //
314 begin
317 {$IF DEFINED(STOPWATCH_IS_HERE)}
322 {$ENDIF}
327 begin
329 {$IF DEFINED(STOPWATCH_IS_HERE)}
331 {$ENDIF}
337 begin
338 {$IF DEFINED(STOPWATCH_IS_HERE)}
343 {$ENDIF}
347 {$IF DEFINED(STOPWATCH_IS_HERE)}
348 var
353 var
355 begin
357 begin
359 begin
367 {$ENDIF}
368 begin
369 {$IF DEFINED(STOPWATCH_IS_HERE)}
373 begin
374 // first time?
376 begin
377 //if (length(bars) <> 0) then raise Exception.Create('FUUUUUUUUUUUUUUU');
380 begin
389 // update bars
392 begin
396 //bars[0].update(xptimer.elapsedMicro);
398 end
399 else
400 begin
402 begin
410 {$ENDIF}
414 {$IF DEFINED(STOPWATCH_IS_HERE)}
415 var
418 {$ENDIF}
419 begin
420 {$IF DEFINED(STOPWATCH_IS_HERE)}
430 // calculate level
434 {$ENDIF}
438 {$IF DEFINED(STOPWATCH_IS_HERE)}
439 var
441 {$ENDIF}
442 begin
443 {$IF DEFINED(STOPWATCH_IS_HERE)}
446 begin
448 begin
450 begin
452 if (xpsecs[idx].prevAct <> -1) then raise Exception.Create('profiler error(1): dobule resume: "'+name+'"');
456 exit;
461 {$ENDIF}
465 {$IF DEFINED(STOPWATCH_IS_HERE)}
466 var
468 {$ENDIF}
469 begin
470 {$IF DEFINED(STOPWATCH_IS_HERE)}
475 // go back to parent
478 {$ENDIF}