Debian Codegraph @

src/base/castletimeutils_processtimer.inc

raw
1 {%MainUnit castletimeutils.pas}
2 {
3 Copyright 2000-2022 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Part of CastleTimeUtils unit: ProcessTimer, measuring CPU usage of this process. }
18
19 {$ifdef read_interface}
20
21 type
22 { Current time from @link(ProcessTimer).
23 If possible, this measures only the CPU usage local to this process. }
24 TProcessTimerResult = record
25 private
26 Value:
27 {$if defined(MSWINDOWS) or defined(CASTLE_NINTENDO_SWITCH)}
28 QWord // ProcessTimer just does CastleGetTickCount64
29 {$else}
30 clock_t // Unix
31 {$endif};
32 public
33 { Seconds passed since this time sample up to now.
34 Equivalent to @code(ProcessTimerSeconds(ProcessTimer, Self)) }
35 function ElapsedTime: TFloatTime;
36 end;
37
38 { Current time, local to this process.
39 Use this to measure and compare the time it takes your program to do
40 some calculations.
41
42 If possible, this measures only the CPU usage of this process.
43 So it ignores delays caused by other processes doing something on your system,
44 and it ignores things like waiting for hard disk (I/O).
45 This is possible on Unix thanks to the @code(clock) API,
46 see http://www.gnu.org/software/libc/manual/html_node/Processor-And-CPU-Time.html .
47 On other platforms (like Windows), this simply measures real time that passed.
48
49 You usually take two ProcessTimer values,
50 subtract them with @link(ProcessTimerSeconds),
51 and this is the time that passed -- in seconds. Like this:
52
53 @longCode(#
54 var
55 TimeStart: TProcessTimerResult;
56 Seconds: TFloatTime;
57 begin
58 TimeStart := ProcessTimer;
59 // ... do something time-consuming ...
60 Seconds := TimeStart.ElapsedTime;
61 // or: Seconds := ProcessTimerSeconds(ProcessTimer, TimeStart);
62 WritelnLog('Seconds passed (in this process): %f', [Seconds]);
63 end;
64 #)
65 }
66 function ProcessTimer: TProcessTimerResult;
67
68 function ProcessTimerNow: TProcessTimerResult; deprecated 'use ProcessTimer';
69
70 { Subtract two times obtained from @link(ProcessTimer),
71 A-B, return a difference in seconds. }
72 function ProcessTimerSeconds(const a, b: TProcessTimerResult): TFloatTime;
73
74 { Simple measure of process CPU time. Call ProcessTimerBegin at the beginning
75 of your calculation, call ProcessTimerEnd at the end. ProcessTimerEnd
76 returns a float number, with 1.0 being one second.
77
78 Note that using this is unsafe in libraries, not to mention multi-threaded
79 programs (it's not "reentrant") --- you risk that some other code
80 called ProcessTimerBegin, and your ProcessTimerEnd doesn't measure
81 what you think. So in general units, do not use this, use @link(ProcessTimer)
82 and @link(ProcessTimerSeconds) instead.
83
84 @groupBegin }
85 procedure ProcessTimerBegin; deprecated 'instead of this, better to use a local variable, and ProcessTimer and ProcessTimerSeconds';
86 function ProcessTimerEnd: TFloatTime; deprecated 'instead of this, better to use a local variable, and ProcessTimer and ProcessTimerSeconds';
87 { @groupEnd }
88
89 {$endif read_interface}
90
91 {$ifdef read_implementation}
92
93 function ProcessTimer: TProcessTimerResult;
94 {$if defined(MSWINDOWS) or defined(CASTLE_NINTENDO_SWITCH)}
95 begin
96 Result.Value := CastleGetTickCount64;
97 {$else}
98 // other Unixes
99 var
100 Dummy: tms;
101 begin
102 { See console.tests/test_times/RESULTS,
103 it seems that (at least on my Linux? Debian, Linux 2.4.20, libc-2.3.2)
104 the only reliable way is to use return value from times (from Libc or FpTimes).
105 tms.tms_utime, tms.tms_stime, clock() values are nonsense!
106 This is not FPC bug as I tested this with C program too. }
107
108 Result.Value := {$ifdef FPC} FpTimes {$else} Times {$endif} (Dummy);
109 {$endif}
110 end;
111
112 function ProcessTimerNow: TProcessTimerResult;
113 begin
114 Result := ProcessTimer;
115 end;
116
117 function ProcessTimerSeconds(const a, b: TProcessTimerResult): TFloatTime;
118 const
119 { Resolution of the timer used by @link(ProcessTimer). }
120 ProcessTimersPerSec =
121 {$if defined(MSWINDOWS) or defined(CASTLE_NINTENDO_SWITCH)}
122 1000 // miliseconds from CastleGetTickCount64
123 {$else}
124 { What is the frequency of FpTimes ?
125 sysconf (_SC_CLK_TCK) ?
126 Or does sysconf exist only in Libc ? }
127 { Values below were choosen experimentally for Linux and FreeBSD
128 (and I know that on most UNIXes it should be 128, that's
129 a traditional value) }
130 {$ifdef LINUX} 100 {$else}
131 {$ifdef DARWIN}
132 { In /usr/include/ppc/_limits.h and
133 /usr/include/i386/_limits.h
134 __DARWIN_CLK_TCK is defined to 100. }
135 100
136 {$else}
137 128
138 {$endif}
139 {$endif}
140 {$endif};
141 begin
142 {$I norqcheckbegin.inc}
143 Result := (A.Value - B.Value) / ProcessTimersPerSec;
144 {$I norqcheckend.inc}
145 end;
146
147 var
148 LastProcessTimerBegin: TProcessTimerResult;
149
150 procedure ProcessTimerBegin;
151 begin
152 LastProcessTimerBegin := ProcessTimer;
153 end;
154
155 function ProcessTimerEnd: TFloatTime;
156 begin
157 Result := ProcessTimerSeconds(ProcessTimer, LastProcessTimerBegin);
158 end;
159
160 function TProcessTimerResult.ElapsedTime: TFloatTime;
161 begin
162 Result := ProcessTimerSeconds(ProcessTimer, Self);
163 end;
164
165 {$endif read_implementation}