Skip to content

Commit bdb2bc9

Browse files
committed
correctly handle thread CPU time (when sti.threadCpuTime != -1) in applyDiffAtGetResultsMoment()
1 parent d9b1057 commit bdb2bc9

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

visualvm/libs.profiler/lib.profiler/src/org/graalvm/visualvm/lib/jfluid/results/cpu/StackTraceSnapshotBuilder.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -696,33 +696,49 @@ protected long getDumpAbsTimeStamp() {
696696
@Override
697697
protected void applyDiffAtGetResultsMoment(ThreadInfo ti) {
698698
long time0 = getDumpAbsTimeStamp();
699-
long diff = time0 - ti.topMethodEntryTime0;
699+
long diff0 = time0 - ti.topMethodEntryTime0;
700+
long diff1 = getThreadTime(ti, diff0) - ti.topMethodEntryTime1;
700701

701-
if (diff>0) {
702-
applyDiffToTopNode(ti, diff);
703-
ti.diffAtGetResultsMoment = diff;
702+
if (diff0<0) diff0=0;
703+
if (diff1<0) diff1=0;
704+
if (diff0>0 || diff1>0) {
705+
applyDiffToTopNode(ti, diff0, diff1);
706+
ti.diffAtGetResultsMoment0 = diff0;
707+
ti.diffAtGetResultsMoment1 = diff1;
704708
}
705709
}
706710

707711
@Override
708712
protected void undoDiffAtGetResultsMoment(ThreadInfo ti) {
709-
if (ti.diffAtGetResultsMoment>0) {
710-
applyDiffToTopNode(ti, -ti.diffAtGetResultsMoment);
711-
ti.diffAtGetResultsMoment = 0;
713+
if (ti.diffAtGetResultsMoment0>0) {
714+
applyDiffToTopNode(ti, -ti.diffAtGetResultsMoment0, -ti.diffAtGetResultsMoment1);
715+
ti.diffAtGetResultsMoment0 = 0;
712716
}
713717
}
714718

715-
private void applyDiffToTopNode(ThreadInfo ti, long diff) {
719+
private long getThreadTime(ThreadInfo ti, long diff0) {
720+
if (isCollectingTwoTimeStamps()) {
721+
SampledThreadInfo sti = lastStackTrace.get().get(Long.valueOf(ti.threadId));
722+
723+
if (sti!=null) {
724+
if (sti.threadCpuTime != -1) {
725+
return sti.threadCpuTime;
726+
}
727+
if (sti.getThreadState() == Thread.State.RUNNABLE) {
728+
return diff0;
729+
}
730+
}
731+
}
732+
return 0;
733+
}
734+
735+
private void applyDiffToTopNode(ThreadInfo ti, long diff0, long diff1) {
716736
TimedCPUCCTNode top = ti.peek();
717737

718738
if (top instanceof MethodCPUCCTNode) {
719-
top.addNetTime0(diff);
739+
top.addNetTime0(diff0);
720740
if (isCollectingTwoTimeStamps()) {
721-
SampledThreadInfo sti = lastStackTrace.get().get(Long.valueOf(ti.threadId));
722-
723-
if (sti!=null && sti.getThreadState() == Thread.State.RUNNABLE) {
724-
top.addNetTime1(diff);
725-
}
741+
top.addNetTime1(diff1);
726742
}
727743
}
728744
}

visualvm/libs.profiler/lib.profiler/src/org/graalvm/visualvm/lib/jfluid/results/cpu/ThreadInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public class ThreadInfo {
5252
// processing of data is resumed, we need to subtract this value back from that node.
5353
// This is effectively the self time for the last invocation of the top method on stack - if we would not keep
5454
// it separately, it would not be reported
55-
long diffAtGetResultsMoment; // diff between last methodEntry and current moment timestamp -
56-
// we will have to compensate for the processing time
55+
long diffAtGetResultsMoment0; // diff between last methodEntry and current moment timestamp - we will have to compensate for the processing time
56+
long diffAtGetResultsMoment1; // as above, but for thread CPU time
5757

5858
//~ Instance fields ------------------------------------------------------------------------------------------------------
5959
final private Object stackLock = new Object();

0 commit comments

Comments
 (0)