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

ADTast-experimental
Last change on this file since ae151cf was 160f1aa, checked in by Thierry Delisle <tdelisle@…>, 20 months ago

Changed links of processors in cluster to avoid anonymous names, which are too brittle for gdb to follow

  • Property mode set to 100644
File size: 9.7 KB
RevLine 
[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//
[454f478]7// kernel -- Header containing the core of the kernel API
[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
[bd98b58]18#include "invoke.h"
[73abe95]19#include "time_t.hfa"
[d76bd79]20#include "coroutine.hfa"
[bd98b58]21
[1eb239e4]22#include "containers/list.hfa"
[64a7146]23
[8def349]24extern "C" {
[c402739f]25        #include <bits/pthreadtypes.h>
[454f478]26        #include <pthread.h>
[c402739f]27        #include <linux/types.h>
[8def349]28}
29
[454f478]30#ifdef __CFA_WITH_VERIFY__
31        extern bool __cfaabi_dbg_in_kernel();
32#endif
33
[78da4ab]34//-----------------------------------------------------------------------------
35// I/O
36struct cluster;
[8bee858]37struct io_context$;
38struct io_arbiter$;
[78da4ab]39
40struct io_context_params {
41        int num_entries;
42};
43
44void  ?{}(io_context_params & this);
45
[bd98b58]46//-----------------------------------------------------------------------------
[de94a60]47// Processor
[de6319f]48extern struct cluster * mainCluster;
[bd98b58]49
[22226e4]50// Coroutine used py processors for the 2-step context switch
[c18bf9e]51
52struct processorCtx_t {
53        struct coroutine$ self;
[094476d]54        struct processor * proc;
55};
56
[22226e4]57struct io_future_t;
[7cf3b1d]58
[22226e4]59// Information needed for idle sleep
[7cf3b1d]60struct __fd_waitctx {
[22226e4]61        // semaphore/future like object
62        // values can be 0, 1 or some file descriptor.
63        // 0 - is the default state
64        // 1 - means the proc should wake-up immediately
65        // FD - means the proc is going asleep and should be woken by writing to the FD.
66        volatile int sem;
67
68        // The event FD that corresponds to this processor
69        int evfd;
70
71        // buffer into which the proc will read from evfd
72        // unused if not using io_uring for idle sleep
73        void * rdbuf;
74
75        // future use to track the read of the eventfd
76        // unused if not using io_uring for idle sleep
77        io_future_t * ftr;
[262fafd9]78
[efa28d5]79        volatile unsigned long long wake__time;
[262fafd9]80        volatile unsigned long long sleep_time;
[d28b70a]81        volatile unsigned long long drain_time;
[7cf3b1d]82};
83
[e60e0dc]84// Wrapper around kernel threads
[2af1943]85struct __attribute__((aligned(64))) processor {
[025278e]86        // Cluster from which to get threads
[de94a60]87        struct cluster * cltr;
[025278e]88
[431cd4f]89        // Ready Queue state per processor
90        struct {
91                unsigned short its;
92                unsigned short itr;
93                unsigned id;
94                unsigned target;
[12daa43]95                unsigned last;
[a2a4566]96                signed   cpu;
[431cd4f]97        } rdq;
[bd0bdd37]98
[37ba662]99        // Set to true to notify the processor should terminate
100        volatile bool do_terminate;
101
102        // Coroutine ctx who does keeps the state of the processor
103        struct processorCtx_t runner;
104
[de6319f]105        // Name of the processor
106        const char * name;
107
[025278e]108        // Handle to pthreads
109        pthread_t kernel_thread;
[2ac095d]110
[c993b15]111        // Unique id for the processor (not per cluster)
112        unsigned unique_id;
113
[78da4ab]114        struct {
[8bee858]115                io_context$ * ctx;
[adb3ea1]116                unsigned target;
[d529ad0]117                volatile bool pending;
118                volatile bool dirty;
[78da4ab]119        } io;
120
[e60e0dc]121        // Preemption data
[025278e]122        // Node which is added in the discrete event simulaiton
123        struct alarm_node_t * preemption_alarm;
124
125        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
126        bool pending_preemption;
[c81ebf9]127
[22226e4]128        // context for idle sleep
[7cf3b1d]129        struct __fd_waitctx idle_wctx;
130
[92e7631]131        // Termination synchronisation (user semaphore)
[454f478]132        oneshot terminated;
[de94a60]133
[27f5f71]134        // pthread Stack
135        void * stack;
136
[de94a60]137        // Link lists fields
[160f1aa]138        dlink(processor) link;
[14a61b5]139
[a1538cd]140        // special init fields
141        // This is needed for memcached integration
142        // once memcached experiments are done this should probably be removed
143        // it is not a particularly safe scheme as it can make processors less homogeneous
144        struct {
[e84ab3d]145                thread$ * thrd;
[a1538cd]146        } init;
147
[a67c5b6]148        struct KernelThreadData * local_data;
149
[c34ebf2]150        #if !defined(__CFA_NO_STATISTICS__)
[69fbc61]151                int print_stats;
[c34ebf2]152                bool print_halts;
153        #endif
154
[e60e0dc]155#ifdef __CFA_DEBUG__
[025278e]156        // Last function to enable preemption on this processor
[cdbfab0]157        const char * last_enable;
[e60e0dc]158#endif
[c84e80a]159};
[160f1aa]160// P9_EMBEDDED( processor, dlink(processor) )
161static inline tytagref( dlink(processor), dlink(processor) ) ?`inner( processor & this ) {
162    dlink(processor) & b = this.link;
163    tytagref( dlink(processor), dlink(processor) ) result = { b };
164    return result;
165}
[c84e80a]166
[a5e7233]167void  ?{}(processor & this, const char name[], struct cluster & cltr);
[242a902]168void ^?{}(processor & this);
[c84e80a]169
[a5e7233]170static inline void  ?{}(processor & this)                        { this{ "Anonymous Processor", *mainCluster}; }
171static inline void  ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
172static inline void  ?{}(processor & this, const char name[])     { this{name, *mainCluster}; }
[de6319f]173
[7768b8d]174//-----------------------------------------------------------------------------
175// Cluster Tools
[dca5802]176
[9cc3a18]177// Intrusives lanes which are used by the ready queue
[2af1943]178union __attribute__((aligned(64))) __intrusive_lane_t;
[dca5802]179void  ?{}(__intrusive_lane_t & this);
180void ^?{}(__intrusive_lane_t & this);
[7768b8d]181
[884f3f67]182// Aligned timestamps which are used by the ready queue and io subsystem
[2af1943]183union __attribute__((aligned(64))) __timestamp_t {
184        struct {
185                volatile unsigned long long tv;
186                volatile unsigned long long ma;
187        } t;
188        char __padding[192];
[8cd40bf]189};
190
[2af1943]191static inline void  ?{}(__timestamp_t & this) { this.t.tv = 0; this.t.ma = 0; }
[a892e61]192static inline void ^?{}(__timestamp_t &) {}
[9cc3a18]193
[dca5802]194
[884f3f67]195struct __attribute__((aligned(16))) __cache_id_t {
196        volatile unsigned id;
197};
[9cc3a18]198
[1eb239e4]199// Idle Sleep
[6a9b12b]200struct __cluster_proc_list {
[1eb239e4]201        // Spin lock protecting the queue
[34b8cb7]202        __spinlock_t lock;
203
204        // FD to use to wake a processor
[7cf3b1d]205        struct __fd_waitctx * volatile fdw;
[1eb239e4]206
207        // Total number of processors
208        unsigned total;
209
210        // Total number of idle processors
211        unsigned idle;
212
213        // List of idle processors
[69914cbc]214        dlist(processor) idles;
[fc59b580]215
216        // List of active processors
[69914cbc]217        dlist(processor) actives;
[1eb239e4]218};
219
[de94a60]220//-----------------------------------------------------------------------------
221// Cluster
[2af1943]222struct __attribute__((aligned(64))) cluster {
[884f3f67]223        struct {
224                struct {
225                        // Arary of subqueues
[adb3ea1]226                        __intrusive_lane_t * data;
[884f3f67]227
228                        // Time since subqueues were processed
[adb3ea1]229                        __timestamp_t * tscs;
[884f3f67]230
231                        // Number of subqueue / timestamps
232                        size_t count;
233                } readyQ;
234
235                struct {
[adb3ea1]236                        // Array of $io_
[8bee858]237                        io_context$ ** data;
[adb3ea1]238
[708ae38]239                        // Time since subqueues were processed
[adb3ea1]240                        __timestamp_t * tscs;
[708ae38]241
242                        // Number of I/O subqueues
243                        size_t count;
[884f3f67]244                } io;
245
246                // Cache each kernel thread belongs to
[adb3ea1]247                __cache_id_t * caches;
[884f3f67]248        } sched;
249
250        // // Ready queue for threads
251        // __ready_queue_t ready_queue;
[de94a60]252
253        // Name of the cluster
254        const char * name;
255
256        // Preemption rate on this cluster
257        Duration preemption_rate;
258
[64a7146]259        // List of idle processors
[6a9b12b]260        __cluster_proc_list procs;
[de94a60]261
[d4e68a6]262        // List of threads
[a1a17a7]263        __spinlock_t thread_list_lock;
[e84ab3d]264        __dllist_t(struct thread$) threads;
[d4e68a6]265        unsigned int nthreads;
[a1a17a7]266
[de94a60]267        // Link lists fields
[ea8b2f7]268        struct __dbg_node_cltr {
[de94a60]269                cluster * next;
270                cluster * prev;
271        } node;
[92976d9]272
[f00b26d4]273        struct {
[8bee858]274                io_arbiter$ * arbiter;
[78da4ab]275                io_context_params params;
[f00b26d4]276        } io;
[038be32]277
278        #if !defined(__CFA_NO_STATISTICS__)
[8834751]279                struct __stats_t * stats;
[69fbc61]280                int print_stats;
[038be32]281        #endif
[de94a60]282};
283extern Duration default_preemption();
284
[f00b26d4]285void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
[de94a60]286void ^?{}(cluster & this);
287
[f00b26d4]288static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; }
289static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; }
290static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; }
291static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; }
292static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io) { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, num_io, default_params}; }
293static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; }
294static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; }
295static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; }
296static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; }
297static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; }
298static 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}; }
299static inline void ?{} (cluster & this, const char name[], unsigned num_io, const io_context_params & io_params)        { this{name, default_preemption(), num_io, io_params}; }
[de94a60]300
[c7a900a]301static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]302
[8fc652e0]303static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE
304static inline struct cluster   * active_cluster  () { return publicTLS_get( this_processor )->cltr; }
[d4e68a6]305
[038be32]306#if !defined(__CFA_NO_STATISTICS__)
[8fc652e0]307        void print_stats_now( cluster & this, int flags );
308
[69fbc61]309        static inline void print_stats_at_exit( cluster & this, int flags ) {
310                this.print_stats |= flags;
[038be32]311        }
[c34ebf2]312
[69fbc61]313        static inline void print_stats_at_exit( processor & this, int flags ) {
314                this.print_stats |= flags;
[c34ebf2]315        }
316
317        void print_halts( processor & this );
[038be32]318#endif
319
[8118303]320// Local Variables: //
[6b0b624]321// mode: c //
322// tab-width: 4 //
[8118303]323// End: //
Note: See TracBrowser for help on using the repository browser.