Permalink
Browse files

Replaced some occurrences of currentTimeMillis() withnanoTime()

  • Loading branch information...
1 parent 3e57100 commit 696434b4f9c4836fae7201927933462a368573ff Bela Ban committed Feb 13, 2012
@@ -34,7 +34,7 @@
/** Is set as soon as the request has received all required responses */
protected final Condition completed=lock.newCondition();
- protected final Message request_msg;
+ protected final Message request_msg;
protected final RequestCorrelator corr; // either use RequestCorrelator or ...
protected final RequestOptions options;
@@ -170,7 +170,7 @@ protected static long getRequestId() {
/** This method runs with lock locked (called by <code>execute()</code>). */
@GuardedBy("lock")
- protected boolean responsesComplete(long timeout) throws InterruptedException {
+ protected boolean responsesComplete(final long timeout) throws InterruptedException {
if(timeout <= 0) {
while(!done) { /* Wait for responses: */
if(responsesComplete()) {
@@ -180,30 +180,29 @@ protected boolean responsesComplete(long timeout) throws InterruptedException {
}
completed.await();
}
- return responsesComplete();
}
else {
- long start_time=System.currentTimeMillis();
- long timeout_time=start_time + timeout;
- while(timeout > 0 && !done) { /* Wait for responses: */
+ long wait_time=TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
+ long target_time=System.nanoTime() + wait_time;
+ while(wait_time > 0 && !done) { /* Wait for responses: */
if(responsesComplete()) {
if(corr != null)
corr.done(req_id);
return true;
}
- timeout=timeout_time - System.currentTimeMillis();
- if(timeout > 0) {
- completed.await(timeout, TimeUnit.MILLISECONDS);
+ wait_time=target_time - System.nanoTime();
+ if(wait_time > 0) {
+ completed.await(wait_time, TimeUnit.NANOSECONDS);
}
}
if(corr != null)
corr.done(req_id);
- return responsesComplete();
}
+ return responsesComplete();
}
@GuardedBy("lock")
- protected boolean waitForResults(long timeout) {
+ protected boolean waitForResults(final long timeout) {
if(timeout <= 0) {
while(true) { /* Wait for responses: */
if(responsesComplete())
@@ -212,32 +211,19 @@ protected boolean waitForResults(long timeout) {
}
}
else {
- long start_time=System.currentTimeMillis();
- long timeout_time=start_time + timeout;
- while(timeout > 0) { /* Wait for responses: */
+ long wait_time=TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
+ long target_time=System.nanoTime() + wait_time;
+ while(wait_time > 0) { /* Wait for responses: */
if(responsesComplete())
return true;
- timeout=timeout_time - System.currentTimeMillis();
- if(timeout > 0) {
- try {completed.await(timeout, TimeUnit.MILLISECONDS);} catch(Exception e) {}
+ wait_time=target_time - System.nanoTime();
+ if(wait_time > 0) {
+ try {completed.await(wait_time, TimeUnit.NANOSECONDS);} catch(Exception e) {}
}
}
return false;
}
}
-
- /*public static String modeToString(int m) {
- switch(m) {
- case GET_FIRST: return "GET_FIRST";
- case GET_ALL: return "GET_ALL";
- case GET_MAJORITY: return "GET_MAJORITY";
- case GET_ABS_MAJORITY: return "GET_ABS_MAJORITY";
- case GET_NONE: return "GET_NONE";
- default: return "<unknown> (" + m + ")";
- }
- }*/
-
-
}
@@ -1769,7 +1769,7 @@ private void handleMyMessage(Message msg, boolean multicast) {
int num_msgs=0;
@GuardedBy("lock")
int num_bundling_tasks=0;
- long last_bundle_time;
+ long last_bundle_time; // in nanoseconds
final ReentrantLock lock=new ReentrantLock();
final Log log=LogFactory.getLog(getClass());
@@ -1819,7 +1819,7 @@ private void addMessage(Message msg) {
SingletonAddress dest=new SingletonAddress(cluster_name, dst);
if(msgs.isEmpty())
- last_bundle_time=System.currentTimeMillis();
+ last_bundle_time=System.nanoTime();
List<Message> tmp=msgs.get(dest);
if(tmp == null) {
tmp=new LinkedList<Message>();
@@ -1837,13 +1837,13 @@ private void addMessage(Message msg) {
*/
private void sendBundledMessages(final Map<SingletonAddress,List<Message>> msgs) {
if(log.isTraceEnabled()) {
- long stop=System.currentTimeMillis();
double percentage=100.0 / max_bundle_size * count;
StringBuilder sb=new StringBuilder("sending ").append(num_msgs).append(" msgs (");
num_msgs=0;
sb.append(count).append(" bytes (" + f.format(percentage) + "% of max_bundle_size)");
if(last_bundle_time > 0) {
- sb.append(", collected in ").append(stop-last_bundle_time).append("ms) ");
+ long diff=(System.nanoTime() - last_bundle_time) / 1000000;
+ sb.append(", collected in ").append(diff).append("ms) ");
}
sb.append(" to ").append(msgs.size()).append(" destination(s)");
if(msgs.size() > 1) sb.append(" (dests=").append(msgs.keySet()).append(")");
@@ -23,17 +23,14 @@
private T result=null;
private volatile boolean hasResult=false;
- public Lock getLock() {
- return lock;
- }
-
- public Condition getCond() {
- return cond;
- }
+
+ public Lock getLock() {return lock;}
+ public Condition getCond() {return cond;}
+
/**
* Blocks until a result is available, or timeout milliseconds have elapsed
- * @param timeout
+ * @param timeout in ms
* @return An object
* @throws TimeoutException If a timeout occurred (implies that timeout > 0)
*/
@@ -48,39 +45,6 @@ public T getResultWithTimeout(long timeout) throws TimeoutException {
}
}
-
- /**
- * Blocks until a result is available, or timeout milliseconds have elapsed. Needs to be called with lock held
- * @param timeout
- * @return An object
- * @throws TimeoutException If a timeout occurred (implies that timeout > 0)
- */
- private T _getResultWithTimeout(long timeout) throws TimeoutException {
- long time_to_wait=timeout, start;
- boolean timeout_occurred=false;
-
- start=System.currentTimeMillis();
- while(hasResult == false) {
- if(timeout <= 0) {
- doWait();
- }
- else {
- if(time_to_wait <= 0) {
- timeout_occurred=true;
- break; // terminate the while loop
- }
- else {
- doWait(time_to_wait);
- time_to_wait=timeout - (System.currentTimeMillis() - start);
- }
- }
- }
-
- if(timeout_occurred)
- throw new TimeoutException();
- return result;
- }
-
public T getResult() {
try {
return getResultWithTimeout(0);
@@ -92,8 +56,8 @@ public T getResult() {
/**
* Returns the result, but never throws a TimeoutException; returns null instead.
- * @param timeout
- * @return Object
+ * @param timeout in ms
+ * @return T
*/
public T getResult(long timeout) {
try {
@@ -104,18 +68,6 @@ public T getResult(long timeout) {
}
}
-
- private void doWait() {
- try {cond.await();} catch(InterruptedException e) {}
- }
-
- private void doWait(long timeout) {
- try {cond.await(timeout, TimeUnit.MILLISECONDS);} catch(InterruptedException e) {}
- }
-
-
-
-
/**
* Checks whether result is available. Does not block.
*/
@@ -130,8 +82,7 @@ public boolean hasResult() {
}
/**
- * Sets the result and notifies any threads
- * waiting for it
+ * Sets the result and notifies any threads waiting for it
*/
public void setResult(T obj) {
lock.lock();
@@ -167,4 +118,35 @@ public String toString() {
}
+
+
+ /**
+ * Blocks until a result is available, or timeout milliseconds have elapsed. Needs to be called with lock held
+ * @param timeout in ms
+ * @return An object
+ * @throws TimeoutException If a timeout occurred (implies that timeout > 0)
+ */
+ protected T _getResultWithTimeout(final long timeout) throws TimeoutException {
+ if(timeout <= 0) {
+ while(!hasResult) { /* Wait for responses: */
+ try {cond.await();} catch(Exception e) {}
+ }
+ }
+ else {
+ long wait_time=TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
+ final long target_time=System.nanoTime() + wait_time;
+ while(wait_time > 0 && !hasResult) { /* Wait for responses: */
+ wait_time=target_time - System.nanoTime();
+ if(wait_time > 0) {
+ try {cond.await(wait_time, TimeUnit.NANOSECONDS);} catch(Exception e) {}
+ }
+ }
+ if(!hasResult && wait_time <= 0)
+ throw new TimeoutException();
+ }
+ return result;
+ }
+
+
+
}
@@ -180,17 +180,16 @@ public int size() {
public boolean waitForAllResponses(long timeout) {
if(timeout <= 0)
timeout=2000L;
- long end_time=System.currentTimeMillis() + timeout;
- long wait_time;
lock.lock();
try {
+ final long end_time=System.nanoTime() + TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
while(!hasAllResponses()) {
- wait_time=end_time - System.currentTimeMillis();
+ long wait_time=end_time - System.nanoTime();
if(wait_time <= 0)
return false;
try {
- cond.await(wait_time, TimeUnit.MILLISECONDS);
+ cond.await(wait_time, TimeUnit.NANOSECONDS);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt(); // set interrupt flag again
@@ -4,6 +4,7 @@
import java.util.LinkedList;
import java.util.List;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@@ -46,11 +47,11 @@
/** The highest delivered (= removed) seqno */
protected long hd;
- /** Time (in ms) after which a compaction should take place. 0 disables compaction */
- protected long max_compaction_time=DEFAULT_MAX_COMPACTION_TIME;
+ /** Time (in nanoseconds) after which a compaction should take place. 0 disables compaction */
+ protected long max_compaction_time=TimeUnit.NANOSECONDS.convert(DEFAULT_MAX_COMPACTION_TIME, TimeUnit.MILLISECONDS);
/** The time when the last compaction took place. If a {@link #compact()} takes place and sees that the
- * last compaction is more than max_compaction_time ms ago, a compaction will take place */
+ * last compaction is more than max_compaction_time nanoseconds ago, a compaction will take place */
protected long last_compaction_timestamp=0;
protected final Lock lock=new ReentrantLock();
@@ -59,7 +60,7 @@
protected int num_compactions=0, num_resizes=0, num_moves=0, num_purges=0;
- protected static final long DEFAULT_MAX_COMPACTION_TIME=10000;
+ protected static final long DEFAULT_MAX_COMPACTION_TIME=10000; // in milliseconds
protected static final double DEFAULT_RESIZE_FACTOR=1.2;
@@ -100,12 +101,20 @@ public Table(int num_rows, int elements_per_row, long offset, double resize_fact
this(num_rows,elements_per_row, offset, resize_factor, DEFAULT_MAX_COMPACTION_TIME);
}
+ /**
+ * Creates a new table
+ * @param num_rows the number of rows in the matrix
+ * @param elements_per_row the number of messages per row.
+ * @param offset the seqno before the first seqno to be inserted. E.g. if 0 then the first seqno will be 1
+ * @param resize_factor teh factor with which to increase the number of rows
+ * @param max_compaction_time the max time in milliseconds after we attempt a compaction
+ */
@SuppressWarnings("unchecked")
public Table(int num_rows, int elements_per_row, long offset, double resize_factor, long max_compaction_time) {
this.num_rows=num_rows;
this.elements_per_row=elements_per_row;
this.resize_factor=resize_factor;
- this.max_compaction_time=max_compaction_time;
+ this.max_compaction_time=TimeUnit.NANOSECONDS.convert(max_compaction_time, TimeUnit.MILLISECONDS);
this.offset=this.low=this.hr=this.hd=offset;
matrix=(T[][])new Object[num_rows][];
if(resize_factor <= 1)
@@ -132,7 +141,9 @@ public Table(int num_rows, int elements_per_row, long offset, double resize_fact
public long getHighestDelivered() {return hd;}
public long getHighestReceived() {return hr;}
public long getMaxCompactionTime() {return max_compaction_time;}
- public void setMaxCompactionTime(long max_compaction_time) {this.max_compaction_time=max_compaction_time;}
+ public void setMaxCompactionTime(long max_compaction_time) {
+ this.max_compaction_time=TimeUnit.NANOSECONDS.convert(max_compaction_time, TimeUnit.MILLISECONDS);
+ }
public int getNumRows() {return matrix.length;}
public void resetStats() {num_compactions=num_moves=num_resizes=num_purges=0;}
@@ -357,14 +368,14 @@ public void purge(long seqno, boolean force) {
if(max_compaction_time <= 0) // see if compaction should be triggered
return;
- long current_time=System.currentTimeMillis();
+ long current_time=System.nanoTime();
if(last_compaction_timestamp > 0) {
if(current_time - last_compaction_timestamp >= max_compaction_time) {
_compact();
last_compaction_timestamp=current_time;
}
}
- else
+ else // the first time we don't do a compaction
last_compaction_timestamp=current_time;
}
finally {
@@ -7,7 +7,6 @@
import org.jgroups.conf.ClassConfigurator;
import org.jgroups.jmx.JmxConfigurator;
import org.jgroups.logging.Log;
-import org.jgroups.logging.LogFactory;
import org.jgroups.protocols.*;
import org.jgroups.protocols.pbcast.FLUSH;
import org.jgroups.protocols.pbcast.GMS;
@@ -81,7 +81,7 @@ public void run() {
};
t.start();
long start=System.currentTimeMillis(), stop;
- Object result=p.getResult(100000);
+ Object result=p.getResult(30000);
stop=System.currentTimeMillis();
System.out.println("-- waited for " + (stop-start) + "ms, result is " + result);
assert result != null;
Oops, something went wrong.

0 comments on commit 696434b

Please sign in to comment.