source: doc/theses/thierry_delisle_PhD/comp_II/comp_II.tex @ 0254454

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 0254454 was 61b1447, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Fixed small typing issues in comp-II

  • Property mode set to 100644
File size: 40.6 KB
Line 
1\documentclass[11pt]{article}
2\usepackage{fullpage}
3\usepackage[T1]{fontenc}
4\usepackage[utf8]{inputenc}
5\usepackage{xspace}
6\usepackage{xcolor}
7\usepackage{graphicx}
8\usepackage{epic,eepic}
9\usepackage{listings}                   % for code listings
10\usepackage{glossaries}
11\usepackage{textcomp}
12% cfa macros used in the document
13\input{common}
14
15\setlist{topsep=6pt,parsep=0pt}         % global reduce spacing between points
16\newcommand{\uC}{$\mu$\CC}
17\usepackage[hidelinks]{hyperref}
18\setlength{\abovecaptionskip}{5pt plus 3pt minus 2pt}
19\lstMakeShortInline$%                   % single-character for \lstinline
20%\usepackage[margin=1in]{geometry}
21%\usepackage{float}
22
23\input{glossary}
24
25\CFAStyle                               % use default CFA format-style
26
27\title{
28        \Huge \vspace*{1in} The \CFA Scheduler\\
29        \huge \vspace*{0.25in} PhD Comprehensive II Research Proposal
30        \vspace*{1in}
31}
32
33\author{
34        \huge Thierry Delisle \vspace*{5pt} \\
35        \Large \texttt{tdelisle@uwaterloo.ca} \vspace*{5pt} \\
36        \Large Cheriton School of Computer Science \\
37        \Large University of Waterloo
38}
39
40\date{
41        \today
42}
43
44\begin{document}
45\maketitle
46\thispagestyle{empty}
47\cleardoublepage
48
49\newcommand{\cit}{\textsuperscript{[Citation Needed]}\xspace}
50\newcommand{\TODO}{{\large\bf\color{red} TODO: }\xspace}
51
52% ===============================================================================
53% ===============================================================================
54
55\tableofcontents
56
57% ===============================================================================
58% ===============================================================================
59\newpage
60\section{Introduction}
61\subsection{\CFA and the \CFA concurrency package}
62\CFA\cite{Moss18} is a modern, polymorphic, non-object-oriented, concurrent, backwards-compatible extension of the C programming language.
63It aims to add high-productivity features while maintaining the predictable performance of C.
64As such, concurrency in \CFA\cite{Delisle19} aims to offer simple and safe high-level tools while still allowing performant code.
65\CFA concurrent code is written in the synchronous programming paradigm but uses \glspl{uthrd} in order to achieve the simplicity and maintainability of synchronous programming without sacrificing the efficiency of asynchronous programing.
66As such, the \CFA \newterm{scheduler} is a preemptive user-level scheduler that maps \glspl{uthrd} onto \glspl{kthrd}.
67
68\newterm{Scheduling} occurs when execution switches from one thread to another, where the second thread is implicitly chosen by the scheduler.
69This scheduling is an indirect handoff, as opposed to generators and coroutines which explicitly switch to the next generator and coroutine respectively.
70The cost of switching between two threads for an indirect handoff has two components:
71\begin{enumerate}
72\item
73the cost of actually context-switching, \ie changing the relevant registers to move execution from one thread to the other,
74\item
75and the cost of scheduling, \ie deciding which thread to run next among all the threads ready to run.
76\end{enumerate}
77The first cost is generally constant and fixed\footnote{Affecting the constant context-switch cost is whether it is done in one step, after the scheduling, or in two steps, context-switching to a fixed third-thread before scheduling.}, while the scheduling cost can vary based on the system state.
78Adding multiple \glspl{kthrd} does not fundamentally change the scheduler semantics or requirements, it simply adds new correctness requirements, \ie \newterm{linearizability}\footnote{Meaning however fast the CPU threads run, there is an equivalent sequential order that gives the same result.}, and a new dimension to performance: scalability, where scheduling cost now also depends on contention.
79
80The more threads switch, the more the administration cost of scheduling becomes noticeable.
81It is therefore important to build a scheduler with the lowest possible cost and latency.
82Another important consideration is \newterm{fairness}.
83In principle, scheduling should give the illusion of perfect fairness, where all threads ready to run are running \emph{simultaneously}.
84While the illusion of simultaneity is easier to reason about, it can break down if the scheduler allows too much unfairness.
85Therefore, the scheduler should offer as much fairness as needed to guarantee eventual progress, but use unfairness to help performance.
86In practice, threads must wait in turn but there can be advantages to unfair scheduling, similar to the the express cash-register at a grocery store.
87
88The goal of this research is to produce a scheduler that is simple for programmers to understand and offers good performance.
89Here understandability does not refer to the API but to how much scheduling concerns programmers need to take into account when writing a \CFA concurrent package.
90Therefore, the main goal of this proposal is :
91\begin{quote}
92The \CFA scheduler should be \emph{viable} for \emph{any} workload.
93\end{quote}
94
95For a general purpose scheduler, it is impossible to produce an optimal algorithm as it would require knowledge of the future behaviour of threads.
96As such, scheduling performance is generally either defined by the best case scenario, \ie a workload to which the scheduler is tailored, or the worst case scenario, \ie the scheduler behaves no worst than \emph{X}.
97For this proposal, the performance is evaluated using the second approach to allow \CFA programmers to rely on scheduling performance.
98Because there is no optimal scheduler, ultimately \CFA may allow programmers to write their own scheduler; but that is not the subject of this proposal, which considers only the default scheduler.
99As such, it is important that only programmers with exceptionally high performance requirements should need to write their own scheduler and replace the scheduler in this proposal.
100
101To achieve the \CFA scheduling goal includes:
102\begin{enumerate}
103\item
104producing a scheduling strategy with sufficient fairness guarantees,
105\item
106creating an abstraction layer over the operating system to handle kernel-threads spinning unnecessarily,
107\item
108scheduling blocking I/O operations,
109\item
110and writing sufficient library tools to allow developers to indirectly use the scheduler, either through tuning knobs or replacing the default scheduler.
111\end{enumerate}
112
113% ===============================================================================
114% ===============================================================================
115
116\section{\CFA Scheduling}
117To schedule user-level threads across all workloads, the scheduler has a number of requirements:
118
119\paragraph{Correctness} As with any other concurrent data structure or algorithm, the correctness requirement is paramount.
120The scheduler cannot allow threads to be dropped from the ready queue, \ie scheduled but never run, or be executed multiple times when only being scheduled once.
121Since \CFA concurrency has no spurious wakeup, this definition of correctness also means the scheduler should have no spurious wakeup.
122The \CFA scheduler must be correct.
123
124\paragraph{Performance} The performance of a scheduler can generally be measured in terms of scheduling cost, scalability and latency.
125\newterm{Scheduling cost} is the cost to switch from one thread to another, as mentioned above.
126For simple applications, where a single kernel thread does most of the scheduling, it is generally the dominating cost.
127\newterm{Scalability} is the cost of adding multiple kernel threads because it increases the time for context switching because of contention by multiple threads accessing shared resources, \eg the ready queue.
128Finally, \newterm{tail latency} is service delay and relates to thread fairness.
129Specifically, latency measures how long a thread waits to run once scheduled and is evaluated in the worst case.
130The \CFA scheduler should offer good performance for all three metrics.
131
132\paragraph{Fairness} Like performance, this requirement has several aspect : eventual progress, predictability and performance reliability.
133\newterm{Eventual progress} guarantees every scheduled thread is eventually run, \ie prevent starvation.
134As a hard requirement, the \CFA scheduler must guarantee eventual progress, otherwise the above mentioned illusion of simultaneous execution is broken and the scheduler becomes much more complex to reason about.
135\newterm{Predictability} and \newterm{reliability} means similar workloads achieve similar performance and programmer execution intuition is respected.
136For example, a thread that yields aggressively should not run more often then other tasks.
137While this is intuitive, it does not hold true for many work-stealing or feedback based schedulers.
138The \CFA scheduler must guarantee eventual progress and should be predictable and offer reliable performance.
139
140\paragraph{Efficiency} Finally, efficient usage of CPU resources is also an important requirement and is discussed in depth towards the end of the proposal.
141\newterm{Efficiency} means avoiding using CPU cycles when there are no threads to run, and conversely, use all CPUs available when the workload can benefit from it.
142Balancing these two states is where the complexity lies.
143The \CFA scheduler should be efficient with respect to the underlying (shared) computer.
144
145\bigskip To achieve these requirements, I can reject two broad types of scheduling strategies : feedback-based and priority schedulers.
146
147\subsection{Feedback-Based Schedulers}
148Many operating systems use schedulers based on feedback in some form, \eg measuring how much CPU a particular thread has used\footnote{Different metrics can be measured but it is not relevant to the discussion.} and schedule threads based on this metric.
149These strategies are sensible for operating systems but rely on two assumptions for the workload:
150
151\begin{enumerate}
152        \item Threads live long enough for useful feedback information to be to gathered.
153        \item Threads belong to multiple users so fairness across threads is insufficient.
154\end{enumerate}
155
156While these two assumptions generally hold for operating systems, they may not for user-level threading.
157Since \CFA has the explicit goal of allowing many smaller threads, this can naturally lead to threads with much shorter lifetimes that are only scheduled a few times.
158Scheduling strategies based on feedback cannot be effective in these cases because there is no opportunity to measure the metrics that underlie the algorithm.
159Note, the problem of \newterm{feedback convergence} (reacting too slowly to scheduling events) is not specific to short lived threads but can also occur with threads that show drastic changes in scheduling, \eg threads running for long periods of time and then suddenly blocking and unblocking quickly and repeatedly.
160
161In the context of operating systems, these concerns can be overshadowed by a more pressing concern : security.
162When multiple users are involved, it is possible some users are malevolent and try to exploit the scheduling strategy to achieve some nefarious objective.
163Security concerns mean more precise and robust fairness metrics must be used to guarantee fairness across processes created by users as well as threads created within a process.
164In the case of the \CFA scheduler, every thread runs in the same user space and is controlled by the same user.
165Fairness across users is therefore a given and it is then possible to safely ignore the possibility that threads are malevolent.
166This approach allows for a much simpler fairness metric and in this proposal \emph{fairness} is defined as: when multiple threads are cycling through the system, the total ordering of threads being scheduled, \ie pushed onto the ready-queue, should not differ much from the total ordering of threads being executed, \ie popped from the ready-queue.
167
168Since feedback is not necessarily feasible within the lifetime of all threads and a simple fairness metric can be used, the scheduling strategy proposed for the \CFA runtime does not use per-threads feedback.
169Feedback in general is not rejected for secondary concerns like idle sleep for kernel threads, but no feedback is used to decide which thread to run next.
170
171\subsection{Priority Schedulers}
172Another broad category of schedulers are priority schedulers.
173In these scheduling strategies, threads have priorities and the runtime schedules the threads with the highest priority before scheduling other threads.
174Threads with equal priority are scheduled using a secondary strategy, often something simple like round-robin or FIFO.
175A consequence of priority is that, as long as there is a thread with a higher priority that desires to run, a thread with a lower priority does not run.
176This possible starving of threads can dramatically increase programming complexity since starving threads and priority inversion (prioritizing a lower priority thread) can both lead to serious problems.
177
178An important observation is that threads do not need to have explicit priorities for problems to occur.
179Indeed, any system with multiple ready-queues that attempts to exhaust one queue before accessing the other queues, essentially provide implicit priority, which can encounter starvation problems.
180For example, a popular scheduling strategy that suffers from implicit priorities is work stealing.
181\newterm{Work stealing} is generally presented as follows:
182\begin{enumerate}
183        \item Each processor has a list of ready threads.
184        \item Each processor runs threads from its ready queue first.
185        \item If a processor's ready queue is empty, attempt to run threads from some other processor's ready queue.
186\end{enumerate}
187
188In a loaded system\footnote{A \newterm{loaded system} is a system where threads are being run at the same rate they are scheduled.}, if a thread does not yield, block, or preempt for an extended period of time, threads on the same processor's list starve if no other processors exhaust their list.
189
190Since priorities can be complex for programmers to incorporate into their execution intuition, the scheduling strategy proposed for the \CFA runtime does not use a strategy with either implicit or explicit thread priorities.
191
192\subsection{Schedulers without feedback or priorities}
193This proposal conjectures that is is possible to construct a default scheduler for the \CFA runtime that offers good scalability and a simple fairness guarantee that is easy for programmers to reason about.
194The simplest fairness guarantee is FIFO ordering, \ie threads scheduled first run first.
195However, enforcing FIFO ordering generally conflicts with scalability across multiple processors because of the additional synchronization.
196Thankfully, strict FIFO is not needed for sufficient fairness.
197Since concurrency is inherently non-deterministic, fairness concerns in scheduling are only a problem if a thread repeatedly runs before another thread can run.
198Some relaxation is possible because non-determinism means programmers already handle ordering problems to produce correct code and hence rely on weak guarantees, \eg that a specific thread will \emph{eventually} run.
199Since some reordering does not break correctness, the FIFO fairness guarantee can be significantly relaxed without causing problems.
200For this proposal, the target guarantee is that the \CFA scheduler provides \emph{probable} FIFO ordering, which allows reordering but makes it improbable that threads are reordered far from their position in total ordering.
201
202The \CFA scheduler fairness is defined as follows:
203\begin{itemize}
204        \item Given two threads $X$ and $Y$, the odds that thread $X$ runs $N$ times \emph{after} thread $Y$ is scheduled but \emph{before} it is run, decreases exponentially with regard to $N$.
205\end{itemize}
206While this is not a bounded guarantee, the probability that unfairness persist for long periods of times decreases exponentially, making persisting unfairness virtually impossible.
207
208% ===============================================================================
209% ===============================================================================
210\section{Proposal Details}
211
212\subsection{Central Ready Queue} \label{sec:queue}
213A central ready queue can be built from a FIFO queue, where user threads are pushed onto the queue when they are ready to run, and processors (kernel-threads acting as virtual processors) pop the user threads from the queue and execute them.
214Alistarh \etal~\cite{alistarh2018relaxed} show it is straightforward to build a relaxed FIFO list that is fast and scalable for loaded or overloaded systems.
215The described queue uses an array of underlying strictly FIFO queues as shown in Figure~\ref{fig:base}\footnote{For this section, the number of underlying queues is assumed to be constant.
216Section~\ref{sec:resize} discusses resizing the array.}.
217Pushing new data is done by selecting one of these underlying queues at random, recording a timestamp for the operation and pushing to the selected queue.
218Popping is done by selecting two queues at random and popping from the queue with the oldest timestamp.
219A higher number of underlying queues leads to less contention on each queue and therefore better performance.
220In a loaded system, it is highly likely the queues are non-empty, \ie several tasks are on each of the underlying queues.
221This means that selecting a queue at random to pop from is highly likely to yield a queue with available items.
222In Figure~\ref{fig:base}, ignoring the ellipsis, the chances of getting an empty queue is 2/7 per pick, meaning two random picks yield an item approximately 9 times out of 10.
223
224\begin{figure}
225        \begin{center}
226                \input{base}
227        \end{center}
228        \caption{Relaxed FIFO list at the base of the scheduler: an array of strictly FIFO lists.
229The timestamp is in all nodes and cell arrays.}
230        \label{fig:base}
231\end{figure}
232
233\begin{figure}
234        \begin{center}
235                \input{empty}
236        \end{center}
237        \caption{``More empty'' state of the queue: the array contains many empty cells.}
238        \label{fig:empty}
239\end{figure}
240
241When the ready queue is \emph{more empty}, \ie several of the queues are empty, selecting a random queue for popping is less likely to yield a successful selection and more attempts are needed, resulting in a performance degradation.
242Figure~\ref{fig:empty} shows an example with fewer elements, where the chances of getting an empty queue is 5/7 per pick, meaning two random picks yield an item only half the time.
243Since the ready queue is not empty, the pop operation \emph{must} find an element before returning and therefore must retry.
244Note, the popping kernel thread has no work to do, but CPU cycles are wasted both for available user and kernel threads during the pop operation as the popping thread is using a CPU.
245Overall performance is therefore influenced by the contention on the underlying queues and pop performance is influenced by the item density.
246
247This leads to four performance cases for the centralized ready-queue, as depicted in Table~\ref{tab:perfcases}.
248The number of processors (many or few) refers to the number of kernel threads \emph{actively} attempting to pop user threads from the queues, not the total number of kernel threads.
249The number of threads (many or few) refers to the number of user threads ready to be run.
250Many threads means they outnumber processors significantly and most underlying queues have items, few threads mean there are barely more threads than processors and most underlying queues are empty.
251Cases with fewer threads than processors are discussed in Section~\ref{sec:sleep}.
252
253\begin{table}
254        \begin{center}
255                \begin{tabular}{|r|l|l|}
256                        \cline{2-3}
257                        \multicolumn{1}{r|}{} & \multicolumn{1}{c|}{Many Processors} & \multicolumn{1}{c|}{Few Processors} \\
258                        \hline
259                        Many Threads & A: good performance & B: good performance \\
260                        \hline
261                        Few Threads  & C: worst performance & D: poor performance \\
262                        \hline
263                \end{tabular}
264        \end{center}
265        \caption{Expected performance of the relaxed FIFO list in different cases.}
266        \label{tab:perfcases}
267\end{table}
268
269Performance can be improved in case~D (Table~\ref{tab:perfcases}) by adding information to help processors find which inner queues are used.
270This addition aims to avoid the cost of retrying the pop operation but does not affect contention on the underlying queues and can incur some management cost for both push and pop operations.
271The approach used to encode this information can vary in density and be either global or local.
272\newterm{Density} means the information is either packed in a few cachelines or spread across several cachelines, and \newterm{local information} means each thread uses an independent copy instead of a single global, \ie common, source of information.
273
274For example, Figure~\ref{fig:emptybit} shows a dense bitmask to identify which inner queues are currently in use.
275This approach means processors can often find user threads in constant time, regardless of how many underlying queues are empty.
276Furthermore, modern x86 CPUs have extended bit manipulation instructions (BMI2) that allow using the bitmask with very little overhead compared to the randomized selection approach for a filled ready queue, offering good performance even in cases with many empty inner queues.
277However, this technique has its limits: with a single word\footnote{Word refers here to however many bits can be written atomically.} bitmask, the total number of underlying queues in the ready queue is limited to the number of bits in the word.
278With a multi-word bitmask, this maximum limit can be increased arbitrarily, but it is not possible to check if the queue is empty by reading the bitmask atomically.
279
280Finally, a dense bitmap, either single or multi-word, causes additional problems in case C (Table 1), because many processors are continuously scanning the bitmask to find the few available threads.
281This increased contention on the bitmask(s) reduces performance because of cache misses after updates and the bitmask is updated more frequently by the scanning processors racing to read and/or update that information.
282This increased update frequency means the information in the bitmask is more often stale before a processor can use it to find an item, \ie mask read says there are available user threads but none on queue.
283
284\begin{figure}
285        \begin{center}
286                {\resizebox{0.8\textwidth}{!}{\input{emptybit}}}
287        \end{center}
288        \caption{``More empty'' queue with added bitmask to indicate which array cells have items.}
289        \label{fig:emptybit}
290\end{figure}
291
292Figure~\ref{fig:emptytree} shows another approach using a hierarchical tree data-structure to reduce contention and has been shown to work in similar cases~\cite{ellen2007snzi}\footnote{This particular paper seems to be patented in the US.
293How does that affect \CFA? Can I use it in my work?}.
294However, this approach may lead to poorer performance in case~B (Table~\ref{tab:perfcases}) due to the inherent pointer chasing cost and already low contention cost in that case.
295
296\begin{figure}
297        \begin{center}
298                {\resizebox{0.8\textwidth}{!}{\input{emptytree}}}
299        \end{center}
300        \caption{``More empty'' queue with added binary search tree indicate which array cells have items.}
301        \label{fig:emptytree}
302\end{figure}
303
304Finally, a third approach is to use dense information, similar to the bitmap, but have each thread keep its own independent copy of it.
305While this approach can offer good scalability \emph{and} low latency, the liveliness of the information can become a problem.
306In the simple cases, local copies of which underlying queues are empty can become stale and end-up not being useful for the pop operation.
307A more serious problem is that reliable information is necessary for some parts of this algorithm to be correct.
308As mentioned in this section, processors must know \emph{reliably} whether the list is empty or not to decide if they can return \texttt{NULL} or if they must keep looking during a pop operation.
309Section~\ref{sec:sleep} discusses another case where reliable information is required for the algorithm to be correct.
310
311\begin{figure}
312        \begin{center}
313                \input{emptytls}
314        \end{center}
315        \caption{``More empty'' queue with added per processor bitmask to indicate which array cells have items.}
316        \label{fig:emptytls}
317\end{figure}
318
319There is a fundamental tradeoff among these approach.
320Dense global information about empty underlying queues helps zero-contention cases at the cost of high-contention case.
321Sparse global information helps high-contention cases but increases latency in zero-contention-cases, to read and ``aggregate'' the information\footnote{Hierarchical structures, \eg binary search tree, effectively aggregate information but follow pointer chains, learning information at each node.
322Similarly, other sparse schemes need to read multiple cachelines to acquire all the information needed.}.
323Finally, dense local information has both the advantages of low latency in zero-contention cases and scalability in high-contention cases, however the information can become stale making it difficult to use to ensure correctness.
324The fact that these solutions have these fundamental limits suggest to me a better solution that attempts to combine these properties in an interesting ways.
325Also, the lock discussed in Section~\ref{sec:resize} allows for solutions that adapt to the number of processors, which could also prove useful.
326
327\paragraph{Objectives and Existing Work}
328
329How much scalability is actually needed is highly debatable.
330\emph{libfibre}\cite{libfibre} has compared favorably to other schedulers in webserver tests\cite{Karsten20} and uses a single atomic counter in its scheduling algorithm similarly to the proposed bitmask.
331As such, the single atomic instruction on a shared cacheline may be sufficiently performant.
332
333I have built a prototype of this ready queue in the shape of a data queue, \ie nodes on the queue are structures with a single int representing a thread and intrusive data fields.
334Using this prototype I ran preliminary performance experiments that confirm the expected performance in Table~\ref{tab:perfcases}.
335However, these experiments only offer a hint at the actual performance of the scheduler since threads form more complex operations than simple integer nodes, \eg threads are not independent of each other, when a thread blocks some other thread must intervene to wake it.
336
337I have also integrated this prototype into the \CFA runtime, but have not yet created performance experiments to compare results, as creating one-to-one comparisons between the prototype and the \CFA runtime will be complex.
338
339\subsection{Dynamic Resizing} \label{sec:resize}
340
341\begin{figure}
342        \begin{center}
343                \input{system}
344        \end{center}
345        \caption{Global structure of the \CFA runtime system.}
346        \label{fig:system}
347\end{figure}
348
349The \CFA runtime system groups processors together as \newterm{clusters}, as shown in Figure~\ref{fig:system}.
350Threads on a cluster are always scheduled on one of the processors of the cluster.
351Currently, the runtime handles dynamically adding and removing processors from clusters at any time.
352Since this is part of the existing design, the proposed scheduler must also support this behaviour.
353However, dynamically resizing a cluster is considered a rare event associated with setup, tear down and major configuration changes.
354This assumption is made both in the design of the proposed scheduler as well as in the original design of the \CFA runtime system.
355As such, the proposed scheduler must honour the correctness of this behaviour but does not have any performance objectives with regard to resizing a cluster.
356How long adding or removing processors take and how much this disrupts the performance of other threads is considered a secondary concern since it should be amortized over long period of times.
357However, as mentioned in Section~\ref{sec:queue}, contention on the underlying queues can have a direct impact on performance.
358The number of underlying queues must therefore be adjusted as the number of processors grows or shrinks.
359Since the underlying queues are stored in a dense array, changing the number of queues requires resizing the array and expanding the array requires moving it, which can introduce memory reclamation problems if not done correctly.
360
361\begin{figure}
362        \begin{center}
363                \input{resize}
364        \end{center}
365        \caption{Copy of data structure shown in Figure~\ref{fig:base}.}
366        \label{fig:base2}
367\end{figure}
368
369It is important to note how the array is used in this case.
370While the array cells are modified by every push and pop operation, the array itself, \ie the pointer that would change when resized, is only read during these operations.
371Therefore the use of this pointer can be described as frequent reads and infrequent writes.
372This description effectively matches with the description of a reader-writer lock, infrequent but invasive updates among frequent read operations.
373In the case of the ready queue described above, read operations are operations that push or pop from the ready queue but do not invalidate any references to the ready queue data structures.
374Writes on the other hand would add or remove inner queues, invalidating references to the array of inner queues in a process.
375Therefore, the current proposed approach to this problem is to add a per-cluster reader-writer lock around the ready queue to prevent restructuring of the ready-queue data-structure while threads are being pushed or popped.
376
377There are possible alternatives to the reader-writer lock solution.
378This problem is effectively a memory reclamation problem and as such there is a large body of research on the subject\cite{michael2004hazard, brown2015reclaiming}.
379However, the reader-write lock-solution is simple and can be leveraged to solve other problems (\eg processor ordering and memory reclamation of threads), which makes it an attractive solution.
380
381\paragraph{Objectives and Existing Work}
382The lock must offer scalability and performance on par with the actual ready-queue in order not to introduce a new bottleneck.
383I have already built a lock that fits the desired requirements and preliminary testing show scalability and performance that exceed the target.
384As such, I do not consider this lock to be a risk for this project.
385
386\subsection{Idle Sleep} \label{sec:sleep}
387
388\newterm{Idle sleep} is the process of putting processors to sleep when they have no threads to execute.
389In this context, processors are kernel threads and sleeping refers to asking the kernel to block a thread.
390This operation can be achieved with either thread synchronization operations like $pthread_cond_wait$ or using signal operations like $sigsuspend$.
391The goal of putting idle processors to sleep is:
392\begin{enumerate}
393\item
394reduce contention on the ready queue, since the otherwise idle processors generally contend trying to pop items from the queue,
395\item
396give back unneeded CPU time associated with a process to other user processors executing on the computer,
397\item
398and reduce energy consumption in cases where more idle kernel-threads translate to idle CPUs, which can cycle down.
399\end{enumerate}
400Support for idle sleep broadly involves calling the operating system to block the kernel thread and handling the race between a blocking thread and the waking thread, and handling which kernel thread should sleep or wake up.
401
402When a processor decides to sleep, there is a race that occurs between it signalling that is going to sleep (so other processors can find sleeping processors) and actually blocking the kernel thread.
403This operation is equivalent to the classic problem of missing signals when using condition variables: the ``sleepy'' processor indicates its intention to block but has not yet gone to sleep when another processor attempts to wake it up.
404The waking-up operation sees the blocked process and signals it, but the blocking process is racing to sleep so the signal is missed.
405In cases where kernel threads are managed as processors on the current cluster, loosing signals is not necessarily critical, because at least some processors on the cluster are awake and may check for more processors eventually.
406Individual processors always finish scheduling user threads before looking for new work, which means that the last processor to go to sleep cannot miss threads scheduled from inside the cluster (if they do, that demonstrates the ready queue is not linearizable).
407However, this guarantee does not hold if threads are scheduled from outside the cluster, either due to an external event like timers and I/O, or due to a user (or kernel) thread migrating from a different cluster.
408In this case, missed signals can lead to the cluster deadlocking\footnote{Clusters should only deadlock in cases where a \CFA programmer \emph{actually} write \CFA code that leads to a deadlock.}.
409Therefore, it is important that the scheduling of threads include a mechanism where signals \emph{cannot} be missed.
410For performance reasons, it can be advantageous to have a secondary mechanism that allows signals to be missed in cases where it cannot lead to a deadlock.
411To be safe, this process must include a ``handshake'' where it is guaranteed that either~: the sleeping processor notices that a user thread is scheduled after the sleeping processor signalled its intent to block or code scheduling threads sees the intent to sleep before scheduling and be able to wake-up the processor.
412This matter is complicated by the fact that pthreads and Linux offer few tools to implement this solution and no guarantee of ordering of threads waking up for most of these tools.
413
414Another important issue is avoiding kernel threads sleeping and waking frequently because there is a significant operating-system cost.
415This scenario happens when a program oscillates between high and low activity, needing most and then less processors.
416A possible partial solution is to order the processors so that the one which most recently went to sleep is woken up.
417This allows other sleeping processors to reach deeper sleep state (when these are available) while keeping ``hot'' processors warmer.
418Note that while this generally means organizing the processors in a stack, I believe that the unique index provided in my reader-writer lock can be reused to strictly order the waking processors, causing a mostly LIFO order.
419While a strict LIFO stack is probably better, the processor index could prove useful for other reasons, while still offering a sufficiently LIFO ordering.
420
421A final important aspect of idle sleep is when should processors make the decision to sleep and when is it appropriate for sleeping processors to be woken up.
422Processors that are unnecessarily unblocked lead to unnecessary contention, CPU usage, and power consumption, while too many sleeping processors can lead to sub-optimal throughput.
423Furthermore, transitions from sleeping to awake and vice-versa also add unnecessary latency.
424There is already a wealth of research on the subject\cite{schillings1996engineering, wiki:thunderherd} and I may use an existing approach for the idle-sleep heuristic in this project, \eg\cite{Karsten20}.
425
426\subsection{Asynchronous I/O}
427
428The final aspect of this proposal is asynchronous I/O.
429Without it, user threads that execute I/O operations block the underlying kernel thread, which leads to poor throughput.
430It is preferable to block the user thread performing the I/O and reuse the underlying kernel-thread to run other ready user threads.
431This approach requires intercepting user-thread calls to I/O operations, redirecting them to an asynchronous I/O interface, and handling the multiplexing/demultiplexing between the synchronous and asynchronous API.
432As such, there are three components needed to implemented support for asynchronous I/O:
433\begin{enumerate}
434\item
435an OS abstraction layer over the asynchronous interface,
436\item
437an event-engine to (de)multiplex the operations,
438\item
439and a synchronous interface for users to use.
440\end{enumerate}
441None of these components currently exist in \CFA and I will need to build all three for this project.
442
443\paragraph{OS Abstraction}
444One fundamental part for converting blocking I/O operations into non-blocking ones is having an underlying asynchronous I/O interface to direct the I/O operations.
445While there exists many different APIs for asynchronous I/O, it is not part of this proposal to create a novel API.
446It is sufficient to make one work in the complex context of the \CFA runtime.
447\uC uses the $select$\cite{select} as its interface, which handles ttys, pipes and sockets, but not disk.
448$select$ entails significant complexity and is being replaced in UNIX operating-systems, which make it a less interesting alternative.
449Another popular interface is $epoll$\cite{epoll}, which is supposed to be cheaper than $select$.
450However, $epoll$ also does not handle the file system and anectodal evidence suggest it has problem with linux pipes and $TTY$s.
451A popular cross-platform alternative is $libuv$\cite{libuv}, which offers asynchronous sockets and asynchronous file system operations (among other features).
452However, as a full-featured library it includes much more than I need and could conflict with other features of \CFA unless significant effort is made to merge them together.
453A very recent alternative that I am investigating is $io_uring$\cite{io_uring}.
454It claims to address some of the issues with $epoll$ and my early investigating suggest that the claim is accurate.
455$io_uring$ uses a much more general approach where system calls are register to a queue and later executed by the kernel, rather than relying on system calls to return an error instead of blocking and subsequently waiting for changes on file descriptors.
456I believe this approach allows for fewer problems, \eg the manpage for $open$\cite{open} states:
457\begin{quote}
458        Note that [the $O_NONBLOCK$ flag] has no effect for regular files and block devices;
459        that is, I/O operations will (briefly) block when device activity is required, regardless of whether $O_NONBLOCK$ is set.
460        Since $O_NONBLOCK$ semantics might eventually be implemented, applications should not depend upon blocking behavior when specifying this flag for regular files and block devices.
461\end{quote}
462This makes approach based on $epoll$/$select$ less reliable since they may not work for every file descriptors.
463For this reason, I plan to use $io_uring$ as the OS abstraction for the \CFA runtime, unless further work shows problems I haven't encountered yet.
464However, only a small subset of the features are available in Ubuntu as of April 2020\cite{wiki:ubuntu-linux}, which will limit performance comparisons.
465I do not believe this will affect the comparison result.
466
467\paragraph{Event Engine}
468Laying on top of the asynchronous interface layer is the event engine.
469This engine is responsible for multiplexing (batching) the synchronous I/O requests into asynchronous I/O requests and demultiplexing the results to appropriate blocked user threads.
470This step can be straightforward for simple cases, but becomes quite complex when there are thousands of user threads performing both reads and writes, possibly on overlapping file descriptors.
471Decisions that need to be made include:
472\begin{enumerate}
473\item
474whether to poll from a separate kernel thread or a regularly scheduled user thread,
475\item
476what should be the ordering used when results satisfy many requests,
477\item
478how to handle threads waiting for multiple operations, etc.
479\end{enumerate}
480
481\paragraph{Interface}
482Finally, for these non-blocking I/O components to be available, it is necessary to expose them through a synchronous interface because that is the \CFA concurrent programming style.
483The interface can be novel but it is preferable to match the existing POSIX interface when possible to be compatible with existing code.
484Matching allows C programs written using this interface to be transparently converted to \CFA with minimal effort.
485Where new functionality is needed, I will create a novel interface to fill gaps and provide advanced features.
486
487
488% ===============================================================================
489% ===============================================================================
490\section{Discussion}
491I believe that runtime system and scheduling are still open topics.
492Many ``state of the art'' production frameworks still use single threaded event-loops because of performance considerations, \eg \cite{nginx-design}, and, to my knowledge, no wideyl available system language offers modern threading facilities.
493I believe the proposed work offers a novel runtime and scheduling package, where existing work only offers fragments that users must assemble themselves when possible.
494
495% ===============================================================================
496% ===============================================================================
497\section{Timeline}
498\begin{center}
499\begin{tabular}{ | r @{--} l | p{4in} | }
500\hline May 2020 & October 2020   & Creation of the performance benchmark. \\
501\hline November 2020 & March 2021   & Completion of the implementation. \\
502\hline March 2021 & April 2021  & Final Performance experiments. \\
503\hline May 2021 & August 2021 & Thesis writing and defense. \\
504\hline
505\end{tabular}
506\end{center}
507
508% B I B L I O G R A P H Y
509% -----------------------------
510\cleardoublepage
511\phantomsection         % allows hyperref to link to the correct page
512\addcontentsline{toc}{section}{\refname}
513\bibliographystyle{plain}
514\bibliography{pl,local}
515
516% G L O S S A R Y
517% -----------------------------
518\cleardoublepage
519\phantomsection         % allows hyperref to link to the correct page
520\addcontentsline{toc}{section}{Glossary}
521\printglossary
522
523\end{document}
Note: See TracBrowser for help on using the repository browser.