How to count the context switches times

H

Haitao

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
 
J

j1mb0jay

Haitao said:
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

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

j1mb0jay
 
L

Lew

j1mb0jay said:
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.
 
P

Patricia Shanahan

Lew said:
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
 
H

Haitao

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!
 
L

Lew

Haitao said:

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

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.
 
A

Arne Vajhøj

Haitao said:
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.

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
 
A

Arne Vajhøj

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


True.

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
 
H

Haitao

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!
 
H

Haitao

I am sorry, I mean it might help if you never heard of hooks. It is not
helpful to this question.
 
L

Lew

Haitao said:
I am sorry, I mean it might help if you never heard of hooks. It is not
helpful to this question.

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.

--
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?
 
G

Gordon Beaton

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

--
 
G

Gordon Beaton

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

--
 
E

Eric Sosman

Gordon said:
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.
[...]

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).
 
D

Daniel Pitts

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.
 
H

Haitao

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,797
Messages
2,569,647
Members
45,377
Latest member
Zebacus

Latest Threads

Top