aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/reservations/table_driven_ext_reservation.c
blob: db9e38195248aef303d6beddd41d5b42096de4c1 (plain) (blame)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <linux/list.h>
#include <linux/sched.h>
#include <linux/smp.h>
#include <linux/slab.h>
#include <linux/hrtimer.h>
#include <asm/uaccess.h>

#include <litmus/litmus.h>
#include <litmus/bheap.h>
#include <litmus/rt_domain.h>
#include <litmus/jobs.h>
#include <litmus/np.h>
#include <litmus/rt_param.h>
#include <litmus/sched_trace.h>
#include <litmus/debug_trace.h>

#include <litmus/reservations/table_driven_ext_reservation.h>

static void mtd_shutdown(
	struct reservation* res)
{
	struct mtd_reservation* mtd_res =
		container_of(res, struct mtd_reservation, res[0]);
	int cpu;

	mtd_res->res[0].env->ops->shutdown(res->env);
	for_each_online_cpu(cpu) {
		clean_up_ext_reservation(&mtd_res->res[cpu]);
		if (mtd_res->num_intervals[cpu])
			kfree(mtd_res->intervals[cpu]);
	}
	kfree(res);
}

static int mtd_is_np(
	struct reservation* res,
	int cpu)
{
	BUG_ON(res->env->ops->is_np(res->env, cpu));
	return 0;
}

static void mtd_on_preempt(
	struct reservation* res,
	int cpu)
{
	res->env->ops->suspend(res->env, cpu);
}

static void mtd_on_schedule(
	struct reservation* res,
	int cpu)
{
	res->env->ops->resume(res->env, cpu);
}

static struct task_struct* mtd_dispatch_client(
	struct reservation* res,
	lt_t* time_slice,
	int cpu)
{
	return res->env->ops->dispatch(res->env, time_slice, cpu);
}

static void mtd_replenish_budget(
	struct reservation* res,
	int cpu)
{
	struct mtd_reservation* mtd_res =
		container_of(res, struct mtd_reservation, res[cpu]);

	BUG_ON(res != &mtd_res->res[cpu]);
	BUG_ON(!mtd_res->num_intervals[cpu]);

	/* calculate next interval index */
	mtd_res->interval_index[cpu] =
		(mtd_res->interval_index[cpu] + 1) % mtd_res->num_intervals[cpu];
	/* if wrap around, then increment major cycle */
	if (!mtd_res->interval_index[cpu])
		mtd_res->major_cycle_start[cpu] += mtd_res->major_cycle;

	mtd_res->cur_interval[cpu].start = mtd_res->intervals[cpu][mtd_res->interval_index[cpu]].start;
	mtd_res->cur_interval[cpu].end = mtd_res->intervals[cpu][mtd_res->interval_index[cpu]].end;

	res->cur_budget = mtd_res->cur_interval[cpu].end - mtd_res->cur_interval[cpu].start;
	res->replenishment_time = mtd_res->major_cycle_start[cpu];
	res->replenishment_time += mtd_res->cur_interval[cpu].start;
}

static void mtd_drain_budget(
	struct reservation* res,
	lt_t how_much,
	int cpu)
{
	struct mtd_reservation* mtd_res;
	lt_t now, end;

	mtd_res = container_of(res, struct mtd_reservation, res[cpu]);

	BUG_ON(res != &mtd_res->res[cpu]);
	BUG_ON(!mtd_res->num_intervals[cpu]);

	now = litmus_clock();
	end = mtd_res->major_cycle_start[cpu] + mtd_res->cur_interval[cpu].end;
	if (now >= end)
		res->cur_budget = 0;
	else
		res->cur_budget = end - now;

	res->env->ops->update_time(res->env, how_much, cpu);
}

static struct reservation_ops mtd_ops = {
	.drain_budget = mtd_drain_budget,
	.replenish_budget = mtd_replenish_budget,
	.dispatch_client = mtd_dispatch_client,
	.on_schedule = mtd_on_schedule,
	.on_preempt = mtd_on_preempt,
	.is_np = mtd_is_np,
	.shutdown = mtd_shutdown
};

/* cannot handle installing table of same core from different threads simultaneously.
 * intervals is passed from config, which is in userspace
 */
long mtd_res_install_table(
	struct mtd_reservation* mtd_res,
	struct lt_interval* intervals,
	lt_t major_cycle,
	unsigned int num_intervals,
	int cpu)
{
	long err;

	if (mtd_res->major_cycle && major_cycle != mtd_res->major_cycle)
		return -EINVAL;

	/* deallocate memory for past table */
	if (mtd_res->num_intervals[cpu])
		kfree(&mtd_res->intervals[cpu]);

	mtd_res->major_cycle = major_cycle;
	mtd_res->num_intervals[cpu] = num_intervals;

	/* allocate kernel memory for intervals */
	mtd_res->intervals[cpu] = kzalloc(sizeof(struct lt_interval) * num_intervals, GFP_KERNEL);
	if (!mtd_res->intervals[cpu])
		return ENOMEM;
	/* copy from user space to kernel space */
	err = copy_from_user(mtd_res->intervals[cpu], intervals, sizeof(struct lt_interval) * num_intervals);
	if (err)
		return err;

	/* reservation always begin executing start on hyperperiod boundaries */
	mtd_res->interval_index[cpu] = 0;
	mtd_res->cur_interval[cpu].start = mtd_res->intervals[cpu][0].start;
	mtd_res->cur_interval[cpu].end = mtd_res->intervals[cpu][0].end;

	return 0;
}

long alloc_mtd_reservation(
	struct mtd_reservation** _res,
	unsigned int id,
	lt_t major_cycle)
{
	struct mtd_reservation* mtd_res;
	int i;

	mtd_res = kzalloc(sizeof(struct mtd_reservation), GFP_KERNEL);
	if (!mtd_res)
		return -ENOMEM;
	for_each_online_cpu(i) {
		init_ext_reservation(&mtd_res->res[i], id, &mtd_ops);
	}
	mtd_res->major_cycle = major_cycle;

	*_res = mtd_res;

	return 0;
}

/* ***************************************************************** */
static int mtd_ready_order(struct bheap_node* a, struct bheap_node* b)
{
	return higher_res_prio(bheap2res(a), bheap2res(b));
}

static void requeue(
	struct mtd_cpu_entry* entry,
	struct reservation* res)
{
	BUG_ON(!res);
	BUG_ON(is_queued_res(res));

	if (lt_before_eq(res->replenishment_time, litmus_clock()))
		__add_ready_res(&entry->domain, res);
	else
		__add_release_res(&entry->domain, res);

}

/* ***************************************************************** */
static void mtd_env_shutdown(
	struct reservation_environment* env)
{
	struct mtd_reservation_environment* mtd_env;
	struct reservation* res;
	unsigned long flags;
	int cpu;

	mtd_env = container_of(env, struct mtd_reservation_environment, env);

	for_each_online_cpu(cpu) {
		domain_suspend_releases(&mtd_env->cpu_entries[cpu].domain);
	}

	raw_spin_lock_irqsave(&mtd_env->insert_lock, flags);

	/* call shutdown on all scheduled reservations */
	while (!list_empty(&env->all_reservations)) {
		res = list_first_entry(&env->all_reservations,
					struct reservation, all_list);
		list_del_init(&res->all_list);
		res->ops->shutdown(res);
	}
	raw_spin_unlock_irqrestore(&mtd_env->insert_lock, flags);

	/* free memory */
	kfree(env);
}

static int mtd_env_is_np(
	struct reservation_environment* env,
	int cpu)
{
	return 1;
}

static struct reservation* mtd_find_res_by_id(
	struct reservation_environment* env,
	int id)
{
	struct reservation* res;
	unsigned long flags;
	struct mtd_reservation_environment* mtd_env =
		container_of(env, struct mtd_reservation_environment, env);

	raw_spin_lock_irqsave(&mtd_env->insert_lock, flags);
	list_for_each_entry(res, &env->all_reservations, all_list) {
		if (res->id == id) {
			raw_spin_unlock_irqrestore(&mtd_env->insert_lock, flags);
			return res;
		}
	}
	raw_spin_unlock_irqrestore(&mtd_env->insert_lock, flags);
	return NULL;
}

/* not supported */
static void mtd_env_remove_res(
	struct reservation_environment* env,
	struct reservation* res,
	int complete,
	int cpu)
{
	return;
}

/* the reservation is added one core at a time due to how the table is specified */
static void mtd_env_add_res(
	struct reservation_environment* env,
	struct reservation* res,
	int cpu)
{
	struct mtd_reservation_environment* mtd_env;
	struct mtd_reservation* mtd_res;
	lt_t tmp;
	unsigned long flags;

	mtd_env = container_of(env, struct mtd_reservation_environment, env);
	mtd_res = container_of(res, struct mtd_reservation, res[cpu]);

	BUG_ON(&mtd_res->res[cpu] != res);

	/* only add to list in reservation if is first core of res to be added */
	raw_spin_lock_irqsave(&mtd_env->insert_lock, flags);
	if (!mtd_res->res[0].par_env) {
		mtd_res->res[0].par_env = env;
		list_add_tail(&mtd_res->res[0].all_list, &env->all_reservations);
	}
	raw_spin_unlock_irqrestore(&mtd_env->insert_lock, flags);

	tmp = div64_u64(litmus_clock(), mtd_res->major_cycle);
	mtd_res->major_cycle_start[cpu] = tmp * mtd_res->major_cycle;

	res->par_env = env;
	if (mtd_res->num_intervals[cpu]) {
		tmp = mtd_res->major_cycle_start[cpu];
		res->replenishment_time = tmp + mtd_res->cur_interval[cpu].start;

		res->cur_budget = mtd_res->cur_interval[cpu].end - mtd_res->cur_interval[cpu].start;

		raw_spin_lock_irqsave(&mtd_env->cpu_entries[cpu].domain.ready_lock, flags);
		requeue(&mtd_env->cpu_entries[cpu], &mtd_res->res[cpu]);
		if (mtd_res->res[cpu].replenishment_time <= litmus_clock())
			litmus_reschedule_local();
		raw_spin_unlock_irqrestore(&mtd_env->cpu_entries[cpu].domain.ready_lock, flags);
	}
}

/* not supported */
static void mtd_env_suspend(
	struct reservation_environment* env,
	int cpu)
{
	return;
}

/* not supported */
static void mtd_env_resume(
	struct reservation_environment* env,
	int cpu)
{
	return;
}

/* If two reservations have overlapping intervals on the same core,
 * then which one is scheduled is undefined
 */
static struct task_struct* mtd_env_dispatch(
	struct reservation_environment* env,
	lt_t* time_slice,
	int cpu)
{
	struct mtd_reservation_environment* mtd_env;
	struct mtd_cpu_entry* entry;
	struct task_struct* next = NULL;
	unsigned long flags;

	mtd_env = container_of(env, struct mtd_reservation_environment, env);
	entry = &mtd_env->cpu_entries[cpu];


	raw_spin_lock_irqsave(&entry->domain.ready_lock, flags);

	/* if linked and scheduled differ, preempt and schedule accordingly */
	if (!entry->linked)
		entry->linked = __take_ready_res(&entry->domain);

	if (entry->scheduled != entry->linked) {
		if (entry->scheduled && entry->scheduled->ops->on_preempt) {
			entry->scheduled->ops->on_preempt(entry->scheduled, cpu);
		}
		if (entry->linked && entry->linked->ops->on_schedule) {
			entry->linked->ops->on_schedule(entry->linked, cpu);
		}
		entry->scheduled = entry->linked;
	}
	raw_spin_unlock_irqrestore(&entry->domain.ready_lock, flags);

	if (entry->scheduled) {
		/* let scheduled reservation decide what runs next */
		next = entry->scheduled->ops->dispatch_client(entry->scheduled, time_slice, cpu);
		*time_slice = (*time_slice > entry->scheduled->cur_budget) ?
			entry->scheduled->cur_budget : *time_slice;
	} else
		*time_slice = ULLONG_MAX;

	return next;
}

static void mtd_env_update_time(
	struct reservation_environment* env,
	lt_t how_much,
	int cpu)
{
	struct mtd_reservation_environment* mtd_env;
	struct mtd_cpu_entry* entry;
	unsigned long flags;

	mtd_env = container_of(env, struct mtd_reservation_environment, env);
	entry = &mtd_env->cpu_entries[cpu];

	/* drains budget of ready task */
	/* In the case that multiple tasks on this core share an execution frame,
	 * only 1 has its budget drained. However, the other tasks will be scheduled
	 * at the end of the frame for epsilon time and immediately have its budget drained
	 * before the task of the next frame is scheduled.
	 * This results in only 1 of the tasks getting actual execution.
	 */
	if (entry->scheduled) {
		entry->scheduled->ops->drain_budget(entry->scheduled, how_much, cpu);
		/* if no more budget, replenish and requeue */
		if (!entry->scheduled->cur_budget) {
			entry->scheduled->ops->replenish_budget(entry->scheduled, cpu);
			raw_spin_lock_irqsave(&entry->domain.ready_lock, flags);
			requeue(entry, entry->scheduled);
			raw_spin_unlock_irqrestore(&entry->domain.ready_lock, flags);
			entry->linked = NULL;
		}
		else {
			entry->linked = entry->scheduled;
		}
	}
}

/* callback for how the domain will release jobs */
static void mtd_env_release_jobs(rt_domain_t* rt, struct bheap* res)
{
	unsigned long flags;

	raw_spin_lock_irqsave(&rt->ready_lock, flags);
	__merge_ready(rt, res);
	litmus_reschedule_local();
	raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
}

static struct reservation_environment_ops mtd_env_ops = {
	.update_time = mtd_env_update_time,
	.dispatch = mtd_env_dispatch,
	.resume = mtd_env_resume,
	.suspend = mtd_env_suspend,
	.add_res = mtd_env_add_res,
	.remove_res = mtd_env_remove_res,
	.find_res_by_id = mtd_find_res_by_id,
	.is_np = mtd_env_is_np,
	.shutdown = mtd_env_shutdown
};

long alloc_mtd_reservation_environment(
	struct mtd_reservation_environment** _env,
	int num_cpus)
{
	struct mtd_reservation_environment* mtd_env;
	int i;

	mtd_env = kzalloc(sizeof(struct mtd_reservation_environment), GFP_KERNEL);
	if (!mtd_env)
		return -ENOMEM;

	memset(mtd_env, 0, sizeof(struct mtd_reservation_environment));

	/* set environment callback actions */
	mtd_env->env.ops = &mtd_env_ops;
	INIT_LIST_HEAD(&mtd_env->env.all_reservations);

	mtd_env->num_cpus = num_cpus;
	for (i = 0; i < num_cpus; i++) {
		mtd_env->cpu_entries[i].id = i;
		/* initialize per cpu domain */
		rt_domain_init(&mtd_env->cpu_entries[i].domain, mtd_ready_order, NULL, mtd_env_release_jobs);
	}

	*_env = mtd_env;
	return 0;
}