Search code examples
multithreadingcpu-usagemetricsperf

How to measure context switching and duration of thread running


As we know, Linux system has a functionality to switch contexts of threads to archive concurrency. I need to measure some metrics to have them visualized. What metrics I need:

  1. A CPU time when a thread is loaded to a CPU to run
  2. A CPU time when a thread is offloaded from a CPU
  3. A CPU core

Example: I have a process with 2 threads (main and additional)

Metrics:

PID TID Time Event CPU core
1 1 85.0234 CPU takes on this thread 1
1 1 85.1102 CPU takes off this thread 1
1 2 85.1201 CPU takes on this thread 1
1 2 85.1409 CPU takes off this thread 1
1 1 86.0192 CPU takes on this thread 2
1 1 86.1122 CPU takes off this thread 2
1 2 86.2012 CPU takes on this thread 3
1 2 86.2513 CPU takes off this thread 3

Having this metrics I can calculate how much time it took for first Thread(TID 1) to be performed on each iteration. For example, the first iteration is 0.0868 (85.1102 - 85.0234)

Do you know any tools which help me with that? Could you provide some examples to get it started?


Solution

  • Have a look at 'perf sched'

    And for reporting you could check out 'perf sched timehist' and 'perf sched latency'.

    For more information see: https://www.brendangregg.com/blog/2017-03-16/perf-sched.html