Class ThreadAffinityConfigure
- java.lang.Object
-
- net.sf.eBus.config.ThreadAffinityConfigure
-
public class ThreadAffinityConfigure extends java.lang.ObjectThis 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 cpuIdto assign a CPU with specified identifier to thread. Requires propertyCPU_OFFSET_KEYbe set. -
CPU_ID: UseAffinityLock.acquireLock(int cpuIdto assign a CPU with specified identifier to thread. Requires propertyCPU_ID_KEYbe 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_KEYbe 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 allocatedAffinityLockreserving the whole core. This property is used only whenbindproperty istrue. -
cpuId: Required whenaffinityTypeis set toCPU_ID. Specifies the allocated CPU by its identifier. -
cpuStrategies: Required whenaffinityTypeis set toCPU_STRATEGIES. Values are restricted to enumnet.openhft.affinity.AffinityStrategies.Note: strategy ordering is important.
AffinityStrategies.ANYmust 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.SelectorThreadandnet.sf.eBus.client.EClient.RQThreadinstances to a core or cores.- Author:
- Charles W. Rapp
- See Also:
ThreadAffinityConfigure.Builder
-
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classThreadAffinityConfigure.AffinityTypeThese affinity types map to a specificAffinityLock staticmethod used to acquire an affinity lock.static classThreadAffinityConfigure.BuilderConstructs aThreadAffinityConfigureinstance based on the builder settings.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringAFFINITY_TYPE_KEYKey "affinityType" contains anThreadAffinityConfigure.AffinityTypevalue.static java.lang.StringBIND_KEYKey "bind" contains the bind-thread-to-affinity lock flag.static java.lang.StringCPU_ID_KEYKey "cpuId" contains CPU identifier used whenAFFINITY_TYPE_KEYis set toThreadAffinityConfigure.AffinityType.CPU_ID.static java.lang.StringCPU_OFFSET_KEYKey "lastMinusOffset" contains CPU offset used whenAFFINITY_TYPE_KEYis set toThreadAffinityConfigure.AffinityType.CPU_LAST_MINUS.static java.lang.StringSTRATEGIES_KEYKey "cpuStrategies" contains CPU selection strategies array used whenAFFINITY_TYPE_KEYis set toThreadAffinityConfigure.AffinityType.CPU_STRATEGIES.static java.lang.StringWHOLE_CORE_KEYKey "wholeCore" contains reserve-whole-core flag.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ThreadAffinityConfigure.AffinityTypeaffinityType()Returns affinity type used for creating thread affinity to selected CPU_ID.booleanbindFlag()Returns the bind-thread-to-affinity lock setting.static ThreadAffinityConfigure.Builderbuilder()Returns newThreadAffinityConfigurebuilder instance.intcpuId()Returns CPU_ID identifier used forThreadAffinityConfigure.AffinityType.CPU_IDaffinity type.intcpuOffset()Returns CPU offset used forThreadAffinityConfigure.AffinityType.CPU_LAST_MINUSaffinity type.booleanequals(java.lang.Object o)inthashCode()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 ThreadAffinityConfigureloadAffinity(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_STRATEGIESaffinity type.java.lang.StringtoString()booleanwholeCoreFlag()Returns whole core reservation bind settings.
-
-
-
Field Detail
-
AFFINITY_TYPE_KEY
public static final java.lang.String AFFINITY_TYPE_KEY
Key "affinityType" contains anThreadAffinityConfigure.AffinityTypevalue.- 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_KEYis 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_KEYis 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_KEYis 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_KEYis 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:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin 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. Iftruethen thread is bound to the lock.- Returns:
trueif thread is bound to the affinity lock.- See Also:
wholeCoreFlag()
-
wholeCoreFlag
public boolean wholeCoreFlag()
Returns whole core reservation bind settings. Iftruethen 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_IDaffinity 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_MINUSaffinity 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_STRATEGIESaffinity type. Set tonullfor 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- ifconfigis 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- ifconfigis missing a required property or a property value is invalid.
-
builder
public static ThreadAffinityConfigure.Builder builder()
Returns newThreadAffinityConfigurebuilder instance.ThreadAffinityConfigure.Builderis 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.
-
-