-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimpleTSC.pas
3323 lines (2760 loc) · 117 KB
/
SimpleTSC.pas
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/.
-------------------------------------------------------------------------------}
{===============================================================================
SimpleTSC
Provides means of reading Time-Stamp Counter (TSC) Model-Specific Register
(MSR), which can be used for high-resolution time measurements.
NOTE - this register is present only on IA-32 (x86) and AMD64 (x86-64,
x64) processors.
Because proper use of TSC requires manipulation of thread affinity and
potentially also priority, a set of auxiliary functions manipulating those
thread properties is also provided.
The TSC register is 64 bits wide, but time-stamp type used here is declared
as Int64 (that is, a signed value). This could pose problems in comparisons
and arithmetics, if the bit 63 of the TSC would be set. Therefore, all time-
stamps returned by implemented functions (with noted exceptions) are masked
so that bit 63 (sign bit) is always clear (0).
The TSC is NOT guaranteed to be present and/or enabled on all systems, and
most functions also require another instructions (memory fences) which too
are not guaranteed to be supported everywhere.
So, in initialization section of this unit, it is discerned whether the
register is present on the CPU, is enabled (or, more precisely, not
disabled) by the OS and whether other required instructions are also
supported.
Because this unit also provides some auxiliry funtions that do not depend
on presence of this register, it has been decided to not raise an exception
if the TSC is not supported.
That being said, you, as a user of this library, must check whether the TSC
is fully supported or not before making any call. Do it by checking a set
returned by function STSC_SupportedFeatures.
If this set contains tscEnabled, you can safely call only the following
functions:
STSC_GetTSC
STSC_GetFullTSC
If the set contains tscSupported, you can, in addition, also call
following functions:
STSC_GetTSCEnter
STSC_GetTSCLeave
STSC_GetTSCFence
STSC_GetFullTSCEnter
STSC_GetFullTSCLeave
STSC_GetFullTSCFence
STSC_Start
STSC_TimePoint
STSC_End
STSC_MeasureCall (both overloads)
If the set does not contain tscEnabled or tscSupported, do not make any
calls to abovementioned functions.
Auxiliary functions do not depend on TSC support, so you can call them
even if the returned set is empty.
TSC is a counter register, which means it is monotonically incremented each
cycle, but frequency of these cycles might not be constant.
In very old processors (Pentium 4, Pentium M, ...), the register is
incremented in every internal processor clock cycle. If CPU frequency
changes, TSC frequency will change with it.
In newer processors, the register runs at constant rate, but this rate
can and will change if processor power state changes. Therefore, the
frequency is not fully contant and the TSC still cannot be used to
measure real time (eg. microseconds).
Only if the TSC is implemented as invariant (indicated when tscInvariant
is in the set returned by STSC_SupportedFeatures), the frequency is truly
constant over all power states and can therefore be used for time keeping.
In any way, obtaining current frequency of the TSC can be a major headache,
therefore this library does not provide it.
Given all that, SimpleTSC was not created to be a universal time measurement
tool, it is intended to be used for testing of RELATIVE time or performace
of code (ie. whether some function is faster than another one), please use
it as such.
And finally, be aware that TSC is usually implemented per-core, and there
is no synchronization between them. This means TSC on one core/logical
processor can have wildly different value than on other cores.
So if you run your mesurement on multi-processor system, make sure the
measurement runs the whole time only on one processor core (use provided
auxiliary functions to set thread affinity).
Version 1.2 (2024-05-18)
Last change 2024-05-18
©2023-2024 František Milt
Contacts:
František Milt: [email protected]
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.SimpleTSC
Dependencies:
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
Library AuxExceptions is required only when rebasing local exception classes
(see symbol SimpleTSC_UseAuxExceptions for details).
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit SimpleTSC;
{
SimpleTSC_PurePascal
If you want to compile this unit without ASM, don't want to or cannot define
PurePascal for the entire project and at the same time you don't want to or
cannot make changes to this unit, define this symbol for the entire project
and this unit will be compiled in PurePascal mode.
NOTE - this unit cannot be compiled without asm, it is here for the sake of
completeness.
}
{$IFDEF SimpleTSC_PurePascal}
{$DEFINE PurePascal}
{$ENDIF}
{
SimpleTSC_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
SimpleTSC_UseAuxExceptions to achieve this.
}
{$IF Defined(SimpleTSC_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IF Defined(CPUX86_64) or Defined(CPUX64)}
{$DEFINE x64}
{$ELSEIF Defined(CPU386)}
{$DEFINE x86}
{$ELSE}
{$MESSAGE FATAL 'Unsupported CPU.'}
{$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 ClassicProcVars+}
{$MODESWITCH PointerToProcVar+}
{$ASMMODE Intel}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
{$IF Defined(PurePascal) and not Defined(CompTest)}
{$MESSAGE WARN 'This unit cannot be compiled without ASM.'}
{$IFEND}
interface
uses
SysUtils, {$IFNDEF Windows}baseunix,{$ENDIF}
AuxTypes{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
ESTSCException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
ESTSCInvalidValue = class(ESTSCException);
ESTSCInvalidState = class(ESTSCException);
ESTSCIndexOutOfBounds = class(ESTSCException);
ESTSCSystemError = class(ESTSCException);
ESTSCCallNotImplemented = class(ESTSCException);
{===============================================================================
--------------------------------------------------------------------------------
TSC functions
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Core functions - declaration
===============================================================================}
type
{
tscPresent indicates that the executing CPU supports Time-Stamp Counter
(TSC) Model-Specific Register (MSR) (CPUID.1:EDX.TSC[4] = 1)
tscEnabled TSC is enabled by the operating system, ie. instruction
RDTSC is not disabled (implies tscPresent)
tscSupported instructions LFENCE and MFENCE (both are part of SSE2) are
supported by the CPU and OS - they are used in most functions
(implies tscEnabled)
tscInvariant TSC is invariant, that is, it does not change frequency and
can be used to measure real time (CPUID.80000007H:EDX[8] = 1)
(implies tscPresent)
tscSysProcID function STSC_GetThreadProcessor is using a system call to
obtain the thread processor ID
}
TSTSCSupportedFeature = (tscPresent,tscEnabled,tscSupported,tscInvariant,tscSysProcID);
TSTSCSupportedFeatures = set of TSTSCSupportedFeature;
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
{
STSC_SupportedFeatures
Returns set of TSC features supported by the current hardware and operating
system. See description of TSTSCSupportedFeature type for details about
individual features.
Use this function to detect what functions can be safely called.
}
Function STSC_SupportedFeatures: TSTSCSupportedFeatures;
//------------------------------------------------------------------------------
type
TSTSCTimeStamp = Int64;
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
{
STSC_GetTSC
Returns value of time-stamp counter (TSC) register.
}
Function STSC_GetTSC: TSTSCTimeStamp; register; assembler;
{
STSC_GetTSCEnter
Returns value of time-stamp counter (TSC) register while ensuring that the
instruction reading the value (instruction RDTSC) is executed prior to
execution of any subsequent instruction (including any memory accesses).
}
Function STSC_GetTSCEnter: TSTSCTimeStamp; register; assembler;
{
STSC_GetTSCLeave
Returns value of time-stamp counter (TSC) register while ensuring that the
instruction reading the value is executed only after all previous instructions
have executed and all previous memory loads and stores are globally visible.
}
Function STSC_GetTSCLeave: TSTSCTimeStamp; register; assembler;
{
STSC_GetTSCFence
Returns value of time-stamp counter (TSC) register while ensuring that the
instruction reading the value is executed only after all previous instructions
have executed and all previous memory loads and stores are globally visible
and, at the same time, prior to execution of any subsequent instruction
(including any memory accesses).
}
Function STSC_GetTSCFence: TSTSCTimeStamp; register; assembler;
//------------------------------------------------------------------------------
{
STSC_GetFullTSC
Returns full value of time-stamp counter (TSC) register, without masking
bit #63 in the result.
}
Function STSC_GetFullTSC: TSTSCTimeStamp; register; assembler;
{
STSC_GetFullTSCEnter
Works the same as STSC_GetTSCEnter, but returns the value in full width,
without masking bit #63.
}
Function STSC_GetFullTSCEnter: TSTSCTimeStamp; register; assembler;
{
STSC_GetFullTSCLeave
Works the same as STSC_GetTSCLeave, but returns the value with bit #63
unmasked.
}
Function STSC_GetFullTSCLeave: TSTSCTimeStamp; register; assembler;
{
STSC_GetFullTSCFence
Works the same as STSC_GetTSCFence, but returns the value with bit #63
unmasked.
}
Function STSC_GetFullTSCFence: TSTSCTimeStamp; register; assembler;
//------------------------------------------------------------------------------
{
STSC_GetDistance
Returns distance (forward difference) between two given time-stamps.
If TimeStampThen is higher than TimeStampNow, it is assumed the lower 63 bits
of TSC owerflowed (unlikely, but possible) and the distance is calculated as
such (note that only ONE overflow event is assumed, because overflowing
multiple times would take at least decades, if not centuries - as per Intel's
documentation, which states that the counter should not overflow within 10
years).
}
Function STSC_TicksBetween(TimeStampNow,TimeStampThen: TSTSCTimeStamp): TSTSCTimeStamp;
{===============================================================================
Continuous measurement - declaration
===============================================================================}
{
Use following types and functions for standard and continuous measurement
(measurement of several sequential intervals).
For example, if you want to measure three intervals, do following:
STSC_Start(Measurement,2);
-first_interval-
STSC_TimePoint(Measurement,0);
-second_interval-
STSC_TimePoint(Measurement,1);
-third_interval-
STSC_End(Measurement);
You can then get the distances (intervals length) this way:
first ... Measurement.TimePoints[0].DistanceFromPrevious
second ... Measurement.TimePoints[1].DistanceFromPrevious
third ... Measurement.EndTimePoints.DistanceFromPrevious
WARNING - these functions have unavoidable overhead (they take some time to
execute), so if you want to do more precise measurements, use core
functions instead and manage the time-stamps yourself.
}
type
TSTSCTimePoint = record
TimeStamp: TSTSCTimeStamp;
IsAssigned: Boolean;
DistanceFromStart: TSTSCTimeStamp;
DistanceFromPrevious: TSTSCTimeStamp;
end;
TSTSCMeasurement = record
Initialized: Boolean;
StartTimeStamp: TSTSCTimeStamp;
TimePoints: array of TSTSCTimePoint;
EndTimePoint: TSTSCTimePoint;
end;
PSTSCMeasurement = ^TSTSCMeasurement;
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
{
STSC_Start
Initializes Measurement structure, allocates array of time points and then
obtains start time-stamp.
Never set TimePointCount to value below 0, doing so will raise an exception
of class ESTSCInvalidValue.
}
procedure STSC_Start(out Measurement: TSTSCMeasurement; TimePointCount: Integer = 0);
{
STSC_TimePoint
Obtains time-stamp for selected time point. Distances are not calculated here
for the sake of perforamnce.
Remember that time point indices are zero-based (they start at zero, end at
count - 1). Invalid index will raise an ESTSCIndexOutOfBounds exception.
Measurement must be intialized, otherwise an exception of ESTSCInvalidState
class is raised.
}
procedure STSC_TimePoint(var Measurement: TSTSCMeasurement; TimePointIndex: Integer);
{
STSC_End
Obtains ending time-stamp and then finalizes the Measurement. Also calculates
distances for endpoint and all assigned time points.
Measurement must be intialized, otherwise an exception of ESTSCInvalidState
class is raised.
}
procedure STSC_End(var Measurement: TSTSCMeasurement);
{===============================================================================
Call measurement - declaration
===============================================================================}
{
These functions and types are designed to measure short time interval it
takes to execute a single function call, if resolution of TSC allows it.
The call must fully conform to signature of prodedural type TSTSCMeasuredCall,
otherwice the behavior is completely undefined and will most probably result
in nasty errors or, in the worst case, memory corruption.
}
type
TSTSCTimeStamps = record
StartTimeStamp: TSTSCTimeStamp;
EndTimeStamp: TSTSCTimeStamp;
end;
PSTSCTimeStamps = ^TSTSCTimeStamps;
TSTSCMeasuredCall = procedure(Param: Pointer); register;
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
{
STSC_MeasureCall
Measures a time it takes to execute a function referenced by parameter Call.
Start time-stamp and end time-stamp are stored in TimeStamps variable.
NOTE - this function is entirely implemented in assembly to minimize
overhead time when calling the provided function call.
}
procedure STSC_MeasureCall(Call: TSTSCMeasuredCall; CallParam: Pointer; out TimeStamps: TSTSCTimeStamps); overload; register; assembler;
{
STSC_MeasureCall
Measures a time it takes to execute a given call and returns the measured
distance (number of ticks).
}
Function STSC_MeasureCall(Call: TSTSCMeasuredCall; CallParam: Pointer): Int64; overload;
{===============================================================================
--------------------------------------------------------------------------------
Auxiliary functions
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
CPU affinity functions - declaration
===============================================================================}
type
{$IFDEF Windows}
TSTSCProcessorMask = PtrUInt;
{$ELSE}
TSTSCProcessorMask = array[0..Pred(128 div SizeOf(PtrUInt))] of PtrUInt;
{$ENDIF}
PSTSCProcessorMask = ^TSTSCProcessorMask;
TSTSCProcessID = {$IFDEF Windows}DWORD{$ELSE}pid_t{$ENDIF};
TSTSCThreadID = {$IFDEF Windows}DWORD{$ELSE}pid_t{$ENDIF};
{-------------------------------------------------------------------------------
CPU affinity functions - processor mask manipulation
-------------------------------------------------------------------------------}
{
STSC_InitProcessorMask
Fills all bits in the given processor mask to selected value (true = all bits
set, false(default) = all bits clear).
}
procedure STSC_InitProcessorMask(out ProcessorMask: TSTSCProcessorMask; Value: Boolean = False);
{
STSC_GetProcessorMaskBit
Returns true when selected Bit in the ProcessorMask is 1, false otherwise.
When Bit is out of allowable range, an exception of type ESTSCInvalidValue is
raised.
}
Function STSC_GetProcessorMaskBit(const ProcessorMask: TSTSCProcessorMask; Bit: Integer): Boolean;
{
STSC_SetProcessorMaskBit
Sets selected Bit in the ProcessorMask variable to 1.
When Bit is out of allowable range, an exception of type ESTSCInvalidValue is
raised.
}
procedure STSC_SetProcessorMaskBit(var ProcessorMask: TSTSCProcessorMask; Bit: Integer);
{
STSC_ClrProcessorMaskBit
Sets selected Bit in the ProcessorMask variable to 0.
When Bit is out of allowable range, an exception of type ESTSCInvalidValue is
raised.
}
procedure STSC_ClrProcessorMaskBit(var ProcessorMask: TSTSCProcessorMask; Bit: Integer);
{
STSC_CplProcessorMaskBit
Complements (switches) selected Bit in the ProcessorMask and returns its
original value.
When Bit is out of allowable range, an exception of type ESTSCInvalidValue is
raised.
}
Function STSC_CplProcessorMaskBit(var ProcessorMask: TSTSCProcessorMask; Bit: Integer): Boolean;
{-------------------------------------------------------------------------------
CPU affinity functions - process affinity
-------------------------------------------------------------------------------}
{
STSC_GetProcessHandleAffinity
Returns affinity mask of the selected (opened) process.
In Windows, the ProcessHandle argument must be a previously opened handle to
a process or current process pseudo-handle.
In Linux, this argument must be process ID (the call is equivalent to calling
STSC_GetProcessAffinity).
}
Function STSC_GetProcessHandleAffinity(ProcessHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCProcessID{$ENDIF}): TSTSCProcessorMask;
{
STSC_GetProcessAffinity
Returns affinity mask of the selected process.
}
Function STSC_GetProcessAffinity(ProcessID: TSTSCProcessID): TSTSCProcessorMask; overload;
{
STSC_GetProcessAffinity
Returns affinity mask of the calling process.
}
Function STSC_GetProcessAffinity: TSTSCProcessorMask; overload;
//------------------------------------------------------------------------------
{
STSC_SetProcessHandleAffinity
Sets processor affinity mask of selected (opened) process to a value passed
in argument AffinityMask and returns its previous affinity.
In Windows, the ProcessHandle argument must be a previously opened handle to
a process or current process pseudo-handle.
In Linux, this argument must be process ID (the call is equivalent to calling
STSC_SetProcessAffinity).
}
Function STSC_SetProcessHandleAffinity(ProcessHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCProcessID{$ENDIF}; AffinityMask: TSTSCProcessorMask): TSTSCProcessorMask;
{
STSC_SetProcessHandleAffinity
Sets processor affinity mask of selected process to a value passed in
argument AffinityMask and returns its previous affinity.
}
Function STSC_SetProcessAffinity(ProcessID: TSTSCProcessID; AffinityMask: TSTSCProcessorMask): TSTSCProcessorMask; overload;
{
STSC_SetProcessHandleAffinity
Sets processor affinity mask of calling process to a value passed in
argument AffinityMask and returns its previous affinity.
}
Function STSC_SetProcessAffinity(AffinityMask: TSTSCProcessorMask): TSTSCProcessorMask; overload;
{-------------------------------------------------------------------------------
CPU affinity functions - logical processor availability
-------------------------------------------------------------------------------}
{
STSC_GetNumberOfProcessors
Returns number of logical processors currently available in the system.
}
Function STSC_GetNumberOfProcessors: Integer;
{
STSC_GetAvailableProcessors
Returns affinity mask of the current process. This can be used to discern
which CPU(s) can be used by a thread. Equivalent to calling argument-less
overload of STSC_GetProcessAffinity.
}
Function STSC_GetAvailableProcessors: TSTSCProcessorMask;
{
STSC_ProcessorAvailable
Returns true when processor of given ID (number) is configured for the
current process (ie. is present in its affinity mask), false otherwise.
When ProcessorID is out of allowable range, the function will return false.
}
Function STSC_ProcessorAvailable(ProcessorID: Integer): Boolean;
{-------------------------------------------------------------------------------
CPU affinity functions - thread affinity
-------------------------------------------------------------------------------}
{
STSC_GetThreadHandleAffinity
Returns affinity mask of the selected (opened) thread.
In Windows, argument ThreadHandle must be a previously opened handle to a
thread or current thread pseudo-handle.
In Linux, this argument must contain a thread ID (call is then equivalent to
STSC_GetThreadAffinity).
}
Function STSC_GetThreadHandleAffinity(ThreadHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCThreadID{$ENDIF}): TSTSCProcessorMask;
{
STSC_GetThreadAffinity
Returns affinity mask of the selected thread.
}
Function STSC_GetThreadAffinity(ThreadID: TSTSCThreadID): TSTSCProcessorMask; overload;
{
STSC_GetThreadAffinity
Returns affinity mask of the calling thread.
}
Function STSC_GetThreadAffinity: TSTSCProcessorMask; overload;
//------------------------------------------------------------------------------
{
STSC_SetThreadHandleAffinity
Sets affinity mask of the selected (opened) thread according to parameter
AffinityMask and returns its previous value.
In Windows, argument ThreadHandle must be a previously opened handle to a
thread or current thread pseudo-handle.
In Linux, this argument must contain a thread ID (call is then equivalent to
STSC_SetThreadAffinity).
}
Function STSC_SetThreadHandleAffinity(ThreadHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCThreadID{$ENDIF}; AffinityMask: TSTSCProcessorMask): TSTSCProcessorMask; overload;
{
STSC_SetThreadAffinity
Sets affinity mask of the selected thread according to parameter AffinityMask
and returns its previous value.
}
Function STSC_SetThreadAffinity(ThreadID: TSTSCThreadID; AffinityMask: TSTSCProcessorMask): TSTSCProcessorMask; overload;
{
STSC_SetThreadAffinity
Sets affinity mask of the calling thread according to parameter AffinityMask
and returns its previous value.
}
Function STSC_SetThreadAffinity(AffinityMask: TSTSCProcessorMask): TSTSCProcessorMask; overload;
//------------------------------------------------------------------------------
{
STSC_GetThreadProcessor
Returns processor ID (number) that executed this call (more precisely, the
system call that obtained the value).
Linux
The number is obtained using function sched_getcpu.
Windows
There is a function GetCurrentProcessorNumber exported by kernel32.dll, but
it is available only from Windows Vista onwards (Windows XP 64bit also
seems to have it), and since I am writing this library so it can run in
Windows XP 32bit too, use of that function cannot be hardcoded. So, in unit
initialization, the kernel32.dll is probed for this funtion.
When it is there, is gets binded and is then used to obtain the number.
If it is not present, then the number is obtained using SimpleCPUID library
(which is required by this unit anyway), more specifically from a mapping
of local APIC IDs to processor numbers (this mapping is constructed at the
unit initialization). If the APIC ID cannot be mapped, then processor 0 is
returned.
You can use STSC_SupportedFeatures to see what method is being used. When
its result contains tscSysProcID then the system call is used, when this
flag is not included, then APIC ID mapping is used.
}
Function STSC_GetThreadProcessor: Integer;
{
STSC_SetThreadProcessor
Sets affinity of calling thread so that it will run only on the selected
processor ID and returns previous affinity mask.
If the selected processor cannot be used (is not configured for current
process), then an ESTSCInvalidValue exception is raised and affinity is not
changed.
}
Function STSC_SetThreadProcessor(ProcessorID: Integer): TSTSCProcessorMask;
{===============================================================================
Priority funtions - declaration
===============================================================================}
{-------------------------------------------------------------------------------
Priority funtions - process priority class
-------------------------------------------------------------------------------}
type
{
TSCSCPriorityClass
Priority classes are meaningless in Linux.
Values pcBackgroundModeBegin and pcBackgroundModeEnd are never returned as
process priority class. Use them only when setting priority class, but first
consult Windows SDK documentation for details. Note that these two values
can only be used when operating on current process, never on foreign process.
}
TSCSCPriorityClass = (pcIdle,pcBelowNormal,pcNormal,pcAboveNormal,pcHigh,
pcRealtime,pcBackgroundModeBegin,pcBackgroundModeEnd);
//------------------------------------------------------------------------------
{
STSC_GetPriorityClass
Returns priority class of selected (opened) process. Has meaning only in
Windows OS, in Linux it always returns pcNormal (arguments are ignored).
For details about priority classes, refer to Windows SDK documentation.
}
Function STSC_GetProcessHandlePriorityClass(ProcessHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCProcessID{$ENDIF}): TSCSCPriorityClass;
{
STSC_GetPriorityClass
Returns priority class of selected process. Has meaning only in Windows OS,
in Linux it always returns pcNormal (arguments are ignored).
For details about priority classes, refer to Windows SDK documentation.
}
Function STSC_GetProcessPriorityClass(ProcessID: TSTSCProcessID): TSCSCPriorityClass; overload;
{
STSC_GetPriorityClass
Returns priority class of current process. Has meaning only in Windows OS,
in Linux it always returns pcNormal.
For details about priority classes, refer to Windows SDK documentation.
}
Function STSC_GetProcessPriorityClass: TSCSCPriorityClass; overload;
//------------------------------------------------------------------------------
{
STSC_SetPriorityClass
Sets priority class of selected (opened) process and returns its previous
value. Has meaning only in Windows OS, in Linux it does nothing and always
returns pcNormal (arguments are ignored).
For details about priority classes, refer to Windows SDK documentation.
}
Function STSC_SetProcessHandlePriorityClass(ProcessHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCProcessID{$ENDIF}; PriorityClass: TSCSCPriorityClass): TSCSCPriorityClass;
{
STSC_SetPriorityClass
Sets priority class of selected process and returns its previous value. Has
meaning only in Windows OS, in Linux it does nothing and always returns
pcNormal (arguments are ignored).
For details about priority classes, refer to Windows SDK documentation.
}
Function STSC_SetProcessPriorityClass(ProcessID: TSTSCProcessID; PriorityClass: TSCSCPriorityClass): TSCSCPriorityClass; overload;
{
STSC_SetPriorityClass
Sets priority class of current process and returns its previous value. Has
meaning only in Windows OS, in Linux it does nothing and always returns
pcNormal.
For details about priority classes, refer to Windows SDK documentation.
}
Function STSC_SetProcessPriorityClass(PriorityClass: TSCSCPriorityClass): TSCSCPriorityClass; overload;
{-------------------------------------------------------------------------------
Priority funtions - process priority boost
-------------------------------------------------------------------------------}
{
STSC_GetProcessHandlePriorityBoost
Returns state of priority boost for selected (opened) process.
Priority boost is Windows-specific option, in Linux this function always
returns false (arguments are ignored).
}
Function STSC_GetProcessHandlePriorityBoost(ProcessHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCProcessID{$ENDIF}): Boolean;
{
STSC_GetProcessPriorityBoost
Returns state of priority boost for selected process.
Priority boost is Windows-specific option, in Linux this function always
returns false (arguments are ignored).
}
Function STSC_GetProcessPriorityBoost(ProcessID: TSTSCProcessID): Boolean; overload;
{
STSC_GetProcessPriorityBoost
Returns state of priority boost for calling process.
Priority boost is Windows-specific option, in Linux this function always
returns false (arguments are ignored).
}
Function STSC_GetProcessPriorityBoost: Boolean; overload;
//------------------------------------------------------------------------------
{
STSC_SetProcessHandlePriorityBoost
Sets state of priority boost for selected (opened) process and returns its
previous state.
Note that setting priority boost for a process changes this setting for all
existing and future threads belonging to that process.
Priority boost is Windows-specific option, in Linux this function does
nothing and always returns false (arguments are ignored).
}
Function STSC_SetProcessHandlePriorityBoost(ProcessHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCProcessID{$ENDIF}; EnablePriorityBoost: Boolean): Boolean;
{
STSC_SetProcessPriorityBoost
Sets state of priority boost for selected process and returns its previous
state.
Note that setting priority boost for a process changes this setting for all
existing and future threads belonging to that process.
Priority boost is Windows-specific option, in Linux this function does
nothing and always returns false (arguments are ignored).
}
Function STSC_SetProcessPriorityBoost(ProcessID: TSTSCProcessID; EnablePriorityBoost: Boolean): Boolean; overload;
{
STSC_SetProcessPriorityBoost
Sets state of priority boost for calling process and returns its previous
state.
Note that setting priority boost for a process changes this setting for all
existing and future threads belonging to that process.
Priority boost is Windows-specific option, in Linux this function does
nothing and always returns false (arguments are ignored).
}
Function STSC_SetProcessPriorityBoost(EnablePriorityBoost: Boolean): Boolean; overload;
{-------------------------------------------------------------------------------
Priority funtions - thread priority boost
-------------------------------------------------------------------------------}
{
STSC_GetThreadHandlePriorityBoost
Returns state of priority boost for selected (opened) thread.
Priority boost is Windows-specific option, in Linux this function always
returns false (arguments are ignored).
}
Function STSC_GetThreadHandlePriorityBoost(ThreadHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCThreadID{$ENDIF}): Boolean;
{
STSC_GetThreadPriorityBoost
Returns state of priority boost for selected thread.
Priority boost is Windows-specific option, in Linux this function always
returns false (arguments are ignored).
}
Function STSC_GetThreadPriorityBoost(ThreadID: TSTSCThreadID): Boolean; overload;
{
STSC_GetThreadPriorityBoost
Returns state of priority boost for calling thread.
Priority boost is Windows-specific option, in Linux this function always
returns false (arguments are ignored).
}
Function STSC_GetThreadPriorityBoost: Boolean; overload;
//------------------------------------------------------------------------------
{
STSC_SetThreadHandlePriorityBoost
Sets state of priority boost for selected (opened) thread and returns its
previous state.
Priority boost is Windows-specific option, in Linux this function does
nothing and always returns false (arguments are ignored).
}
Function STSC_SetThreadHandlePriorityBoost(ThreadHandle: {$IFDEF Windows}THandle{$ELSE}TSTSCThreadID{$ENDIF}; EnablePriorityBoost: Boolean): Boolean;
{
STSC_SetThreadHandlePriorityBoost
Sets state of priority boost for selected thread and returns its previous
state.
Priority boost is Windows-specific option, in Linux this function does
nothing and always returns false (arguments are ignored).
}
Function STSC_SetThreadPriorityBoost(ThreadID: TSTSCThreadID; EnablePriorityBoost: Boolean): Boolean; overload;
{
STSC_SetThreadHandlePriorityBoost
Sets state of priority boost for calling thread and returns its previous
state.
Priority boost is Windows-specific option, in Linux this function does
nothing and always returns false (arguments are ignored).
}
Function STSC_SetThreadPriorityBoost(EnablePriorityBoost: Boolean): Boolean; overload;
{-------------------------------------------------------------------------------
Priority funtions - thread scheduling policy
-------------------------------------------------------------------------------}
type
{
TSTSCSchedPolicy
Scheduling policy has meaning only in Linux. There is nothing even vaguely
similar in Windows.
For details about sheduling policies, please refer to Linux manual.
NOTE - spNormal and spOther are denoting the same policy (SCHED_OTHER).
}
TSTSCSchedPolicy = (spNormal,spOther,spFifo,spRR,spBatch,spIso,spIdle,spDeadline);
//------------------------------------------------------------------------------
{
STSC_GetThreadSchedulingPolicy
Returns scheduling policy of the selected thread. Has meaning only in Linux,
in Windows it will always return spNormal.
}
Function STSC_GetThreadSchedulingPolicy(ThreadID: TSTSCThreadID): TSTSCSchedPolicy; overload;
{
STSC_GetThreadSchedulingPolicy
Returns current scheduling policy of the calling thread. Has meaning only in
Linux, in Windows it will always return spNormal.
}
Function STSC_GetThreadSchedulingPolicy: TSTSCSchedPolicy; overload;
//------------------------------------------------------------------------------
{
STSC_SetThreadSchedulingPolicy
Sets selected scheduling policy for the selected thread and returns the
previous one. Has meaning only in Linux, in Windows it does nothing and
always returns spNormal.
When setting spFifo or spRR policy, the scheduling priority is set to 0.5
times the range implemented by system. In all other cases it is set to zero.