Skip to content

CPU Shielding

Idan Sheinberg edited this page Oct 19, 2020 · 20 revisions

No matter what's the scope of resource allocation you're trying to optimize, aside from dividing your own workloads between different CPU cores, you also need to ensure other tasks competing for the same computation resources will also abide by your affinity settings.

Just to clarify, imagine an 8 core machine, where you divide these 8 cores between 2 applications. Nothing prevents the OS' internal tasks and processes from competing for computational resources with any of your applications, as these tend to run without any affinity setting set in place.

In order to address this issue, the concept of CPU Shielding was conceived - excluding a set of specified cores from native the OS scheduler, meaning no task would be scheduled on the shielded core set, aside from such tasks explicitly designated and set to run inside the shielded core-set. This is in turn, ensures tasks running inside the shielded core-set won't ever be interrupted by any task not explicitly permitted to do so.

Process Level Shielding

JVM Thread Level Shielding

Differing from other JVM core-affinity solutions, needle provides the option to control the affinity settings of JVM threads not managed directly by the programmer. Meaning, you can control the affinity of threads spawned by 3rd party libraries (or the JVM itself) with altering needing access to the source code.

In order to use this feature, all you need to do is download and use needle's javaagent (available here).

Note: The agent supports both dynamic and static loading.

The agent accepts a single parameter - a file URL pointing to a yaml configuration file:

 java -javaagent:needle-agent-0.3.0-RELEASE.jar=file:/some/path/to/agent/configuration.yml -jar yourApplication.har

Note: The agent manifest relies on the versioned agent jar name (needle-agent-x.x.x-RELEASE.jar) for proper class-loading, do not change the jar file name.

Agent configuration

The yaml configuration file instructs the agent how to set affinity for various threads. It's structured in the following manner:

defaultAffinity: 0
affinityGroups:
  - identifier: name-prefix-example
    affinity: "0"
    qualifier: NAME
    matcher:
      type: PREFIX
      prefix: "some-thread-name"
  - identifier: class-regex-example
    affinity: 2
    qualifier: CLASS
    matcher:
      type: REGEX
      pattern: "org\\.sheinbergon\\..+SomeThreadImplementation.*"
defaultAffinity

Affinity setting to use for all JVM threads that does not match any of the affinity groups listed in this configuration file.
0 means no setting (use the process' affinity settings).

affinityGroups

affinityGroups[*].identifier

affinityGroups[*].affinity

affinityGroups[*].qualifier

affinityGroups[*].matcher

affinityGroups[*].type

affinityGroups[*].pattern

affinityGroups[*].prefix