Is it possible to schedule a given task to run exactly n
machine instructions before control is returned to the user?
The motivation for this question is the debugging of multithreaded programs, where this could be helpful to reliably reproduce certain bugs or undefined behaviour.
I'm particularly interested in the case of x86_64-linux
running on an Intel CPU, but solutions for other architectures or operating systems would also be interesting.
The documentation for the kernel perf
suite says
Performance counters are special hardware registers available on most modern CPUs. These registers count the number of certain types of hw events: such as instructions executed, cachemisses suffered, or branches mis-predicted - without slowing down the kernel or applications. These registers can also trigger interrupts when a threshold number of events have passed.
so it seems like the hardware could support this in principle, but I'm not sure if this is exposed in any way to the user.
Of course it's possible to just use ptrace
to single-step the program n
times, but that would make all but the most simple programs impossibly slow.