aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/schedule/scalable_ordered.c
blob: 90ddb61c908644451a6913125f339af79322e52c (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* Copyright (c) 2017, ARM Limited. All rights reserved.
 *
 * Copyright (c) 2017, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <odp/api/shared_memory.h>
#include <odp_queue_scalable_internal.h>
#include <odp_schedule_if.h>
#include <odp_bitset.h>

#include <string.h>

extern __thread sched_scalable_thread_state_t *sched_ts;

reorder_window_t *rwin_alloc(_odp_ishm_pool_t *pool, unsigned lock_count)
{
	reorder_window_t *rwin;
	uint32_t i;

	rwin = (reorder_window_t *)
	       shm_pool_alloc_align(pool, sizeof(reorder_window_t));
	if (rwin == NULL)
		return NULL;

	rwin->hc.head = 0;
	rwin->hc.chgi = 0;
	rwin->winmask = RWIN_SIZE - 1;
	rwin->tail = 0;
	rwin->turn = 0;
	rwin->lock_count = (uint16_t)lock_count;
	memset(rwin->olock, 0, sizeof(rwin->olock));
	for (i = 0; i < RWIN_SIZE; i++)
		rwin->ring[i] = NULL;

	return rwin;
}

int rwin_free(_odp_ishm_pool_t *pool, reorder_window_t *rwin)
{
	return _odp_ishm_pool_free(pool, rwin);
}

bool rwin_reserve(reorder_window_t *rwin, uint32_t *sn)
{
	uint32_t head;
	uint32_t oldt;
	uint32_t newt;
	uint32_t winmask;

	/* Read head and tail separately */
	oldt = rwin->tail;
	winmask = rwin->winmask;
	do {
		/* Need __atomic_load to avoid compiler reordering */
		head = __atomic_load_n(&rwin->hc.head, __ATOMIC_RELAXED);
		if (odp_unlikely(oldt - head >= winmask))
			return false;

		newt = oldt + 1;
	} while (!__atomic_compare_exchange(&rwin->tail,
					    &oldt,
					    &newt,
					    true,
					    __ATOMIC_RELAXED,
					    __ATOMIC_RELAXED));
	*sn = oldt;

	return true;
}

void rwin_insert(reorder_window_t *rwin,
		 reorder_context_t *rctx,
		 uint32_t sn,
		 void (*callback)(reorder_context_t *))
{
	/* Initialise to silence scan-build */
	hc_t old = {0, 0};
	hc_t new;
	uint32_t winmask;

	__atomic_load(&rwin->hc, &old, __ATOMIC_ACQUIRE);
	winmask = rwin->winmask;
	if (old.head != sn) {
		/* We are out-of-order. Store context in reorder window,
		 * releasing its content.
		 */
		ODP_ASSERT(rwin->ring[sn & winmask] == NULL);
		atomic_store_release(&rwin->ring[sn & winmask],
				     rctx,
				     /*readonly=*/false);
		rctx = NULL;
		do {
			hc_t new;

			new.head = old.head;
			new.chgi = old.chgi + 1; /* Changed value */
			/* Update head & chgi, fail if any has changed */
			if (__atomic_compare_exchange(&rwin->hc,
						      /* Updated on fail */
						      &old,
						      &new,
						      true,
						      /* Rel our ring update */
						      __ATOMIC_RELEASE,
						      __ATOMIC_ACQUIRE))
				/* CAS succeeded => head same (we are not
				 * in-order), chgi updated.
				 */
				return;
			/* CAS failed => head and/or chgi changed.
			 * We might not be out-of-order anymore.
			 */
		} while (old.head != sn);
	}

	/* old.head == sn => we are now in-order! */
	ODP_ASSERT(old.head == sn);
	/* We are in-order so our responsibility to retire contexts */
	new.head = old.head;
	new.chgi = old.chgi + 1;

	/* Retire our in-order context (if we still have it) */
	if (rctx != NULL) {
		callback(rctx);
		new.head++;
	}

	/* Retire in-order contexts in the ring
	 * The first context might actually be ours (if we were originally
	 * out-of-order)
	 */
	do {
		for (;;) {
			rctx = __atomic_load_n(&rwin->ring[new.head & winmask],
					       __ATOMIC_ACQUIRE);
			if (rctx == NULL)
				break;
			/* We are the only thread that are in-order
			 * (until head updated) so don't have to use
			 * atomic load-and-clear (exchange)
			 */
			rwin->ring[new.head & winmask] = NULL;
			callback(rctx);
			new.head++;
		}
	/* Update head&chgi, fail if chgi has changed (head cannot change) */
	} while (!__atomic_compare_exchange(&rwin->hc,
			&old, /* Updated on failure */
			&new,
			false, /* weak */
			__ATOMIC_RELEASE, /* Release our ring updates */
			__ATOMIC_ACQUIRE));
}

void rctx_init(reorder_context_t *rctx, uint16_t idx,
	       reorder_window_t *rwin, uint32_t sn)
{
	/* rctx->rvec_free and rctx->idx already initialised in
	 * thread_state_init function.
	 */
	ODP_ASSERT(rctx->idx == idx);
	rctx->rwin = rwin;
	rctx->sn = sn;
	rctx->olock_flags = 0;
	/* First => no next reorder context */
	rctx->next_idx = idx;
	/* Where to store next event */
	rctx->cur_idx = idx;
	rctx->numevts = 0;
}

inline void rctx_free(const reorder_context_t *rctx)
{
	const reorder_context_t *const base = &rctx[-(int)rctx->idx];
	const uint32_t first = rctx->idx;
	uint32_t next_idx;

	next_idx = rctx->next_idx;

	ODP_ASSERT(rctx->rwin != NULL);
	/* Set free bit */
	if (rctx->rvec_free == &sched_ts->rvec_free)
		/* Since it is our own reorder context, we can instead
		 * perform a non-atomic and relaxed update on our private
		 * rvec_free.
		 */
		sched_ts->priv_rvec_free =
			bitset_set(sched_ts->priv_rvec_free, rctx->idx);
	else
		atom_bitset_set(rctx->rvec_free, rctx->idx, __ATOMIC_RELEASE);

	/* Can't dereference rctx after the corresponding free bit is set */
	while (next_idx != first) {
		rctx = &base[next_idx];
		next_idx = rctx->next_idx;
		/* Set free bit */
		if (rctx->rvec_free == &sched_ts->rvec_free)
			sched_ts->priv_rvec_free =
				bitset_set(sched_ts->priv_rvec_free, rctx->idx);
		else
			atom_bitset_set(rctx->rvec_free, rctx->idx,
					__ATOMIC_RELEASE);
	}
}

inline void olock_unlock(const reorder_context_t *rctx, reorder_window_t *rwin,
			 uint32_t lock_index)
{
	if ((rctx->olock_flags & (1U << lock_index)) == 0) {
		/* Use relaxed ordering, we are not releasing any updates */
		rwin->olock[lock_index] = rctx->sn + 1;
	}
}

void olock_release(const reorder_context_t *rctx)
{
	reorder_window_t *rwin;
	int i;

	rwin = rctx->rwin;

	for (i = 0; i < rwin->lock_count; i++)
		olock_unlock(rctx, rwin, i);
}

static void blocking_enqueue(queue_entry_t *q, odp_buffer_hdr_t **evts, int num)
{
	int actual;

	/* Iterate until all events have been successfully enqueued */
	for (;;) {
		/* Attempt to enqueue remaining events */
		actual = q->s.enqueue_multi(qentry_to_int(q), evts, num);
		if (odp_unlikely(actual < 0))
			ODP_ERR("Failed to enqueue deferred events\n");
		/* Update for potential partial success */
		evts += actual;
		num -= actual;
		if (num == 0)
			break;
		/* Back-off to decrease load on the system */
		odp_cpu_pause();
	}
}

void rctx_retire(reorder_context_t *first)
{
	reorder_context_t *rctx;
	queue_entry_t *q;
	uint32_t i;
	uint32_t j;
	uint32_t num;

	rctx = first;
	do {
		/* Process all events in this reorder context */
		for (i = 0; i < rctx->numevts;) {
			q = rctx->destq[i];
			/* Find index of next different destq */
			j = i + 1;
			while (j < rctx->numevts && rctx->destq[j] == q)
				j++;
			num = j - i;
			/* Blocking enqueue of events to this destq */
			blocking_enqueue(q, &rctx->events[i], num);
			i += num;
		}
		/* Update rctx pointer to point to 'next_idx' element */
		rctx += (int)rctx->next_idx - (int)rctx->idx;
	} while (rctx != first);
	olock_release(first);
	rctx_free(first);
}

void rctx_release(reorder_context_t *rctx)
{
	/* Insert reorder context into reorder window, potentially calling the
	 * rctx_retire function for all pending reorder_contexts.
	 */
	rwin_insert(rctx->rwin, rctx, rctx->sn, rctx_retire);
}

/* Save destination queue and events in the reorder context for deferred
 * enqueue.
 */
int rctx_save(queue_entry_t *queue, odp_buffer_hdr_t *buf_hdr[], int num)
{
	int i;
	sched_scalable_thread_state_t *ts;
	reorder_context_t *first;
	reorder_context_t *cur;
	bitset_t next_idx;

	ts = sched_ts;
	first = ts->rctx;
	ODP_ASSERT(ts->rctx != NULL);
	cur = &first[(int)first->cur_idx - (int)first->idx];
	for (i = 0; i < num; i++) {
		if (odp_unlikely(cur->numevts == RC_EVT_SIZE)) {
			/* No more space in current reorder context
			 * Try to allocate another.
			 */
			if (odp_unlikely(
				bitset_is_null(ts->priv_rvec_free))) {
				ts->priv_rvec_free =
					atom_bitset_xchg(
						&ts->rvec_free,
						0,
						__ATOMIC_RELAXED);
				if (odp_unlikely(bitset_is_null(
						ts->priv_rvec_free)))
					/* Out of reorder contexts.
					 * Return the number of events
					 * stored so far.
					 */
					return i;
			}
			next_idx = bitset_ffs(ts->priv_rvec_free) - 1;
			ts->priv_rvec_free =
				bitset_clr(ts->priv_rvec_free,
					   next_idx);
			/* Link current to next (for eventual
			 * retiring)
			 */
			cur->next_idx = next_idx;
			/* Link first to next (for next call to
			* queue_enq_multi())
			*/
			first->cur_idx = next_idx;
			/* Update current to next */
			cur = &ts->rvec[next_idx];
			rctx_init(cur, next_idx, NULL, 0);
			/* The last rctx (so far) */
			cur->next_idx = first->idx;
		}
		cur->events[cur->numevts] = buf_hdr[i];
		cur->destq[cur->numevts] = queue;
		cur->numevts++;
	}
	/* All events stored. */
	return num;
}