5e6212b921b5b7ddc04784ad9d601503b3d93483
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 {$IFDEF USE_SDL}
25 SDL,
26 {$ENDIF}
27 {$IFDEF USE_SDL2}
28 SDL2,
29 {$ENDIF}
30 SysUtils;
32 {$DEFINE STOPWATCH_IS_HERE}
34 {$ELSE}
35 uses
36 SysUtils
37 {$IF DEFINED(LINUX) OR DEFINED(ANDROID)}
38 {$DEFINE STOPWATCH_IS_HERE}
40 {$ELSEIF DEFINED(WINDOWS)}
41 {$DEFINE STOPWATCH_IS_HERE}
42 , Windows
43 {$ELSEIF DEFINED(HAIKU)}
44 {$DEFINE STOPWATCH_IS_HERE}
45 , unixtype
46 {$ELSE}
47 {$IFDEF STOPWATCH_IS_HERE}
48 {$UNDEF STOPWATCH_IS_HERE}
49 {$ENDIF}
50 {$WARNING You suck!}
51 {$ENDIF}
52 ;
55 {$IFDEF USE_SDL}
56 type
58 {$ENDIF}
60 {$IF DEFINED(STOPWATCH_IS_HERE)}
61 type
63 strict private
68 strict private
74 public
78 public
82 // the following is like start/stop, but doesn't reset elapsed time
90 {$ENDIF}
93 const
96 type
98 private
99 //const FilterFadeoff = 0.05; // 5%
101 private
104 //curval: Single;
110 private
116 public
127 private
128 {$IF DEFINED(STOPWATCH_IS_HERE)}
129 type
138 var
143 {$ENDIF}
145 public
150 public
154 // call this on frame start
156 // call this on frame end
162 // this will reuse the section with the given name (if there is any); use `sectionEnd()` to end it as usual
171 implementation
173 {$IFNDEF IN_TOOLS}
174 type
176 {$ELSE}
177 {$IF DEFINED(LINUX)}
179 {$ELSE}
181 {$ENDIF}
183 var
186 {$ENDIF}
189 // ////////////////////////////////////////////////////////////////////////// //
191 {$IFDEF IN_TOOLS}
192 var
194 {$ENDIF}
195 begin
196 {$IFDEF IN_TOOLS}
198 begin
199 {$IF DEFINED(LINUX)}
200 if (clock_getres(CLOCK_MONOTONIC, @r) <> 0) then raise Exception.Create('profiler error: cannot get timer resolution');
205 {$ELSEIF DEFINED(WINDOWS)}
209 {$ENDIF}
211 {$ENDIF}
212 (* init sdl timers? *)
216 {$IF DEFINED(IN_TOOLS)}
218 var
220 begin
221 //if (mFrequency = 0) then initTimerIntr();
222 {$IF DEFINED(LINUX)}
225 {$ELSEIF DEFINED(WINDOWS)}
228 {$ENDIF}
230 (* !!!
231 {$ELSEIF DEFINED(USE_SDL)}
232 function getTimeMicro: UInt64; inline;
233 begin
234 {$WARNING use inaccurate profiling timer}
235 result := SDL_GetTicks() * 1000
236 end;
237 *)
238 {$ELSEIF DEFINED(USE_SDL2)}
240 begin
243 {$ELSE}
245 begin
246 {$WARNING use stub profiling timer}
248 {$ENDIF}
252 begin
257 // ////////////////////////////////////////////////////////////////////////// //
259 begin
265 begin
272 var
274 begin
283 begin
290 begin
297 begin
305 begin
314 begin
322 begin
328 begin
334 // ////////////////////////////////////////////////////////////////////////// //
335 procedure TProfilerBar.initialize (aHistSize: Integer); begin SetLength(history, aHistSize); hisLast := -1; curAccum := 0; curAccumCount := 0; end;
338 var
340 begin
342 if (hisLast = -1) then begin hisLast := High(history); curAccum := 0; curAccumCount := 0; for idx := 0 to High(history) do history[idx] := val; end;
343 if (curAccumCount = Length(history)) then Dec(curAccum, UInt64(history[(hisLast+1) mod Length(history)])) else Inc(curAccumCount);
348 //curval := FilterFadeoff*val+(1.0-FilterFadeoff)*curval;
352 {$IFDEF XPROFILER_SLOW_AVERAGE}
354 {$ENDIF}
355 begin
356 {$IFDEF XPROFILER_SLOW_AVERAGE}
360 {$ELSE}
361 //result := round(curval);
363 {$ENDIF}
369 begin
370 if (idx < 0) or (idx >= Length(history)) then result := 0 else result := history[(hisLast-idx+Length(history)*2) mod Length(history)];
374 // ////////////////////////////////////////////////////////////////////////// //
376 begin
382 {$IF DEFINED(STOPWATCH_IS_HERE)}
387 {$ENDIF}
392 var
394 begin
397 {$IF DEFINED(STOPWATCH_IS_HERE)}
399 {$ENDIF}
405 begin
406 {$IF DEFINED(STOPWATCH_IS_HERE)}
411 {$ENDIF}
415 {$IF DEFINED(STOPWATCH_IS_HERE)}
416 var
421 var
423 begin
425 begin
427 begin
435 {$ENDIF}
436 begin
437 {$IF DEFINED(STOPWATCH_IS_HERE)}
441 begin
442 // first time?
444 begin
445 //if (length(bars) <> 0) then raise Exception.Create('FUUUUUUUUUUUUUUU');
448 begin
457 // update bars
460 begin
464 //bars[0].update(xptimer.elapsedMicro);
466 end
467 else
468 begin
470 begin
478 {$ENDIF}
482 {$IF DEFINED(STOPWATCH_IS_HERE)}
483 var
486 {$ENDIF}
487 begin
488 {$IF DEFINED(STOPWATCH_IS_HERE)}
498 // calculate level
502 {$ENDIF}
506 {$IF DEFINED(STOPWATCH_IS_HERE)}
507 var
509 {$ENDIF}
510 begin
511 {$IF DEFINED(STOPWATCH_IS_HERE)}
514 begin
516 begin
518 begin
520 if (xpsecs[idx].prevAct <> -1) then raise Exception.Create('profiler error(1): double resume: "'+aName+'"');
524 exit;
529 {$ENDIF}
533 {$IF DEFINED(STOPWATCH_IS_HERE)}
534 var
536 {$ENDIF}
537 begin
538 {$IF DEFINED(STOPWATCH_IS_HERE)}
543 // go back to parent
546 {$ENDIF}
550 begin