3

I want to output the number of context switches that are performed by the windows OS while executing my program.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
Ganapa
  • 73
  • 1
  • 9
  • I can't check right now but I seem to recall there's a performance counter for that, if you're on Windows. There are also counters on the chip itself but querying those is very environment-specific. – 500 - Internal Server Error Jan 27 '13 at 07:59
  • Thanking your immediate help. Is there any ways I can do that without using performance counter? Cause I don't know to work with it. – Ganapa Jan 27 '13 at 08:06
  • Well, it sounds like you don't know any other way either :) - here's some info to get you going: http://technet.microsoft.com/en-us/library/cc938606.aspx – 500 - Internal Server Error Jan 27 '13 at 08:10
  • Yes sir , I had this link before and thought there might be some means other than this. – Ganapa Jan 27 '13 at 08:19
  • [This](http://stackoverflow.com/a/12653951/1888362) will print the amount of context switches used by thread 0 of notepad (you can substitute any process and thread number you want in the CounterPathBuffer initialization) every second – Serg Jan 27 '13 at 08:23
  • That was very nice. Thanking you. I will work with it. – Ganapa Jan 27 '13 at 08:26
  • which windows os do you use? – Andreas Grapentin Jan 27 '13 at 12:59
  • I have used Sergmat sir's link to produce below results for notepad application: I have extracted few lines from the o/p which showed instant changes"01/27/2013 18:45:47.523","26.862527043633797"
    "01/27/2013 18:45:48.528","112.35574813481777"
    If I write anything in notepad then the value is as below: "01/27/2013 18:47:28.340","3418.9313164256719"
    "01/27/2013 18:47:29.346","2197.8973136579948"
    – Ganapa Jan 27 '13 at 13:20
  • Sorry for messy output in comments , I was not able to print new line – Ganapa Jan 27 '13 at 13:24
  • What does the output 01/27/2013 18:45:47.523","26.862527043633797" indicate actually?? Does it mean 26.86.. context switches?? If that is the case , then why it is not a whole number?? – Ganapa Jan 29 '13 at 17:06

1 Answers1

1

You can use Performance-counter for this purpose.

From : http://msdn.microsoft.com/en-us/library/aa373083%28VS.85%29.aspx

You can view context switch data in two ways:

  • The System\Context Switches/sec counter in System Monitor reports systemwide context switches.
  • The Thread(_Total)\Context Switches/sec counter reports the total number of context switches generated per second by all threads.