Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Performance of Context-Switches under Win32

4 views
Skip to first unread message
Message has been deleted

Phil Armstrong

unread,
Sep 11, 2007, 4:38:49 AM9/11/07
to
Elcaro Nosille <Elcaro....@mailinator.com> wrote:
> 2003 Server and Vista? Or would anyone write a comparable benchmark
> for a Linux/Unix-system?

Take a look at the lmbench Linux benchmark suite.

Phil

--
http://www.kantaka.co.uk/ .oOo. public key: http://www.kantaka.co.uk/gpg.txt

Chris Thomasson

unread,
Sep 11, 2007, 6:57:31 AM9/11/07
to
"Elcaro Nosille" <Elcaro....@mailinator.com> wrote in message
news:46e643c4$0$4536$9b4e...@newsspool3.arcor-online.net...
>I wrote a little program that measures the performance of a context
> -switch between two threads under Win32 (source is below). [...]

> __declspec(naked)
> DWORD __stdcall CmpXchgAtomic32( DWORD volatile *pdwWhere, DWORD
> dwComparand, DWORD dwNew )
> {
> __asm
> {
> mov edx, [esp + 4 + 0] // pdwWhere
> mov eax, [esp + 4 + 4] // dwComparand
> mov ecx, [esp + 4 + 8] // dwNew
> cmpxchg [edx], ecx
> ret (4 + 4 + 4)
> }
> }
[...]

If the CMPXCHG instruction above happens to be executed concurrently by two
or more threads, that are not bound to the same processor, then should you
not have the LOCK prefix? IIRC, XCHG is the only instruction that asserts
the LOCK implicitly...

Message has been deleted

Chris Thomasson

unread,
Sep 11, 2007, 5:05:36 PM9/11/07
to
"Elcaro Nosille" <Elcaro....@mailinator.com> wrote in message
news:46e69888$0$16102$9b4e...@newsspool1.arcor-online.net...
> Chris Thomasson schrieb:

[...]
>> If the CMPXCHG instruction above happens to be executed concurrently
>> by two or more threads, that are not bound to the same processor, then
>> should you not have the LOCK prefix? IIRC, XCHG is the only instruction
>> that asserts the LOCK implicitly...
>
> Chris, your bumbledom is really bothersome.

Are you trying to piss me off or something?


> Of cours these operations are
> performed on the same CPU and I'm pretty sure you discovered that yourself
> when you read the code.
[...]

Yes I did.

[...]

Chris Thomasson

unread,
Sep 11, 2007, 5:14:06 PM9/11/07
to
"Elcaro Nosille" <Elcaro....@mailinator.com> wrote in message
news:46e69888$0$16102$9b4e...@newsspool1.arcor-online.net...
> Chris Thomasson schrieb:
[...]
> Your chaotic programming stye, the many
> bugs in your source code

The code I post in USENET is mostly pseudo-code sketches of algorithms I am
thinking about. I type this code out in the newsreader:

http://groups.google.com/group/comp.programming.threads/msg/785ffb9cd422533c

If you want to see some actual source code look here:

http://appcore.home.comcast.net

> and your self-corrections in follow-ups to
> your own postings speak a strong language; i.e. I'm pretty sute you
> have a subcortical mental disporder of this kind.

Posting corrections brings you to the conclusion of a mental disorder? I
feel sorry for your patients! Ouch.

> I'm pretty sure multithreading is a obsession for you because you miss
> the feeling of control in your mind. It's a counter-phobic behaviour to
> your uncertainness.

I love multi-threading.

Patrick Schaaf

unread,
Sep 12, 2007, 2:33:03 AM9/12/07
to
"Chris Thomasson" <cri...@comcast.net> writes:

>> and your self-corrections in follow-ups to
>> your own postings speak a strong language; i.e. I'm pretty sute you
>> have a subcortical mental disporder of this kind.

>Posting corrections brings you to the conclusion of a mental disorder? I
>feel sorry for your patients! Ouch.

Trolls don't have patience.

winsto...@gmail.com

unread,
Sep 13, 2007, 2:45:21 AM9/13/07
to
On Sep 11, 12:31 am, Elcaro Nosille <Elcaro.Nosi...@mailinator.com>
wrote:

> I wrote a little program that measures the performance of a context
> -switch between two threads under Win32 (source is below). I ran it
> under XP/SP2 and got about 7700 cycles for a thread-switch in the
> best case on a Ahthlon64 X2 3800+. Can anyone of you compile an run
> this tool on other Win32 -incarnations; preferrably under Windows

> 2003 Server and Vista? Or would anyone write a comparable benchmark
> for a Linux/Unix-system?
> I think I'll write another app that spawns two processes to measure
> the context-switch performance between processes.
>
> Thanks in advance,
> Elcaro
>
> #include <windows.h>
> #include <stdio.h>
>
> DWORD WINAPI ThreadProc( LPVOID lpvThreadParam );
>
> union QWUNION
> {
> DWORDLONG dwl;
> struct {
> DWORD dwLo,
> dwHi;
> };
>
> };
>
> HANDLE hEvtStart;
> #pragma pack(push, 8)
> DWORDLONG volatile dwlIdAndTick = 0;
> DWORD volatile dwFastestThreadSwitch = (DWORD)-1;
> #pragma pack(pop)
>
> int main()
> {
> HANDLE ahThreads[2];
>
> SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS );
> SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );
>
> ::hEvtStart = CreateEvent( NULL, TRUE, FALSE, NULL );
> ahThreads[0] = CreateThread( NULL, 0, ThreadProc, (LPVOID)2, CREATE_SUSPENDED, NULL );
> ahThreads[1] = CreateThread( NULL, 0, ThreadProc, (LPVOID)3, CREATE_SUSPENDED, NULL );
> SetThreadAffinityMask( ahThreads[0], 1 );
> SetThreadAffinityMask( ahThreads[1], 1 );
> SetThreadPriority( ahThreads[0], THREAD_PRIORITY_HIGHEST );
> SetThreadPriority( ahThreads[1], THREAD_PRIORITY_HIGHEST );
> ResumeThread( ahThreads[0] );
> ResumeThread( ahThreads[1] );
>
> SetEvent( ::hEvtStart );
> WaitForMultipleObjects( 2, ahThreads, TRUE, 10000 );
>
> TerminateThread( ahThreads[0], 0 );
> TerminateThread( ahThreads[1], 0 );
>
> printf( "fastest thread-switch: %d cycles\n", (int)::dwFastestThreadSwitch );
> getchar();
>
> return 0;
>
> }
>
> DWORDLONG __fastcall GetTSC();
> DWORD __stdcall CmpXchgAtomic32( DWORD volatile *pdwWhere, DWORD dwComparand, DWORD dwNew );
> DWORDLONG __stdcall XchgAtomic64( DWORDLONG volatile *pdwlWhere, DWORDLONG dwlNew );
>
> DWORD WINAPI ThreadProc( LPVOID lpvThreadParam )
> {
> DWORD dwMyId,
> dwOtherId;
> QWUNION qwuTick,
> qwuIdAndTick,
> qwuPrevIdAndTick,
> qwuPrevTick,
> qwuTicks;
> DWORD dwFoundId;
> DWORD dwFtsComparand,
> dwXchgPrev;
>
> WaitForSingleObject( ::hEvtStart, INFINITE );
> Sleep( 20 ); // necessary because of scheduler-bug
>
> dwMyId = (DWORD)(size_t)lpvThreadParam << 28;
> dwOtherId = dwMyId ^ ((DWORD)1 << 28);
>
> for( ; ; )
> {
> do
> {
> qwuTick.dwl = GetTSC();
> qwuIdAndTick = qwuTick;
> qwuIdAndTick.dwHi = (qwuIdAndTick.dwHi & (DWORD)0x0FFFFFFF) | dwMyId;
> qwuPrevIdAndTick.dwl = XchgAtomic64( &::dwlIdAndTick, qwuIdAndTick.dwl );
> dwFoundId = qwuPrevIdAndTick.dwHi & (DWORD)0xF0000000;
> qwuPrevTick.dwl = qwuPrevIdAndTick.dwl & (DWORDLONG)0x0FFFFFFFFFFFFFFF;
> qwuTicks.dwl = qwuTick.dwl - qwuPrevTick.dwl;
> } while( dwFoundId != dwOtherId || qwuTicks.dwHi != 0 );
>
> dwFtsComparand = ::dwFastestThreadSwitch;
>
> while( qwuTicks.dwLo < dwFtsComparand )
> if( (dwXchgPrev = CmpXchgAtomic32( &::dwFastestThreadSwitch, dwFtsComparand, qwuTicks.dwLo )) == dwFtsComparand )
> break;
> else
> dwFtsComparand = dwXchgPrev;
> }
>
> return 0;
>
> }
>
> __declspec(naked)
> DWORDLONG __fastcall GetTSC()
> {
> __asm
> {
> rdtsc
> ret

> }
>
> }
>
> __declspec(naked)
> DWORD __stdcall CmpXchgAtomic32( DWORD volatile *pdwWhere, DWORD dwComparand, DWORD dwNew )
> {
> __asm
> {
> mov edx, [esp + 4 + 0] // pdwWhere
> mov eax, [esp + 4 + 4] // dwComparand
> mov ecx, [esp + 4 + 8] // dwNew
> cmpxchg [edx], ecx
> ret (4 + 4 + 4)
> }}
>
> __declspec(naked)
> DWORDLONG __stdcall XchgAtomic64( DWORDLONG volatile *pdwlWhere, DWORDLONG dwlNew )
> {
> __asm
> {
> push edi
> push ebx
> mov edi, [esp + 12 + 0] // pdwlWhere
> mov ebx, [esp + 12 + 4] // dwlNew.lo
> mov ecx, [esp + 12 + 8] // dwlNew.hi
>
> mov eax, [edi] // previous value into edx:eax
> mov edx, [edi + 4] // "
>
> writeLoop:
> cmpxchg8b [edi]
> jnz writeLoop
>
> pop ebx
> pop edi
> ret (4 + 8)
> }
>
> }

What is the scheduler bug that requires you to sleep 20 ms? Also, you
say it takes about 7700 cycles for a context switch, how much time in
seconds is that? I, too, am trying to figure out how much time it
takes to perform a thread and process switch in windows xp.

wk

Message has been deleted
0 new messages