Windows Live Alerts
EnglishDeutsch
|
Contact
|  
Welcome to ThomasKOetzing.de!
   
 
Start access
Article
Support Forum
SBC FAQ
xenApp (Presentation Server)
Remote Desktop Services
Terminal Services
Web Interface
Tips & Tools
Sponsors 
 
ControlUP 3.0 Smart-X

Understanding and Troubleshooting context switches Print E-mail
Written by Thomas Koetzing at Sunday, 12 November 2006 | Article editor: Shawn Bass

Article Details 
 
User Rating:   | 167
PoorBest 

Ever heard of "context switches"? If yes, then you are probably an Administrator for a Citrix Presentation Server farm. Now this is not a Citrix related problem but the context switches are one of the default performance counters in the Citrix Resource Manager and obviously the most often raised alert. Other systems might also have issues with context switches but if there isn't an alerting monitor you will never know.


Understanding Context Switches


I don't want to go too much into details here, so this is only a very simple basic description of context switches but it should help you understand what it really means.

Processes contain "threads" that are doing the work; they are scheduled and run on the system CPU (at one CPU not on all available CPU's). A process can have multiple threads but only one thread can run on a CPU at a time. The amount of time a thread runs is called "quatum" and when the time is over the system "switches" to the next thread in line (This is the normal case for a switch) – a "context switch" happened.


If the performance counter shows high context switches, it means that threads have less time to do their work and the system performance might go down. At that time the Citrix Resource Manager or any other monitor will raise an alert to inform the Administrator that something is wrong.


Two other definition of context switches:


Microsoft

"The average rate per second at which the processor switches context among threads. A high rate can indicate that many threads are contending for processor time."


Windows Internals

"When Windows selects a new thread to run, it performs a context switch to it. A context switch is the procedure of saving the volatile state associated with a running thread, loading another thread’s volatile state, and starting the new thread’s execution."



Cause of high Context Switches


Common issue I have encountered is a too small page file or where the page file could dynamically grow (start- and end size not set to the same value). Also an option is the write cache of a (RAID) controller that you might want to change using Microsoft's dskcache utility (or the vendor tool). High activity rates can also result from inefficient hardware or poorly designed applications.



Troubleshooting Context Switches

As always there are different ways to troubleshoot such problem but the main target is to find the process(es) that are generating high context switches. Keep in mind that you might need better hardware.


Now how can I find the amount context switches on the system? The answer is the Microsoft performance counter (perfmon.msc) under system/context switches or thread/context switches.
Looking at the performance monitor for context switches based on threads is hard to figure out what process(es) is causing the high rate.


A better utility is sysinternals process explorer. By default process explorer doesn't show context switches and needs to be set in view | select columns | Process Performance | Activate context switches and context switch delta.


Image  Image

   


You should see both columns in the main view of process explorer. The context switches row shows the total number of switches since the system boot time. Sort the row and look for a) high values and b) fast growing values both are good indicator for high switch rates of the process(es).


Image
Process Explorer - Context Switches row


Next check the CSwitch Delta row for a high value, since the value shows the context switches made per process explorer refresh interval (if the "update speed" is set to one second, then you have Context Switches / Sec). Once you have found the process(es) you should find out why the process is generating those context switches.


Image
Process Explorer - CSwitches Delta row

 




Values for "bad" Context Switches / sec


The default context switches "red alert" value for Citrix Resource Manager is 14.000 but is for a single CPU. The value is per CPU and if the system has two CPU’s you should change the value to 28.000 or 42.000 for three CPU's and 56.000 for a quad CPU system. Still these values are just some basic suggestions and for a "good" value you have to monitor your system over time.


Anyway I have talked to Tim Mangan at BriForum 2006 EU and he said that context switches is something you should not constantly keep an eye on.


References



Comments


Great Article
Written by Guest on 2006-11-16 04:41:11
Thomas. 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
Written by Remmi on 2007-10-06 00:29:51
Nicht 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
Written by Guest on 2007-01-13 23:45:47
Perfect explanation!  
Thank you very much.


Thanks Thomas
Written by Guest on 2007-02-11 06:18:39
Yes, I always see this Context Switches problem on my Citrix Test environment running on Virtual Server VMWare. Thanks. :eek


context switches
Written by Gast on 2007-05-29 13:43:19
sehr gut erklärt.


Many thanks
Written by Guest on 2007-08-22 12:13:10
Many thanks for the great site and help you have become to deal with the citrix beast 
 
Mario


beta
Written by Guest on 2007-10-09 14:02:11
Hi, 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...
Written by Gast on 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
Written by Thomas Koetzing on 2008-01-17 15:23:35
Hyperthreading 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
Written by Gast on 2008-05-09 14:01:18
Vielen Dank dafür und weiter so ...


Erst Klassig
Written by Guest on 2008-05-20 16:07:30
Jetzt 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
Written by Guest on 2008-09-25 22:26:10
With multiple CPU i have to duble or more. Now i know. Thank U.


Vielen Dank!
Written by Gast on 2009-02-24 11:45:29
Großartig zusammengefasste Erklärung :) 
Genau so etwas habe ich gerade gebraucht!


Markus Merz
Written by Gast on 2009-03-20 17:39:22
Schicke 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.




sysinternals newsletter
Written by Gast on 2009-03-20 17:42:11
Quelle: sysinternals newsletter 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.




europe and decimal point
Written by Guest on 2009-08-20 14:44:33
to 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.
Written by Guest on 2010-05-05 00:05:02
Working 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.


Context Switching in the millions?
Written by Guest on 2010-11-01 11:55:04
I'm using ProcXP and I'm seeing the top 25-30 processes having context switches in the millions. e.g.  
pnamain.exe 1,113,632  
imasrv.exe 11,125,833  
System Idle process 75,000,000 
 
I expected to see these values in thousands. Am I missing something? 
 
Thanks, 
Paul


Context Switching above 500K
Written by Guest on 2011-08-26 09:40:25
Thank you a lot Thomas. I am in the process of analysing the same on our servers as CS/sec. are crossing 500K as per the graph in RM. 
 
Thanks again!


Good article
Written by Guest on 2012-12-20 16:44:44
Nice Article ...simple and easy to understand :)


Thanks a ton
Written by Guest on 2013-05-02 06:21:57
Very well written and easy to understand...Keep it up and keep sharing the knowledge :)


NOTE  
NOTE  You have to register in the Forum to post comments with your name.

Write Comment
Name:Guest
Title:
BBCode:Web AddressEmail AddressBold TextItalic TextUnderlined TextQuoteCodeOpen ListList ItemClose List
Comment:




Code Verification
CAPTCHA Security Code Security Code *


 
find or follow me @