📄 workload.java
字号:
* e.g. ";" or "#" * @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise * @pre comment != null * @post $none */ public boolean setComment(String comment) { boolean success = false; if (comment != null && comment.length() > 0) { COMMENT = comment; success = true; } return success; } /** * Tells this class what to look in the trace file. * This method should be called before the start of the simulation. * <p> * By default, this class follows the standard workload format as specified * in <a href="http://www.cs.huji.ac.il/labs/parallel/workload/"> * http://www.cs.huji.ac.il/labs/parallel/workload/</a> <br> * However, you can use other format by calling this method. * <p> * The parameters must be a positive integer number starting from 1. * A special case is where <tt>jobNum == -1</tt>, meaning the job or * gridlet ID starts at 1. * * @param maxField max. number of field/column in one row * @param jobNum field/column number for locating the job ID * @param submitTime field/column number for locating the job submit time * @param runTime field/column number for locating the job run time * @param numProc field/column number for locating the number of PEs * required to run a job * @return <tt>true</tt> if successful, <tt>false</tt> otherwise * @pre maxField > 0 * @pre submitTime > 0 * @pre runTime > 0 * @pre numProc > 0 * @post $none */ public boolean setField(int maxField, int jobNum, int submitTime, int runTime, int numProc) { // need to substract by 1 since array starts at 0. Need to convert, // position in a field into the index of the array if (jobNum > 0) { JOB_NUM = jobNum - 1; } else if (jobNum == 0) { System.out.println(super.get_name() + ".setField(): Invalid job number field."); return false; } else { JOB_NUM = -1; } // get the max. number of field if (maxField > 0) { MAX_FIELD = maxField; } else { System.out.println(super.get_name() + ".setField(): Invalid max. number of field."); return false; } // get the submit time field if (submitTime > 0) { SUBMIT_TIME = submitTime - 1; } else { System.out.println(super.get_name() + ".setField(): Invalid submit time field."); return false; } // get the run time field if (runTime > 0) { REQ_RUN_TIME = runTime - 1; } else { System.out.println(super.get_name() + ".setField(): Invalid run time field."); return false; } // get the number of processors field if (numProc > 0) { REQ_NUM_PROC = numProc - 1; } else { System.out.println(super.get_name() + ".setField(): Invalid number of processors field."); return false; } return true; } /** * Gets a list of completed Gridlets * @return a list of Gridlets * @pre $none * @post $none */ public ArrayList getGridletList() { return list_; } /** * Prints the Gridlet objects * @param history <tt>true</tt> means printing each Gridlet's history, * <tt>false</tt> otherwise * @pre $none * @post $none */ public void printGridletList(boolean history) { String name = super.get_name(); int size = list_.size(); Gridlet gridlet; String indent = " "; System.out.println(); System.out.println("========== OUTPUT for " + name + " =========="); System.out.println("Gridlet_ID" + indent + "STATUS" + indent + "Resource_ID" + indent + "Cost"); int i = 0; for (i = 0; i < size; i++) { gridlet = (Gridlet) list_.get(i); System.out.print(indent + gridlet.getGridletID() + indent + indent); // get the status of a Gridlet System.out.print( gridlet.getGridletStatusString() ); System.out.println( indent + indent + gridlet.getResourceID() + indent + indent + gridlet.getProcessingCost() ); } System.out.println(); if (history == true) { // a loop to print each Gridlet's history System.out.println(); for (i = 0; i < size; i++) { gridlet = (Gridlet) list_.get(i); System.out.println( gridlet.getGridletHistory() ); System.out.print("Gridlet #" + gridlet.getGridletID() ); System.out.println(", length = " + gridlet.getGridletLength() + ", finished so far = " + gridlet.getGridletFinishedSoFar() ); System.out.println("========================================="); System.out.println(); } } } /** * Reads from a given file when the simulation starts running. * Then submits Gridlets to a resource and collects them before exiting. * To collect the completed Gridlets, use {@link #getGridletList()} * @pre $none * @post $none */ public void body() { System.out.println(); System.out.println(super.get_name() + ".body() :%%%% Start ..."); // create a temp array fieldArray_ = new String[MAX_FIELD]; // get the resource id if (resID_ < 0) { System.out.println(super.get_name() + ".body(): Error - invalid resource name: " + resName_); return; } boolean success = false; // read the gz file if (fileName_.endsWith(".gz") == true) { success = readGZIPFile(fileName_); } // read the zip file else if (fileName_.endsWith(".zip") == true) { success = readZipFile(fileName_); } // read from uncompressed file as well else { success = readFile(fileName_); } // if all the gridlets have been submitted if (success == true) { collectGridlet(); } else { System.out.println(super.get_name() + ".body(): Error - unable to parse from a file."); } // shut down all the entities, including GridStatistics entity since // we used it to record certain events. shutdownGridStatisticsEntity(); shutdownUserEntity(); terminateIOEntities(); System.out.println(super.get_name() + ".body() : %%%% Exit ..."); } //////////////////////// PRIVATE METHODS /////////////////////// /** * Collects Gridlets sent and stores them into a list. * @pre $none * @post $none */ private void collectGridlet() { System.out.println(super.get_name() + ": Collecting Gridlets ..."); list_ = new ArrayList(gridletID_ + 1); Object data = null; Gridlet gl = null; int counter = 1; // starts at 1, since gridletID_ starts at 1 too Sim_event ev = new Sim_event(); while ( Sim_system.running() ) { super.sim_get_next(ev); // get the next available event data = ev.get_data(); // get the event's data // handle ping request if (ev.get_tag() == GridSimTags.INFOPKT_SUBMIT) { processPingRequest(ev); continue; } // get the Gridlet data if (data != null && data instanceof Gridlet) { gl = (Gridlet) data; list_.add(gl); counter++; } // if all the Gridlets have been collected if (counter == gridletID_) { break; } } } /** * Processes a ping request. * @param ev a Sim_event object * @pre ev != null * @post $none */ private void processPingRequest(Sim_event ev) { InfoPacket pkt = (InfoPacket) ev.get_data(); pkt.setTag(GridSimTags.INFOPKT_RETURN); pkt.setDestID( pkt.getSrcID() ); // sends back to the sender super.send(super.output, GridSimTags.SCHEDULE_NOW, GridSimTags.INFOPKT_RETURN, new IO_data(pkt, pkt.getSize(), pkt.getSrcID()) ); } /** * Breaks a line of string into many fields. * @param line a line of string * @param lineNum a line number * @pre line != null * @pre lineNum > 0 * @post $none */ private void parseValue(String line, int lineNum) { // skip a comment line if (line.startsWith(COMMENT) == true) { return; } String[] sp = line.split("\\s+"); // split the fields based on a space int i; // a counter int len = 0; // length of a string int index = 0; // the index of an array // check for each field in the array for (i = 0; i < sp.length; i++) { len = sp[i].length(); // get the length of a string // if it is empty then ignore if (len == 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -