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