aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/dgl.c
blob: 9749597aa76952792b55264852da92f06f32f153 (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
#include <linux/sched.h>
#include <litmus/litmus.h>
#include <litmus/dgl.h>
#include <litmus/sched_trace.h>

/* Word, bit -> resource id */
#define ri(w, b) (w * MASK_SIZE + b)

 /* For loop, where @i iterates over each set bit in @bit_arr */
#define for_each_resource(bit_arr, w, b, i)                                    \
	 for(w = 0; w < MASK_WORDS; ++w)				       \
		 for(b = find_first_bit(&bit_arr[w],MASK_SIZE), i = ri(w, b);  \
		     b < MASK_SIZE;					       \
		     b = find_next_bit(&bit_arr[w],MASK_SIZE,b+1), i = ri(w, b))

/* Return resource id in dgl @d for resource @r */
#define resource_id(d, r) ((((void*)r) - (void*)(&(d)->resources))/ sizeof(*r))

/* Return request group of req @r for resource @i */
#define req_group(r, i) (container_of(((void*)r) - sizeof(*r)*(i),	\
				      struct dgl_group_req, requests))

/* Resource id -> word, bit */
static inline void mask_idx(int resource, int *word, int *bit)
{
	*word = resource / MASK_SIZE;
	*bit  = resource % MASK_SIZE;
}

void dgl_init(struct dgl *dgl)
{
	int i;
	struct dgl_resource *resource;

	for (i = 0; i < NR_CPUS; i++)
		dgl->acquired[i] = NULL;

	for (i = 0; i < NUM_RESOURCES; i++) {
		resource = &dgl->resources[i];

		INIT_LIST_HEAD(&resource->waiting);
		resource->free_replicas = NUM_REPLICAS;
	}
}

void dgl_group_req_init(struct dgl_group_req *greq)
{
	int i;
	greq->cpu = NO_CPU;
	for (i = 0; i < MASK_WORDS; i++) {
		greq->requested[i] = 0;
		greq->waiting[i]   = 0;
	}
}

/**
 * set_req - create request for @replicas of @resource.
 */
void set_req(struct dgl_group_req *greq, int resource, int replicas)
{
	int word, bit;
	struct dgl_req *req;

	BUG_ON(replicas > NUM_REPLICAS);

	mask_idx(resource, &word, &bit);
	TRACE("0x%p will request resource %d, word %d, bit %d\n",
	      greq, resource, word, bit);

	__set_bit(bit, &greq->requested[word]);

	req = &greq->requests[resource];
	INIT_LIST_HEAD(&req->list);
	req->replicas = replicas;
}

/*
 * Attempt to fulfill request @req for @resource.
 * Return 1 if successful. If the matching group request has acquired all of
 * its needed resources, this will then set that req as dgl->acquired[cpu].
 */
static unsigned long try_acquire(struct dgl *dgl, struct dgl_resource *resource,
				 struct dgl_req *req)
{
	int word, bit, rid;
	unsigned long waiting;
	struct dgl_group_req *greq;

	if (resource->free_replicas < req->replicas) {
		TRACE("0x%p cannot acquire %d replicas, only %d free\n",
		      greq, req->replicas, resource->free_replicas);
		return 0;
	}

	resource->free_replicas -= req->replicas;

	rid  = resource_id(dgl, resource);
	greq = req_group(req, rid);
	mask_idx(rid, &word, &bit);

	TRACE("0x%p acquired rid %d, word %d, bit %d\n",
	      greq, rid, word, bit);

	clear_bit(bit, &greq->waiting[word]);

        waiting = 0;
	for (word = 0; word < MASK_WORDS; word++) {
		waiting |= greq->waiting[word];
		if (waiting)
			break;
	}

	if (!waiting) {
		TRACE("0x%p acquired all resources\n", greq);
		BUG_ON(dgl->acquired[greq->cpu]);
		dgl->acquired[greq->cpu] = greq;
		litmus_reschedule(greq->cpu);
	}

	return 1;
}

/**
 * add_group_req - initiate group request.
 */
void add_group_req(struct dgl *dgl, struct dgl_group_req *greq, int cpu)
{
	int b, w, i, succ, all_succ = 1;
	struct dgl_req *req;
	struct dgl_resource *resource;

	greq->cpu = cpu;

	TRACE("0x%p group request added for CPU %d\n", greq, cpu);
	BUG_ON(dgl->acquired[cpu] == greq);

	for_each_resource(greq->requested, w, b, i) {
		__set_bit(b, &greq->waiting[w]);
	}

	for_each_resource(greq->requested, w, b, i) {
		req = &greq->requests[i];
		resource = &dgl->resources[i];

		succ = try_acquire(dgl, resource, req);
		all_succ &= succ;

		if (!succ) {
			TRACE("0x%p waiting on resource %d\n", greq, i);
			list_add_tail(&req->list, &resource->waiting);
		}
	}
}

/**
 * remove_group_req - abandon group request.
 *
 * This will also progress the waiting queues of resources acquired by @greq.
 */
void remove_group_req(struct dgl *dgl, struct dgl_group_req *greq)
{
	int b, w, i;
	struct dgl_req *req, *next;
	struct dgl_resource *resource;

	TRACE("0x%p removing group request for CPU %d\n", greq, greq->cpu);

	if (dgl->acquired[greq->cpu] == greq) {
		TRACE("0x%p no longer acquired on CPU %d\n", greq, greq->cpu);
		dgl->acquired[greq->cpu] = NULL;
	}

	for_each_resource(greq->requested, w, b, i) {
		req = &greq->requests[i];
		resource = &dgl->resources[i];

		if (!list_empty(&req->list)) {
			/* Waiting on resource */
			clear_bit(b, &greq->waiting[w]);
			list_del_init(&req->list);
			TRACE("0x%p quit waiting for resource %d\n", greq, i);
		} else {
			/* Have resource */
			resource->free_replicas += req->replicas;
			TRACE("0x%p releasing resource %d\n", greq, i);

			if (!list_empty(&resource->waiting)) {
				/* Give it to the next guy */
				next = list_first_entry(&resource->waiting,
							struct dgl_req,
							list);
				if (try_acquire(dgl, resource, next))
					list_del_init(&next->list);
			}
		}
	}
}