1 package Analysis.Scheduling;
4 import java.io.FileOutputStream;
5 import java.io.PrintWriter;
6 import java.util.Collection;
7 import java.util.Enumeration;
8 import java.util.HashSet;
9 import java.util.Hashtable;
10 import java.util.Iterator;
12 import java.util.Vector;
13 import java.util.Map.Entry;
15 import Analysis.Scheduling.ScheduleSimulator.Action;
16 import Analysis.Scheduling.ScheduleSimulator.CheckPoint;
17 import Analysis.TaskStateAnalysis.Allocations;
18 import Analysis.TaskStateAnalysis.FEdge;
19 import Analysis.TaskStateAnalysis.FlagState;
20 import Analysis.TaskStateAnalysis.FEdge.NewObjInfo;
21 import IR.ClassDescriptor;
23 import IR.Tree.FlagExpressionNode;
24 import IR.Tree.FlagNode;
25 import IR.Tree.FlagOpNode;
27 import Util.GraphNode;
30 public class SchedulingUtil {
32 /*public static int maxDivisor(int l, int r) {
44 if(((a&1)==0) && ((b&1)==0)) {
45 // a and b are both even
49 } else if(((a&1)==0) && ((b&1)!=0)) {
50 // a is even, b is odd
52 } else if (((a&1)!=0) && ((b&1)==0)) {
53 // a is odd, b is even
55 } else if (((a&1)!=0) && ((b&1)!=0)) {
56 // a and b are both odd
58 a = a>b ? (a-b):(b-a);
64 public static boolean isTaskTrigger_flag(FlagExpressionNode fen,FlagState fs) {
67 else if (fen instanceof FlagNode)
68 return fs.get(((FlagNode)fen).getFlag());
70 switch (((FlagOpNode)fen).getOp().getOp()) {
71 case Operation.LOGIC_AND:
72 return ((isTaskTrigger_flag(((FlagOpNode)fen).getLeft(),fs)) && (isTaskTrigger_flag(((FlagOpNode)fen).getRight(),fs)));
73 case Operation.LOGIC_OR:
74 return ((isTaskTrigger_flag(((FlagOpNode)fen).getLeft(),fs)) || (isTaskTrigger_flag(((FlagOpNode)fen).getRight(),fs)));
75 case Operation.LOGIC_NOT:
76 return !(isTaskTrigger_flag(((FlagOpNode)fen).getLeft(),fs));
82 public static void printScheduleGraph(String path, Vector<ScheduleNode> sNodes) {
84 File file=new File(path);
85 FileOutputStream dotstream=new FileOutputStream(file,false);
86 PrintWriter output = new java.io.PrintWriter(dotstream, true);
87 output.println("digraph G {");
88 output.println("\tcompound=true;\n");
89 traverseSNodes(output, sNodes);
90 output.println("}\n");
92 } catch (Exception e) {
98 private static void traverseSNodes(PrintWriter output, Vector<ScheduleNode> sNodes){
99 //Draw clusters representing ScheduleNodes
100 Iterator it = sNodes.iterator();
101 while (it.hasNext()) {
102 ScheduleNode gn = (ScheduleNode) it.next();
103 Iterator edges = gn.edges();
104 output.println("\tsubgraph " + gn.getLabel() + "{");
105 output.println("\t\tlabel=\"" + gn.getTextLabel() + "\";");
106 Iterator it_cnodes = gn.getClassNodesIterator();
107 traverseCNodes(output, it_cnodes);
108 //Draw the internal 'new' edges
109 Iterator it_edges =gn.getScheduleEdgesIterator();
110 while(it_edges.hasNext()) {
111 ScheduleEdge se = (ScheduleEdge)it_edges.next();
113 if(se.getSourceCNode().isclone()) {
114 output.print(se.getSourceCNode().getLabel());
116 if(se.getSourceFState() == null) {
117 output.print(se.getSourceCNode().getClusterLabel());
119 output.print(se.getSourceFState().getLabel());
123 output.print(" -> ");
124 //if(se.getTargetFState() == null) {
126 if(se.getTargetCNode().isclone()) {
127 output.print(se.getTargetCNode().getLabel());
129 output.print(se.getTargetCNode().getClusterLabel());
131 output.println(" [label=\"" + se.getLabel() + "\", color=red];");
133 output.print(se.getTargetFState().getLabel() + " [label=\"" + se.getLabel() + "\", color=red, ltail=");
134 if(se.getSourceCNode().isclone()) {
135 output.println(se.getSourceCNode().getLabel() + "];");
137 output.println(se.getSourceCNode().getClusterLabel() + "];");
141 output.println("\t}\n");
142 //Draw 'new' edges of this ScheduleNode
143 while(edges.hasNext()) {
144 ScheduleEdge se = (ScheduleEdge)edges.next();
146 if(se.getSourceCNode().isclone()) {
147 output.print(se.getSourceCNode().getLabel());
149 if(se.getSourceFState() == null) {
150 output.print(se.getSourceCNode().getClusterLabel());
152 output.print(se.getSourceFState().getLabel());
156 output.print(" -> ");
157 //if(se.getTargetFState() == null) {
159 if(se.getTargetCNode().isclone()) {
160 output.print(se.getTargetCNode().getLabel());
162 output.print(se.getTargetCNode().getClusterLabel());
164 output.println(" [label=\"" + se.getLabel() + "\", color=red, style=dashed]");
166 output.println(se.getTargetFState().getLabel() + " [label=\"" + se.getLabel() + "\", color=red, style=dashed]");
172 private static void traverseCNodes(PrintWriter output, Iterator it){
173 //Draw clusters representing ClassNodes
174 while (it.hasNext()) {
175 ClassNode gn = (ClassNode) it.next();
177 output.println("\t\t" + gn.getLabel() + " [style=dashed, label=\"" + gn.getTextLabel() + "\", shape=box];");
179 output.println("\tsubgraph " + gn.getClusterLabel() + "{");
180 output.println("\t\tstyle=dashed;");
181 output.println("\t\tlabel=\"" + gn.getTextLabel() + "\";");
182 traverseFlagStates(output, gn.getFlagStates());
183 output.println("\t}\n");
188 private static void traverseFlagStates(PrintWriter output, Collection nodes) {
189 Set cycleset=GraphNode.findcycles(nodes);
190 Vector namers=new Vector();
191 namers.add(new Namer());
192 namers.add(new Allocations());
193 //namers.add(new TaskEdges());
195 Iterator it = nodes.iterator();
196 while (it.hasNext()) {
197 GraphNode gn = (GraphNode) it.next();
198 Iterator edges = gn.edges();
200 String dotnodeparams="";
202 for(int i=0;i<namers.size();i++) {
203 Namer name=(Namer) namers.get(i);
204 String newlabel=name.nodeLabel(gn);
205 String newparams=name.nodeOption(gn);
207 if (!newlabel.equals("") && !label.equals("")) {
210 if (!newparams.equals("")) {
211 dotnodeparams+=", " + name.nodeOption(gn);
213 label+=name.nodeLabel(gn);
215 label += ":[" + ((FlagState)gn).getExeTime() + "]";
218 output.println("\t" + gn.getLabel() + " [label=\"" + label + "\"" + dotnodeparams + "];");
221 while (edges.hasNext()) {
222 Edge edge = (Edge) edges.next();
223 GraphNode node = edge.getTarget();
224 if (nodes.contains(node)) {
225 for(Iterator nodeit=nonmerge(node, nodes).iterator();nodeit.hasNext();) {
226 GraphNode node2=(GraphNode)nodeit.next();
227 String edgelabel = "";
228 String edgedotnodeparams="";
230 for(int i=0;i<namers.size();i++) {
231 Namer name=(Namer) namers.get(i);
232 String newlabel=name.edgeLabel(edge);
233 String newoption=name.edgeOption(edge);
234 if (!newlabel.equals("")&& ! edgelabel.equals(""))
237 if (!newoption.equals(""))
238 edgedotnodeparams+=", "+newoption;
240 edgelabel+=":[" + ((FEdge)edge).getExeTime() + "]";
241 Hashtable<ClassDescriptor, NewObjInfo> hashtable = ((FEdge)edge).getNewObjInfoHashtable();
242 if(hashtable != null) {
243 Set<ClassDescriptor> keys = hashtable.keySet();
244 Iterator it_keys = keys.iterator();
245 while(it_keys.hasNext()) {
246 ClassDescriptor cd = (ClassDescriptor)it_keys.next();
247 NewObjInfo noi = hashtable.get(cd);
248 edgelabel += ":{ class " + cd.getSymbol() + " | " + noi.getNewRate() + " | (" + noi.getProbability() + "%) }";
251 output.println("\t" + gn.getLabel() + " -> " + node2.getLabel() + " [" + "label=\"" + edgelabel + "\"" + edgedotnodeparams + "];");
258 private static Set nonmerge(GraphNode gn, Collection nodes) {
259 HashSet newset=new HashSet();
260 HashSet toprocess=new HashSet();
262 while(!toprocess.isEmpty()) {
263 GraphNode gn2=(GraphNode)toprocess.iterator().next();
264 toprocess.remove(gn2);
268 Iterator edges = gn2.edges();
269 while (edges.hasNext()) {
270 Edge edge = (Edge) edges.next();
271 GraphNode node = edge.getTarget();
272 if (!newset.contains(node)&&nodes.contains(node))
280 public static void printSimulationResult(String path, int time, int coreNum, Vector<CheckPoint> checkpoints) {
282 File file=new File(path);
283 FileOutputStream dotstream=new FileOutputStream(file,false);
284 PrintWriter output = new java.io.PrintWriter(dotstream, true);
285 output.println("digraph simulation{");
287 output.println("node [shape=plaintext];");
289 output.println("edge [dir=none];");
291 output.println("ranksep=.05;");
297 output.print("{rank=source; \"Time\"; ");
298 for(j = 0; j < coreNum; j++) {
299 output.print("\"core " + j + "\"; ");
302 // time coordinate nodes
303 Vector<String> timeNodes = new Vector<String>();
304 String[] lastTaskNodes = new String[coreNum];
305 boolean[] isTaskFinish = new boolean[coreNum];
306 for(j = 0; j < coreNum; j++) {
307 lastTaskNodes[j] = "first";
308 isTaskFinish[j] = true;
311 for(j = 0; j < checkpoints.size(); j++) {
312 CheckPoint tcp = checkpoints.elementAt(j);
313 String tnode = String.valueOf(tcp.getTimepoint());
314 if(!timeNodes.contains(tnode)) {
315 timeNodes.add(tnode);
317 Vector<Action> actions = tcp.getActions();
318 Hashtable<String, StringBuffer> tmpTaskNodes = new Hashtable<String, StringBuffer>();
319 //Vector<String> sortedttnodes = new Vector<String>();
320 for(int i = 0; i < actions.size(); i++) {
321 Action taction = actions.elementAt(i);
322 int cNum = taction.getCoreNum();
323 String tmpTaskNode = "\"" + tnode + "core" + cNum + "\"";
324 StringBuffer tmpLabel = null;
325 boolean isfirst = false;
326 if(!tmpTaskNodes.containsKey(tmpTaskNode)) {
327 tmpTaskNodes.put(tmpTaskNode, new StringBuffer(tnode + ":"));
329 /*int length = sortedttnodes.size();
332 String tmptnode = sortedttnodes.elementAt(k-1);
333 int tcorenum = Integer.parseInt(tmptnode.substring(tmptnode.indexOf("core") + 4, tmptnode.length() - 1));
334 if(tcorenum < cNum) {
338 sortedttnodes.add(k, tmpTaskNode);*/
340 tmpLabel = tmpTaskNodes.get(tmpTaskNode);
341 switch(taction.getType()){
342 case Action.ADDOBJ: {
344 tmpLabel.append("\\n");
346 tmpLabel.append("(" + taction.getTransObj().getSymbol() + ")arrives;");
347 if(!(lastTaskNodes[cNum].equals(tmpTaskNode))) {
349 if(lastTaskNodes[cNum].equals("first")) {
350 output.println("\"core " + cNum + "\"->" + tmpTaskNode);
352 output.print(lastTaskNodes[cNum] + "->" + tmpTaskNode);
354 if(isTaskFinish[cNum]) {
355 output.print(" [style=invis]");
358 lastTaskNodes[cNum] = tmpTaskNode;
362 case Action.TASKFINISH: {
364 tmpLabel.append("\\n");
366 tmpLabel.append("<" + taction.getTd().getSymbol() + ">finishes;");
367 if(!(lastTaskNodes[cNum].equals("first")) &&
368 !(lastTaskNodes[cNum].equals(tmpTaskNode))) {
370 output.println(lastTaskNodes[cNum] + "->" + tmpTaskNode);
371 lastTaskNodes[cNum] = tmpTaskNode;
372 isTaskFinish[cNum] = true;
374 throw new Exception("Error: unexpected task finish");
378 case Action.TFWITHOBJ: {
380 tmpLabel.append("\\n");
382 tmpLabel.append("<" + taction.getTd().getSymbol() + ">finishes; ");
383 Iterator<Entry<ClassDescriptor, Integer>> it_entry = (Iterator<Entry<ClassDescriptor, Integer>>)taction.getNObjs().entrySet().iterator();
384 while(it_entry.hasNext()) {
385 Entry<ClassDescriptor, Integer> entry = it_entry.next();
386 tmpLabel.append(entry.getValue() + "(" + entry.getKey().getSymbol() + ")");
387 if(it_entry.hasNext()) {
388 tmpLabel.append(",");
390 tmpLabel.append(";");
393 if(!(lastTaskNodes[cNum].equals("first")) &&
394 !(lastTaskNodes[cNum].equals(tmpTaskNode))) {
396 output.println(lastTaskNodes[cNum] + "->" + tmpTaskNode);
397 lastTaskNodes[cNum] = tmpTaskNode;
398 isTaskFinish[cNum] = true;
400 throw new Exception("Error: unexpected task finish");
404 case Action.TASKSTART: {
406 tmpLabel.append("\\n");
408 tmpLabel.append("<" + taction.getTd().getSymbol() + ">starts;");
410 if (!(lastTaskNodes[cNum].equals(tmpTaskNode))) {
412 if(lastTaskNodes[cNum].equals("first")) {
413 output.print("\"core " + cNum + "\"->" + tmpTaskNode);
415 output.print(lastTaskNodes[cNum] + "->" + tmpTaskNode);
417 if(isTaskFinish[cNum]) {
418 output.print(" [style=invis]");
421 lastTaskNodes[cNum] = tmpTaskNode;
423 isTaskFinish[cNum] = false;
428 Enumeration<String> keys = tmpTaskNodes.keys();
429 while(keys.hasMoreElements()) {
430 String tmpTaskNode = keys.nextElement();
432 output.println(tmpTaskNode + "[label=\"" + tmpTaskNodes.get(tmpTaskNode).toString() + "\"]");
435 output.print("{rank=same; rankdir=LR; " + tnode + "; ");
436 /*for(int k = 0; k < sortedttnodes.size(); k++) {
437 output.print(sortedttnodes.elementAt(k));
440 keys = tmpTaskNodes.keys();
441 while(keys.hasMoreElements()) {
442 String tmpTaskNode = keys.nextElement();
443 output.print(tmpTaskNode);
448 /*output.print(tnode + "->");
449 for(int k = 0; k < sortedttnodes.size() - 1; k++) {
450 output.print(sortedttnodes.elementAt(k) + "->");
452 output.println(sortedttnodes.lastElement() + " [style=dashed];");*/
455 //output.println("node [shape=point, color=blue];");
457 output.println("\"Time\"->" + timeNodes.elementAt(0) + "[style=invis];");
458 //for(j = 0; j < timeNodes.size() - 1; j++) {
459 for(j = 0; j < time; j++) {
460 output.print(j + "->");
462 output.println(timeNodes.lastElement() + ";");
465 } catch (Exception e) {