-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFrameClock.pas
More file actions
1460 lines (1222 loc) · 51 KB
/
FrameClock.pas
File metadata and controls
1460 lines (1222 loc) · 51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
Frame Clock
Frame clock is intended to be used primarily as a mean of measuring distance
between two points in time with high resolution. But please note that,
given the implementation, the high resolution does not automatically imply
high precission, especially on long time intervals.
It is meant to be used to measure only very short intervals (at most
minutes). But it can, of course, exists for indefinite time, while only
measuring the last frame.
Within this library, the measured point is called just that, a point.
Space between two points is called a frame (hence frame clock).
Length of a frame, or time between two points, is called a distance.
The measurement is done by obtaining values from monotonic high-resolution
or performace counters and then calculating difference between them.
The frame clock stores three such values called creation point, previous
point and current point. It also stores distance (elapsed time between)
previous and current point as a current frame.
Creation point is stored at the object creation. Previous and current
points are both initialized to the same value as creation point.
Each time method Tick is called, value from current point is moved to
previous point and current point is filled by an actual value from
preformance counter. The current frame distance is also calculated at that
point.
a frame
|
creation point |-----| previous point
| | | |
P-----P-----P-----P-----P-----P-----P-----P-----P-----P...
<---> | |
time >>> | | current point
a distance |
current frame
The distance is measured in ticks. Length of these ticks is implementation
and system dependent, so do not assume anything about them. It is just a
number that has meaning only within the object that produced it.
Do not pass these values between different instances of the frame clock!
To obtain the distance in usual units, use properties or methods designed
for that purpose (eg. CurrentFrame, TimeStampDistance, ...) - they,
in most cases, return value of type TFCTime which contains usable units.
Frame clock is using performace counter or its alternative that is available
on the current system for time measurement. If none of such clock or counter
is available, it defaults to normal time functions - these usually do not
provide resolution better than few milliseconds.
Whether the frame clock is running with high resolution can be discerned by
examining HighResolution property after instantiation.
If you force high resolution while creating an instance of frame clock, and
high-res timer is not available on the system, the constructor will raise
an EFCHighResolutionFail exception. Forcing low resolution cannot fail, it
just prevents the clock from even attempting to obtain high-res counter.
Recommended use of the frame clock should look like this:
Clock := TFrameClock.Create;
try
Clock.Tick;
// measured interval #1
Clock.Tick;
// CurrentFrame property now contains length of measured interval #1
<some_code>
Clock.Tick;
// measured interval #2
Clock.Tick;
// CurrentFrame property now contains length of measured interval #2
// measured interval #3
Clock.Tick;
// CurrentFrame property now contains length of measured interval #3
...
finally
Clock.Free;
end;
WARNING - Do continuous measurements on one instance of the frame clock
only. Never use points from previous or paralel class instances,
it is not guaranteed to work reliably. Measuring across system
reboots will straight-up fail to produce anything sensible.
Version 1.0.3 (2024-01-14)
Last change 2026-02-25
©2020-2026 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.FrameClock
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
Library AuxExceptions is required only when rebasing local exception classes
(see symbol FrameClock_UseAuxExceptions for details).
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
ListUtils - github.com/TheLazyTomcat/Lib.ListUtils
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit FrameClock;
{
FrameClock_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
FrameClock_UseAuxExceptions to achieve this.
}
{$IF Defined(FrameClock_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$ELSEIF Defined(LINUX) and Defined(FPC)}
{$DEFINE Linux}
{$ELSE}
{$MESSAGE FATAL 'Unsupported operating system.'}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
SysUtils,
AuxTypes, AuxClasses{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
type
EFCException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
EFCHighResolutionFail = class(EFCException);
EFCSystemException = class(EFCException);
EFCInvalidList = class(EFCException);
EFCIndexOutOfBounds = class(EFCException);
{===============================================================================
--------------------------------------------------------------------------------
TFrameClock
--------------------------------------------------------------------------------
===============================================================================}
type
TFCTicks = Int64;
TFCTime = record
Ticks: TFCTicks;
Sec: Double; // seconds
MiS: Double; // milliseconds
UiS: Double; // microseconds
iSec: Int64; // integral seconds
iMiS: Int64; // integral milliseconds
iUiS: Int64; // integral microseconds
end;
TFCForcedResolution = (frForceHigh,frForceLow,frDontForce);
{===============================================================================
TFrameClock - class declaration
===============================================================================}
{
A note on corrections...
Correction is a distance (in ticks, itself not corrected) between two
consecutive calls to method Tick. This distance more or less corresponds
to a time the method Tick requires to execute. It is there for instances
where it is desirable to remove overhead time from measured intervals.
When ApplyCorrection property is se to true, value stored in Correction
property is subtracted from ticks within method FillFromTicks before the
ticks are converted to normal units. If the correction is larger than the
ticks, the ticks are set to 0.
To obtain actual value for correction, call method MeasureCorrection. This
method reapeatedly measures distance between two calls to Tick and returns
an average of this time. You can change number of repeated measurements by
altering Repeats parameter - but note that setting it to large number may
cause the method to execute for a long time. Note that measuring the
correction itself does not change any clock properties.
If you want to activate correction and at the same time set it to measured
value, call method ApplyMeasuredCorrection.
By default, ApplyCorrection is set to false and Correction is set to 0.
You have to change those properties if you want to apply corrections to your
measurements.
Correction, when applied, affects all values of type TFCTime returned from
the clock, including timestamp distances and accumulators in TFrameClockEx.
Standalone functions hawe corrections disabled.
}
type
TFrameClock = class(TCustomMultiListObject)
protected
fHighResolution: Boolean;
fFrequency: Int64;
fResolution: Int64;
fFrameCounter: UInt64;
fCreationPoint: TFCTicks;
fPreviousPoint: TFCTicks;
fCurrentPoint: TFCTicks;
fCurrentFrame: TFCTime;
fApplyCorrection: Boolean;
fCorrection: TFCTicks;
//- lists methods ---
Function GetCapacity(List: Integer): Integer; override;
procedure SetCapacity(List,Value: Integer); override;
Function GetCount(List: Integer): Integer; override;
procedure SetCount(List,Value: Integer); override;
//- other protected methods ---
procedure InitializeTime(LowResOnly: Boolean); virtual;
procedure Initialize(ForcedResolution: TFCForcedResolution); virtual;
procedure Finalize; virtual;
public
constructor Create(ForcedResolution: TFCForcedResolution = frDontForce);
destructor Destroy; override;
Function LowIndex(List: Integer): Integer; override;
Function HighIndex(List: Integer): Integer; override;
Function Tick: TFCTime; virtual; // returns frame distance
Function PointDistance(Point: TFCTicks): TFCTime; virtual; // time between given point and current point
Function CreationDistance: TFCTime; virtual; // time from object creation to current point
Function PreviousDistance: TFCTime; virtual; // time from previous point to immediate time
Function CurrentDistance: TFCTime; virtual; // time from current point to immediate time
//--- utility functions ---
Function GetActualPoint: TFCTicks; virtual;
Function GetPointsDifference(A,B: TFCTicks): TFCTicks; virtual;
Function GetPointsDistance(A,B: TFCTicks): TFCTime; virtual;
Function MeasureCorrection(Repeats: Integer = 1000): TFCTicks; virtual;
Function ApplyMeasuredCorrection: TFCTicks; virtual;
procedure FillFromTicks(var Time: TFCTime); virtual;
//--- properties ---
property HighResolution: Boolean read fHighResolution;
property Frequency: Int64 read fFrequency; // [Hz]
property Resolution: Int64 read fResolution; // [ns] can be 0, which means resolution better than 1 ns
property FrameCounter: UInt64 read fFrameCounter; // number of measured frames
property CreationPoint: TFCTicks read fCreationPoint; // time point when an instance was created
property PreviousPoint: TFCTicks read fPreviousPoint; // previous time point (second lass call to TickFrame)
property CurrentPoint: TFCTicks read fCurrentPoint; // current time point (lass call to TickFrame)
property CurrentFrame: TFCTime read fCurrentFrame; // distance between previous and current points
property ApplyCorrection: Boolean read fApplyCorrection write fApplyCorrection;
property Correction: TFCTicks read fCorrection write fCorrection;
end;
{===============================================================================
--------------------------------------------------------------------------------
TFrameClockEx
--------------------------------------------------------------------------------
===============================================================================}
// types and constants for lists...
type
TFCTimeStamp = record
Name: String;
Value: TFCTicks; // stores tisck at which the time stamp was made
UserData: PtrInt;
end;
PFCTimeStamp = ^TFCTimeStamp;
TFCAccumulator = record
Name: String;
Value: TFCTicks; // stores number of ticks accumulated (ie. a length of time)
UserData: PtrInt;
end;
PFCAccumulator = ^TFCAccumulator;
const
FCE_LIST_IDX_TIMESTAMPS = 0;
FCE_LIST_IDX_ACCUMULATORS = 1;
{===============================================================================
TFrameClockEx - class declaration
===============================================================================}
type
TFrameClockEx = class(TFrameClock)
protected
fTimeStamps: array of TFCTimeStamp;
fTimeStampCount: Integer;
fAccumulators: array of TFCAccumulator;
fAccumulatorCount: Integer;
//- lists getters/setters ---
Function GetTimeStamp(Index: Integer): TFCTimeStamp; virtual;
procedure SetTimeStamp(Index: Integer; Value: TFCTimeStamp); virtual;
Function GetTimeStampPtr(Index: Integer): PFCTimeStamp; virtual;
Function GetAccumulator(Index: Integer): TFCAccumulator; virtual;
procedure SetAccumulator(Index: Integer; Value: TFCAccumulator); virtual;
Function GetAccumulatorPtr(Index: Integer): PFCAccumulator; virtual;
//- inherited list methods ---
Function GetCapacity(List: Integer): Integer; override;
procedure SetCapacity(List,Value: Integer); override;
Function GetCount(List: Integer): Integer; override;
procedure SetCount(List,Value: Integer); override;
//- other methods ---
procedure Initialize(ForcedResolution: TFCForcedResolution); override;
procedure Finalize; override;
public
constructor Create(ForcedResolution: TFCForcedResolution = frDontForce);
Function LowIndex(List: Integer): Integer; override;
Function HighIndex(List: Integer): Integer; override;
//- timestamps list ---
Function TimeStampLowIndex: Integer; virtual;
Function TimeStampHighIndex: Integer; virtual;
Function TimeStampCheckIndex(Index: Integer): Boolean; virtual;
Function TimeStampIndexOf(const Name: String): Integer; overload; virtual;
Function TimeStampIndexOf(Value: TFCTicks): Integer; overload; virtual;
Function TimeStampIndexOf(const Name: String; Value: TFCTicks): Integer; overload; virtual;
Function TimeStampFind(const Name: String; out Index: Integer): Boolean; overload; virtual;
Function TimeStampFind(Value: TFCTicks; out Index: Integer): Boolean; overload; virtual;
Function TimeStampFind(const Name: String; Value: TFCTicks; out Index: Integer): Boolean; overload; virtual;
Function TimeStampAdd(const Name: String; Value: TFCTicks; UserData: PtrInt = 0): Integer; virtual;
Function TimeStampAddCurrent(const Name: String; UserData: PtrInt = 0): Integer; virtual; // adds current point as a new timestamp
procedure TimeStampInsert(Index: Integer; const Name: String; Value: TFCTicks; UserData: PtrInt = 0); virtual;
Function TimeStampRemove(const Name: String): Integer; overload; virtual;
Function TimeStampRemove(Value: TFCTicks): Integer; overload; virtual;
Function TimeStampRemove(const Name: String; Value: TFCTicks): Integer; overload; virtual;
procedure TimeStampDelete(Index: Integer); virtual;
procedure TimeStampClear; virtual;
Function TimeStampDistance(Index: Integer): TFCTime; overload; virtual; // distance between selected timestamp and current point
Function TimeStampDistance(const Name: String): TFCTime; overload; virtual;
//- accumulators list ---
Function AccumulatorLowIndex: Integer; virtual;
Function AccumulatorHighIndex: Integer; virtual;
Function AccumulatorCheckIndex(Index: Integer): Boolean; virtual;
Function AccumulatorIndexOf(const Name: String): Integer; overload; virtual;
Function AccumulatorIndexOf(Value: TFCTicks): Integer; overload; virtual;
Function AccumulatorIndexOf(const Name: String; Value: TFCTicks): Integer; overload; virtual;
Function AccumulatorFind(const Name: String; out Index: Integer): Boolean; overload; virtual;
Function AccumulatorFind(Value: TFCTicks; out Index: Integer): Boolean; overload; virtual;
Function AccumulatorFind(const Name: String; Value: TFCTicks; out Index: Integer): Boolean; overload; virtual;
Function AccumulatorAdd(const Name: String; InitialValue: TFCTicks = 0; UserData: PtrInt = 0): Integer; virtual;
procedure AccumulatorInsert(Index: Integer; const Name: String; InitialValue: TFCTicks = 0; UserData: PtrInt = 0); virtual;
Function AccumulatorRemove(const Name: String): Integer; overload; virtual;
Function AccumulatorRemove(Value: TFCTicks): Integer; overload; virtual;
Function AccumulatorRemove(const Name: String; Value: TFCTicks): Integer; overload; virtual;
procedure AccumulatorDelete(Index: Integer); virtual;
procedure AccumulatorClear; virtual;
procedure AccumulatorReset(Index: Integer); virtual;
Function AccumulatorAccumulate(Index: Integer; Delta: TFCTicks): TFCTime; overload; virtual;
Function AccumulatorAccumulate(Index: Integer): TFCTime; overload; virtual;
Function AccumulatorAccumulate(const Name: String; Delta: TFCTicks): TFCTime; overload; virtual;
Function AccumulatorAccumulate(const Name: String): TFCTime; overload; virtual;
procedure AccumulatorAccumulateAll(Delta: TFCTicks); overload; virtual;
procedure AccumulatorAccumulateAll; overload; virtual;
Function AccumulatorDistance(Index: Integer): TFCTime; overload; virtual; // how much distance has been accumulated
Function AccumulatorDistance(const Name: String): TFCTime; overload; virtual;
//- timestamp properties ---
property TimeStampCount: Integer index FCE_LIST_IDX_TIMESTAMPS read GetCount write SetCount;
property TimeStampCapacity: Integer index FCE_LIST_IDX_TIMESTAMPS read GetCapacity write SetCapacity;
property TimeStamps[Index: Integer]: TFCTimeStamp read GetTimeStamp write SetTimeStamp;
property TimeStampPtrs[Index: Integer]: PFCTimeStamp read GetTimeStampPtr;
//- accumulator properties ---
property AccumulatorCount: Integer index FCE_LIST_IDX_ACCUMULATORS read GetCount write SetCount;
property AccumulatorCapacity: Integer index FCE_LIST_IDX_ACCUMULATORS read GetCapacity write SetCapacity;
property Accumulators[Index: Integer]: TFCAccumulator read GetAccumulator write SetAccumulator;
property AccumulatorPtrs[Index: Integer]: PFCAccumulator read GetAccumulatorPtr;
end;
{===============================================================================
Standalone functions - declaration
===============================================================================}
type
TClockContext = type Pointer;
TClockUnit = (cuTick,cuSecond,cuMilli,cuMicro,cuTicks,cuSeconds,cuMillis,
cuMicros);
procedure ClockStart(out Context: TClockContext);
Function ClockTick(var Context: TClockContext; ReturnUnit: TClockUnit = cuMilli): Int64;
Function ClockTickF(var Context: TClockContext; ReturnUnit: TClockUnit = cuMilli): Double;
{
Do NOT call ClockTick before ClockEnd if you want to use its return value to
measure interval just before the call to ClockEnd, it calls ClockTick
implicitly.
}
Function ClockEnd(var Context: TClockContext; ReturnUnit: TClockUnit = cuMilli): Int64;
Function ClockEndF(var Context: TClockContext; ReturnUnit: TClockUnit = cuMilli): Double;
implementation
uses
{$IFDEF Windows}Windows{$ELSE}baseunix, linux{$ENDIF};
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TFrameClock
--------------------------------------------------------------------------------
===============================================================================}
const
FC_MILLIS_PER_SEC = 1000; // milliseconds per second
FC_MICROS_PER_SEC = 1000000; // microseconds per second
FC_NANOS_PER_SEC = 1000000000; // nanoseconds per second
FC_NANOS_PER_MILLI = 1000000; // nanoseconds per millisecond
FC_MILLIS_PER_DAY = 86400000; // milliseconds per day
{===============================================================================
TFrameClock - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TFrameClock - protected methods
-------------------------------------------------------------------------------}
Function TFrameClock.GetCapacity(List: Integer): Integer;
begin
{$IFDEF FPC}Result := 0;{$ENDIF}
raise EFCInvalidList.CreateFmt('TFrameClock.GetCapacity: Invalid list (%d).',[List]);
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TFrameClock.SetCapacity(List,Value: Integer);
begin
raise EFCInvalidList.CreateFmt('TFrameClock.SetCapacity: Invalid list (%d).',[List]);
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
Function TFrameClock.GetCount(List: Integer): Integer;
begin
{$IFDEF FPC}Result := 0;{$ENDIF}
raise EFCInvalidList.CreateFmt('TFrameClock.GetCount: Invalid list (%d).',[List]);
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TFrameClock.SetCount(List,Value: Integer);
begin
raise EFCInvalidList.CreateFmt('TFrameClock.SetCount: Invalid list (%d).',[List]);
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure TFrameClock.InitializeTime(LowResOnly: Boolean);
{$IFNDEF Windows}
var
Time: TTimeSpec;
{$ENDIF}
begin
{$IFDEF Windows}
If not LowResOnly and QueryPerformanceFrequency(fFrequency) then
begin
fHighResolution := True;
fFrequency := fFrequency and $7FFFFFFFFFFFFFFF; // mask out sign bit
fResolution := Trunc((1 / fFrequency) * FC_NANOS_PER_SEC);
end
{$ELSE}
If not LowResOnly and (clock_getres(CLOCK_MONOTONIC_RAW,@Time) = 0) then
begin
fHighResolution := True;
fFrequency := FC_NANOS_PER_SEC; // frequency is hardcoded for nanoseconds
fResolution := (Int64(Time.tv_sec) * FC_NANOS_PER_SEC) + Time.tv_nsec;
end
{$ENDIF}
else
begin
fHighResolution := False;
fFrequency := FC_MILLIS_PER_SEC;
fResolution := FC_NANOS_PER_MILLI;
end;
end;
//------------------------------------------------------------------------------
procedure TFrameClock.Initialize(ForcedResolution: TFCForcedResolution);
begin
InitializeTime(ForcedResolution = frForceLow);
If (ForcedResolution = frForceHigh) and not fHighResolution then
raise EFCHighResolutionFail.Create('TFrameClock.Create: Failed to obtain high resolution timer.');
fFrameCounter := 0;
fCreationPoint := GetActualPoint;
fPreviousPoint := fCreationPoint;
fCurrentPoint := fCreationPoint;
FillChar(fCurrentFrame,SizeOf(TFCTime),0);
fApplyCorrection := False;
fCorrection := 0;
end;
//------------------------------------------------------------------------------
procedure TFrameClock.Finalize;
begin
// nothing to do here
end;
{-------------------------------------------------------------------------------
TFrameClock - public methods
-------------------------------------------------------------------------------}
constructor TFrameClock.Create(ForcedResolution: TFCForcedResolution = frDontForce);
begin
inherited Create(0);
Initialize(ForcedResolution);
end;
//------------------------------------------------------------------------------
destructor TFrameClock.Destroy;
begin
Finalize;
inherited;
end;
//------------------------------------------------------------------------------
Function TFrameClock.LowIndex(List: Integer): Integer;
begin
{$IFDEF FPC}Result := 0;{$ENDIF}
raise EFCInvalidList.CreateFmt('TFrameClock.LowIndex: Invalid list (%d).',[List]);
end;
//------------------------------------------------------------------------------
Function TFrameClock.HighIndex(List: Integer): Integer;
begin
{$IFDEF FPC}Result := -1;{$ENDIF}
raise EFCInvalidList.CreateFmt('TFrameClock.HighIndex: Invalid list (%d).',[List]);
end;
//------------------------------------------------------------------------------
Function TFrameClock.Tick: TFCTime;
begin
Inc(fFrameCounter);
fPreviousPoint := fCurrentPoint;
fCurrentPoint := GetActualPoint;
fCurrentFrame := GetPointsDistance(fPreviousPoint,fCurrentPoint);
Result := fCurrentFrame;
end;
//------------------------------------------------------------------------------
Function TFrameClock.PointDistance(Point: TFCTicks): TFCTime;
begin
Result := GetPointsDistance(Point,fCurrentPoint);
end;
//------------------------------------------------------------------------------
Function TFrameClock.CreationDistance: TFCTime;
begin
Result := GetPointsDistance(fCreationPoint,fCurrentPoint);
end;
//------------------------------------------------------------------------------
Function TFrameClock.PreviousDistance: TFCTime;
begin
Result := GetPointsDistance(fPreviousPoint,GetActualPoint);
end;
//------------------------------------------------------------------------------
Function TFrameClock.CurrentDistance: TFCTime;
begin
Result := GetPointsDistance(fCurrentPoint,GetActualPoint);
end;
//------------------------------------------------------------------------------
Function TFrameClock.GetActualPoint: TFCTicks;
{$IFNDEF Windows}
var
Time: TTimeSpec;
{$ENDIF}
begin
Result := 0;
If fHighResolution then
begin
{$IFDEF Windows}
If QueryPerformanceCounter(Result) then
Result := Result and $7FFFFFFFFFFFFFFF // mask out sign bit
else
raise EFCSystemException.CreateFmt('TFrameClock.GetCurrentTicks: System error 0x%.8x.',[GetLastError]);
{$ELSE}
If clock_gettime(CLOCK_MONOTONIC_RAW,@Time) = 0 then
Result := (Int64(Time.tv_sec) * FC_NANOS_PER_SEC) + Time.tv_nsec
else
raise EFCSystemException.CreateFmt('TFrameClock.GetCurrentTicks: System error %d.',[errno]);
{$ENDIF}
end
else Result := Int64(Trunc(Now * FC_MILLIS_PER_DAY));
end;
//------------------------------------------------------------------------------
Function TFrameClock.GetPointsDifference(A,B: TFCTicks): TFCTicks;
begin
If A < B then
Result := B - A
else If A > B then
Result := High(Int64) - A + B + 1{overflow tick}
else
Result := 0;
end;
//------------------------------------------------------------------------------
Function TFrameClock.GetPointsDistance(A,B: TFCTicks): TFCTime;
begin
Result.Ticks := GetPointsDifference(A,B);
FillFromTicks(Result);
end;
//------------------------------------------------------------------------------
Function TFrameClock.MeasureCorrection(Repeats: Integer = 1000): TFCTicks;
var
SavedFrameCounter: UInt64;
SavedPreviousPoint: TFCTicks;
SavedCurrentPoint: TFCTicks;
SavedCurrentFrame: TFCTime;
SavedApplyCorrection: Boolean;
i: Integer;
Counter: TFCTicks;
begin
If Repeats > 0 then
begin
// save fields that will change during testing
SavedFrameCounter := fFrameCounter;
SavedPreviousPoint := fPreviousPoint;
SavedCurrentPoint := fCurrentPoint;
SavedCurrentFrame := fCurrentFrame;
SavedApplyCorrection := fApplyCorrection;
try
fApplyCorrection := False;
Counter := 0;
For i := 1 to Repeats do
begin
Tick; Tick;
Counter := Counter + fCurrentFrame.Ticks;
end;
Result := Round(Counter / Repeats);
finally
// restore changed fields
fFrameCounter := SavedFrameCounter;
fPreviousPoint := SavedPreviousPoint;
fCurrentPoint := SavedCurrentPoint;
fCurrentFrame := SavedCurrentFrame;
fApplyCorrection := SavedApplyCorrection;
end;
end
else Result := 0
end;
//------------------------------------------------------------------------------
Function TFrameClock.ApplyMeasuredCorrection: TFCTicks;
begin
fApplyCorrection := True;
fCorrection := MeasureCorrection;
Result := fCorrection;
end;
//------------------------------------------------------------------------------
procedure TFrameClock.FillFromTicks(var Time: TFCTime);
var
Temp: Extended;
begin
If fApplyCorrection then
begin
If Time.Ticks >= fCorrection then
Time.Ticks := Time.Ticks - fCorrection
else
Time.Ticks := 0;
end;
Temp := Time.Ticks / fFrequency;
Time.Sec := Temp;
Time.MiS := Temp * FC_MILLIS_PER_SEC;
Time.UiS := Temp * FC_MICROS_PER_SEC;
Time.iSec := Trunc(Temp);
Time.iMiS := Trunc(Temp * FC_MILLIS_PER_SEC);
Time.iUiS := Trunc(Temp * FC_MICROS_PER_SEC);
end;
{===============================================================================
--------------------------------------------------------------------------------
TFrameClockEx
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TFrameClockEx - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TFrameClockEx - protected methods
-------------------------------------------------------------------------------}
Function TFrameClockEx.GetTimeStamp(Index: Integer): TFCTimeStamp;
begin
If TimeStampCheckIndex(Index) then
Result := fTimeStamps[Index]
else
raise EFCIndexOutOfBounds.CreateFmt('TFrameClockEx.GetTimeStamp: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
procedure TFrameClockEx.SetTimeStamp(Index: Integer; Value: TFCTimeStamp);
begin
If TimeStampCheckIndex(Index) then
fTimeStamps[Index] := Value
else
raise EFCIndexOutOfBounds.CreateFmt('TFrameClockEx.SetTimeStamp: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.GetTimeStampPtr(Index: Integer): PFCTimeStamp;
begin
If TimeStampCheckIndex(Index) then
Result := Addr(fTimeStamps[Index])
else
raise EFCIndexOutOfBounds.CreateFmt('TFrameClockEx.GetTimeStampPtr: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.GetAccumulator(Index: Integer): TFCAccumulator;
begin
If AccumulatorCheckIndex(Index) then
Result := fAccumulators[Index]
else
raise EFCIndexOutOfBounds.CreateFmt('TFrameClockEx.GetAccumulator: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
procedure TFrameClockEx.SetAccumulator(Index: Integer; Value: TFCAccumulator);
begin
If AccumulatorCheckIndex(Index) then
fAccumulators[Index] := Value
else
raise EFCIndexOutOfBounds.CreateFmt('TFrameClockEx.SetAccumulator: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.GetAccumulatorPtr(Index: Integer): PFCAccumulator;
begin
If AccumulatorCheckIndex(Index) then
Result := Addr(fAccumulators[Index])
else
raise EFCIndexOutOfBounds.CreateFmt('TFrameClockEx.GetAccumulatorPtr: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.GetCapacity(List: Integer): Integer;
begin
case List of
FCE_LIST_IDX_TIMESTAMPS: Result := Length(fTimeStamps);
FCE_LIST_IDX_ACCUMULATORS: Result := Length(fAccumulators);
else
Result := inherited GetCapacity(List);
end;
end;
//------------------------------------------------------------------------------
procedure TFrameClockEx.SetCapacity(List,Value: Integer);
begin
case List of
FCE_LIST_IDX_TIMESTAMPS: begin
If Value < fTimeStampCount then
fTimeStampCount := Value;
SetLength(fTimeStamps,Value);
end;
FCE_LIST_IDX_ACCUMULATORS: begin
If Value < fAccumulatorCount then
fAccumulatorCount := Value;
SetLength(fAccumulators,Value);
end;
else
inherited SetCapacity(List,Value);
end;
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.GetCount(List: Integer): Integer;
begin
case List of
FCE_LIST_IDX_TIMESTAMPS: Result := fTimeStampCount;
FCE_LIST_IDX_ACCUMULATORS: Result := fAccumulatorCount;
else
Result := inherited GetCount(List);
end;
end;
//------------------------------------------------------------------------------
procedure TFrameClockEx.SetCount(List,Value: Integer);
begin
case List of
FCE_LIST_IDX_TIMESTAMPS:; // do nothing
FCE_LIST_IDX_ACCUMULATORS:; // do nothing
else
inherited SetCount(List,Value);
end;
end;
//------------------------------------------------------------------------------
procedure TFrameClockEx.Initialize(ForcedResolution: TFCForcedResolution);
begin
inherited Initialize(ForcedResolution);
SetLength(fTimeStamps,0);
fTimeStampCount := 0;
SetLength(fAccumulators,0);
fAccumulatorCount := 0;
end;
//------------------------------------------------------------------------------
procedure TFrameClockEx.Finalize;
begin
TimeStampClear;
AccumulatorClear;
inherited;
end;
{-------------------------------------------------------------------------------
TFrameClockEx - public methods
-------------------------------------------------------------------------------}
constructor TFrameClockEx.Create(ForcedResolution: TFCForcedResolution = frDontForce);
begin
inherited Create(ForcedResolution);
ListCount := ListCount + 2;
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.LowIndex(List: Integer): Integer;
begin
case List of
FCE_LIST_IDX_TIMESTAMPS: Result := Low(fTimeStamps);
FCE_LIST_IDX_ACCUMULATORS: Result := Low(fAccumulators);
else
Result := inherited LowIndex(List);
end;
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.HighIndex(List: Integer): Integer;
begin
case List of
FCE_LIST_IDX_TIMESTAMPS: Result := Pred(fTimeStampCount);
FCE_LIST_IDX_ACCUMULATORS: Result := Pred(fAccumulatorCount);
else
Result := inherited HighIndex(List);
end;
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.TimeStampLowIndex: Integer;
begin
Result := LowIndex(FCE_LIST_IDX_TIMESTAMPS);
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.TimeStampHighIndex: Integer;
begin
Result := HighIndex(FCE_LIST_IDX_TIMESTAMPS);
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.TimeStampCheckIndex(Index: Integer): Boolean;
begin
Result := CheckIndex(FCE_LIST_IDX_TIMESTAMPS,Index);
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.TimeStampIndexOf(const Name: String): Integer;
var
i: Integer;
begin
Result := -1;
For i := TimeStampLowIndex to TimeStampHighIndex do
If AnsiSameStr(fTimeStamps[i].Name,Name) then
begin
Result := i;
Break{For i};
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function TFrameClockEx.TimeStampIndexOf(Value: TFCTicks): Integer;
var
i: Integer;
begin
Result := -1;
For i := TimeStampLowIndex to TimeStampHighIndex do
If fTimeStamps[i].Value = Value then
begin
Result := i;
Break{For i};
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function TFrameClockEx.TimeStampIndexOf(const Name: String; Value: TFCTicks): Integer;
var
i: Integer;
begin
Result := -1;
For i := TimeStampLowIndex to TimeStampHighIndex do
If (fTimeStamps[i].Value = Value) and AnsiSameStr(fTimeStamps[i].Name,Name) then
begin
Result := i;
Break{For i};
end;
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.TimeStampFind(const Name: String; out Index: Integer): Boolean;
begin
Index := TimeStampIndexOf(Name);
Result := TimeStampCheckIndex(Index);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function TFrameClockEx.TimeStampFind(Value: TFCTicks; out Index: Integer): Boolean;
begin
Index := TimeStampIndexOf(Value);
Result := TimeStampCheckIndex(Index);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function TFrameClockEx.TimeStampFind(const Name: String; Value: TFCTicks; out Index: Integer): Boolean;
begin
Index := TimeStampIndexOf(Name,Value);
Result := TimeStampCheckIndex(Index);
end;
//------------------------------------------------------------------------------
Function TFrameClockEx.TimeStampAdd(const Name: String; Value: TFCTicks; UserData: PtrInt = 0): Integer;