[Autogenerated] let's see some aspects of affinity in action.
In fact,
learn quite a bit about scheduling even without knowing
exactly how the internal algorithms work simply by using
tools such as performance monitor.
So before I look at performance monitor, let's open task manager.
Now with task manager there is the option to right
click and select set affinity here.
And this allows us to change the affinity of a process,
essentially calling the set process affinity mask function behind the scenes.
You can see that by default,
every process will have a full affinity but you can remove
certain processors if we so desire and that's it.
That would change the affinity for that particular process.
Now let's go back to CPU stress here.
Here's another CPU stress instance.
I've already configured performance monitor to show these two first two threads.
Currently the first threat number one is running with low activity.
The second one is not running at all.
That's the science threat here.
And that's why it's state is always five which means waiting.
So if I let it run as well we can see that's going to do some work
and go into state number two from time to time.
Let's change both of these two maximum activity.
So this means they're both now should be always in ST number two.
So you can see the state number two all the time.
And that's because they both want to run all the time and
I have enough processors to go around.
But now let's stress things out a little bit by reducing the affinity so we
can change the affinity using task manager as we've seen.
It's also possible to do the same thing with Process Explorer but in fact we
can do that within CPU stress itself under the process menu.
There's the affinity we can change.
We can see the full affinity here.
We can reduce the affinity for example.
Let me just remove most of these processors and just leave two processors.
It doesn't really matter which ones for this example let's just
leave two of them when we have two processors only.
We have two threads that I want that want to run all the time.
Then in most cases they will run.
However sometimes it might not run exactly.
So notice that I'm going to play with the wild little bit here.
And so it's not always going to show up very easily
because of the low into survival of updates.
That performance monitor has only once per second and a lot
of things could change because we know that these Quantum's
are around 30 milliseconds.
So a lot of changes could happen between updates but still
let's do something a bit different by adding another threat
that is running with low low activity.
So notice what happens now this third thread is not
shown here in the graph at all.
But we can see its effects,
notice that now we have a new state we haven't seen just yet.
And the new state state number one, if you go back to the state machine diagram,
you'll notice it is the state already,
which means that at some point uh these threads the blue and the sign
threats go into into state number one going to the ready state because
at that point in time the third thread was running and so one of them
had to go to the ready state.
There's no other way because there's just two processors to go around.
Let's remove that threat for now.
Let's pause it, Go back to the case where they're always running,
at least when we don't notice any UI activity that is
being shown by performance monitor.
But now let's complicate things further by removing one of the processors again,
doesn't really matter which.
So at this point we have to threats and only one processor.
So obviously there's no way that could be running at the same time.
There's just no way.
So this is what we're going to see when one of the
threats in the state number two running,
the other one has to be in state number one ready.
Sometimes they might both be in the ready state if the ui thread is
doing something or some other threat in the process,
but there's no way both of them will be in ST number two,
there's just no way in most cases this is what we'll see,
we'll see this changing exactly that point where one of them is
running in the other one is ready because really there's just
only one processor to go around so you can learn quite a bit
about what's happening here.
Let's take the first threat and increase its priority to
above normal to be slightly higher.
And if it's slightly higher then we expected to run
all the time or almost all the time.
Notice the blue thread now takes control and it has a higher priority than the
science thread and so it's running most of the time but not all of the time
because at some point you can see there's a switch here and that's because of
the starvation avoidance that I mentioned.
So every four seconds.
Also we have a peak in priority here.
Although it doesn't seem to get to 15 something might have changed in recent
Windows 11 bills but definitely there is a peak here now in most cases won't be
able to actually notice that because again this only happens for a single
quantum and it's more likely than not to miss it.
However he is another example of this actually happened and here's another
we're very lucky here you probably don't know how much luck you're in because
usually you can just spend the minutes looking at that and it might never be
caught exactly in the act so to speak.
But now we've caught that three times.
So this is really um unusual.
I would say I remember I was writing the book the Windows internals book.
I tried to get to this scenario when I did I just
immediately captured the screen shot and moved on.
But now it actually happens quite a bit.
Maybe some things have changed internally in the timing of this
algorithm of this avoidance for starvation.
So you can learn quite a bit about how things work simply by
utilizing tools and by the way if you're thinking how can you tell
the exact times these changes are happening?
Um not just roughly what we see in performance monitor for that.
You would want to use event tracing for Windows or E T W.
Which is a much more powerful and precise mechanism where you can get
intimate and precise details as to when switches are happening and priority
boosts are happening such as this one and so on.
In fact it happens quite a bit here.
So I'm really happy that we are able to see that kind of live catch these
threats in the act because of the starvation that the science threat has
here because it doesn't get enough CPU time.
So from time to time it gets a boost