Finish bugfixing for threaded allocation algorithm.

Threaded allocation is now complete.
This commit is contained in:
Bradlee Speice 2012-11-16 22:13:29 -05:00
parent 25fbca314c
commit 51aad5ee74
4 changed files with 53 additions and 39 deletions

View File

@ -87,7 +87,6 @@ public class jobThread extends Thread {
Object[] deallocateArgs = {this.jobSize, this.beginningLocation}; Object[] deallocateArgs = {this.jobSize, this.beginningLocation};
//We're done, go ahead and notify our algorithm to clean us up //We're done, go ahead and notify our algorithm to clean us up
parentAlgorithmDeallocate.invoke(parentAlgorithm, deallocateArgs); parentAlgorithmDeallocate.invoke(parentAlgorithm, deallocateArgs);
} catch (Exception e) { } catch (Exception e) {
return; return;
} }

View File

@ -9,8 +9,8 @@ import java.util.StringTokenizer;
public class memoryManagement{ public class memoryManagement{
static final int JOBAMOUNT = 30; static final int JOBAMOUNT = 200;
static final int MEMORYSIZE = 10000; static final int MEMORYSIZE = 100;
public static void main(String args[])throws Exception{ public static void main(String args[])throws Exception{
@ -31,9 +31,9 @@ public class memoryManagement{
//******Add your algorithm class here******// //******Add your algorithm class here******//
//baseAlgorithm alg = new dummyAlgorithm(MEMORYSIZE); //baseAlgorithm alg = new dummyAlgorithm(MEMORYSIZE);
//threadedAllocation Bradlee_Speice = new threadedAllocation(MEMORYSIZE); threadedAllocation Bradlee_Speice = new threadedAllocation(MEMORYSIZE);
//FirstFit David01 = new FirstFit(); //FirstFit David01 = new FirstFit();
NextFit David02 = new NextFit(); //NextFit David02 = new NextFit();
//Gets a file name, else creates five random jobs //Gets a file name, else creates five random jobs
do{ do{
@ -50,8 +50,8 @@ public class memoryManagement{
jobLength = JOBAMOUNT; jobLength = JOBAMOUNT;
for(int i = 0; i < jobLength; i++){ for(int i = 0; i < jobLength; i++){
id[i] = i+1; id[i] = i+1;
size[i] = rand.nextInt(1000)+1; size[i] = rand.nextInt(5)+1;
time[i] = rand.nextInt(1000)+1; time[i] = rand.nextInt(1000)+2001;
} }
System.out.println("complete"); System.out.println("complete");
} }
@ -74,9 +74,9 @@ public class memoryManagement{
timeStart = System.currentTimeMillis(); timeStart = System.currentTimeMillis();
//Note that we use `jobLength - 1` to compensate for the id above //Note that we use `jobLength - 1` to compensate for the id above
for(int i = 0; i < jobLength - 1; i++){ for(int i = 0; i < jobLength - 1; i++){
//Bradlee_Speice.allocate(id[i], size[i], time[i]); Bradlee_Speice.allocate(id[i], size[i], time[i]);
//David01.allocate(id[i], size[i], time[i]); //David01.allocate(id[i], size[i], time[i]);
David02.allocate(id[i], size[i], time[i]); //David02.allocate(id[i], size[i], time[i]);
} }
timeEnd = System.currentTimeMillis() - timeStart; timeEnd = System.currentTimeMillis() - timeStart;
System.out.println("complete"); System.out.println("complete");

View File

@ -12,11 +12,12 @@ public class threadedAllocation implements baseAlgorithm{
* our actual operations */ * our actual operations */
memoryBlock = new int[memorySize]; memoryBlock = new int[memorySize];
// Set up the array of job references
this.jobArray = new Job[memoryManagement.JOBAMOUNT + 1];
this.garbageThread = new threadedAllocationGarbage(this.memoryBlock, 20, this.jobArray); this.garbageThread = new threadedAllocationGarbage(this.memoryBlock, 20, this.jobArray);
this.garbageThread.start(); this.garbageThread.start();
// Set up the array of job references
jobArray = new Job[memoryManagement.JOBAMOUNT];
} }
int locateBlock(int blockSize){ int locateBlock(int blockSize){
@ -26,23 +27,25 @@ public class threadedAllocation implements baseAlgorithm{
int memoryLoc = 0; int memoryLoc = 0;
while (memoryLoc < this.memoryBlock.length) while (memoryLoc < this.memoryBlock.length)
{ {
if (memoryBlock[memoryLoc] != 0) if (memoryBlock[memoryLoc] != 0){
memoryLoc++;
//Block isn't free //Block isn't free
continue; continue;
}
//One location is free, find out total size free //One location is free, find out total size free
//This loop breaks either when we've found the size we need, or //This loop breaks either when we've found the size we need, or
//we found the beginning of the next block. //we found the beginning of the next block.
int beginningLoc = memoryLoc; int beginningLoc = memoryLoc;
int free = 0; int free = 0;
while (free < blockSize && memoryBlock[memoryLoc] == 0) while (free < blockSize && memoryLoc < this.memoryBlock.length && memoryBlock[memoryLoc] == 0)
{ {
memoryLoc += 1; memoryLoc += 1;
free += 1; free += 1;
} }
if (free >= blockSize){ if (free >= blockSize){
System.out.println("Found a block of size " + blockSize + " at " + beginningLoc); //System.out.println("Found a block of size " + blockSize + " at " + beginningLoc);
return beginningLoc; return beginningLoc;
} }
} }
@ -53,7 +56,6 @@ public class threadedAllocation implements baseAlgorithm{
public void allocate(int jobID, int jobSize, int jobLength ){ public void allocate(int jobID, int jobSize, int jobLength ){
/* Over-rides allocate() of baseAlgorithm */ /* Over-rides allocate() of baseAlgorithm */
try{ try{
System.err.println("Allocating job with ID " + jobID);
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class}); Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
//Loop until we get a block big enough for our job //Loop until we get a block big enough for our job
@ -62,29 +64,36 @@ public class threadedAllocation implements baseAlgorithm{
while (beginningLocation == -1) while (beginningLocation == -1)
beginningLocation = locateBlock( jobSize ); beginningLocation = locateBlock( jobSize );
//We've got a location, mark it as filled, and start the job. //We've got a location, mark it as filled, and start the job.
synchronized(memoryBlock){
for (int x = 0; x < jobSize; x++) for (int x = 0; x < jobSize; x++)
{ {
memoryBlock[beginningLocation + x] = jobID; memoryBlock[beginningLocation + x] = jobID;
} }
}
Job newJob = new Job(jobLength, jobID, jobSize, beginningLocation, deallocateMethod, this); Job newJob = new Job(jobLength, jobID, jobSize, beginningLocation, deallocateMethod, this);
jobArray[jobID - 1] = newJob; jobArray[jobID] = newJob;
newJob.start(); newJob.start();
} catch (Exception e){ } catch (Exception e){
System.out.println("Could not allocate job with ID " + jobID); e.printStackTrace();
System.exit(-1);
} }
} }
public void deallocate(int jobSize, int beginningLocation){ public void deallocate(int jobSize, int beginningLocation){
/* Over-rides deallocate() of baseAlgorithm */ /* Over-rides deallocate() of baseAlgorithm */
//System.err.println("Deallocation job with ID " + memoryBlock[beginningLocation] + " at time " + System.currentTimeMillis());
//Simple algorithm, basically just mark the memory as cleared. //Simple algorithm, basically just mark the memory as cleared.
synchronized(memoryBlock){
for (int x = 0; x < jobSize; x++) for (int x = 0; x < jobSize; x++)
{ {
memoryBlock[beginningLocation + x] = 0; memoryBlock[beginningLocation + x] = 0;
} }
} }
}
} }

View File

@ -70,6 +70,7 @@ class threadedAllocationGarbage extends Thread
*/ */
int[] largestBlockInfo = largestBlock(); int[] largestBlockInfo = largestBlock();
int maxFreeBeginning = largestBlockInfo[0]; int maxFreeBeginning = largestBlockInfo[0];
int maxFreeSize = largestBlockInfo[1]; int maxFreeSize = largestBlockInfo[1];
@ -97,13 +98,14 @@ class threadedAllocationGarbage extends Thread
//Pause the job, and then relocate it //Pause the job, and then relocate it
//Note that we need to lock out the allocation to prevent a race //Note that we need to lock out the allocation to prevent a race
int memoryLoc = maxFreeBeginning;
synchronized (this.memoryBlock) { synchronized (this.memoryBlock) {
//Pause the job operation try{
System.out.println("Job ID about to pause: " + jobID ); //Pause the job operation - note that we use a try-catch, as the job may disappear on us,
jobArray[jobID - 1].pause(); //but is not a fatal error.
jobArray[jobID].pause();
//Write the job into the free space //Write the job into the free space
int memoryLoc = maxFreeBeginning;
counter = 0; counter = 0;
while (counter < jobSize){ while (counter < jobSize){
memoryBlock[memoryLoc] = jobID; memoryBlock[memoryLoc] = jobID;
@ -111,15 +113,19 @@ class threadedAllocationGarbage extends Thread
} }
//Inform the job of its new beginning location //Inform the job of its new beginning location
jobArray[jobID - 1].setBeginningLocation(maxFreeBeginning); jobArray[jobID].setBeginningLocation(maxFreeBeginning);
//Restart the job //Restart the job
jobArray[jobID - 1].resume(); jobArray[jobID].resume();
} catch (Exception e){
//Job ended before we could clean it up, proceed as if nothing happened.
}
//Write the remaining memory as free //Write the remaining memory as free
counter = 0; counter = 0;
while (counter < maxFreeSize){ while (counter < maxFreeSize){
memoryBlock[memoryLoc] = 0; memoryBlock[memoryLoc] = 0;
counter++;
} }
} }