CPU affinity refers to the strong preference a task has for a particular CPU (core). When a task is made to always run on a particular core, the chances of experiencing cache misses reduces. This effectively improves the performance and thus throughput of a program.
Affinity in SCONE
SCONE has three types of threads:
- lthreads: these are threads that execute the application generated threads (like pthreads) inside of an enclave.
- ethreads: these are threads that execute the lthreads inside of enclaves. The number of ethreads has to be defined in the SCONE configuration file. SCONE performs an n:m mapping of lthreads to ethreads. This includes workstealing in the sense that before an ethread goes to sleep, it checks if another ethread has more than one lthread ready to execute.
- sthreads: run outside the enclave (i.e., in native mode). The sthreads execute the syscalls on behalf of the lthreads.
CPU affinity in SCONE is supported by locking ethreads (as well as sthreads) to particular cores/cpus. The affinity of ethreads can be specified in the scone configuration file passed to the SCONE runtime. By default, an ethread has zero cpu preference. This is also the default behavior on Linux.
- has no affinity by default, but
- can be pinned to a particular ethread and a particular core if the ethread is pinned. By default, a lthread has zero cpu preference, even if it's parent had affinity for particular ehtread. This is the default behavior on Linux.
Number of available CPUs
In SCONE, then number of available CPUs is reported as the number of ethreads.
This means the number of virtual cores/CPUS is determined by the default SCONE SCONE configuration file
/etc/sgx-musl.conf) unless the path is changed via the environment variable
Using CPU Affinity
The standard C library (musl-libc in the case of SCONE), exposes the inquisition and setting of CPU preference by an application through two system calls.
#define _GNU_SOURCE #include <sched.h> int sched_setaffinity(pid_t pid, size_t cpusetsize,const cpu_set_t *mask); int sched_getaffinity(pid_t pid, size_t cpusetsize,cpu_set_t *mask);
man 2 SCHED_AFFINITY for a complete description.
Configuring CPU affinity
The CPU affinity for an arbitrary application can be effected by specifying the affinity a particular ethread has. By doing so, all the lthreads that will be locked to the ethreads, will effectively run on the CPUS on which the ethreads run. This will guarantee cpu prefrence is propagated to userspace.