[5] | 1 | package simulator.workload; |
---|
| 2 | |
---|
| 3 | import org.qcg.broker.schemas.jobdesc.QcgJob; |
---|
| 4 | import gssim.schedframe.scheduling.utils.JobDescription; |
---|
| 5 | import gssim.schedframe.scheduling.utils.TaskDescription; |
---|
| 6 | |
---|
| 7 | import java.io.IOException; |
---|
| 8 | import java.text.DateFormat; |
---|
| 9 | import java.text.ParseException; |
---|
| 10 | import java.text.SimpleDateFormat; |
---|
| 11 | import java.util.ArrayList; |
---|
| 12 | import java.util.Date; |
---|
| 13 | import java.util.List; |
---|
| 14 | import java.util.Locale; |
---|
| 15 | import java.util.Map; |
---|
| 16 | import java.util.TreeMap; |
---|
| 17 | |
---|
| 18 | import javax.xml.parsers.ParserConfigurationException; |
---|
| 19 | import javax.xml.xpath.XPathExpressionException; |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | import org.apache.commons.logging.Log; |
---|
| 23 | import org.apache.commons.logging.LogFactory; |
---|
| 24 | import org.exolab.castor.xml.MarshalException; |
---|
| 25 | import org.exolab.castor.xml.ValidationException; |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | import simulator.utils.XsltTransformations; |
---|
| 29 | import simulator.workload.exceptons.NoSuchCommentException; |
---|
| 30 | import simulator.workload.reader.archive.AbstractWAParser; |
---|
| 31 | import simulator.workload.reader.archive.WAFields; |
---|
| 32 | import simulator.workload.reader.archive.WAReader; |
---|
| 33 | import simulator.workload.reader.archive.WAParser; |
---|
| 34 | import simulator.workload.reader.archive.gwf.GWFFields; |
---|
| 35 | import simulator.workload.reader.archive.swf.SWFFields; |
---|
| 36 | import simulator.workload.reader.xmlJob.XMLJobReader; |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * |
---|
| 40 | * @author Marcin Krystek |
---|
| 41 | * |
---|
| 42 | */ |
---|
| 43 | public class WorkloadLoader { |
---|
| 44 | |
---|
| 45 | private static Log log = LogFactory.getLog(WorkloadLoader.class); |
---|
| 46 | |
---|
| 47 | protected XMLJobReader<QcgJob> xmlJobReader; |
---|
| 48 | protected WAReader<QcgJob> waReader; |
---|
| 49 | protected WAParser localWAParser; |
---|
| 50 | protected int generatedJobsCnt; |
---|
| 51 | protected int generatedTasksCnt; |
---|
| 52 | |
---|
| 53 | protected Date simulationStartTime; |
---|
| 54 | |
---|
| 55 | protected XsltTransformations xsltTransformation = null; |
---|
| 56 | |
---|
| 57 | protected Map<String, JobDescription> jobGridletsMap; |
---|
| 58 | |
---|
| 59 | public WorkloadLoader(XMLJobReader<QcgJob> xmlReader, WAReader<QcgJob> swfReader){ |
---|
| 60 | if(swfReader == null){ |
---|
| 61 | throw new RuntimeException("Swf reader is required to build proper gridlerts."); |
---|
| 62 | } |
---|
| 63 | this.xmlJobReader = xmlReader; |
---|
| 64 | this.waReader = swfReader; |
---|
| 65 | this.generatedJobsCnt = 0; |
---|
| 66 | this.generatedTasksCnt = 0; |
---|
| 67 | this.jobGridletsMap = new TreeMap<String, JobDescription>(); |
---|
| 68 | try { |
---|
| 69 | this.xsltTransformation = new XsltTransformations(); |
---|
| 70 | } catch (XPathExpressionException e) { |
---|
| 71 | e.printStackTrace(); |
---|
| 72 | } catch (ParserConfigurationException e) { |
---|
| 73 | e.printStackTrace(); |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | protected JobDescription createJobDescription(String jobDesc, long puSpeed) throws IOException{ |
---|
| 78 | |
---|
| 79 | JobDescription jobDescription = null; |
---|
| 80 | try { |
---|
| 81 | jobDescription = this.xsltTransformation.splitJobToTasks(jobDesc); |
---|
| 82 | } catch (Exception e) { |
---|
| 83 | throw new IOException(e.getMessage()); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | // look for any |
---|
| 87 | |
---|
| 88 | if(jobDescription.size() == 0){ |
---|
| 89 | if(log.isWarnEnabled()) |
---|
| 90 | log.warn("Omiting job gridlet creation for job "+jobDescription.getDescription().getAppId()+". This job contains no tasks."); |
---|
| 91 | return null; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // needed for Gridlet class |
---|
| 95 | |
---|
| 96 | String jobId = jobDescription.getJobId(); |
---|
| 97 | int parserType = localWAParser.getType(); |
---|
| 98 | |
---|
| 99 | for(int i = 0; i < jobDescription.size(); i++){ |
---|
| 100 | TaskDescription taskDesc = jobDescription.get(i); |
---|
| 101 | |
---|
| 102 | String waTaskDesc[] = localWAParser.readTask(jobId, taskDesc.getTaskId()); |
---|
| 103 | if(waTaskDesc != null) { |
---|
| 104 | |
---|
| 105 | if(parserType == 0){ |
---|
| 106 | |
---|
| 107 | taskDesc.setUserDn(waTaskDesc[SWFFields.DATA_USER_ID]); |
---|
| 108 | |
---|
| 109 | long timeValue = Long.parseLong(waTaskDesc[SWFFields.DATA_SUBMIT_TIME]); |
---|
| 110 | taskDesc.setSubmissionTime(timeValue); |
---|
| 111 | |
---|
| 112 | long waitTime = Long.parseLong(waTaskDesc[GWFFields.DATA_WAIT_TIME]); |
---|
| 113 | taskDesc.setWorkloadLogWaitTime(waitTime); |
---|
| 114 | |
---|
| 115 | timeValue = Long.parseLong(waTaskDesc[SWFFields.DATA_RUN_TIME]); |
---|
| 116 | if(timeValue <= 0) |
---|
| 117 | return null; |
---|
| 118 | |
---|
| 119 | long allocProcesors = Long.parseLong(waTaskDesc[SWFFields.DATA_NUMBER_OF_ALLOCATED_PROCESSORS]); |
---|
| 120 | if(allocProcesors <= 0) |
---|
| 121 | return null; |
---|
| 122 | |
---|
| 123 | timeValue = timeValue * puSpeed * allocProcesors; |
---|
| 124 | taskDesc.setTaskLength(timeValue); |
---|
| 125 | |
---|
| 126 | } else if(parserType == 1){ |
---|
| 127 | |
---|
| 128 | taskDesc.setUserDn(waTaskDesc[GWFFields.DATA_USER_ID]); |
---|
| 129 | |
---|
| 130 | long timeValue = Long.parseLong(waTaskDesc[GWFFields.DATA_SUBMIT_TIME]); |
---|
| 131 | taskDesc.setSubmissionTime(timeValue); |
---|
| 132 | |
---|
| 133 | long waitTime = Long.parseLong(waTaskDesc[GWFFields.DATA_WAIT_TIME]); |
---|
| 134 | taskDesc.setWorkloadLogWaitTime(waitTime); |
---|
| 135 | |
---|
| 136 | timeValue = Long.parseLong(waTaskDesc[GWFFields.DATA_RUN_TIME]); |
---|
| 137 | if(timeValue <= 0) |
---|
| 138 | return null; |
---|
| 139 | |
---|
| 140 | long allocProcesors = Long.parseLong(waTaskDesc[GWFFields.DATA_NPROCS]); |
---|
| 141 | if(allocProcesors <= 0) |
---|
| 142 | return null; |
---|
| 143 | |
---|
| 144 | timeValue = timeValue * puSpeed * allocProcesors; |
---|
| 145 | taskDesc.setTaskLength(timeValue); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | this.generatedTasksCnt++; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | this.generatedJobsCnt++; |
---|
| 153 | |
---|
| 154 | return jobDescription; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | public boolean load() throws IOException, MarshalException, ValidationException{ |
---|
| 159 | |
---|
| 160 | // prepare local swf parser |
---|
| 161 | this.localWAParser = AbstractWAParser.getInstance(this.waReader.getParser().getFileName()); |
---|
| 162 | this.localWAParser.loadHeader(); |
---|
| 163 | |
---|
| 164 | long puSpeed = getPUSpeed(); |
---|
| 165 | this.simulationStartTime = getSimulationStartTimeValue(); |
---|
| 166 | |
---|
| 167 | this.localWAParser.reset(); |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | // determine which reader should be used |
---|
| 171 | String jobDesc = null; |
---|
| 172 | JobDescription job = null; |
---|
| 173 | |
---|
| 174 | if(this.xmlJobReader != null){ // use xml job reader. Xml job require xslt transformation |
---|
| 175 | while((jobDesc = this.xmlJobReader.readRaw()) != null){ |
---|
| 176 | jobDesc = this.xsltTransformation.extendJobDescription(jobDesc); |
---|
| 177 | job = createJobDescription(jobDesc, puSpeed); |
---|
| 178 | if(job != null) |
---|
| 179 | this.jobGridletsMap.put(job.getJobId(), job); |
---|
| 180 | } |
---|
| 181 | } else { // use swf job reader. Job created by this reader does not require xslt transformation |
---|
| 182 | while((jobDesc = this.waReader.readRaw()) != null){ |
---|
| 183 | job = createJobDescription(jobDesc, puSpeed); |
---|
| 184 | if(job != null) |
---|
| 185 | this.jobGridletsMap.put(job.getJobId(), job); |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | this.localWAParser.close(); |
---|
| 190 | if(this.xmlJobReader != null) |
---|
| 191 | this.xmlJobReader.close(); |
---|
| 192 | |
---|
| 193 | return true; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | protected long getPUSpeed(){ |
---|
| 197 | long puSpeed = 1; |
---|
| 198 | try { |
---|
| 199 | puSpeed = Long.parseLong(this.localWAParser.getCommentValue(WAFields.COMMENT_PUSPEED)); |
---|
| 200 | } catch (NoSuchCommentException e1) { |
---|
| 201 | if(log.isWarnEnabled()) |
---|
| 202 | log.warn("Assuming default processing unit speed = 1"); |
---|
| 203 | } |
---|
| 204 | return puSpeed; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | protected Date getSimulationStartTimeValue(){ |
---|
| 209 | Date simulationStartTime = null; |
---|
| 210 | try { |
---|
| 211 | |
---|
| 212 | DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy", Locale.ENGLISH); |
---|
| 213 | simulationStartTime = df.parse(this.localWAParser.getCommentValue(WAFields.COMMENT_STARTTIME)); |
---|
| 214 | } catch (NoSuchCommentException e){ |
---|
| 215 | simulationStartTime = new Date(0); |
---|
| 216 | if(log.isWarnEnabled()) |
---|
| 217 | log.warn("Assuming default simulation start time - "+simulationStartTime); |
---|
| 218 | } catch (ParseException e) { |
---|
| 219 | simulationStartTime = new Date(0); |
---|
| 220 | if(log.isWarnEnabled()) |
---|
| 221 | log.warn("Wrong format of simulation start time comment.\nAssuming default simulation start time - "+simulationStartTime); |
---|
| 222 | } |
---|
| 223 | return simulationStartTime; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | public int getJobCount(){ |
---|
| 227 | return generatedJobsCnt; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | public int getTaskCount(){ |
---|
| 231 | return generatedTasksCnt; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | public Date getSimulationStartTime(){ |
---|
| 235 | return this.simulationStartTime; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | public List<JobDescription> getJobs(){ |
---|
| 239 | ArrayList<JobDescription> list = |
---|
| 240 | new ArrayList<JobDescription>(this.jobGridletsMap.values()); |
---|
| 241 | return list; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | } |
---|