aboutsummaryrefslogtreecommitdiffstats
path: root/native/src/blocking/linprog/lp_mpcp.cpp
blob: 6037c3d97b0e0fec015675452dc6314cec3e6754 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include <iostream>
#include <set>
#include <algorithm>
#include <cmath>

#include "lp_common.h"
#include "blocking.h"
#include "stl-hashmap.h"

#include "cpu_time.h"

#include "mpcp.h"

#define NO_BOUND (-1)

typedef hashmap<unsigned int, unsigned int> PerResourceCounts;
typedef hashmap<unsigned int, hashmap<unsigned int, unsigned int> > PerTaskPerRequestDirectBlockingBound;
typedef hashmap<unsigned int, unsigned int> PerTaskIndirectBlockingBound;

class GcsResponseTimes
{
private:
	// per each task, and per each global critical section (gcs),
	// the maximum wait time bound
	hashmap< unsigned long, hashmap< unsigned int, long> > remote_delay;

	hashmap< unsigned long, hashmap< unsigned int, unsigned long> > gcs_response;

	const ResourceSharingInfo &info;
	const MPCPCeilings &prio_ceilings;

	long bound_remote_delay(const TaskInfo &tsk,
			     	unsigned int res_id);

	void bound_gcs_response_times(void);


public:
	GcsResponseTimes(const ResourceSharingInfo &i,
			 const MPCPCeilings &pc)
			 : info(i), prio_ceilings(pc)
	{
		bound_gcs_response_times();
	}


	long get_max_remote_delay(const TaskInfo &ti, unsigned int res_id)
	{
		if (remote_delay.find(ti.get_id()) == remote_delay.end())
			remote_delay[ti.get_id()] = hashmap<unsigned int, long>();

		hashmap<unsigned int, long> &tmap = remote_delay[ti.get_id()];

		if (tmap.find(res_id) == tmap.end())
			tmap[res_id] = bound_remote_delay(ti, res_id);

		return tmap[res_id];
	}

	long get_gcs_response(const TaskInfo &ti, unsigned int res_id)
	{
		// look up the task
		if (gcs_response.find(ti.get_id()) == gcs_response.end())
			return NO_BOUND;

		hashmap<unsigned int, unsigned long> &tmap = gcs_response[ti.get_id()];

		// look up the resource
		if (tmap.find(res_id) == tmap.end())
			return NO_BOUND;

		return tmap[res_id];
	}
};


void GcsResponseTimes::bound_gcs_response_times()
{
	Clusters clusters;
	split_by_cluster(info, clusters);

	ClusterResponseTimes responses;
	determine_gcs_response_times(clusters, prio_ceilings, responses);

	for (unsigned int c = 0; c < clusters.size(); c++)
	{
		const Cluster &cluster = clusters[c];
		const TaskResponseTimes &tasks_response_times = responses[c];

		for (unsigned int i = 0; i < cluster.size(); i++)
		{
			const TaskInfo *ti      = cluster[i];
			const ResponseTimes &ri = tasks_response_times[i];

			gcs_response[ti->get_id()] = hashmap<unsigned int, unsigned long>();
			hashmap<unsigned int, unsigned long> &response = gcs_response[ti->get_id()];

			for (unsigned int r = 0; r < ti->get_requests().size(); r++)
			{
				unsigned int q = ti->get_requests()[r].get_resource_id();
				response[q] = ri[r];
			}
		}
	}
}

// This function computes an upper bound on remote blocking using response-time analysis.
// This corresponds to Equation (3) in LNR:09.
long GcsResponseTimes::bound_remote_delay(const TaskInfo &ti, unsigned int res_id)
{
	unsigned long delay_by_lower = 0, delay_by_equal = 0, delay_by_higher = 0;

	// find maximum lower-priority request span that can block directly
	foreach_lowereq_priority_task_except(info.get_tasks(), ti, tx)
	{
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			if (q == res_id)
			{
				unsigned long resp = get_gcs_response(*tx, q);

				if (tx->get_priority() > ti.get_priority())
					delay_by_lower = std::max(delay_by_lower,
								  resp);
				else
					delay_by_equal += resp;
			}
		}
	}

	// response-time analysis to find final maximum wait time

	unsigned long next_estimate = delay_by_lower + delay_by_equal;
	unsigned long estimate = 0;

	while (next_estimate <= ti.get_response() && next_estimate != estimate)
	{
		delay_by_higher = 0;
		estimate = next_estimate;

		// accumulate direct higher-priority blocking
		foreach_higher_priority_task(info.get_tasks(), ti, tx)
		{
			foreach(tx->get_requests(), request)
			{
				unsigned int q = request->get_resource_id();

				if (res_id == q)
				{
					unsigned int nreqs = request->get_max_num_requests(estimate);
					delay_by_higher += nreqs * get_gcs_response(*tx, q);
				}
			}
		}

		next_estimate = delay_by_lower + delay_by_equal + delay_by_higher;
	}

	if (estimate <= ti.get_response())
		return estimate;
	else
		return NO_BOUND;
}

static unsigned int count_gcs_preemption_opportunities(
	const ResourceSharingInfo& info,
	const RequestBound &req,
	PerTaskPerRequestDirectBlockingBound &db_bounds,
	const MPCPCeilings &prio_ceilings,
	const TaskInfo& ti)
{
	unsigned int count = 0;
	const TaskInfo &tr = *req.get_task();
	unsigned int req_prio = prio_ceilings[req.get_task()->get_cluster()][req.get_resource_id()];

	// Count everything that can be preempted by 'req' and that
	// is also potentially blocking ti.

	foreach_local_task(info.get_tasks(), tr, tx)
	{
		if (tx->get_id() != tr.get_id() &&
		    tx->get_id() != ti.get_id())
		{
			foreach(tx->get_requests(), request)
			{
				unsigned int q = request->get_resource_id();
				if (q != req.get_resource_id() &&
				    db_bounds[tx->get_id()][q] > 0)
				{
					// request can directly block Ti
					unsigned int xprio = prio_ceilings[tx->get_cluster()][q];

					if (xprio >= req_prio)
					{
						// indirect delay is possible
						// each time that Ti is directly blocked by 'request'.
						// This kind of blocking is possible
						count += db_bounds[tx->get_id()][q];
					}
				}
			}
		}
	}

	return count;
}

// Constraint 18
static void add_per_request_indirect_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	PerTaskPerRequestDirectBlockingBound &db_bounds,
	const MPCPCeilings &prio_ceilings,
	const TaskInfo& ti,
	LinearProgram& lp)
{
	PerTaskIndirectBlockingBound bounds;

	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();
			LinearExpression *exp = new LinearExpression();
			foreach_request_instance(*request, ti, v)
			{
				unsigned int var_id;
				var_id = vars.lookup(t, q, v, BLOCKING_INDIRECT);
				exp->add_var(var_id);
			}
			unsigned int bound = count_gcs_preemption_opportunities(
				info, *request, db_bounds, prio_ceilings, ti);
			lp.add_inequality(exp, bound);
		}
	}
}

// Constraint 17
static void add_per_task_indirect_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	PerTaskPerRequestDirectBlockingBound &db_bounds,
	const MPCPCeilings &prio_ceilings,
	const TaskInfo& ti,
	LinearProgram& lp)
{
	PerTaskIndirectBlockingBound bounds;

	// initialize
	foreach_remote_task(info.get_tasks(), ti, tx)
	{
		bounds[tx->get_id()] = 0;
	}

	foreach_remote_task(info.get_tasks(), ti, tx)
	{
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			if (db_bounds[tx->get_id()][q] > 0)
			{
				// note for each local task an opportunity to cause indirect delay

				unsigned int prio = prio_ceilings[tx->get_cluster()][request->get_resource_id()];
				foreach_local_task_except(info.get_tasks(), *tx, tl)
				{
					// see if it has anything to preempt us with
					foreach(tl->get_requests(), lreq)
					{
						if (prio_ceilings[tl->get_cluster()][lreq->get_resource_id()] <= prio)
						{
							// yes, has one, count opportunity per direct blocking
							bounds[tl->get_id()] += db_bounds[tx->get_id()][q];
							break;
						}
					}
				}
			}
		}
	}

	foreach_remote_task(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		LinearExpression *exp = new LinearExpression();

		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_INDIRECT);
				exp->add_var(var_id);
			}
		}
		lp.add_inequality(exp, bounds[t]);
	}
}

static PerResourceCounts get_per_resource_counts(const TaskInfo &ti)
{
	// Count the number of times that each resource is accessed by ti.
	PerResourceCounts per_resource_counts;
	foreach(ti.get_requests(), req)
		per_resource_counts[req->get_resource_id()] += req->get_num_requests();

	return per_resource_counts;
}

// Constraints 15, 16, and 19
static void add_direct_blocking_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	GcsResponseTimes &rta,
	const TaskInfo& ti,
	PerResourceCounts &per_resource_counts,
	LinearProgram& lp,
	PerTaskPerRequestDirectBlockingBound &db_bounds)
{
	// Each request can be directly delayed at most once by a lower-priority task
	// accessing the same resource.
	// Ti is never directly delayed due to resources it does not request,
	// regardless of the priority of the lock holder.

	// build one constraint for each resource.
	hashmap<unsigned int, LinearExpression *> constraints;

	foreach_task_except(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		// Is Tx a higher-priority task?
		bool hiprio = tx->get_priority() < ti.get_priority();

		db_bounds[t] = hashmap<unsigned int, unsigned int>();

		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			bool accessed = per_resource_counts.find(q) !=
 	  				per_resource_counts.end();

 	  		db_bounds[t][q] = 0;

			if (!hiprio || !accessed)
			{
				LinearExpression *exp;

				if (constraints.find(q) == constraints.end())
					constraints[q] = new LinearExpression();

				exp = constraints[q];

				foreach_request_instance(*request, ti, v)
				{
					unsigned int var_id;
					var_id = vars.lookup(t, q, v, BLOCKING_DIRECT);
					exp->add_var(var_id);
				}

				if (accessed)
					// loprio => direct blocking at most once per request
		 	  		db_bounds[t][q] = std::min(
		 	  			request->get_max_num_requests(ti.get_response()),
					        per_resource_counts[q]);
			}
			else
			{
				// higher-priority request, conflicting
				// how many blocking instances per request?
				long interval = rta.get_max_remote_delay(ti, q);
				if (interval != NO_BOUND)
				{
					// can add constraints
					unsigned int request_count;
					// max per request?
					request_count = request->get_max_num_requests(interval);
					// how many requests?
					request_count *= per_resource_counts[q];
					// add constraint for this task

					db_bounds[t][q] = std::min(
						request->get_max_num_requests(ti.get_response()),
					        request_count);

					LinearExpression *exp = new LinearExpression();
					foreach_request_instance(*request, ti, v)
					{
						unsigned int var_id;
						var_id = vars.lookup(t, q, v, BLOCKING_DIRECT);
						exp->add_var(var_id);
					}
					lp.add_inequality(exp, request_count);
				}
			}
		}
	}

	// add each per-resource constraint
	foreach(constraints, it)
	{
		unsigned int bound = 0;
		if (per_resource_counts.find(it->first) !=
 	  	    per_resource_counts.end())
 	  		bound = per_resource_counts[it->first];
		lp.add_inequality(it->second, bound);
	}
}

// Constraint 20
static void add_remote_blocking_constraint(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	GcsResponseTimes &rta,
	const TaskInfo& ti,
	LinearProgram& lp)
{
	unsigned long remote_blocking_bound = 0;

	// sum up maximum remote blocking on a per-request basis
	foreach(ti.get_requests(), req)
	{
		unsigned int num = req->get_num_requests();
		unsigned int q   = req->get_resource_id();
		unsigned long rem_blocking = rta.get_max_remote_delay(ti, q);
		remote_blocking_bound += rem_blocking * num;
	}

	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();
			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);
				exp->add_term(length, var_id);

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

	lp.add_inequality(exp, remote_blocking_bound);
}

static void add_mpcp_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const TaskInfo& ti,
	const MPCPCeilings& prio_ceilings,
	GcsResponseTimes &gcs_response,
	LinearProgram& lp)
{
	PerResourceCounts counts = get_per_resource_counts(ti);
	PerTaskPerRequestDirectBlockingBound db_bounds;

	// Constraint  1
	add_mutex_constraints(vars, info, ti, lp);
	// Constraint  9
	add_local_higher_priority_constraints_shm(vars, info, ti, lp);
	// Constraint 10
	add_topology_constraints_shm(vars, info, ti, lp);
	// Constraint 11
	add_local_lower_priority_constraints_shm(vars, info, ti, lp);
	// Constraints 15, 16, and 19
	add_direct_blocking_constraints(vars, info, gcs_response, ti, counts, lp, db_bounds);
	// Constraint 17
	add_per_task_indirect_constraints(vars, info, db_bounds, prio_ceilings, ti, lp);
	// Constraint 18
	add_per_request_indirect_constraints(vars, info, db_bounds, prio_ceilings, ti, lp);
	// Constraint 20
	add_remote_blocking_constraint(vars, info, gcs_response, ti, lp);
}

static void apply_mpcp_bounds_for_task(
	unsigned int i,
	BlockingBounds& bounds,
	const ResourceSharingInfo& info,
	const MPCPCeilings& prio_ceilings,
	GcsResponseTimes &gcs_response)
{
	LinearProgram lp;
	VarMapper vars;
	const TaskInfo& ti = info.get_tasks()[i];
	LinearExpression *local_obj = new LinearExpression();
	LinearExpression *remote_obj = new LinearExpression();

#if DEBUG_LP_OVERHEADS >= 2
	static DEFINE_CPU_CLOCK(model_gen_cost);
	static DEFINE_CPU_CLOCK(solver_cost);
	static DEFINE_CPU_CLOCK(remote_cost);

	std::cout << "---- " << __FUNCTION__ << " ----" << std::endl;

	model_gen_cost.start();
#endif

	set_blocking_objective_part_shm(vars, info, ti, lp, local_obj, remote_obj);
	vars.seal();

	add_mpcp_constraints(vars, info, ti,
			     prio_ceilings,
			     gcs_response, lp);

#if DEBUG_LP_OVERHEADS >= 2
	model_gen_cost.stop();
	std::cout << model_gen_cost << std::endl;
	solver_cost.start();
#endif

	Solution *sol = linprog_solve(lp, vars.get_num_vars());

#if DEBUG_LP_OVERHEADS >= 2
	solver_cost.stop();
	std::cout << solver_cost << std::endl;
#endif
	assert(sol != NULL);

	Interference total, remote, local;

	total.total_length = lrint(sol->evaluate(*lp.get_objective()));
	local.total_length = lrint(sol->evaluate(*local_obj));

	bounds[i] = total;
	bounds.set_local_blocking(i, local);

	delete local_obj;
	delete sol;

#if DEBUG_LP_OVERHEADS >= 2
	remote_cost.start();
#endif

	// compute remote blocking maximum
	lp.set_objective(remote_obj);
	sol = linprog_solve(lp, vars.get_num_vars());

	assert(sol != NULL);

	remote.total_length = lrint(sol->evaluate(*lp.get_objective()));
	bounds.set_remote_blocking(i, remote);

#if DEBUG_LP_OVERHEADS >= 2
	remote_cost.stop();
	std::cout << remote_cost << std::endl;
#endif
	delete sol;
}


static BlockingBounds* _lp_mpcp_bounds(const ResourceSharingInfo& info)
{
	BlockingBounds* results = new BlockingBounds(info);

	MPCPCeilings prio_ceilings = get_mpcp_ceilings(info);
	GcsResponseTimes gcs_response = GcsResponseTimes(info, prio_ceilings);

	for (unsigned int i = 0; i < info.get_tasks().size(); i++)
		apply_mpcp_bounds_for_task(i, *results, info, prio_ceilings, gcs_response);

	return results;
}

BlockingBounds* lp_mpcp_bounds(const ResourceSharingInfo& info)
{
#if DEBUG_LP_OVERHEADS >= 1
	static DEFINE_CPU_CLOCK(cpu_costs);

	cpu_costs.start();
#endif

	BlockingBounds *results = _lp_mpcp_bounds(info);

#if DEBUG_LP_OVERHEADS >= 1
	cpu_costs.stop();
	std::cout << cpu_costs << std::endl;
#endif

	return results;
}