1 | package de.uka.ipd.sdq.pcmsolver.transformations.pcm2lqn; |
2 | |
3 | import java.math.BigInteger; |
4 | |
5 | import org.apache.log4j.Logger; |
6 | import org.eclipse.emf.common.util.EList; |
7 | |
8 | import LqnCore.ActivityPhasesType; |
9 | import LqnCore.EntryType; |
10 | import LqnCore.PhaseActivities; |
11 | import LqnCore.ProcessorType; |
12 | import LqnCore.SchedulingType; |
13 | import LqnCore.TaskType; |
14 | import LqnCore.TypeType; |
15 | import de.uka.ipd.sdq.pcm.core.PCMRandomVariable; |
16 | import de.uka.ipd.sdq.pcm.resourceenvironment.CommunicationLinkResourceSpecification; |
17 | import de.uka.ipd.sdq.pcm.resourceenvironment.LinkingResource; |
18 | import de.uka.ipd.sdq.pcm.resourceenvironment.ProcessingResourceSpecification; |
19 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceContainer; |
20 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceEnvironment; |
21 | import de.uka.ipd.sdq.pcm.resourceenvironment.util.ResourceenvironmentSwitch; |
22 | import de.uka.ipd.sdq.pcm.resourcetype.CommunicationLinkResourceType; |
23 | import de.uka.ipd.sdq.pcm.resourcetype.ProcessingResourceType; |
24 | import de.uka.ipd.sdq.pcmsolver.runconfig.PCMSolverWorkflowRunConfiguration; |
25 | import de.uka.ipd.sdq.pcmsolver.transformations.ExpressionToPDFWrapper; |
26 | import de.uka.ipd.sdq.pcmsolver.visitors.ExpressionHelper; |
27 | |
28 | public class ResourceEnvironment2Lqn extends ResourceenvironmentSwitch { |
29 | |
30 | private static Logger logger = Logger.getLogger(ResourceEnvironment2Lqn.class.getName()); |
31 | |
32 | private LqnBuilder lqnBuilder; |
33 | private PCMSolverWorkflowRunConfiguration config; |
34 | |
35 | public ResourceEnvironment2Lqn(LqnBuilder aLqnBuilder, PCMSolverWorkflowRunConfiguration aConfig) { |
36 | lqnBuilder = aLqnBuilder; |
37 | config = aConfig; |
38 | } |
39 | |
40 | @Override |
41 | public Object caseCommunicationLinkResourceSpecification( |
42 | CommunicationLinkResourceSpecification object) { |
43 | LinkingResource lr = (LinkingResource) object.eContainer(); |
44 | CommunicationLinkResourceType clrt = object |
45 | .getCommunicationLinkResourceType_CommunicationLinkResourceSpecification(); |
46 | String id = Pcm2LqnHelper.getIdForCommResource(lr,clrt); |
47 | |
48 | PCMRandomVariable throughputVariable = object.getThroughput_CommunicationLinkResourceSpecification(); |
49 | PCMRandomVariable latencyVariable = object.getLatency_CommunicationLinkResourceSpecification(); |
50 | |
51 | |
52 | if (throughputVariable != null || latencyVariable != null){ |
53 | //only handle linking resource if a throughput or latency is specified |
54 | |
55 | ProcessorType pt = lqnBuilder.addProcessor(id); |
56 | pt.setSpeedFactor("1"); |
57 | pt.setScheduling(SchedulingType.FCFS); |
58 | |
59 | TaskType tt = lqnBuilder.addTask(id, pt); |
60 | |
61 | // Throughput: Create throughput entry that can be called |
62 | // for remote external call with bytesize information |
63 | // FIXME: Calls to this need to be added with BYTESIZE info. Not yet supported. |
64 | if (throughputVariable != null){ |
65 | |
66 | String throughputID = Pcm2LqnHelper.getIdForThroughput(id); |
67 | ActivityPhasesType apt = lqnBuilder.addActivityPhases(throughputID); |
68 | PhaseActivities pa = lqnBuilder.addPhaseActivities(apt); |
69 | EntryType et = lqnBuilder.addEntry(throughputID, tt); |
70 | et.setType(TypeType.NONE); |
71 | et.setEntryPhaseActivities(pa); |
72 | |
73 | // callers must call this task BYTESIZE times, that should make no difference for the LQN mean value analysis |
74 | // we could thin about aggregating this to KB or so, but that might cause misunderstandings. |
75 | // alternatively, for each call using a BYTESIZE, a new entry of this processor must be created. |
76 | double throughputSpeedFactor = 1/ExpressionHelper.getMeanValue(throughputVariable); |
77 | apt.setHostDemandMean(""+throughputSpeedFactor); |
78 | |
79 | //TODO: each external call can create a new entry here with the demand (compare internal actions) |
80 | |
81 | } |
82 | if (latencyVariable != null){ |
83 | // careful: calls to the latency are generated only if latency is larger > 0 |
84 | |
85 | ExpressionToPDFWrapper expToPDF = ExpressionToPDFWrapper.createExpressionToPDFWrapper(latencyVariable.getExpression()); |
86 | double latency = expToPDF.getMeanValue(); |
87 | |
88 | CallType[] callTypes = CallType.values(); |
89 | // LQN does not allow to receive synchronous and asynchronous calls at one entry, |
90 | // so need to create one entry per call type. |
91 | for (int i = 0; i < callTypes.length; i++) { |
92 | CallType callType = callTypes[i]; |
93 | |
94 | String latencyID = Pcm2LqnHelper.getIdForLatency(id, callType); |
95 | ActivityPhasesType apt = lqnBuilder.addActivityPhases(latencyID); |
96 | PhaseActivities pa = lqnBuilder.addPhaseActivities(apt); |
97 | |
98 | EntryType et = lqnBuilder.addEntry(latencyID, tt); |
99 | et.setType(TypeType.PH1PH2); |
100 | et.setEntryPhaseActivities(pa); |
101 | |
102 | apt.setHostDemandMean(""+latency); |
103 | double stdev = expToPDF.getStandardDeviation(); |
104 | if (!Double.isNaN(stdev) && latency != 0){ |
105 | // squared coefficient of variation |
106 | apt.setHostDemandCvsq(""+(stdev*stdev/(latency*latency))); |
107 | } |
108 | } |
109 | } |
110 | |
111 | } else { |
112 | logger.warn("Found linking resource with neither throughput specification nor latency specification, ignoring it."); |
113 | } |
114 | |
115 | return null; |
116 | } |
117 | |
118 | @Override |
119 | public Object caseLinkingResource(LinkingResource object) { |
120 | doSwitch(object |
121 | .getCommunicationLinkResourceSpecifications_LinkingResource()); |
122 | return null; |
123 | } |
124 | |
125 | @Override |
126 | public Object caseProcessingResourceSpecification( |
127 | ProcessingResourceSpecification object) { |
128 | ResourceContainer rc = (ResourceContainer) object.eContainer(); |
129 | ProcessingResourceType prt = object |
130 | .getActiveResourceType_ActiveResourceSpecification(); |
131 | String id = Pcm2LqnHelper.getIdForProcResource(rc, prt); |
132 | |
133 | ProcessorType pt = lqnBuilder.addProcessor(id); |
134 | setSchedulingPolicy(object, pt); |
135 | |
136 | // The speed of the processing resource is already considered in the resource demands, |
137 | // they are calculated in term of tiem spent on the processor. |
138 | /*String processingRate = object |
139 | .getProcessingRate_ProcessingResourceSpecification() |
140 | .getSpecification();*/ |
141 | pt.setSpeedFactor("1.0"); |
142 | //pt.setMultiplicity(new BigInteger("1")); |
143 | pt.setMultiplicity(new BigInteger(""+object.getNumberOfReplicas())); |
144 | |
145 | // String processingRateSolved = ExpressionHelper.getSolvedExpressionAsString(processingRate, null); |
146 | // |
147 | // pt.setSpeedFactor(processingRateSolved); |
148 | |
149 | TaskType tt = lqnBuilder.addTask(id,pt); |
150 | ActivityPhasesType apt = lqnBuilder.addActivityPhases(id); |
151 | PhaseActivities pa = lqnBuilder.addPhaseActivities(apt); |
152 | EntryType et = lqnBuilder.addEntry(id, tt); |
153 | et.setType(TypeType.NONE); |
154 | et.setEntryPhaseActivities(pa); |
155 | |
156 | return null; |
157 | } |
158 | |
159 | private void setSchedulingPolicy(ProcessingResourceSpecification object, |
160 | ProcessorType pt) { |
161 | switch (object.getSchedulingPolicy()) { |
162 | case FCFS: |
163 | pt.setScheduling(SchedulingType.FCFS); |
164 | break; |
165 | case DELAY: |
166 | // The SchedulingType.INF has been added manually to the generated code, so |
167 | // there may appear a compile error here if the LQN code is regenerated again. |
168 | // I purposefully did not set it to "generated not" so that this problem is |
169 | // brought to attention whenever the model is regenerated |
170 | // However, if the model is successfully regenerated from the LQN version 4.3 or later XML schema |
171 | // the inf value should be available in the generated code and this note can be deleted. |
172 | pt.setScheduling(SchedulingType.INF); |
173 | break; |
174 | case PROCESSOR_SHARING: |
175 | pt.setScheduling(SchedulingType.PS); |
176 | pt.setQuantum(config.getPsQuantum()); |
177 | break; |
178 | default : |
179 | logger.warn("Unknown scheduling strategy " |
180 | + object.getSchedulingPolicy().getName() |
181 | + ", using PROCESSOR_SHARING for " |
182 | + object |
183 | .getActiveResourceType_ActiveResourceSpecification() |
184 | .getEntityName() |
185 | + " of server " |
186 | + object |
187 | .getResourceContainer_ProcessingResourceSpecification() |
188 | .getEntityName() |
189 | + " (id: " |
190 | + object |
191 | .getResourceContainer_ProcessingResourceSpecification() |
192 | .getId() + ")"); |
193 | pt.setScheduling(SchedulingType.PS); |
194 | pt.setQuantum(config.getPsQuantum()); |
195 | } |
196 | } |
197 | |
198 | @Override |
199 | public Object caseResourceContainer(ResourceContainer object) { |
200 | EList<ProcessingResourceSpecification> procResList = object |
201 | .getActiveResourceSpecifications_ResourceContainer(); |
202 | for (ProcessingResourceSpecification prs : procResList) { |
203 | doSwitch(prs); |
204 | } |
205 | return null; |
206 | } |
207 | |
208 | @Override |
209 | public Object caseResourceEnvironment(ResourceEnvironment object) { |
210 | EList<ResourceContainer> resConList = object |
211 | .getResourceContainer_ResourceEnvironment(); |
212 | for (ResourceContainer rc : resConList) { |
213 | doSwitch(rc); //TODO |
214 | } |
215 | |
216 | EList<LinkingResource> linkResList = object.getLinkingResources__ResourceEnvironment(); |
217 | for (LinkingResource lr : linkResList) { |
218 | doSwitch(lr); |
219 | } |
220 | |
221 | return null; |
222 | } |
223 | |
224 | } |