083704863848142f8e7f8342f3c57a260d1e9959
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}
20 interface
22 uses
23 SysUtils,
24 {$IF DEFINED(LINUX)}
25 {$DEFINE STOPWATCH_IS_HERE}
26 unixtype, linux
27 {$ELSEIF DEFINED(WINDOWS)}
28 {$DEFINE STOPWATCH_IS_HERE}
29 Windows
30 {$ELSE}
31 {$IFDEF STOPWATCH_IS_HERE}
32 {$UNDEF STOPWATCH_IS_HERE}
33 {$ENDIF}
34 {$WARNING You suck!}
35 {$ENDIF}
36 ;
38 {$IF DEFINED(STOPWATCH_IS_HERE)}
39 type
41 strict private
46 strict private
52 public
56 public
60 // the following is like start/stop, but doesn't reset elapsed time
68 {$ENDIF}
71 const
74 type
76 private
79 private
86 private
92 public
103 private
104 {$IF DEFINED(STOPWATCH_IS_HERE)}
105 type
114 var
119 {$ENDIF}
121 public
125 public
129 // call this on frame start
131 // call this on frame end
137 // this will reuse the section with the given name (if there is any); use `sectionEnd()` to end it as usual
142 implementation
144 {$IF DEFINED(LINUX)}
146 {$ELSE}
148 {$ENDIF}
150 var
155 // ////////////////////////////////////////////////////////////////////////// //
157 var
159 begin
161 begin
162 {$IF DEFINED(LINUX)}
163 if (clock_getres(CLOCK_MONOTONIC, @r) <> 0) then raise Exception.Create('profiler error: cannot get timer resolution');
168 {$ELSE}
172 {$ENDIF}
178 var
180 begin
182 {$IF DEFINED(LINUX)}
185 {$ELSE}
188 {$ENDIF}
192 // ////////////////////////////////////////////////////////////////////////// //
194 begin
200 begin
207 var
209 begin
218 begin
225 begin
232 begin
240 begin
249 begin
257 begin
263 begin
269 // ////////////////////////////////////////////////////////////////////////// //
273 var
275 begin
277 if (hisHead = -1) then begin hisHead := 0; curval := 0; for idx := 0 to TProfHistorySize-1 do history[idx] := val; end;
285 //var idx: Integer;
286 begin
288 {
289 result := 0;
290 for idx := 0 to TProfHistorySize-1 do Inc(result, history[idx]);
291 result := result div TProfHistorySize;
292 }
298 begin
299 if (idx < 0) or (idx >= TProfHistorySize) then result := 0 else result := history[(hisHead-idx-1+TProfHistorySize*2) mod TProfHistorySize];
303 // ////////////////////////////////////////////////////////////////////////// //
305 begin
308 {$IF DEFINED(STOPWATCH_IS_HERE)}
313 {$ENDIF}
318 begin
320 {$IF DEFINED(STOPWATCH_IS_HERE)}
322 {$ENDIF}
328 begin
329 {$IF DEFINED(STOPWATCH_IS_HERE)}
334 {$ENDIF}
338 {$IF DEFINED(STOPWATCH_IS_HERE)}
339 var
344 var
346 begin
348 begin
350 begin
358 {$ENDIF}
359 begin
360 {$IF DEFINED(STOPWATCH_IS_HERE)}
364 begin
365 // first time?
367 begin
368 //if (length(bars) <> 0) then raise Exception.Create('FUUUUUUUUUUUUUUU');
371 begin
380 // update bars
383 begin
387 //bars[0].update(xptimer.elapsedMicro);
389 end
390 else
391 begin
393 begin
401 {$ENDIF}
405 {$IF DEFINED(STOPWATCH_IS_HERE)}
406 var
409 {$ENDIF}
410 begin
411 {$IF DEFINED(STOPWATCH_IS_HERE)}
421 // calculate level
425 {$ENDIF}
429 {$IF DEFINED(STOPWATCH_IS_HERE)}
430 var
432 {$ENDIF}
433 begin
434 {$IF DEFINED(STOPWATCH_IS_HERE)}
437 begin
439 begin
441 begin
443 if (xpsecs[idx].prevAct <> -1) then raise Exception.Create('profiler error(1): dobule resume: "'+name+'"');
447 exit;
452 {$ENDIF}
456 {$IF DEFINED(STOPWATCH_IS_HERE)}
457 var
459 {$ENDIF}
460 begin
461 {$IF DEFINED(STOPWATCH_IS_HERE)}
466 // go back to parent
469 {$ENDIF}