source: doc/theses/thierry_delisle_PhD/thesis/text/core.tex @ aa60460

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since aa60460 was aa60460, checked in by Peter A. Buhr <pabuhr@…>, 21 months ago

proofread chapter text/core.tex

  • Property mode set to 100644
File size: 29.5 KB
RevLine 
[86c1f1c3]1\chapter{Scheduling Core}\label{core}
2
[bace538]3Before discussing scheduling in general, where it is important to address systems that are changing states, this document discusses scheduling in a somewhat ideal scenario, where the system has reached a steady state. For this purpose, a steady state is loosely defined as a state where there are always \glspl{thrd} ready to run and the system has the resources necessary to accomplish the work, \eg, enough workers. In short, the system is neither overloaded nor underloaded.
[86c1f1c3]4
[6db62fa]5It is important to discuss the steady state first because it is the easiest case to handle and, relatedly, the case in which the best performance is to be expected. As such, when the system is either overloaded or underloaded, a common approach is to try to adapt the system to this new load and return to the steady state, \eg, by adding or removing workers. Therefore, flaws in scheduling the steady state tend to be pervasive in all states.
[86c1f1c3]6
[b9537e6]7\section{Design Goals}
[bace538]8As with most of the design decisions behind \CFA, an important goal is to match the expectation of the programmer according to their execution mental-model. To match expectations, the design must offer the programmer sufficient guarantees so that, as long as they respect the execution mental-model, the system also respects this model.
[86c1f1c3]9
[bace538]10For threading, a simple and common execution mental-model is the ``Ideal multi-tasking CPU'' :
[b9537e6]11
12\begin{displayquote}[Linux CFS\cit{https://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt}]
13        {[The]} ``Ideal multi-tasking CPU'' is a (non-existent  :-)) CPU that has 100\% physical power and which can run each task at precise equal speed, in parallel, each at [an equal fraction of the] speed.  For example: if there are 2 tasks running, then it runs each at 50\% physical power --- i.e., actually in parallel.
[bace538]14        \label{q:LinuxCFS}
[b9537e6]15\end{displayquote}
16
17Applied to threads, this model states that every ready \gls{thrd} immediately runs in parallel with all other ready \glspl{thrd}. While a strict implementation of this model is not feasible, programmers still have expectations about scheduling that come from this model.
18
[bace538]19In general, the expectation at the center of this model is that ready \glspl{thrd} do not interfere with each other but simply share the hardware. This assumption makes it easier to reason about threading because ready \glspl{thrd} can be thought of in isolation and the effect of the scheduler can be virtually ignored. This expectation of \gls{thrd} independence means the scheduler is expected to offer two guarantees:
[b9537e6]20\begin{enumerate}
[bace538]21        \item A fairness guarantee: a \gls{thrd} that is ready to run is not prevented by another thread.
22        \item A performance guarantee: a \gls{thrd} that wants to start or stop running is not prevented by other threads wanting to do the same.
[b9537e6]23\end{enumerate}
24
[bace538]25It is important to note that these guarantees are expected only up to a point. \Glspl{thrd} that are ready to run should not be prevented to do so, but they still share the limited hardware resources. Therefore, the guarantee is considered respected if a \gls{thrd} gets access to a \emph{fair share} of the hardware resources, even if that share is very small.
[b9537e6]26
[aa60460]27Similar to the performance guarantee, the lack of interference among threads is only relevant up to a point. Ideally, the cost of running and blocking should be constant regardless of contention, but the guarantee is considered satisfied if the cost is not \emph{too high} with or without contention. How much is an acceptable cost is obviously highly variable. For this document, the performance experimentation attempts to show the cost of scheduling is at worst equivalent to existing algorithms used in popular languages. This demonstration can be made by comparing applications built in \CFA to applications built with other languages or other models. Recall programmer expectation is that the impact of the scheduler can be ignored. Therefore, if the cost of scheduling is competitive to other popular languages, the guarantee is consider achieved.
[c04a19e]28More precisely the scheduler should be:
29\begin{itemize}
30        \item As fast as other schedulers that are less fair.
[bace538]31        \item Faster than other schedulers that have equal or better fairness.
[c04a19e]32\end{itemize}
[aa60460]33(Everything should be made as fair as possible, but not fairer. Chuck Einstein, Albert's younger brother)
[c04a19e]34
[05e33f5]35\subsection{Fairness Goals}
[aa60460]36For this work, fairness is considered to have two strongly related requirements: true starvation freedom and ``fast'' load balancing.
[05e33f5]37
[aa60460]38\paragraph{True starvation freedom} means as long as at least one \proc continues to dequeue \ats, all ready \ats should be able to run eventually (eventual progress).
39In any running system, a \proc can stop dequeuing \ats if it starts running a \at that never blocks.
40Without preemption, traditional work-stealing schedulers do not have starvation freedom in this case.
[05e33f5]41Now this requirement begs the question, what about preemption?
42Generally speaking preemption happens on the timescale of several milliseconds, which brings us to the next requirement: ``fast'' load balancing.
43
44\paragraph{Fast load balancing} means that load balancing should happen faster than preemption would normally allow.
[aa60460]45For interactive applications that need to run at 60, 90, 120 frames per second, \ats having to wait for several milliseconds to run are effectively starved.
[05e33f5]46Therefore load-balancing should be done at a faster pace, one that can detect starvation at the microsecond scale.
47With that said, this is a much fuzzier requirement since it depends on the number of \procs, the number of \ats and the general load of the system.
48
[6db62fa]49\subsection{Fairness vs Scheduler Locality} \label{fairnessvlocal}
[bace538]50An important performance factor in modern architectures is cache locality. Waiting for data at lower levels or not present in the cache can have a major impact on performance. Having multiple \glspl{hthrd} writing to the same cache lines also leads to cache lines that must be waited on. It is therefore preferable to divide data among each \gls{hthrd}\footnote{This partitioning can be an explicit division up front or using data structures where different \glspl{hthrd} are naturally routed to different cache lines.}.
[c04a19e]51
[aa60460]52For a scheduler, having good locality {\color{red}PAB: I think you should fold this footnote into the paragraph}\footnote{This section discusses \emph{internal locality}, \ie, the locality of the data used by the scheduler versus \emph{external locality}, \ie, how the data used by the application is affected by scheduling. External locality is a much more complicated subject and is discussed in the next section.}, \ie, having the data local to each \gls{hthrd}, generally conflicts with fairness. Indeed, good locality often requires avoiding the movement of cache lines, while fairness requires dynamically moving a \gls{thrd}, and as consequence cache lines, to a \gls{hthrd} that is currently available.
[c04a19e]53
[aa60460]54However, I claim that in practice it is possible to strike a balance between fairness and performance because these goals do not necessarily overlap temporally. Figure~\ref{fig:fair} shows a visual representation of this behaviour. As mentioned, some unfairness is acceptable; therefore it is desirable to have an algorithm that prioritizes cache locality as long as thread delay does not exceed the execution mental-model.
[c04a19e]55
56\begin{figure}
[bace538]57        \centering
58        \input{fairness.pstex_t}
59        \vspace*{-10pt}
[aa60460]60        \caption[Fairness vs Locality graph]{Rule of thumb Fairness vs Locality graph \smallskip\newline The importance of Fairness and Locality while a ready \gls{thrd} awaits running is shown as the time the ready \gls{thrd} waits increases, Ready Time, the chances that its data is still in cache decreases, Locality. At the same time, the need for fairness increases since other \glspl{thrd} may have the chance to run many times, breaking the fairness model. Since the actual values and curves of this graph can be highly variable, the graph is an idealized representation of the two opposing goals.}
[c04a19e]61        \label{fig:fair}
62\end{figure}
[86c1f1c3]63
[6db62fa]64\subsection{Performance Challenges}\label{pref:challenge}
65While there exists a multitude of potential scheduling algorithms, they generally always have to contend with the same performance challenges. Since these challenges are recurring themes in the design of a scheduler it is relevant to describe the central ones here before looking at the design.
66
67\subsubsection{Scalability}
68The most basic performance challenge of a scheduler is scalability.
69Given a large number of \procs and an even larger number of \ats, scalability measures how fast \procs can enqueue and dequeues \ats.
70One could expect that doubling the number of \procs would double the rate at which \ats are dequeued, but contention on the internal data structure of the scheduler can lead to worst improvements.
[aa60460]71While the ready-queue itself can be sharded to alleviate the main source of contention, auxiliary scheduling features, \eg counting ready \ats, can also be sources of contention.
[6db62fa]72
73\subsubsection{Migration Cost}
[aa60460]74Another important source of scheduling latency is migration.
75A \at migrates if it executes on two different \procs consecutively, which is the process discussed in \ref{fairnessvlocal}.
76Migrations can have many different causes, but in certain programs, it can be impossible to limit migration.
77Chapter~\ref{microbench} has a benchmark where any \at can potentially unblock any other \at, which can lead to \ats migrating frequently.
78Hence, it is important to design the internal data structures of the scheduler to limit any latency penalty from migrations.
[6db62fa]79
80
81\section{Inspirations}
[aa60460]82In general, a na\"{i}ve \glsxtrshort{fifo} ready-queue does not scale with increased parallelism from \glspl{hthrd}, resulting in decreased performance. The problem is a single point of contention when adding/removing \glspl{thrd}. As shown in the evaluation sections, most production schedulers do scale when adding \glspl{hthrd}. The solution to this problem is to shard the ready-queue: create multiple \emph{subqueues} forming the logical ready-queue and the subqueues are accessed by multiple \glspl{hthrd} without interfering.
[86c1f1c3]83
[aa60460]84Before going into the design of \CFA's scheduler, it is relevant to discuss two sharding solutions that served as the inspiration scheduler in this thesis.
[86c1f1c3]85
[729c991]86\subsection{Work-Stealing}
87
[aa60460]88As mentioned in \ref{existing:workstealing}, a popular pattern in work-stealing is sharding the ready-queue.
89In this pattern, each \gls{proc} has its own local ready-queue and \glspl{proc} only access each other's ready-queue if they run out of work on their local ready-queue.
90The interesting aspect of work stealing happens in the steady-state scheduling case, \ie all \glspl{proc} have work and no load balancing is needed.
91In this case, work stealing is close to optimal scheduling: it can achieve perfect locality and have no contention.
[729c991]92On the other hand, work-stealing schedulers only attempt to do load-balancing when a \gls{proc} runs out of work.
[6db62fa]93This means that the scheduler never balances unfair loads unless they result in a \gls{proc} running out of work.
[aa60460]94Chapter~\ref{microbench} shows that pathological cases work stealing can lead to indefinite starvation.
[729c991]95
[aa60460]96Based on these observation, the conclusion is that a \emph{perfect} scheduler should behave similar to work-stealing in the steady-state case, but load balance proactively when the need arises.
[729c991]97
[aa60460]98\subsection{Relaxed-FIFO}
[729c991]99
[aa60460]100A different scheduling approach is to create a ``relaxed-FIFO'' queue as in \todo{cite Trevor's paper}. This approach forgoes any ownership between \gls{proc} and ready-queue, and simply creates a pool of ready-queues from which \glspl{proc} pick.
101Scheduling is performed as follows:
102\begin{itemize}
103\item
104All ready queues are protected by TryLocks.
105\item
106Timestamps are added to each element of a ready queue.
107\item
108A \gls{proc} randomly tests ready queues until it has acquired two queues.
109\item
110The older of the two \ats at the front the acquired queues is dequeued.
111\end{itemize}
112The result is a queue that has both good scalability and sufficient fairness.
113The lack of ownership ensures that as long as one \gls{proc} is still able to repeatedly dequeue elements, it is unlikely any element will delay longer than any other element.
114This guarantee contrasts with work-stealing, where a \gls{proc} with a long ready queue results in unfairness for its \ats in comparison to a \gls{proc} with a short ready queue. This unfairness persists until a \gls{proc} runs out of work and steals.
[729c991]115
116An important aspects of this scheme's fairness approach is that the timestamps make it possible to evaluate how long elements have been on the queue.
[aa60460]117However, \glspl{proc} eagerly search for these older elements instead of focusing on specific queues, which affects locality.
[729c991]118
[aa60460]119While this scheme has good fairness, its performance suffers.
120It requires wide sharding, \eg at least 4 queues per \gls{hthrd}, and finding non-empty queues is difficult when there are few ready \ats.
[6db62fa]121
122\section{Relaxed-FIFO++}
[aa60460]123The inherent fairness and good performance with many \ats, makes the relaxed-FIFO queue a good candidate to form the basis of a new scheduler.
124The problem case is workloads where the number of \ats is barely greater than the number of \procs.
125In these situations, the wide sharding of the ready queue means most of its (relaxed) subqueues are empty.
126Furthermore, the non-empty subqueues are unlikely to hold more than one item.
127The consequence is that a random dequeue operation is likely to pick an empty subqueue, resulting in an unbounded number of selections.
128This state is generally unstable: each subqueue is likely to frequently toggle between being empty and nonempty.
129Indeed, when the number of \ats is \emph{equal} to the number of \procs, every pop operation is expected to empty a subqueue and every push is expected to add to an empty subqueue.
130In the worst case, a check of the subqueues sees all are empty or full.
[6db62fa]131
132As this is the most obvious challenge, it is worth addressing first.
[aa60460]133The obvious solution is to supplement each sharded subqueue with data that indicates if the queue is empty/nonempty to simplify finding nonempty queues, \ie ready \glspl{at}.
134This sharded data can be organized in different forms, \eg a bitmask or a binary tree that tracks the nonempty subqueues.
135Specifically, many modern architectures have powerful bitmask manipulation instructions or searching a binary tree has good Big-O complexity.
136However, precisely tracking nonempty subqueues is problematic.
137The reason is that the subqueues are initially sharded with a width presumably chosen to avoid contention.
138However, tracking which ready queue is nonempty is only useful if the tracking data is dense, \ie denser than the sharded subqueues.
139Otherwise, it does not provide useful information because reading this new data structure risks being as costly as simply picking a subqueue at random.
140But if the tracking mechanism \emph{is} denser than the shared subqueues, than constant updates invariably create a new source of contention.
141Early experiments with this approach showed that randomly picking, even with low success rates, is often faster than bit manipulations or tree walks.
[6db62fa]142
143The exception to this rule is using local tracking.
[aa60460]144If each \proc locally keeps track of empty subqueues, than this can be done with a very dense data structure without introducing a new source of contention.
145However, the consequence of local tracking is that the information is incomplete.
146Each \proc is only aware of the last state it saw about each subqueue so this information quickly becomes stale.
147Even on systems with low \gls{hthrd} count, \eg 4 or 8, this approach can quickly lead to the local information being no better than the random pick.
148This result is due in part to the cost of maintaining information and its poor quality.
[6db62fa]149
[aa60460]150However, using a very low cost but inaccurate approach for local tracking can actually be beneficial.
151If the local tracking is no more costly than a random pick, than \emph{any} improvement to the success rate, however low it is, leads to a performance benefits.
152This suggests to the following approach:
[6db62fa]153
154\subsection{Dynamic Entropy}\cit{https://xkcd.com/2318/}
[aa60460]155The Relaxed-FIFO approach can be made to handle the case of mostly empty subqueues by tweaking the \glsxtrlong{prng} (PRNG).
156The \glsxtrshort{prng} state can be seen as containing a list of all the future subqueues that will be accessed.
157While this concept is not particularly useful on its own, the consequence is that if the \glsxtrshort{prng} algorithm can be run \emph{backwards}, then the state also contains a list of all the subqueues that were accessed.
158Luckily, bidirectional \glsxtrshort{prng} algorithms do exist, \eg some Linear Congruential Generators\cit{https://en.wikipedia.org/wiki/Linear\_congruential\_generator} support running the algorithm backwards while offering good quality and performance.
[6db62fa]159This particular \glsxtrshort{prng} can be used as follows:
[aa60460]160\begin{itemize}
161\item
162Each \proc maintains two \glsxtrshort{prng} states, refereed to as $F$ and $B$.
163\item
164When a \proc attempts to dequeue a \at, it picks a subqueue by running $B$ backwards.
165\item
166When a \proc attempts to enqueue a \at, it runs $F$ forward picking a subqueue to enqueue to.
167If the enqueue is successful, the state $B$ is overwritten with the content of $F$.
168\end{itemize}
169The result is that each \proc tends to dequeue \ats that it has itself enqueued.
170When most subqueues are empty, this technique increases the odds of finding \ats at very low cost, while also offering an improvement on locality in many cases.
[6db62fa]171
[aa60460]172Tests showed this approach performs better than relaxed-FIFO in many cases.
173However, it is still not competitive with work-stealing algorithms.
[6db62fa]174The fundamental problem is that the constant randomness limits how much locality the scheduler offers.
[aa60460]175This becomes problematic both because the scheduler is likely to get cache misses on internal data-structures and because migrations become frequent.
176Therefore, the attempt to modify the relaxed-FIFO algorithm to behave more like work stealing did not pan out.
177The alternative is to do it the other way around.
[6db62fa]178
179\section{Work Stealing++}
[aa60460]180To add stronger fairness guarantees to work stealing a few changes are needed.
[6db62fa]181First, the relaxed-FIFO algorithm has fundamentally better fairness because each \proc always monitors all subqueues.
[aa60460]182Therefore, the work-stealing algorithm must be prepended with some monitoring.
183Before attempting to dequeue from a \proc's subqueue, the \proc must make some effort to ensure other subqueues are not being neglected.
184To make this possible, \procs must be able to determine which \at has been on the ready queue the longest.
185Second, the relaxed-FIFO approach needs timestamps for each \at to make this possible.
[729c991]186
[86c1f1c3]187\begin{figure}
[bace538]188        \centering
189        \input{base.pstex_t}
[aa60460]190        \caption[Base \CFA design]{Base \CFA design \smallskip\newline A pool of subqueues offers the sharding, two per \glspl{proc}. Each \gls{proc} can access all of the subqueues. Each \at is timestamped when enqueued.}
[86c1f1c3]191        \label{fig:base}
192\end{figure}
193
[aa60460]194Figure~\ref{fig:base} shows the algorithm structure.
195This structure is similar to classic work-stealing except the subqueues are placed in an array so \procs can access them in constant time.
196Sharding width can be adjusted based on contention.
197Note, as an optimization, the TS of a \at is store in the \at in front of it, so the first TS is in the array and the last \at has no TS.
198This organization keeps the highly accessed front TSs close together in the array.
199When a \proc attempts to dequeue a \at, it first picks a random remote subqueue and compares its timestamp to the timestamps of its local subqueue(s).
200The oldest waiting \at (possibly within some range) is dequeued to provide global fairness.
201
202However, this na\"ive implemented has performance problems.
[6db62fa]203First, it is necessary to have some damping effect on helping.
[aa60460]204Random effects like cache misses and preemption can add spurious but short bursts of latency negating the attempt to help.
205These bursts are caused by increased migrations and make this work stealing approach slowdown to the level of relaxed-FIFO.
[86c1f1c3]206
[6db62fa]207\begin{figure}
208        \centering
209        \input{base_avg.pstex_t}
210        \caption[\CFA design with Moving Average]{\CFA design with Moving Average \smallskip\newline A moving average is added to each subqueue.}
211        \label{fig:base-ma}
212\end{figure}
[b9537e6]213
[aa60460]214A simple solution to this problem is to use an exponential moving average\cit{https://en.wikipedia.org/wiki/Moving\_average\#Exponential\_moving\_average} (MA) instead of a raw timestamps, shown in Figure~\ref{fig:base-ma}.
215Note, this is more complex because the \at at the head of a subqueue is still waiting, so its wait time has not ended.
216Therefore, the exponential moving average is actually an exponential moving average of how long each dequeued \at has waited.
217To compare subqueues, the timestamp at the head must be compared to the current time, yielding the best-case wait-time for the \at at the head of the queue.
[6db62fa]218This new waiting is averaged with the stored average.
[aa60460]219To further limit migration, a bias can be added to a local subqueue, where a remote subqueue is helped only if its moving average is more than $X$ times the local subqueue's average.
220Tests for this approach indicate the choice of the weight for the moving average or the bias is not important, \ie weights and biases of similar \emph{magnitudes} have similar effects.
221
222With these additions to work stealing, scheduling can be made as fair as the relaxed-FIFO approach, avoiding the majority of unnecessary migrations.
223Unfortunately, the work to achieve fairness has a performance cost, especially when the workload is inherently fair, and hence, there is only short-term or no starvation.
224The problem is that the constant polling (reading) of remote subqueues generally entail a cache miss because the TSs are constantly being updated (written).
225To make things worst, remote subqueues that are very active, \ie \ats are frequently enqueued and dequeued from them, the higher the chances are that polling will incur a cache-miss.
226Conversely, the active subqueues do not benefit much from helping since starvation is already a non-issue.
227This puts this algorithm in the awkward situation of paying for a cost that is largely unnecessary.
[6db62fa]228The good news is that this problem can be mitigated
229
230\subsection{Redundant Timestamps}
[aa60460]231The problem with polling remote subqueues is that correctness is critical.
232There must be a consensus among \procs on which subqueues hold which \ats, as the \ats are in constant motion.
233Furthermore, since timestamps are use for fairness, it is critical to have consensus on which \at is the oldest.
234However, when deciding if a remote subqueue is worth polling, correctness is less of a problem.
235Since the only requirement is that a subqueue is eventually polled, some data staleness is acceptable.
236This leads to a situation where stale timestamps are only problematic in some cases.
237Furthermore, stale timestamps can be desirable since lower freshness requirements mean less cache invalidations.
238
239Figure~\ref{fig:base-ts2} shows a solution with a second array containing a copy of the timestamps and average.
240This copy is updated \emph{after} the subqueue's critical sections using relaxed atomics.
241\Glspl{proc} now check if polling is needed by comparing the copy of the remote timestamp instead of the actual timestamp.
242The result is that since there is no fencing, the writes can be buffered in the hardware and cause fewer cache invalidations.
[729c991]243
[6db62fa]244\begin{figure}
245        \centering
[aa60460]246        \input{base_ts2.pstex_t}
247        \caption[\CFA design with Redundant Timestamps]{\CFA design with Redundant Timestamps \smallskip\newline An array is added containing a copy of the timestamps. These timestamps are written to with relaxed atomics, so there is no order among concurrent memory accesses, leading to fewer cache invalidations.}
[6db62fa]248        \label{fig:base-ts2}
249\end{figure}
[729c991]250
[aa60460]251The correctness argument is somewhat subtle.
[6db62fa]252The data used for deciding whether or not to poll a queue can be stale as long as it does not cause starvation.
[aa60460]253Therefore, it is acceptable if stale data makes queues appear older than they really are but not fresher.
254For the timestamps, this means missing writes to the timestamp is acceptable since they make the head \at look older.
255For the moving average, as long as the operations are just atomic reads/writes, the average is guaranteed to yield a value that is between the oldest and newest values written.
256Therefore, this unprotected read of the timestamp and average satisfy the limited correctness that is required.
257
258With redundant timestamps, this scheduling algorithm achieves both the fairness and performance requirements on most machines.
259The problem is that the cost of polling and helping is not necessarily consistent across each \gls{hthrd}.
260For example, on machines with a CPU containing multiple hyperthreads and cores and multiple CPU sockets, cache misses can be satisfied from the caches on same (local) CPU, or by a CPU on a different (remote) socket.
261Cache misses satisfied by a remote CPU have significantly higher latency than from the local CPU.
262However, these delays are not specific to systems with multiple CPUs.
263Depending on the cache structure, cache misses can have different latency on the same CPU, \eg the AMD EPYC 7662 CPUs used in Chapter~\ref{microbench}.
[729c991]264
[13888c0]265\begin{figure}
266        \centering
267        \input{cache-share.pstex_t}
[aa60460]268        \caption[CPU design with wide L3 sharing]{CPU design with wide L3 sharing \smallskip\newline A CPU with 4 cores, where caches L1 and L2 are private to each core, and the L3 cache is shared across all cores.}
[13888c0]269        \label{fig:cache-share}
270
[aa60460]271        \vspace{25pt}
272
[13888c0]273        \input{cache-noshare.pstex_t}
[aa60460]274        \caption[CPU design with a narrower L3 sharing]{CPU design with a narrow L3 sharing \smallskip\newline A CPU with 4 cores, where caches L1 and L2 are private to each core, and the L3 cache is shared across a pair of cores.}
[13888c0]275        \label{fig:cache-noshare}
276\end{figure}
277
[aa60460]278Figures~\ref{fig:cache-share} and~\ref{fig:cache-noshare} show two different cache topologies that highlight this difference.
279In Figure~\ref{fig:cache-share}, all cache misses are either private to a CPU or shared with another CPU.
280This means latency due to cache misses is fairly consistent.
281In contrast, in Figure~\ref{fig:cache-noshare} misses in the L2 cache can be satisfied by either instance of L3 cache.
282However, the memory-access latency to the remote L3 is higher than the memory-access latency to the local L3.
283The impact of these different designs on this algorithm is that scheduling only scales well on architectures with a wide L3 cache, similar to Figure~\ref{fig:cache-share}, and less well on architectures with many narrower L3 cache instances, similar to Figure~\ref{fig:cache-noshare}.
284Hence, as the number of L3 instances grow, so too does the chance that the random helping causes significant cache latency.
285The solution is for the scheduler be aware of the cache topology.
[13888c0]286
[6db62fa]287\subsection{Per CPU Sharding}
[aa60460]288
289Building a scheduler that is cache aware poses two main challenges: discovering the cache topology and matching \procs to this cache structure.
290Unfortunately, there is no portable way to discover cache topology, and it is outside the scope of this thesis to solve this problem.
291This work uses the cache topology information from Linux's \texttt{/sys/devices/system/cpu} directory.
292This leaves the challenge of matching \procs to cache structure, or more precisely identifying which subqueues of the ready queue are local to which subcomponents of the cache structure.
293Once a matching is generated, the helping algorithm is changed to add bias so that \procs more often help subqueues local to the same cache substructure.\footnote{
294Note that like other biases mentioned in this section, the actual bias value does not appear to need precise tuning.}
295
296The simplest approach for mapping subqueues to cache structure is to statically tie subqueues to CPUs.
297Instead of having each subqueue local to a specific \proc (kernel thread), the system is initialized with subqueues for each hardware hyperthread/core up front.
298Then \procs dequeue and enqueue by first asking which CPU id they are executing on, in order to identify which subqueues are the local ones.
[13888c0]299\Glspl{proc} can get the CPU id from \texttt{sched\_getcpu} or \texttt{librseq}.
300
[aa60460]301This approach solves the performance problems on systems with topologies with narrow L3 caches, similar to Figure \ref{fig:cache-noshare}.
302However, it can still cause some subtle fairness problems in systems with few \procs and many \glspl{hthrd}.
303In this case, the large number of subqueues and the bias against subqueues tied to different cache substructures make it unlikely that every subqueue is picked.
304To make things worst, the small number of \procs mean that few helping attempts are made.
305This combination of low selection and few helping attempts allow a \at to become stranded on a subqueue for a long time until it gets randomly helped.
[13888c0]306On a system with 2 \procs, 256 \glspl{hthrd} with narrow cache sharing, and a 100:1 bias, it can actually take multiple seconds for a \at to get dequeued from a remote queue.
307Therefore, a more dynamic matching of subqueues to cache instance is needed.
[729c991]308
[6db62fa]309\subsection{Topological Work Stealing}
[aa60460]310
311The approach used in the \CFA scheduler is to have per-\proc subqueues, but have an explicit data-structure track which cache substructure each subqueue is tied to.
312This tracking requires some finesse because reading this data structure must lead to fewer cache misses than not having the data structure in the first place.
[13888c0]313A key element however is that, like the timestamps for helping, reading the cache instance mapping only needs to give the correct result \emph{often enough}.
[aa60460]314Therefore the algorithm can be built as follows: before enqueueing or dequeuing a \at, each \proc queries the CPU id and the corresponding cache instance.
[13888c0]315Since subqueues are tied to \procs, each \proc can then update the cache instance mapped to the local subqueue(s).
316To avoid unnecessary cache line invalidation, the map is only written to if the mapping changes.
[b9537e6]317
Note: See TracBrowser for help on using the repository browser.