Subversion Repositories shark

Compare Revisions

Ignore whitespace Rev 1638 → Rev 1639

/advdemos/trunk/elastic/elastic.c
33,93 → 33,177
 
#include "kernel/kern.h"
#include "modules/elastic.h"
#include <drivers/shark_keyb26.h>
 
extern unsigned int usleep(unsigned int usec); // include file?
#define MAX_TASKS 7
 
extern unsigned int usleep(unsigned int usec); // include file for this??
 
 
PID pidvec[10];
 
 
TASK elastic_test(void *arg) {
 
int nbr = (int)arg;
int y = (int)arg;
int x = 0;
 
char str[2];
str[0] = '0' + y;
str[1] = '\0';
 
while(1) {
cprintf("%d.", nbr);
puts_xy(x,y,LIGHTGRAY," ");
x = (x + 1) % 80;
puts_xy(x,y,LIGHTGRAY,str);
task_testcancel();
task_endcycle();
}
}
 
 
void myinput(char *str, char *buf, int len) {
BYTE c;
int n = 0;
 
cprintf(str);
do {
c = keyb_getch(NON_BLOCK);
if (c == ENTER) break;
if (c == 8) { // backspace
if (n > 0) {
cprintf("%c", c);
n--;
}
} else if (c) {
cprintf("%c", c);
buf[n++] = c;
}
} while (n < len);
 
buf[n] = '\0';
cprintf("\n");
}
 
 
int main(int argc, char **argv)
{
ELASTIC_TASK_MODEL e1, e2, e3, e4;
PID el_pid1, el_pid2, el_pid3, el_pid4;
struct timespec t;
ELASTIC_TASK_MODEL e;
PID el_pid;
int quit = 0;
char buf[80];
int y;
int Tmin, Tmax, C, E, T, nbr, i;
double U;
 
cprintf("Elastic Task Demo (U=1.0 in initfile.c)\n");
clear();
for (i=0; i<MAX_TASKS; i++) pidvec[i] = NIL;
 
cprintf("Creating elastic task with Tmin = 100000, Tmax = 500000, C = 30000, E = 1\n");
elastic_task_default_model(e1);
elastic_task_def_period(e1, 100000, 500000);
elastic_task_def_wcet(e1, 30000);
elastic_task_def_param(e1, 1, PERIOD_SCALING);
elastic_task_def_arg(e1, (void *)1);
el_pid1 = task_create("Elastic1",elastic_test,&e1,NULL);
if (el_pid1 == NIL) {
cprintf("ERROR: Cannot create Elastic1\n");
sys_end();
}
do {
 
task_activate(el_pid1);
for (y = 7; y <=15; y++) {
puts_xy(0,y,LIGHTGRAY," ");
}
place(0,7);
 
cprintf("\nCreating elastic task with Tmin = 200000, Tmax = 500000, C = 160000, E = 2\n");
elastic_task_default_model(e2);
elastic_task_def_period(e2, 200000, 500000);
elastic_task_def_wcet(e2, 160000);
elastic_task_def_param(e2, 2, PERIOD_SCALING);
elastic_task_def_arg(e2, (void *)2);
el_pid2 = task_create("Elastic2",elastic_test,&e2,NULL);
if (el_pid2 == NIL) {
cprintf("ERROR: Cannot create Elastic2\n");
sys_end();
}
U = 0.0;
cprintf("Nbr Tmin Tmax C E T \n");
for (i=0; i<MAX_TASKS; i++) {
if (pidvec[i] != NIL) {
Tmin = ELASTIC_get_Tmin(pidvec[i])/1000;
Tmax = ELASTIC_get_Tmax(pidvec[i])/1000;
C = ELASTIC_get_C(pidvec[i])/1000;
E = ELASTIC_get_E(pidvec[i]);
T = ELASTIC_get_period(pidvec[i])/1000;
U += (double)C/(double)T;
cprintf(" %1d %4d %4d %4d %4d %4d\n", i, Tmin, Tmax, C, E, T);
}
}
cprintf("Total utilization: %5.3f\n", U);
 
task_activate(el_pid2);
puts_xy(0,16,LIGHTGRAY," ");
place(0,16);
 
usleep(500000);
myinput("(q)uit, (c)reate, (f)orce period, (d)elete? ", buf, 10);
for (y = 17; y <=24; y++) {
puts_xy(0,y,LIGHTGRAY," ");
}
 
ELASTIC_set_period(el_pid2, 210000);
switch (buf[0]) {
case 'q':
quit = 1;
break;
case 'c':
cprintf("Create task\n");
for (i=0; i<MAX_TASKS; i++) {
if (pidvec[i] == NIL) break;
}
if (i == MAX_TASKS) {
cprintf("No more task slots available!\n");
break;
}
myinput("Tmin (ms): ", buf, 10);
Tmin = 1000*atoi(buf);
myinput("Tmax (ms): ", buf, 10);
Tmax = 1000*atoi(buf);
myinput("C (ms): ", buf, 10);
C = 1000*atoi(buf);
myinput("E: ", buf, 10);
E = atoi(buf);
elastic_task_default_model(e);
elastic_task_def_period(e, Tmin, Tmax);
elastic_task_def_wcet(e, C);
elastic_task_def_param(e, E, PERIOD_SCALING);
elastic_task_def_arg(e, (void *)i);
el_pid = task_create("Elastic",elastic_test,&e,NULL);
if (el_pid == NIL) {
cprintf("Cannot create task!\n");
} else {
pidvec[i] = el_pid;
task_activate(el_pid);
}
break;
case 'f':
myinput("Force period\nTask nbr: ", buf, 10);
nbr = atoi(buf);
if (nbr < 0 || nbr >= MAX_TASKS) {
cprintf("Invalid task number!\n");
break;
}
el_pid = pidvec[nbr];
if (el_pid == NIL) {
cprintf("Task doesn't exist!\n");
break;
}
myinput("T (ms): ", buf, 10);
T = 1000*atoi(buf);
ELASTIC_set_period(el_pid, T);
break;
case 'd':
myinput("Delete task\nTask nbr: ", buf, 10);
nbr = atoi(buf);
if (nbr < 0 || nbr >= MAX_TASKS) {
cprintf("Invalid task number!\n");
break;
}
el_pid = pidvec[nbr];
if (el_pid == NIL) {
cprintf("Task doesn't exist!\n");
break;
}
task_kill(el_pid);
pidvec[nbr] = NIL;
break;
default:
cprintf("Unknown command\n");
break;
}
 
} while (!quit);
 
/*
cprintf("\nCreating elastic task with Tmin = 300000, Tmax = 500000, C = 90000, E = 1\n");
elastic_task_default_model(e3);
elastic_task_def_period(e3, 300000, 500000);
elastic_task_def_wcet(e3, 90000);
elastic_task_def_param(e3, 1, PERIOD_SCALING);
elastic_task_def_arg(e3, (void *)3);
el_pid3 = task_create("Elastic3",elastic_test,&e3,NULL);
if (el_pid3 == NIL) {
cprintf("ERROR: Cannot create Elastic3\n");
sys_end();
}
task_activate(el_pid3);
sys_end();
 
cprintf("\nCreating elastic task with Tmin = 50000, Tmax = 500000, C = 24000, E = 0\n");
elastic_task_default_model(e4);
elastic_task_def_period(e4, 50000, 500000);
elastic_task_def_wcet(e4, 24000);
elastic_task_def_param(e4, 0, PERIOD_SCALING);
elastic_task_def_arg(e4, (void *)4);
el_pid4 = task_create("Elastic4",elastic_test,&e4,NULL);
if (el_pid4 == NIL) {
cprintf("ERROR: Cannot create Elastic4\n");
sys_end();
}
task_activate(el_pid4);
*/
/* Exit at time t = 1.0 */
t.tv_sec = 1;
t.tv_nsec = 0;
kern_event_post(&t, (void(*)(void*))sys_end, NULL);
while(1);
return 0;
 
}