📄 wcetanalyser.java
字号:
sb.append("\n/*"+ jc.getClassName() + "." + methodbcel.getName() + methodbcel.getSignature()+"*/\n"); if(!global){ sb.append("digraph G {\n"); sb.append("size = \"10,7.5\"\n"); } for (int i = 0; i < dg.length; i++) { for (int j = 0; j < dg.length; j++) { if(dg[i][j]>0){ sb.append("\t"+getBbs(i).toDotFlowEdge(getBbs(j))); if(labels){ //sb.append(" [label=\""+dg[i][j]+"\""); String edge = getBbs(i).toDotFlowLabel(getBbs(j)); if(wcetvars.get(edge)!=null){ int edgeval = Integer.parseInt((String)wcetvars.get(edge)); if(edgeval>0) sb.append(" [label=\""+getBbs(i).toDotFlowLabel(getBbs(j))+"="+edgeval+"\""); else sb.append(" [style=dashed,label=\""+getBbs(i).toDotFlowLabel(getBbs(j))+"="+edgeval+"\""); } else sb.append(" [label=\""+getBbs(i).toDotFlowLabel(getBbs(j))+"=?\""); //sb.append(",labelfloat=true"); sb.append("]"); } sb.append(";\n"); } } } for (Iterator iter = bbs.keySet().iterator(); iter.hasNext();) { Integer keyInt = (Integer) iter.next(); WCETBasicBlock wcbb = (WCETBasicBlock) bbs.get(keyInt); int id = wcbb.getBid(); if(wcbb.nodetype != WCETBasicBlock.SNODE && wcbb.nodetype != WCETBasicBlock.TNODE) sb.append("\t"+wcbb.getIDS()+" [label=\""+wcbb.getIDS()+"\\n"+wcbb.wcetHit+"\"];\n"); else sb.append("\t"+wcbb.getIDS()+";\n"); } if(!global){ sb.append("}\n"); } if(!global){ try { dotf = new File(WCETAnalyser.outFile).getParentFile().getAbsolutePath()+"\\"+jc.getClassName()+"."+methodbcel.getName()+".dot"; dotf = dotf.replace('<','_'); dotf = dotf.replace('>','_'); dotf = dotf.replace('\\','/'); PrintWriter dotout = new PrintWriter(new FileOutputStream(dotf)); dotout.write(sb.toString()); dotout.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } } return sb.toString(); } //TODO: loop follows loop controller? /** * @param global follow the invokes * @param term terminate with s=1, t=1 * @param invowcbb the invoking wcbb or null */ public String toLS(boolean global, boolean term, WCETBasicBlock invowcbb){ if(global) E++; StringBuffer ls = new StringBuffer(); StringBuffer lsinvo = new StringBuffer(); lsobj = new StringBuffer(); ls.append("/* WCA flow constraints: "+name+" : M"+mid+" */\n"); lsobj.append(toLSO()); WCETBasicBlock wcbb = null; for (Iterator iter = bbs.keySet().iterator(); iter.hasNext();) { Integer keyInt = (Integer) iter.next(); wcbb = (WCETBasicBlock) bbs.get(keyInt); //S if(wcbb.nodetype==WCETBasicBlock.SNODE) if(invowcbb != null) ls.append(wcbb.toLSS(invowcbb)); else ls.append(wcbb.toLSS(null)); else if(wcbb.nodetype==WCETBasicBlock.BNODE || wcbb.nodetype==WCETBasicBlock.INODE){ ls.append(wcbb.toLSFlow()); if(wcbb.loopcontroller) ls.append(wcbb.toLSLoop()); } else if(wcbb.nodetype==WCETBasicBlock.TNODE){ if(invowcbb != null) ls.append(wcbb.toLST(invowcbb)); else ls.append(wcbb.toLST(null)); } if(wcbb.nodetype==WCETBasicBlock.INODE && global){ lsinvo.append(wcbb.invowcmb.toLS(global,false, wcbb)); ls.append(wcbb.toLSInvo()); lsobj.append(" "+wcbb.invowcmb.getLSO()); } } ls.append("/* WCA flow to cycle count */\n"); for (Iterator iter = bbs.keySet().iterator(); iter.hasNext();) { Integer keyInt = (Integer) iter.next(); wcbb = (WCETBasicBlock) bbs.get(keyInt); if(wcbb.nodetype==WCETBasicBlock.BNODE || wcbb.nodetype==WCETBasicBlock.INODE){ ls.append(wcbb.toLSCycles()); } } if(wcbb.invowcmb != null){ ls.append("/* Invocation(s) from "+name+":[M"+mid+"] -> "+wcbb.invowcmb.name+":[M"+mid+"]*/\n"); ls.append(lsinvo.toString()); } else{ ls.append("/* Invocation(s) from "+name+":[M"+mid+"] */\n"); ls.append(lsinvo.toString()); } if(term){ // once //String lso = obs.toString(); StringBuffer lso = new StringBuffer(); lso.append("/***WCET calculation source***/\n"); lso.append("/* WCA WCET objective: "+jc.getClassName() + "." + methodbcel.getName()+ " */\n"); lso.append("max: "+lsobj.toString()+";\n"); ls.insert(0, lso.toString()); try { lpf = new File(WCETAnalyser.outFile).getParentFile().getAbsolutePath()+"\\"+jc.getClassName()+"."+methodbcel.getName()+".lp"; lpf = lpf.replace('<','_'); lpf = lpf.replace('>','_'); //System.out.println("about to write:"+lpf); PrintWriter lsout = new PrintWriter(new FileOutputStream(lpf)); lsout.write(ls.toString());//System.out.println("LS to be solved:"+ls.toString()); lsout.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { wcetvars = new HashMap(); LpSolve problem = LpSolve.readLp(lpf, LpSolve.NORMAL, jc.getClassName()+"."+methodbcel.getName()); problem.setOutputfile(lpf+".output.txt"); problem.solve(); problem.setOutputfile(lpf+".solved.txt"); problem.printObjective(); problem.printSolution(1); wcetlp = (int)problem.getObjective(); try { BufferedReader in = new BufferedReader(new FileReader(lpf+".solved.txt")); String str; while ((str = in.readLine()) != null) { ls.append(str+"\n"); StringTokenizer st = new StringTokenizer(str); if(st.countTokens()==2){ String st1 = st.nextToken(); String st2 = st.nextToken(); wcetvars.put(st1,st2);//System.out.println("putting:"+st1+","+st2); } } in.close(); } catch (IOException e) { } } catch (LpSolveException e) { System.out.println("LP not solvable for: "+jc.getClassName()+"."+methodbcel.getName()); //e.printStackTrace(); } } return ls.toString(); } // tS tB1 etc. private String toLSO(){ StringBuffer lso = new StringBuffer(); for (Iterator iter = bbs.keySet().iterator(); iter.hasNext();) { Integer keyInt = (Integer) iter.next(); WCETBasicBlock wcbb = (WCETBasicBlock) bbs.get(keyInt); lso.append(wcbb.toLSObj()); if(iter.hasNext()) lso.append(" "); } return lso.toString(); } public String toLinkBBS(){ StringBuffer lsb = new StringBuffer(); int l[] = (int[])WCETBasicBlock.bbl.get(WCETBasicBlock.bcetid); lsb.append("BBs bcet link:"); for (int i=0;i<l.length;i++){ if(l[i]!=-1){ lsb.append(l[i]+"->"); } else break; } lsb.append("T\n"); lsb.append("BBs bcet:"+WCETBasicBlock.bbe[WCETBasicBlock.bcetid]+"\n"); l = (int[])WCETBasicBlock.bbl.get(WCETBasicBlock.wcetid); lsb.append("BBs wcet link:"); for (int i=0;i<l.length;i++){ if(l[i]!=-1){ lsb.append(l[i]+"->"); } else break; } lsb.append("T\n"); lsb.append("BBs wcet:"+WCETBasicBlock.bbe[WCETBasicBlock.wcetid]+"\n"); //lsb.append("BBs bcet:"+WCETBasicBlock.bbe[WCETBasicBlock.bcetid]+"\n"); return lsb.toString(); } public TreeMap getBbs() { return bbs; } public WCETBasicBlock[] getBBSArray(){ WCETBasicBlock[] awcbb = new WCETBasicBlock[bbs.size()]; int i=0; for (Iterator iter = getBbs().keySet().iterator(); iter.hasNext();) { WCETBasicBlock wbb = (WCETBasicBlock) getBbs().get((Integer) iter.next()); awcbb[i] = wbb; i++; } return awcbb; } public WCETBasicBlock getBbs(int bid){ WCETBasicBlock wbb = null; for (Iterator iter = getBbs().keySet().iterator(); iter.hasNext();) { wbb = (WCETBasicBlock) getBbs().get((Integer) iter.next()); if(wbb.bid == bid){ break; } else wbb = null; } return wbb; } public int getN() { return n; } public ConstantPoolGen getCpg() { return cpg; } // valid after call to toLS public StringBuffer getLSO() { return lsobj; } }/** * Basic block of byte codes */class WCETBasicBlock { //public String ids; // id like "S", "T" or "B1" // parent WCETMethodBlock wcmb; WCETMethodBlock invowcmb = null; static ArrayList bbl = new ArrayList(); // bb links static int[] bbe; // execution times static int wcetid; static int bcetid; static WCETBasicBlock[] bba; //S on 0 and T on end // loopcontroller vars boolean innerloop = false; // used both for invo block and lc blocks when applicable ArrayList loopchains; // chains of BB that loop back to the lc // id of the bb int bid = -1; int line = -1; int loopid = -1; boolean loopdriver = false; boolean loopcontroller = false; WCETBasicBlock loopdriverwcbb = null; // is set for loopcontrollers boolean loopreturn = false; boolean leq = false; // loop target int loop = -1; int looptargetid = -1; // the reason why we are doing this... int wcetHit; int wcetMiss; int blockcyc; // additional cycles from a cache miss on invoke int cacheInvokeMiss = -1; // additional cycles from a cache int cacheReturnMiss = -1; // false if we encounter WCETNOTAVAILABLE bytecodes while counting boolean valid; // start pos final int start; final Integer key; InstructionHandle stih; // end pos which will change as splitting happens int end; // end instruction handle InstructionHandle endih; // previous bb WCETBasicBlock prevbb; // sucessor block WCETBasicBlock sucbb; // target block WCETBasicBlock tarbb; // invard links from other BBs called targeters HashMap inbbs; // invoke info after toCodeString has been called String invokeStr; //Strings of method ids String bbinvo; // Walking int sc = -1; // positive if controlled int scsid = -1; //id of source controller int sctid = -1; //id of target controller int tc = -1; int tcsid = -1; int tctid = -1; // T or S boolean s = false; boolean t = false; public final static int SNODE = 1; public final static int BNODE = 2; public final static int INODE = 3; public final static int TNODE = 4; public int nodetype = BNODE; WCETBasicBlock(WCETMethodBlock wcmb, int nodetype){ this.nodetype = nodetype; this.wcmb = wcmb; valid = true; wcetHit =0; wcetMiss =0; start = 0; key = new Integer(-1); inbbs = new HashMap(); } WCETBasicBlock(InstructionHandle stih, InstructionHandle endih, WCETMethodBlock wcmb, int nodetype) { this.wcmb = wcmb; this.nodetype = nodetype; valid = false; wcetHit = -1; wcetMiss = -1; inbbs = new HashMap(); start = stih.getPosition(); key = new Integer(start); end = endih.getPosition(); this.stih = stih; this.endih = endih; } public static void linkbb(WCETBasicBlock S){//System.out.println("About to link:"+S.wcmb.name); //for (int i=0;i<bba.length;i++){// System.out.println("bba["+i+"]"+bba[i].id);//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -