Linux context switch - loop takes long time to process

Hi Experts,
I am running a test program and want to check how many context switches is happening within that process.  Basically, this busy loop takes more than
20 seconds to run in a system. Takes 2 seconds to run in my PC.

I want to know if there are other issues and whether the thread is running or context switched etc.  How can I investigate this?

I tried the following command: watch -n2 grep ctxt /proc/5846/status
Which gives these numbers?  Would nonvoluntary_ctxt_switches be this big?

voluntary_ctxt_switches:      0
nonvoluntary_ctxt_switches:   24747
#include <stdio.h>

void busyLoop()

    volatile unsigned long long i;
    printf("start test\n");
    for(i = 0; i < 1000000000ULL; ++i);
    printf("done test\n");

int main()
        return 0;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

That's not particularly large for a process that runs in an infinite loop. It basically means that your process was taken off the CPU around 10 times a second (assuming the 20s runnign time). There are easily more than 10 process running on your system simultaneously...
What is "this system"
Maybe it is 10 years old with very high HZ ? Like 1000 Hz on linux 2.4 ?
ambuliAuthor Commented:
Thank JKR.  I think the problem I am investigating is something to do with thread priority.  When a pthread_create is called with NULL for attr does it use the calling threads priority or does it get created with a default one?

(The problem I am seeing is that one of the thread appeared to be hang for a while about 20 to 30 seconds and then runs.  The place it gets stuck does not make sense.. so I was investigating whether that thread get pre-empted by another.  The system is mostly idle when I was doing the testing).  Any suggestion to debug the issue.... thanks.

// this is the cpu info:  This is an embedded env.

Processor      : FA726TE rev 1 (v5l)
BogoMIPS      : 794.62
Features      : swp half thumb edsp
CPU implementer      : 0x66
CPU architecture: 5TE
CPU variant      : 0x05
CPU part      : 0x726
CPU revision      : 1
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

As for the docs: and - if that argument is NULL, the default atttribute is used, which means

           Thread attributes:
                   Detach state        = PTHREAD_CREATE_JOINABLE
                   Scope               = PTHREAD_SCOPE_SYSTEM
                   Inherit scheduler   = PTHREAD_INHERIT_SCHED
                   Scheduling policy   = SCHED_OTHER
                   Scheduling priority = 0
                   Guard size          = 4096 bytes
                   Stack address       = 0x40196000
                   Stack size          = 0x201000 bytes

(from the 2nd link)
BTW, as an additional side note: this ins not unusual for a process running a very long loop. Usuallym your process would make several system calls, which would result in a higher value of 'voluntary_ctxt_switches', since every one of these would yield CPU time. the reason why you see only 'nonvoluntary_ctxt_switches' is just because of the fact that your code doesn't.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
32 TLB entries
My laptop:
TLB size        : 1536 4K pages

Which means that your embedded CPU has to take process off CPU much more often to reload TLBs, especially for 1000Hz timer of operating system that you hide from us.

Since you dont tell we cannot offer any help. You must reduce timer to 100Hz typical in arm9 times, or try tickless kernel.

BTW there is no wonder that 5 years old embedded CPU is 10x slower than normal todays CPU.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.