aboutsummaryrefslogtreecommitdiffstats
path: root/native/src/blocking/linprog/lp_common.cpp
blob: 1715d3b0d7698976b9e0825aa34c95c1e6418aa2 (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
#include "lp_common.h"

// LP-based analysis of semaphore protocols.
// Based on the paper:
// B. Brandenburg, "Improved Analysis and Evaluation of Real-Time Semaphore
// Protocols for P-FP Scheduling", Proceedings of the 19th IEEE Real-Time and
// Embedded Technology and Applications Symposium (RTAS 2013), April 2013.

void set_blocking_objective(
	VarMapper& vars,
	const ResourceSharingInfo& info, const ResourceLocality& locality,
	const TaskInfo& ti,
	LinearProgram& lp,
	LinearExpression *local_obj,
	LinearExpression *remote_obj)
{
        LinearExpression *obj;

	obj = lp.get_objective();

	foreach_task_except(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			bool local = locality[q] == (int) ti.get_cluster();
			double length;

			// Sanity check topology info in debug mode.
			assert(locality[q] != NO_CPU);

			length = request->get_request_length();

			foreach_request_instance(*request, ti, v)
			{
				unsigned int var_id;

				var_id = vars.lookup(t, q, v, BLOCKING_DIRECT);
				obj->add_term(length, var_id);
				if (local && local_obj)
					local_obj->add_term(length, var_id);
				else if (!local && remote_obj)
					remote_obj->add_term(length, var_id);

				var_id = vars.lookup(t, q, v, BLOCKING_INDIRECT);
				obj->add_term(length, var_id);
				if (local && local_obj)
					local_obj->add_term(length, var_id);
				else if (!local && remote_obj)
					remote_obj->add_term(length, var_id);

				var_id = vars.lookup(t, q, v, BLOCKING_PREEMPT);
				obj->add_term(length, var_id);
				if (local && local_obj)
					local_obj->add_term(length, var_id);
				else if (!local && remote_obj)
					remote_obj->add_term(length, var_id);
			}
		}
	}
#ifndef CONFIG_MERGED_LINPROGS
	// We have enumerated all relevant variables. Do not allow any more to
	// be created.
	vars.seal();
#endif
}

// This version is for partitioned shared-memory protocols where each
// task executes its critical section on its assigned processor.
void set_blocking_objective_part_shm(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const TaskInfo& ti,
	LinearProgram& lp,
	LinearExpression *local_obj,
	LinearExpression *remote_obj)
{
        LinearExpression *obj;

	obj = lp.get_objective();

	foreach_task_except(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		bool local = tx->get_cluster() == ti.get_cluster();

		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			double length = request->get_request_length();;

			foreach_request_instance(*request, ti, v)
			{
				unsigned int var_id;

				var_id = vars.lookup(t, q, v, BLOCKING_DIRECT);
				obj->add_term(length, var_id);
				if (local && local_obj)
					local_obj->add_term(length, var_id);
				else if (!local && remote_obj)
					remote_obj->add_term(length, var_id);

				var_id = vars.lookup(t, q, v, BLOCKING_INDIRECT);
				obj->add_term(length, var_id);
				if (local && local_obj)
					local_obj->add_term(length, var_id);
				else if (!local && remote_obj)
					remote_obj->add_term(length, var_id);

				var_id = vars.lookup(t, q, v, BLOCKING_PREEMPT);
				obj->add_term(length, var_id);
				if (local && local_obj)
					local_obj->add_term(length, var_id);
				else if (!local && remote_obj)
					remote_obj->add_term(length, var_id);
			}
		}
	}
}

// Constraint 1 in [Brandenburg 2013]
void add_mutex_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const TaskInfo& ti,
	LinearProgram& lp)
{

	foreach_task_except(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			foreach_request_instance(*request, ti, v)
			{
				LinearExpression *exp = new LinearExpression();
				unsigned int var_id;

				var_id = vars.lookup(t, q, v, BLOCKING_DIRECT);
				exp->add_var(var_id);

				var_id = vars.lookup(t, q, v, BLOCKING_INDIRECT);
				exp->add_var(var_id);

				var_id = vars.lookup(t, q, v, BLOCKING_PREEMPT);
				exp->add_var(var_id);

				lp.add_inequality(exp, 1);
			}
		}
	}
}

// Constraint 2 in [Brandenburg 2013]
void add_topology_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const ResourceLocality& locality,
	const TaskInfo& ti,
	LinearProgram& lp)
{
	LinearExpression *exp = new LinearExpression();

	foreach_task_except(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		foreach_remote_request(tx->get_requests(), locality, ti, request)
		{
			unsigned int q = request->get_resource_id();
			foreach_request_instance(*request, ti, v)
			{
				unsigned int var_id;
				var_id = vars.lookup(t, q, v, BLOCKING_PREEMPT);
				exp->add_var(var_id);
			}
		}
	}
	lp.add_equality(exp, 0);
}

static unsigned int max_num_arrivals_remote(
	const ResourceLocality& locality,
	const TaskInfo& ti)
{
	// initialize to 1 to account for job release
	unsigned int count = 1;

	// count how often resources on remote cores are accessed
	foreach(ti.get_requests(), req)
		if (locality[req->get_resource_id()] != (int) ti.get_cluster())
			count += req->get_num_requests();

	return count;
}


// Constraint 3 in [Brandenburg 2013]
// One priority-boosting-related preemption per local task
// each time that Ji arrives (is released or resumed)
void add_local_lower_priority_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const ResourceLocality& locality,
	const TaskInfo& ti,
	LinearProgram& lp)
{
	unsigned int num_arrivals = max_num_arrivals_remote(locality, ti);

	foreach_local_lowereq_priority_task_except(info.get_tasks(), ti, tx)
	{
		LinearExpression *exp = new LinearExpression();

		unsigned int t = tx->get_id();
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();

			// is it a resource local to Ti?
			if (locality[q] == (int) ti.get_cluster())
			{
				foreach_request_instance(*request, ti, v)
				{
					unsigned int var_id;
					var_id = vars.lookup(t, q, v,
					                     BLOCKING_PREEMPT);
					exp->add_var(var_id);
				}
			}
		}
		lp.add_equality(exp, num_arrivals);
	}
}


// Constraint 10 in [Brandenburg 2013]
// For shared-memory protocols.
// Remote tasks cannot preempt Ti since they are not scheduled
// on Ti's assigned task; therefore force BLOCKING_PREEMPT to zero.
void add_topology_constraints_shm(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const TaskInfo& ti,
	LinearProgram& lp)
{
	LinearExpression *exp = new LinearExpression();

	foreach_remote_task(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			foreach_request_instance(*request, ti, v)
			{
				unsigned int var_id;
				var_id = vars.lookup(t, q, v, BLOCKING_PREEMPT);
				exp->add_var(var_id);
			}
		}
	}
	lp.add_equality(exp, 0);
}

// Constraint 9 in [Brandenburg 2013]
// local higher-priority tasks never cause blocking under SHM protocols
void add_local_higher_priority_constraints_shm(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const TaskInfo& ti,
	LinearProgram& lp)
{
	LinearExpression *exp = new LinearExpression();

	foreach_local_task(info.get_tasks(), ti, tx)
	{
		if (tx->get_priority() < ti.get_priority())
		{
			unsigned int t = tx->get_id();
			foreach(tx->get_requests(), request)
			{
				unsigned int q = request->get_resource_id();
				foreach_request_instance(*request, ti, v)
				{
					unsigned int var_id;
					var_id = vars.lookup(t, q, v,
					                     BLOCKING_PREEMPT);
					exp->add_var(var_id);

					var_id = vars.lookup(t, q, v,
					                     BLOCKING_INDIRECT);
					exp->add_var(var_id);

					var_id = vars.lookup(t, q, v,
					                     BLOCKING_DIRECT);
					exp->add_var(var_id);
				}
			}
		}
	}
	lp.add_equality(exp, 0);
}

static unsigned int max_num_arrivals_shm(
	const ResourceSharingInfo& info,
	const TaskInfo& ti)
{
	hashmap<unsigned int, unsigned int> request_counts;

	foreach(ti.get_requests(), req)
		request_counts[req->get_resource_id()] = 0;

	// count how often each resource is accessed on remote cores
	foreach_remote_task(info.get_tasks(), ti, tx)
	{
		foreach(tx->get_requests(), req)
		{
			unsigned int q = req->get_resource_id();
			if (request_counts.find(q) != request_counts.end())
				request_counts[q] += req->get_max_num_requests(ti.get_response());
		}
	}

	// initialize to 1 to account for job release
	unsigned int total = 1;

	foreach(ti.get_requests(), req)
		total += std::min(request_counts[req->get_resource_id()],
		                  req->get_num_requests());

	return total;
}

// Constraint 11 in [Brandenburg 2013]
// Local lower-priority tasks block at most once each time
// that Ti suspends (and once after release).
void add_local_lower_priority_constraints_shm(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const TaskInfo& ti,
	LinearProgram& lp)
{
	unsigned int num_arrivals = max_num_arrivals_shm(info, ti);

	foreach_local_lowereq_priority_task_except(info.get_tasks(), ti, tx)
	{
		LinearExpression *exp = new LinearExpression();
		unsigned int t = tx->get_id();
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			foreach_request_instance(*request, ti, v)
			{
				unsigned int var_id;
				var_id = vars.lookup(t, q, v,
						     BLOCKING_PREEMPT);
				exp->add_var(var_id);

				var_id = vars.lookup(t, q, v,
						     BLOCKING_INDIRECT);
				exp->add_var(var_id);

				var_id = vars.lookup(t, q, v,
						     BLOCKING_DIRECT);
				exp->add_var(var_id);
			}
		}
		lp.add_equality(exp, num_arrivals);
	}
}