| 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 | // Processor id, required for scheduling threads
 | 
|---|
| 51 | 
 | 
|---|
| 52 | 
 | 
|---|
| 53 | coroutine processorCtx_t {
 | 
|---|
| 54 |         struct processor * proc;
 | 
|---|
| 55 | };
 | 
|---|
| 56 | 
 | 
|---|
| 57 | // Wrapper around kernel threads
 | 
|---|
| 58 | struct __attribute__((aligned(128))) processor {
 | 
|---|
| 59 |         // Cluster from which to get threads
 | 
|---|
| 60 |         struct cluster * cltr;
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         // Ready Queue state per processor
 | 
|---|
| 63 |         struct {
 | 
|---|
| 64 |                 unsigned short its;
 | 
|---|
| 65 |                 unsigned short itr;
 | 
|---|
| 66 |                 unsigned id;
 | 
|---|
| 67 |                 unsigned target;
 | 
|---|
| 68 |                 unsigned last;
 | 
|---|
| 69 |                 unsigned cnt;
 | 
|---|
| 70 |                 unsigned long long int cutoff;
 | 
|---|
| 71 |         } rdq;
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         // Set to true to notify the processor should terminate
 | 
|---|
| 74 |         volatile bool do_terminate;
 | 
|---|
| 75 | 
 | 
|---|
| 76 |         // Coroutine ctx who does keeps the state of the processor
 | 
|---|
| 77 |         struct processorCtx_t runner;
 | 
|---|
| 78 | 
 | 
|---|
| 79 |         // Name of the processor
 | 
|---|
| 80 |         const char * name;
 | 
|---|
| 81 | 
 | 
|---|
| 82 |         // Handle to pthreads
 | 
|---|
| 83 |         pthread_t kernel_thread;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         // Unique id for the processor (not per cluster)
 | 
|---|
| 86 |         unsigned unique_id;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         struct {
 | 
|---|
| 89 |                 $io_context * ctx;
 | 
|---|
| 90 |                 bool pending;
 | 
|---|
| 91 |                 bool dirty;
 | 
|---|
| 92 |         } io;
 | 
|---|
| 93 | 
 | 
|---|
| 94 |         // Preemption data
 | 
|---|
| 95 |         // Node which is added in the discrete event simulaiton
 | 
|---|
| 96 |         struct alarm_node_t * preemption_alarm;
 | 
|---|
| 97 | 
 | 
|---|
| 98 |         // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
 | 
|---|
| 99 |         bool pending_preemption;
 | 
|---|
| 100 | 
 | 
|---|
| 101 |         // Idle lock (kernel semaphore)
 | 
|---|
| 102 |         int idle;
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         // Termination synchronisation (user semaphore)
 | 
|---|
| 105 |         oneshot terminated;
 | 
|---|
| 106 | 
 | 
|---|
| 107 |         // pthread Stack
 | 
|---|
| 108 |         void * stack;
 | 
|---|
| 109 | 
 | 
|---|
| 110 |         // Link lists fields
 | 
|---|
| 111 |         inline dlink(processor);
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         // special init fields
 | 
|---|
| 114 |         // This is needed for memcached integration
 | 
|---|
| 115 |         // once memcached experiments are done this should probably be removed
 | 
|---|
| 116 |         // it is not a particularly safe scheme as it can make processors less homogeneous
 | 
|---|
| 117 |         struct {
 | 
|---|
| 118 |                 thread$ * thrd;
 | 
|---|
| 119 |         } init;
 | 
|---|
| 120 | 
 | 
|---|
| 121 |         struct KernelThreadData * local_data;
 | 
|---|
| 122 | 
 | 
|---|
| 123 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| 124 |                 int print_stats;
 | 
|---|
| 125 |                 bool print_halts;
 | 
|---|
| 126 |         #endif
 | 
|---|
| 127 | 
 | 
|---|
| 128 | #ifdef __CFA_DEBUG__
 | 
|---|
| 129 |         // Last function to enable preemption on this processor
 | 
|---|
| 130 |         const char * last_enable;
 | 
|---|
| 131 | #endif
 | 
|---|
| 132 | };
 | 
|---|
| 133 | P9_EMBEDDED( processor, dlink(processor) )
 | 
|---|
| 134 | 
 | 
|---|
| 135 | void  ?{}(processor & this, const char name[], struct cluster & cltr);
 | 
|---|
| 136 | void ^?{}(processor & this);
 | 
|---|
| 137 | 
 | 
|---|
| 138 | static inline void  ?{}(processor & this)                        { this{ "Anonymous Processor", *mainCluster}; }
 | 
|---|
| 139 | static inline void  ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
 | 
|---|
| 140 | static inline void  ?{}(processor & this, const char name[])     { this{name, *mainCluster}; }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | //-----------------------------------------------------------------------------
 | 
|---|
| 143 | // Cluster Tools
 | 
|---|
| 144 | 
 | 
|---|
| 145 | // Intrusives lanes which are used by the ready queue
 | 
|---|
| 146 | struct __attribute__((aligned(128))) __intrusive_lane_t;
 | 
|---|
| 147 | void  ?{}(__intrusive_lane_t & this);
 | 
|---|
| 148 | void ^?{}(__intrusive_lane_t & this);
 | 
|---|
| 149 | 
 | 
|---|
| 150 | // Aligned timestamps which are used by the relaxed ready queue
 | 
|---|
| 151 | struct __attribute__((aligned(128))) __timestamp_t {
 | 
|---|
| 152 |         volatile unsigned long long tv;
 | 
|---|
| 153 |         volatile unsigned long long ma;
 | 
|---|
| 154 | };
 | 
|---|
| 155 | 
 | 
|---|
| 156 | // Aligned timestamps which are used by the relaxed ready queue
 | 
|---|
| 157 | struct __attribute__((aligned(128))) __help_cnts_t {
 | 
|---|
| 158 |         volatile unsigned long long src;
 | 
|---|
| 159 |         volatile unsigned long long dst;
 | 
|---|
| 160 |         volatile unsigned long long tri;
 | 
|---|
| 161 | };
 | 
|---|
| 162 | 
 | 
|---|
| 163 | static inline void  ?{}(__timestamp_t & this) { this.tv = 0; this.ma = 0; }
 | 
|---|
| 164 | static inline void ^?{}(__timestamp_t & this) {}
 | 
|---|
| 165 | 
 | 
|---|
| 166 | //TODO adjust cache size to ARCHITECTURE
 | 
|---|
| 167 | // Structure holding the relaxed ready queue
 | 
|---|
| 168 | struct __ready_queue_t {
 | 
|---|
| 169 |         // Data tracking the actual lanes
 | 
|---|
| 170 |         // On a seperate cacheline from the used struct since
 | 
|---|
| 171 |         // used can change on each push/pop but this data
 | 
|---|
| 172 |         // only changes on shrink/grow
 | 
|---|
| 173 |         struct {
 | 
|---|
| 174 |                 // Arary of lanes
 | 
|---|
| 175 |                 __intrusive_lane_t * volatile data;
 | 
|---|
| 176 | 
 | 
|---|
| 177 |                 // Array of times
 | 
|---|
| 178 |                 __timestamp_t * volatile tscs;
 | 
|---|
| 179 | 
 | 
|---|
| 180 |                 // Array of stats
 | 
|---|
| 181 |                 __help_cnts_t * volatile help;
 | 
|---|
| 182 | 
 | 
|---|
| 183 |                 // Number of lanes (empty or not)
 | 
|---|
| 184 |                 volatile size_t count;
 | 
|---|
| 185 |         } lanes;
 | 
|---|
| 186 | };
 | 
|---|
| 187 | 
 | 
|---|
| 188 | void  ?{}(__ready_queue_t & this);
 | 
|---|
| 189 | void ^?{}(__ready_queue_t & this);
 | 
|---|
| 190 | #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| 191 |         unsigned cnt(const __ready_queue_t & this, unsigned idx);
 | 
|---|
| 192 | #endif
 | 
|---|
| 193 | 
 | 
|---|
| 194 | // Idle Sleep
 | 
|---|
| 195 | struct __cluster_proc_list {
 | 
|---|
| 196 |         // Spin lock protecting the queue
 | 
|---|
| 197 |         volatile uint64_t lock;
 | 
|---|
| 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 |         // Ready queue for threads
 | 
|---|
| 216 |         __ready_queue_t ready_queue;
 | 
|---|
| 217 | 
 | 
|---|
| 218 |         // Name of the cluster
 | 
|---|
| 219 |         const char * name;
 | 
|---|
| 220 | 
 | 
|---|
| 221 |         // Preemption rate on this cluster
 | 
|---|
| 222 |         Duration preemption_rate;
 | 
|---|
| 223 | 
 | 
|---|
| 224 |         // List of idle processors
 | 
|---|
| 225 |         __cluster_proc_list procs;
 | 
|---|
| 226 | 
 | 
|---|
| 227 |         // List of threads
 | 
|---|
| 228 |         __spinlock_t thread_list_lock;
 | 
|---|
| 229 |         __dllist_t(struct thread$) threads;
 | 
|---|
| 230 |         unsigned int nthreads;
 | 
|---|
| 231 | 
 | 
|---|
| 232 |         // Link lists fields
 | 
|---|
| 233 |         struct __dbg_node_cltr {
 | 
|---|
| 234 |                 cluster * next;
 | 
|---|
| 235 |                 cluster * prev;
 | 
|---|
| 236 |         } node;
 | 
|---|
| 237 | 
 | 
|---|
| 238 |         struct {
 | 
|---|
| 239 |                 $io_arbiter * arbiter;
 | 
|---|
| 240 |                 io_context_params params;
 | 
|---|
| 241 |         } io;
 | 
|---|
| 242 | 
 | 
|---|
| 243 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| 244 |                 struct __stats_t * stats;
 | 
|---|
| 245 |                 int print_stats;
 | 
|---|
| 246 |         #endif
 | 
|---|
| 247 | };
 | 
|---|
| 248 | extern Duration default_preemption();
 | 
|---|
| 249 | 
 | 
|---|
| 250 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
 | 
|---|
| 251 | void ^?{}(cluster & this);
 | 
|---|
| 252 | 
 | 
|---|
| 253 | static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; }
 | 
|---|
| 254 | static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; }
 | 
|---|
| 255 | static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; }
 | 
|---|
| 256 | static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; }
 | 
|---|
| 257 | 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}; }
 | 
|---|
| 258 | static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; }
 | 
|---|
| 259 | static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; }
 | 
|---|
| 260 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; }
 | 
|---|
| 261 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; }
 | 
|---|
| 262 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; }
 | 
|---|
| 263 | 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}; }
 | 
|---|
| 264 | 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}; }
 | 
|---|
| 265 | 
 | 
|---|
| 266 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
 | 
|---|
| 267 | 
 | 
|---|
| 268 | static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE
 | 
|---|
| 269 | static inline struct cluster   * active_cluster  () { return publicTLS_get( this_processor )->cltr; }
 | 
|---|
| 270 | 
 | 
|---|
| 271 | #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| 272 |         void print_stats_now( cluster & this, int flags );
 | 
|---|
| 273 | 
 | 
|---|
| 274 |         static inline void print_stats_at_exit( cluster & this, int flags ) {
 | 
|---|
| 275 |                 this.print_stats |= flags;
 | 
|---|
| 276 |         }
 | 
|---|
| 277 | 
 | 
|---|
| 278 |         static inline void print_stats_at_exit( processor & this, int flags ) {
 | 
|---|
| 279 |                 this.print_stats |= flags;
 | 
|---|
| 280 |         }
 | 
|---|
| 281 | 
 | 
|---|
| 282 |         void print_halts( processor & this );
 | 
|---|
| 283 | #endif
 | 
|---|
| 284 | 
 | 
|---|
| 285 | // Local Variables: //
 | 
|---|
| 286 | // mode: c //
 | 
|---|
| 287 | // tab-width: 4 //
 | 
|---|
| 288 | // End: //
 | 
|---|