1 | % ====================================================================== |
---|
2 | % ====================================================================== |
---|
3 | \chapter{Actors}\label{s:actors} |
---|
4 | % ====================================================================== |
---|
5 | % ====================================================================== |
---|
6 | |
---|
7 | Actors are an indirect concurrent feature that abstracts threading away from a programmer, and instead provides \gls{actor}s and messages as building blocks for concurrency, where message passing means there is no shared data to protect, making actors amenable in a distributed environment. |
---|
8 | Actors are another message passing concurrency feature, similar to channels but with more abstraction, and are in the realm of \gls{impl_concurrency}, where programmers write concurrent code without dealing with explicit thread creation or interaction. |
---|
9 | The study of actors can be broken into two concepts, the \gls{actor_model}, which describes the model of computation and the \gls{actor_system}, which refers to the implementation of the model. |
---|
10 | Before discussing \CFA's actor system in detail, it is important to first describe the actor model, and the classic approach to implementing an actor system. |
---|
11 | |
---|
12 | \section{Actor Model} |
---|
13 | The \Newterm{actor model} is a concurrent paradigm where computation is broken into units of work called actors, and the data for computation is distributed to actors in the form of messages~\cite{Hewitt73}. |
---|
14 | An actor is composed of a \Newterm{mailbox} (message queue) and a set of \Newterm{behaviours} that receive from the mailbox to perform work. |
---|
15 | Actors execute asynchronously upon receiving a message and can modify their own state, make decisions, spawn more actors, and send messages to other actors. |
---|
16 | Because the actor model is implicit concurrency, its strength is that it abstracts away many details and concerns needed in other concurrent paradigms. |
---|
17 | For example, mutual exclusion and locking are rarely relevant concepts in an actor model, as actors typically only operate on local state. |
---|
18 | |
---|
19 | An actor does not have a thread. |
---|
20 | An actor is executed by an underlying \Newterm{executor} (kernel thread-pool) that fairly invokes each actor, where an actor invocation processes one or more messages from its mailbox. |
---|
21 | The default number of executor threads is often proportional to the number of computer cores to achieve good performance. |
---|
22 | An executor is often tunable with respect to the number of kernel threads and its scheduling algorithm, which optimize for specific actor applications and workloads \see{end of Section~\ref{s:ActorSystem}}. |
---|
23 | |
---|
24 | \subsection{Classic Actor System} |
---|
25 | An implementation of the actor model with a community of actors is called an \Newterm{actor system}. |
---|
26 | Actor systems largely follow the actor model, but can differ in some ways. |
---|
27 | While the semantics of message \emph{send} is asynchronous, the implementation may be synchronous or a combination. |
---|
28 | The default semantics for message \emph{receive} is \gls{fifo}, so an actor receives messages from its mailbox in temporal (arrival) order; |
---|
29 | however, messages sent among actors arrive in any order. |
---|
30 | Some actor systems provide priority-based mailboxes and/or priority-based message-selection within a mailbox, where custom message dispatchers search among or within a mailbox(es) with a predicate for specific kinds of actors and/or messages. |
---|
31 | Some actor systems provide a shared mailbox where multiple actors receive from a common mailbox~\cite{Akka}, which is contrary to the no-sharing design of the basic actor-model (and requires additional locking). |
---|
32 | For non-\gls{fifo} service, some notion of fairness (eventual progress) must exist, otherwise messages have a high latency or starve, \ie never received. |
---|
33 | Finally, some actor systems provide multiple typed-mailboxes, which then lose the actor-\lstinline{become} mechanism \see{Section~\ref{s:SafetyProductivity}}). |
---|
34 | %While the definition of the actor model provides no restrictions on message ordering, actor systems tend to guarantee that messages sent from a given actor $i$ to actor $j$ will arrive at actor $j$ in the order they were sent. |
---|
35 | Another way an actor system varies from the model is allowing access to shared global-state. |
---|
36 | When this occurs, it complicates the implementation as this breaks any implicit mutual-exclusion guarantees when only accessing local-state. |
---|
37 | |
---|
38 | \begin{figure} |
---|
39 | \begin{tabular}{l|l} |
---|
40 | \subfloat[Actor-centric system]{\label{f:standard_actor}\input{diagrams/standard_actor.tikz}} & |
---|
41 | \subfloat[Message-centric system]{\label{f:inverted_actor}\raisebox{.1\height}{\input{diagrams/inverted_actor.tikz}}} |
---|
42 | \end{tabular} |
---|
43 | \caption{Classic and inverted actor implementation approaches with sharded queues.} |
---|
44 | \end{figure} |
---|
45 | |
---|
46 | \subsection{\CFA Actor System} |
---|
47 | Figure~\ref{f:standard_actor} shows an actor system designed as \Newterm{actor-centric}, where a set of actors are scheduled and run on underlying executor threads~\cite{CAF,Akka,ProtoActor}. |
---|
48 | The simplest design has a single global queue of actors accessed by the executor threads, but this approach results in high contention as both ends of the queue by the executor threads. |
---|
49 | The more common design is to \Newterm{shard} the single queue among the executor threads, where actors are permanently assigned or can float among the queues. |
---|
50 | Sharding significantly decreases contention among executor threads adding and removing actors to/from a queue. |
---|
51 | Finally, each actor has a receive queue of messages (mailbox), which is a single consumer, multi-producer queue, \ie only the actor removes from the mailbox but multiple actors add messages. |
---|
52 | When an actor receives a message in its mailbox, the actor is marked ready and scheduled by a thread to run the actor's current behaviour on the message(s). |
---|
53 | |
---|
54 | % cite parallel theatre and our paper |
---|
55 | Figure \ref{f:inverted_actor} shows an actor system designed as \Newterm{message-centric}, where a set of messages are scheduled and run on underlying executor threads~\cite{uC++,Nigro21}. |
---|
56 | This design is \Newterm{inverted} because actors belong to a message queue, whereas in the classic approach a message queue belongs to each actor. |
---|
57 | Now a message send must queries the actor to know which message queue to post the message. |
---|
58 | Again, the simplest design has a single global queue of messages accessed by the executor threads, but this approach has the same contention problem by the executor threads. |
---|
59 | Therefore, the messages (mailboxes) are sharded and executor threads schedule each message, which points to its corresponding actor. |
---|
60 | Here, an actor's messages are permanently assigned to one queue to ensure \gls{fifo} receiving and/or reduce searching for specific actor/messages. |
---|
61 | Since multiple actors belong to each message queue, actor messages are interleaved on a queue, but individually in FIFO order. |
---|
62 | % In this inverted actor system instead of each executor threads owning a queue of actors, they each own a queue of messages. |
---|
63 | % In this scheme work is consumed from their queue and executed by underlying threads. |
---|
64 | The inverted model can be taken a step further by sharding the message queues for each executor threads, so each executor thread owns a set of queues and cycles through them. |
---|
65 | Again, this extra level of sharding is to reduce queue contention. |
---|
66 | % The arrows from the message queues to the actors in the diagram indicate interleaved messages addressed to each actor. |
---|
67 | |
---|
68 | The actor system in \CFA uses a message-centric design, adopts several features from my prior actor work in \uC~\cite{Buhr22} but implemented in \CFA, and adds the following new \CFA contributions: |
---|
69 | \begin{enumerate}[topsep=5pt,itemsep=3pt,parsep=0pt] |
---|
70 | \item |
---|
71 | Provide insight into the impact of envelope allocation in actor systems \see{Section~\ref{s:envelope}}. |
---|
72 | In all actor systems, dynamic allocation is needed to ensure the lifetime of a unit of work persists from its creation until the unit of work is executed. |
---|
73 | This allocation is often called an \Newterm{envelope} as it ``packages'' the information needed to run the unit of work, alongside any other information needed to send the unit of work, such as an actor's address or link fields. |
---|
74 | This dynamic allocation occurs once per message sent. |
---|
75 | Unfortunately, the high rate of message sends in an actor system results in significant contention on the memory allocator. |
---|
76 | A novel data structure is introduced to consolidate allocations to improve performance by minimizing allocator contention. |
---|
77 | |
---|
78 | \item |
---|
79 | Improve performance of the inverted actor system using multiple approaches to minimize contention on queues, such as queue gulping and avoiding atomic operations. |
---|
80 | |
---|
81 | \item |
---|
82 | Introduce work stealing in the inverted actor system. |
---|
83 | Work stealing in an actor-centric system involves stealing one or more actors among executor threads. |
---|
84 | In the inverted system, the notion of stealing message queues is introduced. |
---|
85 | The queue stealing is implemented such that the act of stealing work does not contend with non-stealing executor threads running actors. |
---|
86 | |
---|
87 | \item |
---|
88 | Introduce and evaluate a timestamp-based work-stealing heuristic with the goal of maintaining non-workstealing performance in work-saturated workloads and improving performance on unbalanced workloads. |
---|
89 | |
---|
90 | \item |
---|
91 | Provide a suite of safety and productivity features including static-typing, detection of erroneous message sends, statistics tracking, and more. |
---|
92 | \end{enumerate} |
---|
93 | |
---|
94 | \section{\CFA Actor}\label{s:CFAActor} |
---|
95 | \CFA is not an object oriented language and it does not have \gls{rtti}. |
---|
96 | As such, all message sends and receives among actors can only occur using static type-matching, as in Typed-Akka~\cite{AkkaTyped}. |
---|
97 | Figure~\ref{f:BehaviourStyles} contrasts dynamic and static type-matching. |
---|
98 | Figure~\ref{l:dynamic_style} shows the dynamic style with a heterogeneous message receive and an indirect dynamic type-discrimination for message processing. |
---|
99 | Figure~\ref{l:static_style} shows the static style with a homogeneous message receive and a direct static type-discrimination for message processing. |
---|
100 | The static-typing style is safer because of the static check and faster because there is no dynamic type-discrimination. |
---|
101 | The dynamic-typing style is more flexible because multiple kinds of messages can be handled in a behaviour condensing the processing code. |
---|
102 | |
---|
103 | \begin{figure} |
---|
104 | \centering |
---|
105 | |
---|
106 | \begin{lrbox}{\myboxA} |
---|
107 | \begin{cfa}[morekeywords=case] |
---|
108 | allocation receive( message & msg ) { |
---|
109 | case( @msg_type1@, msg ) { // discriminate type |
---|
110 | ... msg_d-> ...; // msg_type1 msg_d |
---|
111 | } else case( @msg_type2@, msg ) { |
---|
112 | ... msg_d-> ...; // msg_type2 msg_d |
---|
113 | ... |
---|
114 | } |
---|
115 | \end{cfa} |
---|
116 | \end{lrbox} |
---|
117 | |
---|
118 | \begin{lrbox}{\myboxB} |
---|
119 | \begin{cfa} |
---|
120 | allocation receive( @msg_type1@ & msg ) { |
---|
121 | ... msg ...; |
---|
122 | } |
---|
123 | allocation receive( @msg_type2@ & msg ) { |
---|
124 | ... msg ...; |
---|
125 | } |
---|
126 | ... |
---|
127 | \end{cfa} |
---|
128 | \end{lrbox} |
---|
129 | |
---|
130 | \subfloat[dynamic typing]{\label{l:dynamic_style}\usebox\myboxA} |
---|
131 | \hspace*{10pt} |
---|
132 | \vrule |
---|
133 | \hspace*{10pt} |
---|
134 | \subfloat[static typing]{\label{l:static_style}\usebox\myboxB} |
---|
135 | \caption{Behaviour Styles} |
---|
136 | \label{f:BehaviourStyles} |
---|
137 | \end{figure} |
---|
138 | |
---|
139 | \begin{figure} |
---|
140 | \centering |
---|
141 | |
---|
142 | \begin{cfa} |
---|
143 | // actor |
---|
144 | struct my_actor { |
---|
145 | @inline actor;@ $\C[3.25in]{// Plan-9 C inheritance}$ |
---|
146 | }; |
---|
147 | // messages |
---|
148 | struct str_msg { |
---|
149 | char str[12]; |
---|
150 | @inline message;@ $\C{// Plan-9 C inheritance}$ |
---|
151 | }; |
---|
152 | void ?{}( str_msg & this, char * str ) { strcpy( this.str, str ); } $\C{// constructor}$ |
---|
153 | struct int_msg { |
---|
154 | int i; |
---|
155 | @inline message;@ $\C{// Plan-9 C inheritance}$ |
---|
156 | }; |
---|
157 | // behaviours |
---|
158 | allocation receive( my_actor &, @str_msg & msg@ ) with(msg) { |
---|
159 | sout | "string message \"" | str | "\""; |
---|
160 | return Nodelete; $\C{// actor not finished}$ |
---|
161 | } |
---|
162 | allocation receive( my_actor &, @int_msg & msg@ ) with(msg) { |
---|
163 | sout | "integer message" | i; |
---|
164 | return Nodelete; $\C{// actor not finished}$ |
---|
165 | } |
---|
166 | int main() { |
---|
167 | str_msg str_msg{ "Hello World" }; $\C{// constructor call}$ |
---|
168 | int_msg int_msg{ 42 }; $\C{// constructor call}$ |
---|
169 | start_actor_system(); $\C{// sets up executor}$ |
---|
170 | my_actor actor; $\C{// default constructor call}$ |
---|
171 | @actor | str_msg | int_msg;@ $\C{// cascade sends}$ |
---|
172 | @actor | int_msg;@ $\C{// send}$ |
---|
173 | @actor | finished_msg;@ $\C{// send => terminate actor (deallocation deferred)}$ |
---|
174 | stop_actor_system(); $\C{// waits until actors finish}\CRT$ |
---|
175 | } // deallocate int_msg, str_msg, actor |
---|
176 | \end{cfa} |
---|
177 | \caption{\CFA Actor Syntax} |
---|
178 | \label{f:CFAActor} |
---|
179 | \end{figure} |
---|
180 | |
---|
181 | Figure~\ref{f:CFAActor} shows a complete \CFA actor example, which is discussed in detail. |
---|
182 | The actor type @my_actor@ is a @struct@ that inherits from the base @actor@ @struct@ via the @inline@ keyword. |
---|
183 | This inheritance style is the Plan-9 C-style inheritance discussed in Section~\ref{s:Inheritance}. |
---|
184 | Similarly, the message types @str_msg@ and @int_msg@ are @struct@s that inherits from the base @message@ @struct@ via the @inline@ keyword. |
---|
185 | Only @str_msg@ needs a constructor to copy the C string; |
---|
186 | @int_msg@ is initialized using its \CFA auto-generated constructors. |
---|
187 | There are two matching @receive@ (behaviour) routines that process the corresponding typed messages. |
---|
188 | Both @receive@ routines use a @with@ clause so message fields are not qualified and return @Nodelete@ indicating the actor is not finished. |
---|
189 | Also, all messages are marked with @Nodelete@ as their default allocation state. |
---|
190 | The program main begins by creating two messages on the stack. |
---|
191 | Then the executor system is started by calling @start_actor_system@. |
---|
192 | Now an actor is created on the stack and four messages are sent to it using operator @?|?@. |
---|
193 | The last message is the builtin @finish_msg@, which returns @Finished@ to an executor thread, causing it to remove the actor from the actor system \see{Section~\ref{s:ActorBehaviours}}. |
---|
194 | The call to @stop_actor_system@ blocks the program main until all actors are finished and removed from the actor system. |
---|
195 | The program main ends by deleting the actor and two messages from the stack. |
---|
196 | The output for the program is: |
---|
197 | \begin{cfa} |
---|
198 | string message "Hello World" |
---|
199 | integer message 42 |
---|
200 | integer message 42 |
---|
201 | \end{cfa} |
---|
202 | |
---|
203 | \subsection{Actor Behaviours}\label{s:ActorBehaviours} |
---|
204 | In general, a behaviour for some derived actor and derived message type is defined with the following signature: |
---|
205 | \begin{cfa} |
---|
206 | allocation receive( my_actor & receiver, my_msg & msg ) |
---|
207 | \end{cfa} |
---|
208 | where @my_actor@ and @my_msg@ inherit from types @actor@ and @message@, respectively. |
---|
209 | The return value of @receive@ must be a value from enumerated type, @allocation@: |
---|
210 | \begin{cfa} |
---|
211 | enum allocation { Nodelete, Delete, Destroy, Finished }; |
---|
212 | \end{cfa} |
---|
213 | The values represent a set of actions that dictate what the executor does with an actor or message after a given behaviour returns. |
---|
214 | For actors, the @receive@ routine returns the @allocation@ status to the executor, which takes the appropriate action. |
---|
215 | For messages, either the default allocation, @Nodelete@, or any changed value in the message is examined by the executor, which takes the appropriate action. |
---|
216 | Message state is updated via a call to: |
---|
217 | \begin{cfa} |
---|
218 | void set_allocation( message & this, allocation state ); |
---|
219 | \end{cfa} |
---|
220 | |
---|
221 | In detail, the actions taken by an executor for each of the @allocation@ values are: |
---|
222 | |
---|
223 | \noindent@Nodelete@ |
---|
224 | tells the executor that no action is to be taken with regard to an actor or message. |
---|
225 | This status is used when an actor continues receiving messages or a message is reused. |
---|
226 | |
---|
227 | \noindent@Delete@ |
---|
228 | tells the executor to call the object's destructor and deallocate (delete) the object. |
---|
229 | This status is used with dynamically allocated actors and messages when they are not reused. |
---|
230 | |
---|
231 | \noindent@Destroy@ |
---|
232 | tells the executor to call the object's destructor, but not deallocate the object. |
---|
233 | This status is used with dynamically allocated actors and messages whose storage is reused. |
---|
234 | |
---|
235 | \noindent@Finished@ |
---|
236 | tells the executor to mark the respective actor as finished executing, but not call the object's destructor nor deallocate the object. |
---|
237 | This status is used when actors or messages are global or stack allocated, or a programmer wants to manage deallocation themselves. |
---|
238 | Note, for messages, there is no difference between allocations @Nodelete@ and @Finished@ because both tell the executor to do nothing to the message. |
---|
239 | Hence, @Finished@ is implicitly changed to @Nodelete@ in a message constructor, and @Nodelete@ is used internally for message error-checking \see{Section~\ref{s:SafetyProductivity}}. |
---|
240 | Therefore, reading a message's allocation status after setting to @Finished@ may be either @Nodelete@ (after construction) or @Finished@ (after explicitly setting using @set_allocation@). |
---|
241 | |
---|
242 | For the actor system to terminate, all actors must have returned a status other than @Nodelete@. |
---|
243 | After an actor is terminated, it is erroneous to send messages to it. |
---|
244 | Similarly, after a message is terminated, it cannot be sent to an actor. |
---|
245 | Note, it is safe to construct an actor or message with a status other than @Nodelete@, since the executor only examines the allocation action \emph{after} a behaviour returns. |
---|
246 | |
---|
247 | \subsection{Actor Envelopes}\label{s:envelope} |
---|
248 | As stated, each message, regardless of where it is allocated, can be sent to an arbitrary number of actors, and hence, appear on an arbitrary number of message queues. |
---|
249 | Because a C program manages message lifetime, messages cannot be copied for each send, otherwise who manages the copies? |
---|
250 | Therefore, it is up to the actor program to manage message life-time across receives. |
---|
251 | However, for a message to appear on multiple message queues, it needs an arbitrary number of associated destination behaviours. |
---|
252 | Hence, there is the concept of an envelop, which is dynamically allocated on each send, that wraps a message with any extra implementation fields needed to persist between send and receive. |
---|
253 | Managing the envelop is straightforward because it is created at the send and deleted after the receive, \ie there is 1:1 relationship for an envelop and a many to one relationship for a message. |
---|
254 | |
---|
255 | \PAB{Do you need to say more here?} |
---|
256 | |
---|
257 | % In actor systems, messages are sent and received by actors. |
---|
258 | % When a actor receives a message it executes its behaviour that is associated with that message type. |
---|
259 | % However the unit of work that stores the message, the receiving actor's address, and other pertinent information needs to persist between send and the receive. |
---|
260 | % Furthermore the unit of work needs to be able to be stored in some fashion, usually in a queue, until it is executed by an actor. |
---|
261 | % All these requirements are fulfilled by a construct called an envelope. |
---|
262 | % The envelope wraps up the unit of work and also stores any information needed by data structures such as link fields. |
---|
263 | |
---|
264 | % One may ask, "Could the link fields and other information be stored in the message?". |
---|
265 | % This is a good question to ask since messages also need to have a lifetime that persists beyond the work it delivers. |
---|
266 | % However, if one were to use messages as envelopes then a message would not be able to be sent to multiple actors at a time. |
---|
267 | % Therefore this approach would just push the allocation into another location, and require the user to dynamically allocate a message for every send, or require careful ordering to allow for message reuse. |
---|
268 | |
---|
269 | \subsection{Actor System}\label{s:ActorSystem} |
---|
270 | The calls to @start_actor_system@, and @stop_actor_system@ mark the start and end of a \CFA actor system. |
---|
271 | The call to @start_actor_system@ sets up an executor and executor threads for the actor system. |
---|
272 | It is possible to have multiple start/stop scenarios in a program. |
---|
273 | |
---|
274 | @start_actor_system@ has three overloaded signatures that vary the executor's configuration: |
---|
275 | |
---|
276 | \noindent@void start_actor_system()@ |
---|
277 | configures the executor to implicitly use all preallocated kernel-threads (processors), \ie the processors created by the program main prior to starting the actor system. |
---|
278 | When the number of processors is greater than 1, each executor's message queue is sharded by a factor of 16 to reduce contention, \ie for 4 executor threads (processors), there is a total of 4 $\times$ 16 message queues evenly distributed across the executor threads. |
---|
279 | |
---|
280 | \noindent@void start_actor_system( size_t num_thds )@ |
---|
281 | configures the number of executor threads to @num_thds@, with the same message queue sharding. |
---|
282 | |
---|
283 | \noindent@void start_actor_system( executor & this )@ |
---|
284 | allows the programmer to explicitly create and configure an executor for use by the actor system. |
---|
285 | Executor configuration options are discussed in Section~\ref{s:executor}. |
---|
286 | |
---|
287 | \noindent |
---|
288 | All actors must be created \emph{after} calling @start_actor_system@ so the executor can keep track of the number of actors that have entered the system but not yet terminated. |
---|
289 | |
---|
290 | \subsection{Actor Send}\label{s:ActorSend} |
---|
291 | All message sends are done using the vertical-bar (bit-or) operator, @?|?@, similar to the syntax of the \CFA stream I/O. |
---|
292 | One way to provide this operator is through the \CFA type system: |
---|
293 | \begin{cfa} |
---|
294 | actor & ?|?( actor &, message & ) { // base actor and message types |
---|
295 | // boilerplate to send message to executor mail queue |
---|
296 | } |
---|
297 | actor | str_msg | int_msg; // rewritten: ?|?( ?|?( actor, int_msg ), str_msg ) |
---|
298 | \end{cfa} |
---|
299 | In the \CFA type system, calls to this routine work for any pair of parameters that inherit from the @actor@ and @message@ types via Plan-9 inheritance. |
---|
300 | However, within the body the routine, all type information about the derived actor and message is lost (type erasure), so this approach is unable to find the right @receive@ routine to put in the envelope. |
---|
301 | |
---|
302 | If \CFA had a fully-fledged virtual system, the generic @?|?@ routine would work, since the virtual system could dynamically select the derived @receive@ routine via virtual dispatch. |
---|
303 | \CFA does have a preliminary form of virtual routines, but it is not mature enough for use in this work, so a different approach is needed. |
---|
304 | |
---|
305 | Without virtuals, the idiomatic \CFA way to create the generic @?|?@ routine is using @forall@: |
---|
306 | \begin{cfa} |
---|
307 | // forall types A, M that have a receive that returns allocation |
---|
308 | forall( A &, M & | { allocation receive( A &, M & ); } ) |
---|
309 | A & ?|?( A &, M & ) { // actor and message types |
---|
310 | // boilerplate to send message to executor mail queue |
---|
311 | } |
---|
312 | \end{cfa} |
---|
313 | This approach should work. |
---|
314 | However, the \CFA type system is still a work in progress, and there is a nontrivial bug where inherited routines are not recognized by @forall@. |
---|
315 | For example, Figure~\ref{f:type_problem} shows type @B@ has an inherited @foo@ routine through type @A@ and should find the @bar@ routine defined via the @forall@, but does not due the type-system bug. |
---|
316 | |
---|
317 | \begin{figure} |
---|
318 | \begin{cfa} |
---|
319 | struct A {}; |
---|
320 | struct B { inline A; } |
---|
321 | void foo( A & a ) { ... } |
---|
322 | |
---|
323 | // for all types that have a foo routine here is a bar routine |
---|
324 | forall( T & | { void foo( T & ); } ) |
---|
325 | void bar( T & t ) { ... } |
---|
326 | |
---|
327 | int main() { |
---|
328 | B b; |
---|
329 | foo( b ); // B has a foo so it should find a bar via the forall |
---|
330 | bar( b ); // compilation error, no bar found for type B |
---|
331 | } |
---|
332 | \end{cfa} |
---|
333 | \caption{\CFA Type-System Problem} |
---|
334 | \label{f:type_problem} |
---|
335 | \end{figure} |
---|
336 | |
---|
337 | Users could be expected to write the @?|?@ routines, but this approach is error prone and creates maintenance issues. |
---|
338 | Until the \CFA type-system matures, I created a workaround using a template-like approach, where the compiler generates a matching @?|?@ routine for each @receive@ routine it finds with the correct actor/message type-signature. |
---|
339 | This workaround is outside of the type system, but performing a type-system like action. |
---|
340 | The workaround requires no annotation or additional code to be written by users; |
---|
341 | thus, it resolves the maintenance and error problems. |
---|
342 | It should be possible to seamlessly transition the workaround into any updated version of the \CFA type-system. |
---|
343 | |
---|
344 | Figure~\ref{f:send_gen} shows the generated send routine for the @int_msg@ receive in Figure~\ref{f:CFAActor}. |
---|
345 | Operator @?|?@ has the same parameter signature as the corresponding @receive@ routine and returns an @actor@ so the operator can be cascaded. |
---|
346 | The routine sets @rec_fn@ to the matching @receive@ routine using the left-hand type to perform the selection. |
---|
347 | Then the routine packages the actor and message, along with the receive routine into an envelope. |
---|
348 | Finally, the envelop is added to the executor queue designated by the actor using the executor routine @send@. |
---|
349 | |
---|
350 | \begin{figure} |
---|
351 | \begin{cfa} |
---|
352 | $\LstCommentStyle{// from Figure~\ref{f:CFAActor}}$ |
---|
353 | struct my_actor { inline actor; }; $\C[3.75in]{// actor}$ |
---|
354 | struct int_msg { inline message; int i; }; $\C{// message}$ |
---|
355 | allocation receive( @my_actor &, int_msg & msg@ ) {...} $\C{// receiver}$ |
---|
356 | |
---|
357 | // compiler generated send operator |
---|
358 | typedef allocation (*receive_t)( actor &, message & ); |
---|
359 | actor & ?|?( @my_actor & receiver, int_msg & msg@ ) { |
---|
360 | allocation (*rec_fn)( my_actor &, int_msg & ) = @receive@; // deduce receive routine |
---|
361 | request req{ (actor *)&receiver, (message *)&msg, (receive_t)rec_fn }; |
---|
362 | send( receiver, req ); $\C{// queue message for execution}\CRT$ |
---|
363 | return receiver; |
---|
364 | } |
---|
365 | \end{cfa} |
---|
366 | \caption{Generated Send Operator} |
---|
367 | \label{f:send_gen} |
---|
368 | \end{figure} |
---|
369 | |
---|
370 | Figure~\ref{f:ConvenienceMessages} shows three builtin convenience messages and receive routines used to terminate actors, depending on how an actor is allocated: @Delete@, @Destroy@ or @Finished@. |
---|
371 | For example, in Figure~\ref{f:CFAActor}, the builtin @finished_msg@ message and receive are used to terminate the actor because the actor is allocated on the stack, so no deallocation actions are performed by the executor. |
---|
372 | Note, assignment is used to initialize these messages rather than constructors because the constructor changes the allocation to @Nodelete@ for error checking |
---|
373 | |
---|
374 | \begin{figure} |
---|
375 | \begin{cfa} |
---|
376 | message __base_msg_finished $@$= { .allocation_ : Finished }; |
---|
377 | struct delete_msg_t { inline message; } delete_msg = __base_msg_finished; |
---|
378 | struct destroy_msg_t { inline message; } destroy_msg = __base_msg_finished; |
---|
379 | struct finished_msg_t { inline message; } finished_msg = __base_msg_finished; |
---|
380 | |
---|
381 | allocation receive( actor & this, delete_msg_t & msg ) { return Delete; } |
---|
382 | allocation receive( actor & this, destroy_msg_t & msg ) { return Destroy; } |
---|
383 | allocation receive( actor & this, finished_msg_t & msg ) { return Finished; } |
---|
384 | \end{cfa} |
---|
385 | \caption{Builtin Convenience Messages} |
---|
386 | \label{f:ConvenienceMessages} |
---|
387 | \end{figure} |
---|
388 | |
---|
389 | \subsection{Actor Termination}\label{s:ActorTerm} |
---|
390 | During a message send, the receiving actor and message being sent are stored via pointers in the envelope. |
---|
391 | These pointers are the base actor and message types, so type information of the derived actor and message is lost and must be recovered later when the typed receive routine is called. |
---|
392 | After the receive routine is done, the executor must clean up the actor and message according to their allocation status. |
---|
393 | If the allocation status is @Delete@ or @Destroy@, the appropriate destructor must be called by the executor. |
---|
394 | This poses a problem; |
---|
395 | the derived type of the actor or message is not available to the executor, but it needs to call the derived destructor.! |
---|
396 | This requires downcasting from the base type to the derived type, which requires a virtual system. |
---|
397 | To accomplish the dowcast, I implemented a rudimentary destructor-only virtual system in \CFA. |
---|
398 | This virtual system is used via Plan-9 inheritance of the @virtual_dtor@ type, shown in Figure~\ref{f:VirtDtor}. |
---|
399 | The @virtual_dtor@ type maintains a pointer to the start of the object, and a pointer to the correct destructor. |
---|
400 | When a type inherits @virtual_dtor@, the compiler adds code to its destructor to intercepted any destructor calls along this segment of the inheritance tree and restart at the appropriate destructor for that object. |
---|
401 | |
---|
402 | \begin{figure} |
---|
403 | \centering |
---|
404 | |
---|
405 | \begin{lrbox}{\myboxA} |
---|
406 | \begin{cfa} |
---|
407 | struct base { inline virtual_dtor; }; |
---|
408 | void ^?{}( base & ) { sout | "^base"; } |
---|
409 | struct intermediate { inline base; }; |
---|
410 | void ^?{}( intermediate & ) { sout | "^intermediate"; } |
---|
411 | struct derived { inline intermediate; }; |
---|
412 | void ^?{}( derived & ) { sout | "^derived"; } |
---|
413 | |
---|
414 | int main() { |
---|
415 | base & b; |
---|
416 | intermediate i; |
---|
417 | derived d1, d2, d3; |
---|
418 | intermediate & ri = d2; |
---|
419 | base & rb = d3; |
---|
420 | // explicit destructor calls |
---|
421 | ^d1{}; sout | nl; |
---|
422 | ^ri{}; sout | nl; |
---|
423 | ^rb{}; sout | nl; |
---|
424 | } // ^i, ^b |
---|
425 | \end{cfa} |
---|
426 | \end{lrbox} |
---|
427 | |
---|
428 | \begin{lrbox}{\myboxB} |
---|
429 | \begin{cfa} |
---|
430 | ^derived |
---|
431 | ^intermediate |
---|
432 | ^base |
---|
433 | |
---|
434 | ^derived |
---|
435 | ^intermediate |
---|
436 | ^base |
---|
437 | |
---|
438 | ^derived |
---|
439 | ^intermediate |
---|
440 | ^base |
---|
441 | |
---|
442 | ^intermediate |
---|
443 | ^base |
---|
444 | |
---|
445 | |
---|
446 | |
---|
447 | |
---|
448 | \end{cfa} |
---|
449 | |
---|
450 | \end{lrbox} |
---|
451 | \subfloat[Destructor calls]{\label{l:destructor_calls}\usebox\myboxA} |
---|
452 | \hspace*{10pt} |
---|
453 | \vrule |
---|
454 | \hspace*{10pt} |
---|
455 | \subfloat[Output]{\usebox\myboxB} |
---|
456 | |
---|
457 | \caption{\CFA Virtual Destructor} |
---|
458 | \label{f:VirtDtor} |
---|
459 | \end{figure} |
---|
460 | |
---|
461 | While this virtual destructor system was built for this work, it is general and can be used in any type in \CFA. |
---|
462 | Actors and messages opt into this system by inheriting the @virtual_dtor@ type, which allows the executor to call the right destructor without knowing the derived actor or message type. |
---|
463 | |
---|
464 | \section{\CFA Executor}\label{s:executor} |
---|
465 | This section describes the basic architecture of the \CFA executor. |
---|
466 | An executor of an actor system is the scheduler that organizes where actor behaviours are run and how messages are sent and delivered. |
---|
467 | In \CFA, the executor is message-centric \see{Figure~\ref{f:inverted_actor}}, but extended by over sharding of a message queue \see{left side of Figure~\ref{f:gulp}}, \ie there are $M$ message queues where $M$ is greater than the number of executor threads $N$ (usually a multiple of $N$). |
---|
468 | This approach reduces contention by spreading message delivery among the $M$ queues rather than $N$, while still maintaining actor \gls{fifo} message-delivery semantics. |
---|
469 | The only extra overhead is each executor cycling (usually round-robin) through its $M$/$N$ queues. |
---|
470 | The goal is to achieve better performance and scalability for certain kinds of actor applications by reducing executor locking. |
---|
471 | Note, lock-free queues do not help because busy waiting on any atomic instruction is the source of the slowdown whether it is a lock or lock-free. |
---|
472 | |
---|
473 | \begin{figure} |
---|
474 | \begin{center} |
---|
475 | \input{diagrams/gulp.tikz} |
---|
476 | \end{center} |
---|
477 | \caption{Queue Gulping Mechanism} |
---|
478 | \label{f:gulp} |
---|
479 | \end{figure} |
---|
480 | |
---|
481 | Each executor thread iterates over its own message queues until it finds one with messages. |
---|
482 | At this point, the executor thread atomically \gls{gulp}s the queue, meaning it moves the contents of message queue to a local queue of the executor thread using a single atomic instruction. |
---|
483 | An example of the queue gulping operation is shown in the right side of Figure \ref{f:gulp}, where a executor threads gulps queue 0 and begins to process it locally. |
---|
484 | This step allows an executor thread to process the local queue without any atomics until the next gulp. |
---|
485 | Other executor threads can continue adding to the ends of executor thread's message queues. |
---|
486 | In detail, an executor thread performs a test-and-gulp, non-atomically checking if a queue is non-empty, before attempting to gulp it. |
---|
487 | If an executor misses an non-empty queue due to a race, it eventually finds the queue after cycling through its message queues. |
---|
488 | This approach minimizes costly lock acquisitions. |
---|
489 | |
---|
490 | Processing a local queue involves: removing a unit of work from the queue, dereferencing the actor pointed-to by the work-unit, running the actor's behaviour on the work-unit message, examining the returned allocation status from the @receive@ routine for the actor and internal status in the delivered message, and taking the appropriate actions. |
---|
491 | Since all messages to a given actor are in the same queue, this guarantees atomicity across behaviours of that actor since it can only execute on one thread at a time. |
---|
492 | As each actor is created or terminated by an executor thread, it increments/decrements a global counter. |
---|
493 | When an executor decrements the counter to zero, it sets a global boolean variable that is checked by each executor thread when it has no work. |
---|
494 | Once a executor threads sees the flag is set it stops running. |
---|
495 | After all executors stop, the actor system shutdown is complete. |
---|
496 | |
---|
497 | \subsection{Copy Queue}\label{s:copyQueue} |
---|
498 | Unfortunately, the frequent allocation of envelopes for each send results in heavy contention on the memory allocator. |
---|
499 | This contention is reduced using a novel data structure, called a \Newterm{copy queue}. |
---|
500 | The copy queue is a thin layer over a dynamically sized array that is designed with the envelope use case in mind. |
---|
501 | A copy queue supports the typical queue operations of push/pop but in a different way from a typical array-based queue. |
---|
502 | |
---|
503 | The copy queue is designed to take advantage of the \gls{gulp}ing pattern, giving an amortized runtime cost for each push/pop operation of $O(1)$. |
---|
504 | In contrast, a na\"ive array-based queue often has either push or pop cost $O(n)$ and the other cost $O(1)$ since one of the operations requires shifting the elements of the queue. |
---|
505 | Since the executor threads gulp a queue to operate on it locally, this creates a usage pattern where all elements are popped from the copy queue without any interleaved pushes. |
---|
506 | As such, during pop operations there is no need to shift array elements. |
---|
507 | Instead, an index is stored in the copy-queue data-structure that keeps track of which element to pop next allowing pop to be $O(1)$. |
---|
508 | Push operations are amortized $O(1)$ since pushes may cause doubling reallocations of the underlying dynamic-sized array (like \CC @vector@). |
---|
509 | |
---|
510 | Since the copy queue is an array, envelopes are allocated first on the stack and then copied into the copy queue to persist until they are no longer needed. |
---|
511 | For many workload, the copy queues grow in size to facilitate the average number of messages in flight and there is no further dynamic allocations. |
---|
512 | One downside of this approach that more storage is allocated than needed, \ie each copy queue is only partially full. |
---|
513 | Comparatively, the individual envelope allocations of a list-based queue mean that the actor system always uses the minimum amount of heap space and cleans up eagerly. |
---|
514 | Additionally, bursty workloads can cause the copy queues to allocate a large amounts of space to accommodate the peaks of the throughput, even if most of that storage is not needed for the rest of the workload's execution. |
---|
515 | |
---|
516 | To mitigate memory wastage, a reclamation scheme is introduced. |
---|
517 | Initially, the memory reclamation na\"ively reclaims one index of the array per \gls{gulp}, if the array size is above a low fixed threshold. |
---|
518 | However, this approach has a problem. |
---|
519 | The high memory watermark nearly doubled! |
---|
520 | The issue is highlighted with an example. |
---|
521 | Assume a fixed throughput workload, where a queue never has more than 19 messages at a time. |
---|
522 | If the copy queue starts with a size of 10, it ends up doubling at some point to size 20 to accommodate 19 messages. |
---|
523 | However, after 2 gulps and subsequent reclamations the array size is 18. |
---|
524 | The next time 19 messages are enqueued, the array size is doubled to 36! |
---|
525 | To avoid this issue, a second check is added. |
---|
526 | Reclamation only occurs if less than half of the array is utilized. |
---|
527 | This check achieves a lower total storage and overall memory utilization compared to the non-reclamation copy queues. |
---|
528 | However, the use of copy queues still incurs a higher memory cost than list-based queueing, but the increase in memory usage is reasonable considering the performance gains \see{Section~\ref{s:actor_perf}}. |
---|
529 | |
---|
530 | \section{Work Stealing}\label{s:steal} |
---|
531 | Work stealing is a scheduling strategy to provide \Newterm{load balancing}. |
---|
532 | The goal is to increase resource utilization by having an idle thread steal work from a working thread. |
---|
533 | While there are multiple parts in a work-stealing scheduler, two important components are the stealing mechanism and victim selection. |
---|
534 | |
---|
535 | \subsection{Stealing Mechanism} |
---|
536 | In work stealing, the stealing worker is called the \Newterm{thief} and the worker being stolen from is called the \Newterm{victim}. |
---|
537 | % Workers consume actors from their ready queue and execute their behaviours. |
---|
538 | % Through these behaviours, a worker inserts messages onto its own and other worker ready-queues. |
---|
539 | To steal, a thief takes work from a victim's ready queue, so work stealing always results in a potential increase in contention on ready queues between the victim gulping from a queue and the thief stealing the queue. |
---|
540 | This contention can reduce the victim's throughput. |
---|
541 | Note, the data structure used for the ready queue is not discussed since the largest cost is the mutual exclusion and its duration for safely performing the queue operations. |
---|
542 | |
---|
543 | The stealing mechanism in this work differs from most work-stealing actor-systems because of the message-centric (inverted) actor-system. |
---|
544 | Actor systems, such as Akka~\cite{Akka} and CAF~\cite{CAF} using actor-centric systems, steal by dequeuing an actor from a non-empty actor ready-queue and enqueue\-ing to an empty ready-queue. |
---|
545 | % As an example, in CAF, the sharded actor ready queue is a set of double-ended queues (dequeues). |
---|
546 | In \CFA, the actor work-stealing implementation is unique because of the message-centric system. |
---|
547 | With this approach, it is impractical to steal actors because an actor's messages are distributed in temporal order along the message queue. |
---|
548 | To ensure sequential actor execution and \gls{fifo} message delivery in a message-centric system, stealing requires finding and removing \emph{all} of an actor's messages, and inserting them consecutively in another message queue. |
---|
549 | This operation is $O(N)$ with a non-trivial constant. |
---|
550 | The only way for work stealing to become practical is to shard each worker's message queue, which also reduces contention, and to steal queues to eliminate queue searching. |
---|
551 | |
---|
552 | Given queue stealing, the goal of the presented stealing implementation is to have an essentially zero-contention-cost stealing mechanism. |
---|
553 | Achieving this goal requires work stealing to have minimal (practically no) effect on the performance of the victim. |
---|
554 | The implication is that thieves cannot contend with a victim, and that a victim should perform no stealing related work unless it becomes a thief. |
---|
555 | In theory, this goal is not achievable, but practical results show the goal is virtually achieved. |
---|
556 | |
---|
557 | One important lesson learned while working on \uC actors~\cite{} and through discussions with fellow student Thierry Delisle, who examined work-stealing for user-threads in his Ph.D.~\cite{Delisle22}, is \emph{not} to aggressively steal. |
---|
558 | With reasonable workloads, being a thief should be a temporary state, \ie eventually work appears on the thief's ready-queues and it returns to normal operation. |
---|
559 | Furthermore, the act of \emph{looking} to find work is invasive (Heisenberg uncertainty principle), possibly disrupting multiple victims. |
---|
560 | Therefore, stealing should be done lazily in case work appears for the thief and to minimize disruption of victims. |
---|
561 | Note, the cost of stealing is not crucial for the thief because it does not have anything else to do except poll or block. |
---|
562 | |
---|
563 | The outline for lazy-stealing by a thief is: select a victim, scan its queues once, and return immediately if a queue is stolen. |
---|
564 | The thief then returns to normal operation and conducts a regular scan over its own queues looking for work, where stolen work is placed at the end of the scan. |
---|
565 | Hence, only one victim is affected and there is a reasonable delay between stealing events by scanning all the thief's ready queue looking for its own work. |
---|
566 | This lazy examination by the thief has a low perturbation cost for victims, while still finding work in a moderately loaded system. |
---|
567 | In all work-stealing algorithms, there is the pathological case where there is too little work and too many workers; |
---|
568 | this scenario can only be handled by putting workers to sleep or deleting them. |
---|
569 | This case is not examined in this work. |
---|
570 | |
---|
571 | In more detail, the \CFA work-stealing algorithm begins by iterating over its message queues twice without finding any work before it tries to steal a queue from another worker. |
---|
572 | Stealing a queue is done wait-free (\ie no busy waiting) with a few atomic instructions that only create contention with other stealing workers, not the victim. |
---|
573 | The complexity in the implementation is that victim gulping does not take the mailbox queue; |
---|
574 | rather it atomically transfers the mailbox nodes to another list leaving the mailbox empty, \ie it unlinks the queue nodes from the top pointer leaving the original list empty. |
---|
575 | Hence, the original mailbox is always available for new message deliveries. |
---|
576 | However, this transfer logically subdivides the mailbox, and during this period, the mailbox cannot be stolen; |
---|
577 | otherwise there are two threads simultaneously running messages on actors in the two parts of the mailbox queue. |
---|
578 | To solve this problem, an atomic gulp also marks the mailbox queue as subdivided, making it ineligible for stealing. |
---|
579 | Hence, a thief checks if a queue is eligible and non-empty before attempting an atomic steal of a queue. |
---|
580 | |
---|
581 | Figure~\ref{f:steal} shows the queue architecture and stealing mechanism. |
---|
582 | Internally, the mailbox queues are accessed through two arrays of size $N$, which are shared among all workers. |
---|
583 | There is an array of mailbox queues, @mailboxes@, and an array of pointers to the mailboxes, @worker_queues@: |
---|
584 | \begin{cfa} |
---|
585 | struct work_queue { |
---|
586 | spinlock_t mutex_lock; $\C[2.75in]{// atomicity for queue operations}$ |
---|
587 | copy_queue * owned_queue; $\C{// copy queue}$ |
---|
588 | copy_queue * c_queue; $\C{// current queue}$ |
---|
589 | volatile bool being_processed; $\C{// flag to prevent concurrent processing}$ |
---|
590 | }; |
---|
591 | work_queue * mailboxes; $\C{// master array of work request queues}$ |
---|
592 | work_queue ** worker_queues; $\C{// secondary array of work queues to allow for swapping}\CRT$ |
---|
593 | \end{cfa} |
---|
594 | A send inserts a request at the end of a @mailboxes@. |
---|
595 | A steal swaps two pointers in @worker_queues@. |
---|
596 | Conceptually, @worker_queues@ represents the ownership relation between mailboxes and workers. |
---|
597 | Given $M$ workers and $N$ mailboxes, each worker owns a contiguous $M$/$N$ block of pointers in @worker_queues@. |
---|
598 | When a worker gulps, it dereferences one of the pointers in its section of @worker_queues@ and then gulps from the queue it points at. |
---|
599 | To transfer ownership of a mailbox from one worker to another, a pointer from each of the workers' ranges are swapped. |
---|
600 | This structure provides near-complete separation of stealing and gulping/send operations. |
---|
601 | As such, operations can happen on the queues independent of stealing, which avoids almost all contention between thief threads and victim threads. |
---|
602 | |
---|
603 | \begin{figure} |
---|
604 | \begin{center} |
---|
605 | \input{diagrams/steal.tikz} |
---|
606 | \end{center} |
---|
607 | \caption{Queue Stealing Mechanism} |
---|
608 | \label{f:steal} |
---|
609 | \end{figure} |
---|
610 | |
---|
611 | To steal a queue, a thief does the following: |
---|
612 | \begin{enumerate}[topsep=5pt,itemsep=3pt,parsep=0pt] |
---|
613 | \item |
---|
614 | chooses a victim. |
---|
615 | Victim selection heuristics are independent of the stealing mechanism and discussed in Section~\ref{s:victimSelect}. |
---|
616 | |
---|
617 | \item |
---|
618 | scan the victim's $M$/$N$ range of @worker_queues@ and non-atomically checks each mail queue to see if it is eligible and non-empty. |
---|
619 | If a match is found, the thief attempts to steal the queue by swapping the appropriate victim worker-queue pointer with an empty thief's pointer, where the pointers come from the victim's and thief's ranges, respectively. |
---|
620 | % The swap races to interchange the appropriate victim's mail-queue pointer with an empty mail-queue pointer in the thief's @worker_queues@ range. |
---|
621 | This swap can fail if another thief steals the queue, which is discussed further in Section~\ref{s:swap}. |
---|
622 | % Note, a thief never exceeds its $M$/$N$ worker range because it is always exchanging queues with other workers. |
---|
623 | If no appropriate victim mailbox is found, no swap is attempted. |
---|
624 | |
---|
625 | \item |
---|
626 | stops searching after a successful queue steal, a failed queue steal, or all queues in the victim's range are examined. |
---|
627 | The thief then resumes normal execution and ceases being a thief. |
---|
628 | Hence, it iterates over its own worker queues because new messages may have arrived during stealing, including ones in the potentially stolen queue. |
---|
629 | Stealing is only repeated after the worker completes two consecutive iterations over its owned queues without finding work. |
---|
630 | \end{enumerate} |
---|
631 | |
---|
632 | \subsection{Stealing Problem} |
---|
633 | |
---|
634 | Each queue access involving a thief is protected using spinlock @mutex_lock@ and contention among thieves should be low. |
---|
635 | However, to achieve the goal of almost zero contention for the victim, it is necessary to remove this lock around the only critical section involving the victim. |
---|
636 | The victim critical-section is gulping a queue, which involves two steps: |
---|
637 | \begin{cfa} |
---|
638 | temp = worker_queues[x]; |
---|
639 | // preemption and steal |
---|
640 | transfer( temp->owned_queuem, temp->c_queue ); // @being_processed@ set in transfer with mutual exclusion |
---|
641 | \end{cfa} |
---|
642 | where @transfer@ moves the top point from @c_queue@ to @owned_queuem@ and sets the top pointer of @c_queue@ to @0p@ (null) to partition the mailbox. |
---|
643 | Specifically, |
---|
644 | \begin{enumerate}[topsep=5pt,itemsep=3pt,parsep=0pt] |
---|
645 | \item |
---|
646 | The victim must dereference its current queue pointer from @worker_queues@. |
---|
647 | \item |
---|
648 | The victim calls @transfer@ to gulp the queue. |
---|
649 | \end{enumerate} |
---|
650 | If the victim is preempted after the dereference, a thief can steal the queue pointer before the victim calls @transfer@. |
---|
651 | The thief then races ahead, transitions back to a victim, searches its mailbox queues, finds the stolen non-empty queue, and gulps it. |
---|
652 | The original victim now restarts and gulps from the stolen queue pointed to by its dereference, even though the thief put an empty mailbox queue in place for the victim. |
---|
653 | At this point, the victim and thief are possibly processing the same messages, which violates mutual exclusion and the message-ordering guarantee. |
---|
654 | Preventing this race requires either a lock acquire or an atomic operation on the victim's fast-path to guarantee the victim's gulp retrieves the empty mailbox queue installed by the thief. |
---|
655 | However, any form of locking here creates contention between thief and victim. |
---|
656 | |
---|
657 | The alternative to locking is allowing the race and resolving it lazily. |
---|
658 | % As mentioned, there is a race between a victim gulping and a thief stealing because gulping partitions a mailbox queue making it ineligible for stealing. |
---|
659 | % Furthermore, after a thief steals, there is moment when victim gulps but the queue no longer |
---|
660 | % This algorithm largely eliminates contention among thieves and victims except for the rare moment when a victim/thief concurrently attempt to gulp/steal the same queue. |
---|
661 | % Restating, when a victim operates on a queue, it first copies the queue pointer from @worker_queues@ to a local variable (gulp). |
---|
662 | % It then uses that local variable for all queue operations until it moves to the next index of its range. |
---|
663 | % This approach ensures any swaps do not interrupt gulping operations, however this introduces a correctness issue. |
---|
664 | % There is a window for a race condition between the victim and a thief. |
---|
665 | % Once a victim copies the queue pointer from @worker_queues@, a thief could steal that pointer and both may try to gulp from the same queue. |
---|
666 | % These two gulps cannot be allowed to happen concurrently. |
---|
667 | % If any behaviours from a queue are run by two workers at a time it violates both mutual exclusion and the actor ordering guarantees. |
---|
668 | To resolve the race, each mailbox header stores a @being_processed@ flag that is atomically set when a queue is transferred. |
---|
669 | The flag indicates that a queue is being processed by a worker and is reset once the local processing is finished. |
---|
670 | If a worker, either victim or thief turned victim, attempts to gulp a queue and finds that the @being_processed@ flag is set, it does not gulp the queue and moves on to the next queue in its range. |
---|
671 | This resolves the race no matter the winner. |
---|
672 | If the thief wins the race, it steals and gulps the queue, and the victim sees the flag set and skips gulping the queue. |
---|
673 | If the victim wins the race, it gulps the queue, and the thief sees the flag set and skips gulping the queue. |
---|
674 | |
---|
675 | There is a final pathological case where the race occurs and is resolved with \emph{both} gulps occurring. |
---|
676 | Here, the winner of the race finishes processing the queue and resets the @being_processed@ flag. |
---|
677 | Then the loser unblocks and completes its gulp by transferring a \emph{stolen queue} and atomically setting the @being_processed@ flag. |
---|
678 | The loser is now processing messages from the winner's queue, which is safe because the winner ignores this queue as @being_processed@ is set. |
---|
679 | The loser never attempts to process this stolen queue again because its next queue cycle sees the swapped queue from the thief (which may or may not be empty at this point). |
---|
680 | This race is now the only source of contention between victim and thieve as they both try to acquire a lock on the same queue during a transfer. |
---|
681 | In theory, this scenario can cascade across multiple workers all performing this sequence across multiple queues. |
---|
682 | However, the window for this race is extremely small, making this contention rare. |
---|
683 | Furthermore, the chance of a cascading scenario across multiple workers is even rarer. |
---|
684 | % The rarity is why this mechanism is zero-victim-cost in practice but not in theory. |
---|
685 | By collecting statistics on failed gulps due to the @being_processed@ flag, this contention occurs $\approx$0.05\% of the time when a gulp occurs (1 in every 2000 gulps). |
---|
686 | \PAB{You need to explain the experiment that gathered this information.} |
---|
687 | Hence, the claim is made that this stealing mechanism has (probabilistically) zero-victim-cost in practice. |
---|
688 | |
---|
689 | \subsection{Queue Pointer Swap}\label{s:swap} |
---|
690 | Two atomically swap two pointers in @worker_queues@, a novel wait-free swap algorithm is used. |
---|
691 | The novel wait-free swap is effectively a special case of a DCAS. |
---|
692 | DCAS stands for Double Compare-And-Swap, which is a more general version of the Compare-And-Swap (CAS) atomic operation~\cite{Doherty04}. |
---|
693 | CAS compares is a read-modify-write operation available on most modern architectures which atomically compares an expected value with a memory location. |
---|
694 | If the expected value and value in memory are equal it then writes a new value into the memory location. |
---|
695 | A sample software implemention of CAS follows. |
---|
696 | \begin{cfa} |
---|
697 | // assume this routine executes atomically |
---|
698 | bool CAS( val * ptr, val expected, val new ) { |
---|
699 | if ( *ptr != expected ) |
---|
700 | return false; |
---|
701 | *ptr = new; |
---|
702 | return true; |
---|
703 | } |
---|
704 | \end{cfa} |
---|
705 | As shown CAS only operates on one memory location. |
---|
706 | Where CAS operates on a single memory location and some values, DCAS operates on two memory locations. |
---|
707 | \begin{cfa} |
---|
708 | // assume this routine executes atomically |
---|
709 | bool DCAS( val * addr1, val * addr2, val old1, val old2, val new1, val new2 ) { |
---|
710 | if ( ( *addr1 == old1 ) && ( *addr2 == old2 ) ) { |
---|
711 | *addr1 = new1; |
---|
712 | *addr2 = new2; |
---|
713 | return true; |
---|
714 | } |
---|
715 | return false; |
---|
716 | } |
---|
717 | \end{cfa} |
---|
718 | The DCAS implemented in this work is special cased in two ways. |
---|
719 | First of all, a DCAS is more powerful than what is needed to swap two pointers. |
---|
720 | A double atomic swap (DAS) is all that is needed for this work. |
---|
721 | The atomic swap provided by most modern hardware requires that at least one operand is a register. |
---|
722 | A DAS would relax that restriction so that both operands of the swap could be memory locations. |
---|
723 | As such a DAS can be written in terms of the DCAS above as follows. |
---|
724 | \begin{cfa} |
---|
725 | bool DAS( val * addr1, val * addr2 ) { |
---|
726 | return DCAS( addr1, addr2, *addr1, *addr2, *addr2, *addr1 ); |
---|
727 | } |
---|
728 | \end{cfa} |
---|
729 | The other special case is that the values being swapped will never null pointers. |
---|
730 | This allows the DAS implementation presented to use null pointers as intermediate values during the swap. |
---|
731 | |
---|
732 | Given the wait-free swap used is novel, it is important to show that it is correct. |
---|
733 | It is clear to show that the swap is wait-free since all thieves will fail or succeed in swapping the queues in a finite number of steps since there are no locks or looping. |
---|
734 | There is no retry mechanism in the case of a failed swap, since a failed swap either means the work was already stolen, or that work was stolen from the thief. |
---|
735 | In both cases it is apropos for a thief to give up on stealing. |
---|
736 | \CFA-style pseudocode for the queue swap is presented below. |
---|
737 | The swap uses compare-and-swap (@CAS@) which is just pseudocode for C's @__atomic_compare_exchange_n@. |
---|
738 | A pseudocode implementation of @CAS@ is also shown below. |
---|
739 | The correctness of the wait-free swap will now be discussed in detail. |
---|
740 | To first verify sequential correctness, consider the equivalent sequential swap below: |
---|
741 | |
---|
742 | \begin{cfa} |
---|
743 | void swap( uint victim_idx, uint my_idx ) { |
---|
744 | // Step 0: |
---|
745 | work_queue * my_queue = mailboxes[my_idx]; |
---|
746 | work_queue * vic_queue = mailboxes[victim_idx]; |
---|
747 | // Step 2: |
---|
748 | mailboxes[my_idx] = 0p; |
---|
749 | // Step 3: |
---|
750 | mailboxes[victim_idx] = my_queue; |
---|
751 | // Step 4: |
---|
752 | mailboxes[my_idx] = vic_queue; |
---|
753 | } |
---|
754 | \end{cfa} |
---|
755 | |
---|
756 | Step 1 is missing in the sequential example since it only matters in the concurrent context presented later. |
---|
757 | By looking at the sequential swap it is easy to see that it is correct. |
---|
758 | Temporary copies of each pointer being swapped are stored, and then the original values of each pointer are set using the copy of the other pointer. |
---|
759 | |
---|
760 | \begin{cfa} |
---|
761 | |
---|
762 | bool try_swap_queues( worker & this, uint victim_idx, uint my_idx ) with(this) { |
---|
763 | // Step 0: |
---|
764 | // mailboxes is the shared array of all sharded queues |
---|
765 | work_queue * my_queue = mailboxes[my_idx]; |
---|
766 | work_queue * vic_queue = mailboxes[victim_idx]; |
---|
767 | |
---|
768 | // Step 1: |
---|
769 | // If the victim queue is 0p then they are in the process of stealing so return false |
---|
770 | // 0p is Cforall's equivalent of C++'s nullptr |
---|
771 | if ( vic_queue == 0p ) return false; |
---|
772 | |
---|
773 | // Step 2: |
---|
774 | // Try to set our own (thief's) queue ptr to be 0p. |
---|
775 | // If this CAS fails someone stole our (thief's) queue so return false |
---|
776 | if ( !CAS( &mailboxes[my_idx], &my_queue, 0p ) ) |
---|
777 | return false; |
---|
778 | |
---|
779 | // Step 3: |
---|
780 | // Try to set victim queue ptr to be our (thief's) queue ptr. |
---|
781 | // If it fails someone stole the other queue, so fix up then return false |
---|
782 | if ( !CAS( &mailboxes[victim_idx], &vic_queue, my_queue ) ) { |
---|
783 | mailboxes[my_idx] = my_queue; // reset queue ptr back to prev val |
---|
784 | return false; |
---|
785 | } |
---|
786 | |
---|
787 | // Step 4: |
---|
788 | // Successfully swapped. |
---|
789 | // Thief's ptr is 0p so no one will touch it |
---|
790 | // Write back without CAS is safe |
---|
791 | mailboxes[my_idx] = vic_queue; |
---|
792 | return true; |
---|
793 | } |
---|
794 | \end{cfa}\label{c:swap} |
---|
795 | |
---|
796 | Now consider the concurrent implementation of the swap. |
---|
797 | \begin{enumerate}[topsep=5pt,itemsep=3pt,parsep=0pt] |
---|
798 | \item |
---|
799 | Step 0 is the same as the sequential example, and the thief stores local copies of the two pointers to be swapped. |
---|
800 | \item |
---|
801 | Step 1 verifies that the stored copy of the victim queue pointer, @vic_queue@, is valid. |
---|
802 | If @vic_queue@ is equal to @0p@, then the victim queue is part of another swap so the operation fails. |
---|
803 | No state has changed at this point so no fixups are needed. |
---|
804 | Note, @my_queue@ can never be equal to @0p@ at this point since thieves only set their own queues pointers to @0p@ when stealing. |
---|
805 | At no other point will a queue pointer be set to @0p@. |
---|
806 | Since each worker owns a disjoint range of the queue array, it is impossible for @my_queue@ to be @0p@. |
---|
807 | \item |
---|
808 | Step 2 attempts to set the thief's queue pointer to @0p@ via @CAS@. |
---|
809 | The @CAS@ will only fail if the thief's queue pointer is no longer equal to @my_queue@, which implies that this thief has become a victim and its queue has been stolen. |
---|
810 | At this point the thief-turned-victim will fail and since it has not changed any state it just fails and returns false. |
---|
811 | If the @CAS@ succeeds then the thief's queue pointer will now be @0p@. |
---|
812 | Nulling the pointer is safe since only thieves look at other worker's queue ranges, and whenever thieves need to dereference a queue pointer they check for @0p@. |
---|
813 | \item |
---|
814 | Step 3 attempts to set the victim's queue pointer to be @my_queue@ via @CAS@. |
---|
815 | If the @CAS@ succeeds then the victim's queue pointer has been set and swap can no longer fail. |
---|
816 | If the @CAS@ fails then the thief's queue pointer must be restored to its previous value before returning. |
---|
817 | \item |
---|
818 | Step 4 sets the thief's queue pointer to be @vic_queue@ completing the swap. |
---|
819 | \end{enumerate} |
---|
820 | |
---|
821 | \begin{theorem} |
---|
822 | The presented swap is correct and concurrently safe in both the success and failure cases. |
---|
823 | \end{theorem} |
---|
824 | |
---|
825 | Correctness of the swap is shown through the existence of an invariant. |
---|
826 | The invariant is that when a queue pointer is set to @0p@ by a thief, then the next write to the pointer can only be performed by the same thief. |
---|
827 | To show that this invariant holds, it is shown that it is true at each step of the swap. |
---|
828 | Step 0 and 1 do not write and as such they cannot invalidate the invariant of any other thieves. |
---|
829 | In step 2 a thief attempts to write @0p@ to one of their queue pointers. |
---|
830 | This queue pointer cannot be @0p@. |
---|
831 | As stated above, @my_queue@ is never equal to @0p@ since thieves will only write @0p@ to queue pointers from their own queue range and all worker's queue ranges are disjoint. |
---|
832 | As such step 2 upholds the invariant since in a failure case no write occurs, and in the success case, the value of the queue pointer is guaranteed to not be 0p. |
---|
833 | In step 3 the thief attempts to write @my_queue@ to the victim's queue pointer. |
---|
834 | If the current value of the victim's queue pointer is @0p@, then the CAS will fail since @vic_queue@ cannot be equal to @0p@ because of the check in step 1. |
---|
835 | Therefore in the success case where the @CAS@ succeeds, the value of the victim's queue pointer must not be @0p@. |
---|
836 | As such, the write will never overwrite a value of @0p@, hence the invariant is held in the @CAS@ of step 3. |
---|
837 | The write back to the thief's queue pointer that happens in the failure case of step three and in step 4 hold the invariant since they are the subsequent write to a @0p@ queue pointer and they are being set by the same thief that set the pointer to @0p@. |
---|
838 | |
---|
839 | Given this informal proof of invariance it can be shown that the successful swap is correct. |
---|
840 | Once a thief atomically sets their queue pointer to be @0p@ in step 2, the invariant guarantees that pointer will not change. |
---|
841 | As such, in the success case step 3 it is known that the value of the victim's queue pointer that was overwritten must be @vic_queue@ due to the use of @CAS@. |
---|
842 | Given that pointers all have unique memory locations, this first write of the successful swap is correct since it can only occur when the pointer has not changed. |
---|
843 | By the invariant the write back in the successful case is correct since no other worker can write to the @0p@ pointer. |
---|
844 | |
---|
845 | In the failed case the outcome is correct in steps 1 and 2 since no writes have occurred so the program state is unchanged. |
---|
846 | In the failed case of step 3 the program state is safely restored to its state it had prior to the @0p@ write in step 2, thanks to the invariant that makes the write back to the @0p@ pointer safe. |
---|
847 | |
---|
848 | \begin{comment} |
---|
849 | \subsection{Stealing Guarantees} |
---|
850 | Given that the stealing operation can potentially fail, it is important to discuss the guarantees provided by the stealing implementation. |
---|
851 | Given a set of $N$ swaps a set of connected directed graphs can be constructed where each vertex is a queue and each edge is a swap directed from a thief queue to a victim queue. |
---|
852 | Since each thief can only steal from one victim at a time, each vertex can only have at most one outgoing edge. |
---|
853 | A corollary that can be drawn from this, is that there are at most $V$ edges in this constructed set of connected directed graphs, where $V$ is the total number of vertices. |
---|
854 | |
---|
855 | \begin{figure} |
---|
856 | \begin{center} |
---|
857 | \input{diagrams/M_to_one_swap.tikz} |
---|
858 | \end{center} |
---|
859 | \caption{Graph of $M$ thieves swapping with one victim.} |
---|
860 | \label{f:M_one_swap} |
---|
861 | \end{figure} |
---|
862 | |
---|
863 | \begin{theorem} |
---|
864 | Given $M$ thieves queues all attempting to swap with one victim queue, and no other swaps occurring that involve these queues, at least one swap is guaranteed to succeed. |
---|
865 | \end{theorem}\label{t:one_vic} |
---|
866 | A graph of the $M$ thieves swapping with one victim discussed in this theorem is presented in Figure~\ref{f:M_one_swap}. |
---|
867 | \\ |
---|
868 | First it is important to state that a thief will not attempt to steal from themselves. |
---|
869 | As such, the victim here is not also a thief. |
---|
870 | Stepping through the code in \ref{c:swap}, for all thieves steps 0-1 succeed since the victim is not stealing and will have no queue pointers set to be @0p@. |
---|
871 | Similarly for all thieves step 2 will succeed since no one is stealing from any of the thieves. |
---|
872 | In step 3 the first thief to @CAS@ will win the race and successfully swap the queue pointer. |
---|
873 | Since it is the first one to @CAS@ and @CAS@ is atomic, there is no way for the @CAS@ to fail since no other thief could have written to the victim's queue pointer and the victim did not write to the pointer since they aren't stealing. |
---|
874 | Hence at least one swap is guaranteed to succeed in this case. |
---|
875 | |
---|
876 | \begin{figure} |
---|
877 | \begin{center} |
---|
878 | \input{diagrams/chain_swap.tikz} |
---|
879 | \end{center} |
---|
880 | \caption{Graph of a chain of swaps.} |
---|
881 | \label{f:chain_swap} |
---|
882 | \end{figure} |
---|
883 | |
---|
884 | \begin{theorem} |
---|
885 | Given $M$ > 1, ordered queues pointers all attempting to swap with the queue in front of them in the ordering, except the first queue, and no other swaps occurring that involve these queues, at least one swap is guaranteed to succeed. |
---|
886 | \end{theorem}\label{t:vic_chain} |
---|
887 | A graph of the chain of swaps discussed in this theorem is presented in Figure~\ref{f:chain_swap}. |
---|
888 | \\ |
---|
889 | This is a proof by contradiction. |
---|
890 | Assume no swaps occur. |
---|
891 | Then all thieves must have failed at step 1, step 2 or step 3. |
---|
892 | For a given thief $b$ to fail at step 1, thief $b + 1$ must have succeeded at step 2 before $b$ executes step 0. |
---|
893 | Hence, not all thieves can fail at step 1. |
---|
894 | Furthermore if a thief $b$ fails at step 1 it logically splits the chain into two subchains $0 <- b$ and $b + 1 <- M - 1$, where $b$ has become solely a victim since its swap has failed and it did not modify any state. |
---|
895 | There must exist at least one chain containing two or more queues after since it is impossible for a split to occur both before and after a thief, since that requires failing at step 1 and succeeding at step 2. |
---|
896 | Hence, without loss of generality, whether thieves succeed or fail at step 1, this proof can proceed inductively. |
---|
897 | |
---|
898 | For a given thief $i$ to fail at step 2, it means that another thief $j$ had to have written to $i$'s queue pointer between $i$'s step 0 and step 2. |
---|
899 | The only way for $j$ to write to $i$'s queue pointer would be if $j$ was stealing from $i$ and had successfully finished step 3. |
---|
900 | If $j$ finished step 3 then the at least one swap was successful. |
---|
901 | Therefore all thieves did not fail at step 2. |
---|
902 | Hence all thieves must successfully complete step 2 and fail at step 3. |
---|
903 | However, since the first worker, thief $0$, is solely a victim and not a thief, it does not change the state of any of its queue pointers. |
---|
904 | Hence, in this case thief $1$ will always succeed in step 3 if all thieves succeed in step 2. |
---|
905 | Thus, by contradiction with the earlier assumption that no swaps occur, at least one swap must succeed. |
---|
906 | |
---|
907 | % \raisebox{.1\height}{} |
---|
908 | \begin{figure} |
---|
909 | \centering |
---|
910 | \begin{tabular}{l|l} |
---|
911 | \subfloat[Cyclic Swap Graph]{\label{f:cyclic_swap}\input{diagrams/cyclic_swap.tikz}} & |
---|
912 | \subfloat[Acyclic Swap Graph]{\label{f:acyclic_swap}\input{diagrams/acyclic_swap.tikz}} |
---|
913 | \end{tabular} |
---|
914 | \caption{Illustrations of cyclic and acyclic swap graphs.} |
---|
915 | \end{figure} |
---|
916 | |
---|
917 | \begin{theorem} |
---|
918 | Given a set of $M > 1$ swaps occurring that form a single directed connected graph. |
---|
919 | At least one swap is guaranteed to succeed if and only if the graph does not contain a cycle. |
---|
920 | \end{theorem}\label{t:vic_cycle} |
---|
921 | Representations of cyclic and acyclic swap graphs discussed in this theorem are presented in Figures~\ref{f:cyclic_swap} and \ref{f:acyclic_swap}. |
---|
922 | \\ |
---|
923 | First the reverse direction is proven. |
---|
924 | If the graph does not contain a cycle, then there must be at least one successful swap. |
---|
925 | Since the graph contains no cycles and is finite in size, then there must be a vertex $A$ with no outgoing edges. |
---|
926 | The graph can then be formulated as a tree with $A$ at the top since each node only has at most one outgoing edge and there are no cycles. |
---|
927 | The forward direction is proven by contradiction in a similar fashion to \ref{t:vic_chain}. |
---|
928 | Assume no swaps occur. |
---|
929 | Similar to \ref{t:vic_chain}, this graph can be inductively split into subgraphs of the same type by failure at step 1, so the proof proceeds without loss of generality. |
---|
930 | Similar to \ref{t:vic_chain} the conclusion is drawn that all thieves must successfully complete step 2 for no swaps to occur, since for step 2 to fail, a different thief has to successfully complete step 3, which would imply a successful swap. |
---|
931 | Hence, the only way forward is to assume all thieves successfully complete step 2. |
---|
932 | Hence for there to be no swaps all thieves must fail step 3. |
---|
933 | However, since $A$ has no outgoing edges, since the graph is connected there must be some $K$ such that $K < M - 1$ thieves are attempting to swap with $A$. |
---|
934 | Since all $K$ thieves have passed step 2, similar to \ref{t:one_vic} the first one of the $K$ thieves to attempt step 3 is guaranteed to succeed. |
---|
935 | Thus, by contradiction with the earlier assumption that no swaps occur, if the graph does not contain a cycle, at least one swap must succeed. |
---|
936 | |
---|
937 | The forward direction is proven by contrapositive. |
---|
938 | If the graph contains a cycle then there exists a situation where no swaps occur. |
---|
939 | This situation is constructed. |
---|
940 | Since all vertices have at most one outgoing edge the cycle must be directed. |
---|
941 | Furthermore, since the graph contains a cycle all vertices in the graph must have exactly one outgoing edge. |
---|
942 | This is shown through construction of an arbitrary cyclic graph. |
---|
943 | The graph contains a directed cycle by definition, so the construction starts with $T$ vertices in a directed cycle. |
---|
944 | Since the graph is connected, and each vertex has at most one outgoing edge, none of the vertices in the cycle have available outgoing edges to accommodate new vertices with no outgoing edges. |
---|
945 | Any vertices added to the graph must have an outgoing edge to connect, leaving the resulting graph with no available outgoing edges. |
---|
946 | Thus, by induction all vertices in the graph must have exactly one outgoing edge. |
---|
947 | Hence all vertices are thief queues. |
---|
948 | Now consider the case where all thieves successfully complete step 0-1, and then they all complete step 2. |
---|
949 | At this point all thieves are attempting to swap with a queue pointer whose value has changed to @0p@. |
---|
950 | If all thieves attempt the @CAS@ before any write backs, then they will all fail. |
---|
951 | Thus, by contrapositive, if the graph contains a cycle then there exists a situation where no swaps occur. |
---|
952 | Hence, at least one swap is guaranteed to succeed if and only if the graph does not contain a cycle. |
---|
953 | \end{comment} |
---|
954 | |
---|
955 | % C_TODO: go through and use \paragraph to format to make it look nicer |
---|
956 | \subsection{Victim Selection}\label{s:victimSelect} |
---|
957 | In any work stealing algorithm thieves have some heuristic to determine which victim to choose from. |
---|
958 | Choosing this algorithm is difficult and can have implications on performance. |
---|
959 | There is no one selection heuristic that is known to be the best on all workloads. |
---|
960 | Recent work focuses on locality aware scheduling in actor systems\cite{barghi18}\cite{wolke17}. |
---|
961 | However, while locality aware scheduling provides good performance on some workloads, something as simple as randomized selection performs better on other workloads\cite{barghi18}. |
---|
962 | Since locality aware scheduling has been explored recently, this work introduces a heuristic called \textbf{longest victim} and compares it to randomized work stealing. |
---|
963 | The longest victim heuristic maintains a timestamp per executor threads that is updated every time a worker attempts to steal work. |
---|
964 | Thieves then attempt to steal from the thread with the oldest timestamp. |
---|
965 | This means that if two thieves look to steal at the same time, they likely will attempt to steal from the same victim. |
---|
966 | This does increase the chance at contention between thieves, however given that workers have multiple queues under them, often in the tens or hundreds of queues per worker it is rare for two queues to attempt so steal the same queue. |
---|
967 | Furthermore in the case they attempt to steal the same queue at least one of them is guaranteed to successfully steal the queue as shown in Theorem \ref{t:one_vic}. |
---|
968 | Additionally, the longest victim heuristic makes it very improbable that the no swap scenario presented in Theorem \ref{t:vic_cycle} manifests. |
---|
969 | Given the longest victim heuristic, for a cycle to manifest it would require all workers to attempt to steal in a short timeframe. |
---|
970 | This is the only way that more than one thief could choose another thief as a victim, since timestamps are only updated upon attempts to steal. |
---|
971 | In this case, the probability of lack of any successful swaps is a non issue, since it is likely that these steals were not important if all workers are trying to steal. |
---|
972 | |
---|
973 | \section{Safety and Productivity}\label{s:SafetyProductivity} |
---|
974 | \CFA's actor system comes with a suite of safety and productivity features. |
---|
975 | Most of these features are present in \CFA's debug mode, but are removed when code is compiled in nodebug mode. |
---|
976 | The suit of features include the following. |
---|
977 | |
---|
978 | \begin{itemize} |
---|
979 | \item Static-typed message sends. |
---|
980 | If an actor does not support receiving a given message type, the actor program is rejected at compile time, allowing unsupported messages to never be sent to actors. |
---|
981 | \item Detection of message sends to Finished/Destroyed/Deleted actors. |
---|
982 | All actors have a ticket that assigns them to a respective queue. |
---|
983 | The maximum integer value of the ticket is reserved to indicate that an actor is dead, and subsequent message sends result in an error. |
---|
984 | \item Actors made before the executor can result in undefined behaviour since an executor needs to be created beforehand so it can give out the tickets to actors. |
---|
985 | As such, this is detected and an error is printed. |
---|
986 | \item When an executor is created, the queues are handed out to executor threads in round robin order. |
---|
987 | If there are fewer queues than executor threads, then some workers will spin and never do any work. |
---|
988 | There is no reasonable use case for this behaviour so an error is printed if the number of queues is fewer than the number of executor threads. |
---|
989 | \item A warning is printed when messages are deallocated without being sent. |
---|
990 | Since the @Finished@ allocation status is unused for messages, it is used internally to detect if a message has been sent. |
---|
991 | Deallocating a message without sending it could indicate to a user that they are touching freed memory later, or it could point out extra allocations that could be removed. |
---|
992 | \item Detection of messages sent but not received |
---|
993 | As discussed in Section~\ref{s:executor}, once all actors have terminated shutdown is communicated to executor threads via a status flag. Upon termination the executor threads check their queues to see if any contain messages. If they do, an error is reported. Messages being sent but not received means that their allocation action did not occur and their payload was not delivered. Missing the allocation action can lead to memory leaks and missed payloads can cause unpredictable behaviour. Detecting this can indicate a race or logic error in the user's code. |
---|
994 | \end{itemize} |
---|
995 | |
---|
996 | In addition to these features, \CFA's actor system comes with a suite of statistics that can be toggled on and off. |
---|
997 | These statistics have minimal impact on the actor system's performance since they are counted on a per executor threads basis. |
---|
998 | During shutdown of the actor system they are aggregated, ensuring that the only atomic instructions used by the statistics counting happen at shutdown. |
---|
999 | The statistics measured are as follows. |
---|
1000 | |
---|
1001 | \begin{description} |
---|
1002 | \item[\LstBasicStyle{\textbf{Actors Created}}] |
---|
1003 | Actors created. |
---|
1004 | Includes both actors made by the main and ones made by other actors. |
---|
1005 | \item[\LstBasicStyle{\textbf{Messages Sent}}] |
---|
1006 | Messages sent and received. |
---|
1007 | Includes termination messages send to the executor threads. |
---|
1008 | \item[\LstBasicStyle{\textbf{Gulps}}] |
---|
1009 | Gulps that occurred across the executor threads. |
---|
1010 | \item[\LstBasicStyle{\textbf{Average Gulp Size}}] |
---|
1011 | Average number of messages in a gulped queue. |
---|
1012 | \item[\LstBasicStyle{\textbf{Missed gulps}}] |
---|
1013 | Occurrences where a worker missed a gulp due to the concurrent queue processing by another worker. |
---|
1014 | \item[\LstBasicStyle{\textbf{Steal attempts}}] |
---|
1015 | Worker threads attempts to steal work. |
---|
1016 | |
---|
1017 | \item[\LstBasicStyle{\textbf{Steal failures (no candidates)}}] |
---|
1018 | Work stealing failures due to selected victim not having any non empty or non-being-processed queues. |
---|
1019 | \item[\LstBasicStyle{\textbf{Steal failures (failed swaps)}}] |
---|
1020 | Work stealing failures due to the two stage atomic swap failing. |
---|
1021 | \item[\LstBasicStyle{\textbf{Messages stolen}}] |
---|
1022 | Aggregate of the number of messages in queues as they were stolen. |
---|
1023 | \item[\LstBasicStyle{\textbf{Average steal size}}] |
---|
1024 | Average number of messages in a stolen queue. |
---|
1025 | \end{description} |
---|
1026 | |
---|
1027 | These statistics enable a user of \CFA's actor system to make informed choices about how to configure their executor, or how to structure their actor program. |
---|
1028 | For example, if there is a lot of messages being stolen relative to the number of messages sent, it could indicate to a user that their workload is heavily imbalanced across executor threads. |
---|
1029 | In another example, if the average gulp size is very high, it could indicate that the executor could use more queue sharding. |
---|
1030 | |
---|
1031 | Another productivity feature that is included is a group of poison-pill messages. |
---|
1032 | Poison-pill messages are common across actor systems, including Akka and ProtoActor \cite{Akka,ProtoActor}. |
---|
1033 | Poison-pill messages inform an actor to terminate. |
---|
1034 | In \CFA, due to the allocation of actors and lack of garbage collection, there needs to be a suite of poison-pills. |
---|
1035 | The messages that \CFA provides are @DeleteMsg@, @DestroyMsg@, and @FinishedMsg@. |
---|
1036 | These messages are supported on all actor types via inheritance and when sent to an actor, the actor takes the corresponding allocation action after receiving the message. |
---|
1037 | Note that any pending messages to the actor will still be sent. |
---|
1038 | It is still the user's responsibility to ensure that an actor does not receive any messages after termination. |
---|
1039 | |
---|
1040 | \section{Performance}\label{s:actor_perf} |
---|
1041 | The performance of \CFA's actor system is tested using a suite of microbenchmarks, and compared with other actor systems. |
---|
1042 | Most of the benchmarks are the same as those presented in \ref{}, with a few additions. |
---|
1043 | % C_TODO cite actor paper |
---|
1044 | At the time of this work the versions of the actor systems are as follows. |
---|
1045 | \CFA 1.0, \uC 7.0.0, Akka Typed 2.7.0, CAF 0.18.6, and ProtoActor-Go v0.0.0-20220528090104-f567b547ea07. |
---|
1046 | Akka Classic is omitted as Akka Typed is their newest version and seems to be the direction they are headed in. |
---|
1047 | The experiments are run on |
---|
1048 | \begin{list}{\arabic{enumi}.}{\usecounter{enumi}\topsep=5pt\parsep=5pt\itemsep=0pt} |
---|
1049 | \item |
---|
1050 | Supermicro SYS--6029U--TR4 Intel Xeon Gold 5220R 24--core socket, hyper-threading $\times$ 2 sockets (48 process\-ing units) 2.2GHz, running Linux v5.8.0--59--generic |
---|
1051 | \item |
---|
1052 | Supermicro AS--1123US--TR4 AMD EPYC 7662 64--core socket, hyper-threading $\times$ 2 sockets (256 processing units) 2.0 GHz, running Linux v5.8.0--55--generic |
---|
1053 | \end{list} |
---|
1054 | |
---|
1055 | The benchmarks are run on up to 48 cores. |
---|
1056 | On the Intel, when going beyond 24 cores there is the choice to either hop sockets or to use hyperthreads. |
---|
1057 | Either choice will cause a blip in performance trends, which can be seen in the following performance figures. |
---|
1058 | On the Intel the choice was made to hyperthread instead of hopping sockets for experiments with more than 24 cores. |
---|
1059 | |
---|
1060 | All benchmarks presented are run 5 times and the median is taken. |
---|
1061 | Error bars showing the 95\% confidence intervals are drawn on each point on the graphs. |
---|
1062 | If the confidence bars are small enough, they may be obscured by the point. |
---|
1063 | In this section \uC will be compared to \CFA frequently, as the actor system in \CFA was heavily based off \uC's actor system. |
---|
1064 | As such the performance differences that arise are largely due to the contributions of this work. |
---|
1065 | |
---|
1066 | \begin{table}[t] |
---|
1067 | \centering |
---|
1068 | \setlength{\extrarowheight}{2pt} |
---|
1069 | \setlength{\tabcolsep}{5pt} |
---|
1070 | |
---|
1071 | \caption{Static Actor/Message Performance: message send, program memory} |
---|
1072 | \label{t:StaticActorMessagePerformance} |
---|
1073 | \begin{tabular}{*{5}{r|}r} |
---|
1074 | & \multicolumn{1}{c|}{\CFA (100M)} & \multicolumn{1}{c|}{CAF (10M)} & \multicolumn{1}{c|}{Akka (100M)} & \multicolumn{1}{c|}{\uC (100M)} & \multicolumn{1}{c@{}}{ProtoActor (100M)} \\ |
---|
1075 | \hline |
---|
1076 | AMD & \input{data/pykeSendStatic} \\ |
---|
1077 | \hline |
---|
1078 | Intel & \input{data/nasusSendStatic} |
---|
1079 | \end{tabular} |
---|
1080 | |
---|
1081 | \bigskip |
---|
1082 | |
---|
1083 | \caption{Dynamic Actor/Message Performance: message send, program memory} |
---|
1084 | \label{t:DynamicActorMessagePerformance} |
---|
1085 | |
---|
1086 | \begin{tabular}{*{5}{r|}r} |
---|
1087 | & \multicolumn{1}{c|}{\CFA (20M)} & \multicolumn{1}{c|}{CAF (2M)} & \multicolumn{1}{c|}{Akka (2M)} & \multicolumn{1}{c|}{\uC (20M)} & \multicolumn{1}{c@{}}{ProtoActor (2M)} \\ |
---|
1088 | \hline |
---|
1089 | AMD & \input{data/pykeSendDynamic} \\ |
---|
1090 | \hline |
---|
1091 | Intel & \input{data/nasusSendDynamic} |
---|
1092 | \end{tabular} |
---|
1093 | \end{table} |
---|
1094 | |
---|
1095 | \subsection{Message Sends} |
---|
1096 | Message sending is the key component of actor communication. |
---|
1097 | As such latency of a single message send is the fundamental unit of fast-path performance for an actor system. |
---|
1098 | The following two microbenchmarks evaluate the average latency for a static actor/message send and a dynamic actor/message send. |
---|
1099 | Static and dynamic refer to the allocation of the message and actor. |
---|
1100 | In the static send benchmark a message and actor are allocated once and then the message is sent to the same actor repeatedly until it has been sent 100 million (100M) times. |
---|
1101 | The average latency per message send is then calculated by dividing the duration by the number of sends. |
---|
1102 | This benchmark evaluates the cost of message sends in the actor use case where all actors and messages are allocated ahead of time and do not need to be created dynamically during execution. |
---|
1103 | The CAF static send benchmark only sends a message 10M times to avoid extensively long run times. |
---|
1104 | |
---|
1105 | In the dynamic send benchmark the same experiment is performed, with the change that with each send a new actor and message is allocated. |
---|
1106 | This evaluates the cost of message sends in the other common actor pattern where actors and message are created on the fly as the actor program tackles a workload of variable or unknown size. |
---|
1107 | Since dynamic sends are more expensive, this benchmark repeats the actor/message creation and send 20M times (\uC, \CFA), or 2M times (Akka, CAF, ProtoActor), to give an appropriate benchmark duration. |
---|
1108 | |
---|
1109 | The results from the static/dynamic send benchmarks are shown in Figures~\ref{t:StaticActorMessagePerformance} and \ref{t:DynamicActorMessagePerformance} respectively. |
---|
1110 | \CFA leads the charts in both benchmarks, largely due to the copy queue removing the majority of the envelope allocations. |
---|
1111 | Additionally, the receive of all messages sent in \CFA is statically known and is determined via a function pointer cast, which incurs a compile-time cost. |
---|
1112 | All the other systems use their virtual system to find the correct behaviour at message send. |
---|
1113 | This requires two virtual dispatch operations, which is an additional runtime send cost that \CFA does not have. |
---|
1114 | Note that Akka also statically checks message sends, but still uses their virtual system at runtime. |
---|
1115 | In the static send benchmark all systems except CAF have static send costs that are in the same ballpark, only varying by ~70ns. |
---|
1116 | In the dynamic send benchmark all systems experience slower message sends, as expected due to the extra allocations. |
---|
1117 | However, Akka and ProtoActor, slow down by a more significant margin than the \uC and \CFA. |
---|
1118 | This is likely a result of Akka and ProtoActor's garbage collection, which can suffer from hits in performance for allocation heavy workloads, whereas \uC and \CFA have explicit allocation/deallocation. |
---|
1119 | |
---|
1120 | \subsection{Work Stealing} |
---|
1121 | \CFA's actor system has a work stealing mechanism which uses the longest victim heuristic, introduced in Section~ref{s:victimSelect}. |
---|
1122 | In this performance section, \CFA with the longest victim heuristic is compared with other actor systems on the benchmark suite, and is separately compared with vanilla non-stealing \CFA and \CFA with randomized work stealing. |
---|
1123 | |
---|
1124 | \begin{figure} |
---|
1125 | \centering |
---|
1126 | \subfloat[AMD \CFA Balance-One Benchmark]{ |
---|
1127 | \resizebox{0.5\textwidth}{!}{\input{figures/nasusCFABalance-One.pgf}} |
---|
1128 | \label{f:BalanceOneAMD} |
---|
1129 | } |
---|
1130 | \subfloat[Intel \CFA Balance-One Benchmark]{ |
---|
1131 | \resizebox{0.5\textwidth}{!}{\input{figures/pykeCFABalance-One.pgf}} |
---|
1132 | \label{f:BalanceOneIntel} |
---|
1133 | } |
---|
1134 | \caption{The balance-one benchmark comparing stealing heuristics (lower is better).} |
---|
1135 | \end{figure} |
---|
1136 | |
---|
1137 | \begin{figure} |
---|
1138 | \centering |
---|
1139 | \subfloat[AMD \CFA Balance-Multi Benchmark]{ |
---|
1140 | \resizebox{0.5\textwidth}{!}{\input{figures/nasusCFABalance-Multi.pgf}} |
---|
1141 | \label{f:BalanceMultiAMD} |
---|
1142 | } |
---|
1143 | \subfloat[Intel \CFA Balance-Multi Benchmark]{ |
---|
1144 | \resizebox{0.5\textwidth}{!}{\input{figures/pykeCFABalance-Multi.pgf}} |
---|
1145 | \label{f:BalanceMultiIntel} |
---|
1146 | } |
---|
1147 | \caption{The balance-multi benchmark comparing stealing heuristics (lower is better).} |
---|
1148 | \end{figure} |
---|
1149 | |
---|
1150 | There are two benchmarks in which \CFA's work stealing is solely evaluated. |
---|
1151 | The main goal of introducing work stealing to \CFA's actor system is to eliminate the pathological unbalanced cases that can present themselves in a system without some form of load balancing. |
---|
1152 | The following two microbenchmarks construct two such pathological cases, and compare the work stealing variations of \CFA. |
---|
1153 | The balance benchmarks adversarially takes advantage of the round robin assignment of actors to load all actors that will do work on specific cores and create 'dummy' actors that terminate after a single message send on all other cores. |
---|
1154 | The workload on the loaded cores is the same as the executor benchmark described in \ref{s:executorPerf}, but with fewer rounds. |
---|
1155 | The balance-one benchmark loads all the work on a single core, whereas the balance-multi loads all the work on half the cores (every other core). |
---|
1156 | Given this layout, one expects the ideal speedup of work stealing in the balance-one case to be $N / N - 1$ where $N$ is the number of threads. |
---|
1157 | In the balance-multi case the ideal speedup is 0.5. |
---|
1158 | Note that in the balance-one benchmark the workload is fixed so decreasing runtime is expected. |
---|
1159 | In the balance-multi experiment, the workload increases with the number of cores so an increasing or constant runtime is expected. |
---|
1160 | |
---|
1161 | On both balance microbenchmarks slightly less than ideal speedup compared to the non stealing variation is achieved by both the random and longest victim stealing heuristics. |
---|
1162 | On the balance-multi benchmark \ref{f:BalanceMultiAMD},\ref{f:BalanceMultiIntel} the random heuristic outperforms the longest victim. |
---|
1163 | This is likely a result of the longest victim heuristic having a higher stealing cost as it needs to maintain timestamps and look at all timestamps before stealing. |
---|
1164 | Additionally, a performance cost can be observed when hyperthreading kicks in in Figure~\ref{f:BalanceMultiIntel}. |
---|
1165 | |
---|
1166 | In the balance-one benchmark on AMD \ref{f:BalanceOneAMD}, the performance bottoms out at 32 cores onwards likely due to the amount of work becoming less than the cost to steal it and move it across cores and cache. |
---|
1167 | On Intel \ref{f:BalanceOneIntel}, above 32 cores the performance gets worse for all variants due to hyperthreading. |
---|
1168 | Note that the non stealing variation of balance-one will slow down marginally as the cores increase due to having to create more dummy actors on the inactive cores during startup. |
---|
1169 | |
---|
1170 | \subsection{Executor}\label{s:executorPerf} |
---|
1171 | The microbenchmarks in this section are designed to stress the executor. |
---|
1172 | The executor is the scheduler of an actor system and is responsible for organizing the interaction of executor threads to service the needs of a workload. |
---|
1173 | In the executor benchmark, 40'000 actors are created and assigned a group. |
---|
1174 | Each group of actors is a group of 100 actors who send and receive 100 messages from all other actors in their group. |
---|
1175 | Each time an actor completes all their sends and receives, they are done a round. |
---|
1176 | After all groups have completed 400 rounds the system terminates. |
---|
1177 | This microbenchmark is designed to flood the executor with a large number of messages flowing between actors. |
---|
1178 | Given there is no work associated with each message, other than sending more messages, the intended bottleneck of this experiment is the executor message send process. |
---|
1179 | |
---|
1180 | \begin{figure} |
---|
1181 | \centering |
---|
1182 | \subfloat[AMD Executor Benchmark]{ |
---|
1183 | \resizebox{0.5\textwidth}{!}{\input{figures/nasusExecutor.pgf}} |
---|
1184 | \label{f:ExecutorAMD} |
---|
1185 | } |
---|
1186 | \subfloat[Intel Executor Benchmark]{ |
---|
1187 | \resizebox{0.5\textwidth}{!}{\input{figures/pykeExecutor.pgf}} |
---|
1188 | \label{f:ExecutorIntel} |
---|
1189 | } |
---|
1190 | \caption{The executor benchmark comparing actor systems (lower is better).} |
---|
1191 | \end{figure} |
---|
1192 | |
---|
1193 | The results of the executor benchmark in Figures~\ref{f:ExecutorIntel} and \ref{f:ExecutorAMD} show \CFA with the lowest runtime relative to its peers. |
---|
1194 | The difference in runtime between \uC and \CFA is largely due to the usage of the copy queue described in Section~\ref{s:copyQueue}. |
---|
1195 | The copy queue both reduces and consolidates allocations, heavily reducing contention on the memory allocator. |
---|
1196 | Additionally, due to the static typing in \CFA's actor system, it is able to get rid of expensive dynamic casts that occur in \uC to discriminate messages by type. |
---|
1197 | Note that dynamic casts are usually not very expensive, but relative to the high performance of the rest of the implementation of the \uC actor system, the cost is significant. |
---|
1198 | |
---|
1199 | \begin{figure} |
---|
1200 | \centering |
---|
1201 | \subfloat[AMD \CFA Executor Benchmark]{ |
---|
1202 | \resizebox{0.5\textwidth}{!}{\input{figures/nasusCFAExecutor.pgf}} |
---|
1203 | \label{f:cfaExecutorAMD} |
---|
1204 | } |
---|
1205 | \subfloat[Intel \CFA Executor Benchmark]{ |
---|
1206 | \resizebox{0.5\textwidth}{!}{\input{figures/pykeCFAExecutor.pgf}} |
---|
1207 | \label{f:cfaExecutorIntel} |
---|
1208 | } |
---|
1209 | \caption{Executor benchmark comparing \CFA stealing heuristics (lower is better).} |
---|
1210 | \end{figure} |
---|
1211 | |
---|
1212 | When comparing the \CFA stealing heuristics in Figure~\ref{f:cfaExecutorAMD} it can be seen that the random heuristic falls slightly behind the other two, but in Figure~\ref{f:cfaExecutorIntel} the runtime of all heuristics are nearly identical to each other. |
---|
1213 | |
---|
1214 | \begin{figure} |
---|
1215 | \centering |
---|
1216 | \subfloat[AMD Repeat Benchmark]{ |
---|
1217 | \resizebox{0.5\textwidth}{!}{\input{figures/nasusRepeat.pgf}} |
---|
1218 | \label{f:RepeatAMD} |
---|
1219 | } |
---|
1220 | \subfloat[Intel Repeat Benchmark]{ |
---|
1221 | \resizebox{0.5\textwidth}{!}{\input{figures/pykeRepeat.pgf}} |
---|
1222 | \label{f:RepeatIntel} |
---|
1223 | } |
---|
1224 | \caption{The repeat benchmark comparing actor systems (lower is better).} |
---|
1225 | \end{figure} |
---|
1226 | |
---|
1227 | The repeat microbenchmark also evaluates the executor. |
---|
1228 | It stresses the executor's ability to withstand contention on queues, as it repeatedly fans out messages from a single client to 100000 servers who then all respond to the client. |
---|
1229 | After this scatter and gather repeats 200 times the benchmark terminates. |
---|
1230 | The messages from the servers to the client will likely all come in on the same queue, resulting in high contention. |
---|
1231 | As such this benchmark will not scale with the number of processors, since more processors will result in higher contention. |
---|
1232 | In Figure~\ref{f:RepeatAMD} we can see that \CFA performs well compared to \uC, however by less of a margin than the executor benchmark. |
---|
1233 | One factor in this result is that the contention on the queues poses a significant bottleneck. |
---|
1234 | As such the gains from using the copy queue are much less apparent. |
---|
1235 | |
---|
1236 | \begin{figure} |
---|
1237 | \centering |
---|
1238 | \subfloat[AMD \CFA Repeat Benchmark]{ |
---|
1239 | \resizebox{0.5\textwidth}{!}{\input{figures/nasusCFARepeat.pgf}} |
---|
1240 | \label{f:cfaRepeatAMD} |
---|
1241 | } |
---|
1242 | \subfloat[Intel \CFA Repeat Benchmark]{ |
---|
1243 | \resizebox{0.5\textwidth}{!}{\input{figures/pykeCFARepeat.pgf}} |
---|
1244 | \label{f:cfaRepeatIntel} |
---|
1245 | } |
---|
1246 | \caption{The repeat benchmark comparing \CFA stealing heuristics (lower is better).} |
---|
1247 | \end{figure} |
---|
1248 | |
---|
1249 | In Figure~\ref{f:RepeatIntel} \uC and \CFA are very comparable. |
---|
1250 | In comparison with the other systems \uC does well on the repeat benchmark since it does not have work stealing. |
---|
1251 | The client of this experiment is long running and maintains a lot of state, as it needs to know the handles of all the servers. |
---|
1252 | When stealing the client or its respective queue (in \CFA's inverted model), moving the client incurs a high cost due to cache invalidation. |
---|
1253 | As such stealing the client can result in a hit in performance. |
---|
1254 | |
---|
1255 | This result is shown in Figure~\ref{f:cfaRepeatAMD} and \ref{f:cfaRepeatIntel} where the no-stealing version of \CFA performs better than both stealing variations. |
---|
1256 | In particular on the Intel machine in Figure~\ref{f:cfaRepeatIntel}, the cost of stealing is higher, which can be seen in the vertical shift of Akka, CAF and \CFA results in Figure~\ref{f:RepeatIntel} (\uC and ProtoActor do not have work stealing). |
---|
1257 | The shift for CAF is particularly large, which further supports the hypothesis that CAF's work stealing is particularly eager. |
---|
1258 | In both the executor and the repeat benchmark CAF performs poorly. |
---|
1259 | It is hypothesized that CAF has an aggressive work stealing algorithm, that eagerly attempts to steal. |
---|
1260 | This results in poor performance in benchmarks with small messages containing little work per message. |
---|
1261 | On the other hand, in \ref{f:MatrixAMD} CAF performs much better since each message has a large amount of work, and few messages are sent, so the eager work stealing allows for the clean up of loose ends to occur faster. |
---|
1262 | This hypothesis stems from experimentation with \CFA. |
---|
1263 | CAF uses a randomized work stealing heuristic. |
---|
1264 | In \CFA if the system is tuned so that it steals work much more eagerly with a randomized it was able to replicate the results that CAF achieves in the matrix benchmark, but this tuning performed much worse on all other microbenchmarks that we present, since they all perform a small amount of work per message. |
---|
1265 | |
---|
1266 | \begin{table}[t] |
---|
1267 | \centering |
---|
1268 | \setlength{\extrarowheight}{2pt} |
---|
1269 | \setlength{\tabcolsep}{5pt} |
---|
1270 | |
---|
1271 | \caption{Executor Program Memory High Watermark} |
---|
1272 | \label{t:ExecutorMemory} |
---|
1273 | \begin{tabular}{*{5}{r|}r} |
---|
1274 | & \multicolumn{1}{c|}{\CFA} & \multicolumn{1}{c|}{CAF} & \multicolumn{1}{c|}{Akka} & \multicolumn{1}{c|}{\uC} & \multicolumn{1}{c@{}}{ProtoActor} \\ |
---|
1275 | \hline |
---|
1276 | AMD & \input{data/pykeExecutorMem} \\ |
---|
1277 | \hline |
---|
1278 | Intel & \input{data/nasusExecutorMem} |
---|
1279 | \end{tabular} |
---|
1280 | \end{table} |
---|
1281 | |
---|
1282 | Figure~\ref{t:ExecutorMemory} shows the high memory watermark of the actor systems when running the executor benchmark on 48 cores. |
---|
1283 | \CFA has a high watermark relative to the other non-garbage collected systems \uC, and CAF. |
---|
1284 | This is a result of the copy queue data structure, as it will over-allocate storage and not clean up eagerly, whereas the per envelope allocations will always allocate exactly the amount of storage needed. |
---|
1285 | Despite having a higher watermark, the \CFA memory usage remains comparable to other non-garbage-collected systems. |
---|
1286 | |
---|
1287 | \subsection{Matrix Multiply} |
---|
1288 | The matrix benchmark evaluates the actor systems in a practical application, where actors concurrently multiplies two matrices. |
---|
1289 | The majority of the computation in this benchmark involves computing the final matrix, so this benchmark stresses the actor systems' ability to have actors run work, rather than stressing the executor or message sending system. |
---|
1290 | |
---|
1291 | Given $Z_{m,r} = X_{m,n} \cdot Y_{n,r}$, the matrix multiply is defined as: |
---|
1292 | \begin{displaymath} |
---|
1293 | X_{i,j} \cdot Y_{j,k} = \left( \sum_{c=1}^{j} X_{row,c}Y_{c,column} \right)_{i,k} |
---|
1294 | \end{displaymath} |
---|
1295 | |
---|
1296 | The benchmark uses input matrices $X$ and $Y$ that are both $3072$ by $3072$ in size. |
---|
1297 | An actor is made for each row of $X$ and is passed via message the information needed to calculate a row of the result matrix $Z$. |
---|
1298 | |
---|
1299 | |
---|
1300 | Given that the bottleneck of the benchmark is the computation of the result matrix, it follows that the results in Figures~\ref{f:MatrixAMD} and \ref{f:MatrixIntel} are clustered closer than other experiments. |
---|
1301 | In Figure~\ref{f:MatrixAMD} \uC and \CFA have identical performance and in Figure~\ref{f:MatrixIntel} \uC pulls ahead of \CFA after 24 cores likely due to costs associated with work stealing while hyperthreading. |
---|
1302 | As mentioned in \ref{s:executorPerf}, it is hypothesized that CAF performs better in this benchmark compared to others due to its eager work stealing implementation. |
---|
1303 | In Figures~\ref{f:cfaMatrixAMD} and \ref{f:cfaMatrixIntel} there is little negligible performance difference across \CFA stealing heuristics. |
---|
1304 | |
---|
1305 | \begin{figure} |
---|
1306 | \centering |
---|
1307 | \subfloat[AMD Matrix Benchmark]{ |
---|
1308 | \resizebox{0.5\textwidth}{!}{\input{figures/nasusMatrix.pgf}} |
---|
1309 | \label{f:MatrixAMD} |
---|
1310 | } |
---|
1311 | \subfloat[Intel Matrix Benchmark]{ |
---|
1312 | \resizebox{0.5\textwidth}{!}{\input{figures/pykeMatrix.pgf}} |
---|
1313 | \label{f:MatrixIntel} |
---|
1314 | } |
---|
1315 | \caption{The matrix benchmark comparing actor systems (lower is better).} |
---|
1316 | \end{figure} |
---|
1317 | |
---|
1318 | \begin{figure} |
---|
1319 | \centering |
---|
1320 | \subfloat[AMD \CFA Matrix Benchmark]{ |
---|
1321 | \resizebox{0.5\textwidth}{!}{\input{figures/nasusCFAMatrix.pgf}} |
---|
1322 | \label{f:cfaMatrixAMD} |
---|
1323 | } |
---|
1324 | \subfloat[Intel \CFA Matrix Benchmark]{ |
---|
1325 | \resizebox{0.5\textwidth}{!}{\input{figures/pykeCFAMatrix.pgf}} |
---|
1326 | \label{f:cfaMatrixIntel} |
---|
1327 | } |
---|
1328 | \caption{The matrix benchmark comparing \CFA stealing heuristics (lower is better).} |
---|
1329 | \end{figure} |
---|
1330 | |
---|
1331 | % Local Variables: % |
---|
1332 | % tab-width: 4 % |
---|
1333 | % End: % |
---|