package com.macmillan.nmeyers; import java.util.*; import javax.swing.tree.*; import java.text.*; /* * LineInclusive.java: Part of the PerfAnal tool * * This class encapsulates a tree, consisting of DefaultMutableTreeNodes, * of performance data by procedure, broken down by line number, inclusive * of called procedures. * * Author: Nathan Meyers, nmeyers@javalinux.net * $Id: LineInclusive.java,v 1.9 1999/11/10 03:36:14 nathanm Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * with this program. If not, the license is available from the * GNU project, at http://www.gnu.org. */ class LineInclusive extends PerfTree.PerfTreeNode implements Comparable { LineInclusive(HashMap procedures, int totalCount) { super(new ProcCountInfo("", totalCount)); // Build a list of procedures, sorted by usage Iterator iterator = procedures.values().iterator(); while (iterator.hasNext()) { Procedure procedure = (Procedure)iterator.next(); ProcCountInfo thisProcInfo = new ProcCountInfo(procedure.procName, 0); LineInclusive li = new LineInclusive(thisProcInfo); add(li); Iterator lines = procedure.lines.iterator(); ProcCountInfo prev = null; while (lines.hasNext()) { Procedure.Line line = (Procedure.Line)lines.next(); if (line.thread != null && !line.thread.enabled) continue; thisProcInfo.count += line.countInclusive; if (prev != null && prev.procName.equals(line.lineInfo)) prev.count += line.countInclusive; else li.add(new LineInclusive(prev = new ProcCountInfo(line.lineInfo, line.countInclusive))); } if (li.children != null) Collections.sort(li.children); } if (children != null) Collections.sort(children); } TreePath findThisProc(String procName) { // Linear search for child with given name Iterator iterator = children.iterator(); while (iterator.hasNext()) { LineInclusive child = (LineInclusive)iterator.next(); ProcCountInfo pci = (ProcCountInfo)child.getUserObject(); if (pci.procName.equals(procName)) return new TreePath(child.getPath()); } return null; } private LineInclusive(ProcCountInfo pci) { super(pci); } public int compareTo(Object o) { return ((ProcCountInfo)getUserObject()).compareTo( ((PerfTree.PerfTreeNode)o).getUserObject()); } public String toString() { ProcCountInfo rootPci = (ProcCountInfo) ((PerfTree.PerfTreeNode)getRoot()).getUserObject(); if (getLevel() == 0) return "Method Times by Line Number (times inclusive): " + rootPci.count + " ticks"; ProcCountInfo pci = (ProcCountInfo)getUserObject(); return pci.toString(rootPci.count); } public String getProcName() { if (getLevel() > 1) return ((PerfTree.PerfTreeNode)getParent()).getProcName(); return ((ProcCountInfo)getUserObject()).procName; } static class ProcCountInfo implements Comparable { String procName; int count; static DecimalFormat format = new DecimalFormat("##0.##%"); ProcCountInfo(String p, int c) { procName = p; count = c; } public String toString(int totalCount) { return procName + ": " + format.format((double)count / (double)totalCount) + " (" + count + " inclusive)"; } public int compareTo(Object o) { return ((ProcCountInfo)o).count - count; } public boolean equals(Object o) { return count == ((ProcCountInfo)o).count; } } }