Measuring the length of a time slice is an interesting problem that I have attempted to solve several times before with no success. This is a crude proof of concept app that appears to mostly work. Running on my 3.0GHz P4 Windows XP test system I get ~31ms per time slice.
;==============================================================================
include \masm32\include\masm32rt.inc
.686
;==============================================================================
.data
hThread1 dd 0
hThread2 dd 0
pBuff dd 0
count dd 0
.code
;==============================================================================
ThreadProc1 proc uses ebx lpParameter:LPVOID
.WHILE count < 10000000
xor eax, eax
cpuid
rdtsc
mov ecx, count
mov ebx, pBuff
mov DWORD PTR [ebx+ecx*8], 1
mov DWORD PTR [ebx+ecx*8+4], eax
inc count
.ENDW
ret
ThreadProc1 endp
;==============================================================================
ThreadProc2 proc uses ebx lpParameter:LPVOID
.WHILE count < 10000000
xor eax, eax
cpuid
rdtsc
mov ecx, count
mov ebx, pBuff
mov DWORD PTR [ebx+ecx*8], 2
mov DWORD PTR [ebx+ecx*8+4], eax
inc count
.ENDW
ret
ThreadProc2 endp
;==============================================================================
start:
;==============================================================================
invoke GetCurrentProcess
invoke SetProcessAffinityMask, eax, 1
;--------------------------------------------------------------------------
; Minimize interruptions (THESE PRIORITY LEVELS NOT SAFE FOR SINGLE CORE).
;--------------------------------------------------------------------------
invoke GetCurrentProcess
invoke SetPriorityClass, eax, REALTIME_PRIORITY_CLASS
invoke GetCurrentThread
invoke SetThreadPriority, eax, THREAD_PRIORITY_TIME_CRITICAL
;-----------------------------------------------------------
; Buffer entries will be a dword thread identifier (1 or 2)
; followed by the low-order dword of the current TSC.
;-----------------------------------------------------------
mov pBuff, alloc(10000000*8+1000)
invoke CreateThread, NULL, 0, ThreadProc1, 0, 0, NULL
mov hThread1, eax
invoke CreateThread, NULL, 0, ThreadProc2, 0, 0, NULL
mov hThread2, eax
invoke WaitForMultipleObjects, 2, ADDR hThread1, TRUE, INFINITE
;--------------------------------------------------------------
; Display only buffer entries where thread identifier changed.
;--------------------------------------------------------------
mov ebx, -1
xor edi, edi
mov esi, pBuff
@@:
add ebx, 1
cmp ebx, 10000000
ja @F
mov eax, [esi+ebx*8]
cmp eax, edi
je @B
mov edi, eax
printf("%d\t", eax)
mov eax, [esi+ebx*8+4]
printf("%d\n", eax)
jmp @B
@@:
inkey
exit
;==============================================================================
end start
1 -663624222
2 -481849142
1 -388584002
2 -294945830
1 -201522110
2 -201592358
1 -14500670
2 -14566522
1 172600054
2 266095538
1 359615594
2 453104686
0 0
1 546554114
2 640150026
1 733605030
2 733534342
1 920610562
2 1014232250
1 1014073002
2 1201347382
1 1294654642
2 1388214506
1 1481674266
2 1575242006
1 1668778034
2 1762279714
1 1855800558
2 1949369834
1 1949199638
2 2042712442
1 2136223258
2 -1971577282
1 -1878131010
2 -1784582230
1 -1691100778
2 -1597555930
1 -1504092486
2 -1504153934
1 -1317026250
2 -1223522986
1 -1130035162
2 -1036435754
1 -943030074
2 -849501226
1 -823197342
Press any key to continue ...