Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/java/org/tikv/common/log/SlowLogImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ public class SlowLogImpl implements SlowLog {
private Throwable error = null;

private final long startMS;
/**
* use System.nanoTime() to calculate duration, cause System.currentTimeMillis() is not monotonic
*/
private final long startNS;

private final long slowThresholdMS;

/** Key-Value pairs which will be logged, e.g. function name, key, region, etc. */
private final Map<String, String> properties;

public SlowLogImpl(long slowThresholdMS, Map<String, String> properties) {
this.startMS = System.currentTimeMillis();
this.startNS = System.nanoTime();
this.slowThresholdMS = slowThresholdMS;
this.properties = new HashMap<>(properties);
}
Expand All @@ -56,7 +62,7 @@ public void addProperty(String key, String value) {

@Override
public synchronized SlowLogSpan start(String name) {
SlowLogSpan slowLogSpan = new SlowLogSpanImpl(name);
SlowLogSpan slowLogSpan = new SlowLogSpanImpl(name, startMS, startNS);
if (slowLogSpans.size() < MAX_SPAN_SIZE) {
slowLogSpans.add(slowLogSpan);
}
Expand All @@ -71,7 +77,8 @@ public void setError(Throwable err) {

@Override
public void log() {
long currentMS = System.currentTimeMillis();
long currentNS = System.nanoTime();
long currentMS = startMS + (currentNS - startNS) / 1_000_000;
if (error != null || (slowThresholdMS >= 0 && currentMS - startMS > slowThresholdMS)) {
logger.warn("SlowLog:" + getSlowLogString(currentMS));
}
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/org/tikv/common/log/SlowLogSpanImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,38 @@

public class SlowLogSpanImpl implements SlowLogSpan {
private final String name;
private final long requestStartNS;
private final long requestStartMS;

private long startMS;
private long endMS;
/**
* use System.nanoTime() to calculate duration, cause System.currentTimeMillis() is not monotonic
*/
private long startNS;

private long endNS;

public SlowLogSpanImpl(String name) {
public SlowLogSpanImpl(String name, long requestStartMS, long requestStartNS) {
this.name = name;
this.requestStartMS = requestStartMS;
this.requestStartNS = requestStartNS;
this.startMS = 0;
this.startNS = 0;
this.endMS = 0;
this.endNS = 0;
}

@Override
public void start() {
this.startMS = System.currentTimeMillis();
startNS = System.nanoTime();
startMS = requestStartMS + (startNS - requestStartNS) / 1_000_000;
}

@Override
public void end() {
this.endMS = System.currentTimeMillis();
endNS = System.nanoTime();
endMS = startMS + (endNS - startNS) / 1_000_000;
}

@Override
Expand Down