| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // kernel -- Header containing the core of the kernel API
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Thierry Delisle
 | 
|---|
| 10 | // Created On       : Tue Jan 17 12:27:26 2017
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Tue Feb  4 12:29:26 2020
 | 
|---|
| 13 | // Update Count     : 22
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #pragma once
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "invoke.h"
 | 
|---|
| 19 | #include "time_t.hfa"
 | 
|---|
| 20 | #include "coroutine.hfa"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "containers/list.hfa"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | extern "C" {
 | 
|---|
| 25 |         #include <bits/pthreadtypes.h>
 | 
|---|
| 26 |         #include <pthread.h>
 | 
|---|
| 27 |         #include <linux/types.h>
 | 
|---|
| 28 | }
 | 
|---|
| 29 | 
 | 
|---|
| 30 | #ifdef __CFA_WITH_VERIFY__
 | 
|---|
| 31 |         extern bool __cfaabi_dbg_in_kernel();
 | 
|---|
| 32 | #endif
 | 
|---|
| 33 | 
 | 
|---|
| 34 | //-----------------------------------------------------------------------------
 | 
|---|
| 35 | // I/O
 | 
|---|
| 36 | struct cluster;
 | 
|---|
| 37 | struct $io_context;
 | 
|---|
| 38 | struct $io_arbiter;
 | 
|---|
| 39 | 
 | 
|---|
| 40 | struct io_context_params {
 | 
|---|
| 41 |         int num_entries;
 | 
|---|
| 42 | };
 | 
|---|
| 43 | 
 | 
|---|
| 44 | void  ?{}(io_context_params & this);
 | 
|---|
| 45 | 
 | 
|---|
| 46 | //-----------------------------------------------------------------------------
 | 
|---|
| 47 | // Processor
 | 
|---|
| 48 | extern struct cluster * mainCluster;
 | 
|---|
| 49 | 
 | 
|---|
| 50 | // Coroutine used py processors for the 2-step context switch
 | 
|---|
| 51 | 
 | 
|---|
| 52 | struct processorCtx_t {
 | 
|---|
| 53 |         struct coroutine$ self;
 | 
|---|
| 54 |         struct processor * proc;
 | 
|---|
| 55 | };
 | 
|---|
| 56 | 
 | 
|---|
| 57 | struct io_future_t;
 | 
|---|
| 58 | 
 | 
|---|
| 59 | // Information needed for idle sleep
 | 
|---|
| 60 | struct __fd_waitctx {
 | 
|---|
| 61 |         // semaphore/future like object
 | 
|---|
| 62 |         // values can be 0, 1 or some file descriptor.
 | 
|---|
| 63 |         // 0 - is the default state
 | 
|---|
| 64 |         // 1 - means the proc should wake-up immediately
 | 
|---|
| 65 |         // FD - means the proc is going asleep and should be woken by writing to the FD.
 | 
|---|
| 66 |         volatile int sem;
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         // The event FD that corresponds to this processor
 | 
|---|
| 69 |         int evfd;
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         // buffer into which the proc will read from evfd
 | 
|---|
| 72 |         // unused if not using io_uring for idle sleep
 | 
|---|
| 73 |         void * rdbuf;
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         // future use to track the read of the eventfd
 | 
|---|
| 76 |         // unused if not using io_uring for idle sleep
 | 
|---|
| 77 |         io_future_t * ftr;
 | 
|---|
| 78 | 
 | 
|---|
| 79 |         volatile unsigned long long wake__time;
 | 
|---|
| 80 |         volatile unsigned long long sleep_time;
 | 
|---|
| 81 |         volatile unsigned long long drain_time;
 | 
|---|
| 82 | };
 | 
|---|
| 83 | 
 | 
|---|
| 84 | // Wrapper around kernel threads
 | 
|---|
| 85 | struct __attribute__((aligned(128))) processor {
 | 
|---|
| 86 |         // Cluster from which to get threads
 | 
|---|
| 87 |         struct cluster * cltr;
 | 
|---|
| 88 | 
 | 
|---|
| 89 |         // Ready Queue state per processor
 | 
|---|
| 90 |         struct {
 | 
|---|
| 91 |                 unsigned short its;
 | 
|---|
| 92 |                 unsigned short itr;
 | 
|---|
| 93 |                 unsigned id;
 | 
|---|
| 94 |                 unsigned target;
 | 
|---|
| 95 |                 unsigned last;
 | 
|---|
| 96 |                 signed   cpu;
 | 
|---|
| 97 |         } rdq;
 | 
|---|
| 98 | 
 | 
|---|
| 99 |         // Set to true to notify the processor should terminate
 | 
|---|
| 100 |         volatile bool do_terminate;
 | 
|---|
| 101 | 
 | 
|---|
| 102 |         // Coroutine ctx who does keeps the state of the processor
 | 
|---|
| 103 |         struct processorCtx_t runner;
 | 
|---|
| 104 | 
 | 
|---|
| 105 |         // Name of the processor
 | 
|---|
| 106 |         const char * name;
 | 
|---|
| 107 | 
 | 
|---|
| 108 |         // Handle to pthreads
 | 
|---|
| 109 |         pthread_t kernel_thread;
 | 
|---|
| 110 | 
 | 
|---|
| 111 |         // Unique id for the processor (not per cluster)
 | 
|---|
| 112 |         unsigned unique_id;
 | 
|---|
| 113 | 
 | 
|---|
| 114 |         struct {
 | 
|---|
| 115 |                 $io_context * ctx;
 | 
|---|
| 116 |                 unsigned target;
 | 
|---|
| 117 |                 volatile bool pending;
 | 
|---|
| 118 |                 volatile bool dirty;
 | 
|---|
| 119 |         } io;
 | 
|---|
| 120 | 
 | 
|---|
| 121 |         // Preemption data
 | 
|---|
| 122 |         // Node which is added in the discrete event simulaiton
 | 
|---|
| 123 |         struct alarm_node_t * preemption_alarm;
 | 
|---|
| 124 | 
 | 
|---|
| 125 |         // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
 | 
|---|
| 126 |         bool pending_preemption;
 | 
|---|
| 127 | 
 | 
|---|
| 128 |         // context for idle sleep
 | 
|---|
| 129 |         struct __fd_waitctx idle_wctx;
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         // Termination synchronisation (user semaphore)
 | 
|---|
| 132 |         oneshot terminated;
 | 
|---|
| 133 | 
 | 
|---|
| 134 |         // pthread Stack
 | 
|---|
| 135 |         void * stack;
 | 
|---|
| 136 | 
 | 
|---|
| 137 |         // Link lists fields
 | 
|---|
| 138 |         inline dlink(processor);
 | 
|---|
| 139 | 
 | 
|---|
| 140 |         // special init fields
 | 
|---|
| 141 |         // This is needed for memcached integration
 | 
|---|
| 142 |         // once memcached experiments are done this should probably be removed
 | 
|---|
| 143 |         // it is not a particularly safe scheme as it can make processors less homogeneous
 | 
|---|
| 144 |         struct {
 | 
|---|
| 145 |                 thread$ * thrd;
 | 
|---|
| 146 |         } init;
 | 
|---|
| 147 | 
 | 
|---|
| 148 |         struct KernelThreadData * local_data;
 | 
|---|
| 149 | 
 | 
|---|
| 150 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| 151 |                 int print_stats;
 | 
|---|
| 152 |                 bool print_halts;
 | 
|---|
| 153 |         #endif
 | 
|---|
| 154 | 
 | 
|---|
| 155 | #ifdef __CFA_DEBUG__
 | 
|---|
| 156 |         // Last function to enable preemption on this processor
 | 
|---|
| 157 |         const char * last_enable;
 | 
|---|
| 158 | #endif
 | 
|---|
| 159 | };
 | 
|---|
| 160 | P9_EMBEDDED( processor, dlink(processor) )
 | 
|---|
| 161 | 
 | 
|---|
| 162 | void  ?{}(processor & this, const char name[], struct cluster & cltr);
 | 
|---|
| 163 | void ^?{}(processor & this);
 | 
|---|
| 164 | 
 | 
|---|
| 165 | static inline void  ?{}(processor & this)                        { this{ "Anonymous Processor", *mainCluster}; }
 | 
|---|
| 166 | static inline void  ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
 | 
|---|
| 167 | static inline void  ?{}(processor & this, const char name[])     { this{name, *mainCluster}; }
 | 
|---|
| 168 | 
 | 
|---|
| 169 | //-----------------------------------------------------------------------------
 | 
|---|
| 170 | // Cluster Tools
 | 
|---|
| 171 | 
 | 
|---|
| 172 | // Intrusives lanes which are used by the ready queue
 | 
|---|
| 173 | struct __attribute__((aligned(128))) __intrusive_lane_t;
 | 
|---|
| 174 | void  ?{}(__intrusive_lane_t & this);
 | 
|---|
| 175 | void ^?{}(__intrusive_lane_t & this);
 | 
|---|
| 176 | 
 | 
|---|
| 177 | // Aligned timestamps which are used by the ready queue and io subsystem
 | 
|---|
| 178 | struct __attribute__((aligned(128))) __timestamp_t {
 | 
|---|
| 179 |         volatile unsigned long long tv;
 | 
|---|
| 180 |         volatile unsigned long long ma;
 | 
|---|
| 181 | };
 | 
|---|
| 182 | 
 | 
|---|
| 183 | static inline void  ?{}(__timestamp_t & this) { this.tv = 0; this.ma = 0; }
 | 
|---|
| 184 | static inline void ^?{}(__timestamp_t &) {}
 | 
|---|
| 185 | 
 | 
|---|
| 186 | 
 | 
|---|
| 187 | struct __attribute__((aligned(16))) __cache_id_t {
 | 
|---|
| 188 |         volatile unsigned id;
 | 
|---|
| 189 | };
 | 
|---|
| 190 | 
 | 
|---|
| 191 | // Idle Sleep
 | 
|---|
| 192 | struct __cluster_proc_list {
 | 
|---|
| 193 |         // Spin lock protecting the queue
 | 
|---|
| 194 |         __spinlock_t lock;
 | 
|---|
| 195 | 
 | 
|---|
| 196 |         // FD to use to wake a processor
 | 
|---|
| 197 |         struct __fd_waitctx * volatile fdw;
 | 
|---|
| 198 | 
 | 
|---|
| 199 |         // Total number of processors
 | 
|---|
| 200 |         unsigned total;
 | 
|---|
| 201 | 
 | 
|---|
| 202 |         // Total number of idle processors
 | 
|---|
| 203 |         unsigned idle;
 | 
|---|
| 204 | 
 | 
|---|
| 205 |         // List of idle processors
 | 
|---|
| 206 |         dlist(processor) idles;
 | 
|---|
| 207 | 
 | 
|---|
| 208 |         // List of active processors
 | 
|---|
| 209 |         dlist(processor) actives;
 | 
|---|
| 210 | };
 | 
|---|
| 211 | 
 | 
|---|
| 212 | //-----------------------------------------------------------------------------
 | 
|---|
| 213 | // Cluster
 | 
|---|
| 214 | struct __attribute__((aligned(128))) cluster {
 | 
|---|
| 215 |         struct {
 | 
|---|
| 216 |                 struct {
 | 
|---|
| 217 |                         // Arary of subqueues
 | 
|---|
| 218 |                         __intrusive_lane_t * data;
 | 
|---|
| 219 | 
 | 
|---|
| 220 |                         // Time since subqueues were processed
 | 
|---|
| 221 |                         __timestamp_t * tscs;
 | 
|---|
| 222 | 
 | 
|---|
| 223 |                         // Number of subqueue / timestamps
 | 
|---|
| 224 |                         size_t count;
 | 
|---|
| 225 |                 } readyQ;
 | 
|---|
| 226 | 
 | 
|---|
| 227 |                 struct {
 | 
|---|
| 228 |                         // Array of $io_
 | 
|---|
| 229 |                         $io_context ** data;
 | 
|---|
| 230 | 
 | 
|---|
| 231 |                         // Time since subqueues were processed
 | 
|---|
| 232 |                         __timestamp_t * tscs;
 | 
|---|
| 233 | 
 | 
|---|
| 234 |                         // Number of I/O subqueues
 | 
|---|
| 235 |                         size_t count;
 | 
|---|
| 236 |                 } io;
 | 
|---|
| 237 | 
 | 
|---|
| 238 |                 // Cache each kernel thread belongs to
 | 
|---|
| 239 |                 __cache_id_t * caches;
 | 
|---|
| 240 |         } sched;
 | 
|---|
| 241 | 
 | 
|---|
| 242 |         // // Ready queue for threads
 | 
|---|
| 243 |         // __ready_queue_t ready_queue;
 | 
|---|
| 244 | 
 | 
|---|
| 245 |         // Name of the cluster
 | 
|---|
| 246 |         const char * name;
 | 
|---|
| 247 | 
 | 
|---|
| 248 |         // Preemption rate on this cluster
 | 
|---|
| 249 |         Duration preemption_rate;
 | 
|---|
| 250 | 
 | 
|---|
| 251 |         // List of idle processors
 | 
|---|
| 252 |         __cluster_proc_list procs;
 | 
|---|
| 253 | 
 | 
|---|
| 254 |         // List of threads
 | 
|---|
| 255 |         __spinlock_t thread_list_lock;
 | 
|---|
| 256 |         __dllist_t(struct thread$) threads;
 | 
|---|
| 257 |         unsigned int nthreads;
 | 
|---|
| 258 | 
 | 
|---|
| 259 |         // Link lists fields
 | 
|---|
| 260 |         struct __dbg_node_cltr {
 | 
|---|
| 261 |                 cluster * next;
 | 
|---|
| 262 |                 cluster * prev;
 | 
|---|
| 263 |         } node;
 | 
|---|
| 264 | 
 | 
|---|
| 265 |         struct {
 | 
|---|
| 266 |                 $io_arbiter * arbiter;
 | 
|---|
| 267 |                 io_context_params params;
 | 
|---|
| 268 |         } io;
 | 
|---|
| 269 | 
 | 
|---|
| 270 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| 271 |                 struct __stats_t * stats;
 | 
|---|
| 272 |                 int print_stats;
 | 
|---|
| 273 |         #endif
 | 
|---|
| 274 | };
 | 
|---|
| 275 | extern Duration default_preemption();
 | 
|---|
| 276 | 
 | 
|---|
| 277 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
 | 
|---|
| 278 | void ^?{}(cluster & this);
 | 
|---|
| 279 | 
 | 
|---|
| 280 | static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; }
 | 
|---|
| 281 | static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; }
 | 
|---|
| 282 | static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; }
 | 
|---|
| 283 | static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; }
 | 
|---|
| 284 | static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io) { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, num_io, default_params}; }
 | 
|---|
| 285 | static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; }
 | 
|---|
| 286 | static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; }
 | 
|---|
| 287 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; }
 | 
|---|
| 288 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; }
 | 
|---|
| 289 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; }
 | 
|---|
| 290 | static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io, const io_context_params & io_params) { this{"Anonymous Cluster", preemption_rate, num_io, io_params}; }
 | 
|---|
| 291 | static inline void ?{} (cluster & this, const char name[], unsigned num_io, const io_context_params & io_params)        { this{name, default_preemption(), num_io, io_params}; }
 | 
|---|
| 292 | 
 | 
|---|
| 293 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
 | 
|---|
| 294 | 
 | 
|---|
| 295 | static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE
 | 
|---|
| 296 | static inline struct cluster   * active_cluster  () { return publicTLS_get( this_processor )->cltr; }
 | 
|---|
| 297 | 
 | 
|---|
| 298 | #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| 299 |         void print_stats_now( cluster & this, int flags );
 | 
|---|
| 300 | 
 | 
|---|
| 301 |         static inline void print_stats_at_exit( cluster & this, int flags ) {
 | 
|---|
| 302 |                 this.print_stats |= flags;
 | 
|---|
| 303 |         }
 | 
|---|
| 304 | 
 | 
|---|
| 305 |         static inline void print_stats_at_exit( processor & this, int flags ) {
 | 
|---|
| 306 |                 this.print_stats |= flags;
 | 
|---|
| 307 |         }
 | 
|---|
| 308 | 
 | 
|---|
| 309 |         void print_halts( processor & this );
 | 
|---|
| 310 | #endif
 | 
|---|
| 311 | 
 | 
|---|
| 312 | // Local Variables: //
 | 
|---|
| 313 | // mode: c //
 | 
|---|
| 314 | // tab-width: 4 //
 | 
|---|
| 315 | // End: //
 | 
|---|