What is Windows Priority and Affinity and what advatanges does it provide?

58,386

Solution 1

Setting affinity does do something, but you'll never want to use it.

Setting the CPU affinity forces Windows to only use the CPU (or cores) selected. If you set the affinity to a single CPU, Windows will only run that application on that CPU, never on any others.

Windows automatically puts run applications on the least-busy processor, so limiting it to a single CPU doesn't let Windows do its job. Even if CPU/core 1 is busy running other applications, Windows won't be able to run an application with affinity set on CPU/Core 2.

Really the only reason you would want to do that is to run an old application that doesn't work correctly when running on a multi-CPU/Core system.

Solution 2

This is a very useful feature in certain scenarios. Let's say you have a multi-threaded application that tends to either be idle or aggressively grabbing 100% of every CPU for several minutes, doing searches, builds, etc. Let's call this application "eclipse."

Let's also say that while you're working on this application, you have a bunch of other applications that have modest cpu requirements, but are essentially real-time applications. For example, while you're using Eclipse and it is randomly kicking off builds or doing gwt compiles, you're also using your computer to stream music or perform research in a browser window (for example, researching the cause of a build problem). Sure, you will not die if your music skips or your browser stops responding, but it's annoying.

What affinity lets you do is confine your cpu eating app to 7/8 cores so that everyone else is guaranteed access to a relatively unused cpu and you don't constantly have to deal with stuttering and interruptions to the usability of everything else on your computer while eclipse is grinding away.

Solution 3

Setting affinity tells that process what processors it's allowed to run on.

While very useful for some niche cases, the average user probably shouldn't mess with it.

For instance if a process were allowed its own core to run it could run in (near) real-time without those 70 windows utilities constantly interrupting and stack-swapping on the processor for their own time slice. Real-time applications were something that windows could never do before multi-processor/multi-core systems hit the scene because the OS would constantly be interrupting/task-switching the application for its own purposes. That can now mostly be overcome by isolating the real-time application one processor while preventing all the other applications in the system from using that processor. This is a very niche subject but systems like (real) flight simulators, factory automation, and control feedback systems depend on real-time architecture to work.

Processor intensive applications (like VMs) can be isolated to their own core so you can use them without bringing the rest of your system to a crawl. In theory, a hypervisor running on a processor that supports bare-metal hypervisor interaction can reach processor performance equal to independent OS running on its own (minus the processor needed to run the host OS). Of course, in practice even a VM running on its own isolated core/processor will still need to accept small amount of overhead from the host the host OS.

For applications that handle a large amount of data in flux, isolating the application to its own processor (and potentially still utilizing multiple cores) will cut down on cache swapping.

Older applications that break when they're spread across multiple processors can effectively be limited to one core/processor to fix the issue.

If you were doing performance measurements on a specific application, it's near impossible to get consistent results across different systems unless you can isolate the process because, otherwise, you have no control over how much time the OS gives your application. Most people agree that measuring runtime performance doesn't give good results but those people have never considered that the OS intervention (that makes results so inconsistent) can be limited by using affinity.

There are plenty of cases where affinity is vital but, if you don't know what they are, you probably won't need it.

Solution 4

A perfect example of this is old computer games (or other software), especially when 32-bit games (applications) are emulated on a modern 64-bit computer. By setting the affinity for old games limiting them to only four cores, crashes can OFTEN be avoided to enable stubburn games to start. Some rendering engines used by OLD games, video editers, and hardware-accelerated graphics software, or CAD software, does not understand more than four CPU cores and will crash whan launched.

I'm not creating an account just to post this, to find me google 'kieseyhow'

Solution 5

I use Windows Priority when my system becomes unresponsive to me (click and wait) or I notice some process is taking a long time or running too fast with regard to another process. For example, if I run a Jupyter Notebook and some other scripts in a command window, the notebook will hog all the resources. So, I go in and either raise the command prompt's priority or lower the notebook's priority to make the system more balanced overall.

Windows Affinity I find most useful on a system with multiple processors. By default, all processes will run on all processors, but this can bog down the system with moving data from one Numa node to another. I've found that if I segregate parts of a workload to to specific nodes, I get a 3X difference in performance overall. This is definitely worthwhile when doing something compute intensive for long periods on a system with multiple processors.

Share:
58,386

Related videos on Youtube

Prerak Diwan
Author by

Prerak Diwan

I am an author for the Real Python website and Software Assurance Engineer at NASA's Jet Propulsion Lab. You can follow me on twitter: @mertz_james Or check out my blog: Mertz Musings There are no winners or losers in the race of life, only finishers and quitters. #SOreadytohelp on the following topics: Python DOORS DXL

Updated on September 17, 2022

Comments

  • Prerak Diwan
    Prerak Diwan almost 2 years

    What exactly is Priority and Affinity (found within Task Manager) and what are they used for:

    Found within Task Manager

    In what situations should/could they be used and what advantages are found while customizing these setting.

  • Prerak Diwan
    Prerak Diwan almost 14 years
    Sweet thanks! So what's a good guide to using afinity?
  • Moab
    Moab over 13 years
  • Synetech
    Synetech almost 12 years
    > you'll never want to use it. Not true. If you have a single-threaded application, then it cannot use the CPU’s threads/cores to their fullest extent and there is nothing that Windows can do to force/trick it to. If you have two single-threaded applications, you can tell Windows to have each one use a separate thread/core so that instead of having the total CPU usage hover around 50-75%, you can max it out to 100% (each core being used to the maximum by one program). So yes, you can indeed make use of the setting, but it is a specific scenario that most people won’t know/realize/need.
  • shf301
    shf301 almost 12 years
    If you have two single threaded applications Windows will run each on a different thread/core and you'll get 100% CPU utilization without setting affinity. To test I just ran 4 instances of a single threaded program on my 4 core machine and got 100% CPU usage. Windows is smart enough to schedule CPU's efficiently.
  • Synetech
    Synetech almost 12 years
    I guess it depends on the system and/or version of Windows. I have seen it (XP) hover both cores at 50-75% where both processes were set to both cores and manually setting them maxed out the CPU.
  • David Schwartz
    David Schwartz over 11 years
    @Synetech: That seems almost impossible. It would have to have been moving the two processes from core to core, always both at the same time.
  • Jamie Hanrahan
    Jamie Hanrahan over 9 years
    A good guide to using affinity: "Don't".
  • Jamie Hanrahan
    Jamie Hanrahan almost 9 years
    The "only 20% CPU time available to other processes" is not an accurate description. The actual implementation is that a thread that opts into the multimedia class scheduler is allowed to run for up to 80% of each scheduling interval in the real-time priority class. It doesn't necessarily have to use that much, and if it doesn't. more than 20% will be available to other threads.
  • Eyal Roth
    Eyal Roth almost 6 years
    Another valid use case for this: There are application which may utilize all of the available CPUs and may prevent other programs from running well and / or make the OS respond very slowly to user input. In this case, someone might decide to limit such an application to only some of the CPUs. As a developer I use this feature frequently when profiling my CPU-intensive applications, as the act of profiling itself requires CPU.
  • Jamie Hanrahan
    Jamie Hanrahan over 5 years
    You would be much better off in this case simply setting your CPU hog process to a low priority.
  • Jamie Hanrahan
    Jamie Hanrahan over 5 years
    The thing that makes affinity a whole lot less useful than many think it should be is this: setting your process affinity to some subset of the available CPUs. does not shut anything else out of those CPUs. To do that you'd have to go to all other processes in the system to keep them out of the CPU you want to have for yourself. That's almost never practical.
  • Evan Plaice
    Evan Plaice over 5 years
    It's possible using 3rd party software. Why MS doesn't include this functionality by default is beyond me.
  • Jamie Hanrahan
    Jamie Hanrahan over 5 years
    Because, as I wrote, it isn't all that useful. And it is very prone to misuse. In fact the usual result of being too "creative" with affinity is that things aren't running when they otherwise could be. It's usually better to run a thread on other than what you think is the preferred CPU than not at all (after you've foolishly set affinity to deny it access to CPUs that are otherwise idle). Windows' default "ideal processor" and "previous processor" mechanism accomplishes good cache coherency without such the blunt instrument of the affinity mask.
  • J. Dimeo
    J. Dimeo over 2 years
    @EyalRoth in that case though just lowering the priority works for me, so that my development tools and IDEs take priority and remain responsive. I've never needed to use affinity for this purpose.