source: libcfa/src/concurrency/actor.hfa@ 8607a72

ADT ast-experimental
Last change on this file since 8607a72 was efdd18c, checked in by caparson <caparson@…>, 2 years ago

updated actor header to use better name for stat toggling

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