source: libcfa/src/concurrency/kernel.hfa @ 716b62c

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 716b62c was 089d30c, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Changed cpu schedulig to use moving average.

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