Class ThreadAffinityConfigure
- java.lang.Object
-
- net.sf.eBus.config.ThreadAffinityConfigure
-
public class ThreadAffinityConfigure extends java.lang.Object
This immutable class contains the necessary information needed to create an affinity between a thread and a core using OpenHFT Thread Affinity Library. User is assumed to understand thread affinity and the necessary operating system configuration needed to support it. Please see the above link for more information on how to use thread affinity and its correct use.ThreadAffinityConfigure consist of the following properties:
-
affinityType
: Required. Defines how core is acquired for the thread. There are five acquisition types as defined byThreadAffinityConfigure.AffinityType
:-
ANY_CORE
: UseAffinityLock.acquireCore()
to assign any free core to thread. -
ANY_CPU
: UseAffinityLock.acquireLock()
to assign any free CPU to thread. -
CPU_LAST_MINUS
: UseAffinityLock.acquireLock(int cpuId
to assign a CPU with specified identifier to thread. Requires propertyCPU_OFFSET_KEY
be set. -
CPU_ID
: UseAffinityLock.acquireLock(int cpuId
to assign a CPU with specified identifier to thread. Requires propertyCPU_ID_KEY
be set. -
CPU_STRATEGIES
: UseAffinityLock.acquireLock(AffinityStrategies...)
to assign a CPU for thread affinity. Selects a CPU for thread affinity based on the given selection strategies. Requires propertySTRATEGIES_KEY
be set.Please note that this type may not be used by itself or as an initial CPU acquisition type. Rather there must be previous CPU allocation to this (for example a previous dispatcher configuration using thread affinity) which the strategy then uses to allocate the next CPU. Attempts to use this acquisition type either by itself or as the first strategy will result in an error and no CPU allocated for the thread.
-
-
bind
: Optional, default value isfalse
. Iftrue
, then bind current thread to allocatedAffinityLock
. -
wholeCore
: Optional, default value isfalse
. Iftrue
, then bind current thread to allocatedAffinityLock
reserving the whole core. This property is used only whenbind
property istrue
. -
cpuId
: Required whenaffinityType
is set toCPU_ID
. Specifies the allocated CPU by its identifier. -
cpuStrategies
: Required whenaffinityType
is set toCPU_STRATEGIES
. Values are restricted to enumnet.openhft.affinity.AffinityStrategies
.Note: strategy ordering is important.
AffinityStrategies.ANY
must appear as the last listed strategy. This allows any CPU to be selected in case none of the other strategies found an acceptable CPU.
Users should be familiar with the OpenHFT Java Thread Affinity library and how it works before using eBus thread affinity configuration. This includes configuring the operating system to isolate acquired CPUs from the operating system. This prevents the OS from pre-empting the thread from its assigned CPU which means the thread does not entirely own the CPU. That said, isolating too many CPUs from the OS can lead to a kernel panic. So using thread affinity is definitely an advanced software technique, requiring good understanding of how an OS functions.
The following example shows how to use thread affinity for eBus dispatcher threads and especially the CPU_STRATEGIES acquisition type.
"dispatchers" : [ { "name" : "mdDispatcher" "numberThreads" : 1 "runQueueType" : "spinning" "priority" : 9 "quantum" : 10000 "isDefault" : false "classes" : [ "com.acme.trading.MDHandler" ] "threadAffinity" { // optional, selector thread core affinity affinityType : CPU_ID // required, core selection type. cpuId : 7 // required for CPU_ID affinity type bind : true // optional, defaults to false wholeCore : true // optional, defaults to false } }, { "name" : "orderDispatcher" "numberThreads" : 1 "runQueueType" : "spinning" "priority" : 9 "quantum" : 10000 "isDefault" : false "classes" : [ "com.acme.trading.OrderHandler" ] "threadAffinity" { // optional, selector thread core affinity affinityType : CPU_STRATEGIES // required, core selection type. cpuStrategies : [ // required for CPU_STRATEGIES affinity type SAME_CORE, SAME_SOCKET, ANY // Note: ANY must be last strategy. ] bind : true // optional, defaults to false wholeCore : true // optional, defaults to false } }, { "name" : "defaultDispatcher" "numberThreads" : 8 "runQueueType" : "blocking" "priority" : 4 "quantum" : 100000 "isDefault" : true } ]
eBus uses this configuration to optionally pin
net.sf.eBus.net.SelectorThread
andnet.sf.eBus.client.EClient.RQThread
instances to a core or cores.- Author:
- Charles W. Rapp
- See Also:
ThreadAffinityConfigure.Builder
-
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ThreadAffinityConfigure.AffinityType
These affinity types map to a specificAffinityLock static
method used to acquire an affinity lock.static class
ThreadAffinityConfigure.Builder
Constructs aThreadAffinityConfigure
instance based on the builder settings.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
AFFINITY_TYPE_KEY
Key "affinityType" contains anThreadAffinityConfigure.AffinityType
value.static java.lang.String
BIND_KEY
Key "bind" contains the bind-thread-to-affinity lock flag.static java.lang.String
CPU_ID_KEY
Key "cpuId" contains CPU identifier used whenAFFINITY_TYPE_KEY
is set toThreadAffinityConfigure.AffinityType.CPU_ID
.static java.lang.String
CPU_OFFSET_KEY
Key "lastMinusOffset" contains CPU offset used whenAFFINITY_TYPE_KEY
is set toThreadAffinityConfigure.AffinityType.CPU_LAST_MINUS
.static java.lang.String
STRATEGIES_KEY
Key "cpuStrategies" contains CPU selection strategies array used whenAFFINITY_TYPE_KEY
is set toThreadAffinityConfigure.AffinityType.CPU_STRATEGIES
.static java.lang.String
WHOLE_CORE_KEY
Key "wholeCore" contains reserve-whole-core flag.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ThreadAffinityConfigure.AffinityType
affinityType()
Returns affinity type used for creating thread affinity to selected CPU_ID.boolean
bindFlag()
Returns the bind-thread-to-affinity lock setting.static ThreadAffinityConfigure.Builder
builder()
Returns newThreadAffinityConfigure
builder instance.int
cpuId()
Returns CPU_ID identifier used forThreadAffinityConfigure.AffinityType.CPU_ID
affinity type.int
cpuOffset()
Returns CPU offset used forThreadAffinityConfigure.AffinityType.CPU_LAST_MINUS
affinity type.boolean
equals(java.lang.Object o)
int
hashCode()
static java.util.List<ThreadAffinityConfigure>
loadAffinities(java.lang.String key, com.typesafe.config.Config config)
Returns list of thread affinity configurations loaded from given JSON configuration under property key.static ThreadAffinityConfigure
loadAffinity(java.lang.String key, com.typesafe.config.Config config)
Returns a thread affinity configuration loaded from a given JSON configuration under property key.java.util.List<net.openhft.affinity.AffinityStrategies>
strategies()
Returns immutable list of CPU_ID selection strategies used forThreadAffinityConfigure.AffinityType.CPU_STRATEGIES
affinity type.java.lang.String
toString()
boolean
wholeCoreFlag()
Returns whole core reservation bind settings.
-
-
-
Field Detail
-
AFFINITY_TYPE_KEY
public static final java.lang.String AFFINITY_TYPE_KEY
Key "affinityType" contains anThreadAffinityConfigure.AffinityType
value.- See Also:
- Constant Field Values
-
BIND_KEY
public static final java.lang.String BIND_KEY
Key "bind" contains the bind-thread-to-affinity lock flag.- See Also:
- Constant Field Values
-
WHOLE_CORE_KEY
public static final java.lang.String WHOLE_CORE_KEY
Key "wholeCore" contains reserve-whole-core flag. This property is used only ifBIND_KEY
is set totrue
.- See Also:
- Constant Field Values
-
CPU_OFFSET_KEY
public static final java.lang.String CPU_OFFSET_KEY
Key "lastMinusOffset" contains CPU offset used whenAFFINITY_TYPE_KEY
is set toThreadAffinityConfigure.AffinityType.CPU_LAST_MINUS
. Otherwise this property is ignored for any other affinity type.- See Also:
- Constant Field Values
-
CPU_ID_KEY
public static final java.lang.String CPU_ID_KEY
Key "cpuId" contains CPU identifier used whenAFFINITY_TYPE_KEY
is set toThreadAffinityConfigure.AffinityType.CPU_ID
. Otherwise this property is ignored for any other affinity type. Property value must be ≥ zero.- See Also:
- Constant Field Values
-
STRATEGIES_KEY
public static final java.lang.String STRATEGIES_KEY
Key "cpuStrategies" contains CPU selection strategies array used whenAFFINITY_TYPE_KEY
is set toThreadAffinityConfigure.AffinityType.CPU_STRATEGIES
. Otherwise this property is ignored for any other affinity type. Property value must be a non-empty array where the final element isAffinityStrategies.ANY
. Note that strategy ordering is significant.- See Also:
- Constant Field Values
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
affinityType
public ThreadAffinityConfigure.AffinityType affinityType()
Returns affinity type used for creating thread affinity to selected CPU_ID.- Returns:
- CPU_ID selection type.
-
bindFlag
public boolean bindFlag()
Returns the bind-thread-to-affinity lock setting. Iftrue
then thread is bound to the lock.- Returns:
true
if thread is bound to the affinity lock.- See Also:
wholeCoreFlag()
-
wholeCoreFlag
public boolean wholeCoreFlag()
Returns whole core reservation bind settings. Iftrue
then thread reserves entire core and does not allow hyper-threading. This value is used only ifbindFlag()
returnstrue
; otherwise it is ignored.- Returns:
- whole core reservation flag.
- See Also:
bindFlag()
-
cpuId
public int cpuId()
Returns CPU_ID identifier used forThreadAffinityConfigure.AffinityType.CPU_ID
affinity type. Set to a negative number for any other affinity type.- Returns:
- CPU_ID identifier or < zero if no identifier specified.
-
cpuOffset
public int cpuOffset()
Returns CPU offset used forThreadAffinityConfigure.AffinityType.CPU_LAST_MINUS
affinity type. Set to zero for any other affinity type.- Returns:
- CPU offset or zero if no offset specified.
-
strategies
public java.util.List<net.openhft.affinity.AffinityStrategies> strategies()
Returns immutable list of CPU_ID selection strategies used forThreadAffinityConfigure.AffinityType.CPU_STRATEGIES
affinity type. Set tonull
for any other affinity type.- Returns:
- CPU_ID selection strategies.
-
loadAffinity
public static ThreadAffinityConfigure loadAffinity(java.lang.String key, com.typesafe.config.Config config)
Returns a thread affinity configuration loaded from a given JSON configuration under property key.- Parameters:
key
- affinity key is stored under this property.config
- JSON configuration.- Returns:
- thread affinity configuration.
- Throws:
com.typesafe.config.ConfigException
- ifconfig
is missing a required property or a property value is invalid.
-
loadAffinities
public static java.util.List<ThreadAffinityConfigure> loadAffinities(java.lang.String key, com.typesafe.config.Config config)
Returns list of thread affinity configurations loaded from given JSON configuration under property key.- Parameters:
key
- affinity keys stored under this property.config
- JSON configuration.- Returns:
- list of thread affinity configurations.
- Throws:
com.typesafe.config.ConfigException
- ifconfig
is missing a required property or a property value is invalid.
-
builder
public static ThreadAffinityConfigure.Builder builder()
Returns newThreadAffinityConfigure
builder instance.ThreadAffinityConfigure.Builder
is the only way to create a thread affinity configuration. It is recommended that a new builder is used to create each new thread affinity configuration.- Returns:
- thread affinity configuration builder.
-
-