/* Actual scheduler goes here. Lots of room for optimizations here ;-) because, as of now, algoriths are relatively primitive. */ public QueueItem analyze(List<QueueItem> queue) { int count = queue.Count; double max = 0; int selectedIndex = 0; bool itemChosen = false; scores.Clear(); scores.AddRange(new double[count]); //run through queue, save to for (int a = 0; a < count; a++) { if (queue[a].ticks >= 0) { scores[a] = ((255 - queue[a].priority) * priorityFactor) + (queue[a].ticks * lengthFactor) + (queue[a].ticksRun * ticksRunFactor) + (((UInt64)DateTime.UtcNow.Ticks - queue[a].lastRun) * lastRunFactor); if (scores[a] > max) { max = scores[a]; selectedIndex = a; itemChosen = true; } } } if (itemChosen) { queue[selectedIndex].lastRun = (UInt64)DateTime.UtcNow.Ticks; return queue[selectedIndex]; } else { Console.WriteLine("Done with jobs."); QueueItem errItem = new QueueItem(); errItem.id = -1; return errItem; } }
public int addItem(Int32 idnum, UInt16 priorty, UInt32 ticks) { if (queue.Count >= maxSize) { return maxSizeReached; } QueueItem item = new QueueItem(); item.id = idnum; item.priority = priorty; item.ticks = (Int32)ticks; item.ticksRun = 0; item.lastRun = 0; //ptr; queue.Add(item); return 0; }
public int updateItemByID(Int32 id, QueueItem newCopy) { for (int a = 0; a < queue.Count; a++) { if (queue[a].id == id) { queue[a] = newCopy; return 0; } } return 1; }
//actual scheduler gets called here. In future, you may want to add ways for main to pass specific qualities to favor public Int32 schedule() { if (!firstRun) { Console.WriteLine("hit"); UInt32 tstamp = (UInt32)((UInt64)DateTime.UtcNow.Ticks - runer.lastRun); runer.ticks -= (Int32)tstamp; runer.ticksRun += tstamp; if (runer.ticks <= 0) { //destroy job Console.WriteLine("Job #{0} complete.", runer.id); } if (updateItemByID(runer.id, runer) != 0) { Console.WriteLine("Error! Failed to run job id #{0}. \nScheduler Failure.", runer.id); } } firstRun = false; //Run job here. Specifics - TBD QueueItem runnee = analyzer.analyze(queue); Console.WriteLine("Job #{0} run", runnee.id); if (runnee.id == -1) { return -1; } runer = runnee; return runnee.id; }
public QueueItem getItemByIndex(int index) { if (index > queue.Count - 1) { return queue[index]; } else { QueueItem errorItem = new QueueItem(); errorItem.id = -1; errorItem.priority = 0; errorItem.ptr = (UIntPtr)0; return errorItem; //if error, return QueueItem with ID set to 0xffffffff } }
public QueueItem getItemByID(UInt32 id) { for (int a = 0; a < queue.Count; a++) { if (queue[a].id == id) { return queue[a]; } } QueueItem errorItem = new QueueItem(); errorItem.id = -1; errorItem.priority = 0; errorItem.ptr = (UIntPtr)0; return errorItem; //if error, return QueueItem with ID set to 0xffffffff }