Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non voluntary context switches: How can I prevent them?


I have a small application that I was running right now and I wanted to check if I have any memory leaks in it so I put in this piece of code:

for (unsigned int i = 0; i<10000; i++) {
    for (unsigned int j = 0; j<10000; j++) {
        std::ifstream &a = s->fhandle->open("test");
        char temp[30];
        a.getline(temp, 30);
        s->fhandle->close("test");
    }
}

When I ran the application i cat'ed /proc//status to see if the memory increases. The output is the following after about 2 Minutes of runtime:

Name:   origin-test
State:  R (running)
Tgid:   7267
Pid:    7267
PPid:   6619
TracerPid:  0
Uid:    1000    1000    1000    1000
Gid:    1000    1000    1000    1000
FDSize: 256
Groups: 4 20 24 46 110 111 119 122 1000 
VmPeak:   183848 kB
VmSize:   118308 kB
VmLck:         0 kB
VmHWM:      5116 kB
VmRSS:      5116 kB
VmData:     9560 kB
VmStk:       136 kB
VmExe:        28 kB
VmLib:     11496 kB
VmPTE:       240 kB
VmSwap:        0 kB
Threads:    2
SigQ:   0/16382
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000002004
SigCgt: 00000001800044c2
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: ffffffffffffffff
Cpus_allowed:   3f
Cpus_allowed_list:  0-5
Mems_allowed:   00000000,00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:    120
nonvoluntary_ctxt_switches: 26475

None of the values did change except the last one, so does the mean there are no memory leaks?

But what's more important and what I would like to know is, if it is bad that the last value is increasing rapidly (about 26475 switches in about 2 Minutes!).

I looked at some other applications to compare how much non-volunary switches they have:

  1. Firefox: about 200
  2. Gdm: 2
  3. Netbeans: 19

Then I googled and found out some stuff but it's to technical for me to understand. What I got from it is that this happens when the application switches the processor or something? (I have an Amd 6-core processor btw).

How can I prevent my application from doing that and in how far could this be a problem when running the application?

Thanks in advance, Robin.

like image 497
Robin Avatar asked Dec 13 '10 15:12

Robin


People also ask

How can involuntary context switches be reduced?

If you want to reduce the number of context switches you can reduce the number of worker threads so there are less threads than there are CPU cores. Assuming that the load is identical in both cases, it looks like the code modifications have increased the cpu usage.

What causes an involuntary context switch?

Involuntary context switch refers to the context switch that occurs when the process is forced to be rescheduled by the system due to time slice has expired. For example, involuntary context switches are prone to occur when a large number of processes are competing for CPU.

What is the solution of context switching?

One of the best ways to deal with context switching is to design your schedule so that you avoid context switching.

How can we reduce the time taken for context switching?

The context switch time is dependent on the registers you have to save / restore. One way you can possibly save time is via the AVX extensions on new processors, which allow you to save/restore all of the registers to one block of memory.


1 Answers

Voluntary context switch occurs when your application is blocked in a system call and the kernel decide to give it's time slice to another process.

Non voluntary context switch occurs when your application has used all the timeslice the scheduler has attributed to it (the kernel try to pretend that each application has the whole computer for themselves, and can use as much CPU they want, but has to switch from one to another so that the user has the illusion that they are all running in parallel).

In your case, since you're opening, closing and reading from the same file, it probably stay in the virtual file system cache during the whole execution of the process, and youre program is being preempted by the kernel as it is not blocking (either because of system or library caches). On the other hand, Firefox, Gdm and Netbeans are mostly waiting for input from the user or from the network, and must not be preempted by the kernel.

Those context switches are not harmful. On the contrary, it allow your processor to be used to fairly by all application even when one of them is waiting for some resource.≈

And BTW, to detect memory leaks, a better solution would be to use a tool dedicated to this, such as valgrind.

like image 160
Sylvain Defresne Avatar answered Oct 07 '22 03:10

Sylvain Defresne