-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OAP-209 Optional ability to change processor affinity for worker thre…
…ads in the undertow
- Loading branch information
Showing
12 changed files
with
321 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>oap</groupId> | ||
<artifactId>oap</artifactId> | ||
<version>${oap.project.version}</version> | ||
</parent> | ||
|
||
<artifactId>oap-highload</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>net.openhft</groupId> | ||
<artifactId>affinity</artifactId> | ||
<version>${oap.deps.affinity.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>oap</groupId> | ||
<artifactId>oap-stdlib-test</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>${oap.deps.lombok.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package oap.highload; | ||
|
||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.ArrayList; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@Slf4j | ||
public class Affinity { | ||
@Getter | ||
private final int[] cpus; | ||
private final AtomicInteger position = new AtomicInteger(); | ||
|
||
public Affinity( String cpu ) { | ||
log.info( "cpu {}", cpu ); | ||
|
||
if( cpu.trim().equals( "*" ) ) { | ||
cpus = new int[0]; | ||
} else { | ||
String[] split = cpu.split( "," ); | ||
|
||
ArrayList<Integer> cpus = new ArrayList<>(); | ||
|
||
for( var n : split ) { | ||
String nTrimmed = n.trim(); | ||
if( nTrimmed.endsWith( "+" ) ) { | ||
for( int i = Integer.parseInt( nTrimmed.substring( 0, nTrimmed.length() - 1 ) ); i < Runtime.getRuntime().availableProcessors(); i++ ) { | ||
cpus.add( i ); | ||
} | ||
} else { | ||
String[] range = n.split( "-" ); | ||
|
||
if( range.length > 1 ) { | ||
int start = Integer.parseInt( range[0].trim() ); | ||
int end = Integer.parseInt( range[1].trim() ); | ||
|
||
for( int i = start; i <= end; i++ ) { | ||
cpus.add( i ); | ||
} | ||
|
||
} else { | ||
cpus.add( Integer.parseInt( nTrimmed ) ); | ||
} | ||
} | ||
} | ||
|
||
this.cpus = cpus.stream().mapToInt( i -> i ).toArray(); | ||
} | ||
} | ||
|
||
public static Affinity any() { | ||
return new Affinity( "*" ); | ||
} | ||
|
||
public void set() { | ||
if( isEnabled() ) { | ||
int cpuIndex = position.getAndUpdate( index -> index >= cpus.length ? 0 : index + 1 ); | ||
int cpu = cpus[cpuIndex]; | ||
log.trace( "affinity -> {}", cpu ); | ||
net.openhft.affinity.Affinity.setAffinity( cpu ); | ||
} | ||
} | ||
|
||
public boolean isEnabled() { | ||
return cpus.length > 0; | ||
} | ||
|
||
public int size() { | ||
return cpus.length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package oap.highload; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class AffinityTest { | ||
@Test | ||
public void testCpuParse() { | ||
assertThat( new Affinity( "1" ).getCpus() ).isEqualTo( new int[] { 1 } ); | ||
assertThat( new Affinity( "3+" ).getCpus() ).isEqualTo( IntStream.range( 3, Runtime.getRuntime().availableProcessors() ).toArray() ); | ||
assertThat( new Affinity( "1, 3 ,7 " ).getCpus() ).isEqualTo( new int[] { 1, 3, 7 } ); | ||
assertThat( new Affinity( "1-3, 8" ).getCpus() ).isEqualTo( new int[] { 1, 2, 3, 8 } ); | ||
assertThat( new Affinity( "1-3, 8" ).isEnabled() ).isTrue(); | ||
assertThat( new Affinity( "*" ).getCpus() ).isEqualTo( new int[0] ); | ||
assertThat( new Affinity( "*" ).isEnabled() ).isFalse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.