Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting the CPU's hypervisor flag in C#

I am working on a distributed computing project and I want to run fewer tasks on computers that are virtual machines. How can I detect the hypervisor flag from the CPU in C#?

I know there are lots of answers that involve checking the motherboard manufacturer, display adapter, or MAC address for certain strings, but those seem like fragile implementations.

like image 531
Kevin Chen Avatar asked Oct 21 '22 03:10

Kevin Chen


1 Answers

Those fragile implementations seem like your best bet. Languages like C# and Java were never really intended to run close enough to bare metal to easily enable robust implementations of runtime bare metal interactions, hence the lack of pointers and automatic GC. Modern VM implementations actually are supposed to even make it difficult for you to tell you're running on a VM in the first place.

That said, if you know the hypervisor IP addresses (as the link suggests you could), you could check the IP of the request and deal with it accordingly, provided of course that the IP of your hypervisor is static or you know the exact range of IP addresses it could have.

PS: Going further, you could also make a list of all IPs -- again, if they're static -- and write your own load balancer of sorts to direct the traffic as you wish. Although, I'm just curious, why are you trying to offload tasks from VMs? Are there computationally intensive tasks for which you want to cut out as much overhead as possible?

like image 139
gfish3000 Avatar answered Oct 27 '22 10:10

gfish3000