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