- Timestamp:
- Jun 21, 2023, 2:38:55 AM (3 years ago)
- Branches:
- master, stuck-waitfor-destruct
- Children:
- 92355883
- Parents:
- 0b0a285 (diff), 2de175ce (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- doc/theses/colby_parsons_MMAth
- Files:
-
- 14 added
- 14 edited
-
Makefile (modified) (1 diff)
-
benchmarks/actors/cfa/balance.cfa (modified) (3 diffs)
-
benchmarks/actors/cfa/dynamic.cfa (modified) (2 diffs)
-
benchmarks/actors/cfa/executor.cfa (modified) (2 diffs)
-
benchmarks/actors/cfa/matrix.cfa (modified) (1 diff)
-
benchmarks/actors/cfa/repeat.cfa (modified) (4 diffs)
-
benchmarks/actors/cfa/static.cfa (modified) (2 diffs)
-
benchmarks/actors/plotData.py (modified) (1 diff)
-
benchmarks/channels/plotData.py (modified) (1 diff)
-
benchmarks/mutex_stmt/plotData.py (modified) (1 diff)
-
figures/nasus_Contend_2.pgf (added)
-
figures/nasus_Contend_4.pgf (added)
-
figures/nasus_Contend_8.pgf (added)
-
figures/nasus_Future.pgf (added)
-
figures/nasus_Spin_2.pgf (added)
-
figures/nasus_Spin_4.pgf (added)
-
figures/nasus_Spin_8.pgf (added)
-
figures/pyke_Contend_2.pgf (added)
-
figures/pyke_Contend_4.pgf (added)
-
figures/pyke_Contend_8.pgf (added)
-
figures/pyke_Future.pgf (added)
-
figures/pyke_Spin_2.pgf (added)
-
figures/pyke_Spin_4.pgf (added)
-
figures/pyke_Spin_8.pgf (added)
-
glossary.tex (modified) (1 diff)
-
local.bib (modified) (1 diff)
-
text/actors.tex (modified) (13 diffs)
-
text/mutex_stmt.tex (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/colby_parsons_MMAth/Makefile
r0b0a285 rc84dd61 50 50 figures/nasus_Channel_Contention \ 51 51 figures/pyke_Channel_Contention \ 52 figures/pyke_Future \ 53 figures/nasus_Future \ 54 figures/pyke_Contend_2 \ 55 figures/pyke_Contend_4 \ 56 figures/pyke_Contend_8 \ 57 figures/pyke_Spin_2 \ 58 figures/pyke_Spin_4 \ 59 figures/pyke_Spin_8 \ 60 figures/nasus_Contend_2 \ 61 figures/nasus_Contend_4 \ 62 figures/nasus_Contend_8 \ 63 figures/nasus_Spin_2 \ 64 figures/nasus_Spin_4 \ 65 figures/nasus_Spin_8 \ 52 66 } 53 67 -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/balance.cfa
r0b0a285 rc84dd61 33 33 allocation receive( d_actor & this, start_msg & msg ) with( this ) { 34 34 for ( i; Set ) { 35 *actor_arr[i + gstart] <<shared_msg;35 *actor_arr[i + gstart] | shared_msg; 36 36 } 37 37 return Nodelete; … … 42 42 if ( recs % Batch == 0 ) { 43 43 for ( i; Batch ) { 44 *actor_arr[gstart + sends % Set] <<shared_msg;44 *actor_arr[gstart + sends % Set] | shared_msg; 45 45 sends += 1; 46 46 } … … 163 163 #ifndef MULTI 164 164 for ( i; qpw ) 165 *actors[i * ActorsPerQueue] <<start_send;165 *actors[i * ActorsPerQueue] | start_send; 166 166 #else 167 167 for ( i; qpw * ActorProcs ) { 168 *actors[i * ActorsPerQueue] <<start_send;168 *actors[i * ActorsPerQueue] | start_send; 169 169 } 170 170 #endif 171 171 172 172 for ( i; FillActors ) 173 *filler_actors[i] <<shared_msg;173 *filler_actors[i] | shared_msg; 174 174 175 175 stop_actor_system(); -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/dynamic.cfa
r0b0a285 rc84dd61 33 33 derived_actor * d_actor = malloc(); 34 34 (*d_actor){}; 35 *d_actor <<*d_msg;35 *d_actor | *d_msg; 36 36 return Delete; 37 37 } … … 62 62 derived_actor * d_actor = malloc(); 63 63 (*d_actor){}; 64 *d_actor <<*d_msg;64 *d_actor | *d_msg; 65 65 66 66 -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/executor.cfa
r0b0a285 rc84dd61 29 29 if ( recs % Batch == 0 ) { 30 30 for ( i; Batch ) { 31 gstart[sends % Set] <<shared_msg;31 gstart[sends % Set] | shared_msg; 32 32 sends += 1; 33 33 } … … 94 94 95 95 for ( i; Actors ) { 96 actors[i] <<shared_msg;96 actors[i] | shared_msg; 97 97 } // for 98 98 -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/matrix.cfa
r0b0a285 rc84dd61 103 103 104 104 for ( unsigned int r = 0; r < xr; r += 1 ) { 105 actors[r] <<messages[r];105 actors[r] | messages[r]; 106 106 } // for 107 107 -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/repeat.cfa
r0b0a285 rc84dd61 46 46 47 47 Client * cl; 48 allocation receive( Server & this, IntMsg & msg ) { msg.val = 7; *cl <<msg; return Nodelete; }49 allocation receive( Server & this, CharMsg & msg ) { msg.val = 'x'; *cl <<msg; return Nodelete; }48 allocation receive( Server & this, IntMsg & msg ) { msg.val = 7; *cl | msg; return Nodelete; } 49 allocation receive( Server & this, CharMsg & msg ) { msg.val = 'x'; *cl | msg; return Nodelete; } 50 50 allocation receive( Server & this, StateMsg & msg ) { return Finished; } 51 51 52 52 void terminateServers( Client & this ) with(this) { 53 53 for ( i; Messages ) { 54 servers[i] <<stateMsg;54 servers[i] | stateMsg; 55 55 } // for 56 56 } … … 60 60 if ( times == Times ) { terminateServers( this ); return Finished; } 61 61 results = 0; 62 this <<stateMsg;62 this | stateMsg; 63 63 return Nodelete; 64 64 } … … 74 74 allocation receive( Client & this, StateMsg & msg ) with(this) { 75 75 for ( i; Messages ) { 76 servers[i] <<intmsg[i];77 servers[i] <<charmsg[i];76 servers[i] | intmsg[i]; 77 servers[i] | charmsg[i]; 78 78 } 79 79 return Nodelete; … … 124 124 Client client; 125 125 cl = &client; 126 client <<stateMsg;126 client | stateMsg; 127 127 128 128 stop_actor_system(); -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/static.cfa
r0b0a285 rc84dd61 29 29 } 30 30 msg.cnt++; 31 receiver <<msg;31 receiver | msg; 32 32 return Nodelete; 33 33 } … … 58 58 derived_actor actor; 59 59 60 actor <<msg;60 actor | msg; 61 61 62 62 stop_actor_system(); -
doc/theses/colby_parsons_MMAth/benchmarks/actors/plotData.py
r0b0a285 rc84dd61 166 166 for idx, arr in enumerate(data): 167 167 plt.errorbar( procs, arr, [bars[idx][0], bars[idx][1]], capsize=2, marker=next(marker) ) 168 marker = itertools.cycle(('o', 's', 'D', 'x', 'p', '^', 'h', '*', 'v' )) 168 169 if currBench == Bench.Executor or currBench == Bench.Matrix or currBench == Bench.Balance_One or currBench == Bench.Repeat: 169 170 plt.yscale("log") -
doc/theses/colby_parsons_MMAth/benchmarks/channels/plotData.py
r0b0a285 rc84dd61 130 130 for idx, arr in enumerate(data): 131 131 plt.errorbar( procs, arr, [bars[idx][0], bars[idx][1]], capsize=2, marker=next(marker) ) 132 132 marker = itertools.cycle(('o', 's', 'D', 'x', 'p', '^', 'h', '*', 'v' )) 133 133 plt.yscale("log") 134 134 # plt.ylim(1, None) -
doc/theses/colby_parsons_MMAth/benchmarks/mutex_stmt/plotData.py
r0b0a285 rc84dd61 103 103 for idx, arr in enumerate(data): 104 104 plt.errorbar( procs, arr, [bars[idx][0], bars[idx][1]], capsize=2, marker=next(marker) ) 105 marker = itertools.cycle(('o', 's', 'D', 'x', 'p', '^', 'h', '*', 'v' )) 105 106 plt.yscale("log") 106 107 plt.xticks(procs) -
doc/theses/colby_parsons_MMAth/glossary.tex
r0b0a285 rc84dd61 37 37 \newabbreviation{toctou}{TOCTOU}{\Newterm{time-of-check to time-of-use}} 38 38 39 \newglossaryentry{actor} 40 { 39 \newglossaryentry{actor}{ 41 40 name=actor, 42 41 description={A basic unit of an actor system that can store local state and send messages to other actors.} 43 42 } 44 43 45 \newglossaryentry{gulp} 46 { 47 name=gulp,44 \newglossaryentry{gulp}{ 45 name={gulp}, 46 first={\Newterm{gulp}}, 48 47 description={Move the contents of message queue to a local queue of the executor thread using a single atomic instruction.} 49 48 } 50 49 51 \newglossaryentry{impl_concurrency} 52 { 50 \newglossaryentry{impl_concurrency}{ 53 51 name=implicit concurrency, 52 first={\Newterm{implicit concurrency}}, 54 53 description={A class of concurrency features that abstract away explicit thread synchronization and mutual exclusion.} 55 54 } 56 55 57 \newglossaryentry{actor_model} 58 { 56 \newglossaryentry{actor_model}{ 59 57 name=actor model, 58 first={\Newterm{actor model}}, 60 59 description={A concurrent computation model, where tasks are broken into units of work that are distributed to actors in the form of messages.} 61 60 } 62 61 63 \newglossaryentry{actor_system} 64 { 62 \newglossaryentry{actor_system}{ 65 63 name=actor system, 64 first={\Newterm{actor system}}, 66 65 description={An implementation of the actor model.} 67 66 } 68 67 69 \newglossaryentry{synch_multiplex} 70 { 68 \newglossaryentry{synch_multiplex}{ 71 69 name=synchronous multiplexing, 70 first={\Newterm{synchronous multiplexing}}, 72 71 description={synchronization on some subset of a set of resources.} 73 72 } -
doc/theses/colby_parsons_MMAth/local.bib
r0b0a285 rc84dd61 100 100 } 101 101 102 @misc{go:sched, 103 author = "The Go Programming Language", 104 title = "src/runtime/proc.go", 105 howpublished = {\href{https://go.dev/src/runtime/proc.go}}, 106 note = "[Online; accessed 23-May-2023]" 107 } 108 102 109 @misc{go:selectref, 103 110 author = "The Go Programming Language Specification", -
doc/theses/colby_parsons_MMAth/text/actors.tex
r0b0a285 rc84dd61 7 7 % C_TODO: add citations throughout chapter 8 8 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. 9 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 creat eor interaction.9 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. 10 10 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. 11 11 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. … … 21 21 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. 22 22 The default number of executor threads is often proportional to the number of computer cores to achieve good performance. 23 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:CFAActor Syntax}}.24 25 \s ection{Classic Actor System}23 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:CFAActor}}. 24 25 \subsection{Classic Actor System} 26 26 An implementation of the actor model with a community of actors is called an actor system. 27 27 Actor systems largely follow the actor model, but can differ in some ways. … … 45 45 \end{figure} 46 46 47 \s ection{\CFA Actors}47 \subsection{\CFA Actor System} 48 48 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}. 49 49 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. … … 92 92 \end{enumerate} 93 93 94 \section{\CFA Actor Syntax}\label{s:CFAActorSyntax}94 \section{\CFA Actor}\label{s:CFAActor} 95 95 \CFA is not an object oriented language and it does not have \gls{rtti}. 96 96 As such, all message sends and receives among actors can only occur using static type-matching, as in Typed-Akka~\cite{AkkaTyped}. … … 146 146 // messages 147 147 struct str_msg { 148 @inline message;@ $\C{// Plan-9 C nominal inheritance}$149 148 char str[12]; 149 @inline message;@ $\C{// Plan-9 C inheritance}$ 150 150 }; 151 151 void ?{}( str_msg & this, char * str ) { strcpy( this.str, str ); } $\C{// constructor}$ 152 152 struct int_msg { 153 @inline message;@ $\C{// Plan-9 C nominal inheritance}$154 153 int i; 154 @inline message;@ $\C{// Plan-9 C inheritance}$ 155 155 }; 156 void ?{}( int_msg & this, int i ) { this.i = i; } $\C{// constructor}$157 156 // behaviours 158 allocation receive( my_actor &, @str_msg & msg@ ) {159 sout | "string message \"" | msg.str | "\"";157 allocation receive( my_actor &, @str_msg & msg@ ) with(msg) { 158 sout | "string message \"" | str | "\""; 160 159 return Nodelete; $\C{// actor not finished}$ 161 160 } 162 allocation receive( my_actor &, @int_msg & msg@ ) {163 sout | "integer message" | msg.i;161 allocation receive( my_actor &, @int_msg & msg@ ) with(msg) { 162 sout | "integer message" | i; 164 163 return Nodelete; $\C{// actor not finished}$ 165 164 } 166 165 int main() { 166 str_msg str_msg{ "Hello World" }; $\C{// constructor call}$ 167 int_msg int_msg{ 42 }; $\C{// constructor call}$ 167 168 start_actor_system(); $\C{// sets up executor}$ 168 169 my_actor actor; $\C{// default constructor call}$ 169 str_msg str_msg{ "Hello World" }; $\C{// constructor call}$ 170 int_msg int_msg{ 42 }; $\C{// 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)}$ 170 @actor | str_msg | int_msg;@ $\C{// cascade sends}$ 171 @actor | int_msg;@ $\C{// send}$ 172 @actor | finished_msg;@ $\C{// send => terminate actor (deallocation deferred)}$ 174 173 stop_actor_system(); $\C{// waits until actors finish}\CRT$ 175 174 } // deallocate int_msg, str_msg, actor 176 175 \end{cfa} 177 176 \caption{\CFA Actor Syntax} 178 \label{f:CFAActor Syntax}179 \end{figure} 180 181 Figure~\ref{f:CFAActor Syntax} shows a complete \CFA actor example starting with the actor type @my_actor@ created by defining a @struct@ that inherits from the base @actor@ @struct@ via the @inline@ keyword.182 This inheritance style is the Plan-9 C-style nominalinheritance discussed in Section~\ref{s:Inheritance}.177 \label{f:CFAActor} 178 \end{figure} 179 180 Figure~\ref{f:CFAActor} shows a complete \CFA actor example starting with the actor type @my_actor@ created by defining a @struct@ that inherits from the base @actor@ @struct@ via the @inline@ keyword. 181 This inheritance style is the Plan-9 C-style inheritance discussed in Section~\ref{s:Inheritance}. 183 182 Similarly, the message types @str_msg@ and @int_msg@ are created by defining a @struct@ that inherits from the base @message@ @struct@ via the @inline@ keyword. 184 Both message types have constructors to set the message value. 183 Only @str_msg@ needs a constructor to copy the C string; 184 @int_msg@ is initialized using its \CFA auto-generated constructors. 185 185 There are two matching @receive@ (behaviour) routines that process the corresponding typed messages. 186 The program main begins by calling @start_actor_system@ to start the actor implementation, including executor threads to run the actors. 187 An actor and two messages are created on the stack, and four messages are sent to the actor using operator @<<@. 188 The last message is the builtin @finish_msg@, which returns @Finished@ to an executor thread, which removes the actor from the actor system \see{Section~\ref{s:ActorBehaviours}}. 189 The call to @stop_actor_system@ blocks program main until all actors are finished and removed from the actor system. 190 The program main ends by deleting the actor and messages from the stack. 186 Both @receive@ routines use a @with@ clause so message fields are not qualified and return @Nodelete@ indicating the actor is not finished. 187 Also, all messages are marked with @Nodelete@ as their default allocation state. 188 The program main begins by creating two messages on the stack. 189 Then the executor system is started by calling @start_actor_system@. 190 Now an actor is created on the stack and four messages are sent it using operator @?|?@. 191 The last message is the builtin @finish_msg@, which returns @Finished@ to an executor thread, causing it to removes the actor from the actor system \see{Section~\ref{s:ActorBehaviours}}. 192 The call to @stop_actor_system@ blocks the program main until all actors are finished and removed from the actor system. 193 The program main ends by deleting the actor and two messages from the stack. 191 194 The output for the program is: 192 195 \begin{cfa} … … 237 240 Note, it is safe to construct an actor or message with a status other than @Nodelete@, since the executor only examines the allocation action after a behaviour returns. 238 241 242 \subsection{Actor Envelopes}\label{s:envelope} 243 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. 244 Because a C program manages message lifetime, messages cannot be copied for each send, otherwise who manages the copies. 245 Therefore, it up to the actor program to manage message life-time across receives. 246 However, for a message to appear on multiple message queues, it needs an arbitrary number of associated destination behaviours. 247 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. 248 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. 249 250 % In actor systems, messages are sent and received by actors. 251 % When a actor receives a message it executes its behaviour that is associated with that message type. 252 % 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. 253 % 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. 254 % All these requirements are fulfilled by a construct called an envelope. 255 % The envelope wraps up the unit of work and also stores any information needed by data structures such as link fields. 256 257 % One may ask, "Could the link fields and other information be stored in the message?". 258 % This is a good question to ask since messages also need to have a lifetime that persists beyond the work it delivers. 259 % 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. 260 % 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. 261 239 262 \subsection{Actor System}\label{s:ActorSystem} 240 263 The calls to @start_actor_system@, and @stop_actor_system@ mark the start and end of a \CFA actor system. … … 258 281 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. 259 282 260 % All message sends are done using the left-shift operator, @<<@, similar to the syntax of \CC's stream output.261 % \begin{cfa}262 % allocation ?<<?( my_actor & receiver, my_msg & msg )263 % \end{cfa}264 % Notice this signature is the same as the @receive@ routine, which is no coincidence.265 % The \CFA compiler generates a @?<<?@ routine definition and forward declaration for each @receive@ routine that has the appropriate signature.266 % The generated routine packages the message and actor in an \hyperref[s:envelope]{envelope} and adds it to the executor's queues via an executor routine.267 % As part of packaging the envelope, the @?<<?@ routine sets a routine pointer in the envelope to point to the appropriate receive routine for given actor and message types.268 269 283 \subsection{Actor Send}\label{s:ActorSend} 270 All message sends are done using the left-shift operator, @<<@, similar to the syntax of \CC's stream output. 284 All message sends are done using the vertical-bar (bit-or) operator, @?|?@, similar to the syntax of the \CFA stream I/O. 285 Hence, programmers must write a matching @?|?@ routine for each @receive@ routine, which is awkward and generates a maintenance problem that must be solved. 286 \CFA's current best approach for creating a generic @?|?@ routine requires users to create routines for their actor and message types that access the base type. 287 Since these routines are not complex, they can be generated using macros that are used to annotate the user-defined actor and message types. 288 This approach is used in \CFA's intrusive list data structure. 289 This is not much better than asking users to write the @?|?@ routine themselves in terms of maintenance, since the user needs to remember the boilerplate macro annotation. 290 271 291 As stated, \CFA does not have named inheritance with RTTI. 272 292 \CFA does have a preliminary form of virtual routines, but it is not mature enough for use in this work. 273 Therefore, there is no mechanism to write a generic @<<@ routine taking a base actor and message type, and then dynamically selectingthe @receive@ routine from the actor argument.274 (For messages, the Plan-9 inheritance is sufficient because only the inherited fields are needed during the message send.) 275 Hence, programmers must write a matching @<<@ routine for each @receive@ routine, which is awkward and generates a maintenance problem. 276 Therefore, I chose to use a template-like approach, where the compiler generates a matching @<<@ routine for each @receive@ routine it finds with anactor/message type-signature.277 Th en, \CFA uses the type from the left-hand side of an assignment to select the matching receive routine.293 Virtuals would provide a mechanism to write a single generic @?|?@ routine taking a base actor and message type, that would dynamically select the @receive@ routine from the actor argument. 294 Note, virtuals are not needed for the send; Plan-9 inheritance is sufficient because only the inherited fields are needed during the message send (only upcasting is needed). 295 296 Therefore, a template-like approach was chosen, where the compiler generates a matching @?|?@ routine for each @receive@ routine it finds with the correct actor/message type-signature. 297 This approach requires no annotation or additional code to be written by users, thus it resolves the maintenance problem. 278 298 (When the \CFA virtual routines mature, it should be possible to seamlessly transition to it from the template approach.) 279 299 280 % Funneling all message sends through a single @allocation ?<<?(actor &, message &)@ routine is not feasible since the type of the actor and message would be erased, making it impossible to acquire a pointer to the correct @receive@. 281 % As such a @?<<?@ routine per @receive@ provides type information needed to write the correct "address" on the envelope. 282 283 % The left-shift operator routines are generated by the compiler. 284 An example of a receive routine and its corresponding generated operator routine is shown in Figure~\ref{f:actor_gen}. 285 Notice the parameter signature of @?<<?@ is the same as the @receive@ routine. 286 A @?<<?@ routine is generated per @receive@ routine with a matching signature. 287 The @?<<?@ routine packages the message and actor in an \hyperref[s:envelope]{envelope} and adds it to the executor's queues via the executor routine @send@. 288 The envelope is conceptually "addressed" to a behaviour, which is stored in the envelope as a function pointer to a @receive@ routine. 289 The @?<<?@ routines ensure that messages are sent to the right address, \ie sent to the right @receive@ routine based on actor and message type. 300 Figure~\ref{f:send_gen} shows the generated send routine for the @int_msg@ receive in Figure~\ref{f:CFAActor}. 301 Operator @?|?@ has the same parameter signature as the corresponding @receive@ routine and returns an @actor@ so the operator can be cascaded. 302 The routine sets @rec_fn@ to the matching @receive@ routine using the left-hand type to perform the selection. 303 Then the routine packages the base and derived actor and message and actor, along with the receive routine into an \hyperref[s:envelope]{envelope}. 304 Finally, the envelop is added to the executor queue designated by the actor using the executor routine @send@. 290 305 291 306 \begin{figure} 292 307 \begin{cfa} 293 $\LstCommentStyle{// from Figure~\ref{f:CFAActorSyntax}}$ 294 struct my_actor { inline actor; }; $\C[3.5in]{// actor}$ 295 struct int_msg { inline message; int i; }; $\C{// message}$ 296 void ?{}( int_msg & this, int i ) { this.i = i; } $\C{// constructor}$ 297 allocation receive( @my_actor &, int_msg & msg@ ) { $\C{// receiver}$ 298 sout | "integer message" | msg.i; 299 return Nodelete; 300 } 301 302 // compiler generated operator 303 #define RECEIVER( A, M ) (allocation (*)(actor &, message &))(allocation (*)( A &, M & ))receive 304 my_actor & ?<<?( @my_actor & receiver, int_msg & msg@ ) { 305 send( receiver, (request){ &receiver, &msg, RECEIVER( my_actor, int_msg ) } ); 308 $\LstCommentStyle{// from Figure~\ref{f:CFAActor}}$ 309 struct my_actor { inline actor; }; $\C[3.75in]{// actor}$ 310 struct int_msg { inline message; int i; }; $\C{// message}$ 311 allocation receive( @my_actor &, int_msg & msg@ ) {...} $\C{// receiver}$ 312 313 // compiler generated send operator 314 typedef allocation (*receive_t)( actor &, message & ); 315 actor & ?|?( @my_actor & receiver, int_msg & msg@ ) { 316 allocation (*rec_fn)( my_actor &, int_msg & ) = @receive@; // deduce receive routine 317 request req{ &receiver, (actor *)&receiver, &msg, (message *)&msg, (receive_t)rec_fn }; 318 send( receiver, req ); $\C{// queue message for execution}\CRT$ 306 319 return receiver; 307 320 } 308 321 \end{cfa} 309 322 \caption{Generated Send Operator} 310 \label{f:actor_gen} 323 \label{f:send_gen} 324 \end{figure} 325 326 \subsection{Actor Termination}\label{s:ActorTerm} 327 During a message send, the receiving actor and message being sent are stored via pointers in the envelope. 328 These pointers have the base actor and message types, so type information of the actor and message is lost and then recovered later when the typed receive routine is called. 329 After the receive routine is done, the executor must clean up the actor and message according to their allocation status. 330 If the allocation status is @Delete@ or @Destroy@, the appropriate destructor must be called by the executor. 331 This poses a problem; the correct type of the actor or message is not available to the executor, but it needs to call the right destructor! 332 This requires downcasting from the base type to derived type, which requires a virtual system. 333 Thus, a rudimentary destructor-only virtual system was added to \CFA as part of this work. 334 This virtual system is used via Plan-9 inheritance of the @virtual_dtor@ type, shown in Figure~\ref{f:VirtDtor}. 335 The @virtual_dtor@ type maintains a pointer to the start of the object, and a pointer to the correct destructor. 336 When a type inherits the @virtual_dtor@ type, the compiler adds code to its destructor to make sure that any destructor calls along this segment of the inheritance tree is called are intercepted, and restart at the appropriate destructor for that object. 337 338 \begin{figure} 339 \begin{cfa} 340 struct base_type { inline virtual_dtor; }; 341 struct intermediate_type { inline base_type; }; 342 struct derived_type { inline intermediate_type; }; 343 344 int main() { 345 derived_type d1, d2, d3; 346 intermediate_type & i = d2; 347 base_type & b = d3; 348 349 // these will call the destructors in the correct order 350 ^d1{}; ^i{}; ^b{}; 351 } 352 353 \end{cfa} 354 \caption{\CFA Virtual Destructor} 355 \label{f:VirtDtor} 356 \end{figure} 357 358 This virtual destructor system was built for this work, but is general and can be used in any type in \CFA. 359 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. 360 361 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@. 362 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. 363 364 \begin{figure} 365 \begin{cfa} 366 message __base_msg_finished $@$= { .allocation_ : Finished }; // no auto-gen constructors 367 struct __delete_msg_t { inline message; } delete_msg = __base_msg_finished; 368 struct __destroy_msg_t { inline message; } destroy_msg = __base_msg_finished; 369 struct __finished_msg_t { inline message; } finished_msg = __base_msg_finished; 370 371 allocation receive( actor & this, __delete_msg_t & msg ) { return Delete; } 372 allocation receive( actor & this, __destroy_msg_t & msg ) { return Destroy; } 373 allocation receive( actor & this, __finished_msg_t & msg ) { return Finished; } 374 \end{cfa} 375 \caption{Builtin Convenience Messages} 376 \label{f:ConvenienceMessages} 311 377 \end{figure} 312 378 … … 319 385 The goal is to achieve better performance and scalability for certain kinds of actor applications by reducing executor locking. 320 386 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. 321 Work steal now becomes queue stealing, where an entire actor/message queue is stolen, which trivially preserves message ordering in a queue \see{Section~\ref{s:steal}}.322 387 323 388 \begin{figure} … … 330 395 331 396 Each executor thread iterates over its own message queues until it finds one with messages. 332 At this point, the executor thread atomically \gls{gulp}s the queue, meaning move the contents of message queue to a local queue of the executor thread using a single atomic instruction. 333 This step allows the executor threads to process the local queue without any atomics until the next gulp, while other executor threads are adding in parallel to the end of one of the message queues. 334 In detail, an executor thread performs a test-and-gulp, non-atomically checking if a queue is non-empty before gulping it. 335 If a test fails during a message add, the worst-case is cycling through all the message queues. 336 However, the gain is minimizing costly lock acquisitions. 397 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. 337 398 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. 338 339 Processing a local queue involves removing a unit of work from the queue and executing it. 399 This step allows an executor thread to process the local queue without any atomics until the next gulp. 400 Other executor threads can continue adding to the ends of executor thread's message queues. 401 In detail, an executor thread performs a test-and-gulp, non-atomically checking if a queue is non-empty, before attempting to gulp it. 402 If an executor misses an non-empty queue due to a race, it eventually finds the queue after cycling through its message queues. 403 This approach minimizes costly lock acquisitions. 404 405 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. 340 406 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. 341 After running a behaviour, the executor thread examines the returned allocation status from the @receive@ routine for the actor and internal status in the delivered message, and takes the appropriate action. 342 Once all actors have marked themselves as being finished the executor initiates shutdown by inserting a sentinel value into the message queues. 343 Once a executor threads sees a sentinel it stops running. 344 After all executors stop running the actor system shutdown is complete. 345 346 \section{Envelopes}\label{s:envelope} 347 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. 348 Because a C program manages message lifetime, messages cannot be copied for each send, otherwise who manages the copies. 349 Therefore, it up to the actor program to manage message life-time across receives. 350 However, for a message to appear on multiple message queues, it needs an arbitrary number of link fields. 351 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. 352 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. 353 354 % In actor systems, messages are sent and received by actors. 355 % When a actor receives a message it executes its behaviour that is associated with that message type. 356 % 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. 357 % 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. 358 % All these requirements are fulfilled by a construct called an envelope. 359 % The envelope wraps up the unit of work and also stores any information needed by data structures such as link fields. 360 361 % One may ask, "Could the link fields and other information be stored in the message?". 362 % This is a good question to ask since messages also need to have a lifetime that persists beyond the work it delivers. 363 % 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. 364 % 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. 365 366 Unfortunately, this frequent allocation of envelopes for each send results in heavy contention on the memory allocator. 367 As such, a way to alleviate contention on the memory allocator would result in a performance improvement. 368 Contention is reduced using a novel data structure, called a \Newterm{copy queue}. 407 As each actor is created or terminated by an executor thread, it increments/decrements a global counter. 408 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. 409 Once a executor threads sees the flag is set it stops running. 410 After all executors stop, the actor system shutdown is complete. 369 411 370 412 \subsection{Copy Queue}\label{s:copyQueue} 413 Unfortunately, the frequent allocation of envelopes for each send results in heavy contention on the memory allocator. 414 This contention is reduced using a novel data structure, called a \Newterm{copy queue}. 371 415 The copy queue is a thin layer over a dynamically sized array that is designed with the envelope use case in mind. 372 A copy queue supports the typical queue operations of push/pop but in a different way than a typical arraybased queue.373 The copy queue is designed to take advantage of the \gls{gulp}ing pattern. 374 As such, the amortized runtime cost of each push/pop operation for the copy queue is$O(1)$.416 A copy queue supports the typical queue operations of push/pop but in a different way from a typical array-based queue. 417 418 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)$. 375 419 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. 376 420 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. … … 384 428 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. 385 429 One downside of this approach that more storage is allocated than needed, \ie each copy queue is only partially full. 386 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.430 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. 387 431 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. 388 432 … … 390 434 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. 391 435 However, this approach has a problem. 392 The high memory usagewatermark nearly doubled!393 The issue can easily behighlighted with an example.436 The high memory watermark nearly doubled! 437 The issue is highlighted with an example. 394 438 Assume a fixed throughput workload, where a queue never has more than 19 messages at a time. 395 439 If the copy queue starts with a size of 10, it ends up doubling at some point to size 20 to accommodate 19 messages. 396 440 However, after 2 gulps and subsequent reclamations the array size is 18. 397 441 The next time 19 messages are enqueued, the array size is doubled to 36! 398 To avoid this issue a second check is added. 399 Each copy queue started tracking the utilization of its array size. 442 To avoid this issue, a second check is added. 400 443 Reclamation only occurs if less than half of the array is utilized. 401 In doing this, the reclamation scheme is able to achieve a lower high-watermark and a lower overall memory utilization compared to the non-reclamation copy queues. 402 However, the use of copy queues still incurs a higher memory cost than list-based queueing. 403 With the inclusion of a memory reclamation scheme the increase in memory usage is reasonable considering the performance gains and is discussed further in Section~\ref{s:actor_perf}. 444 This check achieves a lower total storage and overall memory utilization compared to the non-reclamation copy queues. 445 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}}. 404 446 405 447 \section{Work Stealing}\label{s:steal} 406 Work stealing is a scheduling strategy t hat attempts to load balance, and increase resource utilization by having idle threads steal work.407 The re are many parts that make up a work stealing actor scheduler, but the two that will be highlighted in this work are the stealing mechanism and victim selection.408 409 % C_TODO enter citation for langs 448 Work stealing is a scheduling strategy to provide \Newterm{load balance}. 449 The goal is to increase resource utilization by having idle threads steal work from working threads. 450 While there are multiple parts in work-stealing scheduler, the two important components are victim selection and the stealing mechanism. 451 410 452 \subsection{Stealing Mechanism} 411 In this discussion of work stealing the worker being stolen from will be referred to as the \textbf{victim} and the worker stealing work will be called the \textbf{thief}.412 The stealing mechanism presented here differs from existing work stealing actor systems due the inverted actorsystem.413 Other actor systems such as Akka \cite{} and CAF \cite{} have work stealing, but since they use an classic actor system that is actor-centric, stealing work is the act of stealing an actor from a dequeue.414 As an example, in CAF, the sharded actor queue is a set of double ended queues (dequeues).415 When ever an actor is moved to a ready queue, it is inserted into a worker's dequeue.453 In work stealing, the stealing worker is called the \Newterm{thief} and the stolen-from worker is called the \Newterm{victim}. 454 The stealing mechanism presented here differs from existing work-stealing actor-systems because of the message-centric (inverted) actor-system. 455 Other actor systems, such as Akka~\cite{Akka} and CAF~\cite{CAF}, have work stealing, but use an actor-centric system where stealing is dequeuing from a non-empty ready-queue to an empty ready-queue. 456 As an example, in CAF, the sharded actor queue is a set of double-ended queues (dequeues). 457 When an actor has messages, it is inserted into a worker's dequeue (ready queue). 416 458 Workers then consume actors from the dequeue and execute their behaviours. 417 459 To steal work, thieves take one or more actors from a victim's dequeue. 418 This action creates contention on the dequeue, which can slow down the throughput of the victim. 419 The notion of which end of the dequeue is used for stealing, consuming, and inserting is not discussed since it isn't relevant. 420 By the pigeon hole principle there are three dequeue operations (push/victim pop/thief pop) that can occur concurrently and only two ends to a dequeue, so work stealing being present in a dequeue based system will always result in a potential increase in contention on the dequeues. 460 By the pigeon hole principle, there are three dequeue operations (push/victim pop/thief pop) that can occur concurrently and only two ends to a dequeue, so work stealing in a dequeue-based system always results in a potential increase in contention on the dequeues. 461 This contention can slows down the victim's throughput. 462 Note, which end of the dequeue is used for stealing, consuming, and inserting is not discussed since the largest cost is the mutual exclusion and its duration for safely performing the queue operations. 463 464 Work steal now becomes queue stealing, where an entire actor/message queue is stolen, which trivially preserves message ordering in a queue \see{Section~\ref{s:steal}}. 421 465 422 466 % C_TODO: maybe insert stealing diagram 423 467 424 In \CFA, the actor work stealing implementation is unique. 425 While other systems are concerned with stealing actors, the \CFA actor system steals queues. 426 This is a result of \CFA's use of the inverted actor system. 427 The goal of the \CFA actor work stealing mechanism is to have a zero-victim-cost stealing mechanism. 428 This does not means that stealing has no cost. 429 This goal is to ensure that stealing work does not impact the performance of victim workers. 430 This means that thieves can not contend with victims, and that victims should perform no stealing related work unless they become a thief. 431 In theory this goal is not achieved, but results will be presented that show the goal is achieved in practice. 432 In \CFA's actor system workers own a set of sharded queues which they iterate over and gulp. 433 If a worker has iterated over the queues they own twice without finding any work, they try to steal a queue from another worker. 434 Stealing a queue is done wait-free with a few atomic instructions that can only create contention with other stealing workers. 435 To steal a queue a worker does the following: 468 In \CFA, the actor work-stealing implementation is unique because of the message-centric system. 469 In this system, it is impractical to steal actors because an actor's messages are distributed in temporal order along the message queue. 470 To ensure sequential actor execution and FIFO message delivery, actor stealing requires finding and removing all of an actor's messages, and inserting them consecutively in another message queue. 471 This operation is $O(N)$ with a non-trivial constant. 472 The only way for work stealing to become practical is to shard the message queue, which also reduces contention, and steal queues to eliminate queue searching. 473 474 Given queue stealing, the goal is to have a zero-victim-cost stealing mechanism, which does not mean stealing has no cost. 475 It means work stealing does not affect the performance of the victim worker. 476 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. 477 In theory, this goal is not achievable, but results show the goal is achieved in practice. 478 479 In \CFA's actor system, workers own a set of sharded queues, which they iterate over and gulp. 480 If a worker has iterated over its message queues twice without finding any work, it tries to steal a queue from another worker. 481 Stealing a queue is done wait-free with a few atomic instructions that can only create contention with other stealing workers, not the victim. 482 To steal a queue, a worker does the following: 436 483 \begin{enumerate}[topsep=5pt,itemsep=3pt,parsep=0pt] 437 484 \item 438 The thief chooses a victim .485 The thief chooses a victim, which is trivial because all workers are stored in a shared array. 439 486 440 487 \item 441 488 The thief starts at a random index in the array of the victim's queues and searches for a candidate queue. 442 A candidate queue is any queue that is not empty, is not being stolen by another thief, and is not being processed by the victim. 443 These are not strictly enforced rules. 444 The candidate is identified non-atomically and as such queues that do not satisfy these rules may be stolen. 445 However, steals that do not meet these requirements do not affect correctness so they are allowed and do not constitute failed steals as the queues will still be swapped. 446 447 448 \item 449 Once a candidate queue is chosen, the thief attempts a wait-free swap of the victim's queue and a random on of the thief's queues. 450 This swap can fail. 451 If the swap is successful the thief swaps the two queues. 452 If the swap fails, another thief must have attempted to steal one of the two queues being swapped. 453 Failing to steal is good in this case since stealing a queue that was just swapped would likely result in stealing an empty queue. 489 A candidate queue is any non-empty queue not being processed by the victim and not being stolen by another thief. 490 These rules are not strictly enforced. 491 A candidate is identified non-atomically, and as such, queues that do not satisfy these rules may be stolen. 492 However, steals not meeting the rules do not affect correctness and do not constitute failed steals as the queue is always swapped. 493 494 \item 495 Once a candidate queue is chosen, the thief attempts a wait-free swap of a victim's queue to a random empty thief queue. 496 If the swap successes, the steal is completed. 497 If the swap fails, the victim may have been gulping that message queue or another thief must have attempted to steal the victim's queue. 498 In either case, that message queue is highly likely to be empty. 499 500 \item 501 Once a thief fails or succeeds in stealing a queue, it iterates over its messages queues again because new messages may have arrived during stealing. 502 Stealing is only repeated after two consecutive iterations over its owned queues without finding work. 454 503 \end{enumerate} 455 504 456 Once a thief fails or succeeds in stealing a queue, it goes back to its own set of queues and iterates over them again.457 It will only try to steal again once it has completed two consecutive iterations over its owned queues without finding any work.458 505 The key to the stealing mechanism is that the queues can still be operated on while they are being swapped. 459 This eliminates any contention between thieves and victims. 506 This functionality eliminates any contention among thieves and victims. 507 460 508 The first key to this is that actors and workers maintain two distinct arrays of references to queues. 461 509 Actors will always receive messages via the same queues. … … 606 654 607 655 \subsection{Stealing Guarantees} 608 609 % C_TODO insert graphs for each proof610 656 Given that the stealing operation can potentially fail, it is important to discuss the guarantees provided by the stealing implementation. 611 657 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. … … 749 795 Since the @Finished@ allocation status is unused for messages, it is used internally to detect if a message has been sent. 750 796 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. 797 \item Detection of messages sent but not received 798 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. 751 799 \end{itemize} 752 800 -
doc/theses/colby_parsons_MMAth/text/mutex_stmt.tex
r0b0a285 rc84dd61 415 415 \begin{figure} 416 416 \centering 417 \captionsetup[subfloat]{labelfont=footnotesize,textfont=footnotesize} 417 418 \subfloat[AMD]{ 418 419 \resizebox{0.5\textwidth}{!}{\input{figures/nasus_Aggregate_Lock_2.pgf}} … … 421 422 \resizebox{0.5\textwidth}{!}{\input{figures/pyke_Aggregate_Lock_2.pgf}} 422 423 } 424 \bigskip 423 425 424 426 \subfloat[AMD]{ … … 428 430 \resizebox{0.5\textwidth}{!}{\input{figures/pyke_Aggregate_Lock_4.pgf}} 429 431 } 432 \bigskip 430 433 431 434 \subfloat[AMD]{
Note:
See TracChangeset
for help on using the changeset viewer.