2

I am trying to determine the impact of context switching on my Java thread. Therefore I would have to know first "how many times has it been preempted?" and second, if possible, "how long was my thread suspended from execution?". I would doubt very much that this can be done from Java, but hey! the fun things is to do the impossible, right? :) Even If I have to recompile the VM or to use some kind of LINUX tool or hack I would be happy. So now that we know that can't be done, let's do it. :)

EDIT: Come on people. The other question was priority related. I guess you just enjoy easy and boring questions. The question is clear and valid.

JohnPristine
  • 3,485
  • 5
  • 30
  • 49
  • 2
    This smacks a lot like premature optimization to me. You are asking for things that a normal JVM does not provide. Can you edit your question and explain why you need to do this? – Gray Mar 29 '12 at 16:44
  • 1
    Possible duplicate of http://stackoverflow.com/questions/9916876/is-it-possible-to-create-a-high-priority-thread-with-a-regular-jvm-one-that-wil – Gray Mar 29 '12 at 16:44
  • 1
    Even if it could be done, there is almost no value in this. It's going to depend highly on what CPU your testing, plus a whole lot of other environmental factors, plus variability given what code you are testing with. – ControlAltDel Mar 29 '12 at 16:46
  • See my edit. This question is clear and valid. – JohnPristine Mar 29 '12 at 17:39
  • 1
    Maybe `kernelshark` provides that information? Otherwise, `perf` or `oprofile` may show something. – ninjalj Mar 29 '12 at 19:47

2 Answers2

4

If you can identify the Linux TID (thread ID) for the Java thread you're interested in (assuming your JVM maps Java threads onto Linux threads) then the pseudo-file /proc/<tid>/status contains many statistics, including voluntary_ctxt_switches and nonvoluntary_ctxt_switches.

caf
  • 233,326
  • 40
  • 323
  • 462
2

It clearly doesn't do everything you want, but you may find the ThreadMXBean available in java.lang.management useful.

Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
  • That's already something. If contention time has increased that means the thread was blocked / interrupted. And people want to close the question. ts ts ts – JohnPristine Mar 29 '12 at 18:59