source: libcfa/src/concurrency/kernel.hfa @ 4ecc35a

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 4ecc35a was adb3ea1, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

Some more incremental work towards using timestamps for io fairness

  • Property mode set to 100644
File size: 9.3 KB
Line 
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
24extern "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
36struct cluster;
37struct $io_context;
38struct $io_arbiter;
39
40struct io_context_params {
41        int num_entries;
42};
43
44void  ?{}(io_context_params & this);
45
46//-----------------------------------------------------------------------------
47// Processor
48extern struct cluster * mainCluster;
49
50// Coroutine used py processors for the 2-step context switch
51coroutine processorCtx_t {
52        struct processor * proc;
53};
54
55struct io_future_t;
56
57// Information needed for idle sleep
58struct __fd_waitctx {
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;
76};
77
78// Wrapper around kernel threads
79struct __attribute__((aligned(128))) processor {
80        // Cluster from which to get threads
81        struct cluster * cltr;
82
83        // Ready Queue state per processor
84        struct {
85                unsigned short its;
86                unsigned short itr;
87                unsigned id;
88                unsigned target;
89                unsigned last;
90                signed   cpu;
91        } rdq;
92
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
99        // Name of the processor
100        const char * name;
101
102        // Handle to pthreads
103        pthread_t kernel_thread;
104
105        // Unique id for the processor (not per cluster)
106        unsigned unique_id;
107
108        struct {
109                $io_context * ctx;
110                unsigned id;
111                unsigned target;
112                volatile bool pending;
113                volatile bool dirty;
114        } io;
115
116        // Preemption data
117        // Node which is added in the discrete event simulaiton
118        struct alarm_node_t * preemption_alarm;
119
120        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
121        bool pending_preemption;
122
123        // context for idle sleep
124        struct __fd_waitctx idle_wctx;
125
126        // Termination synchronisation (user semaphore)
127        oneshot terminated;
128
129        // pthread Stack
130        void * stack;
131
132        // Link lists fields
133        inline dlink(processor);
134
135        // special init fields
136        // This is needed for memcached integration
137        // once memcached experiments are done this should probably be removed
138        // it is not a particularly safe scheme as it can make processors less homogeneous
139        struct {
140                thread$ * thrd;
141        } init;
142
143        struct KernelThreadData * local_data;
144
145        #if !defined(__CFA_NO_STATISTICS__)
146                int print_stats;
147                bool print_halts;
148        #endif
149
150#ifdef __CFA_DEBUG__
151        // Last function to enable preemption on this processor
152        const char * last_enable;
153#endif
154};
155P9_EMBEDDED( processor, dlink(processor) )
156
157void  ?{}(processor & this, const char name[], struct cluster & cltr);
158void ^?{}(processor & this);
159
160static inline void  ?{}(processor & this)                        { this{ "Anonymous Processor", *mainCluster}; }
161static inline void  ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
162static inline void  ?{}(processor & this, const char name[])     { this{name, *mainCluster}; }
163
164//-----------------------------------------------------------------------------
165// Cluster Tools
166
167// Intrusives lanes which are used by the ready queue
168struct __attribute__((aligned(128))) __intrusive_lane_t;
169void  ?{}(__intrusive_lane_t & this);
170void ^?{}(__intrusive_lane_t & this);
171
172// Aligned timestamps which are used by the ready queue and io subsystem
173struct __attribute__((aligned(128))) __timestamp_t {
174        volatile unsigned long long tv;
175        volatile unsigned long long ma;
176};
177
178static inline void  ?{}(__timestamp_t & this) { this.tv = 0; this.ma = 0; }
179static inline void ^?{}(__timestamp_t &) {}
180
181
182struct __attribute__((aligned(16))) __cache_id_t {
183        volatile unsigned id;
184};
185
186// Idle Sleep
187struct __cluster_proc_list {
188        // Spin lock protecting the queue
189        __spinlock_t lock;
190
191        // FD to use to wake a processor
192        struct __fd_waitctx * volatile fdw;
193
194        // Total number of processors
195        unsigned total;
196
197        // Total number of idle processors
198        unsigned idle;
199
200        // List of idle processors
201        dlist(processor) idles;
202
203        // List of active processors
204        dlist(processor) actives;
205};
206
207//-----------------------------------------------------------------------------
208// Cluster
209struct __attribute__((aligned(128))) cluster {
210        struct {
211                struct {
212                        // Arary of subqueues
213                        __intrusive_lane_t * data;
214
215                        // Time since subqueues were processed
216                        __timestamp_t * tscs;
217
218                        // Number of subqueue / timestamps
219                        size_t count;
220                } readyQ;
221
222                struct {
223                        // Array of $io_
224                        $io_context ** data;
225
226                        // Time since subqueues were processed
227                        __timestamp_t * tscs;
228
229                        // Number of I/O subqueues
230                        size_t count;
231                } io;
232
233                // Cache each kernel thread belongs to
234                __cache_id_t * caches;
235        } sched;
236
237        // // Ready queue for threads
238        // __ready_queue_t ready_queue;
239
240        // Name of the cluster
241        const char * name;
242
243        // Preemption rate on this cluster
244        Duration preemption_rate;
245
246        // List of idle processors
247        __cluster_proc_list procs;
248
249        // List of threads
250        __spinlock_t thread_list_lock;
251        __dllist_t(struct thread$) threads;
252        unsigned int nthreads;
253
254        // Link lists fields
255        struct __dbg_node_cltr {
256                cluster * next;
257                cluster * prev;
258        } node;
259
260        struct {
261                $io_arbiter * arbiter;
262                io_context_params params;
263        } io;
264
265        #if !defined(__CFA_NO_STATISTICS__)
266                struct __stats_t * stats;
267                int print_stats;
268        #endif
269};
270extern Duration default_preemption();
271
272void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
273void ^?{}(cluster & this);
274
275static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; }
276static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; }
277static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; }
278static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; }
279static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io) { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, num_io, default_params}; }
280static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; }
281static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; }
282static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; }
283static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; }
284static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; }
285static 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}; }
286static inline void ?{} (cluster & this, const char name[], unsigned num_io, const io_context_params & io_params)        { this{name, default_preemption(), num_io, io_params}; }
287
288static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
289
290static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE
291static inline struct cluster   * active_cluster  () { return publicTLS_get( this_processor )->cltr; }
292
293#if !defined(__CFA_NO_STATISTICS__)
294        void print_stats_now( cluster & this, int flags );
295
296        static inline void print_stats_at_exit( cluster & this, int flags ) {
297                this.print_stats |= flags;
298        }
299
300        static inline void print_stats_at_exit( processor & this, int flags ) {
301                this.print_stats |= flags;
302        }
303
304        void print_halts( processor & this );
305#endif
306
307// Local Variables: //
308// mode: c //
309// tab-width: 4 //
310// End: //
Note: See TracBrowser for help on using the repository browser.