How to count the context switches times

R

Roedy Green

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

Roedy Green

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?

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

Tim Smith

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

Patricia Shanahan

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

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
 
D

Daniel Pitts

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.

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

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
474,261
Messages
2,571,040
Members
48,769
Latest member
Clifft

Latest Threads

Top