Skip to content

Linux get context switch timing

I’m implementing some form of internal profiler. Is there a way to know when and for how long a thread is context switched out? I know windows has it w the event tracing api and I know perf logs how many context switches happens. Is there a way to do it on linux? Needing root privileges is not an issue since it will be an internal tool.



Sort of.

See about the getrusage() function.

Notice that the structure it returns has voluntary and involuntary context switch numbers. Also, you have user and system time. Other APIs return wall-clock time.

Any wall-clock time greater than your user and system time is time you weren’t running.

Other than that, you could probably use the kernel ftrace ability. See

Read for even more options.

User contributions licensed under: CC BY-SA
5 People found this is helpful