Skip to content

Commit

Permalink
Improve Simulation speed and Closing behavior on MacOS and Windows
Browse files Browse the repository at this point in the history
See merge request main/Sumatra!1880

sumatra-commit: f9b7ef1c3a4e43f94feeb355d9e0e45af70d82af
  • Loading branch information
mickmack1213 authored and TIGERs GitLab committed Nov 7, 2024
1 parent 8b44baa commit 6d064a3
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,14 @@ protected void startReplayCompressionThread(Path path)
}


protected void exit()
@Override
public void onClose()
{
for (final IMainFrameObserver o : observers)
{
o.onExit();
o.onClose();
}
log.debug("Closed");
}


Expand Down Expand Up @@ -636,7 +638,7 @@ public void actionPerformed(final ActionEvent e)
public void windowClosing(final WindowEvent windowEvent)
{
super.windowClosing(windowEvent);
exit();
onClose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void onLoadLayout(final String filename)


@Override
public void onExit()
public void onClose()
{
GlobalShortcuts.removeAllForFrame(getMainFrame());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@ public interface IMainFrame
* @param name
*/
void selectLayoutItem(String name);

/**
* Hook to run some cleanup when the frame is closed.
*/
void onClose();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface IMainFrameObserver
/**
*
*/
void onExit();
void onClose();


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public static void parkNanosSafe(final long sleepTotal)
final long sleepStart = System.nanoTime();
long stillSleep = sleepTotal;

do
while (stillSleep > 1)
{
LockSupport.parkNanos(stillSleep);
// On some OS, the threads sleep too long, so we apply an exponential strategy
LockSupport.parkNanos(stillSleep / 2);
long timeSinceStart = System.nanoTime() - sleepStart;
stillSleep = sleepTotal - timeSinceStart;
} while (stillSleep > 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.LockSupport;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


/**
Expand Down Expand Up @@ -38,4 +35,32 @@ public void testParkNanosSafeLong()
assertTrue("Not slept enough: " + duration + "ns < " + sleepFor + "ns!!!", duration > sleepFor);
}
}


private static void measureParkNanos()
{
long iterationCount = 100;
long[] sleepTimes = new long[] { 0, 1, 10_000, 100_000, 500_000, 1_000_000, 10_000_000 };

for (long sleepTime : sleepTimes)
{
long startNanos = System.nanoTime();
for (long i = 0; i < iterationCount; i++)
{
LockSupport.parkNanos(sleepTime);
}
long durationNanos = System.nanoTime() - startNanos;
long microsPerIteration = durationNanos / iterationCount;
long diff = (microsPerIteration - sleepTime);
double rel = (double) diff / sleepTime;

System.out.printf("%8d | %8d | %8d | %.1f %n", sleepTime, microsPerIteration, diff, rel * 100);
}
}


public static void main(String[] args)
{
measureParkNanos();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ public interface IRecordObserver
* @param recording
*/
void onStartStopRecord(boolean recording);


/**
* @param persistence the open persistence reference
* @param startTime the initial time after opening the replay (timestamp in ns)
*/
default void onViewReplay(PersistenceDb persistence, long startTime)
{
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.apache.logging.log4j.Logger;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

Expand Down Expand Up @@ -333,35 +332,6 @@ protected void onNewPersistanceRecorder(PersistenceAsyncRecorder recorder)
}


/**
* Notify UI to view a replay window
*
* @param startTime
*/
public void notifyViewReplay(long startTime)
{
String dbPath = getCurrentDbPath();
if (dbPath.isEmpty())
{
log.error("No open DB found.");
return;
}
PersistenceDb db;
try
{
db = newPersistenceDb(Paths.get(dbPath));
} catch (Exception e)
{
log.error("Could not open DB", e);
return;
}
for (IRecordObserver observer : observers)
{
observer.onViewReplay(db, startTime);
}
}


/**
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class SslGameControllerProcess implements Runnable
private final int gcUiPort;
private final String publishAddress;
private final String timeAcquisitionMode;
private final Thread shutdownHook = new Thread(this::stop);

@Setter
private boolean useSystemBinary = false;
Expand Down Expand Up @@ -82,8 +81,6 @@ public void run()
return;
}

Runtime.getRuntime().addShutdownHook(shutdownHook);

Path engineConfig = Path.of("config", "engine.yaml");
Path engineConfigDefault = Path.of("config", "engine-default.yaml");
if (!Files.exists(engineConfig) && Files.exists(engineConfigDefault))
Expand Down Expand Up @@ -137,7 +134,6 @@ public void run()
log.warn("game-controller has returned a non-zero exit code: {}", p.exitValue());
}
log.debug("game-controller process thread finished");
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}


Expand Down Expand Up @@ -264,5 +260,6 @@ public void stop()
log.warn("Interrupted while waiting for the process to exit");
Thread.currentThread().interrupt();
}
log.debug("Process stopped");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,10 @@ private void addPositionObserver(final IReplayPositionObserver o)


@Override
public void onExit()
public void onClose()
{
super.onExit();
super.onClose();
stop();
getMainFrame().dispose();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;
import java.awt.event.KeyEvent;


Expand All @@ -27,6 +28,7 @@ public class ReplayWindow extends AMainFrame
public ReplayWindow()
{
setTitle("Replay");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

addView(new LogView(false));
addView(new ReplayControlView());
Expand All @@ -42,10 +44,6 @@ public ReplayWindow()
shortcutMenuItem.addActionListener(actionEvent -> new ShortcutsDialog(ReplayWindow.this));
replayMenu.add(shortcutMenuItem);

JMenuItem menuClose = new JMenuItem("Close");
menuClose.addActionListener(e -> exit());
replayMenu.add(menuClose);

getJMenuBar().add(replayMenu);
addMenuItems();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.SwingUtilities;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void propertyChange(final PropertyChangeEvent evt)
try
{
log.trace("Notify {}", o.getClass());
o.onModuliStateChanged(newState);
SwingUtilities.invokeLater(() -> o.onModuliStateChanged(newState));
} catch (Exception err)
{
log.error("Exception while changing moduli state in class " + o.getClass().getName(), err);
Expand Down

0 comments on commit 6d064a3

Please sign in to comment.