aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/budget.c
blob: 559c54709acc086ee1e77dcd89853f97ead83d3f (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
#include <linux/sched.h>
#include <linux/percpu.h>
#include <linux/hrtimer.h>
#include <linux/signal.h>

#include <litmus/litmus.h>
#include <litmus/preempt.h>
#include <litmus/sched_plugin.h>
#include <litmus/budget.h>
#include <litmus/signal.h>

inline static void cancel_enforcement_timer(struct task_struct* t)
{
	struct enforcement_timer* et;
	int ret;
	unsigned long flags;

	BUG_ON(!t);
	BUG_ON(!is_realtime(t));

	et = &tsk_rt(t)->budget.timer;

	TRACE("cancelling enforcement timer.\n");

	if (et->armed) {
		raw_spin_lock_irqsave(&et->lock, flags);
		if (et->armed) {
			ret = hrtimer_try_to_cancel(&et->timer);
			et->armed = 0;
		}
		else {
			TRACE("timer was not armed (race).\n");
		}
		raw_spin_unlock_irqrestore(&et->lock, flags);
	}
	else {
		TRACE("timer was not armed.\n");
	}
}

inline static void arm_enforcement_timer(struct task_struct* t)
{
	struct enforcement_timer* et;
	lt_t when_to_fire;
	unsigned long flags;

	BUG_ON(!t);
	BUG_ON(!is_realtime(t));

	et = &tsk_rt(t)->budget.timer;
	if (et->armed) {
		TRACE_TASK(t, "timer already armed!\n");
		return;
	}

	/* Calling this when there is no budget left for the task
	 * makes no sense, unless the task is non-preemptive. */
	if (budget_exhausted(t)) {
		TRACE_TASK(t, "can't arm timer because no budget remaining\n");
		return;
	}

	if ( (!budget_enforced(t) ||
			(budget_enforced(t) && bt_flag_is_set(t, BTF_BUDGET_EXHAUSTED)))
			&&
		(!budget_signalled(t) ||
			(budget_signalled(t) && bt_flag_is_set(t, BTF_SIG_BUDGET_SENT)))) {
		TRACE_TASK(t, "trying to arm timer when budget has already been exhausted.\n");
		return;
	}

	TRACE_TASK(t, "arming enforcement timer.\n");

	/* __hrtimer_start_range_ns() cancels the timer
	 * anyway, so we don't have to check whether it is still armed */
	raw_spin_lock_irqsave(&et->lock, flags);

	if (et->armed) {
		TRACE_TASK(t, "timer already armed (race)!\n");
		goto out;
	}

	when_to_fire = litmus_clock() + budget_remaining(t);

	TRACE_TASK(t, "bremaining: %ld, when_to_fire: %ld\n", budget_remaining(t), when_to_fire);

	__hrtimer_start_range_ns(&et->timer,
				 ns_to_ktime(when_to_fire),
				 0 /* delta */,
				 HRTIMER_MODE_ABS_PINNED,  // TODO: need to use non-pinned?
				 0 /* no wakeup */);
	et->armed = 1;

out:
	raw_spin_unlock_irqrestore(&et->lock, flags);
}


void send_sigbudget(struct task_struct* t)
{
	if (!bt_flag_test_and_set(t, BTF_SIG_BUDGET_SENT)) {
		/* signal has not yet been sent and we are responsible for sending
		 * since we just set the sent-bit when it was previously 0. */

		TRACE_TASK(t, "SIG_BUDGET being sent!\n");
		send_sig(SIG_BUDGET, t, 1); /* '1' denotes signal sent from kernel */
	}
}


void simple_on_scheduled(struct task_struct* t)
{
	BUG_ON(!t);

	if (budget_precisely_tracked(t) && !bt_flag_is_set(t, BTF_SIG_BUDGET_SENT)) {
		BUG_ON(tsk_rt(t)->budget.timer.armed);
		arm_enforcement_timer(t);
	}
}

static void __simple_on_unscheduled(struct task_struct* t)
{
	BUG_ON(!t);

	if (budget_precisely_tracked(t)) {
		cancel_enforcement_timer(t);
	}
}

void simple_on_blocked(struct task_struct* t)
{
	__simple_on_unscheduled(t);
}

void simple_on_preempt_or_sleep(struct task_struct* t)
{
	__simple_on_unscheduled(t);
}

void simple_on_exit(struct task_struct* t)
{
	__simple_on_unscheduled(t);
}




static enum hrtimer_restart __on_timeout(struct hrtimer *timer)
{
	unsigned long flags;
	struct budget_tracker* bt =
		container_of(
			container_of(timer,
						struct enforcement_timer,
						timer),
			struct budget_tracker,
			timer);

	struct task_struct* t =
		container_of(
			container_of(bt, struct rt_param, budget),
			struct task_struct,
			rt_param);

	TRACE_TASK(t, "budget timer interrupt fired at time %lu\n", litmus_clock());

	raw_spin_lock_irqsave(&bt->timer.lock, flags);
	tsk_rt(t)->budget.timer.armed = 0;
	raw_spin_unlock_irqrestore(&bt->timer.lock, flags);

	bt->ops->on_exhausted(t);

	return  HRTIMER_NORESTART;
}


void init_budget_tracker(struct budget_tracker* bt, const struct budget_tracker_ops* ops)
{
	BUG_ON(!bt);
	BUG_ON(!ops);

	BUG_ON(!ops->on_scheduled);
	BUG_ON(!ops->on_blocked);
	BUG_ON(!ops->on_preempt_or_sleep);
	BUG_ON(!ops->on_exhausted);

	memset(bt, 0, sizeof(*bt));
	raw_spin_lock_init(&bt->timer.lock);
	hrtimer_init(&bt->timer.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
	bt->timer.timer.function = __on_timeout;
	bt->ops = ops;
}