/// <summary> /// Destroys all existing task graphs and instantiates the new required amount /// </summary> /// <param name="amount">Number of graphs to be instantiated</param> private void ResetAllTaskGraphs(int amount) { if (taskGraphs.Count != 0) { foreach (TaskGraph TG in taskGraphs) { Destroy(TG.gameObject); } taskGraphs.Clear(); } for (int i = 0; i < amount; i++) { TaskGraph TG = Instantiate(taskPrefab, taskGraphParent).GetComponent <TaskGraph>(); TG.graphTitle.text = "Task " + (i + 1).ToString(); taskGraphs.Add(TG); } }
/// <summary> /// This IEnumerator is the simulation loop for all scheduling algorithms. /// </summary> /// <param name="algorithmID">Algorithm to simulate</param> /// <returns></returns> private IEnumerator SimulationLoop(int algorithmID) { inputGroup.interactable = false; ResetStats(); ResetAllTaskGraphs((int)tasksSlider.value); GenerateTasks(allTaskSettings); //Get each tick size by dividing total width by the simulation time int duration = int.Parse(simTime.text); float tickSizes = taskGraphParent.rect.width / duration; //Check if last algorithm selected (Find best algorithm) for (int tick = 1; tick < duration + 1; tick++) { //Debug.Log("Tick: " + tick); List <TaskControlBlock> sortedList = new List <TaskControlBlock>(tasks); switch (algorithmID) { case 0: //FCFS //Sort tasks based on most recently scheduled sortedList.Sort((t1, t2) => t2.lastScheduled.CompareTo(t1.lastScheduled)); break; case 1: //RR //Sort tasks based on last scheduled time sortedList.Sort((t1, t2) => t1.lastScheduled.CompareTo(t2.lastScheduled)); break; case 2: //RM //Sort tasks by inverse period sortedList.Sort((t1, t2) => t1.period.CompareTo(t2.period)); break; case 3: //EDF //Sort tasks by their next closest deadline sortedList.Sort((t1, t2) => { int edf1 = GetNextDeadline(tick, t1.period, t1.release); int edf2 = GetNextDeadline(tick, t2.period, t2.release); if (edf1 < edf2) { return(-1); } if (edf1 > edf2) { return(1); } //If same, use most recently scheduled task. return(t2.lastScheduled.CompareTo(t1.lastScheduled)); }); break; case 4: //LLF //Sort tasks by their next deadline, minus remaining time sortedList.Sort((t1, t2) => { int llf1 = GetNextDeadline(tick, t1.period, t1.release) - t1.currentRemaining - tick; int llf2 = GetNextDeadline(tick, t2.period, t2.release) - t2.currentRemaining - tick; if (llf1 < llf2) { return(-1); } if (llf1 > llf2) { return(1); } //If equal, use most recently scheduled task return(t2.lastScheduled.CompareTo(t1.lastScheduled)); }); break; } //Filter priority list by tasks with remaining time and have been released then iterate over to select the task to be scheduled. foreach (TaskControlBlock TCB in sortedList.Where(TCB => tasks[TCB.id].currentRemaining > 0).Where(TCB => tick > tasks[TCB.id].release)) { tasks[TCB.id].scheduled = true; usedTicks++; break; } for (int graphID = 0; graphID < taskGraphs.Count; graphID++) { TaskGraph TG = taskGraphs[graphID]; TaskControlBlock TCB = tasks[graphID]; TimeTick TT = Instantiate(timeTickPrefab, TG.ticksParent).GetComponent <TimeTick>(); RectTransform TT_Rect = TT.GetComponent <RectTransform>(); TT_Rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tickSizes); TG.ticks.Add(TT); TT.text.text = tick.ToString(); //Current tick is a deadline if period is a factor of tick and the task has already been released bool deadline = (tick - TCB.release) % TCB.period == 0 && tick > TCB.release; bool missed = false; bool release = tick == TCB.release; //Check here for zero release during tick 1 if (tick == 1 && TCB.release == 0) { TG.zeroRelease.enabled = true; } if (TCB.scheduled) { TCB.currentRemaining--; TCB.lastScheduled = tick; //Debug.Log("Scheduling task " + TCB.id); //Debug.Log("Remaining time now " + TCB.currentRemaining); } //If currently deadline tick check for missed deadline and refresh remaining if (deadline) { if (TCB.currentRemaining > 0) { //Debug.LogError("MISSED DEADLINE AT TICK " + tick + " FOR TASK " + TCB.id); missed = true; deadlineMissed++; } else { deadlineMet++; } TCB.currentRemaining = TCB.duration; } //Sets visual ques for scheduled and deadline TT.UpdateGraphics(TCB.scheduled, deadline, missed, release); TCB.scheduled = false; } yield return(new WaitForSecondsRealtime(simSpeed.value == 0 ? 0 : 0.25f)); } inputGroup.interactable = true; ShowResults(); }