/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.threadlog.ui; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JToolBar; import javax.swing.filechooser.FileNameExtensionFilter; import org.simantics.threadlog.Task; import org.simantics.threadlog.ThreadLog; import gnu.trove.map.hash.TLongObjectHashMap; import gnu.trove.procedure.TObjectProcedure; public class ThreadLogVisualizer extends JFrame { private static final long serialVersionUID = 6250996509358338304L; TimeLineViewer viewer = new TimeLineViewer(); JToolBar toolbar = new JToolBar("Thread Log Visualizer Tools"); JButton saveButton = new JButton(new SaveAction()); ThreadLog currentLog; public ThreadLogVisualizer() { super("Thread log visualizer"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setSize(800, 600); getContentPane().setLayout(new BorderLayout()); getContentPane().add(toolbar, BorderLayout.NORTH); getContentPane().add(viewer, BorderLayout.CENTER); toolbar.add(saveButton); } class SaveAction extends AbstractAction { private static final long serialVersionUID = 1L; public SaveAction() { super("Save"); putValue(SHORT_DESCRIPTION, "Save the Current Log"); putValue(MNEMONIC_KEY, KeyEvent.VK_S); } @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "Thread Logs (*.tlog)", "tlog", "tlog"); chooser.setFileFilter(filter); int returnVal = chooser.showSaveDialog(ThreadLogVisualizer.this); if (returnVal != JFileChooser.APPROVE_OPTION) return; try { currentLog.serialize(chooser.getSelectedFile()); } catch (IOException ex) { ex.printStackTrace(); } } } public void setLog(ThreadLog log) { this.currentLog = log; String[] tasks = log.getTasks(); double[] times = log.getTimes(); // Relativize to the first task double minTime = Double.POSITIVE_INFINITY; double maxTime = Double.NEGATIVE_INFINITY; for(int i=0;i maxTime) maxTime = temp; } for(int i=0;i> intervals = new TLongObjectHashMap>(); long[] threads = log.getThreads(); for(int i=0;i in = intervals.get(thread); if(in == null) { in = new ArrayList(); intervals.put(thread, in); } in.add(new Interval(tasks[i], times[i*2], times[i*2+1])); } // Create lanes viewer.clear(); intervals.forEachValue(new TObjectProcedure>() { @Override public boolean execute(ArrayList intervals) { Collections.sort(intervals); ArrayList lanes = new ArrayList(); int curLaneId = -1; Lane curLane = null; for(Interval in : intervals) { if(curLane == null || in.begin < curLane.getEnd()) { ++curLaneId; if(curLaneId < lanes.size()) curLane = lanes.get(curLaneId); else { curLane = new Lane(); lanes.add(curLane); } } else { while(curLaneId > 0) { Lane prevLane = lanes.get(curLaneId-1); if(prevLane.getEnd() > in.begin) break; --curLaneId; curLane = prevLane; } } curLane.addInterval(in); } for(Lane lane : lanes) viewer.addLane(lane); return true; } }); // update viewer viewer.repaint(); } public void saveImage() { } public static void main(String[] args) throws Exception { ThreadLog.setDefaultThreadLog(new ThreadLog()); { Task A = ThreadLog.BEGIN("A"); Thread.sleep(200); Task B = ThreadLog.BEGIN("B"); Thread.sleep(100); B.end(); Thread.sleep(100); Task C = ThreadLog.BEGIN("C"); Thread.sleep(100); C.end(); A.end(); } ThreadLogVisualizer vis = new ThreadLogVisualizer(); vis.setLog(ThreadLog.setDefaultThreadLog(null)); vis.setVisible(true); } }