[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 |
|
---|
[8def349] | 25 | extern "C" {
|
---|
| 26 | #include <pthread.h>
|
---|
[6b4cdd3] | 27 | #include <semaphore.h>
|
---|
[8def349] | 28 | }
|
---|
| 29 |
|
---|
[db6f06a] | 30 | //-----------------------------------------------------------------------------
|
---|
| 31 | // Locks
|
---|
[bdeba0b] | 32 | struct semaphore {
|
---|
[ea7d2b0] | 33 | __spinlock_t lock;
|
---|
[bdeba0b] | 34 | int count;
|
---|
[ac2b598] | 35 | __queue_t($thread) waiting;
|
---|
[9c31349] | 36 | };
|
---|
| 37 |
|
---|
[242a902] | 38 | void ?{}(semaphore & this, int count = 1);
|
---|
| 39 | void ^?{}(semaphore & this);
|
---|
[4cedd9f] | 40 | void P (semaphore & this);
|
---|
[f0ce5f4] | 41 | bool V (semaphore & this);
|
---|
[9c31349] | 42 |
|
---|
[db6f06a] | 43 |
|
---|
[bd98b58] | 44 | //-----------------------------------------------------------------------------
|
---|
[de94a60] | 45 | // Processor
|
---|
[de6319f] | 46 | extern struct cluster * mainCluster;
|
---|
[bd98b58] | 47 |
|
---|
[e60e0dc] | 48 | // Processor
|
---|
[094476d] | 49 | coroutine processorCtx_t {
|
---|
| 50 | struct processor * proc;
|
---|
| 51 | };
|
---|
| 52 |
|
---|
[e60e0dc] | 53 | // Wrapper around kernel threads
|
---|
[c84e80a] | 54 | struct processor {
|
---|
[e60e0dc] | 55 | // Main state
|
---|
[025278e] | 56 | // Coroutine ctx who does keeps the state of the processor
|
---|
[094476d] | 57 | struct processorCtx_t runner;
|
---|
[025278e] | 58 |
|
---|
| 59 | // Cluster from which to get threads
|
---|
[de94a60] | 60 | struct cluster * cltr;
|
---|
[025278e] | 61 |
|
---|
[de6319f] | 62 | // Name of the processor
|
---|
| 63 | const char * name;
|
---|
| 64 |
|
---|
[025278e] | 65 | // Handle to pthreads
|
---|
| 66 | pthread_t kernel_thread;
|
---|
[2ac095d] | 67 |
|
---|
[e60e0dc] | 68 | // RunThread data
|
---|
[025278e] | 69 | // Action to do after a thread is ran
|
---|
[ac2b598] | 70 | $thread * destroyer;
|
---|
[c81ebf9] | 71 |
|
---|
[e60e0dc] | 72 | // Preemption data
|
---|
[025278e] | 73 | // Node which is added in the discrete event simulaiton
|
---|
| 74 | struct alarm_node_t * preemption_alarm;
|
---|
| 75 |
|
---|
| 76 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
|
---|
| 77 | bool pending_preemption;
|
---|
[c81ebf9] | 78 |
|
---|
[92e7631] | 79 | // Idle lock (kernel semaphore)
|
---|
| 80 | __bin_sem_t idle;
|
---|
[85b1deb] | 81 |
|
---|
| 82 | // Termination
|
---|
| 83 | // Set to true to notify the processor should terminate
|
---|
| 84 | volatile bool do_terminate;
|
---|
| 85 |
|
---|
[92e7631] | 86 | // Termination synchronisation (user semaphore)
|
---|
[85b1deb] | 87 | semaphore terminated;
|
---|
[de94a60] | 88 |
|
---|
[27f5f71] | 89 | // pthread Stack
|
---|
| 90 | void * stack;
|
---|
| 91 |
|
---|
[de94a60] | 92 | // Link lists fields
|
---|
[ea8b2f7] | 93 | struct __dbg_node_proc {
|
---|
[de94a60] | 94 | struct processor * next;
|
---|
| 95 | struct processor * prev;
|
---|
| 96 | } node;
|
---|
[14a61b5] | 97 |
|
---|
[e60e0dc] | 98 | #ifdef __CFA_DEBUG__
|
---|
[025278e] | 99 | // Last function to enable preemption on this processor
|
---|
[cdbfab0] | 100 | const char * last_enable;
|
---|
[e60e0dc] | 101 | #endif
|
---|
[c84e80a] | 102 | };
|
---|
| 103 |
|
---|
[e3fea42] | 104 | void ?{}(processor & this, const char name[], struct cluster & cltr);
|
---|
[242a902] | 105 | void ^?{}(processor & this);
|
---|
[c84e80a] | 106 |
|
---|
[de6319f] | 107 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
|
---|
[de94a60] | 108 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
|
---|
[e3fea42] | 109 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
|
---|
[de6319f] | 110 |
|
---|
[c7a900a] | 111 | static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; }
|
---|
[de94a60] | 112 |
|
---|
[92976d9] | 113 | //-----------------------------------------------------------------------------
|
---|
| 114 | // I/O
|
---|
| 115 | #if defined(HAVE_LINUX_IO_URING_H)
|
---|
| 116 | struct io_uring_sq {
|
---|
[2d8f7b0] | 117 | // Head and tail of the ring (associated with array)
|
---|
| 118 | volatile uint32_t * head;
|
---|
| 119 | volatile uint32_t * tail;
|
---|
| 120 |
|
---|
| 121 | // The actual kernel ring which uses head/tail
|
---|
| 122 | // indexes into the sqes arrays
|
---|
| 123 | uint32_t * array;
|
---|
| 124 |
|
---|
| 125 | // number of entries and mask to go with it
|
---|
| 126 | const uint32_t * num;
|
---|
| 127 | const uint32_t * mask;
|
---|
| 128 |
|
---|
| 129 | // Submission flags (Not sure what for)
|
---|
[92976d9] | 130 | uint32_t * flags;
|
---|
[2d8f7b0] | 131 |
|
---|
| 132 | // number of sqes not submitted (whatever that means)
|
---|
[92976d9] | 133 | uint32_t * dropped;
|
---|
| 134 |
|
---|
[2d8f7b0] | 135 | // Like head/tail but not seen by the kernel
|
---|
| 136 | volatile uint32_t alloc;
|
---|
[92976d9] | 137 |
|
---|
[2d8f7b0] | 138 | __spinlock_t lock;
|
---|
| 139 |
|
---|
| 140 | // A buffer of sqes (not the actual ring)
|
---|
| 141 | struct io_uring_sqe * sqes;
|
---|
| 142 |
|
---|
| 143 | // The location and size of the mmaped area
|
---|
[92976d9] | 144 | void * ring_ptr;
|
---|
[2d8f7b0] | 145 | size_t ring_sz;
|
---|
[92976d9] | 146 | };
|
---|
| 147 |
|
---|
| 148 | struct io_uring_cq {
|
---|
[2d8f7b0] | 149 | // Head and tail of the ring
|
---|
[92976d9] | 150 | volatile uint32_t * head;
|
---|
| 151 | volatile uint32_t * tail;
|
---|
[2d8f7b0] | 152 |
|
---|
| 153 | // number of entries and mask to go with it
|
---|
| 154 | const uint32_t * mask;
|
---|
| 155 | const uint32_t * num;
|
---|
| 156 |
|
---|
| 157 | // number of cqes not submitted (whatever that means)
|
---|
[92976d9] | 158 | uint32_t * overflow;
|
---|
[2d8f7b0] | 159 |
|
---|
| 160 | // the kernel ring
|
---|
[92976d9] | 161 | struct io_uring_cqe * cqes;
|
---|
| 162 |
|
---|
[2d8f7b0] | 163 | // The location and size of the mmaped area
|
---|
[92976d9] | 164 | void * ring_ptr;
|
---|
[2d8f7b0] | 165 | size_t ring_sz;
|
---|
[92976d9] | 166 | };
|
---|
| 167 |
|
---|
| 168 | struct io_ring {
|
---|
| 169 | struct io_uring_sq submit_q;
|
---|
| 170 | struct io_uring_cq completion_q;
|
---|
| 171 | uint32_t flags;
|
---|
| 172 | int fd;
|
---|
| 173 | pthread_t poller;
|
---|
| 174 | void * stack;
|
---|
| 175 | volatile bool done;
|
---|
[2d8f7b0] | 176 | semaphore submit;
|
---|
[92976d9] | 177 | };
|
---|
| 178 | #endif
|
---|
| 179 |
|
---|
[de94a60] | 180 | //-----------------------------------------------------------------------------
|
---|
| 181 | // Cluster
|
---|
| 182 | struct cluster {
|
---|
| 183 | // Ready queue locks
|
---|
| 184 | __spinlock_t ready_queue_lock;
|
---|
| 185 |
|
---|
| 186 | // Ready queue for threads
|
---|
[ac2b598] | 187 | __queue_t($thread) ready_queue;
|
---|
[de94a60] | 188 |
|
---|
| 189 | // Name of the cluster
|
---|
| 190 | const char * name;
|
---|
| 191 |
|
---|
| 192 | // Preemption rate on this cluster
|
---|
| 193 | Duration preemption_rate;
|
---|
| 194 |
|
---|
| 195 | // List of processors
|
---|
[92e7631] | 196 | __spinlock_t idle_lock;
|
---|
[de94a60] | 197 | __dllist_t(struct processor) procs;
|
---|
| 198 | __dllist_t(struct processor) idles;
|
---|
[d4e68a6] | 199 | unsigned int nprocessors;
|
---|
[de94a60] | 200 |
|
---|
[d4e68a6] | 201 | // List of threads
|
---|
[a1a17a74] | 202 | __spinlock_t thread_list_lock;
|
---|
[ac2b598] | 203 | __dllist_t(struct $thread) threads;
|
---|
[d4e68a6] | 204 | unsigned int nthreads;
|
---|
[a1a17a74] | 205 |
|
---|
[de94a60] | 206 | // Link lists fields
|
---|
[ea8b2f7] | 207 | struct __dbg_node_cltr {
|
---|
[de94a60] | 208 | cluster * next;
|
---|
| 209 | cluster * prev;
|
---|
| 210 | } node;
|
---|
[92976d9] | 211 |
|
---|
| 212 | #if defined(HAVE_LINUX_IO_URING_H)
|
---|
| 213 | struct io_ring io;
|
---|
| 214 | #endif
|
---|
[de94a60] | 215 | };
|
---|
| 216 | extern Duration default_preemption();
|
---|
| 217 |
|
---|
[e3fea42] | 218 | void ?{} (cluster & this, const char name[], Duration preemption_rate);
|
---|
[de94a60] | 219 | void ^?{}(cluster & this);
|
---|
| 220 |
|
---|
| 221 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; }
|
---|
| 222 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
|
---|
[e3fea42] | 223 | static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption()}; }
|
---|
[de94a60] | 224 |
|
---|
[c7a900a] | 225 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
|
---|
[de94a60] | 226 |
|
---|
[0f2c555] | 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; }
|
---|
[d4e68a6] | 229 |
|
---|
[8118303] | 230 | // Local Variables: //
|
---|
[6b0b624] | 231 | // mode: c //
|
---|
| 232 | // tab-width: 4 //
|
---|
[8118303] | 233 | // End: //
|
---|