1 | #pragma once
|
---|
2 |
|
---|
3 | #include <locks.hfa>
|
---|
4 | #include <limits.hfa>
|
---|
5 | #include <kernel.hfa>
|
---|
6 | #include <iofwd.hfa>
|
---|
7 | #include <virtual_dtor.hfa>
|
---|
8 |
|
---|
9 | #ifdef __CFA_DEBUG__
|
---|
10 | #define CFA_DEBUG( stmt ) stmt
|
---|
11 | #else
|
---|
12 | #define CFA_DEBUG( stmt )
|
---|
13 | #endif // CFA_DEBUG
|
---|
14 |
|
---|
15 | #define DEBUG_ABORT( cond, string ) CFA_DEBUG( if ( cond ) abort( string ) )
|
---|
16 |
|
---|
17 | // Define the default number of processors created in the executor. Must be greater than 0.
|
---|
18 | #define __DEFAULT_EXECUTOR_PROCESSORS__ 2
|
---|
19 |
|
---|
20 | // Define the default number of threads created in the executor. Must be greater than 0.
|
---|
21 | #define __DEFAULT_EXECUTOR_WORKERS__ 2
|
---|
22 |
|
---|
23 | // Define the default number of executor request-queues (mailboxes) written to by actors and serviced by the
|
---|
24 | // actor-executor threads. Must be greater than 0.
|
---|
25 | #define __DEFAULT_EXECUTOR_RQUEUES__ 4
|
---|
26 |
|
---|
27 | // Define if executor is created in a separate cluster
|
---|
28 | #define __DEFAULT_EXECUTOR_SEPCLUS__ false
|
---|
29 |
|
---|
30 | #define __DEFAULT_EXECUTOR_BUFSIZE__ 10
|
---|
31 |
|
---|
32 | #define __STEAL 0 // workstealing toggle. Disjoint from toggles above
|
---|
33 |
|
---|
34 | // workstealing heuristic selection (only set one to be 1)
|
---|
35 | // #define RAND 0
|
---|
36 | #define SEARCH 1
|
---|
37 |
|
---|
38 | // show stats
|
---|
39 | // #define ACTOR_STATS
|
---|
40 |
|
---|
41 | // forward decls
|
---|
42 | struct actor;
|
---|
43 | struct message;
|
---|
44 | struct executor;
|
---|
45 |
|
---|
46 | enum allocation { Nodelete, Delete, Destroy, Finished }; // allocation status
|
---|
47 |
|
---|
48 | typedef allocation (*__receive_fn)(actor &, message &);
|
---|
49 | struct request {
|
---|
50 | actor * receiver;
|
---|
51 | message * msg;
|
---|
52 | __receive_fn fn;
|
---|
53 | bool stop;
|
---|
54 | };
|
---|
55 |
|
---|
56 | static inline void ?{}( request & this ) { this.stop = true; } // default ctor makes a sentinel
|
---|
57 | static inline void ?{}( request & this, actor * receiver, message * msg, __receive_fn fn ) {
|
---|
58 | this.receiver = receiver;
|
---|
59 | this.msg = msg;
|
---|
60 | this.fn = fn;
|
---|
61 | this.stop = false;
|
---|
62 | }
|
---|
63 | static inline void ?{}( request & this, request & copy ) {
|
---|
64 | this.receiver = copy.receiver;
|
---|
65 | this.msg = copy.msg;
|
---|
66 | this.fn = copy.fn;
|
---|
67 | this.stop = copy.stop;
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Vector-like data structure that supports O(1) queue operations with no bound on size
|
---|
71 | // assumes gulping behaviour (once a remove occurs, removes happen until empty beforw next insert)
|
---|
72 | struct copy_queue {
|
---|
73 | request * buffer;
|
---|
74 | size_t count, buffer_size, index, utilized, last_size;
|
---|
75 | };
|
---|
76 | static inline void ?{}( copy_queue & this ) {}
|
---|
77 | static inline void ?{}( copy_queue & this, size_t buf_size ) with(this) {
|
---|
78 | buffer_size = buf_size;
|
---|
79 | buffer = aalloc( buffer_size );
|
---|
80 | count = 0;
|
---|
81 | utilized = 0;
|
---|
82 | index = 0;
|
---|
83 | last_size = 0;
|
---|
84 | }
|
---|
85 | static inline void ^?{}( copy_queue & this ) with(this) { adelete(buffer); }
|
---|
86 |
|
---|
87 | static inline void insert( copy_queue & this, request & elem ) with(this) {
|
---|
88 | if ( count >= buffer_size ) { // increase arr size
|
---|
89 | last_size = buffer_size;
|
---|
90 | buffer_size = 2 * buffer_size;
|
---|
91 | buffer = realloc( buffer, sizeof( request ) * buffer_size );
|
---|
92 | /* paranoid */ verify( buffer );
|
---|
93 | }
|
---|
94 | memcpy( &buffer[count], &elem, sizeof(request) );
|
---|
95 | count++;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // once you start removing you need to remove all elements
|
---|
99 | // it is not supported to call insert() before the array is fully empty
|
---|
100 | static inline request & remove( copy_queue & this ) with(this) {
|
---|
101 | if ( count > 0 ) {
|
---|
102 | count--;
|
---|
103 | size_t old_idx = index;
|
---|
104 | index = count == 0 ? 0 : index + 1;
|
---|
105 | return buffer[old_idx];
|
---|
106 | }
|
---|
107 | request * ret = 0p;
|
---|
108 | return *0p;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // try to reclaim some memory if less than half of buffer is utilized
|
---|
112 | static inline void reclaim( copy_queue & this ) with(this) {
|
---|
113 | if ( utilized >= last_size || buffer_size <= 4 ) { utilized = 0; return; }
|
---|
114 | utilized = 0;
|
---|
115 | buffer_size--;
|
---|
116 | buffer = realloc( buffer, sizeof( request ) * buffer_size ); // try to reclaim some memory
|
---|
117 | }
|
---|
118 |
|
---|
119 | static inline bool isEmpty( copy_queue & this ) with(this) { return count == 0; }
|
---|
120 |
|
---|
121 | struct work_queue {
|
---|
122 | __spinlock_t mutex_lock;
|
---|
123 | copy_queue * owned_queue; // copy queue allocated and cleaned up by this work_queue
|
---|
124 | copy_queue * c_queue; // current queue
|
---|
125 | volatile bool being_processed; // flag to prevent concurrent processing
|
---|
126 | #ifdef ACTOR_STATS
|
---|
127 | unsigned int id;
|
---|
128 | size_t missed; // transfers skipped due to being_processed flag being up
|
---|
129 | #endif
|
---|
130 | }; // work_queue
|
---|
131 | static inline void ?{}( work_queue & this, size_t buf_size, unsigned int i ) with(this) {
|
---|
132 | owned_queue = alloc(); // allocated separately to avoid false sharing
|
---|
133 | (*owned_queue){ buf_size };
|
---|
134 | c_queue = owned_queue;
|
---|
135 | being_processed = false;
|
---|
136 | #ifdef ACTOR_STATS
|
---|
137 | id = i;
|
---|
138 | missed = 0;
|
---|
139 | #endif
|
---|
140 | }
|
---|
141 |
|
---|
142 | // clean up copy_queue owned by this work_queue
|
---|
143 | static inline void ^?{}( work_queue & this ) with(this) { delete( owned_queue ); }
|
---|
144 |
|
---|
145 | static inline void insert( work_queue & this, request & elem ) with(this) {
|
---|
146 | lock( mutex_lock __cfaabi_dbg_ctx2 );
|
---|
147 | insert( *c_queue, elem );
|
---|
148 | unlock( mutex_lock );
|
---|
149 | } // insert
|
---|
150 |
|
---|
151 | static inline void transfer( work_queue & this, copy_queue ** transfer_to ) with(this) {
|
---|
152 | lock( mutex_lock __cfaabi_dbg_ctx2 );
|
---|
153 | #ifdef __STEAL
|
---|
154 |
|
---|
155 | // check if queue is being processed elsewhere
|
---|
156 | if ( unlikely( being_processed ) ) {
|
---|
157 | #ifdef ACTOR_STATS
|
---|
158 | missed++;
|
---|
159 | #endif
|
---|
160 | unlock( mutex_lock );
|
---|
161 | return;
|
---|
162 | }
|
---|
163 |
|
---|
164 | being_processed = c_queue->count != 0;
|
---|
165 | #endif // __STEAL
|
---|
166 |
|
---|
167 | c_queue->utilized = c_queue->count;
|
---|
168 |
|
---|
169 | // swap copy queue ptrs
|
---|
170 | copy_queue * temp = *transfer_to;
|
---|
171 | *transfer_to = c_queue;
|
---|
172 | c_queue = temp;
|
---|
173 | unlock( mutex_lock );
|
---|
174 | } // transfer
|
---|
175 |
|
---|
176 | // needed since some info needs to persist past worker lifetimes
|
---|
177 | struct worker_info {
|
---|
178 | volatile unsigned long long stamp;
|
---|
179 | #ifdef ACTOR_STATS
|
---|
180 | size_t stolen_from, try_steal, stolen, failed_swaps, msgs_stolen;
|
---|
181 | unsigned long long processed;
|
---|
182 | size_t gulps;
|
---|
183 | #endif
|
---|
184 | };
|
---|
185 | static inline void ?{}( worker_info & this ) {
|
---|
186 | #ifdef ACTOR_STATS
|
---|
187 | this.stolen_from = 0;
|
---|
188 | this.try_steal = 0; // attempts to steal
|
---|
189 | this.stolen = 0; // successful steals
|
---|
190 | this.processed = 0; // requests processed
|
---|
191 | this.gulps = 0; // number of gulps
|
---|
192 | this.failed_swaps = 0; // steal swap failures
|
---|
193 | this.msgs_stolen = 0; // number of messages stolen
|
---|
194 | #endif
|
---|
195 | this.stamp = rdtscl();
|
---|
196 | }
|
---|
197 |
|
---|
198 | // #ifdef ACTOR_STATS
|
---|
199 | // unsigned int * stolen_arr;
|
---|
200 | // unsigned int * replaced_queue;
|
---|
201 | // #endif
|
---|
202 | thread worker {
|
---|
203 | work_queue ** request_queues;
|
---|
204 | copy_queue * current_queue;
|
---|
205 | executor * executor_;
|
---|
206 | unsigned int start, range;
|
---|
207 | int id;
|
---|
208 | };
|
---|
209 |
|
---|
210 | #ifdef ACTOR_STATS
|
---|
211 | // aggregate counters for statistics
|
---|
212 | size_t __total_tries = 0, __total_stolen = 0, __total_workers, __all_gulps = 0,
|
---|
213 | __total_failed_swaps = 0, __all_processed = 0, __num_actors_stats = 0, __all_msgs_stolen = 0;
|
---|
214 | #endif
|
---|
215 | static inline void ?{}( worker & this, cluster & clu, work_queue ** request_queues, copy_queue * current_queue, executor * executor_,
|
---|
216 | unsigned int start, unsigned int range, int id ) {
|
---|
217 | ((thread &)this){ clu };
|
---|
218 | this.request_queues = request_queues; // array of all queues
|
---|
219 | this.current_queue = current_queue; // currently gulped queue (start with empty queue to use in swap later)
|
---|
220 | this.executor_ = executor_; // pointer to current executor
|
---|
221 | this.start = start; // start of worker's subrange of request_queues
|
---|
222 | this.range = range; // size of worker's subrange of request_queues
|
---|
223 | this.id = id; // worker's id and index in array of workers
|
---|
224 | }
|
---|
225 |
|
---|
226 | static bool no_steal = false;
|
---|
227 | struct executor {
|
---|
228 | cluster * cluster; // if workers execute on separate cluster
|
---|
229 | processor ** processors; // array of virtual processors adding parallelism for workers
|
---|
230 | work_queue * request_queues; // master array of work request queues
|
---|
231 | copy_queue * local_queues; // array of all worker local queues to avoid deletion race
|
---|
232 | work_queue ** worker_req_queues; // secondary array of work queues to allow for swapping
|
---|
233 | worker ** workers; // array of workers executing work requests
|
---|
234 | worker_info * w_infos; // array of info about each worker
|
---|
235 | unsigned int nprocessors, nworkers, nrqueues; // number of processors/threads/request queues
|
---|
236 | bool seperate_clus; // use same or separate cluster for executor
|
---|
237 | }; // executor
|
---|
238 |
|
---|
239 | // #ifdef ACTOR_STATS
|
---|
240 | // __spinlock_t out_lock;
|
---|
241 | // #endif
|
---|
242 | static inline void ^?{}( worker & mutex this ) with(this) {
|
---|
243 | #ifdef ACTOR_STATS
|
---|
244 | __atomic_add_fetch(&__all_gulps, executor_->w_infos[id].gulps,__ATOMIC_SEQ_CST);
|
---|
245 | __atomic_add_fetch(&__all_processed, executor_->w_infos[id].processed,__ATOMIC_SEQ_CST);
|
---|
246 | __atomic_add_fetch(&__all_msgs_stolen, executor_->w_infos[id].msgs_stolen,__ATOMIC_SEQ_CST);
|
---|
247 | __atomic_add_fetch(&__total_tries, executor_->w_infos[id].try_steal, __ATOMIC_SEQ_CST);
|
---|
248 | __atomic_add_fetch(&__total_stolen, executor_->w_infos[id].stolen, __ATOMIC_SEQ_CST);
|
---|
249 | __atomic_add_fetch(&__total_failed_swaps, executor_->w_infos[id].failed_swaps, __ATOMIC_SEQ_CST);
|
---|
250 |
|
---|
251 | // per worker steal stats (uncomment alongside the lock above this routine to print)
|
---|
252 | // lock( out_lock __cfaabi_dbg_ctx2 );
|
---|
253 | // printf("Worker id: %d, processed: %llu messages, attempted %lu, stole: %lu, stolen from: %lu\n", id, processed, try_steal, stolen, __atomic_add_fetch(&executor_->w_infos[id].stolen_from, 0, __ATOMIC_SEQ_CST) );
|
---|
254 | // int count = 0;
|
---|
255 | // int count2 = 0;
|
---|
256 | // for ( i; range ) {
|
---|
257 | // if ( replaced_queue[start + i] > 0 ){
|
---|
258 | // count++;
|
---|
259 | // // printf("%d: %u, ",i, replaced_queue[i]);
|
---|
260 | // }
|
---|
261 | // if (__atomic_add_fetch(&stolen_arr[start + i],0,__ATOMIC_SEQ_CST) > 0)
|
---|
262 | // count2++;
|
---|
263 | // }
|
---|
264 | // printf("swapped with: %d of %u indices\n", count, executor_->nrqueues / executor_->nworkers );
|
---|
265 | // printf("%d of %u indices were stolen\n", count2, executor_->nrqueues / executor_->nworkers );
|
---|
266 | // unlock( out_lock );
|
---|
267 | #endif
|
---|
268 | }
|
---|
269 |
|
---|
270 | static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues, bool seperate_clus, size_t buf_size ) with(this) {
|
---|
271 | if ( nrqueues < nworkers ) abort( "nrqueues needs to be >= nworkers\n" );
|
---|
272 | this.nprocessors = nprocessors;
|
---|
273 | this.nworkers = nworkers;
|
---|
274 | this.nrqueues = nrqueues;
|
---|
275 | this.seperate_clus = seperate_clus;
|
---|
276 |
|
---|
277 | if ( nworkers == nrqueues )
|
---|
278 | no_steal = true;
|
---|
279 |
|
---|
280 | #ifdef ACTOR_STATS
|
---|
281 | // stolen_arr = aalloc( nrqueues );
|
---|
282 | // replaced_queue = aalloc( nrqueues );
|
---|
283 | __total_workers = nworkers;
|
---|
284 | #endif
|
---|
285 |
|
---|
286 | if ( seperate_clus ) {
|
---|
287 | cluster = alloc();
|
---|
288 | (*cluster){};
|
---|
289 | } else cluster = active_cluster();
|
---|
290 |
|
---|
291 | request_queues = aalloc( nrqueues );
|
---|
292 | worker_req_queues = aalloc( nrqueues );
|
---|
293 | for ( i; nrqueues ) {
|
---|
294 | request_queues[i]{ buf_size, i };
|
---|
295 | worker_req_queues[i] = &request_queues[i];
|
---|
296 | }
|
---|
297 |
|
---|
298 | processors = aalloc( nprocessors );
|
---|
299 | for ( i; nprocessors )
|
---|
300 | (*(processors[i] = alloc())){ *cluster };
|
---|
301 |
|
---|
302 | local_queues = aalloc( nworkers );
|
---|
303 | workers = aalloc( nworkers );
|
---|
304 | w_infos = aalloc( nworkers );
|
---|
305 | unsigned int reqPerWorker = nrqueues / nworkers, extras = nrqueues % nworkers;
|
---|
306 |
|
---|
307 | for ( i; nworkers ) {
|
---|
308 | w_infos[i]{};
|
---|
309 | local_queues[i]{ buf_size };
|
---|
310 | }
|
---|
311 |
|
---|
312 | for ( unsigned int i = 0, start = 0, range; i < nworkers; i += 1, start += range ) {
|
---|
313 | range = reqPerWorker + ( i < extras ? 1 : 0 );
|
---|
314 | (*(workers[i] = alloc())){ *cluster, worker_req_queues, &local_queues[i], &this, start, range, i };
|
---|
315 | } // for
|
---|
316 | }
|
---|
317 | static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues, bool seperate_clus ) { this{ nprocessors, nworkers, nrqueues, seperate_clus, __DEFAULT_EXECUTOR_BUFSIZE__ }; }
|
---|
318 | static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues ) { this{ nprocessors, nworkers, nrqueues, __DEFAULT_EXECUTOR_SEPCLUS__ }; }
|
---|
319 | static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers ) { this{ nprocessors, nworkers, __DEFAULT_EXECUTOR_RQUEUES__ }; }
|
---|
320 | static inline void ?{}( executor & this, unsigned int nprocessors ) { this{ nprocessors, __DEFAULT_EXECUTOR_WORKERS__ }; }
|
---|
321 | static inline void ?{}( executor & this ) { this{ __DEFAULT_EXECUTOR_PROCESSORS__ }; }
|
---|
322 |
|
---|
323 | static inline void ^?{}( executor & this ) with(this) {
|
---|
324 | #ifdef __STEAL
|
---|
325 | request sentinels[nrqueues];
|
---|
326 | for ( unsigned int i = 0; i < nrqueues; i++ ) {
|
---|
327 | insert( request_queues[i], sentinels[i] ); // force eventually termination
|
---|
328 | } // for
|
---|
329 | #else
|
---|
330 | request sentinels[nworkers];
|
---|
331 | unsigned int reqPerWorker = nrqueues / nworkers, extras = nrqueues % nworkers;
|
---|
332 | for ( unsigned int i = 0, step = 0, range; i < nworkers; i += 1, step += range ) {
|
---|
333 | range = reqPerWorker + ( i < extras ? 1 : 0 );
|
---|
334 | insert( request_queues[step], sentinels[i] ); // force eventually termination
|
---|
335 | } // for
|
---|
336 | #endif
|
---|
337 |
|
---|
338 | for ( i; nworkers )
|
---|
339 | delete( workers[i] );
|
---|
340 |
|
---|
341 | for ( i; nprocessors ) {
|
---|
342 | delete( processors[i] );
|
---|
343 | } // for
|
---|
344 |
|
---|
345 | #ifdef ACTOR_STATS
|
---|
346 | size_t misses = 0;
|
---|
347 | for ( i; nrqueues ) {
|
---|
348 | misses += worker_req_queues[i]->missed;
|
---|
349 | }
|
---|
350 | // adelete( stolen_arr );
|
---|
351 | // adelete( replaced_queue );
|
---|
352 | #endif
|
---|
353 |
|
---|
354 | adelete( workers );
|
---|
355 | adelete( w_infos );
|
---|
356 | adelete( local_queues );
|
---|
357 | adelete( request_queues );
|
---|
358 | adelete( worker_req_queues );
|
---|
359 | adelete( processors );
|
---|
360 | if ( seperate_clus ) delete( cluster );
|
---|
361 |
|
---|
362 | #ifdef ACTOR_STATS // print formatted stats
|
---|
363 | printf(" Actor System Stats:\n");
|
---|
364 | printf("\tActors Created:\t\t\t\t%lu\n\tMessages Sent:\t\t\t\t%lu\n", __num_actors_stats, __all_processed);
|
---|
365 | size_t avg_gulps = __all_gulps == 0 ? 0 : __all_processed / __all_gulps;
|
---|
366 | printf("\tGulps:\t\t\t\t\t%lu\n\tAverage Gulp Size:\t\t\t%lu\n\tMissed gulps:\t\t\t\t%lu\n", __all_gulps, avg_gulps, misses);
|
---|
367 | printf("\tSteal attempts:\t\t\t\t%lu\n\tSteals:\t\t\t\t\t%lu\n\tSteal failures (no candidates):\t\t%lu\n\tSteal failures (failed swaps):\t\t%lu\n",
|
---|
368 | __total_tries, __total_stolen, __total_tries - __total_stolen - __total_failed_swaps, __total_failed_swaps);
|
---|
369 | size_t avg_steal = __total_stolen == 0 ? 0 : __all_msgs_stolen / __total_stolen;
|
---|
370 | printf("\tMessages stolen:\t\t\t%lu\n\tAverage steal size:\t\t\t%lu\n", __all_msgs_stolen, avg_steal);
|
---|
371 | #endif
|
---|
372 |
|
---|
373 | }
|
---|
374 |
|
---|
375 | // this is a static field of executor but have to forward decl for get_next_ticket
|
---|
376 | static size_t __next_ticket = 0;
|
---|
377 |
|
---|
378 | static inline size_t __get_next_ticket( executor & this ) with(this) {
|
---|
379 | #ifdef __CFA_DEBUG__
|
---|
380 | size_t temp = __atomic_fetch_add( &__next_ticket, 1, __ATOMIC_SEQ_CST) % nrqueues;
|
---|
381 |
|
---|
382 | // reserve MAX for dead actors
|
---|
383 | if ( unlikely( temp == MAX ) ) temp = __atomic_fetch_add( &__next_ticket, 1, __ATOMIC_SEQ_CST) % nrqueues;
|
---|
384 | return temp;
|
---|
385 | #else
|
---|
386 | return __atomic_fetch_add( &__next_ticket, 1, __ATOMIC_RELAXED) % nrqueues;
|
---|
387 | #endif
|
---|
388 | } // tickets
|
---|
389 |
|
---|
390 | // TODO: update globals in this file to be static fields once the static fields project is done
|
---|
391 | static executor * __actor_executor_ = 0p;
|
---|
392 | static bool __actor_executor_passed = false; // was an executor passed to start_actor_system
|
---|
393 | static size_t __num_actors_ = 0; // number of actor objects in system
|
---|
394 | static struct thread$ * __actor_executor_thd = 0p; // used to wake executor after actors finish
|
---|
395 | struct actor {
|
---|
396 | size_t ticket; // executor-queue handle
|
---|
397 | allocation allocation_; // allocation action
|
---|
398 | inline virtual_dtor;
|
---|
399 | };
|
---|
400 |
|
---|
401 | static inline void ?{}( actor & this ) with(this) {
|
---|
402 | // Once an actor is allocated it must be sent a message or the actor system cannot stop. Hence, its receive
|
---|
403 | // member must be called to end it
|
---|
404 | DEBUG_ABORT( __actor_executor_ == 0p, "Creating actor before calling start_actor_system() can cause undefined behaviour.\n" );
|
---|
405 | allocation_ = Nodelete;
|
---|
406 | ticket = __get_next_ticket( *__actor_executor_ );
|
---|
407 | __atomic_fetch_add( &__num_actors_, 1, __ATOMIC_RELAXED );
|
---|
408 | #ifdef ACTOR_STATS
|
---|
409 | __atomic_fetch_add( &__num_actors_stats, 1, __ATOMIC_SEQ_CST );
|
---|
410 | #endif
|
---|
411 | }
|
---|
412 |
|
---|
413 | static inline void check_actor( actor & this ) {
|
---|
414 | if ( this.allocation_ != Nodelete ) {
|
---|
415 | switch( this.allocation_ ) {
|
---|
416 | case Delete: delete( &this ); break;
|
---|
417 | case Destroy:
|
---|
418 | CFA_DEBUG( this.ticket = MAX; ); // mark as terminated
|
---|
419 | ^?{}(this);
|
---|
420 | break;
|
---|
421 | case Finished:
|
---|
422 | CFA_DEBUG( this.ticket = MAX; ); // mark as terminated
|
---|
423 | break;
|
---|
424 | default: ; // stop warning
|
---|
425 | }
|
---|
426 |
|
---|
427 | if ( unlikely( __atomic_add_fetch( &__num_actors_, -1, __ATOMIC_RELAXED ) == 0 ) ) { // all actors have terminated
|
---|
428 | unpark( __actor_executor_thd );
|
---|
429 | }
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | struct message {
|
---|
434 | allocation allocation_; // allocation action
|
---|
435 | inline virtual_dtor;
|
---|
436 | };
|
---|
437 |
|
---|
438 | static inline void ?{}( message & this ) {
|
---|
439 | this.allocation_ = Nodelete;
|
---|
440 | }
|
---|
441 | static inline void ?{}( message & this, allocation alloc ) {
|
---|
442 | memcpy( &this.allocation_, &alloc, sizeof(allocation) ); // optimization to elide ctor
|
---|
443 | DEBUG_ABORT( this.allocation_ == Finished, "The Finished allocation status is not supported for message types.\n" );
|
---|
444 | }
|
---|
445 | static inline void ^?{}( message & this ) with(this) {
|
---|
446 | CFA_DEBUG( if ( allocation_ == Nodelete ) printf("A message at location %p was allocated but never sent.\n", &this); )
|
---|
447 | }
|
---|
448 |
|
---|
449 | static inline void check_message( message & this ) {
|
---|
450 | switch ( this.allocation_ ) { // analyze message status
|
---|
451 | case Nodelete: CFA_DEBUG(this.allocation_ = Finished); break;
|
---|
452 | case Delete: delete( &this ); break;
|
---|
453 | case Destroy: ^?{}(this); break;
|
---|
454 | case Finished: break;
|
---|
455 | } // switch
|
---|
456 | }
|
---|
457 | static inline void set_allocation( message & this, allocation state ) {
|
---|
458 | this.allocation_ = state;
|
---|
459 | }
|
---|
460 |
|
---|
461 | static inline void deliver_request( request & this ) {
|
---|
462 | DEBUG_ABORT( this.receiver->ticket == (unsigned long int)MAX, "Attempted to send message to deleted/dead actor\n" );
|
---|
463 | this.receiver->allocation_ = this.fn( *this.receiver, *this.msg );
|
---|
464 | check_message( *this.msg );
|
---|
465 | check_actor( *this.receiver );
|
---|
466 | }
|
---|
467 |
|
---|
468 | // tries to atomically swap two queues and returns 0p if the swap failed
|
---|
469 | // returns ptr to newly owned queue if swap succeeds
|
---|
470 | static inline work_queue * try_swap_queues( worker & this, unsigned int victim_idx, unsigned int my_idx ) with(this) {
|
---|
471 | work_queue * my_queue = request_queues[my_idx];
|
---|
472 | work_queue * other_queue = request_queues[victim_idx];
|
---|
473 |
|
---|
474 | // if either queue is 0p then they are in the process of being stolen
|
---|
475 | if ( other_queue == 0p ) return 0p;
|
---|
476 |
|
---|
477 | // try to set our queue ptr to be 0p. If it fails someone moved our queue so return false
|
---|
478 | if ( !__atomic_compare_exchange_n( &request_queues[my_idx], &my_queue, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ) )
|
---|
479 | return 0p;
|
---|
480 |
|
---|
481 | // try to set other queue ptr to be our queue ptr. If it fails someone moved the other queue so fix up then return false
|
---|
482 | if ( !__atomic_compare_exchange_n( &request_queues[victim_idx], &other_queue, my_queue, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ) ) {
|
---|
483 | /* paranoid */ verify( request_queues[my_idx] == 0p );
|
---|
484 | request_queues[my_idx] = my_queue; // reset my queue ptr back to appropriate val
|
---|
485 | return 0p;
|
---|
486 | }
|
---|
487 |
|
---|
488 | // we have successfully swapped and since our queue is 0p no one will touch it so write back new queue ptr non atomically
|
---|
489 | request_queues[my_idx] = other_queue; // last write does not need to be atomic
|
---|
490 | return other_queue;
|
---|
491 | }
|
---|
492 |
|
---|
493 | // once a worker to steal from has been chosen, choose queue to steal from
|
---|
494 | static inline void choose_queue( worker & this, unsigned int victim_id, unsigned int swap_idx ) with(this) {
|
---|
495 | // have to calculate victim start and range since victim may be deleted before us in shutdown
|
---|
496 | const unsigned int queues_per_worker = executor_->nrqueues / executor_->nworkers;
|
---|
497 | const unsigned int extras = executor_->nrqueues % executor_->nworkers;
|
---|
498 | unsigned int vic_start, vic_range;
|
---|
499 | if ( extras > victim_id ) {
|
---|
500 | vic_range = queues_per_worker + 1;
|
---|
501 | vic_start = vic_range * victim_id;
|
---|
502 | } else {
|
---|
503 | vic_start = extras + victim_id * queues_per_worker;
|
---|
504 | vic_range = queues_per_worker;
|
---|
505 | }
|
---|
506 | unsigned int start_idx = prng( vic_range );
|
---|
507 |
|
---|
508 | unsigned int tries = 0;
|
---|
509 | work_queue * curr_steal_queue;
|
---|
510 |
|
---|
511 | for ( unsigned int i = start_idx; tries < vic_range; i = (i + 1) % vic_range ) {
|
---|
512 | tries++;
|
---|
513 | curr_steal_queue = request_queues[ i + vic_start ];
|
---|
514 | // avoid empty queues and queues that are being operated on
|
---|
515 | if ( curr_steal_queue == 0p || curr_steal_queue->being_processed || isEmpty( *curr_steal_queue->c_queue ) )
|
---|
516 | continue;
|
---|
517 |
|
---|
518 | #ifdef ACTOR_STATS
|
---|
519 | curr_steal_queue = try_swap_queues( this, i + vic_start, swap_idx );
|
---|
520 | if ( curr_steal_queue ) {
|
---|
521 | executor_->w_infos[id].msgs_stolen += curr_steal_queue->c_queue->count;
|
---|
522 | executor_->w_infos[id].stolen++;
|
---|
523 | // __atomic_add_fetch(&executor_->w_infos[victim_id].stolen_from, 1, __ATOMIC_RELAXED);
|
---|
524 | // replaced_queue[swap_idx]++;
|
---|
525 | // __atomic_add_fetch(&stolen_arr[ i + vic_start ], 1, __ATOMIC_RELAXED);
|
---|
526 | } else {
|
---|
527 | executor_->w_infos[id].failed_swaps++;
|
---|
528 | }
|
---|
529 | #else
|
---|
530 | curr_steal_queue = try_swap_queues( this, i + vic_start, swap_idx );
|
---|
531 | #endif // ACTOR_STATS
|
---|
532 |
|
---|
533 | return;
|
---|
534 | }
|
---|
535 |
|
---|
536 | return;
|
---|
537 | }
|
---|
538 |
|
---|
539 | // choose a worker to steal from
|
---|
540 | static inline void steal_work( worker & this, unsigned int swap_idx ) with(this) {
|
---|
541 | #if RAND
|
---|
542 | unsigned int victim = prng( executor_->nworkers );
|
---|
543 | if ( victim == id ) victim = ( victim + 1 ) % executor_->nworkers;
|
---|
544 | choose_queue( this, victim, swap_idx );
|
---|
545 | #elif SEARCH
|
---|
546 | unsigned long long min = MAX; // smaller timestamp means longer since service
|
---|
547 | int min_id = 0; // use ints not uints to avoid integer underflow without hacky math
|
---|
548 | int n_workers = executor_->nworkers;
|
---|
549 | unsigned long long curr_stamp;
|
---|
550 | int scount = 1;
|
---|
551 | for ( int i = (id + 1) % n_workers; scount < n_workers; i = (i + 1) % n_workers, scount++ ) {
|
---|
552 | curr_stamp = executor_->w_infos[i].stamp;
|
---|
553 | if ( curr_stamp < min ) {
|
---|
554 | min = curr_stamp;
|
---|
555 | min_id = i;
|
---|
556 | }
|
---|
557 | }
|
---|
558 | choose_queue( this, min_id, swap_idx );
|
---|
559 | #endif
|
---|
560 | }
|
---|
561 |
|
---|
562 | void main( worker & this ) with(this) {
|
---|
563 | // #ifdef ACTOR_STATS
|
---|
564 | // for ( i; executor_->nrqueues ) {
|
---|
565 | // replaced_queue[i] = 0;
|
---|
566 | // __atomic_store_n( &stolen_arr[i], 0, __ATOMIC_SEQ_CST );
|
---|
567 | // }
|
---|
568 | // #endif
|
---|
569 |
|
---|
570 | // threshold of empty queues we see before we go stealing
|
---|
571 | const unsigned int steal_threshold = 2 * range;
|
---|
572 |
|
---|
573 | // Store variable data here instead of worker struct to avoid any potential false sharing
|
---|
574 | unsigned int empty_count = 0;
|
---|
575 | request & req;
|
---|
576 | work_queue * curr_work_queue;
|
---|
577 |
|
---|
578 | Exit:
|
---|
579 | for ( unsigned int i = 0;; i = (i + 1) % range ) { // cycle through set of request buffers
|
---|
580 | curr_work_queue = request_queues[i + start];
|
---|
581 |
|
---|
582 | // check if queue is empty before trying to gulp it
|
---|
583 | if ( isEmpty( *curr_work_queue->c_queue ) ) {
|
---|
584 | #ifdef __STEAL
|
---|
585 | empty_count++;
|
---|
586 | if ( empty_count < steal_threshold ) continue;
|
---|
587 | #else
|
---|
588 | continue;
|
---|
589 | #endif
|
---|
590 | }
|
---|
591 | transfer( *curr_work_queue, ¤t_queue );
|
---|
592 | #ifdef ACTOR_STATS
|
---|
593 | executor_->w_infos[id].gulps++;
|
---|
594 | #endif // ACTOR_STATS
|
---|
595 | #ifdef __STEAL
|
---|
596 | if ( isEmpty( *current_queue ) ) {
|
---|
597 | if ( unlikely( no_steal ) ) continue;
|
---|
598 | empty_count++;
|
---|
599 | if ( empty_count < steal_threshold ) continue;
|
---|
600 | empty_count = 0;
|
---|
601 |
|
---|
602 | __atomic_store_n( &executor_->w_infos[id].stamp, rdtscl(), __ATOMIC_RELAXED );
|
---|
603 |
|
---|
604 | #ifdef ACTOR_STATS
|
---|
605 | executor_->w_infos[id].try_steal++;
|
---|
606 | #endif // ACTOR_STATS
|
---|
607 |
|
---|
608 | steal_work( this, start + prng( range ) );
|
---|
609 | continue;
|
---|
610 | }
|
---|
611 | #endif // __STEAL
|
---|
612 | while ( ! isEmpty( *current_queue ) ) {
|
---|
613 | #ifdef ACTOR_STATS
|
---|
614 | executor_->w_infos[id].processed++;
|
---|
615 | #endif
|
---|
616 | &req = &remove( *current_queue );
|
---|
617 | if ( !&req ) continue;
|
---|
618 | if ( req.stop ) break Exit;
|
---|
619 | deliver_request( req );
|
---|
620 | }
|
---|
621 | #ifdef __STEAL
|
---|
622 | curr_work_queue->being_processed = false; // set done processing
|
---|
623 | empty_count = 0; // we found work so reset empty counter
|
---|
624 | #endif
|
---|
625 |
|
---|
626 | // potentially reclaim some of the current queue's vector space if it is unused
|
---|
627 | reclaim( *current_queue );
|
---|
628 | } // for
|
---|
629 | }
|
---|
630 |
|
---|
631 | static inline void send( executor & this, request & req, unsigned long int ticket ) with(this) {
|
---|
632 | insert( request_queues[ticket], req);
|
---|
633 | }
|
---|
634 |
|
---|
635 | static inline void send( actor & this, request & req ) {
|
---|
636 | DEBUG_ABORT( this.ticket == (unsigned long int)MAX, "Attempted to send message to deleted/dead actor\n" );
|
---|
637 | send( *__actor_executor_, req, this.ticket );
|
---|
638 | }
|
---|
639 |
|
---|
640 | static inline void __reset_stats() {
|
---|
641 | #ifdef ACTOR_STATS
|
---|
642 | __total_tries = 0;
|
---|
643 | __total_stolen = 0;
|
---|
644 | __all_gulps = 0;
|
---|
645 | __total_failed_swaps = 0;
|
---|
646 | __all_processed = 0;
|
---|
647 | __num_actors_stats = 0;
|
---|
648 | __all_msgs_stolen = 0;
|
---|
649 | #endif
|
---|
650 | }
|
---|
651 |
|
---|
652 | static inline void start_actor_system( size_t num_thds ) {
|
---|
653 | __reset_stats();
|
---|
654 | __actor_executor_thd = active_thread();
|
---|
655 | __actor_executor_ = alloc();
|
---|
656 | (*__actor_executor_){ 0, num_thds, num_thds == 1 ? 1 : num_thds * 16 };
|
---|
657 | }
|
---|
658 |
|
---|
659 | // TODO: potentially revisit getting number of processors
|
---|
660 | // ( currently the value stored in active_cluster()->procs.total is often stale
|
---|
661 | // and doesn't reflect how many procs are allocated )
|
---|
662 | // static inline void start_actor_system() { start_actor_system( active_cluster()->procs.total ); }
|
---|
663 | static inline void start_actor_system() { start_actor_system( 1 ); }
|
---|
664 |
|
---|
665 | static inline void start_actor_system( executor & this ) {
|
---|
666 | __reset_stats();
|
---|
667 | __actor_executor_thd = active_thread();
|
---|
668 | __actor_executor_ = &this;
|
---|
669 | __actor_executor_passed = true;
|
---|
670 | }
|
---|
671 |
|
---|
672 | static inline void stop_actor_system() {
|
---|
673 | park( ); // will receive signal when actor system is finished
|
---|
674 |
|
---|
675 | if ( !__actor_executor_passed ) delete( __actor_executor_ );
|
---|
676 | __actor_executor_ = 0p;
|
---|
677 | __actor_executor_thd = 0p;
|
---|
678 | __next_ticket = 0;
|
---|
679 | __actor_executor_passed = false;
|
---|
680 | }
|
---|
681 |
|
---|
682 | // Default messages to send to any actor to change status
|
---|
683 | // assigned at creation to __base_msg_finished to avoid unused message warning
|
---|
684 | message __base_msg_finished @= { .allocation_ : Finished };
|
---|
685 | struct __delete_msg_t { inline message; } delete_msg = __base_msg_finished;
|
---|
686 | struct __destroy_msg_t { inline message; } destroy_msg = __base_msg_finished;
|
---|
687 | struct __finished_msg_t { inline message; } finished_msg = __base_msg_finished;
|
---|
688 |
|
---|
689 | allocation receive( actor & this, __delete_msg_t & msg ) { return Delete; }
|
---|
690 | allocation receive( actor & this, __destroy_msg_t & msg ) { return Destroy; }
|
---|
691 | allocation receive( actor & this, __finished_msg_t & msg ) { return Finished; }
|
---|
692 |
|
---|