📄 workload.java
字号:
continue; } // if not, then put into the array else { fieldArray_[index] = sp[i]; index++; } } if (index == MAX_FIELD) { extractField(fieldArray_, lineNum); } } /** * Extracts relevant information from a given array * @param array an array of String * @param line a line number * @pre array != null * @pre line > 0 */ private void extractField(String[] array, int line) { try { Integer obj = null; // get the job number int id = 0; if (JOB_NUM == IRRELEVANT) { id = gridletID_; } else { obj = new Integer( array[JOB_NUM].trim() ); id = obj.intValue(); } // get the submit time Long l = new Long( array[SUBMIT_TIME].trim() ); long submitTime = l.intValue(); // get the run time obj = new Integer( array[REQ_RUN_TIME].trim() ); int runTime = obj.intValue(); // if the required run time field is ignored, then use // the actual run time if (runTime == IRRELEVANT) { obj = new Integer( array[RUN_TIME].trim() ); runTime = obj.intValue(); } // according to the SWF manual, runtime of 0 is possible due // to rounding down. E.g. runtime is 0.4 seconds -> runtime = 0 if (runTime == 0) { runTime = 1; // change to 1 second } // get the number of allocated processors obj = new Integer( array[REQ_NUM_PROC].trim() ); int numProc = obj.intValue(); // if the required num of allocated processors field is ignored // or zero, then use the actual field if (numProc == IRRELEVANT || numProc == 0) { obj = new Integer( array[NUM_PROC].trim() ); numProc = obj.intValue(); } // finally, check if the num of PEs required is valid or not if (numProc <= 0) { System.out.println(super.get_name() + ": Warning - job #" + id + " at line " + line + " requires " + numProc + " CPU. Change to 1 CPU."); numProc = 1; } // submit a Gridlet submitGridlet(id, submitTime, runTime, numProc); } catch (Exception e) { System.out.println(super.get_name() + ": Exception in reading file at line #" + line); e.printStackTrace(); } } /** * Creates a Gridlet with the given information, then submit it to a * resource * @param id a Gridlet ID * @param submitTime Gridlet's submit time * @param runTime Gridlet's run time * @param numProc number of processors * @pre id >= 0 * @pre submitTime >= 0 * @pre runTime >= 0 * @pre numProc > 0 * @post $none */ private void submitGridlet(int id, long submitTime, int runTime, int numProc) { // create the gridlet int len = runTime * rating_; // calculate a job length for each PE Gridlet gl = new Gridlet(id, len, size_, size_); gl.setUserID( super.get_id() ); // set the owner ID gl.setNumPE(numProc); // set the requested num of proc // printing to inform user if (gridletID_ == 1 || gridletID_ % INTERVAL == 0) { System.out.println(super.get_name() + ": Submitting Gridlets to " + resName_ + " ..."); } // check the submit time if (submitTime < 0) { submitTime = 0; } gridletID_++; // increment the counter // submit a gridlet to resource super.send(super.output, submitTime, GridSimTags.GRIDLET_SUBMIT, new IO_data(gl, gl.getGridletFileSize(), resID_) ); } /** * Reads a text file one line at the time * @param fileName a file name * @return <tt>true</tt> if reading a file is successful, <tt>false</tt> * otherwise. * @pre fileName != null * @post $none */ private boolean readFile(String fileName) { boolean success = false; BufferedReader reader = null; try { FileInputStream file = new FileInputStream(fileName); InputStreamReader input = new InputStreamReader(file); reader = new BufferedReader(input); // read one line at the time int line = 1; while ( reader.ready() ) { parseValue(reader.readLine(), line); line++; } reader.close(); // close the file success = true; } catch (FileNotFoundException f) { System.out.println(super.get_name() + ": Error - the file was not found: " + f.getMessage()); } catch (IOException e) { System.out.println(super.get_name() + ": Error - an IOException occurred: " + e.getMessage()); } finally { if (reader != null) { try { reader.close(); // close the file } catch (IOException e) { System.out.println(super.get_name() + ": Error - an IOException occurred: " + e.getMessage()); } } } return success; } /** * Reads a gzip file one line at the time * @param fileName a gzip file name * @return <tt>true</tt> if reading a file is successful, <tt>false</tt> * otherwise. * @pre fileName != null * @post $none */ private boolean readGZIPFile(String fileName) { boolean success = false; BufferedReader reader = null; try { FileInputStream file = new FileInputStream(fileName); GZIPInputStream gz = new GZIPInputStream(file); InputStreamReader input = new InputStreamReader(gz); reader = new BufferedReader(input); // read one line at the time int line = 1; while ( reader.ready() ) { parseValue(reader.readLine(), line); line++; } reader.close(); // close the file success = true; } catch (FileNotFoundException f) { System.out.println(super.get_name() + ": Error - the file was not found: " + f.getMessage()); } catch (IOException e) { System.out.println(super.get_name() + ": Error - an IOException occurred: " + e.getMessage()); } finally { if (reader != null) { try { reader.close(); // close the file } catch (IOException e) { System.out.println(super.get_name() + ": Error - an IOException occurred: " + e.getMessage()); } } } return success; } /** * Reads a Zip file. Iterating through each entry and reading it one line * at the time. * @param fileName a zip file name * @return <tt>true</tt> if reading a file is successful, <tt>false</tt> * otherwise. * @pre fileName != null * @post $none */ private boolean readZipFile(String fileName) { boolean success = false; ZipFile zipFile = null; try { InputStreamReader input = null; BufferedReader reader = null; // ZipFile offers an Enumeration of all the files in the Zip file zipFile = new ZipFile(fileName); for (Enumeration e = zipFile.entries(); e.hasMoreElements();) { success = false; // reset the value again ZipEntry zipEntry = (ZipEntry) e.nextElement(); input = new InputStreamReader(zipFile.getInputStream(zipEntry)); reader = new BufferedReader(input); // read one line at the time int line = 1; while ( reader.ready() ) { parseValue(reader.readLine(), line); line++; } reader.close(); // close the file success = true; } } catch (IOException e) { System.out.println(super.get_name() + ": Error - an IOException occurred: " + e.getMessage()); } finally { if (zipFile != null) { try { zipFile.close(); // close the file } catch (IOException e) { System.out.println(super.get_name() + ": Error - an IOException occurred: " + e.getMessage()); } } } return success; }} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -