Link to home
Start Free TrialLog in
Avatar of ambuli
ambuliFlag for United States of America

asked on

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()
{
        busyLoop();
        return 0;
}

Open in new window

Avatar of jkr
jkr
Flag of Germany image

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 ?
Avatar of ambuli

ASKER

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.

gheist:
// 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
As for the docs: http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_create.html and http://man7.org/linux/man-pages/man3/pthread_attr_init.3.html - 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)
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial