diff --git a/tp4-obj3-meens/Makefile b/tp4-obj3-meens/Makefile new file mode 100644 index 0000000..9992953 --- /dev/null +++ b/tp4-obj3-meens/Makefile @@ -0,0 +1,28 @@ +LIB_PATH=$(shell heptc -where) +OBJ=extern.c scheduler_c/scheduler.c scheduler_data_c/scheduler_data_types.c + +aaa: + heptc -target c scheduler_data.ept + heptc extern.epi + heptc -target c scheduler.ept + gcc -I $(LIB_PATH)/c -I . main.c extern.c -I scheduler_data_c scheduler_c/scheduler.c scheduler_data_c/scheduler_data.c + +al: main + +a: extern.epi + heptc $< + +$(OBJ): scheduler_data.ept scheduler.ept a + heptc -target c scheduler_data.ept + heptc -target c scheduler.ept + +main: $(OBJ) + gcc -I $(LIB_PATH)/c -I . main.c $(OBJ) + +clean: + $(RM) *.log + $(RM) *.epci + $(RM) *.mls + $(RM) *.o *.obj *.obc + $(RM) -r *_c + $(RM) *.out diff --git a/tp4-obj3-meens/extern.c b/tp4-obj3-meens/extern.c new file mode 100644 index 0000000..b02120b --- /dev/null +++ b/tp4-obj3-meens/extern.c @@ -0,0 +1,35 @@ +#include "extern.h" +#include +#include +#include "scheduler_data_c/scheduler_data_types.h" + +void Extern__deadline_miss_log_step(int data, int task_id, Extern__deadline_miss_log_out *_out) +{ + printf("Deadline missed: task: %i, data: %i\n", task_id, data); +} + +int Extern__random_step(int max, Extern__random_out *_out) +{ + _out->v = (rand() % max) + 1; + /* _out->v = max; */ + return _out->v; +} + +void Extern__print_scheduler_state_step(Scheduler_data__scheduler_state s, Extern__print_scheduler_state_out *_out) +{ + printf("Scheduler state - current_date: %i\n", s.current_date); + for (size_t i = 0; i < Scheduler_data__ntasks; i++) { + int left = s.tasks[i].left; + if (s.tasks[i].status == 2) + left = 0; + printf("[%li] Task - status: %d - current_deadline: %i - left: %i, was_running: %i\n", + i, s.tasks[i].status, s.tasks[i].current_deadline, left, s.tasks[i].was_running); + } + printf("\n"); + +} + +void Extern__print_mes_couilles_step(int tid, Scheduler_data__task_status ts, Scheduler_data__task_attributes ta, Scheduler_data__select_acc acc, Extern__print_mes_couilles_out *_out) +{ + printf("tid: %i, ts.was_running: %i, ts.current_deadline: %i, acc.sdeadline: %i\n", tid, ts.was_running, ts.current_deadline, acc.sdeadline); +} diff --git a/tp4-obj3-meens/extern.epi b/tp4-obj3-meens/extern.epi new file mode 100644 index 0000000..1a9dfb9 --- /dev/null +++ b/tp4-obj3-meens/extern.epi @@ -0,0 +1,6 @@ +open Scheduler_data + +fun deadline_miss_log(date:int;task_id:int) returns () +fun random(max:int) returns (v:int) +fun print_scheduler_state(s:scheduler_state) returns () +fun print_mes_couilles(tid:int;ts:task_status; ta:task_attributes;acc:select_acc) returns () diff --git a/tp4-obj3-meens/extern.h b/tp4-obj3-meens/extern.h new file mode 100644 index 0000000..68cf742 --- /dev/null +++ b/tp4-obj3-meens/extern.h @@ -0,0 +1,30 @@ +#ifndef EXTERN_H +#define EXTERN_H + +#include "scheduler_data_c/scheduler_data_types.h" + +typedef struct { + int data; + int task_id; +} Extern__deadline_miss_log_out; + +typedef struct { + int max; + int v; +} Extern__random_out; + +typedef struct { +} Extern__print_scheduler_state_out; + +typedef struct { +} Extern__print_mes_couilles_out; + + + +void Extern__deadline_miss_log_step(int data, int task_id, Extern__deadline_miss_log_out *_out); +int Extern__random_step(int max, Extern__random_out *_out); +void Extern__print_scheduler_state_step(Scheduler_data__scheduler_state s, Extern__print_scheduler_state_out *_out); +void Extern__print_mes_couilles_step(int tid, Scheduler_data__task_status ts, Scheduler_data__task_attributes ta, Scheduler_data__select_acc acc, Extern__print_mes_couilles_out *_out); + +#endif // EXTERN_H + diff --git a/tp4-obj3-meens/extern_types.h b/tp4-obj3-meens/extern_types.h new file mode 100644 index 0000000..863ab7d --- /dev/null +++ b/tp4-obj3-meens/extern_types.h @@ -0,0 +1,6 @@ +#ifndef EXTERN_TYPES_H +#define EXTERN_TYPES_H + + + +#endif /* EXTERN_TYPES_H */ diff --git a/tp4-obj3-meens/main.c b/tp4-obj3-meens/main.c new file mode 100644 index 0000000..cd308f7 --- /dev/null +++ b/tp4-obj3-meens/main.c @@ -0,0 +1,21 @@ +#include +/* #include "scheduler_data_c/scheduler_data.h" */ +/* #include "scheduler_data_c/scheduler_data_types.h" */ +#include "scheduler_c/scheduler.h" +#include "scheduler_c/scheduler_types.h" +#include "stdio.h" + +int main() { + Scheduler__main_out _out; + Scheduler__main_mem self; + char buffer[100]; + + + Scheduler__main_reset(&self); + + while (1) { + fgets(buffer, 10, stdin); + Scheduler__main_step(&_out, &self); + } + return 0; +} diff --git a/tp4-obj3-meens/scheduler.ept b/tp4-obj3-meens/scheduler.ept new file mode 100644 index 0000000..72d039f --- /dev/null +++ b/tp4-obj3-meens/scheduler.ept @@ -0,0 +1,109 @@ +open Scheduler_data +open Extern + +fun select_aux(ts:task_status; ta:task_attributes;tid:int ;acc:select_acc) + returns (acc_o:select_acc) +let + () = print_mes_couilles(tid, ts, ta, acc); + acc_o = + if (ts.was_running = 1 and ts.status = Ready) and (ts.current_deadline = acc.sdeadline) then + { tid = tid; sdeadline = ts.current_deadline; swas_running = ts.was_running } + else if (ts.status = Ready) and (ts.current_deadline < acc.sdeadline) then + { tid = tid; sdeadline = ts.current_deadline; swas_running = 0 } + else acc +tel + +fun select_one_task(ts:task_status^ntasks) returns(selected:int) + var tmp : select_acc ; +let + tmp = foldi<> select_aux (ts,tasks,{ tid = ntasks; sdeadline = int_max; swas_running=0 }) ; + selected = tmp.tid ; +tel + +fun start_inst(current_date:int;tsi:task_status;ta:task_attributes) + returns (tso:task_status) + var c : bool ; +let + c = (current_date-ta.first_start)%ta.period = 0 ; + tso = merge c + (true -> { + was_running = 0; + status = Ready; + current_deadline = (current_date when c) + (ta.deadline when c); + left = (ta.capacity when c) + }) + (false -> tsi whenot c) +tel + +fun update_selected(ts:task_status;selected:int;tid:int) returns (tso:task_status) +let + tso = + if tid = selected then + { ts with .status = Running } + else ts +tel + +fun rate_monotonic(ts:task_status^ntasks) returns (tso:task_status^ntasks) + var selected : int ; +let + selected = select_one_task(ts) ; + tso = mapi<> update_selected (ts, selected^ntasks) ; +tel + +fun check_deadline(current_date:int; tsi:task_status; tid:int) + returns (tso:task_status) + var c: bool ; +let + c = (tsi.status = Ready) + and (tsi.current_deadline = current_date) ; + () = deadline_miss_log(current_date when c, tid when c) ; + tso = if c then { tsi with .status = Waiting} else tsi ; +tel + +fun simulate(tsi:task_status) + returns (o:task_status) +let + o = + if tsi.status = Running then + if tsi.left <=1 then + (* Normal termination, move to Waiting state *) + { status = Waiting; was_running = 0; current_deadline = tsi.current_deadline; left = tsi.left } + else + (* No termination, yet *) + { + was_running = 1; + status = Ready; + current_deadline = tsi.current_deadline ; + left = tsi.left - 1 + } + else tsi +tel + +fun scheduler(si:scheduler_state) returns (so:scheduler_state) + var + new_date : int ; + tmp1,tmp2,tmp3,fin: task_status^ntasks ; +let + new_date = si.current_date + 1 ; (* advance time by 1 *) + tmp1 = map <> simulate (si.tasks) ; + tmp2 = mapi<> check_deadline (new_date^ntasks,tmp1); + tmp3 = map <> start_inst (new_date^ntasks,tmp2,tasks); + fin = rate_monotonic(tmp3) ; (* scheduling policy *) + so = { current_date = new_date; tasks = fin } +tel + + +const init_sstate : scheduler_state = { + current_date = -1 ; + tasks = { + was_running=0;status=Waiting;current_deadline=0;left=0 + }^2 +} + +node main() returns () + var sstate, new_sstate : scheduler_state ; +let + new_sstate = scheduler(sstate) ; + sstate = init_sstate fby new_sstate ; + () = print_scheduler_state(new_sstate) ; +tel diff --git a/tp4-obj3-meens/scheduler_data.ept b/tp4-obj3-meens/scheduler_data.ept new file mode 100644 index 0000000..e776959 --- /dev/null +++ b/tp4-obj3-meens/scheduler_data.ept @@ -0,0 +1,29 @@ +const ntasks : int = 2 +const int_max : int = 10000000 + +type task_attributes = { + period : int ; + capacity : int ; + deadline : int ; + first_start : int + } + +type task_state = Running | Ready | Waiting +type task_status = { + was_running : int; + status : task_state ; + current_deadline : int ; + left : int +} + +type scheduler_state = { + current_date : int ; + tasks : task_status^ntasks +} + +type select_acc = { tid : int; swas_running: int; sdeadline : int } + +const tasks : task_attributes^ntasks = [ + { period=5; capacity=2; deadline=5; first_start=0 }, + { period=7; capacity=4; deadline=7; first_start=3 } +]