\chapter{Micro-Benchmarks}\label{microbench}

The first step in evaluating this work is to test-out small controlled cases to ensure the basics work properly.
This chapter presents five different experimental setup, evaluating some of the basic features of \CFA's scheduler.

\section{Benchmark Environment}
All benchmarks are run on two distinct hardware platforms.
\begin{description}
\item[AMD] is a server with two AMD EPYC 7662 CPUs and 256GB of DDR4 RAM.
The EPYC CPU has 64 cores with 2 \glspl{hthrd} per core, for 128 \glspl{hthrd} per socket with 2 sockets for a total of 256 \glspl{hthrd}.
Each CPU has 4 MB, 64 MB and 512 MB of L1, L2 and L3 caches, respectively.
Each L1 and L2 instance are only shared by \glspl{hthrd} on a given core, but each L3 instance is shared by 4 cores, therefore 8 \glspl{hthrd}.
The server runs Ubuntu 20.04.2 LTS on top of Linux Kernel 5.8.0-55.

\item[Intel] is a server with four Intel Xeon Platinum 8160 CPUs and 384GB of DDR4 RAM.
The Xeon CPU has 24 cores with 2 \glspl{hthrd} per core, for 48 \glspl{hthrd} per socket with 4 sockets for a total of 196 \glspl{hthrd}.
Each CPU has 3 MB, 96 MB and 132 MB of L1, L2 and L3 caches respectively.
Each L1 and L2 instance are only shared by \glspl{hthrd} on a given core, but each L3 instance is shared across the entire CPU, therefore 48 \glspl{hthrd}.
The server runs Ubuntu 20.04.2 LTS on top of Linux Kernel 5.8.0-55.
\end{description}

For all benchmarks, @taskset@ is used to limit the experiment to 1 NUMA Node with no hyper threading.
If more \glspl{hthrd} are needed, then 1 NUMA Node with hyperthreading is used.
If still more \glspl{hthrd} are needed, then the experiment is limited to as few NUMA Nodes as needed.

The limited sharing of the last-level cache on the AMD machine is markedly different than the Intel machine.
Indeed, while on both architectures L2 cache misses that are served by L3 caches on a different CPU incur a significant latency, on the AMD it is also the case that cache misses served by a different L3 instance on the same CPU still incur high latency.


\section{Cycling latency}
\begin{figure}
	\centering
	\input{cycle.pstex_t}
	\caption[Cycle benchmark]{Cycle benchmark\smallskip\newline Each \at unparks the next \at in the cycle before parking itself.}
	\label{fig:cycle}
\end{figure}
The most basic evaluation of any ready queue is to evaluate the latency needed to push and pop one element from the ready queue.
Since these two operation also describe a @yield@ operation, many systems use this operation as the most basic benchmark.
However, yielding can be treated as a special case by optimizing it away since the number of ready \ats does not change.
Not all systems perform this optimization, but those that do have an artificial performance benefit because the yield becomes a \emph{nop}.
For this reason, I chose a different first benchmark, called \newterm{Cycle Benchmark}.
This benchmark arranges a number of \ats into a ring, as seen in Figure~\ref{fig:cycle}, where the ring is a circular singly-linked list.
At runtime, each \at unparks the next \at before parking itself.
Unparking the next \at pushes that \at onto the ready queue while the ensuing park leads to a \at being popped from the ready queue.

Hence, the underlying runtime cannot rely on the number of ready \ats staying constant over the duration of the experiment.
In fact, the total number of \ats waiting on the ready queue is expected to vary because of the race between the next \at unparking and the current \at parking.
That is, the runtime cannot anticipate that the current task will immediately park.
As well, the size of the cycle is also decided based on this race, \eg a small cycle may see the chain of unparks go full circle before the first \at parks because of time-slicing or multiple \procs.
Every runtime system must handle this race and cannot optimized away the ready-queue pushes and pops.
To prevent any attempt of silently omitting ready-queue operations, the ring of \ats is made big enough so the \ats have time to fully park before being unparked again.
(Note, an unpark is like a V on a semaphore, so the subsequent park (P) may not block.)
Finally, to further mitigate any underlying push/pop optimizations, especially on SMP machines, multiple rings are created in the experiment.

Figure~\ref{fig:cycle:code} shows the pseudo code for this benchmark.
There is additional complexity to handle termination (not shown), which requires a binary semaphore or a channel instead of raw @park@/@unpark@ and carefully picking the order of the @P@ and @V@ with respect to the loop condition.

\begin{figure}
\begin{cfa}
Thread.main() {
	count := 0
	for {
		@this.next.wake()@
		@wait()@
		count ++
		if must_stop() { break }
	}
	global.count += count
}
\end{cfa}
\caption[Cycle Benchmark : Pseudo Code]{Cycle Benchmark : Pseudo Code}
\label{fig:cycle:code}
\end{figure}

\subsection{Results}
\begin{figure}
	\subfloat[][Throughput, 100 cycles per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.cycle.jax.ops.pstex_t}
		}
		\label{fig:cycle:jax:ops}
	}
	\subfloat[][Throughput, 1 cycle per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.cycle.low.jax.ops.pstex_t}
		}
		\label{fig:cycle:jax:low:ops}
	}

	\subfloat[][Scalability, 100 cycles per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.cycle.jax.ns.pstex_t}
		}
		\label{fig:cycle:jax:ns}
	}
	\subfloat[][Scalability, 1 cycle per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.cycle.low.jax.ns.pstex_t}
		}
		\label{fig:cycle:jax:low:ns}
	}
	\caption[Cycle Benchmark on Intel]{Cycle Benchmark on Intel\smallskip\newline Throughput and Scalability as a function of \proc count 5 \ats per cycle and different cycle count. For Throughput higher is better, for Scalability lower is better. Each series represent 15 independent runs, the dotted lines are extremums while the solid line is the medium.}
	\label{fig:cycle:jax}
\end{figure}

\begin{figure}
	\subfloat[][Throughput, 100 cycles per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.cycle.nasus.ops.pstex_t}
		}
		\label{fig:cycle:nasus:ops}
	}
	\subfloat[][Throughput, 1 cycle per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.cycle.low.nasus.ops.pstex_t}
		}
		\label{fig:cycle:nasus:low:ops}
	}

	\subfloat[][Scalability, 100 cycles per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.cycle.nasus.ns.pstex_t}
		}
		\label{fig:cycle:nasus:ns}
	}
	\subfloat[][Scalability, 1 cycle per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.cycle.low.nasus.ns.pstex_t}
		}
		\label{fig:cycle:nasus:low:ns}
	}
	\caption[Cycle Benchmark on AMD]{Cycle Benchmark on AMD\smallskip\newline Throughput and Scalability as a function of \proc count 5 \ats per cycle and different cycle count. For Throughput higher is better, for Scalability lower is better. Each series represent 15 independent runs, the dotted lines are extremums while the solid line is the medium.}
	\label{fig:cycle:nasus}
\end{figure}
Figure~\ref{fig:cycle:jax} and Figure~\ref{fig:cycle:nasus} shows the throughput as a function of \proc count on Intel and AMD respectively, where each cycle has 5 \ats.
The graphs show traditional throughput on the top row and \newterm{scalability} on the bottom row.
Where scalability uses the same data but the Y axis is calculated as the number of \procs over the throughput.
In this representation, perfect scalability should appear as a horizontal line, \eg, if doubling the number of \procs doubles the throughput, then the relation stays the same.
The left column shows results for 100 cycles per \proc, enough cycles to always keep every \proc busy.
The right column shows results for only 1 cycle per \proc, where the ready queues are expected to be near empty at all times.
The distinction is meaningful because the idle sleep subsystem is expected to matter only in the right column, where spurious effects can cause a \proc to run out of work temporarily.

The performance goal of \CFA is to obtain equivalent performance to other, less fair schedulers and that is what results show.
Figure~\ref{fig:cycle:jax:ops} and \ref{fig:cycle:jax:ns} show very good throughput and scalability for all runtimes.
The experimental setup prioritizes running on 2 \glspl{hthrd} per core before running on multiple sockets.
The effect of that setup is seen from 25 to 48 \procs, running on 24 core with 2 \glspl{hthrd} per core.
This effect is again repeated from 73 and 96 \procs, where it happens on the second CPU.
When running only a single cycle, most runtime achieve lower throughput because of the idle-sleep mechanism.

Figure~\ref{fig:cycle:nasus} show effectively the same story happening on AMD as it does on Intel.
The different performance bumps due to cache topology happen at different locations and there is a little more variability.
However, in all cases \CFA is still competitive with other runtimes.


\section{Yield}
For completion, the classic yield benchmark is included.
This benchmark is simpler than the cycle test: it creates many \ats that call @yield@.
As mentioned, this benchmark may not be representative because of optimization shortcuts in @yield@.
The only interesting variable in this benchmark is the number of \ats per \procs, where ratios close to 1 means the ready queue(s) can be empty.
This scenario can put a strain on the idle-sleep handling compared to scenarios where there is plenty of work.
Figure~\ref{fig:yield:code} shows pseudo code for this benchmark, where the @wait/next.wake@ is replaced by @yield@.

\begin{figure}
\begin{cfa}
Thread.main() {
	count := 0
	for {
		@yield()@
		count ++
		if must_stop() { break }
	}
	global.count += count
}
\end{cfa}
\caption[Yield Benchmark : Pseudo Code]{Yield Benchmark : Pseudo Code}
\label{fig:yield:code}
\end{figure}

\subsection{Results}
\begin{figure}
	\subfloat[][Throughput, 100 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.yield.jax.ops.pstex_t}
		}
		\label{fig:yield:jax:ops}
	}
	\subfloat[][Throughput, 1 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
		\input{result.yield.low.jax.ops.pstex_t}
		}
		\label{fig:yield:jax:low:ops}
	}

	\subfloat[][Scalability, 100 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
		\input{result.yield.jax.ns.pstex_t}
		}
		\label{fig:yield:jax:ns}
	}
	\subfloat[][Scalability, 1 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
		\input{result.yield.low.jax.ns.pstex_t}
		}
		\label{fig:yield:jax:low:ns}
	}
	\caption[Yield Benchmark on Intel]{Yield Benchmark on Intel\smallskip\newline Throughput and Scalability as a function of \proc count, using 1 \ats per \proc. For Throughput higher is better, for Scalability lower is better. Each series represent 15 independent runs, the dotted lines are extremums while the solid line is the medium.}
	\label{fig:yield:jax}
\end{figure}

\begin{figure}
	\subfloat[][Throughput, 100 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.yield.nasus.ops.pstex_t}
		}
		\label{fig:yield:nasus:ops}
	}
	\subfloat[][Throughput, 1 \at per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.yield.low.nasus.ops.pstex_t}
		}
		\label{fig:yield:nasus:low:ops}
	}

	\subfloat[][Scalability, 100 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.yield.nasus.ns.pstex_t}
		}
		\label{fig:yield:nasus:ns}
	}
	\subfloat[][Scalability, 1 \at per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.yield.low.nasus.ns.pstex_t}
		}
		\label{fig:yield:nasus:low:ns}
	}
	\caption[Yield Benchmark on AMD]{Yield Benchmark on AMD\smallskip\newline Throughput and Scalability as a function of \proc count, using 1 \ats per \proc. For Throughput higher is better, for Scalability lower is better. Each series represent 15 independent runs, the dotted lines are extremums while the solid line is the medium.}
	\label{fig:yield:nasus}
\end{figure}
Figure~\ref{fig:yield:jax} shows the throughput as a function of \proc count on Intel.
It is fairly obvious why I claim this benchmark is more artificial.
The throughput is dominated by the mechanism used to handle the @yield@.
\CFA does not have special handling for @yield@ and achieves very similar performance to the cycle benchmark.
Libfibre uses the fact that @yield@ doesn't change the number of ready fibres and by-passes the idle-sleep mechanism entirely, producing significantly better throughput.
Go puts yielding goroutines on a secondary global ready-queue, giving them lower priority.
The result is that multiple \glspl{hthrd} contend for the global queue and performance suffers drastically.
Based on the scalability, Tokio obtains the same poor performance and therefore it is likely it handles @yield@ in a similar fashion.

When the number of \ats is reduce to 1 per \proc, the cost of idle sleep also comes into play in a very significant way.
If anything causes a \at migration, where two \ats end-up on the same ready-queue, work-stealing will start occuring and cause every \at to shuffle around.
In the process, several \procs can go to sleep transiently if they fail to find where the \ats were shuffled to.
In \CFA, spurious bursts of latency can trick a \proc into helping, triggering this effect.
However, since user-level threading with equal number of \ats and \procs is a somewhat degenerate case, especially when ctxswitching very often, this result is not particularly meaningful and is only included for completness.

Again, Figure~\ref{fig:yield:nasus} show effectively the same story happening on AMD as it does on Intel.
\CFA fairs slightly better with many \ats per \proc, but the performance is satisfactory on both architectures.

Since \CFA obtains the same satisfactory performance as the previous benchmark this is still a success, albeit a less meaningful one.


\section{Churn}
The Cycle and Yield benchmark represent an \emph{easy} scenario for a scheduler, \eg an embarrassingly parallel application.
In these benchmarks, \ats can be easily partitioned over the different \procs upfront and none of the \ats communicate with each other.

The Churn benchmark represents more chaotic executions, where there is more communication among \ats but no relation between the last \proc on which a \at ran and blocked and the \proc that subsequently unblocks it.
With processor-specific ready-queues, when a \at is unblocked by a different \proc that means the unblocking \proc must either ``steal'' the \at from another processor or find it on a global queue.
This dequeuing results in either contention on the remote queue and/or \glspl{rmr} on \at data structure.
In either case, this benchmark aims to measure how well each scheduler handles these cases, since both cases can lead to performance degradation if not handled correctly.

This benchmark uses a fixed-size array of counting semaphores.
Each \at picks a random semaphore, @V@s it to unblock any \at waiting, and then @P@s on the semaphore.
This creates a flow where \ats push each other out of the semaphores before being pushed out themselves.
For this benchmark to work, the number of \ats must be equal or greater than the number of semaphores plus the number of \procs.
Note, the nature of these semaphores mean the counter can go beyond 1, which can lead to nonblocking calls to @P@.
Figure~\ref{fig:churn:code} shows pseudo code for this benchmark, where the @yield@ is replaced by @V@ and @P@.

\begin{figure}
\begin{cfa}
Thread.main() {
	count := 0
	for {
		r := random() % len(spots)
		@spots[r].V()@
		@spots[r].P()@
		count ++
		if must_stop() { break }
	}
	global.count += count
}
\end{cfa}
\caption[Churn Benchmark : Pseudo Code]{Churn Benchmark : Pseudo Code}
\label{fig:churn:code}
\end{figure}

\subsection{Results}
\begin{figure}
	\subfloat[][Throughput, 100 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.churn.jax.ops.pstex_t}
		}
		\label{fig:churn:jax:ops}
	}
	\subfloat[][Throughput, 1 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.churn.low.jax.ops.pstex_t}
		}
		\label{fig:churn:jax:low:ops}
	}

	\subfloat[][Latency, 100 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.churn.jax.ns.pstex_t}
		}
		\label{fig:churn:jax:ns}
	}
	\subfloat[][Latency, 1 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.churn.low.jax.ns.pstex_t}
		}
		\label{fig:churn:jax:low:ns}
	}
	\caption[Churn Benchmark on Intel]{\centering Churn Benchmark on Intel\smallskip\newline Throughput and latency of the Churn on the benchmark on the Intel machine. For Throughput higher is better, for Scalability lower is better. Each series represent 15 independent runs, the dotted lines are extremums while the solid line is the medium.}
	\label{fig:churn:jax}
\end{figure}

\begin{figure}
	\subfloat[][Throughput, 100 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.churn.nasus.ops.pstex_t}
		}
		\label{fig:churn:nasus:ops}
	}
	\subfloat[][Throughput, 1 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.churn.low.nasus.ops.pstex_t}
		}
		\label{fig:churn:nasus:low:ops}
	}

	\subfloat[][Latency, 100 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.churn.nasus.ns.pstex_t}
		}
		\label{fig:churn:nasus:ns}
	}
	\subfloat[][Latency, 1 \ats per \proc]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.churn.low.nasus.ns.pstex_t}
		}
		\label{fig:churn:nasus:low:ns}
	}
	\caption[Churn Benchmark on AMD]{\centering Churn Benchmark on AMD\smallskip\newline Throughput and latency of the Churn on the benchmark on the AMD machine.
	For Throughput higher is better, for Scalability lower is better. Each series represent 15 independent runs, the dotted lines are extremums while the solid line is the medium.}
	\label{fig:churn:nasus}
\end{figure}
Figure~\ref{fig:churn:jax} shows the throughput as a function of \proc count on Intel.
Like for the cycle benchmark, here are runtimes achieve fairly similar performance.
Scalability is notably worst than the previous benchmarks since there is inherently more communication between processors.
Indeed, once the number of \glspl{hthrd} goes beyond a single socket, performance ceases to improve.
An interesting aspect to note here is that the runtimes differ in how they handle this situation.
Indeed, when a \proc unparks a \at that was last run on a different \proc, the \at could be appended to the ready-queue local \proc or to the ready-queue of the remote \proc, which previously ran the \at.
\CFA, tokio and Go all use the approach of unparking to the local \proc while Libfibre unparks to the remote \proc.
In this particular benchmark, the inherent chaos of the benchmark in addition to small memory footprint means neither approach wins over the other.

Figure~\ref{fig:churn:nasus} shows effectively the same picture.
Performance improves as long as all \procs fit on a single socket.
Beyond that performance plateaus.

Again this performance demonstrate \CFA achieves satisfactory performance.

\section{Locality}
\begin{figure}
\begin{cfa}
Thread.main() {
	count := 0
	for {
		r := random() % len(spots)
		// go through the array
		@work( a )@
		spots[r].V()
		spots[r].P()
		count ++
		if must_stop() { break }
	}
	global.count += count
}
\end{cfa}
\begin{cfa}
Thread.main() {
	count := 0
	for {
		r := random() % len(spots)
		// go through the array
		@work( a )@
		// pass array to next thread
		spots[r].V( @a@ )
		@a = @spots[r].P()
		count ++
		if must_stop() { break }
	}
	global.count += count
}
\end{cfa}
\caption[Locality Benchmark : Pseudo Code]{Locality Benchmark : Pseudo Code}
\label{fig:locality:code}
\end{figure}
As mentionned in the churn benchmark, when unparking a \at, it is possible to either unpark to the local or remote ready-queue.
\footnote{It is also possible to unpark to a third unrelated ready-queue, but unless the scheduler has additional knowledge about the situation, it is unlikely to result in good cache locality.}
The locality experiment includes two variations of the churn benchmark, where an array of data is added.
In both variations, before @V@ing the semaphore, each \at increment random cells inside the array.
The @share@ variation then passes the array to the shadow-queue of the semaphore, effectively transferring ownership of the array to the woken thread.
In the @noshare@ variation the array is not passed on and each thread continously accesses its private array.

The objective here is to highlight the different decision made by the runtime when unparking.
Since each thread unparks a random semaphore, it means that it is unlikely that a \at will be unparked from the last \proc it ran on.
In the @share@ version, this means that unparking the \at on the local \proc is appropriate since the data was last modified on that \proc.
In the @noshare@ version, the reverse is true.

The expectation for this benchmark is to see a performance inversion, where runtimes will fare notably better in the variation which matches their unparking policy.
This should lead to \CFA, Go and Tokio achieving better performance in @share@ while libfibre achieves better performance in @noshare@.

\subsection{Results}
\begin{figure}
	\subfloat[][Throughput share]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.locality.share.jax.ops.pstex_t}
		}
		\label{fig:locality:jax:share:ops}
	}
	\subfloat[][Throughput noshare]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.locality.noshare.jax.ops.pstex_t}
		}
		\label{fig:locality:jax:noshare:ops}
	}

	\subfloat[][Scalability share]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.locality.share.jax.ns.pstex_t}
		}
		\label{fig:locality:jax:share:ns}
	}
	\subfloat[][Scalability noshare]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.locality.noshare.jax.ns.pstex_t}
		}
		\label{fig:locality:jax:noshare:ns}
	}
	\caption[Locality Benchmark on Intel]{Locality Benchmark on Intel\smallskip\newline Throughput and Scalability as a function of \proc count. For Throughput higher is better, for Scalability lower is better. Each series represent 15 independent runs, the dotted lines are extremums while the solid line is the medium.}
	\label{fig:locality:jax}
\end{figure}
\begin{figure}
	\subfloat[][Throughput share]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.locality.share.nasus.ops.pstex_t}
		}
		\label{fig:locality:nasus:share:ops}
	}
	\subfloat[][Throughput noshare]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.locality.noshare.nasus.ops.pstex_t}
		}
		\label{fig:locality:nasus:noshare:ops}
	}

	\subfloat[][Scalability share]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.locality.share.nasus.ns.pstex_t}
		}
		\label{fig:locality:nasus:share:ns}
	}
	\subfloat[][Scalability noshare]{
		\resizebox{0.5\linewidth}{!}{
			\input{result.locality.noshare.nasus.ns.pstex_t}
		}
		\label{fig:locality:nasus:noshare:ns}
	}
	\caption[Locality Benchmark on AMD]{Locality Benchmark on AMD\smallskip\newline Throughput and Scalability as a function of \proc count. For Throughput higher is better, for Scalability lower is better. Each series represent 15 independent runs, the dotted lines are extremums while the solid line is the medium.}
	\label{fig:locality:nasus}
\end{figure}

Figure~\ref{fig:locality:jax} shows that the results somewhat follow the expectation.
On the left of the figure showing the results for the shared variation, where \CFA and tokio outperform libfibre as expected.
And correspondingly on the right, we see the expected performance inversion where libfibre now outperforms \CFA and tokio.
Otherwise the results are similar to the churn benchmark, with lower throughtput due to the array processing.
It is unclear why Go's performance is notably worst than the other runtimes.

Figure~\ref{fig:locality:nasus} shows the same experiment on AMD.
\todo{why is cfa slower?}
Again, we see the same story, where tokio and libfibre swap places and Go trails behind.

\section{Transfer}
The last benchmark is more of an experiment than a benchmark.
It tests the behaviour of the schedulers for a misbehaved workload.
In this workload, one of the \at is selected at random to be the leader.
The leader then spins in a tight loop until it has observed that all other \ats have acknowledged its leadership.
The leader \at then picks a new \at to be the next leader and the cycle repeats.
The benchmark comes in two flavours for the non-leader \ats:
once they acknowledged the leader, they either block on a semaphore or spin yielding.

The experiment is designed to evaluate the short-term load-balancing of a scheduler.
Indeed, schedulers where the runnable \ats are partitioned on the \procs may need to balance the \ats for this experiment to terminate.
This problem occurs because the spinning \at is effectively preventing the \proc from running any other \at.
In the semaphore flavour, the number of runnable \ats eventually dwindles down to only the leader.
This scenario is a simpler case to handle for schedulers since \procs eventually run out of work.
In the yielding flavour, the number of runnable \ats stays constant.
This scenario is a harder case to handle because corrective measures must be taken even when work is available.
Note, runtime systems with preemption circumvent this problem by forcing the spinner to yield.

I both flavours, the experiment effectively measures how long it takes for all \ats to run once after a given synchronization point.
In an ideal scenario where the scheduler is strictly FIFO, every thread would run once after the synchronization and therefore the delay between leaders would be given by:
$ \frac{CSL + SL}{NP - 1}$, where $CSL$ is the context switch latency, $SL$ is the cost for enqueuing and dequeuing a \at and $NP$ is the number of \procs.
However, if the scheduler allows \ats to run many times before other \ats are able to run once, this delay will increase.
The semaphore version is an approximation of the strictly FIFO scheduling, where none of the \ats \emph{attempt} to run more than once.
The benchmark effectively provides the fairness guarantee in this case.
In the yielding version however, the benchmark provides no such guarantee, which means the scheduler has full responsability and any unfairness will be measurable.

While this is a fairly artificial scenario, it requires only a few simple pieces.
The yielding version of this simply creates a scenario where a \at runs uninterrupted in a saturated system, and starvation has a easily measured impact.
However, \emph{any} \at that runs uninterrupted for a significant period of time in a saturated system could lead to this kind of starvation.

\begin{figure}
\begin{cfa}
Thread.lead() {
	this.idx_seen = ++lead_idx
	if lead_idx > stop_idx {
		done := true
		return
	}
	// Wait for everyone to acknowledge my leadership
	start: = timeNow()
	for t in threads {
		while t.idx_seen != lead_idx {
			asm pause
			if (timeNow() - start) > 5 seconds { error() }
		}
	}
	// pick next leader
	leader := threads[ prng() % len(threads) ]
	// wake every one
	if ! exhaust {
		for t in threads {
			if t != me { t.wake() }
		}
	}
}
Thread.wait() {
	this.idx_seen := lead_idx
	if exhaust { wait() }
	else { yield() }
}
Thread.main() {
	while !done  {
		if leader == me { this.lead() }
		else { this.wait() }
	}
}
\end{cfa}
\caption[Transfer Benchmark : Pseudo Code]{Transfer Benchmark : Pseudo Code}
\label{fig:transfer:code}
\end{figure}

\subsection{Results}
\begin{figure}
\begin{centering}
\begin{tabular}{r | c c c c | c c c c }
Machine   &			\multicolumn{4}{c |}{Intel}                &          \multicolumn{4}{c}{AMD}                    \\
Variation & \multicolumn{2}{c}{Park} & \multicolumn{2}{c |}{Yield} & \multicolumn{2}{c}{Park} & \multicolumn{2}{c}{Yield} \\
\procs    &      2      &      192   &      2      &      192      &      2      &      256   &      2      &      256    \\
\hline
\CFA      & 106 $\mu$s  & j200       & 68.4 $\mu$s & ~1.2 ms       & 174 $\mu$s  & ~28.4 ms   & 78.8~~$\mu$s& ~~1.21 ms   \\
libfibre  & 127 $\mu$s  &            & DNC         & DNC           & 156 $\mu$s  & ~36.7 ms   & DNC         & DNC         \\
Go        & 106 $\mu$s  & j200       & 24.6 ms     & 74.3 ms       & 271 $\mu$s  & 121.6 ms   & ~~1.21~ms   & 117.4 ms    \\
tokio     & 289 $\mu$s  &            & DNC         & DNC           & 157 $\mu$s  & 111.0 ms   & DNC         & DNC
\end{tabular}
\end{centering}
\caption[Transfer Benchmark on Intel and AMD]{Transfer Benchmark on Intel and AMD\smallskip\newline Average measurement of how long it takes for all \ats to acknowledge the leader \at. DNC stands for ``did not complete'', meaning that after 5 seconds of a new leader being decided, some \ats still had not acknowledged the new leader. }
\label{fig:transfer:res}
\end{figure}
Figure~\ref{fig:transfer:res} shows the result for the transfer benchmark with 2 \procs and all \procs, where each experiement runs 100 \at per \proc.
Note that the results here are only meaningful as a coarse measurement of fairness, beyond which small cost differences in the runtime and concurrent primitives begin to matter.
As such, data points that are the on the same order of magnitude as eachother should be basically considered equal.
The takeaway of this experiement is the presence of very large differences.
The semaphore variation is denoted ``Park'', where the number of \ats dwindles down as the new leader is acknowledged.
The yielding variation is denoted ``Yield''.
The experiement was only run for the extremums of the number of cores since the scaling per core behaves like previous experiements.
This experiments clearly demonstrate that while the other runtimes achieve similar performance, \CFA achieves significantly better fairness.
The semaphore variation serves as a control group, where all runtimes are expected to transfer leadership fairly quickly.
Since \ats block after acknowledging the leader, this experiment effectively measures how quickly \procs can steal \ats from the \proc running leader.
Figure~\ref{fig:transfer:res} shows that while Go and Tokio are slower, all runtime achieve decent latency.
However, the yielding variation shows an entirely different picture.
Since libfibre and tokio have a traditional work-stealing scheduler, \procs that have \ats on their local queues will never steal from other \procs.
The result is that the experiement simply does not complete for these runtime.
Without \procs stealing from the \proc running the leader, the experiment will simply never terminate.
Go manages to complete the experiement because it adds preemption on top of classic work-stealing.
However, since preemption is fairly costly it achieves significantly worst performance.
In contrast, \CFA achieves equivalent performance in both variations, demonstrating very good fairness.
Interestingly \CFA achieves better delays in the yielding version than the semaphore version, however, that is likely due to fairness being equivalent but removing the cost of the semaphores.