Pin processor CPU Isolation on Windows

↘锁芯ラ 提交于 2020-12-08 07:19:19

问题


In linux where I work mostly, we use a technique called CPU isolation, that effectively locks a process on to a processor and also prevents the processor from running anything else. Our kernel guys did some magic to handle the interrupts.

In windows the closest thing I found is an affinity concept which appears to bind a process/thread to a processor. But it makes no guarantee that the processor is ONLY running that process/thread meaning there can still be context switch and other jitter.

Is there a way to have a CPU isolated on windows for semi-deterministic running times?


回答1:


There's no exact way to to this in user mode. And Windows mostly tries to prevent apps from hogging up all the system resources. But you could probably isolate the majority of other processes to another core/processor such that your code can run on a core mostly dedicated to it.

Start by looking at this code here on MSDN on enumerating processes. Except your enumeration code will specify PROCESS_SET_INFORMATION as the flag to OpenProcess. You'll likely need your code running with admin privs to do this for processes not running as your NT creds. For each process handle obtained, call SetProcessAffinityMask to set the process to run on all cores but one. Then set your your process to run another on the reserved core you culled out from all the other process handles.

This won't handle other processes from starting up and getting load balanced to the core you are trying to run your code on.

Your mileage may vary. If this is just for testing or for machines dedicated to your code, then it will probably be fine. If this is for a commercial app meant to run alongside all other apps, customers will be terribly disappointed to see all their other apps and services tank when they run your code. Tread carefully.



来源:https://stackoverflow.com/questions/15324586/pin-processor-cpu-isolation-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!