diff options
author | Bjoern Brandenburg <bbb@mpi-sws.org> | 2013-06-25 01:27:07 -0400 |
---|---|---|
committer | Bjoern Brandenburg <bbb@mpi-sws.org> | 2013-08-07 03:46:49 -0400 |
commit | 543810eb67bea9c3046ecb58388493bca39fe796 (patch) | |
tree | cf65010367e53dfbd3e39a9eb6e89dacf92348f3 /litmus/budget.c | |
parent | 1412c8b72e192a14b8dd620f58a75f55a5490783 (diff) |
Add LITMUS^RT core implementation
This patch adds the core of LITMUS^RT:
- library functionality (heaps, rt_domain, prioritization, etc.)
- budget enforcement logic
- job management
- system call backends
- virtual devices (control page, etc.)
- scheduler plugin API (and dummy plugin)
This code compiles, but is not yet integrated with the rest of Linux.
Diffstat (limited to 'litmus/budget.c')
-rw-r--r-- | litmus/budget.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/litmus/budget.c b/litmus/budget.c new file mode 100644 index 000000000000..f7712be29adb --- /dev/null +++ b/litmus/budget.c | |||
@@ -0,0 +1,113 @@ | |||
1 | #include <linux/sched.h> | ||
2 | #include <linux/percpu.h> | ||
3 | #include <linux/hrtimer.h> | ||
4 | |||
5 | #include <litmus/litmus.h> | ||
6 | #include <litmus/preempt.h> | ||
7 | |||
8 | #include <litmus/budget.h> | ||
9 | |||
10 | struct enforcement_timer { | ||
11 | /* The enforcement timer is used to accurately police | ||
12 | * slice budgets. */ | ||
13 | struct hrtimer timer; | ||
14 | int armed; | ||
15 | }; | ||
16 | |||
17 | DEFINE_PER_CPU(struct enforcement_timer, budget_timer); | ||
18 | |||
19 | static enum hrtimer_restart on_enforcement_timeout(struct hrtimer *timer) | ||
20 | { | ||
21 | struct enforcement_timer* et = container_of(timer, | ||
22 | struct enforcement_timer, | ||
23 | timer); | ||
24 | unsigned long flags; | ||
25 | |||
26 | local_irq_save(flags); | ||
27 | TRACE("enforcement timer fired.\n"); | ||
28 | et->armed = 0; | ||
29 | /* activate scheduler */ | ||
30 | litmus_reschedule_local(); | ||
31 | local_irq_restore(flags); | ||
32 | |||
33 | return HRTIMER_NORESTART; | ||
34 | } | ||
35 | |||
36 | /* assumes called with IRQs off */ | ||
37 | static void cancel_enforcement_timer(struct enforcement_timer* et) | ||
38 | { | ||
39 | int ret; | ||
40 | |||
41 | TRACE("cancelling enforcement timer.\n"); | ||
42 | |||
43 | /* Since interrupts are disabled and et->armed is only | ||
44 | * modified locally, we do not need any locks. | ||
45 | */ | ||
46 | |||
47 | if (et->armed) { | ||
48 | ret = hrtimer_try_to_cancel(&et->timer); | ||
49 | /* Should never be inactive. */ | ||
50 | BUG_ON(ret == 0); | ||
51 | /* Should never be running concurrently. */ | ||
52 | BUG_ON(ret == -1); | ||
53 | |||
54 | et->armed = 0; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | /* assumes called with IRQs off */ | ||
59 | static void arm_enforcement_timer(struct enforcement_timer* et, | ||
60 | struct task_struct* t) | ||
61 | { | ||
62 | lt_t when_to_fire; | ||
63 | TRACE_TASK(t, "arming enforcement timer.\n"); | ||
64 | |||
65 | /* Calling this when there is no budget left for the task | ||
66 | * makes no sense, unless the task is non-preemptive. */ | ||
67 | BUG_ON(budget_exhausted(t) && (!is_np(t))); | ||
68 | |||
69 | /* __hrtimer_start_range_ns() cancels the timer | ||
70 | * anyway, so we don't have to check whether it is still armed */ | ||
71 | |||
72 | if (likely(!is_np(t))) { | ||
73 | when_to_fire = litmus_clock() + budget_remaining(t); | ||
74 | __hrtimer_start_range_ns(&et->timer, | ||
75 | ns_to_ktime(when_to_fire), | ||
76 | 0 /* delta */, | ||
77 | HRTIMER_MODE_ABS_PINNED, | ||
78 | 0 /* no wakeup */); | ||
79 | et->armed = 1; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | |||
84 | /* expects to be called with IRQs off */ | ||
85 | void update_enforcement_timer(struct task_struct* t) | ||
86 | { | ||
87 | struct enforcement_timer* et = &__get_cpu_var(budget_timer); | ||
88 | |||
89 | if (t && budget_precisely_enforced(t)) { | ||
90 | /* Make sure we call into the scheduler when this budget | ||
91 | * expires. */ | ||
92 | arm_enforcement_timer(et, t); | ||
93 | } else if (et->armed) { | ||
94 | /* Make sure we don't cause unnecessary interrupts. */ | ||
95 | cancel_enforcement_timer(et); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | |||
100 | static int __init init_budget_enforcement(void) | ||
101 | { | ||
102 | int cpu; | ||
103 | struct enforcement_timer* et; | ||
104 | |||
105 | for (cpu = 0; cpu < NR_CPUS; cpu++) { | ||
106 | et = &per_cpu(budget_timer, cpu); | ||
107 | hrtimer_init(&et->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); | ||
108 | et->timer.function = on_enforcement_timeout; | ||
109 | } | ||
110 | return 0; | ||
111 | } | ||
112 | |||
113 | module_init(init_budget_enforcement); | ||