Kontextwechsel (context switches) verstehen und Probleme damit beheben
Geschrieben von Thomas Koetzing am Sonntag, 12 November 2006
Schon mal von Kontextwechsel (context switches) gehört? Wenn ja, dann sicher weil Sie Administrator für eine Citrix Presentation Server Farm sind. Das ist kein Citrix bedingtes Problem aber Kontextwechsel gehören zu den Standard Werten die vom Citrix Resource Manager überwacht werden und führen offensichtlich häufig zu Alarm Meldungen.
Andere Systeme können auch Probleme mit den Kontextwechseln haben, jedoch ohne Monitoring bekommt es niemand mit.
Kontextwechsel verstehen
Ich werde hier nicht in Details verfallen und die Beschreibung soll auf einfache Weise die Grundlagen zu Kontextwechsel erklären.
Prozesse bestehen aus so genannten "Threads" die die eigentliche Arbeit verrichten. Threads werden geplant und auf der System CPU ausgeführt (auf einer CPU nicht auf allen im System vorhandenen CPU's), wobei immer nur ein Thread je CPU zurzeit ausgeführt werden kann. Zudem kann ein Prozess eine Vielzahl von Threads haben. Die vorgegebene Dauer der Ausführung heißt "Quantum" bedeutet aber nicht, dass ein Thread unbedingt das gesamte Quantum nutzen darf, noch dass nach Ablauf des Quatum's die Arbeit des Threads beendet ist. Allerdings wenn das Quatum beendet ist, wird ein Kontextwechsel auf den nächsten geplanten Thread vollzogen (nur ein Grund für einen Kontextwechsel, auch das vorzeitige beenden ist ein Kontextwechsel).
Sollte der Performance Zähler für Kontextwechsel (pro Sekunde) einen hohen Wert aufweisen, dann bedeute es das Threads weniger Zeit für ihre Arbeit haben und damit kann die Performance des gesamten Systems fallen.
Zwei andere Definitionen von Kontextwechsel:
Microsoft "Die durchschnittliche Rate pro Sekunde, in der der Prozessor den Kontext zwischen Threads wechselt. Eine hohe Rate impliziert das viele Threads um Prozessor Zeit ringen."
Windows Internals "Mit Kontextwechsel ist der Vorgang gemeint, bei dem der temporäre Prozessorstatus des aktiven Threads gespeichert, der temporäre Prozessorstatus eines anderen Threads geladen und die Ausführung des neuen Threads gestartet wird."
Grund für hohe Kontextwechsel Raten
Oft ist der Grund eine zu kleine Auslagerungsdatei oder wo die Auslagerungsdatei dynamisch wachsen kann. Auch möglich ist der schreib Zwischenspeicher der (RAID) Kontroller das mit dem Microsoft Utility (oder vom Hersteller) dskcache verändert werden kann. Hohe aktive Raten können auch durch ineffiziente Hardware- oder schlecht Programmierte Anwendungen hervorgerufen werden.
Behebung von Problemen mit Kotextwechseln
Wie so oft gibt es unterschiedliche Wege das Problem aufzufinden und zu beheben aber Hauptziel ist das Auffinden des Prozess der eine hohe Rate an Kontextwechsel verursacht. Dabei ist zu berücksichtigen, dass evtl. bessere Hardware notwendig ist.
Wie kann nun der aktuelle Wert des Kontextwechsels für das System festgestellt werden? Die Antwort ist mit Microsoft’s Perforamnce Zählern (perfmon.msc) unter System/Kontextwechsel oder Threads/Kontextwechsel. Wer sich im Performance Monitor die Kontextwechsel basierend auf Thread ansieht, wird feststellen das es nicht einfach ist den Prozess mit hohen Raten zu finden.
Deutlich besser ist hier Sysinternals Prozess Explorer (kostenfrei). Als Standard zeigt der Prozess Explorer aber keine Kontextwechsel an und muss erst aktiviert werden.
Das geht unter view | select columns | Process Performance | aktivieren von Context Switches und Context Switch Delta
Beide Werte sollten nun je Prozess im Hauptfenster des Prozess Explorer sichtbar sein. Die Spalte der Kontextwechsel (Context Switches) zeigt die Gesamtzahl an Wechsel seit dem Start des Systems. Diese Spalte nach dem höchsten Wert sortieren und nach a) hohe Werte- und b) schnell wachsende Werte schauen. Beides sind gute Indikatoren für Prozesse mit hohen Kontextwechseln.
Prozess Explorer - Spalte der Context Switches
Dann sollte die Spalte CSwitch Delta geprüft werden, da diese den Kontextwechsel Wert pro Aktualisierungsintervall des Prozess Explorers anzeigt (mit "update speed" = 1 Sekunde ergibt sich Kontextwechsel pro Sekunde). Wurde der Prozess gefunden, so ist zu prüfen warum dieser so hohe Kontextwechsel aufweist.
Prozess Explorer - Spalte der CSwitches Deltas
Werte für "schlechte" Kontextwechsel
Der Standardwert für Kontextwechsel "Roter Alarm" beim Citrix Resource Manager ist 14.000 gilt aber für eine CPU. Der Wert ist je Prozessor und mit zwei CPU’s sollte der Wert 28.000 oder 42.000 mit drei CPU's bzw. 56.000 für ein Quad CPU System. Das sind trotzdem nur Basis Werte und um einen guten Wert zu erhalten muß das System eine gewisse Zeit betrachtet werden.
Auf dem BriForum 2006 EU habe ich mit Tim Mangan gesprochen und er sagte, das Kontextwechsel eine nicht zu hohe Beachtung bekommen sollten.
Great Article Geschrieben von Guest am 2006-11-16 04:41:11Thomas. As Usual a well written well thought out article. I learned enough about context switches then I ever knew before. Thanks I learned something today.
Remmi Geschrieben von Remmi am 2007-10-06 00:29:51Nicht zu hohe Beachtung ist ok.. sind aber die Kontextswitche zu hoch, was durch Anwendungen verursacht werden kann, legt sich der Terminalserver teilweise ins Koma, ohne das eine extreme CPU-Last beobachtet werden kann. Zur Fehlersuche bei diesem Fall sehr gut.
Jochem Geschrieben von Guest am 2007-01-13 23:45:47Perfect explanation! Thank you very much.
Thanks Thomas Geschrieben von Guest am 2007-02-11 06:18:39Yes, I always see this Context Switches problem on my Citrix Test environment running on Virtual Server VMWare. Thanks.
context switches Geschrieben von Gast am 2007-05-29 13:43:19sehr gut erklärt.
Many thanks Geschrieben von Guest am 2007-08-22 12:13:10Many thanks for the great site and help you have become to deal with the citrix beast
Mario
beta Geschrieben von Guest am 2007-10-09 14:02:11Hi, thanks very much for the informations. What is the good parameter for the servers with 2 physical CPU but with quadcores? (seems 8 CPU) thanks!
Super Hilfe, aber... Geschrieben von Gast am 2008-01-11 15:30:03.. wie sieht es mit Hyperthreading aus. Was sollte man dort einstellen?
vielen Dank
Hyperthreading only core CPU's count Geschrieben von Thomas Koetzing am 2008-01-17 15:23:35Hyperthreading ist NUR eine virtuelle Instanz einer CPU die aber natürlich von dieser auch geleistet werden muss. Daher zählen nur Core CPU's, sollte doch klar sein.
Only core CPU's are really counting.
Klasse Erklärung Geschrieben von Gast am 2008-05-09 14:01:18Vielen Dank dafür und weiter so ...
Erst Klassig Geschrieben von Guest am 2008-05-20 16:07:30Jetzt muss ich finden was es ist in Domino das verursacht die hohe context switching. Page file?
Now I have to find out what it is in Domino that is causing the high context switching. Page file?
Gruesse aus South Carolina.
jason vlad Geschrieben von Guest am 2008-09-25 22:26:10With multiple CPU i have to duble or more. Now i know. Thank U.
Vielen Dank! Geschrieben von Gast am 2009-02-24 11:45:29Großartig zusammengefasste Erklärung Genau so etwas habe ich gerade gebraucht!
Markus Merz Geschrieben von Gast am 2009-03-20 17:39:22Schicke Erklärung! Eine gute weiterführende Ergänzung zum Thema liefert auch der sysinternals newsletter im Absatz using process explorer to track cpu usage. Auszug:
Quote:
Another way to determine process execution, therefore, is to examine the number of context switches that the threads in a process have incurred. When a thread is selected to run (scheduled), its context switch count is incremented.. You can see the total number of context switches that have occurred in each process by adding the Context Switch column (click on View->Select Columns). But a more interesting number is the Context Switch Delta column. This displays the number of context switches that have occur in each process in between Process Explorer's refresh interval (which by default is 1 second).
So, for a very different view of process activity on your system, add the Context Switch Delta column and sort by it. You will see many processes with threads that are running that do not show up as consuming any CPU time, because the threads are running in between the 10ms clock interval. Some of these processes are performing needless polling (such as querying the registry or checking for changes in a folder). That is just plain sloppy programming. Others may be performing useful work, but are running "under the radar" of the system's time accounting mechanisms. It's your job to determine the wheat from the chaff.
Another way to determine process execution, therefore, is to examine the number of context switches that the threads in a process have incurred. When a thread is selected to run (scheduled), its context switch count is incremented.. You can see the total number of context switches that have occurred in each process by adding the Context Switch column (click on View->Select Columns). But a more interesting number is the Context Switch Delta column. This displays the number of context switches that have occur in each process in between Process Explorer's refresh interval (which by default is 1 second).
So, for a very different view of process activity on your system, add the Context Switch Delta column and sort by it. You will see many processes with threads that are running that do not show up as consuming any CPU time, because the threads are running in between the 10ms clock interval. Some of these processes are performing needless polling (such as querying the registry or checking for changes in a folder). That is just plain sloppy programming. Others may be performing useful work, but are running "under the radar" of the system's time accounting mechanisms. It's your job to determine the wheat from the chaff.
europe and decimal point Geschrieben von Guest am 2009-08-20 14:44:33to save a doh moment or head scratching.... for those not in europe the threshold is 14 thousand. not 14 . Europeans sometimes use a dot for thousands seperator.
Decimal point, comma etc. Geschrieben von Guest am 2010-05-05 00:05:02Working in an international setting where one never is quite sure whether the reader assumes a decimal point or a decimal comma, I have started to use an apostrophe for the thousand separator. This makes numbers less ambiguous: 14'000 is not likely to be confused with 14 and three zeros after the decimal character.