aboutsummaryrefslogtreecommitdiffstats
path: root/native/include/sharedres_types.h
blob: fbe8f72feef03306ae9acd12a0fd38a9d512ae60 (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
#ifndef SHAREDRES_TYPES_H
#define SHAREDRES_TYPES_H

#ifndef SWIG
#include <limits.h>
#include <assert.h>

#include <vector>
#include <algorithm>

#include "stl-helper.h"
#endif

typedef enum {
	WRITE = 0,
	READ  = 1,
} request_type_t;

class TaskInfo;

class RequestBound
{
private:
	unsigned int resource_id;
	unsigned int num_requests;
	unsigned int request_length;
	const TaskInfo*    task;
	request_type_t request_type;

public:
	RequestBound(unsigned int res_id,
		     unsigned int num,
		     unsigned int length,
		     const TaskInfo* tsk,
		     request_type_t type = WRITE)
		: resource_id(res_id),
		  num_requests(num),
		  request_length(length),
		  task(tsk),
		  request_type(type)
	{}

	unsigned int get_max_num_requests(unsigned long interval) const;

	unsigned int get_resource_id() const { return resource_id; }
	unsigned int get_num_requests() const { return num_requests; }
	unsigned int get_request_length() const { return request_length; }

	request_type_t get_request_type() const { return request_type; }

	bool is_read() const { return get_request_type() == READ; }
	bool is_write() const { return get_request_type() == WRITE; }

	const TaskInfo* get_task() const { return task; }
};

typedef std::vector<RequestBound> Requests;

class TaskInfo
{
private:
	unsigned int  priority;
	unsigned long period;
	unsigned long response;
	unsigned int  cluster;
	unsigned int  id;
	Requests requests;

public:
	TaskInfo(unsigned long _period,
		 unsigned long _response,
		 unsigned int _cluster,
		 unsigned int _priority,
		 int _id)
		: priority(_priority),
		  period(_period),
		  response(_response),
		  cluster(_cluster),
		  id(_id)
	{}

	void add_request(unsigned int res_id,
			 unsigned int num,
			 unsigned int length,
			 request_type_t type = WRITE)
	{
		requests.push_back(RequestBound(res_id, num, length, this, type));
	}

	const Requests& get_requests() const
	{
		return requests;
	}

	unsigned int get_id() const { return id; }
	unsigned int  get_priority() const { return priority; }
	unsigned long get_period() const { return period; }
	unsigned long get_response() const { return response; }
	unsigned int  get_cluster() const { return cluster; }

	unsigned int get_num_arrivals() const
	{
		return get_total_num_requests() + 1; // one for the job release
	}

	unsigned int get_total_num_requests() const
	{
		unsigned int count = 0;
		foreach(requests, it)
			count += it->get_num_requests();
		return count;
	}

	unsigned int get_max_request_length() const
	{
		unsigned int len = 0;
		foreach(requests, it)
			len = std::max(len, it->get_request_length());
		return len;
	}

	unsigned int get_num_requests(unsigned int res_id) const
	{
		foreach(requests, it)
			if (it->get_resource_id() == res_id)
				return it->get_num_requests();
		return 0;
	}
};

typedef std::vector<TaskInfo> TaskInfos;

class ResourceSharingInfo
{
private:
	TaskInfos tasks;

public:
	ResourceSharingInfo(unsigned int num_tasks)
	{
		// Make sure all tasks will fit without re-allocation.
		tasks.reserve(num_tasks);
	}

	const TaskInfos& get_tasks() const
	{
		return tasks;
	}

	void add_task(unsigned long period,
		      unsigned long response,
		      unsigned int cluster  = 0,
		      unsigned int priority = UINT_MAX)
	{
		// Avoid re-allocation!
		assert(tasks.size() < tasks.capacity());
		int id = tasks.size();
		tasks.push_back(TaskInfo(period, response, cluster, priority, id));
	}

	void add_request(unsigned int resource_id,
			 unsigned int max_num,
			 unsigned int max_length)
	{
		assert(!tasks.empty());

		TaskInfo& last_added = tasks.back();
		last_added.add_request(resource_id, max_num, max_length);
	}

	void add_request_rw(unsigned int resource_id,
			    unsigned int max_num,
			    unsigned int max_length,
			    int type)
	{
		assert(!tasks.empty());
		assert(type == WRITE || type == READ);

		TaskInfo& last_added = tasks.back();
		last_added.add_request(resource_id, max_num, max_length, (request_type_t) type);
	}

};


#define NO_CPU (-1)

class ResourceLocality
{
private:
	std::vector<int> mapping;

public:
	void assign_resource(unsigned int res_id, unsigned int processor)
	{
		while (mapping.size() <= res_id)
			mapping.push_back(NO_CPU);

		mapping[res_id] = processor;
	}

	int operator[](unsigned int res_id) const
	{
		if (mapping.size() <= res_id)
			return NO_CPU;
		else
			return mapping[res_id];
	}

};

class ReplicaInfo
{
private:
	std::vector<unsigned int> num_replicas;

public:
	void set_replicas(unsigned int res_id, unsigned int replicas)
	{
		assert(replicas >= 1);

		while (num_replicas.size() <= res_id)
			num_replicas.push_back(1); // default: not replicated

		num_replicas[res_id] = replicas;
	}

	unsigned int operator[](unsigned int res_id) const
	{
		if (num_replicas.size() <= res_id)
			return 1; // default: not replicated
		else
			return num_replicas[res_id];
	}
};

struct Interference
{
	unsigned int  count;
	unsigned long total_length;

	Interference() : count(0), total_length(0) {}

	Interference& operator+=(const Interference& other)
	{
		count        += other.count;
		total_length += other.total_length;
		return *this;
	}

	Interference operator+(const Interference& other) const
	{
		Interference result;
		result.count = this->count + other.count;
		result.total_length = this->total_length + other.total_length;
		return result;
	}

	bool operator<(const Interference& other) const
	{
		return total_length < other.total_length ||
			(total_length == other.total_length
			 && count < other.count);
	}
};

class BlockingBounds
{
private:
	std::vector<Interference> blocking;
	std::vector<Interference> request_span;
	std::vector<Interference> arrival;
	std::vector<Interference> remote;
	std::vector<Interference> local;

public:
	BlockingBounds(unsigned int num_tasks)
		: blocking(num_tasks),
		  request_span(num_tasks)
	{}

	BlockingBounds(const ResourceSharingInfo& info)
		:  blocking(info.get_tasks().size()),
		request_span(info.get_tasks().size()),
		arrival(info.get_tasks().size()),
		remote(info.get_tasks().size()),
		local(info.get_tasks().size())
	{}

	const Interference& operator[](unsigned int idx) const
	{
		assert( idx < size() );
		return blocking[idx];
	}

	Interference& operator[](unsigned int idx)
	{
		assert( idx < size() );
		return blocking[idx];
	}

	void raise_request_span(unsigned idx, const Interference& val)
	{
		assert( idx < size() );
		request_span[idx] = std::max(request_span[idx], val);
	}

	const Interference& get_max_request_span(unsigned idx) const
	{
		assert( idx < size() );
		return request_span[idx];
	}

	size_t size() const
	{
		return blocking.size();
	}

	unsigned long get_blocking_term(unsigned int tsk_index) const
	{
		assert( tsk_index < blocking.size() );
		return blocking[tsk_index].total_length;
	}

	unsigned long get_blocking_count(unsigned int tsk_index) const
	{
		assert( tsk_index < blocking.size() );
		return blocking[tsk_index].count;
	}

	unsigned long get_span_term(unsigned int tsk_index) const
	{
		assert( tsk_index < blocking.size() );
		return request_span[tsk_index].total_length;
	}

	unsigned long get_span_count(unsigned int tsk_index) const
	{
		assert( tsk_index < blocking.size() );
		return request_span[tsk_index].count;
	}


	unsigned long get_remote_blocking(unsigned int tsk_index) const
	{
		assert( tsk_index < remote.size() );
		return remote[tsk_index].total_length;
	}

	unsigned long get_remote_count(unsigned int tsk_index) const
	{
		assert( tsk_index < remote.size() );
		return remote[tsk_index].count;
	}

	void set_remote_blocking(unsigned int tsk_index,
				 const Interference& inf)
	{
		assert( tsk_index < remote.size() );
		remote[tsk_index] = inf;
	}

	unsigned long get_local_blocking(unsigned int tsk_index) const
	{
		assert( tsk_index < local.size() );
		return local[tsk_index].total_length;
	}

	unsigned long get_local_count(unsigned int tsk_index) const
	{
		assert( tsk_index < local.size() );
		return local[tsk_index].count;
	}

	void set_local_blocking(unsigned int tsk_index,
				const Interference& inf)
	{
		assert( tsk_index < local.size() );
		local[tsk_index] = inf;
	}

	unsigned long get_arrival_blocking(unsigned int tsk_index) const
	{
		assert( tsk_index < arrival.size() );
		return arrival[tsk_index].total_length;
	}

	void set_arrival_blocking(unsigned int tsk_index,
				  const Interference& inf)
	{
		assert( tsk_index < arrival.size() );
		arrival[tsk_index] = inf;
	}
};

#endif