source: libcfa/src/concurrency/kernel.hfa @ 454b4e0

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 454b4e0 was 7cf3b1d, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

Added level of indirection to idle sleeps which helps statistics.

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