Skip to content

Commit

Permalink
Signal low space semaphore on StackOverflowError
Browse files Browse the repository at this point in the history
Related issue: #174
  • Loading branch information
fniephaus committed Feb 14, 2025
1 parent 1b08ecc commit 6233110
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import de.hpi.swa.trufflesqueak.nodes.plugins.JPEGReader;
import de.hpi.swa.trufflesqueak.nodes.plugins.Zip;
import de.hpi.swa.trufflesqueak.nodes.plugins.ffi.InterpreterProxy;
import de.hpi.swa.trufflesqueak.nodes.process.SignalSemaphoreNodeGen;
import de.hpi.swa.trufflesqueak.shared.SqueakImageLocator;
import de.hpi.swa.trufflesqueak.tools.SqueakMessageInterceptor;
import de.hpi.swa.trufflesqueak.util.ArrayUtils;
Expand Down Expand Up @@ -168,6 +169,10 @@ public final class SqueakImageContext {
private ContextObject interopExceptionThrowingContextPrototype;
public ContextObject lastSeenContext;

/* Low space handling */
private static final int LOW_SPACE_NUM_SKIPPED_SENDS = 4;
private int lowSpaceSkippedSendsCount;

@CompilationFinal private ClassObject fractionClass;
private PointersObject parserSharedInstance;
private AbstractSqueakObject requestorSharedInstanceOrNil;
Expand Down Expand Up @@ -679,6 +684,34 @@ public void setSemaphore(final int index, final AbstractSqueakObject semaphore)
setSpecialObject(index, semaphore);
}

/**
* Ensure the active process is saved and try to signal low space semaphore (see
* #setSignalLowSpaceFlagAndSaveProcess). The JVM has just thrown a {@link StackOverflowError},
* so thread stack space is limited. To avoid hitting the limit again, free up some space by
* unwinding a couple of sends before actually signaling the low space semaphore.
*/
public StackOverflowError tryToSignalLowSpace(final VirtualFrame frame, final StackOverflowError stackOverflowError) {
CompilerAsserts.neverPartOfCompilation();
final Object lastSavedProcess = getSpecialObject(SPECIAL_OBJECT.PROCESS_SIGNALING_LOW_SPACE);
if (lastSavedProcess == NilObject.SINGLETON) {
setSpecialObject(SPECIAL_OBJECT.PROCESS_SIGNALING_LOW_SPACE, getActiveProcessSlow());
}
if (lowSpaceSkippedSendsCount < LOW_SPACE_NUM_SKIPPED_SENDS) {
lowSpaceSkippedSendsCount++;
throw stackOverflowError; // continue further up the sender chain
} else {
final Object lowSpaceSemaphoreOrNil = getSpecialObject(SPECIAL_OBJECT.THE_LOW_SPACE_SEMAPHORE);
try {
SignalSemaphoreNodeGen.executeUncached(frame, this, lowSpaceSemaphoreOrNil);
} catch (final ProcessSwitch ps) {
// success! reset counter and continue in new process
lowSpaceSkippedSendsCount = 0;
throw ps;
}
throw CompilerDirectives.shouldNotReachHere("Failed to signal low space semaphore.", stackOverflowError);
}
}

public boolean hasDisplay() {
return display != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public static final class SPECIAL_OBJECT {
public static final int CLASS_CHARACTER = 19;
public static final int SELECTOR_DOES_NOT_UNDERSTAND = 20;
public static final int SELECTOR_CANNOT_RETURN = 21;
public static final int THE_INPUT_SEMAPHORE = 22;
public static final int PROCESS_SIGNALING_LOW_SPACE = 22;
public static final int SPECIAL_SELECTORS = 23;
public static final int CHARACTER_TABLE = 24;
public static final int SELECTOR_MUST_BE_BOOLEAN = 25;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ public Object execute(final VirtualFrame frame, final int startPC) {
try {
return interpretBytecode(frame, startPC);
} catch (final NonLocalReturn nlr) {
/** {@link getHandleNonLocalReturnNode()} acts as {@link BranchProfile} */
/* {@link getHandleNonLocalReturnNode()} acts as {@link BranchProfile} */
return getHandleNonLocalReturnNode().executeHandle(frame, nlr);
} catch (final StackOverflowError e) {
CompilerDirectives.transferToInterpreter();
throw getContext().tryToSignalLowSpace(frame, e);
}
}

Expand Down

1 comment on commit 6233110

@TruffleSqueak-Bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance Report (6233110)

Benchmarks ran on 23.0.2-graal.

Steady (after 100 iterations)

Benchmark Name Min Geomean Median Mean Max Total (ms) Total (min)
Bounce 524 537 528.31 525 528.29 105662 1.76
CD 509 527 515.04 511 515 103007 1.72
DeltaBlue 220 288 261.63 261 261.54 52325 0.87
Havlak 1224 1281 1254.44 1255 1254.4 250888 4.18
Json 322 337 325.67 323 325.63 65134 1.09
List 318 353 319.81 318 319.77 63962 1.07
Mandelbrot 129 159 130.72 130 130.68 26144 0.44
NBody 244 273 248.99 246 248.91 49797 0.83
Permute 155 172 156.75 156 156.72 31349 0.52
Queens 228 248 230.94 229 230.89 46188 0.77
Richards 811 828 814.9 812 814.88 162979 2.72
Sieve 170 209 171.45 170 171.4 34290 0.57
Storage 144 159 146.68 145 146.61 29335 0.49
Towers 173 199 175.39 174 175.33 35078 0.58
5171 5570 5280.69 5255 5280.03 1056138 17.6

6233110-2-steady.svg

Warmup (first 100 iterations)

6233110-3-warmup.svg

Please sign in to comment.