source: libcfa/src/concurrency/kernel.hfa @ b200492

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since b200492 was 0fb3ee5, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

@optim: maximize chances "cache" array stays in cache.

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