News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

SwitchToThread vs Sleep(0)

Started by jj2007, May 11, 2013, 08:17:48 AM

Previous topic - Next topic

jj2007

I am playing with the question how to get the #cycles in a single time slice:

  mov ebx, 200
  .Repeat
   invoke SwitchToThread   ; get new slice
   pushad
   cpuid   ; serialise
   popad
   rdtsc
   push eax
   push edx
   if mode eq 1
      invoke Sleep, 0
   elseif mode eq 2
      invoke SwitchToThread
   elseif mode eq 3
      push 0
      .Repeat
         dec dword ptr [esp]
      .Until Sign?
      pop eax
   else
      ; do nothing
   endif
   rdtsc
   pop edx
   pop ecx
   sub eax, ecx
   ;sub eax, 11   ; AMD: sub 11
   print str$(eax), " "
   dec ebx
  .Until Sign?


Results:
Intel(R) Celeron(R) M CPU        420  @ 1.60GHz (SSE3)
Mode Sleep 0    2724 1356 1128 1044 1128 1104 1068 1008 1044 1044 1056 1044 1104 1128 1104 1032 1044 1020
1056 1044 1044 1140 1164 1140 1080 1080 1092 1056 1068 1056 1140 1164 1176 1080 1080 1092 1044 1080 1080
1176 1140 1140 1056 1044 1056 1056 1080 1068 1128 1152 1152 1044 1056 1068 1068 1068 1068 1116 1128 1140
1068 1044 1068 1056 1044 1080 1116 1116 1116 1044 1080 1044 1068 1056 1068 1128 1140 1128 1056 1104 1068
1044 1044 1044 1152 1152 1116 1068 1068 1068 1032 1068 1056 1128 1140 1116 1068 1068 1056 1068 1068 1044
1128 1116 1128 1032 1032 1020 1044 1068 1044 1104 1116 1104 1080 1068 1044 1056 1068 1068 7128 1140 1152
1056 1044 1056 1032 1056 1056 1128 1152 1152 1068 1032 1032 1032 1056 1044 1104 1116 1152 1056 1056 1044
1056 1032 1032 1104 1128 1128 1092 1044 1068 1044 1056 1032 1116 1116 1152 1044 1056 1044 1056 1032 1056
1104 1140 1128 1032 1044 1044 1044 1056 1044 1128 1128 1116 1056 1032 1020 1020 1020 1032 1116 1128 1164
1056 1056 1044 1044 1032 1032 1116 1116 1116 1056 1056 1044 1044 1044 1068

Mode SwitchToThread     480 456 468 444 456 456 456 456 492 504 468 468 444 456 456 456 444 504 480 468 4
56 456 456 444 456 456 492 492 456 456 444 456 456 456 456 504 492 468 444 456 444 456 456 456 504 504 46
8 444 456 456 456 444 456 492 492 456 456 444 456 444 456 444 504 480 456 456 444 456 456 456 444 504 504
456 456 456 456 456 456 456 504 492 456 456 456 456 456 456 456 504 480 456 444 456 456 456 456 444 504
492 456 456 456 456 456 456 456 504 492 468 456 456 444 456 456 444 504 480 456 456 456 444 444 456 444 4
92 492 468 456 456 456 444 444 444 492 480 456 456 456 456 444 456 444 504 480 456 444 444 456 444 456 44
4 492 492 456 456 444 456 444 456 444 492 480 456 456 456 456 456 456 456 492 480 456 456 444 456 444 456
456 492 480 468 456 444 456 456 456 456 492 480 468 456 456 456 456 456 456 492 492 468 444

Mode loop       120 96 96 96 108 96 96 96 96 108 108 108 108 96 96 108 108 108 108 108 96 108 108 96 108
96 96 96 96 96 96 96 96 96 96 96 108 108 108 108 96 108 108 96 108 96 96 96 108 108 96 96 108 96 108 108
108 96 108 108 96 108 108 108 108 108 96 96 108 96 108 96 96 96 96 96 96 108 108 108 96 108 96 96 96 108
108 96 96 96 108 108 108 108 96 108 96 108 96 108 108 108 96 108 108 108 96 96 96 96 96 108 96 96 108 108
96 108 108 96 108 96 108 96 96 96 108 108 96 96 96 108 96 108 96 108 108 96 96 96 96 108 108 108 96 108
108 108 96 108 108 96 96 108 108 96 108 96 96 96 96 96 96 108 108 96 108 96 96 108 96 108 96 108 108 108
108 108 96 108 108 108 96 108 108 108 108 96 108 108 108 96 96 96 108 96 96 96 108 96 96

Mode nothing    96 108 96 96 96 96 96 96 96 96 96 96 108 108 96 96 96 108 96 108 96 108 96 96 108 96 96 1
08 96 96 96 96 96 108 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 108 96 96 108 96 96 96
96 96 96 96 96 108 96 108 96 96 108 96 108 96 96 96 108 96 96 96 96 108 96 96 108 96 96 96 96 96 96 96 96
96 96 108 96 96 96 96 108 96 96 96 108 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96
96 96 96 96 108 96 96 96 96 108 96 96 96 108 96 96 96 96 96 96 96 96 96 96 96 108 96 108 96 108 96 96 96
96 108 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 108 96 96 108 96 96 96 96 96 96 108 96 96 96 96 96 1
08 96 96 96 96 96 108


Does it make any sense? On my other puter the count went down to a minimum of 10.

Antariy

Hi Jochen! :t


Intel(R) Celeron(R) CPU 2.13GHz (SSE3)
Mode Sleep 0    9864 5552 7800 9760 8288 4336 3944 4472 8296 4672 3128 3912 4112
4080 3696 3560 3792 3528 4224 3952 3520 3800 3472 4200 3896 3768 3520 3384 3520
3480 3400 3392 3400 3520 3520 3432 4040 3424 3944 3784 3416 3544 3536 3760 3384
4032 3536 3440 4280 3880 3520 3872 3416 7384 3968 3872 5944 4096 5456 7344 4064
6984 3984 3584 3848 3504 3832 3456 3840 3920 4288 3848 3960 4240 3960 3400 3568
3472 4096 3824 3080 3960 4160 4024 4096 3872 3408 3400 3112 3112 3112 4080 3512
3424 3600 3488 5496 4048 3864 4184 3432 3552 3736 4504 3624 3824 3976 3560 3600
3984 3448 3480 3384 7256 3960 4136 3416 3496 3480 66288 6024 4384 5536 8640 551
2 6656 4040 11104 4096 3824 9272 3984 3520 3464 3608 3752 3960 3600 4168 3768 35
36 3424 3736 3624 3896 3792 3456 3384 3880 3016 3400 3472 3400 3816 3992 3768 40
88 3928 3800 3552 3632 3448 3072 3352 3104 3648 3264 3472 4008 3464 3856 4056 38
64 5440 4136 3528 3432 6928 3808 3632 3456 3696 4088 4008 3904 3824 3992 4336 35
20 3448 3432 5024 4632 3440 3336 4392 3992 3856 3856 3856 3456

Mode SwitchToThread     21352 1504 888 888 888 888 888 888 888 888 888 888 888 8
88 888 888 888 1064 888 1048 888 888 888 888 888 888 1144 888 888 1112 1048 888
1104 888 1144 888 888 888 888 1048 888 888 888 1064 1048 888 1048 888 888 888 10
40 888 888 888 888 1064 888 1064 888 1040 888 888 888 888 1184 888 1064 888 1064
888 888 888 888 1048 888 1064 888 1120 888 1064 1048 888 888 888 1048 888 1040
888 1048 888 1064 1040 1048 888 888 888 888 1080 888 888 22016 309032 1136 1048
1048 888 888 888 1064 888 888 888 888 1064 888 1040 888 1104 888 888 888 1136 10
48 888 888 888 1064 1048 888 888 888 1064 40128 1040 1048 888 888 888 1080 888 8
88 888 888 888 888 1040 1064 888 888 888 888 888 888 1048 888 888 888 888 1064 1
160 888 1048 888 1040 1064 1064 888 888 888 888 888 888 1040 888 1064 888 1152 8
88 1120 1040 888 888 888 888 888 888 888 888 888 1064 1048 1064 888 1040 1048 12
88 920 888 888 888 888

Mode loop       96 104 96 104 96 104 104 96 104 104 96 104 104 96 104 104 104 10
4 104 104 96 104 104 96 96 96 96 96 104 96 96 96 104 96 96 104 104 104 104 104 1
04 104 96 104 104 96 96 104 104 104 104 96 96 104 96 96 96 96 96 104 96 96 96 10
4 104 104 96 104 104 96 96 104 96 104 96 104 96 104 96 96 96 96 104 104 104 96 9
6 104 96 104 96 96 96 104 104 96 96 104 104 96 96 104 104 96 96 96 96 104 104 10
4 96 96 104 104 104 104 104 104 96 104 104 96 96 104 104 104 104 104 104 104 96
96 104 96 96 104 104 104 104 96 96 104 96 96 104 104 104 104 104 104 96 104 104
104 96 104 96 96 96 96 96 104 104 96 96 104 104 104 104 104 104 96 104 96 104 96
104 96 96 96 96 104 96 96 104 104 96 96 96 96 104 104 104 96 96 104 96 104 104
104 96

Mode nothing    96 104 104 96 104 104 96 96 104 104 104 104 96 104 104 104 104 1
04 96 96 96 96 96 104 96 96 96 96 96 104 96 104 104 104 104 104 96 104 96 96 96
96 104 96 96 104 96 96 96 104 104 104 96 104 104 104 104 96 104 96 104 104 104 9
6 104 96 104 104 104 96 104 104 96 96 104 104 104 104 104 104 96 96 104 96 96 96
96 96 104 104 104 96 96 104 104 96 96 104 96 104 104 96 104 104 104 96 104 96 1
04 104 96 104 104 96 96 96 104 104 104 96 96 96 104 96 96 96 104 104 96 104 96 9
6 104 96 104 104 104 104 96 104 104 96 104 104 96 96 96 104 96 104 104 96 96 96
96 104 96 104 96 96 96 96 104 104 104 96 96 104 104 96 96 104 104 104 96 96 104
96 104 96 96 104 96 96 104 104 104 96 96 104 96 96 104 104 104 104 96 104 96 96
96


ok

MichaelW

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 ...




Well Microsoft, here's another nice mess you've gotten us into.

jj2007

Thanks, Alex & Michael. Today I'm very busy but tonight I might have a chance to look at it again.

Antariy

Hi Michael :t

Probably your source needs MASM11 to be build, but after small changement I got it in EXE with MASM10, here are the results:

1       -700983768
2       -573169360
1       -539986840
2       -473352576
1       -406676328
2       -340023304
1       -340056040
2       -206677800
1       -139850712
0       0
2       -73333560
1       -6666776
2       59998024
1       126676064
2       193347000
1       193303080
2       326677976
1       326643200
2       460011992
1       526689256
2       526651960
1       626656304
2       760389856
1       826697136
2       826666576
1       893336576
2       1030450656
1       1026676792
2       1093346880
1       1226727936
2       1293746208
1       1293355536
2       1429200640
1       1493412624
2       1560250120
0       0
1       1626755408
2       1694110448
1       1760088504
2       1768808272
1       1769072968
2       1769086424
1       1769346944
2       1769356696
1       1769616144
2       1769625320
1       1769884496
2       1769893560
1       1770152848
2       1770162008
1       1770420832
2       1770429848
1       1770714704
2       1770723896
1       1770984064
2       1770993120
1       1771253656
2       1771263032
1       1771522896
2       1771532576
1       1771792048
2       1771801320
1       1772062696
2       1772071896
1       1772332752
2       1772341960
1       1772623632
2       1772633104
1       1772893176
2       1772902352
1       1773161568
2       1773170752
1       1773430672
2       1773439872
1       1773699536
2       1773708752
1       1773969384
2       1773978816
1       1774238624
2       1774247744
1       1774507712
2       1774516520
1       1774805656
2       1774814728
1       1775074128
2       1775082792
1       1775342472
2       1775351632
1       1775611144
2       1775620240
1       1775880208
2       1775889496
1       1776149088
2       1776158224
1       1776418224
2       1776427360
1       1776687392
2       1776696632
1       1776978864
2       1776988096
1       1777248616
2       1777257872
1       1777517888
2       1777527016
1       1777787208
2       1777796592
1       1778056320
2       1778065816
1       1778326168
2       1778335288
1       1778594904
2       1778604368
1       1778887840
2       1778897072
1       1779158848
2       1779168280
1       1779427536
2       1779437336
1       1779697312
2       1779706232
1       1779966720
2       1779976312
1       1780236376
2       1780245616
1       1780505544
2       1780513864
1       1780774056
2       1780783368
1       1781068136
2       1781077360
1       1781338544
2       1781347928
1       1781607384
2       1781616640
1       1781876968
2       1781886216
1       1782147624
2       1782156432
1       1782418008
2       1782427904
1       1782691232
2       1782700728
1       1782960744
2       1782992888
1       1783253952
2       1783263256
1       1783525456
2       1783534816
1       1783795664
2       1783804504
1       1784065040
2       1784074416
1       1784336112
2       1784345560
1       1784606048
2       1784615512
1       1784875776
2       1784884288
1       1785168064
2       1785177560
1       1785439504
2       1785449016
1       1785710464
2       1785719928
1       1785980592
2       1785990344
1       1786250736
2       1786259864
1       1786522000
2       1786531616
1       1786793232
2       1786802744
1       1787064128
2       1787073728
1       1787360128
2       1787369352
1       1787630600
2       1787639928
1       1787901952
2       1787911512
1       1788173936
2       1788183864
1       1788445584
2       1788455328
1       1788717552
2       1788726904
1       1788989008
2       1788998768
1       1789284208
2       1789293680
1       1789555568
2       1789564968
1       1789827200
2       1789836016
1       1790098560
2       1790107944
1       1790370160
2       1790379648
1       1790641408
2       1790650824
1       1790912432
2       1790922048
1       1791184832
2       1791194848
1       1791495600
2       1791613528
1       1791878488
2       1791887728
1       1792149984
2       1792159480
1       1792421616
2       1792431408
1       1792693168
2       1792702568
1       1792964304
2       1792973640
1       1793236576
2       1793246064
0       0
1       1793415264
2       1826787000
1       1893585776
2       1960098160
1       1960054752
2       2093482120
1       -2134711480
2       -2068195632
1       -2068233672
2       -1934863720
1       -1934893520
2       -1868223504
1       -1734399224
2       -1668184408
1       -1601375912
2       -1534845376
1       -1456995456
2       -1401499368
1       -1334680240
2       -1334864416
1       -1268194320
2       -1201524256
1       -1068019200
2       -1001482176
1       -934348528
2       -934326064
1       -934063464
2       -934051816
1       -933791024
2       -933781752
1       -933519952
2       -933510504
1       -933249288
2       -933240192
1       -932979024
2       -932969960
1       -932675584
2       -932665720
1       -932405776
2       -932396648
1       -932136256
2       -932127520
1       -931867440
2       -931858064
1       -931597056
2       -931587576
1       -931324608
2       -931315200
1       -931053720
2       -931044296
1       -930782224
2       -930772952
1       -930484056
2       -930474704
1       -930213416
2       -930204400
1       -929944304
2       -929935080
1       -929674832
2       -929665400
1       -929405552
2       -929396456
1       -929135600
2       -929126384
1       -928863664
2       -928854504
1       -928571384
2       -928562200
1       -928301456
2       -928292024
1       -928031952
2       -928022648
1       -927762568
2       -927753272
1       -927492784
2       -927484000
1       -927222448
2       -927212920
1       -926951920
2       -926942560
1       -926680176
2       -926671360
1       -926386912
2       -926377568
1       -926116544
2       -926106464
1       -925844608
2       -925834032
1       -925573496
2       -925564080
1       -925303400
2       -925293648
1       -925032872
2       -925023352
1       -924762512
2       -924753304
1       -924490560
2       -924481384
1       -924198224
2       -924188584
1       -923927232
2       -923917480
1       -923655464
2       -923646248
1       -923386192
2       -923376984
1       -923115768
2       -923107080
1       -922846016
2       -922836616
1       -922577104
2       -922567680
1       -922285088
2       -922275584
1       -922012616
2       -922003288
1       -921742368
2       -921732944
1       -921473216
2       -921463768
1       -921203120
2       -921193328
1       -920932104
2       -920922560
1       -920660688
2       -920651352
1       -920389616
2       -920380672
1       -920095568
2       -920086272
1       -919825656
2       -919816488
1       -919556232
2       -919546080
1       -919285056
2       -919275816
1       -919014888
2       -919005520
1       -918744568
2       -918735232
1       -918474032
2       -918465504
1       -918204920
2       -918195136
1       -917912576
2       -917903152
1       -917641992
2       -917632656
1       -917371064
2       -917361696
1       -917100528
2       -917091192
1       -916830120
2       -916821392
1       -916560560
2       -916550824
1       -916290088
2       -916280432
1       -915996704
2       -915987120
1       -915725064
2       -915715592
1       -915453744
2       -915444424
1       -915182088
2       -915171872
1       -914909696
2       -914900280
1       -914638168
2       -914628240
1       -914366944
2       -914358232
1       -914096424
2       -914086944
1       -913800232
2       -913790816
1       -913527608
2       -913517888
1       -913256608
2       -913247200
1       -912986144
2       -912976368
1       -912714352
2       -912705024
1       -912443832
2       -912434432
1       -912173416
2       -912163800
1       -911867312
2       -911745832
1       -911477496
2       -911467120
1       -911205928
2       -911196456
1       -910934888
2       -910925016
1       -910663368
2       -910653504
1       -910391024
2       -910381888
1       -910120272
2       -910110016
1       -909848296
2       -909812544
1       -909549664
2       -909540464
1       -909278680
2       -909269272
1       -909007464
2       -908998064
1       -908736128
2       -908726312
1       -908464384
2       -908454424
1       -908192416
2       -908182896
1       -907920392
2       -907910848
1       -907622328
2       -907612848
1       -907350656
2       -907341264
1       -907079160
2       -907069744
1       -906808408
2       -906799168
1       -906535504
2       -906524224
1       -906261744
2       -906252240
1       -905990432
2       -905981088
1       -905719680
2       -905710384
1       -905426152
2       -905416576
1       -905155472
2       -905145088
1       -904882648
2       -904873248
1       -904611160
2       -904601848
1       -904341104
2       -904331592
1       -904069896
2       -904060352
1       -903799248
2       -903789696
1       -903507456
2       -903497936
1       -903235504
2       -903226296
1       -902965456
2       -902956200
1       -902693736
2       -902684280
1       -902422480
2       -902412968
1       -902151032
2       -902141416
1       -901878312
2       -901868816
1       -901607552
2       -901598168
0       0
1       -901473896
2       -901509600
1       -801304968
2       -734795960
1       -734835216
2       -601449424
1       -601495024
2       -534824896
1       -401326624
2       -334774872
1       -334816224
2       -201424456
1       -201476112
2       -68073544
1       -68135896
2       65244048
1       132353448
2       198586448
1       265677736
2       331926920
1       332045432
2       332309648
1       332326280
2       332588328
1       332604008
2       332866648
1       332882376
2       333147056
1       333162648
2       333427424
1       333443128
2       333707056
1       333722496
2       334008832
1       334024960
2       334287576
1       334303416
2       334566448
1       334581872
2       334844952
1       334860456
2       335123032
1       335138752
2       335400928
1       335416408
2       335680848
1       335696584
2       335959488
1       335976944
2       336270568
1       336287408
2       336549736
1       336567464
2       336830224
1       336846464
2       337109232
1       337124944
2       337387056
1       337402608
2       337665080
1       337681320
2       337944488
1       337960032
2       338244440
1       338261952
2       338524816
1       338541576
2       338805080
1       338821536
2       339085144
1       339100760
2       339364840
1       339380480
2       339642384
1       339657784
2       339920328
1       339937928
2       340200264
1       340241520
2       340503912
1       340520192
2       340783688
1       340800248
2       341062984
1       341078648
2       341342264
1       341357640
2       341619264
1       341637824
2       341900656
1       341916832
2       342179888
1       342195560
2       342483440
1       342500888
2       342764344
1       342781464
2       343044696
1       343062184
2       343325112
1       343341800
2       343604144
1       343620040
2       343882912
1       343899496
2       344162656
1       344178720
2       344464888
1       344481520
2       344743832
1       344759272
2       345023288
1       345040168
2       345303192
1       345320560
2       345583536
1       345599016
2       345861416
1       345878976
2       346141304
1       346156848
2       346420616
1       346436016
2       346720296
1       346735888
2       346997144
1       347012440
2       347272992
1       347288360
2       347549712
1       347565216
2       347827584
1       347844376
2       348107688
1       348124824
2       348387344
1       348403528
2       348694080
1       348711336
2       348974016
1       348990776
2       349253168
1       349269216
2       349532072
1       349548152
2       349810560
1       349826160
2       350089328
1       350105568
2       350368624
1       350383816
2       350669568
1       350685224
2       350947416
1       350962952
2       351225784
1       351241184
2       351504176
1       351521288
2       351783304
1       351799864
2       352062304
1       352079584
2       352342216
1       352358584
2       352621344
1       352637264
2       352921528
1       352938544
2       353202184
1       353217584
2       353479680
1       353495312
2       353758192
1       353774064
2       354038160
1       354054080
2       354317424
1       354333472
2       354597008
1       354613144
2       354913464
1       355017440
2       355281016
1       355296280
2       355558808
1       355575504
2       355837696
1       355855136
2       356118072
1       356134152
2       356395448
1       356412744
2       356675536
1       356691344
2       356976968
1       356992904
2       357255832
1       357271208
2       357534360
1       357550072
2       357813576
1       357829464
2       358094152
1       358109760
2       358372656
1       358388512
2       358650904
1       358666584
2       358929688
1       358946008
2       359231936
1       359248808
2       359512984
1       359529584
2       359793144
1       359808552
2       360071016
1       360086976
2       360349672
1       360365376
2       360628384
1       360644256
2       360906728
1       360922216
2       361212632
1       361228464
2       361492032
1       361507640
2       361771392
1       361787464
2       362049424
1       362065856
2       362329344
1       362345016
2       362607728
1       362625744
2       362888168
1       362905200
2       363190040
1       363206000
2       363469504
1       363485064
2       363748432
1       363764088
2       364026984
1       364042296
2       364304344
1       364319896
2       364583352
1       364599080
2       364861240
1       364877304
2       365139176
1       365154936
0       0
2       365245808
1       371808112
Press any key to continue ...

dedndave

prescott w/htt, xp mce2005 sp3
Intel(R) Pentium(R) 4 CPU 3.00GHz (SSE3)
Mode Sleep 0    3983 3322 3083 2813 2880 2797 2865 2895 2805 2820 2827 2828 2872
2865 2910 2842 2865 2895 2835 2858 2812 2805 19463 2895 2813 2805 2887 2828 280
5 2880 2873 2865 2888 2843 2910 3165 3000 2797 2857 2798 2820 2857 2828 2850 288
0 2828 2812 2918 2843 2880 2902 2842 2842 2895 2828 2835 2850 2790 2857 2813 281
3 3037 2835 2805 2895 2835 2828 2880 2865 2813 2850 2880 2865 3405 4778 3772 317
3 3037 2820 2812 2873 2827 2835 2933 2857 2910 2910 2902 2880 2880 2835 2880 291
0 2873 2865 2873 2805 2842 2910 2843 2820 2910 2782 3502 3337 2827 2895 2887 280
5 2873 2880 2827 2813 2835 2850 2850 2865 2820 2865 2910 2850 2887 2813 2918 288
8 2865 2827 2880 3082 2828 2835 2895 2835 2888 2812 2805 2850 2888 2827 2842 287
3 5295 3720 4395 3563 3045 3015 3008 2888 2910 2857 2797 2842 2933 2865 2850 289
5 2835 2805 2827 2903 2842 2805 2865 2805 2887 2865 2820 2827 2902 2805 2850 285
0 2865 2843 2805 2783 2888 2888 2820 2932 2865 2925 3082 2857 2812 2858 2835 284
3 2835 2850 2827 2835 2865 2865 2775 2880 2827 2857 3187 3188

Mode SwitchToThread     855 862 855 863 855 855 862 862 862 862 855 863 863 862
877 863 855 855 855 863 870 855 863 863 863 863 855 862 863 855 862 863 863 862
862 855 862 855 855 862 855 862 855 862 862 863 863 862 862 863 863 863 855 863
855 863 862 855 863 855 863 862 855 863 855 863 863 863 862 877 862 855 863 863
877 862 855 870 862 863 862 863 855 863 855 862 855 863 862 862 855 862 862 862
855 863 863 862 863 863 863 855 878 855 855 863 862 863 862 855 862 863 863 862
863 855 863 855 863 863 862 855 863 863 862 862 863 862 855 863 862 863 863 855
855 855 855 863 855 863 855 855 862 863 863 863 862 863 855 863 863 863 855 863
862 862 855 863 862 862 855 855 855 855 862 862 863 855 863 855 855 863 863 855
855 862 862 863 862 862 863 862 863 862 855 862 855 863 855 863 855 863 1147 132
0 862 862 863 863 863 855 863

Mode loop       105 105 105 105 105 105 105 105 105 105 98 105 98 105 105 105 10
5 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 10
5 105 105 105 105 98 105 105 105 105 97 105 105 105 105 105 105 105 105 105 105
105 105 105 105 105 105 105 105 105 105 105 97 105 105 105 105 105 105 105 105 1
05 105 97 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 97 105 105
105 97 105 105 97 98 105 105 105 105 105 105 105 105 105 105 105 105 105 97 105
97 105 105 105 97 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 1
05 105 98 105 105 113 105 97 97 105 98 105 105 105 105 105 105 105 105 105 97 10
5 105 105 105 105 98 105 98 105 105 105 105 105 98 105 105 105 105 105 105 97 10
5 105 105 105 105 97 105 98 105 105 105 105 98 105 105 105 105 105 105 105

Mode nothing    105 105 105 105 105 105 105 105 105 97 105 105 98 105 105 105 10
5 105 98 98 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105
105 105 105 105 105 105 105 105 105 105 105 105 97 105 105 105 105 105 105 105 1
05 105 105 105 105 105 105 105 98 105 105 105 105 105 105 105 98 105 97 105 98 1
05 105 105 105 105 105 105 105 105 105 105 105 105 98 97 105 105 98 105 105 105
105 105 98 105 105 105 105 105 105 105 105 105 105 105 105 105 97 97 105 105 105
105 105 105 105 105 105 105 105 105 105 105 98 105 105 105 105 97 105 105 105 9
8 105 105 105 105 105 105 105 105 97 105 105 105 98 105 105 105 105 105 105 105
105 97 105 105 97 105 105 98 105 105 105 105 105 105 105 105 105 105 105 105 97
105 105 105 105 105 97 105 105 105 105 105 105 105 105 105 105 105 97 105

dedndave

here is a little program i tried....
;###############################################################################################

        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        .686p
        .MMX
        .XMM
        .List

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

        INVOKE  GetCurrentProcess
        INVOKE  SetProcessAffinityMask,eax,1
        INVOKE  Sleep,750
        INVOKE  Sleep,0

        mov     esi,10

Loop00: rdtsc
        push    edx
        push    eax
        INVOKE  Sleep,0
        dec     esi
        jnz     Loop00

        pop     ebx
        pop     edi
        mov     esi,9

Loop01: xchg    eax,ebx
        mov     edx,edi
        pop     ebx
        pop     edi
        sub     eax,ebx
        sbb     edx,edi
        print   ustr$(eax),13,10
        dec     esi
        jnz     Loop01

        print   chr$(13,10)
        inkey
        INVOKE  ExitProcess,0

_main   ENDP

;###############################################################################################

        END     _main

and the results on my machine...
2137
2138
2107
2055
2100
2078
2085
2167
2378


according to Reed Copsey's comment on this page...
http://stackoverflow.com/questions/1383943/switchtothread-vs-sleep1
QuoteIn general, Sleep(0) will be much more likely to yield a timeslice, and
will ALWAYS yield to the OS, even if there are no other threads waiting.

jj2007

Quote from: dedndave on May 11, 2013, 08:32:55 PM
here is a little program i tried...
Results on my machine:
912
912
912
912
888
912
900
912
1152

But what does it measure?

dedndave

i am guessing that's the length of a time-slice, in clock cycles   :P

we could probably bump the process priority level for better results

hutch--

Its been a while but from regularly using SleepEx() you need to set it to 1 or greater, not zero as it immediately returns if nothing else is pending. Back in the PIV days any call to SleepEx() under about 20 yielded about 20 ms anyway so as a rough guess on a PIV the timeslice was about 20 ms.

Antariy

Quote from: dedndave on May 11, 2013, 08:32:55 PM
according to Reed Copsey's comment on this page...
http://stackoverflow.com/questions/1383943/switchtothread-vs-sleep1
QuoteIn general, Sleep(0) will be much more likely to yield a timeslice, and
will ALWAYS yield to the OS, even if there are no other threads waiting.

That's with no doubts, Dave, the Sleep(0) is, for instance, the only way for the code to switch the core, and this behaviour - timeslice dropping - is a "system promise". The only problem is that if there are no other thread waiting angry for a CPU time, i.e., if other threads are in the idle state (Get(Peek)Message/WaitForSignle(Multiple)Objects/Sleep(Ex) etc), then the time that was spent in Sleep(0), crude speaking, is the time that needed to switch to the kernel mode, scheduler checks for the waiting threads, if there are no such threads, then it just reschedules our thread again. This takes not too much cycles, and probably this timing shows your example :t

NtDelayExecution (ntdll) is the underlying thing of Sleep(Ex). It takes two parameters: first one is the the same as second param if SleepEx (bAlertable), and for Sleep it is 0, second one is the pointer to a QWORD which holds the delay in a negative 100-nanoseconds format (this not meant that for user mode code it will make delays with resulution of 100 nanoseconds). I.e., for a delay of 123 milliseconds it need to be called as:


push 0FFFFFFFFh \
push 0FFED3B50h /  (-1230000)
push esp
push 0
call NtDelayExecution
pop edx
pop edx


BTW, your source may be cahanged a bit :biggrin:

;###############################################################################################

        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        include \masm32\include\ntdll.inc
        includelib \masm32\lib\ntdll.lib
        .686p
        .MMX
        .XMM
        .List

;###############################################################################################

.data?
zeroqword   dq  ?

        .CODE

;***********************************************************************************************

_main   PROC

        INVOKE  GetCurrentProcess
        INVOKE  SetProcessAffinityMask,eax,1
        INVOKE  Sleep,750
        INVOKE  Sleep,0

        mov     esi,10

Loop00: rdtsc
        push    edx
        push    eax
        INVOKE  NtDelayExecution,0,offset zeroqword
        dec     esi
        jnz     Loop00

        pop     ebx
        pop     edi
        mov     esi,9

Loop01: xchg    eax,ebx
        mov     edx,edi
        pop     ebx
        pop     edi
        sub     eax,ebx
        sbb     edx,edi
        print   ustr$(eax),13,10
        dec     esi
        jnz     Loop01

        print   chr$(13,10)
        inkey
        INVOKE  ExitProcess,0

_main   ENDP

;###############################################################################################

        END     _main

jj2007

Quote from: Antariy on May 12, 2013, 02:34:36 PMSleep(0) is, for instance, the only way for the code to switch the core

A behaviour which we exclude with SetProcessAffinityMask. When limited to one core, there should be no difference between SwitchToThread and Sleep(0) except that one of them could be a bit slower...

sinsi

What is the return value from SwitchToThread?
QuoteIf there are no other threads ready to execute, the operating system does not switch execution to another thread, and the return value is zero.

jj2007

Quote from: sinsi on May 12, 2013, 03:33:46 PM
What is the return value from SwitchToThread?

Mixed, about 50:50, except for "mode loop" and "nothing" where 1 is more frequent. And no influence on the timings, i.e. it seems you get a fresh timeslice anyway.

Thanks for the hint, John :t

Antariy

Quote from: jj2007 on May 12, 2013, 03:19:25 PM
Quote from: Antariy on May 12, 2013, 02:34:36 PMSleep(0) is, for instance,

A behaviour which we exclude with SetProcessAffinityMask. When limited to one core, there should be no difference between SwitchToThread and Sleep(0) except that one of them could be a bit slower...

After SetProcess/ThreadAffinityMask the executed thread is not switched to specified core right now, and "the only way for the code to switch the core" is Sleep(0). At least according to a MS.
This was a full sense of that quote.