summaryrefslogtreecommitdiffstats
path: root/Master/Real-Time Systems/Praktikum1/Aufgabe3/src/ResponseTimeAnalysis.cpp
blob: 807b39aba143370dd0268ee1d4e3ffcfb36ee492 (plain)
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
/*
 * ResponseTimeAnalysis.cpp
 *
 *  Created on: 07.11.2010
 *      Author: sven
 */

#include "ResponseTimeAnalysis.h"
#ifndef GLOBAL_H_
	#include "Global.h"
#endif

#include <iostream>
#include <cmath>
#include <map>

ResponseTimeAnalysis::ResponseTimeAnalysis() {
	// TODO Auto-generated constructor stub

}

ResponseTimeAnalysis::~ResponseTimeAnalysis() {
	// TODO Auto-generated destructor stub
}
bool ResponseTimeAnalysis::analyse(rms_queue& task_queue)
{
	bool res = true;
	unsigned int task_count = 0;
	vector<Task*> task_set(task_queue.size());
	while(!task_queue.empty()) {
		Task* t = task_queue.top();
		task_set[task_count++] = t;
		task_queue.pop();
	}
	unsigned int c = 0;
	for(c=0;c<task_set.size();c++) {
		Task* t = task_set[c];
		vector<Task*> higherPriorizedTasks(c);
		for(unsigned int j=0;j<c;j++) {
			higherPriorizedTasks[j]=task_set[j];
		}
		cout << "Task " << t->getName() << " has " << higherPriorizedTasks.size() << " higher priorized tasks: ";
		for(unsigned int j=0;j<higherPriorizedTasks.size();j++) {
			cout << higherPriorizedTasks[j]->getName();
		}
		cout << endl;
		unsigned int ri = t->getOrigC();
		if (c == 0) {
			cout << "Response Time of Task " << t->getName() << " is " << ri << endl;
			continue;
		}
		do {
			unsigned int rii = t->getOrigC();
			for(unsigned int j=0;j<higherPriorizedTasks.size();j++) {
				Task* h = higherPriorizedTasks[j];
				double a = ceil(static_cast<double>(ri)/static_cast<double>(h->getOrigP()));
				double r = a*h->getOrigC();
//				cout << "(Ri)/Tj*Cj = (" << ri << "/" << h->getOrigP() << ")*"<< h->getOrigC() << " = " << r << endl;
				rii += r;
			}
//			cout << "Ri = " << rii << endl;
			if(rii == ri) {
				break;
			}
			ri = rii;
		} while (1);
		cout << "Response Time of Task " << t->getName() << " is " << ri << endl;
		if (ri > static_cast<unsigned int>(t->getOrigD()) ) {
			cout << "Response time of Task "<< t->getName() << " exceeds D:" << ri << ">" << t->getOrigD() << endl;
			res = false;
			break;
		}
	}
	return res;
}
void ResponseTimeAnalysis::calcOptimalStaticPriorities(task_set& tasks)
{
	unsigned int N = tasks.size();
	vector<Task*> prios(N);
	for(unsigned int p=0;p<N;p++) {
		prios[p]=&tasks[p];
	}
	cout << "N: " << N <<endl;
	bool OK = false;
	for(unsigned int K=0;K<N;K++)
	{
		unsigned int actPrio = K;
		cout << "Searching Task to run at prio " << actPrio << endl;
		for(unsigned int Next=K;Next<N;Next++) {
			swap(prios,K,Next);
			Task* candidate = prios[K];
			cout << "candidate: "<< candidate->getName() << " with prio " << actPrio << endl;

			// find tasks with higher priority
			vector<Task*> higherPrioTasks;
			unsigned int higherPrioTasksCounter = 0;
			for(unsigned int taskPrio=actPrio+1;taskPrio<N;taskPrio++) {
				Task *t = prios[taskPrio];
				higherPrioTasks.push_back(t);
				cout << higherPrioTasksCounter << " Added " << t->getName() << " to tasks with higher prio" <<endl;
				higherPrioTasksCounter++;
			}

			// RTA
			int ri = candidate->getOrigC();
			if(higherPrioTasksCounter > 0) {
				do {
					int rii = candidate->getOrigC();
					for(unsigned int k=0;k<higherPrioTasksCounter;k++)
					{
						Task* t = higherPrioTasks[k];
						double a = ceil(static_cast<double>(ri)/static_cast<double>(t->getOrigP()));
						double r = a*t->getOrigC();
						rii += r;
					}
					if(rii == ri) {
						break;
					}
					ri = rii;
				} while(1);
			}
			// check response time
			OK = (ri <= candidate->getOrigD());
			cout << "Response Time of Task " << candidate->getName() << " is " << ri << " with prio " << actPrio << endl;
			if(OK) {
				cout << "Task "<< candidate->getName() << " runs at prio " << actPrio << endl << endl;
				break;
			} else {
				cout << "Task "<< candidate->getName() << " cannot run at prio " << actPrio << endl;
			}
		} // inner for loop
		if(!OK) {
			cout << "Not schedulable! No task to run at prio " << actPrio << endl;
			break;
		}
	}// outer for loop
	if(OK) {
		cout << "Optimal priorities:" << endl;
		for(unsigned int c=0;c<N;c++) {
			Task *t = prios[c];
			if(t) {
				cout << c << " " << t->getName() << endl;
			}
		}
	}
}
void ResponseTimeAnalysis::swap(vector<Task*>& prios,int a,int b)
{
	Task *taskA = prios[a];
	Task *taskB = prios[b];
	cout << "Swapping " << taskA->getName() << " ("<<a<<") and " << taskB->getName() << " (" <<b<< ")"<< endl;
	Task* tmpTask = taskA;
	prios[a] = taskB;
	prios[b] = tmpTask;
}