Class ThreadAffinityConfig
ThreadAffinityConfig consist of the following
typesafe properties:
-
affinityType: Required. Defines how core is acquired for the thread. There are five acquisition types as defined byThreadAffinityConfig.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 efs thread affinity configuration. This includes configuring the operating system to isolate acquired CPUs from the operating system. This prevents the OS from preempting 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 a typesafe HOCON file defining a how to use thread affinity for efs dispatcher threads and especially the CPU_STRATEGIES acquisition type.
dispatchers : [
{
dispatcherName = "mdDispatcher"
threadType = SPINNING
numThreads = 1
priority = 9
eventQueueCapacity = 65536
maxEvents = 65536
runQueueCapacity = 1 // only one market data agent.
affinity { // optional, selector thread core affinity
affinityType = CPU_ID // required, core selection type.
cpuId = 7 // required for CPU_ID affinity type
bindFlag = true // optional, defaults to false
wholeCoreFlag = true // optional, defaults to false
}
},
{
dispatcherName = "orderDispatcher"
threadType = SPINNING
numThreads = 1
priority = 9
eventQueueCapacity = 8196
maxEvents = 128
runQueueCapacity = 16 // equals number of order processing agents.
affinity { // 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.
]
bindFlag = true // optional, defaults to false
wholeCoreFlag = true // optional, defaults to false
}
},
{
dispatcherName = "defaultDispatcher"
threadType = BLOCKING
numThreads = 8
priority = 4
eventQueueCapacity = 512
maxEvents = 64
runQueueCapacity = 128
}
]
efs uses this configuration to pin
EfsDispatcherThread
instances to a core or cores.
- Author:
- Charles W. Rapp
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThese affinity types map to a specificAffinityLock staticmethod used to acquire an affinity lock. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringKey "affinityName" contain unique affinity name.static final StringKey "affinityType" contains anThreadAffinityConfig.AffinityTypevalue.static final StringKey "bind" contains the bind-thread-to-affinity lock flag.static final StringKey "cpuId" contains CPU identifier used whenAFFINITY_TYPE_KEYis set toThreadAffinityConfig.AffinityType.CPU_ID.static final StringKey "cpuOffset" contains CPU offset used whenAFFINITY_TYPE_KEYis set toThreadAffinityConfig.AffinityType.CPU_LAST_MINUS.static final StringKey "cpuStrategies" contains CPU selection strategies array used whenAFFINITY_TYPE_KEYis set toThreadAffinityConfig.AffinityType.CPU_STRATEGIES.static final StringKey "wholeCore" contains reserve-whole-core flag. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns affinity type used for creating thread affinity to selected CPU_ID.booleanReturns the bind-thread-to-affinity lock setting.intgetCpuId()Returns CPU_ID identifier used forThreadAffinityConfig.AffinityType.CPU_IDaffinity type.intReturns CPU offset used forThreadAffinityConfig.AffinityType.CPU_LAST_MINUSaffinity type.List<net.openhft.affinity.AffinityStrategies> Returns immutable list of CPU_ID selection strategies used forThreadAffinityConfig.AffinityType.CPU_STRATEGIESaffinity type.booleanReturns whole core reservation bind settings.voidSets affinity type to the given value.voidsetBindFlag(boolean flag) Sets flag for binding thread to affinity lock.voidsetCpuId(int id) Sets CPU_ID identifier used byThreadAffinityConfig.AffinityType.CPU_IDaffinity type.voidsetLastMinusOffset(int offset) Sets positive integer offset used byThreadAffinityConfig.AffinityType.CPU_LAST_MINUSaffinity type.voidsetStrategies(List<net.openhft.affinity.AffinityStrategies> strategies) Sets CPU selection strategies used byThreadAffinityConfig.AffinityType.CPU_STRATEGIESaffinity type.voidsetWholeCoreFlag(boolean flag) Sets flag reserving the entire core and not allowing hyper-threading on that core.toString()
-
Field Details
-
AFFINITY_NAME_KEY
Key "affinityName" contain unique affinity name.- See Also:
-
AFFINITY_TYPE_KEY
Key "affinityType" contains anThreadAffinityConfig.AffinityTypevalue.- See Also:
-
BIND_KEY
Key "bind" contains the bind-thread-to-affinity lock flag.- See Also:
-
WHOLE_CORE_KEY
-
CPU_OFFSET_KEY
Key "cpuOffset" contains CPU offset used whenAFFINITY_TYPE_KEYis set toThreadAffinityConfig.AffinityType.CPU_LAST_MINUS. Otherwise this property is ignored for any other affinity type.- See Also:
-
CPU_ID_KEY
Key "cpuId" contains CPU identifier used whenAFFINITY_TYPE_KEYis set toThreadAffinityConfig.AffinityType.CPU_ID. Otherwise this property is ignored for any other affinity type. Property value must be ≥ zero.- See Also:
-
STRATEGIES_KEY
Key "cpuStrategies" contains CPU selection strategies array used whenAFFINITY_TYPE_KEYis set toThreadAffinityConfig.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:
-
-
Constructor Details
-
ThreadAffinityConfig
public ThreadAffinityConfig()Default constructor sets fields to invalid values.
-
-
Method Details
-
toString
-
getAffinityType
Returns affinity type used for creating thread affinity to selected CPU_ID.- Returns:
- CPU_ID selection type.
-
getBindFlag
public boolean getBindFlag()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:
-
getWholeCoreFlag
public boolean getWholeCoreFlag()Returns whole core reservation bind settings. Iftruethen thread reserves entire core and does not allow hyper-threading. This value is used only ifgetBindFlag()returnstrue; otherwise ignored.- Returns:
- whole core reservation flag.
- See Also:
-
getCpuId
public int getCpuId()Returns CPU_ID identifier used forThreadAffinityConfig.AffinityType.CPU_IDaffinity type. Set to a negative number for any other affinity type.- Returns:
- CPU_ID identifier or < zero if no identifier specified.
-
getLastMinusOffset
public int getLastMinusOffset()Returns CPU offset used forThreadAffinityConfig.AffinityType.CPU_LAST_MINUSaffinity type. Set to zero for any other affinity type.- Returns:
- CPU offset or zero if no offset specified.
-
getStrategies
Returns immutable list of CPU_ID selection strategies used forThreadAffinityConfig.AffinityType.CPU_STRATEGIESaffinity type. Set tonullfor any other affinity type.- Returns:
- CPU_ID selection strategies.
-
setAffinityType
Sets affinity type to the given value.- Parameters:
type- desired thread affinity type.- Throws:
com.typesafe.config.ConfigException- iftypeisnull.
-
setBindFlag
public void setBindFlag(boolean flag) Sets flag for binding thread to affinity lock.- Parameters:
flag- iftruethen bind thread to affinity lock.
-
setWholeCoreFlag
public void setWholeCoreFlag(boolean flag) Sets flag reserving the entire core and not allowing hyper-threading on that core. This flag is used only whensetBindFlag(boolean)is set totrue; otherwise it is ignored.- Parameters:
flag- iftruethen binding reserves whole core.
-
setCpuId
public void setCpuId(int id) Sets CPU_ID identifier used byThreadAffinityConfig.AffinityType.CPU_IDaffinity type. This value must be set when usingCPU_IDaffinity type and is ignored for all others.Note:
idis not checked to see if it is a valid CPU_ID identifier beyond making sure it is ≥ zero.- Parameters:
id- CPU_ID identifier.- Throws:
com.typesafe.config.ConfigException- ifidis < zero.
-
setLastMinusOffset
public void setLastMinusOffset(int offset) Sets positive integer offset used byThreadAffinityConfig.AffinityType.CPU_LAST_MINUSaffinity type. This value must be set when usingCPU_LAST_MINUSaffinity type and is ignored for all others.Note:
nis not checked to see if it is a valid CPU offset beyond making sure it is > zero.- Parameters:
offset- offset from end used to select CPU.- Throws:
com.typesafe.config.ConfigException- ifnis ≤ zero.
-
setStrategies
Sets CPU selection strategies used byThreadAffinityConfig.AffinityType.CPU_STRATEGIESaffinity type. This list must be set when usingCPU_STRATEGIESaffinity type and is ignored for all others.Note: strategy ordering is important. The
ANYstrategy 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.- Parameters:
strategies- CPU selection strategies list.- Throws:
com.typesafe.config.ConfigException- ifstrategiesis eithernull. and empty list, or the final element is notAffinityStrategies.ANY.
-