Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

How to count the context switches times

Thread view: 
Haitao - 11 Jan 2008 23:45 GMT
Hi All,

Suppose a running Java application have two concurrent threads with the same
priority, and no other user threads exist in the application. If we want to
calculate how many times the context switch happens between these two
threads, or we want to count how many times each thread is interrupted, with
what APIs or how can we do this job?

Thanks!

Tom
j1mb0jay - 12 Jan 2008 00:03 GMT
> Hi All,
>
[quoted text clipped - 7 lines]
>
> Tom

Could you override the yield method for a thread, and output some log
file after calling the super.yield() method?

j1mb0jay
Lew - 12 Jan 2008 00:33 GMT
> Could you override the yield method for a thread, and output some log
> file after calling the super.yield() method?

No.  First of all, you cannot override a static method.  Secondly, even if you
could, the logging wouldn't happen until after the thread comes back into action.

Signature

Lew

Patricia Shanahan - 12 Jan 2008 01:01 GMT
>> Could you override the yield method for a thread, and output some log
>> file after calling the super.yield() method?
>
> No.  First of all, you cannot override a static method.  Secondly, even
> if you could, the logging wouldn't happen until after the thread comes
> back into action.

In any case I suspect that yield is far from the most significant cause
of context switches. What about becoming non-runnable due to waiting for
a monitor, for I/O, or for a lock wait? Or losing the processor due to
the operating system deciding that some other thread needs it more?

Patricia
Haitao - 12 Jan 2008 02:11 GMT
My understanding is that context swithes still exist between two threads
with the same priority, even though there are no blocking codes in each
thread. JVM may use a round-robin algorithm to schedule these threads (That
would be the case of one thread "losing the processor due to the operating
system deciding that some other thread needs it more")

I know that some real-time operating systems (say, VxWorks) provide some
hook functions that will be invoked when a context switch occurs, but I
don't know whether Java (JVM) provides similar functions.

Thanks!

>>> Could you override the yield method for a thread, and output some log
>>> file after calling the super.yield() method?
[quoted text clipped - 9 lines]
>
> Patricia
Lew - 12 Jan 2008 02:29 GMT
> Thanks!

Your use of inline-and-trim posting instead of top-posting will convey your
gratitude sufficiently.

"Patricia Shanahan" wrote:
>> In any case I suspect that yield is far from the most significant cause
>> of context switches. What about becoming non-runnable due to waiting for
>> a monitor, for I/O, or for a lock wait? Or losing the processor due to
>> the operating system deciding that some other thread needs it more?

> My understanding is that context swithes still exist between two threads
> with the same priority, even though there are no blocking codes in each
> thread. JVM may use a round-robin algorithm to schedule these threads (That
> would be the case of one thread "losing the processor due to the operating
> system deciding that some other thread needs it more")

Doesn't "round-robin" mean "doesn't give up the time slice until it blocks"?

Even if not, that is a permissible strategy for the JVM to use in thread
scheduling.

> I know that some real-time operating systems (say, VxWorks) provide some
> hook functions that will be invoked when a context switch occurs, but I
> don't know whether Java (JVM) provides similar functions.

I have not heard of such.

Most Java implementations are not real-time.  Most involve the OS in the
actual thread implementation.

Signature

Lew

Arne Vajhøj - 12 Jan 2008 02:38 GMT
> Doesn't "round-robin" mean "doesn't give up the time slice until it
> blocks"?

No.

http://en.wikipedia.org/wiki/Round-robin_scheduling

http://en.wikipedia.org/wiki/Preemption_%28computing%29

Arne
Lew - 12 Jan 2008 03:19 GMT
>> Doesn't "round-robin" mean "doesn't give up the time slice until it
>> blocks"?
[quoted text clipped - 4 lines]
>
> http://en.wikipedia.org/wiki/Preemption_%28computing%29

Excellent links.

Regardless, there is no requirement that the JVM use round-robin or any other
particular scheduling algorithm.

<http://www.javaworld.com/javaworld/jw-07-2002/jw-0703-java101.html>
> Java does not force a VM to schedule threads in a specific manner or contain a thread scheduler.
...
> Typically, a JVM's thread scheduler chooses the highest-priority thread and allows that thread to run until it either terminates or blocks.

Signature

Lew

Arne Vajhøj - 12 Jan 2008 03:28 GMT
> Regardless, there is no requirement that the JVM use round-robin or any
> other particular scheduling algorithm.

True.

> <http://www.javaworld.com/javaworld/jw-07-2002/jw-0703-java101.html>
>> Java does not force a VM to schedule threads in a specific manner or
>> contain a thread scheduler.

True.

>> Typically, a JVM's thread scheduler chooses the highest-priority
>> thread and allows that thread to run until it either terminates or
>> blocks.

True. But one has to read carefully what it says.

It may be typical for a JVM thread scheduler.

It is not typical for an OS thread scheduler.

And since Java on the most common platforms uses an
OS thread scheduler, then one should not conclude too
much from the above.

Arne
Haitao - 12 Jan 2008 03:24 GMT
> Doesn't "round-robin" mean "doesn't give up the time slice until it
> blocks"?
  Under roound-robin algorithm, a thread has to "give up" (or is deprived
of) the CPU when its time slice is used up.

> I have not heard of such.
This might help.
http://en.wikipedia.org/wiki/Hooking
Lew - 12 Jan 2008 03:48 GMT
>>> I know that some real-time operating systems (say, VxWorks) provide
>>> some hook functions that will be invoked when a context switch occurs,
>>> but I don't know whether Java (JVM) provides similar functions.

Lew wrote:
>> I have not heard of such.

> This might help.
> http://en.wikipedia.org/wiki/Hooking

This says nothing about hooks to trap context switches, nor about Java having
such.  How would it help?

Signature

Lew

Haitao - 12 Jan 2008 04:07 GMT
I am sorry, I mean it might help if you never heard of hooks. It is not
helpful to this question.
> This says nothing about hooks to trap context switches, nor about Java
> having such.  How would it help?
Lew - 12 Jan 2008 04:15 GMT
> I am sorry, I mean it might help if you never heard of hooks. It is not
> helpful to this question.
>> This says nothing about hooks to trap context switches, nor about Java
>> having such.  How would it help?

Please do not top-post.

When I said "I have never heard of such", it was, of course, in reference to
your request about hooks *in Java* to track context switches.  Naturally I've
heard of hooks.

Signature

Lew
A: It makes the conversation harder to read.
Q: Why is it bad?
A: Placing the answer above the question to which one is responding.
Q: What is top-posting?

Gordon Beaton - 12 Jan 2008 09:19 GMT
> Under roound-robin algorithm, a thread has to "give up" (or is
> deprived of) the CPU when its time slice is used up.

That's true of every scheduling algorithm and has nothing to do with
round robin in particular.

Round robin says that each thread is chosen in a fixed order from the
the pool of waiting threads, and given equal priority.

/gordon

--
Arne Vajhøj - 12 Jan 2008 02:35 GMT
> My understanding is that context swithes still exist between two threads
> with the same priority, even though there are no blocking codes in each
[quoted text clipped - 5 lines]
> hook functions that will be invoked when a context switch occurs, but I
> don't know whether Java (JVM) provides similar functions.

A primary design goal for Java was platform independence.

Java does not have many hooks into platform specific functionality.

Java standard does not even specify whether Java threads are OS threads
or a Java abstraction.

Arne
Haitao - 12 Jan 2008 03:43 GMT
> Java does not have many hooks into platform specific functionality.
Then for my question in the top-post (how to count context switch times), is
there any other solutions?

Thanks!
Daniel Pitts - 12 Jan 2008 18:06 GMT
> > Java does not have many hooks into platform specific functionality.
>
> Then for my question in the top-post (how to count context switch times), is
> there any other solutions?
>
> Thanks!

Look into JNI.  It lets you write native c++ code.  You would probably
have to write a version for every JVM/Platform combo that you need to
support.

Really what you should be asking is why do you need that information?
What benefit does it give to the end user?  There may be a different
way to figure out the end result that you're looking for.
Haitao - 13 Jan 2008 05:59 GMT
> Really what you should be asking is why do you need that information?
> What benefit does it give to the end user?  There may be a different
> way to figure out the end result that you're looking for.

In fact I am doing some research related to Real-time Java, and I want to
figure out how much time a context switch between two user threads takes
when the threads are running on a Real-time JVM.

Thanks.
Patricia Shanahan - 13 Jan 2008 14:47 GMT
>> Really what you should be asking is why do you need that information?
>> What benefit does it give to the end user?  There may be a different
[quoted text clipped - 5 lines]
>
> Thanks.

In that case, you could set up a situation in which you know the
approximate number of context switches, because you are forcing them.
You could, for example, have two threads that are both CPU-hogs, but do
a known number of Thread.yield() calls.

Patricia
Daniel Pitts - 13 Jan 2008 18:39 GMT
> > Really what you should be asking is why do you need that information?
> > What benefit does it give to the end user?  There may be a different
[quoted text clipped - 5 lines]
>
> Thanks.

If you're doing that kind of research, then perhaps your best approach
is to add timing into the JVM implementation you're using.  The JVM
should know when a context-switch happens (being a real-time JVM).
Roedy Green - 13 Jan 2008 08:40 GMT
>No.  First of all, you cannot override a static method.  Secondly, even if you
>could, the logging wouldn't happen until after the thread comes back into action.

Further context switches can happen at any time, not just when an app
calls yield.  Threads are handled by the OS, not a co-operative faked
thread scheduler.
Signature

Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com

Gordon Beaton - 12 Jan 2008 09:13 GMT
> Suppose a running Java application have two concurrent threads with
> the same priority, and no other user threads exist in the
> application. If we want to calculate how many times the context
> switch happens between these two threads, or we want to count how
> many times each thread is interrupted, with what APIs or how can we
> do this job?

Java can't tell you but your OS might.

For example Solaris stores context switch counters for each LWP in the
prusage struct in the process file system. You'll need to write some
native code or use one of the proc tools to read it.

See:
http://docs.sun.com/app/docs/doc/816-5174/proc-4?a=view
http://docs.sun.com/app/docs/doc/816-5165/proc-1?a=view

On Linux, /proc/stat contains a system wide context switch counter,
and per-thread information can be found in /proc/PID/stat.

On a lightly loaded system, the information reported by e.g. vmstat
might be sufficient:

http://docs.sun.com/app/docs/doc/816-5166/vmstat-1m?a=view
http://www.linuxmanpages.com/man8/vmstat.8.php

Both platforms also provide information about CPU time per thread,
which might be more helpful depending on what you need it for.

/gordon

--
Eric Sosman - 12 Jan 2008 16:16 GMT
>> Suppose a running Java application have two concurrent threads with
>> the same priority, and no other user threads exist in the
[quoted text clipped - 9 lines]
> native code or use one of the proc tools to read it.
> [...]

    If on Solaris, DTrace seems a natural for this.  Also
available on MacOS X "Leopard" and maybe on FreeBSD (I know
they were working on it, but I don't know if it's ready yet).

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Roedy Green - 13 Jan 2008 08:43 GMT
>Hi All,
>
[quoted text clipped - 3 lines]
>threads, or we want to count how many times each thread is interrupted, with
>what APIs or how can we do this job?

the only tool you have I can think of is the nanotimer. See
http://mindprod.com/jgloss/timer.html.

If you keep sampling the time, and you know the minimum possible time
between two samplings, you know how long the CPU was away doing
something else on that particular run.

Signature

Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com

Tim Smith - 13 Jan 2008 12:08 GMT
> Suppose a running Java application have two concurrent threads with the same
> priority, and no other user threads exist in the application. If we want to
> calculate how many times the context switch happens between these two
> threads, or we want to count how many times each thread is interrupted, with
> what APIs or how can we do this job?

If you are just trying to figure out what the overhead is when the
system decides to switch threads, and so the threads don't have to be
doing useful work, something like this might work.

1. Give each thread an ID number.

2. Have a variable, cur_thread.

3. Each thread does something like this (psuedocode):

  int switchcount = 0
  while (true) {
     if ( cur_thread != our thread ID )
        ++switchcount
     cur_thread = our thread ID
  }

In switchcount, you should have a close approximation at any given time
of the number of times there was a context switch from this thread.

If you put cur_thread in shared memory, you can use this same technique
on many systems to also get process switch counts.

You can also get context switch times by adding an array that has one
entry per thread.  In that array, the thread, each time through the
loop, writes the current time.  When the thread sees that cur_thread is
not its own ID, it can use cur_thread to look in that array for the last
time the other thread ran, and subtract from the current time, to get
the context switch time.

Note that it is quite possible that context switch times when the
context switch is due to a preemptive thread or process switch could
well be quite different from context switch times when a thread or
process does an operation that yields, so don't try to use any benchmark
results such as these where they are not appropriate.

Signature

--Tim Smith



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.