UNPKG

sams-algorithm-fifo

Version:

A memory scheduler that uses a fifo algorithm.

139 lines (116 loc) 4.89 kB
var cocktail = require('cocktail'); //Add Static Logger annotation. var StaticLogger = require('../algorithm-common/src/annotations/StaticLogger'); cocktail.use(StaticLogger); //Other clases used: var Queue = require('../algorithm-common/src/common/Queue'); var Memory = require('../algorithm-common/src/common/Memory'); var Requirement = require('../algorithm-common/src/common/Requirement'); var Page = require('../algorithm-common/src/common/Page'); cocktail.mix({ /* * Replacement Algorithm: First in, First Out. * Asignation strategy: Dynamic. * Replacement strategy: Global. */ '@exports': module, '@as': 'class', //Set the logger and signature of this module. '@StaticLogger' : [console, "Fifo Algorithm"], '@static' : { _dispatch: function(requirement, victimsQueue, memoryFrames, output) { //Check if the requirement is already in memory. if (!memoryFrames.contains(requirement)) { /* * This is a Page Fault. * When a Page Fault is encountered: * -set the loaded page as in PG. * -count a Page Fault. * -Add the requirement to the victimsQueue(FIFO). */ //When a requirement is "loaded" in a page, pageFault is set. this.log("Page Fault issued for requirement " + requirement); var requiredPage = requirement.asPage(); output.pageFaults++; victimsQueue.enqueue(requirement); if (memoryFrames.isFull()) { /* * Memory is full, I need to unload a page. * I choose the victim as the first in my victimsQueue(FIFO). * I can assure that the first in the victims queue has a frame in memory. */ memoryFrames.atPut(memoryFrames.getFrameOf(victimsQueue.dequeue()), requiredPage); } else { //There is free space, so just put it there. memoryFrames.atPut(memoryFrames.getFreeFrame(), requiredPage); } } else { //If the page was already loaded then you should do nothing with the Queue.(FIFO). this.log(requirement + " already in memory. Nothing to do."); //Find the requirement and set the flag referenced to true. memoryFrames.at(memoryFrames.getFrameOf(requirement)).setReferenced(true); } }, _digest: function(requirements, victimsQueue, memoryFrames, output) { //Initialize the pageFaults. output.pageFaults = 0; //Each requirement will be dispatched to aply the algorithm and then a copy of the memory status will be saved. requirements.forEach(function(element, index) { //Clear all pageFault Flags from the memory. memoryFrames.forEach(function(element) { element.clearPageFault(); element.clearReferenced(); }); //Aply the algorithm. this.log("Processing instant " + index + ".\n"); this._dispatch(element, victimsQueue, memoryFrames, output); //Save the memory. this.log("Saving memory state for instant " + index + ".\n") output.instants.push(memoryFrames.clone()); }, this); this.log("All requirements were processed."); }, _parseRequirements: function(rawRequirements) { var aux = []; //Use the raw javascript object to create a Cocktail Requirement instance. //Using the configurable trait. rawRequirements.forEach(function(element) { aux.push(new Requirement(element)); }); return aux; }, //Public interface. ProcessRequirements: function(rawRequirements, framesAmmount, callback) { //Parse the rawRequirements array into a Requirements array. var requirements = this._parseRequirements(rawRequirements); //Create the queue that will store the requirements in the order of victims. var victimsQueue = new Queue(); //The memory in a given instance. var memoryFrames = new Memory(framesAmmount); //Create the array that will store the snapshots(instances of Memory). var framesArray = []; //Start the count of pageFaults. var output = { instants: framesArray, pageFaults: 0 }; //Call the algorithm processing method. this._digest(requirements, victimsQueue, memoryFrames, output); //Return the Cocktail Pages instances as Javascript simple objecs. var parsedOutput = []; output.instants.forEach(function(element) { var aux = []; element.forEach(function(element) { aux.push(element.asSimpleObject()); }); parsedOutput.push(aux); }); this.log("Delegating control back. Starting Callback.\n\n") //Since this algorithm is resolved synchronously the callback just comes after it. if(callback) { callback(parsedOutput, output.pageFaults); } } } });