[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 | //
|
---|
[75a17f1] | 7 | // kernel --
|
---|
[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 |
|
---|
[c84e80a] | 18 | #include <stdbool.h>
|
---|
[92976d9] | 19 | #include <stdint.h>
|
---|
[8118303] | 20 |
|
---|
[bd98b58] | 21 | #include "invoke.h"
|
---|
[73abe95] | 22 | #include "time_t.hfa"
|
---|
[d76bd79] | 23 | #include "coroutine.hfa"
|
---|
[bd98b58] | 24 |
|
---|
[64a7146] | 25 | #include "containers/stackLockFree.hfa"
|
---|
| 26 |
|
---|
[8def349] | 27 | extern "C" {
|
---|
| 28 | #include <pthread.h>
|
---|
[6b4cdd3] | 29 | #include <semaphore.h>
|
---|
[8def349] | 30 | }
|
---|
| 31 |
|
---|
[db6f06a] | 32 | //-----------------------------------------------------------------------------
|
---|
| 33 | // Locks
|
---|
[bdeba0b] | 34 | struct semaphore {
|
---|
[ea7d2b0] | 35 | __spinlock_t lock;
|
---|
[bdeba0b] | 36 | int count;
|
---|
[ac2b598] | 37 | __queue_t($thread) waiting;
|
---|
[9c31349] | 38 | };
|
---|
| 39 |
|
---|
[242a902] | 40 | void ?{}(semaphore & this, int count = 1);
|
---|
| 41 | void ^?{}(semaphore & this);
|
---|
[71c8b7e] | 42 | bool P (semaphore & this);
|
---|
[f0ce5f4] | 43 | bool V (semaphore & this);
|
---|
[d384787] | 44 | bool V (semaphore & this, unsigned count);
|
---|
[9c31349] | 45 |
|
---|
[db6f06a] | 46 |
|
---|
[bd98b58] | 47 | //-----------------------------------------------------------------------------
|
---|
[de94a60] | 48 | // Processor
|
---|
[de6319f] | 49 | extern struct cluster * mainCluster;
|
---|
[bd98b58] | 50 |
|
---|
[9b1dcc2] | 51 | // Processor id, required for scheduling threads
|
---|
| 52 | struct __processor_id_t {
|
---|
| 53 | unsigned id;
|
---|
[8834751] | 54 |
|
---|
| 55 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 56 | struct __stats_t * stats;
|
---|
| 57 | #endif
|
---|
[9b1dcc2] | 58 | };
|
---|
| 59 |
|
---|
[094476d] | 60 | coroutine processorCtx_t {
|
---|
| 61 | struct processor * proc;
|
---|
| 62 | };
|
---|
| 63 |
|
---|
[e60e0dc] | 64 | // Wrapper around kernel threads
|
---|
[37ba662] | 65 | struct __attribute__((aligned(128))) processor {
|
---|
[e60e0dc] | 66 | // Main state
|
---|
[37ba662] | 67 | inline __processor_id_t;
|
---|
[025278e] | 68 |
|
---|
| 69 | // Cluster from which to get threads
|
---|
[de94a60] | 70 | struct cluster * cltr;
|
---|
[025278e] | 71 |
|
---|
[37ba662] | 72 | // Set to true to notify the processor should terminate
|
---|
| 73 | volatile bool do_terminate;
|
---|
| 74 |
|
---|
| 75 | // Coroutine ctx who does keeps the state of the processor
|
---|
| 76 | struct processorCtx_t runner;
|
---|
| 77 |
|
---|
[de6319f] | 78 | // Name of the processor
|
---|
| 79 | const char * name;
|
---|
| 80 |
|
---|
[025278e] | 81 | // Handle to pthreads
|
---|
| 82 | pthread_t kernel_thread;
|
---|
[2ac095d] | 83 |
|
---|
[e60e0dc] | 84 | // RunThread data
|
---|
[025278e] | 85 | // Action to do after a thread is ran
|
---|
[ac2b598] | 86 | $thread * destroyer;
|
---|
[c81ebf9] | 87 |
|
---|
[e60e0dc] | 88 | // Preemption data
|
---|
[025278e] | 89 | // Node which is added in the discrete event simulaiton
|
---|
| 90 | struct alarm_node_t * preemption_alarm;
|
---|
| 91 |
|
---|
| 92 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
|
---|
| 93 | bool pending_preemption;
|
---|
[c81ebf9] | 94 |
|
---|
[92e7631] | 95 | // Idle lock (kernel semaphore)
|
---|
| 96 | __bin_sem_t idle;
|
---|
[85b1deb] | 97 |
|
---|
[92e7631] | 98 | // Termination synchronisation (user semaphore)
|
---|
[85b1deb] | 99 | semaphore terminated;
|
---|
[de94a60] | 100 |
|
---|
[27f5f71] | 101 | // pthread Stack
|
---|
| 102 | void * stack;
|
---|
| 103 |
|
---|
[de94a60] | 104 | // Link lists fields
|
---|
[64a7146] | 105 | Link(processor) link;
|
---|
[14a61b5] | 106 |
|
---|
[c34ebf2] | 107 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
[69fbc61] | 108 | int print_stats;
|
---|
[c34ebf2] | 109 | bool print_halts;
|
---|
| 110 | #endif
|
---|
| 111 |
|
---|
[e60e0dc] | 112 | #ifdef __CFA_DEBUG__
|
---|
[025278e] | 113 | // Last function to enable preemption on this processor
|
---|
[cdbfab0] | 114 | const char * last_enable;
|
---|
[e60e0dc] | 115 | #endif
|
---|
[c84e80a] | 116 | };
|
---|
| 117 |
|
---|
[e3fea42] | 118 | void ?{}(processor & this, const char name[], struct cluster & cltr);
|
---|
[242a902] | 119 | void ^?{}(processor & this);
|
---|
[c84e80a] | 120 |
|
---|
[de6319f] | 121 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
|
---|
[de94a60] | 122 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
|
---|
[e3fea42] | 123 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
|
---|
[de6319f] | 124 |
|
---|
[0f89d4f] | 125 | static inline Link(processor) * ?`next( processor * this ) { return &this->link; }
|
---|
[de94a60] | 126 |
|
---|
[92976d9] | 127 | //-----------------------------------------------------------------------------
|
---|
| 128 | // I/O
|
---|
[61dd73d] | 129 | struct __io_data;
|
---|
[92976d9] | 130 |
|
---|
[47746a2] | 131 | #define CFA_CLUSTER_IO_POLLER_USER_THREAD (1 << 0) // 0x01
|
---|
| 132 | #define CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS (1 << 1) // 0x02
|
---|
| 133 | #define CFA_CLUSTER_IO_EAGER_SUBMITS (1 << 2) // 0x04
|
---|
| 134 | #define CFA_CLUSTER_IO_KERNEL_POLL_SUBMITS (1 << 3) // 0x08
|
---|
| 135 | #define CFA_CLUSTER_IO_KERNEL_POLL_COMPLETES (1 << 4) // 0x10
|
---|
[dd4e2d7] | 136 | #define CFA_CLUSTER_IO_BUFFLEN_OFFSET 16
|
---|
[de94a60] | 137 |
|
---|
[7768b8d] | 138 |
|
---|
| 139 | //-----------------------------------------------------------------------------
|
---|
| 140 | // Cluster Tools
|
---|
[dca5802] | 141 |
|
---|
| 142 | // Intrusives lanes which are used by the relaxed ready queue
|
---|
[61d7bec] | 143 | struct __attribute__((aligned(128))) __intrusive_lane_t;
|
---|
[dca5802] | 144 | void ?{}(__intrusive_lane_t & this);
|
---|
| 145 | void ^?{}(__intrusive_lane_t & this);
|
---|
[7768b8d] | 146 |
|
---|
[61d7bec] | 147 | // Counter used for wether or not the lanes are all empty
|
---|
| 148 | struct __attribute__((aligned(128))) __snzi_node_t;
|
---|
| 149 | struct __snzi_t {
|
---|
| 150 | unsigned mask;
|
---|
| 151 | int root;
|
---|
| 152 | __snzi_node_t * nodes;
|
---|
| 153 | };
|
---|
[b798713] | 154 |
|
---|
[61d7bec] | 155 | void ?{}( __snzi_t & this, unsigned depth );
|
---|
| 156 | void ^?{}( __snzi_t & this );
|
---|
[b798713] | 157 |
|
---|
| 158 | //TODO adjust cache size to ARCHITECTURE
|
---|
[dca5802] | 159 | // Structure holding the relaxed ready queue
|
---|
[37ba662] | 160 | struct __ready_queue_t {
|
---|
[dca5802] | 161 | // Data tracking how many/which lanes are used
|
---|
| 162 | // Aligned to 128 for cache locality
|
---|
[61d7bec] | 163 | __snzi_t snzi;
|
---|
[dca5802] | 164 |
|
---|
| 165 | // Data tracking the actual lanes
|
---|
| 166 | // On a seperate cacheline from the used struct since
|
---|
| 167 | // used can change on each push/pop but this data
|
---|
| 168 | // only changes on shrink/grow
|
---|
[37ba662] | 169 | struct {
|
---|
[dca5802] | 170 | // Arary of lanes
|
---|
| 171 | __intrusive_lane_t * volatile data;
|
---|
| 172 |
|
---|
| 173 | // Number of lanes (empty or not)
|
---|
[b798713] | 174 | volatile size_t count;
|
---|
[dca5802] | 175 | } lanes;
|
---|
[b798713] | 176 | };
|
---|
| 177 |
|
---|
| 178 | void ?{}(__ready_queue_t & this);
|
---|
| 179 | void ^?{}(__ready_queue_t & this);
|
---|
| 180 |
|
---|
[de94a60] | 181 | //-----------------------------------------------------------------------------
|
---|
| 182 | // Cluster
|
---|
[37ba662] | 183 | struct __attribute__((aligned(128))) cluster {
|
---|
[de94a60] | 184 | // Ready queue for threads
|
---|
[b798713] | 185 | __ready_queue_t ready_queue;
|
---|
[de94a60] | 186 |
|
---|
| 187 | // Name of the cluster
|
---|
| 188 | const char * name;
|
---|
| 189 |
|
---|
| 190 | // Preemption rate on this cluster
|
---|
| 191 | Duration preemption_rate;
|
---|
| 192 |
|
---|
[64a7146] | 193 | // List of idle processors
|
---|
| 194 | StackLF(processor) idles;
|
---|
[13c5e19] | 195 | volatile unsigned int nprocessors;
|
---|
[de94a60] | 196 |
|
---|
[d4e68a6] | 197 | // List of threads
|
---|
[a1a17a74] | 198 | __spinlock_t thread_list_lock;
|
---|
[ac2b598] | 199 | __dllist_t(struct $thread) threads;
|
---|
[d4e68a6] | 200 | unsigned int nthreads;
|
---|
[a1a17a74] | 201 |
|
---|
[de94a60] | 202 | // Link lists fields
|
---|
[ea8b2f7] | 203 | struct __dbg_node_cltr {
|
---|
[de94a60] | 204 | cluster * next;
|
---|
| 205 | cluster * prev;
|
---|
| 206 | } node;
|
---|
[92976d9] | 207 |
|
---|
[61dd73d] | 208 | struct __io_data * io;
|
---|
[038be32] | 209 |
|
---|
| 210 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
[8834751] | 211 | struct __stats_t * stats;
|
---|
[69fbc61] | 212 | int print_stats;
|
---|
[038be32] | 213 | #endif
|
---|
[de94a60] | 214 | };
|
---|
| 215 | extern Duration default_preemption();
|
---|
| 216 |
|
---|
[dd4e2d7] | 217 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned flags);
|
---|
[de94a60] | 218 | void ^?{}(cluster & this);
|
---|
| 219 |
|
---|
[dd4e2d7] | 220 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption(), 0}; }
|
---|
| 221 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate, 0}; }
|
---|
| 222 | static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption(), 0}; }
|
---|
| 223 | static inline void ?{} (cluster & this, unsigned flags) { this{"Anonymous Cluster", default_preemption(), flags}; }
|
---|
| 224 | static inline void ?{} (cluster & this, Duration preemption_rate, unsigned flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
|
---|
| 225 | static inline void ?{} (cluster & this, const char name[], unsigned flags) { this{name, default_preemption(), flags}; }
|
---|
[de94a60] | 226 |
|
---|
[c7a900a] | 227 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
|
---|
[de94a60] | 228 |
|
---|
[d4e68a6] | 229 | static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
|
---|
| 230 | static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; }
|
---|
| 231 |
|
---|
[038be32] | 232 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
[69fbc61] | 233 | static inline void print_stats_at_exit( cluster & this, int flags ) {
|
---|
| 234 | this.print_stats |= flags;
|
---|
[038be32] | 235 | }
|
---|
[c34ebf2] | 236 |
|
---|
[69fbc61] | 237 | static inline void print_stats_at_exit( processor & this, int flags ) {
|
---|
| 238 | this.print_stats |= flags;
|
---|
[c34ebf2] | 239 | }
|
---|
| 240 |
|
---|
| 241 | void print_halts( processor & this );
|
---|
[038be32] | 242 | #endif
|
---|
| 243 |
|
---|
[8118303] | 244 | // Local Variables: //
|
---|
[6b0b624] | 245 | // mode: c //
|
---|
| 246 | // tab-width: 4 //
|
---|
[8118303] | 247 | // End: //
|
---|