aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/event_group.c
blob: 22c74a19d1d6f6136027c834207295c8ba55c9ac (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
#include <linux/slab.h>
#include <linux/sched.h>

#include <litmus/litmus.h>
#include <litmus/trace.h>
#include <litmus/event_group.h>

/*
 * Return event_queue slot for the given time.
 */
static unsigned int time2slot(lt_t time)
{
	return (unsigned int) time2quanta(time, FLOOR) % EVENT_QUEUE_SLOTS;
}

/*
 * Executes events from an event_list in priority order.
 * Events can requeue themselves when they are called.
 */
static enum hrtimer_restart on_timer(struct hrtimer *timer)
{
	unsigned long flags;
	struct event_list *el;
	struct rt_event *e;
	struct list_head *pos, *safe, list;

	el = container_of(timer, struct event_list, timer);

	raw_spin_lock_irqsave(&el->group->queue_lock, flags);
	list_del_init(&el->list);
	raw_spin_unlock_irqrestore(&el->group->queue_lock, flags);

	/* Empty event list so this event can be requeued */
	list_replace_init(&el->events, &list);

	/* Fire events */
	list_for_each_safe(pos, safe, &list) {
		e = list_entry(pos, struct rt_event, list);
		TRACE("Dequeueing event with prio %d\n", e->prio);
		list_del_init(pos);
		e->fire(e->data);
	}
	return HRTIMER_NORESTART;
}

/*
 * Insert event in event-list, respecting priority order.
 */
void insert_event(struct event_list *el, struct rt_event *e)
{
	struct list_head *pos;
	struct rt_event *queued;
	list_for_each_prev(pos, &el->events) {
		queued = list_entry(pos, struct rt_event, list);
		if (e->prio < queued->prio) {
			__list_add(&e->list, pos, pos->next);
			return;
		}
	}
	list_add(&e->list, &el->events);
}

/*
 * Return event_list for the given event and time. If no event_list
 * is being used yet and use_event_heap is 1, will create the list
 * and return it. Otherwise it will return NULL.
 */
static struct event_list *get_event_list(struct event_group *group,
				    struct rt_event *e,
				    lt_t fire,
				    int use_event_heap)
{
	struct list_head* pos;
	struct event_list *el = NULL, *tmp;
	unsigned int slot = time2slot(fire);

	/* initialize pos for the case that the list is empty */
	pos = group->event_queue[slot].next;
	list_for_each(pos, &group->event_queue[slot]) {
		tmp = list_entry(pos, struct event_list, list);
		if (lt_after_eq(fire, tmp->fire_time) &&
		    lt_before(tmp->fire_time, fire)) {
			/* perfect match -- this happens on hyperperiod
			 * boundaries
			 */
			el = tmp;
			break;
		} else if (lt_before(fire, tmp->fire_time)) {
			/* we need to insert a new node since rh is
			 * already in the future
			 */
			break;
		}
	}
	if (!el && use_event_heap) {
		/* use pre-allocated release heap */
		tmp = e->event_list;
		tmp->fire_time = fire;
		tmp->group = group;
		/* add to queue */
		list_add(&tmp->list, pos->prev);
		el = tmp;
	}
	return el;
}

/*
 * Prepare a release heap for a new set of events.
 */
static void reinit_event_list(struct rt_event *e)
{
	struct event_list *el = e->event_list;
	BUG_ON(hrtimer_cancel(&el->timer));
	INIT_LIST_HEAD(&el->events);
	atomic_set(&el->info.state, HRTIMER_START_ON_INACTIVE);
}

/**
 * add_event() - Add timer to event group.
 */
void add_event(struct event_group *group, struct rt_event *e, lt_t fire)
{
	struct event_list *el;

	TRACE("Adding event with prio %d @ %llu\n", event->prio, fire);

	raw_spin_lock(&group->queue_lock);
	el = get_event_list(group, e, fire, 0);
	if (!el) {
		/* Use our own, but drop lock first */
		raw_spin_unlock(&group->queue_lock);
		reinit_event_list(e);
		raw_spin_lock(&group->queue_lock);
		el = get_event_list(group, e, fire, 1);
	}

	/* Add event to sorted list */
	insert_event(el, e);
	raw_spin_unlock(&group->queue_lock);

	/* Arm timer if we are the owner */
	if (el == e->event_list) {
		TRACE("Arming timer for %llu\n", fire);
		if (group->cpu == smp_processor_id()) {
			__hrtimer_start_range_ns(&el->timer,
						 ns_to_ktime(el->fire_time),
						 0, HRTIMER_MODE_ABS_PINNED, 0);
		} else {
			hrtimer_start_on(group->cpu, &el->info,
					 &el->timer, ns_to_ktime(el->fire_time),
					 HRTIMER_MODE_ABS_PINNED);
		}
	} else {
		TRACE("Not my timer @%llu", fire);
	}
}

/**
 * cancel_event() - Remove event from the group.
 */
void cancel_event(struct event_group *group, struct rt_event *e)
{
	raw_spin_lock(&group->queue_lock);
	list_del_init(&e->list);
	raw_spin_unlock(&group->queue_lock);
}

/**
 * init_event_group() - Prepare group for events.
 */
void init_event_group(struct event_group *group, lt_t res, int cpu)
{
	int i;
	group->res = res;
	group->cpu = cpu;

	for (i = 0; i < EVENT_QUEUE_SLOTS; i++) {
		INIT_LIST_HEAD(&group->event_queue[i]);
	}

	raw_spin_lock_init(&group->queue_lock);
}

struct kmem_cache *event_list_cache;
struct event_list * event_list_alloc(int gfp_flags)
{
	struct event_list *el = kmem_cache_alloc(event_list_cache, gfp_flags);
	if (el) {
		hrtimer_init(&el->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
		INIT_LIST_HEAD(&el->list);
		el->timer.function = on_timer;
	}
	return el;
}

void event_list_free(struct event_list *el)
{
	hrtimer_cancel(&el->timer);
	kmem_cache_free(event_list_cache, el);
}