| 1 | package de.uka.ipd.sdq.pcmsolver.transformations.pcm2regex; |
| 2 | |
| 3 | import java.util.concurrent.TimeUnit; |
| 4 | |
| 5 | import org.apache.log4j.Logger; |
| 6 | import org.eclipse.emf.ecore.EObject; |
| 7 | |
| 8 | import de.uka.ipd.sdq.pcm.usagemodel.UsageScenario; |
| 9 | import de.uka.ipd.sdq.pcmsolver.exprsolver.ExpressionSolver; |
| 10 | import de.uka.ipd.sdq.pcmsolver.models.PCMInstance; |
| 11 | import de.uka.ipd.sdq.pcmsolver.runconfig.PCMSolverWorkflowRunConfiguration; |
| 12 | import de.uka.ipd.sdq.pcmsolver.transformations.EMFHelper; |
| 13 | import de.uka.ipd.sdq.pcmsolver.transformations.SolverStrategy; |
| 14 | import de.uka.ipd.sdq.pcmsolver.visitors.UsageModelVisitor; |
| 15 | import de.uka.ipd.sdq.pcmsolver.visualisation.JFVisualisation; |
| 16 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
| 17 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
| 18 | import de.uka.ipd.sdq.probfunction.math.ISamplePDF; |
| 19 | import de.uka.ipd.sdq.probfunction.math.ManagedPDF; |
| 20 | import de.uka.ipd.sdq.probfunction.math.PDFConfiguration; |
| 21 | import de.uka.ipd.sdq.probfunction.math.exception.ConfigurationNotSetException; |
| 22 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilityFunctionException; |
| 23 | import de.uka.ipd.sdq.probfunction.math.exception.UnknownPDFTypeException; |
| 24 | import de.uka.ipd.sdq.probfunction.print.ProbFunctionCSVPrint; |
| 25 | import de.uka.ipd.sdq.spa.expression.Expression; |
| 26 | |
| 27 | /** |
| 28 | * This is an excerpt of Heiko's dissertation (see below for link) |
| 29 | * |
| 30 | * The Stochastic Regular Expression (SRE) model is an analytical performance |
| 31 | * model in the class of semi-Markov processes [Tri01]. It consists of a |
| 32 | * discrete time Markov-chain (DTMC) to model state transitions, but the sojourn |
| 33 | * time in each state can follow arbitrary probability distributions instead of |
| 34 | * being limited to exponential distributions as in Markov chains. Furthermore, |
| 35 | * SREs are hierarchically structured and do not allow cycles in the embedded |
| 36 | * DTMC for more accurate predictions. Chapter 6.3.3 will provide the syntax and |
| 37 | * semantics of SREs, afterwards Chapter 6.3.4 shows how to compute overall |
| 38 | * sojourn times with SREs. Only a partial transformation of PCM instances to |
| 39 | * SREs is possible, because of the model�s limited expressiveness. The |
| 40 | * transformation is straight-forward, as the control flow modelling of PCM |
| 41 | * instances and SREs are closely aligned. Chapter 6.3.5 will describe the |
| 42 | * transformation PCM2SRE. While allowing accurate predictions by supporting |
| 43 | * arbitrary distribution functions for timing values, SRE are limited to |
| 44 | * analysing single-user scenarios. They do not include queues or control flow |
| 45 | * forks, and cannot express contention effects due to concurrent requests. |
| 46 | * However, they provide a fast method of producing performance predictions |
| 47 | * during early development stages, as they are usually more quickly solved than |
| 48 | * running a simulation. Chapter 6.3.6 discusses the assumptions underlying SREs |
| 49 | * in detail. The SRE model will be used for a performance prediction in a case |
| 50 | * study in Chapter 7.3.3. |
| 51 | * |
| 52 | * @see Heiko's dissertation, section 6.3 at |
| 53 | * http://docserver.bis.uni-oldenburg.de |
| 54 | * /_publikationen/dissertation/2008/kozpar08/pdf/kozpar08.pdf |
| 55 | * @author Heiko Koziolek |
| 56 | * |
| 57 | */ |
| 58 | public class Pcm2RegExStrategy implements SolverStrategy { |
| 59 | |
| 60 | Expression stoRegEx; |
| 61 | |
| 62 | protected IProbabilityFunctionFactory iProbFuncFactory = |
| 63 | IProbabilityFunctionFactory.eINSTANCE; |
| 64 | |
| 65 | private static Logger logger = Logger.getLogger(Pcm2RegExStrategy.class.getName()); |
| 66 | |
| 67 | private long overallDuration = 0; |
| 68 | |
| 69 | private PCMSolverWorkflowRunConfiguration configuration; |
| 70 | |
| 71 | public Pcm2RegExStrategy(PCMSolverWorkflowRunConfiguration configuration) { |
| 72 | this.configuration = configuration; |
| 73 | } |
| 74 | |
| 75 | public void loadTransformedModel(String fileName) { |
| 76 | EObject object = EMFHelper.loadFromXMIFile(fileName); |
| 77 | if (object instanceof Expression){ |
| 78 | this.stoRegEx = (Expression)object; |
| 79 | } else { |
| 80 | logger.warn("Could not load "+fileName+" because is not an Expression model"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public void solve() { |
| 85 | if (stoRegEx != null){ |
| 86 | long timeBeforeCalc = System.nanoTime(); |
| 87 | ExpressionSolver solver = new ExpressionSolver(); |
| 88 | ManagedPDF resultPDF = solver.getResponseTime(stoRegEx); |
| 89 | |
| 90 | if(resultPDF == null){ |
| 91 | logger.error("StochasticRegularExpression could not be solved!"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | long timeAfterCalc = System.nanoTime(); |
| 96 | long duration = TimeUnit.NANOSECONDS.toMillis(timeAfterCalc-timeBeforeCalc); |
| 97 | overallDuration += duration; |
| 98 | logger.info("Finished Running ExprSolver:\t"+ duration + " ms"); |
| 99 | logger.debug("Resulting PDF:\t\t\t"+resultPDF.toString()); |
| 100 | logger.trace("As csv:\n\nx;probability\n"+new ProbFunctionCSVPrint().doSwitch(resultPDF.getModelBoxedPdf())); |
| 101 | |
| 102 | visualize(resultPDF.getPdfTimeDomain()); |
| 103 | long timeAfterVisualisation = System.nanoTime(); |
| 104 | |
| 105 | //logger.info("PDF in time domain: "+resultPDF.getPdfTimeDomain()); |
| 106 | |
| 107 | duration = TimeUnit.NANOSECONDS.toMillis(timeAfterVisualisation-timeAfterCalc); |
| 108 | overallDuration += duration; |
| 109 | logger.info("Finished Visualisation:\t\t"+ duration + " ms"); |
| 110 | logger.info("Finished SRE-Solver:\t\t"+ overallDuration+ " ms"); |
| 111 | |
| 112 | } else |
| 113 | logger.error("No StochasticRegularExpression available for solution!"); |
| 114 | } |
| 115 | |
| 116 | public void storeTransformedModel(String fileName) { |
| 117 | |
| 118 | EMFHelper.saveToXMIFile(stoRegEx, fileName); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | public void transform(PCMInstance model) { |
| 123 | |
| 124 | if (!this.configuration.isUseSREInputModel()){ |
| 125 | long timeBeforeCalc = System.nanoTime(); |
| 126 | runDSolver(model); |
| 127 | runPcm2RegEx(model); |
| 128 | printStoRegEx(); |
| 129 | |
| 130 | storeTransformedModel(this.configuration.getSREOutputFile()); |
| 131 | |
| 132 | long timeAfterCalc = System.nanoTime(); |
| 133 | long duration = TimeUnit.NANOSECONDS.toMillis(timeAfterCalc-timeBeforeCalc); |
| 134 | overallDuration += duration; |
| 135 | logger.info("Finished Running PCM2SRE:\t\t"+ duration + " ms"); |
| 136 | } else { |
| 137 | String filename = this.configuration.getSREOutputFile(); |
| 138 | loadTransformedModel(filename); |
| 139 | logger.warn("Using predefined Expression model "+filename); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | } |
| 144 | |
| 145 | private void printStoRegEx() { |
| 146 | ExpressionPrinter expPrinter = new ExpressionPrinter(); |
| 147 | expPrinter.doSwitch(stoRegEx); |
| 148 | logger.debug("ExpressionPrinter: "+expPrinter.getOutput()); |
| 149 | } |
| 150 | |
| 151 | private void runPcm2RegEx(PCMInstance model) { |
| 152 | TransformUsageModelVisitor umVisit = new TransformUsageModelVisitor(model); |
| 153 | UsageScenario us = (UsageScenario)model.getUsageModel().getUsageScenario_UsageModel().get(0); |
| 154 | try { |
| 155 | stoRegEx = (Expression)umVisit.doSwitch(us.getScenarioBehaviour_UsageScenario()); |
| 156 | } catch (Exception e) { |
| 157 | logger.error("Transforming the PCM instance into a stochastic regular expression caused an Exception! Check your model for broken references, e.g. old, dangling Connectors." + e.getMessage()); |
| 158 | e.printStackTrace(); |
| 159 | |
| 160 | throw new RuntimeException(e); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | private void runDSolver(PCMInstance model) { |
| 165 | UsageModelVisitor visitor = new UsageModelVisitor(model); |
| 166 | try { |
| 167 | UsageScenario us = (UsageScenario) model.getUsageModel() |
| 168 | .getUsageScenario_UsageModel().get(0); |
| 169 | visitor.doSwitch(us.getScenarioBehaviour_UsageScenario()); |
| 170 | } catch (Exception e) { |
| 171 | logger.error("Running the dependency solver caused an Exception! Check your model for broken references, e.g. old, dangling Connectors." + e.getMessage()); |
| 172 | e.printStackTrace(); |
| 173 | |
| 174 | throw new RuntimeException(e); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private void visualize(IProbabilityDensityFunction iPDF) { |
| 179 | ISamplePDF samplePDF = null; |
| 180 | try { |
| 181 | samplePDF = iProbFuncFactory.transformToSamplePDF(iPDF); |
| 182 | } catch (UnknownPDFTypeException e1) { |
| 183 | // TODO Auto-generated catch block |
| 184 | e1.printStackTrace(); |
| 185 | } |
| 186 | |
| 187 | try { |
| 188 | double dist = 0.0; |
| 189 | try { |
| 190 | dist = PDFConfiguration.getCurrentConfiguration().getDistance(); |
| 191 | } catch (ConfigurationNotSetException e) { |
| 192 | // TODO Auto-generated catch block |
| 193 | e.printStackTrace(); |
| 194 | } |
| 195 | JFVisualisation vis = new JFVisualisation(dist); |
| 196 | vis.addSamplePDF(samplePDF,"Execution Time"); |
| 197 | vis.visualizeOverlay(); |
| 198 | } catch (ProbabilityFunctionException e) { |
| 199 | e.printStackTrace(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | } |