What does “high involuntary context” switches mean?

后端 未结 2 1541
無奈伤痛
無奈伤痛 2021-02-19 01:33

I have re-written a part of code in C. When testing it with logging the resource usage using getrusage(2) C API.

Before changing the code:

相关标签:
2条回答
  • 2021-02-19 02:07

    This is not an answer to exactly your question. Anyway, @Klas points that

    An involontary context switch occurs when a thread has been running too long

    So my idea is that you can check what your threads run too long. Use perf and find places in your code where the context switches happen most often. And possibly compare measuments for the old version of your program with the new one.

    Perf (https://perf.wiki.kernel.org/index.php/Tutorial) has the event context-switches. You can measure it and collect stacktraces where it happens. This is an example of measuring context switches:

    perf record -e cs -g -p `pidof my_test` sleep 5
    

    And then check where they happen. For example, there is a program on C++ with an infinitive loop with no syscalls at all. All switch contents have stracetrace from my function my_thread_func:

    perf report --stdio -g --kallsym=/boot/System.map-2.6.32-431.el6.x86_64
    
    # Samples: 7  of event 'cs'
    # Event count (approx.): 7
    #
    # Overhead  Command      Shared Object                         Symbol
    # ........  .......  .................  .............................
    #
       100.00%  my_test  [kernel.kallsyms]  [k] perf_event_task_sched_out
                |
                --- perf_event_task_sched_out
                    schedule
                    retint_careful
                    my_thread_func(void*)
    

    On the contrary this is an measument for a program on C++ that has an infinitive loop with lots of syscalls:

    # Samples: 6  of event 'cs'
    # Event count (approx.): 6
    #
    # Overhead          Command      Shared Object                         Symbol
    # ........  ...............  .................  .............................
    #
       100.00%  my_test_syscall  [kernel.kallsyms]  [k] perf_event_task_sched_out
                |
                --- perf_event_task_sched_out
                    schedule
                   |
                   |--83.33%-- sysret_careful
                   |          syscall
                   |
                    --16.67%-- retint_careful
                              syscall
    
    0 讨论(0)
  • 2021-02-19 02:08

    A voluntary context switch can occur whenever a thread/process makes a system call that blocks.

    An involuntary context switch occurs when a thread has been running too long (usually something like 10 ms) without making a system call that blocks and there are processes waiting for the CPU.

    It looks like your program is more CPU-intensive now than before. If you have made it multi-threaded then an increase is probably expected.

    821 context switches - depending on the execution time of your program this may or may not be a lot.

    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.

    Update

    Assuming that the load is identical in both cases, it looks like the code modifications have increased the cpu usage. If the increased load is a concern you should analyse the code to find the bottleneck. Instrumentation can be helpful in isolating which part of the code is causing the problem.

    0 讨论(0)
提交回复
热议问题