Skip to content

Commit cdbd28f

Browse files
committed
Replace System.currentTimeMillis() by System.nanoTime()
Fix #206 #264. System.currentTimeMillis() is not monotonic and a second call may return a value inferior to the first call if the system time changes between the 2 calls. This is why we got negative durations from time to time. System.nanoTime() is monotonic and should avoid getting this problem. (cherry picked from commit 893c955)
1 parent 74066fd commit cdbd28f

File tree

6 files changed

+35
-40
lines changed

6 files changed

+35
-40
lines changed

dev/cosbench-driver/src/com/intel/cosbench/driver/generator/XferCountingInputStream.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ public int read(byte[] b, int off, int len) throws IOException {
4242

4343
private void recordTime() {
4444
if (this.isFirstByte) {
45-
this.xferStart = System.currentTimeMillis();
45+
this.xferStart = System.nanoTime();
4646
this.isFirstByte = false;
4747
}
48-
this.xferEnd = System.currentTimeMillis();
48+
this.xferEnd = System.nanoTime();
4949
}
5050

5151
public long getXferTime() {
52-
long xferTime = this.xferEnd - this.xferStart;
52+
long xferTime = (this.xferEnd - this.xferStart) / 1000000;
5353
return xferTime > 0 ? xferTime : 0L;
5454
}
5555

dev/cosbench-driver/src/com/intel/cosbench/driver/operator/Deleter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static Sample doDelete(String conName, String objName,
6969
if (Thread.interrupted())
7070
throw new AbortedException();
7171

72-
long start = System.currentTimeMillis();
72+
long start = System.nanoTime();
7373

7474
try {
7575
session.getApi().deleteObject(conName, objName, config);
@@ -87,11 +87,10 @@ public static Sample doDelete(String conName, String objName,
8787
op.getSampleType(), op.getName(), false);
8888
}
8989

90-
long end = System.currentTimeMillis();
90+
long end = System.nanoTime();
9191

92-
Date now = new Date(end);
93-
return new Sample(now, op.getId(), op.getOpType(), op.getSampleType(),
94-
op.getName(), true, end - start, 0L, 0L);
92+
return new Sample(new Date(), op.getId(), op.getOpType(), op.getSampleType(),
93+
op.getName(), true, (end - start) / 1000000, 0L, 0L);
9594
}
9695

9796
}

dev/cosbench-driver/src/com/intel/cosbench/driver/operator/FileWriter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public Sample doWrite(InputStream in, long length, String conName, String objNam
137137

138138
XferCountingInputStream cin = new XferCountingInputStream(in);
139139

140-
long start = System.currentTimeMillis();
140+
long start = System.nanoTime();
141141

142142
try {
143143
session.getApi().createObject(conName, objName, cin, length, config);
@@ -153,10 +153,9 @@ public Sample doWrite(InputStream in, long length, String conName, String objNam
153153
IOUtils.closeQuietly(cin);
154154
}
155155

156-
long end = System.currentTimeMillis();
156+
long end = System.nanoTime();
157157

158-
Date now = new Date(end);
159-
return new Sample(now, getId(), getOpType(), getSampleType(),
160-
getName(), true, end - start, cin.getXferTime(), cin.getByteCount());
158+
return new Sample(new Date(), getId(), getOpType(), getSampleType(),
159+
getName(), true, (end - start) / 1000000, cin.getXferTime(), cin.getByteCount());
161160
}
162161
}

dev/cosbench-driver/src/com/intel/cosbench/driver/operator/Lister.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ private Sample doList(OutputStream out, String conName, String objName,
7575
InputStream in = null;
7676
CountingOutputStream cout = new CountingOutputStream(out);
7777
doLogWarn(session.getLogger(), "listerrr: "+ conName + "/" + objName);//###
78-
long start = System.currentTimeMillis();
78+
long start = System.nanoTime();
7979
long xferTime = 0L;
8080
try {
8181
doLogDebug(session.getLogger(), "worker "+ session.getIndex() + " List target " + conName + "/" + objName);
8282
in = session.getApi().getList(conName, objName, config);
83-
long xferStart = System.currentTimeMillis();
83+
long xferStart = System.nanoTime();
8484
copyLarge(in, cout);
85-
xferTime = System.currentTimeMillis() - xferStart;
85+
xferTime = (System.nanoTime() - xferStart) / 1000000;
8686
} catch (StorageInterruptedException sie) {
8787
doLogErr(session.getLogger(), sie.getMessage(), sie);
8888
throw new AbortedException();
@@ -95,11 +95,10 @@ private Sample doList(OutputStream out, String conName, String objName,
9595
IOUtils.closeQuietly(in);
9696
IOUtils.closeQuietly(cout);
9797
}
98-
long end = System.currentTimeMillis();
98+
long end = System.nanoTime();
9999

100-
Date now = new Date(end);
101-
return new Sample(now, getId(), getOpType(), getSampleType(),
102-
getName(), true, end - start, xferTime, cout.getByteCount());
100+
return new Sample(new Date(), getId(), getOpType(), getSampleType(),
101+
getName(), true, (end - start) / 1000000, xferTime, cout.getByteCount());
103102
}
104103

105104
public OutputStream copyLarge(InputStream input, OutputStream output)

dev/cosbench-driver/src/com/intel/cosbench/driver/operator/Reader.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ private Sample doRead(OutputStream out, String conName, String objName,
8282
InputStream in = null;
8383
CountingOutputStream cout = new CountingOutputStream(out);
8484

85-
long start = System.currentTimeMillis();
85+
long start = System.nanoTime();
8686
long xferTime = 0L;
87-
long xferTimeCheck = 0L;
8887
try {
8988
in = session.getApi().getObject(conName, objName, config);
90-
long xferStart = System.currentTimeMillis();
91-
if (!hashCheck){
89+
long xferStart = System.nanoTime();
90+
if (!hashCheck) {
9291
copyLarge(in, cout);
93-
long xferEnd = System.currentTimeMillis();
94-
xferTime = xferEnd - xferStart;
95-
} else if (!validateChecksum(conName, objName, session, in, cout, xferTimeCheck))
92+
} else if (!validateChecksum(conName, objName, session, in, cout)) {
9693
return new Sample(new Date(), getId(), getOpType(),
9794
getSampleType(), getName(), false);
95+
}
96+
long xferEnd = System.nanoTime();
97+
xferTime = (xferEnd - xferStart) / 1000000;
9898
} catch (StorageInterruptedException sie) {
9999
doLogErr(session.getLogger(), sie.getMessage(), sie);
100100
throw new AbortedException();
@@ -107,11 +107,11 @@ private Sample doRead(OutputStream out, String conName, String objName,
107107
IOUtils.closeQuietly(in);
108108
IOUtils.closeQuietly(cout);
109109
}
110-
long end = System.currentTimeMillis();
110+
long end = System.nanoTime();
111111

112-
Date now = new Date(end);
113-
return new Sample(now, getId(), getOpType(), getSampleType(),
114-
getName(), true, end - start, hashCheck ? xferTimeCheck : xferTime, cout.getByteCount());
112+
return new Sample(new Date(), getId(), getOpType(), getSampleType(),
113+
getName(), true, (end - start)/1000000,
114+
xferTime, cout.getByteCount());
115115
}
116116

117117
public OutputStream copyLarge(InputStream input, OutputStream output)
@@ -126,7 +126,7 @@ public OutputStream copyLarge(InputStream input, OutputStream output)
126126
}
127127

128128
private static boolean validateChecksum(String conName, String objName,
129-
Session session, InputStream in, OutputStream out, long xferTimeCheck)
129+
Session session, InputStream in, OutputStream out)
130130
throws IOException {
131131
HashUtil util;
132132
try {
@@ -139,7 +139,6 @@ private static boolean validateChecksum(String conName, String objName,
139139
String storedHash = new String();
140140
String calculatedHash = new String();
141141

142-
long xferStart = System.currentTimeMillis();
143142
int br1 = in.read(buf1);
144143

145144
if (br1 <= hashLen) {
@@ -176,8 +175,7 @@ private static boolean validateChecksum(String conName, String objName,
176175
br1 = br2;
177176
}
178177
}
179-
xferTimeCheck = System.currentTimeMillis() - xferStart;
180-
178+
181179
if (!calculatedHash.equals(storedHash)) {
182180
if (storedHash.startsWith(HashUtil.GUARD)) {
183181
String err =

dev/cosbench-driver/src/com/intel/cosbench/driver/operator/Writer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static Sample doWrite(InputStream in, long length, String conName,
9191
throw new AbortedException();
9292

9393
XferCountingInputStream cin = new XferCountingInputStream(in);
94-
long start = System.currentTimeMillis();
94+
long start = System.nanoTime();
9595

9696
try {
9797
session.getApi()
@@ -110,10 +110,10 @@ public static Sample doWrite(InputStream in, long length, String conName,
110110
IOUtils.closeQuietly(cin);
111111
}
112112

113-
long end = System.currentTimeMillis();
114-
Date now = new Date(end);
115-
return new Sample(now, op.getId(), op.getOpType(), op.getSampleType(),
116-
op.getName(), true, end - start, cin.getXferTime(), cin.getByteCount());
113+
long end = System.nanoTime();
114+
return new Sample(new Date(), op.getId(), op.getOpType(), op.getSampleType(),
115+
op.getName(), true, (end - start) / 1000000,
116+
cin.getXferTime(), cin.getByteCount());
117117
}
118118
/*
119119
* public static Sample doWrite(byte[] data, String conName, String objName,

0 commit comments

Comments
 (0)