-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutine_sched.h
235 lines (200 loc) · 7.66 KB
/
routine_sched.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef ROUTINE_SCHED_H
#define ROUTINE_SCHED_H
#include "piece.h"
#include "util/objects.h"
#include "util/linklist.h"
#include "node_config.h"
namespace felis {
struct PriorityQueueValue : public util::GenericListNode<PriorityQueueValue> {
PieceRoutine *routine;
BasePieceCollection::ExecutionRoutine *state;
};
/** state of a waiting ExecutionRoutine (preempted) */
struct WaitState {
BasePieceCollection::ExecutionRoutine *state; /*!< go::Routine of the caller of preempt */
uint64_t sched_key; /*!< scheduling key of the caller routine of the preempt */
uint64_t preempt_ts; /*!< never used */
uint64_t preempt_key; /*!< used to order the waiting routines, allows backoff */
uint64_t preempt_times; /*!< number of times that this routine is preempted */
};
// for use ordering WaitStates in a heap
// as this performs a > b instead of a < b, this functions as a min-heap
static bool Greater(const WaitState &a, const WaitState &b) {
if (a.preempt_key > b.preempt_key)
return true;
if (a.preempt_key < b.preempt_key)
return false;
//edge case, overlap, use original sched key to tie-break
return a.sched_key > b.sched_key;
}
struct PriorityQueueHashEntry : public util::GenericListNode<PriorityQueueHashEntry> {
util::GenericListNode<PriorityQueueValue> values; /*!< list of Pieces with the same priority as in key */
uint64_t key; /*!< sched_key of the collection of PieceRoutines */
};
static constexpr size_t kPriorityQueuePoolElementSize =
std::max(sizeof(PriorityQueueValue), sizeof(PriorityQueueHashEntry));
static_assert(kPriorityQueuePoolElementSize < 64);
// Interface for priority scheduling
class PrioritySchedulingPolicy {
protected:
size_t len = 0;
public:
virtual ~PrioritySchedulingPolicy() {}
bool empty() { return len == 0; }
// Before we try to schedule from this scheduling policy, should we double
// check the zero queue?
virtual bool ShouldRetryBeforePick(std::atomic_ulong *zq_start, std::atomic_ulong *zq_end,
std::atomic_uint *pq_start, std::atomic_uint *pq_end) {
return false;
}
// Would you pick this key according to the current situation?
virtual bool ShouldPickWaiting(const WaitState &ws) = 0;
// Pick a value without consuming it.
virtual PriorityQueueValue *Pick() = 0;
// Consume (delete) the value from the scheduling structure.
virtual void Consume(PriorityQueueValue *value) = 0;
virtual void IngestPending(PriorityQueueHashEntry *hent, PriorityQueueValue *value) = 0;
virtual void Reset() {}
};
class CoroSched;
class ConservativePriorityScheduler final : public PrioritySchedulingPolicy {
friend CoroSched;
~ConservativePriorityScheduler() {}
struct PriorityQueueHeapEntry {
uint64_t key;
PriorityQueueHashEntry *ent;
};
static bool Greater(const PriorityQueueHeapEntry &a, const PriorityQueueHeapEntry &b) {
return a.key > b.key;
}
bool ShouldPickWaiting(const WaitState &ws) override;
/**
* Picks the next PieceRoutine to run from the prority queue.
* @return Next PieceRoutine to run (and its state)
*/
PriorityQueueValue *Pick() override;
/**
* Remove a priority queue entry from the PQ and collects garbage in hashtable if needed.
* @param value Priority queue entry to be removed from the queue.
*/
void Consume(PriorityQueueValue *value) override;
void IngestPending(PriorityQueueHashEntry *hent, PriorityQueueValue *value) override;
void Reset() override {
abort_if(len > 0, "Reset() called, but len {} > 0", len);
}
public:
static ConservativePriorityScheduler *New(size_t maxlen, int numa_node);
private:
PriorityQueueHeapEntry q[]; /*!< The actual priority queue */
};
// For scheduling transactions during execution
class EpochExecutionDispatchService : public PromiseRoutineDispatchService {
friend CoroSched;
template <typename T> friend T &util::Instance() noexcept;
EpochExecutionDispatchService();
using ExecutionRoutine = BasePieceCollection::ExecutionRoutine;
struct CompleteCounter {
ulong completed;
CompleteCounter() : completed(0) {}
};
public:
static unsigned int Hash(uint64_t key) { return key >> 8; }
static constexpr int kOutOfOrderWindow = 25;
static constexpr int keyThreshold = 17000;
static constexpr uint64_t max_backoff = 40;
using PriorityQueueHashHeader = util::GenericListNode<PriorityQueueHashEntry>;
private:
// This is not a normal priority queue because lots of priorities are
// duplicates! Therefore, we use a hashtable to deduplicate them.
struct PriorityQueue {
PrioritySchedulingPolicy *sched_pol; /*!< This is where the actual priority queue locates */
PriorityQueueHashHeader *ht; /*!< Hashtable. First item is a sentinel */
struct {
PieceRoutine **q;
std::atomic_uint start;
std::atomic_uint end;
} pending; /*!< Pending inserts into the heap and the hashtable */
struct {
// Min-heap
WaitState states[kOutOfOrderWindow];
uint32_t unique_preempts; /*!< number of successful preempts, statistics tracking */
uint32_t len; /*!< number of waiting ExecutionRoutines */
} waiting; /*!< a priority queue of waiting ExecutionRoutines */
mem::Brk brk; /*!< memory allocator for hashtables entries and queue values */
};
/** A none wrapping buffer of PieceRoutine ptrs */
struct ZeroQueue {
PieceRoutine **q;
std::atomic_ulong end;
std::atomic_ulong start;
};
struct State {
uint64_t current_sched_key;
uint64_t ts; /*!< Monotonic incrementing timestamp, but never used. */
CompleteCounter complete_counter; /*!< Local counter of completed piece routines. */
static constexpr int kSleeping = 0;
static constexpr int kRunning = 1;
static constexpr int kDeciding = -1;
std::atomic_int running;
State() : current_sched_key(0), ts(0), running(kSleeping) {}
};
struct Queue {
PriorityQueue pq;
ZeroQueue zq;
util::SpinLock lock;
State state;
};
public:
static size_t g_max_item;
private:
static const size_t kHashTableSize;
static constexpr size_t kMaxNrThreads = NodeConfiguration::kMaxNrThreads;
std::array<Queue *, kMaxNrThreads> queues;
std::atomic_ulong tot_bubbles;
private:
/**
* Adds a PieceRoutine into the hashtable and the actual priority queue inside the sched_pol.
* @param q
* @param r
* @param state
*/
void AddToPriorityQueue(PriorityQueue &q, PieceRoutine *&r,
BasePieceCollection::ExecutionRoutine *state = nullptr);
/**
* Process the PieceRoutines that are added to the pending buffer of the PQ but not the hash table.
* @param q
*/
void ProcessPending(PriorityQueue &q);
public:
/**
* Add a number of PieceRoutines to the per core scheduler queue.
* @param core_id
* @param routines
* @param nr_routines
*/
void Add(int core_id, PieceRoutine **routines, size_t nr_routines) final override;
void AddBubble() final override;
bool Peek(int core_id, DispatchPeekListener &should_pop) final override;
/**
* Pushes a ExecutionRoutine onto the waiting queue of the scheduler if possible
* @param core_id
* @param state
* @param sid
* @param ver
* @return
*/
bool Preempt(int core_id, BasePieceCollection::ExecutionRoutine *state, uint64_t sid, uint64_t ver) final override;
void Reset() final override;
void Complete(int core_id) final override;
int TraceDependency(uint64_t key) final override;
bool IsRunning(int core_id) final override {
auto &s = queues[core_id]->state;
int running = -1;
while ((running = s.running.load(std::memory_order_seq_cst)) == State::kDeciding)
_mm_pause();
return running == State::kRunning;
}
bool IsReady(int core_id) final override;
};
}
#endif /* SCHED_H */