Reputation: 5986
I was wondering if it's possible to detect and perform some action every time my code detects that a CPU context switch has occurred (as opposed to counting how many context switches occurred in a certain period of time). Anyway to do this in, say, C#?
Upvotes: 4
Views: 728
Reputation: 4573
I think it could be useful info that it's possible with pure C with getrusage()
struct rusage {
...
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
But i don't know about C#
Upvotes: 0
Reputation: 100555
Context switch is very low level kernel activity so it is unlikely (I don't know for sure) for such hooks to exist.
You definitely will not be able to do it with code running in normal .Net run-time at least because it does not let one to completely control threads and does not run in kernel mode.
Now if the question is "Can I write kernel code in C#" than - yes* (*- I don't believe there is available compiler for C# that will produce native code that can be executed at kernel level - write your own).
Upvotes: 1
Reputation: 35925
No, this is not possible to do in C#. Because C# is a managed language that runs code in a user mode. Context switch is originated from a kernel mode to which no access is possible from the user level code.
This might be possible with a C driver, however, the code may severely damage OS performance and stability.
Upvotes: 6