Skip to content
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

Added code to throttle high cpu usage. #1187

Closed
wants to merge 1 commit into from
Closed
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
69 changes: 69 additions & 0 deletions src/main/java/org/java_websocket/server/HighCycleThrottler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.java_websocket.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* <tt>HighCycleThrottler</tt> is a class that checks for high cycling caused by the jdk epoll selector bug. For some
* reason the selector.select() code becomes not blocking causing a high CPU condition. When this condition is detected,
* this class will add a 1ms delay to keep from loading up the CPU when the selector.select() code is no longer blocking.
* */
public class HighCycleThrottler
{
private final Logger log = LoggerFactory.getLogger( HighCycleThrottler.class );
private static final int CYCLE_THRESHOLD = 600 * 60; // 600 cycles a second for 60 seconds;
private final boolean enabled;
private long nextCycle;
private long cyclesPerMinute = 0;
private boolean highCPUDetected = false;

public HighCycleThrottler() {
enabled = isEnabled( );
nextCycle = System.currentTimeMillis() + 60 * 1000;
}

/**
* Checks for high cycling and throttles with a 1ms delay if detected.
*/
public void checkHighCycleRate() {
if ( enabled ) {
cyclesPerMinute++;
if ( System.currentTimeMillis() >= nextCycle ) {
String cycles = String.format( "Cycles last minute = %d", cyclesPerMinute );
log.warn( cycles );

if ( cyclesPerMinute > CYCLE_THRESHOLD ){
if( !highCPUDetected ){
highCPUDetected = true;
log.warn( "High CPU condition detected" );
}
} else if ( highCPUDetected ) {
log.warn( "High CPU condition cleared" );
highCPUDetected = false;
}

nextCycle = System.currentTimeMillis() + 60 * 1000;
cyclesPerMinute = 0;
}

if ( highCPUDetected ) {
try {
Thread.sleep( 1L );
} catch ( InterruptedException e ) {
log.warn( "Thread.sleep(1L) failed" );
}
}
}
}

/**
* Set the enabled flag and log if USE_EPOLL_SELECTOR_FIX is defined.
*/
private boolean isEnabled() {
if (System.getenv( "USE_EPOLL_SELECTOR_FIX" ) != null){
log.warn( "Using EPoll Selector Fix" );
return true;
}
return false;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/java_websocket/server/WebSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ public void run() {
if (!doSetupSelectorAndServerThread()) {
return;
}
HighCycleThrottler highCycleThrottler = new HighCycleThrottler();
try {
int shutdownCount = 5;
int selectTimeout = 0;
Expand Down Expand Up @@ -411,6 +412,7 @@ public void run() {
// FIXME controlled shutdown (e.g. take care of buffermanagement)
Thread.currentThread().interrupt();
}
highCycleThrottler.checkHighCycleRate();
}
} catch (RuntimeException e) {
// should hopefully never occur
Expand Down