Programmatically Define Context Switches

I was wondering if it is possible to detect and perform an action every time my code detects that a processor context transition has occurred (as opposed to counting the number of context switches for a certain period of time). Anyway, to do this, say, in C #?

+4
source share
3 answers

No, this cannot be done in C #. Because C # is a managed language that runs code in user mode. The context switch starts from kernel mode, which cannot be accessed from user level code.

This may be possible with the C driver, however, the code can seriously damage the OS performance and stability.

+6
source

The context switch is a very low core activity, so it is unlikely (I don't know for sure) that such hooks exist.

You definitely will not be able to do this with code that runs in normal .Net runtime, at least because it does not allow full control over threads and does not start in kernel mode.

Now, if the question is: β€œCan I write kernel code in C #”, than - yes * (* - I do not believe that there is an available compiler for C # that will generate its own code that can be executed at the kernel level - write your own).

+1
source

I think that information may be useful, which is possible with pure C with getrusage ()

struct rusage { ... long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ }; 

But I do not know about C #

0
source

Source: https://habr.com/ru/post/1434244/


All Articles