PseudoTCP performance improvements - patch by Carl Hasselskog.

This commit is contained in:
paweldomas 2014-06-26 19:26:16 +00:00
parent 8949f3f318
commit 3e6ddf0f16
2 changed files with 79 additions and 60 deletions

View File

@ -1741,7 +1741,11 @@ public class PseudoTCPBase
else else
{ {
m_t_ack = now(); m_t_ack = now();
logger.log(Level.FINER, "Delayed ack, m_t_ack: " + m_t_ack); if (logger.isLoggable(Level.FINER))
{
logger.log(
Level.FINER, "Delayed ack, m_t_ack: " + m_t_ack);
}
} }
return; return;
} }

View File

@ -10,6 +10,7 @@ package org.ice4j.pseudotcp;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*; import java.util.logging.*;
class PseudoTcpSocketImpl class PseudoTcpSocketImpl
@ -52,13 +53,7 @@ class PseudoTcpSocketImpl
* Monitor object used to block thread waiting for change of TCP state. * Monitor object used to block thread waiting for change of TCP state.
*/ */
private final Object state_notify = new Object(); private final Object state_notify = new Object();
/**
* Monitor object used by clock thread. Clock thread sleeps for some
* intervals given by pseudotcp logic, but sometimes it is required to
* finish this sleep earlier. In that case notify method of this monitor is
* called.
*/
private final Object clock_notify = new Object();
/** /**
* Exception which occurred in pseudotcp logic and must be propagated to * Exception which occurred in pseudotcp logic and must be propagated to
* threads blocked on any operations. * threads blocked on any operations.
@ -448,10 +443,7 @@ class PseudoTcpSocketImpl
*/ */
private void updateClock() private void updateClock()
{ {
synchronized (clock_notify) scheduleClockTask(0);
{
clock_notify.notifyAll();
}
} }
/** /**
@ -467,17 +459,11 @@ class PseudoTcpSocketImpl
receivePackets(); receivePackets();
} }
}, "PseudoTcpReceiveThread"); }, "PseudoTcpReceiveThread");
clockThread = new Thread(new Runnable()
{
public void run()
{
runClock();
}
}, "PseudoTcpClockThread");
runReceive = true; runReceive = true;
runClock = true; runClock = true;
receiveThread.start(); receiveThread.start();
clockThread.start(); scheduleClockTask(0);
} }
/** /**
@ -564,6 +550,8 @@ class PseudoTcpSocketImpl
runClock = false; runClock = false;
this.exception = e; this.exception = e;
releaseAllLocks(); releaseAllLocks();
cancelClockTask(true);
} }
/** /**
@ -586,7 +574,6 @@ class PseudoTcpSocketImpl
} }
//this interrupt won't work for DatagramSocket read packet operation //this interrupt won't work for DatagramSocket read packet operation
//receiveThread.interrupt(); //receiveThread.interrupt();
clockThread.interrupt();
} }
/** /**
@ -596,7 +583,6 @@ class PseudoTcpSocketImpl
*/ */
private void joinAllThreads() throws InterruptedException private void joinAllThreads() throws InterruptedException
{ {
clockThread.join();
receiveThread.join(); receiveThread.join();
} }
@ -700,7 +686,12 @@ class PseudoTcpSocketImpl
* The run flag for clock thread * The run flag for clock thread
*/ */
private boolean runClock = false; private boolean runClock = false;
private Thread clockThread;
// FIXME: consider larger thread pool and/or making it configurable
private final static ScheduledThreadPoolExecutor clockExecutor
= new ScheduledThreadPoolExecutor(1);
private volatile ScheduledFuture currentlyScheduledClockTask = null;
/** /**
* Method runs cyclic notification about time progress for TCP logic class * Method runs cyclic notification about time progress for TCP logic class
@ -708,9 +699,12 @@ class PseudoTcpSocketImpl
*/ */
private void runClock() private void runClock()
{ {
long sleep; if(!runClock)
while (runClock)
{ {
return;
}
long sleep;
synchronized (pseudoTcp) synchronized (pseudoTcp)
{ {
pseudoTcp.notifyClock(PseudoTCPBase.now()); pseudoTcp.notifyClock(PseudoTCPBase.now());
@ -727,23 +721,44 @@ class PseudoTcpSocketImpl
"STATE: " + pseudoTcp.getState() "STATE: " + pseudoTcp.getState()
+ " ERROR: " + exception.getMessage()); + " ERROR: " + exception.getMessage());
} }
break;
} }
synchronized (clock_notify) else
{
try
{ {
//logger.log(Level.FINEST, "Clock sleep for " + sleep); //logger.log(Level.FINEST, "Clock sleep for " + sleep);
clock_notify.wait(sleep); scheduleClockTask(sleep);
}
catch (InterruptedException ex)
{
//interruption means end of task, as in normal operation
//notify will be called
break;
} }
} }
private Runnable clockTaskRunner = new Runnable()
{
@Override
public void run() {
runClock();
}
};
private void scheduleClockTask(long sleep)
{
synchronized (clockTaskRunner)
{
// Cancel any existing tasks, to make sure we don't run duplicates.
cancelClockTask(false);
if(runClock)
{
currentlyScheduledClockTask
= clockExecutor.schedule(
clockTaskRunner, sleep, TimeUnit.MILLISECONDS);
}
}
}
private void cancelClockTask(boolean interruptIfRunning)
{
// Copy the reference, in case it changes.
ScheduledFuture taskToCancel = this.currentlyScheduledClockTask;
if(taskToCancel != null)
{
taskToCancel.cancel(interruptIfRunning);
} }
} }
@ -973,7 +988,7 @@ class PseudoTcpSocketImpl
@Override @Override
public long skip(long n) throws IOException public long skip(long n) throws IOException
{ {
throw new UnsupportedOperationException("skip"); return super.skip(n);
} }
/** /**