[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 |
|
---|
[5dadc9b7] | 131 | #define CFA_CLUSTER_IO_POLLER_USER_THREAD 1 << 0 // 0x1
|
---|
| 132 | #define CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS 1 << 1 // 0x2
|
---|
| 133 | // #define CFA_CLUSTER_IO_POLLER_KERNEL_SIDE 1 << 2 // 0x4
|
---|
[dd4e2d7] | 134 | #define CFA_CLUSTER_IO_BUFFLEN_OFFSET 16
|
---|
[de94a60] | 135 |
|
---|
[7768b8d] | 136 |
|
---|
| 137 | //-----------------------------------------------------------------------------
|
---|
| 138 | // Cluster Tools
|
---|
[dca5802] | 139 |
|
---|
| 140 | // Intrusives lanes which are used by the relaxed ready queue
|
---|
[61d7bec] | 141 | struct __attribute__((aligned(128))) __intrusive_lane_t;
|
---|
[dca5802] | 142 | void ?{}(__intrusive_lane_t & this);
|
---|
| 143 | void ^?{}(__intrusive_lane_t & this);
|
---|
[7768b8d] | 144 |
|
---|
[61d7bec] | 145 | // Counter used for wether or not the lanes are all empty
|
---|
| 146 | struct __attribute__((aligned(128))) __snzi_node_t;
|
---|
| 147 | struct __snzi_t {
|
---|
| 148 | unsigned mask;
|
---|
| 149 | int root;
|
---|
| 150 | __snzi_node_t * nodes;
|
---|
| 151 | };
|
---|
[b798713] | 152 |
|
---|
[61d7bec] | 153 | void ?{}( __snzi_t & this, unsigned depth );
|
---|
| 154 | void ^?{}( __snzi_t & this );
|
---|
[b798713] | 155 |
|
---|
| 156 | //TODO adjust cache size to ARCHITECTURE
|
---|
[dca5802] | 157 | // Structure holding the relaxed ready queue
|
---|
[37ba662] | 158 | struct __ready_queue_t {
|
---|
[dca5802] | 159 | // Data tracking how many/which lanes are used
|
---|
| 160 | // Aligned to 128 for cache locality
|
---|
[61d7bec] | 161 | __snzi_t snzi;
|
---|
[dca5802] | 162 |
|
---|
| 163 | // Data tracking the actual lanes
|
---|
| 164 | // On a seperate cacheline from the used struct since
|
---|
| 165 | // used can change on each push/pop but this data
|
---|
| 166 | // only changes on shrink/grow
|
---|
[37ba662] | 167 | struct {
|
---|
[dca5802] | 168 | // Arary of lanes
|
---|
| 169 | __intrusive_lane_t * volatile data;
|
---|
| 170 |
|
---|
| 171 | // Number of lanes (empty or not)
|
---|
[b798713] | 172 | volatile size_t count;
|
---|
[dca5802] | 173 | } lanes;
|
---|
[b798713] | 174 | };
|
---|
| 175 |
|
---|
| 176 | void ?{}(__ready_queue_t & this);
|
---|
| 177 | void ^?{}(__ready_queue_t & this);
|
---|
| 178 |
|
---|
[de94a60] | 179 | //-----------------------------------------------------------------------------
|
---|
| 180 | // Cluster
|
---|
[37ba662] | 181 | struct __attribute__((aligned(128))) cluster {
|
---|
[de94a60] | 182 | // Ready queue for threads
|
---|
[b798713] | 183 | __ready_queue_t ready_queue;
|
---|
[de94a60] | 184 |
|
---|
| 185 | // Name of the cluster
|
---|
| 186 | const char * name;
|
---|
| 187 |
|
---|
| 188 | // Preemption rate on this cluster
|
---|
| 189 | Duration preemption_rate;
|
---|
| 190 |
|
---|
[64a7146] | 191 | // List of idle processors
|
---|
| 192 | StackLF(processor) idles;
|
---|
[13c5e19] | 193 | volatile unsigned int nprocessors;
|
---|
[de94a60] | 194 |
|
---|
[d4e68a6] | 195 | // List of threads
|
---|
[a1a17a74] | 196 | __spinlock_t thread_list_lock;
|
---|
[ac2b598] | 197 | __dllist_t(struct $thread) threads;
|
---|
[d4e68a6] | 198 | unsigned int nthreads;
|
---|
[a1a17a74] | 199 |
|
---|
[de94a60] | 200 | // Link lists fields
|
---|
[ea8b2f7] | 201 | struct __dbg_node_cltr {
|
---|
[de94a60] | 202 | cluster * next;
|
---|
| 203 | cluster * prev;
|
---|
| 204 | } node;
|
---|
[92976d9] | 205 |
|
---|
[61dd73d] | 206 | struct __io_data * io;
|
---|
[038be32] | 207 |
|
---|
| 208 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
[8834751] | 209 | struct __stats_t * stats;
|
---|
[69fbc61] | 210 | int print_stats;
|
---|
[038be32] | 211 | #endif
|
---|
[de94a60] | 212 | };
|
---|
| 213 | extern Duration default_preemption();
|
---|
| 214 |
|
---|
[dd4e2d7] | 215 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned flags);
|
---|
[de94a60] | 216 | void ^?{}(cluster & this);
|
---|
| 217 |
|
---|
[dd4e2d7] | 218 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption(), 0}; }
|
---|
| 219 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate, 0}; }
|
---|
| 220 | static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption(), 0}; }
|
---|
| 221 | static inline void ?{} (cluster & this, unsigned flags) { this{"Anonymous Cluster", default_preemption(), flags}; }
|
---|
| 222 | static inline void ?{} (cluster & this, Duration preemption_rate, unsigned flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
|
---|
| 223 | static inline void ?{} (cluster & this, const char name[], unsigned flags) { this{name, default_preemption(), flags}; }
|
---|
[de94a60] | 224 |
|
---|
[c7a900a] | 225 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
|
---|
[de94a60] | 226 |
|
---|
[d4e68a6] | 227 | static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
|
---|
| 228 | static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; }
|
---|
| 229 |
|
---|
[038be32] | 230 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
[69fbc61] | 231 | static inline void print_stats_at_exit( cluster & this, int flags ) {
|
---|
| 232 | this.print_stats |= flags;
|
---|
[038be32] | 233 | }
|
---|
[c34ebf2] | 234 |
|
---|
[69fbc61] | 235 | static inline void print_stats_at_exit( processor & this, int flags ) {
|
---|
| 236 | this.print_stats |= flags;
|
---|
[c34ebf2] | 237 | }
|
---|
| 238 |
|
---|
| 239 | void print_halts( processor & this );
|
---|
[038be32] | 240 | #endif
|
---|
| 241 |
|
---|
[8118303] | 242 | // Local Variables: //
|
---|
[6b0b624] | 243 | // mode: c //
|
---|
| 244 | // tab-width: 4 //
|
---|
[8118303] | 245 | // End: //
|
---|