Skip to content

Optimize FiberQueue for large number of fibers #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion javatools/src/main/java/org/xvm/runtime/Fiber.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ public ObjectHandle getAsyncSection()
*/
public void setStatus(FiberStatus status, long cOps)
{
if (m_status == FiberStatus.Waiting)
{
f_context.f_queueSuspended.exitWait();
}
switch (m_status = status)
{
default:
Expand All @@ -220,6 +224,8 @@ public void setStatus(FiberStatus status, long cOps)
break;

case Waiting:
f_context.f_queueSuspended.enterWait();
// fall through
case Paused:
long cNanos = f_context.f_container.nanoTime() - m_nanoStarted;
m_nanoStarted = 0;
Expand Down Expand Up @@ -541,10 +547,26 @@ public Frame getBlocker()

/**
* Set or clear the frame that blocks this fiber's execution.
*
* @param frameBlocker null if no blocker exists or the blocker fiber itself
* @param lStamp if no blocker exists, the "wait exit stamp", otherwise the "wait enter
* stamp" of the fiber queue when this check occurred
*/
protected void setBlocker(Frame frameBlocker)
protected void setBlocker(Frame frameBlocker, long lStamp)
{
m_frameBlocker = frameBlocker;
m_lLastStamp = lStamp;
}

/**
* @return true iff there were no wait status changes on the FiberQueue that would change
* the blocker computation result for the fiber
*/
protected boolean noChange(long lLastEnterStamp, long lLastExitStamp)
{
return m_frameBlocker == null
? lLastEnterStamp == m_lLastStamp
: lLastExitStamp == m_lLastStamp;
}


Expand Down Expand Up @@ -785,6 +807,11 @@ public int proceed(Frame frameCaller)
*/
private Frame m_frameBlocker;

/**
* THe FiberQueue stamp of the last time it looked for blockers for this fiber.
*/
private long m_lLastStamp;

/**
* The counter used to create fibers ids.
*/
Expand Down
51 changes: 41 additions & 10 deletions javatools/src/main/java/org/xvm/runtime/FiberQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class FiberQueue
private int m_ixTail = 0; // past the tail - insertion point
private int m_cSize = 0;

// this value increases every time any fiber that belong to this queue enters the "Waiting" state
private long m_lWaitEnterStamp = 0;

// this value increases every time any fiber that belong to this queue exits the "Waiting" state
private long m_lWaitExitStamp = 0;

public FiberQueue(ServiceContext ctx)
{
f_context = ctx;
Expand Down Expand Up @@ -129,6 +135,22 @@ public Frame getAny()
return null;
}

/**
* Called when any fiber changes its state to {@link FiberStatus#Waiting}
*/
protected void enterWait()
{
m_lWaitEnterStamp++;
}

/**
* Called when any fiber changes its state from {@link FiberStatus#Waiting} to any other.
*/
protected void exitWait()
{
m_lWaitExitStamp++;
}

/**
* Report on the status of the fiber queue. Temporary: for debugging only.
*/
Expand Down Expand Up @@ -254,8 +276,19 @@ private boolean canResume(Fiber fiber)
*
* @return true iff there are any non-concurrent waiting frames
*/
static int HIT = 0;
static int ALL = 0;
private boolean isAnyNonConcurrentWaiting(Fiber fiberCandidate)
{
//if (++ALL % 100_000 == 0)
// {
// System.err.println("*** hits=" + HIT + " out of " + ALL);
// }
if (fiberCandidate.noChange(m_lWaitEnterStamp, m_lWaitExitStamp))
{
//++HIT;
return fiberCandidate.getBlocker() != null;
}
Frame[] aFrame = m_aFrame;
Fiber fiberCaller = fiberCandidate.getCaller();

Expand All @@ -267,21 +300,19 @@ private boolean isAnyNonConcurrentWaiting(Fiber fiberCandidate)
}

Fiber fiber = frame.f_fiber;
if (fiber != fiberCandidate)
if (fiber != fiberCandidate &&
fiber.getStatus() == FiberStatus.Waiting)
{
if (fiber.getStatus() == FiberStatus.Waiting)
if (frame.isSafeStack() ||
fiberCaller != null && fiberCaller.isContinuationOf(fiber))
{
if (frame.isSafeStack() ||
fiberCaller != null && fiberCaller.isContinuationOf(fiber))
{
continue;
}
fiberCandidate.setBlocker(frame);
return true;
continue;
}
fiberCandidate.setBlocker(frame, m_lWaitExitStamp);
return true;
}
}
fiberCandidate.setBlocker(null);
fiberCandidate.setBlocker(null, m_lWaitEnterStamp);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2405,7 +2405,7 @@ public interface TypeSupplier
/**
* The queue of suspended fibers.
*/
private final FiberQueue f_queueSuspended = new FiberQueue(this);
protected final FiberQueue f_queueSuspended = new FiberQueue(this);

/**
* The reentrancy policy. Must be the same names as in natural Service.Synchronicity.
Expand Down
Loading