1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.threadlog.ui;
14 import java.awt.BorderLayout;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.KeyEvent;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Collections;
21 import javax.swing.AbstractAction;
22 import javax.swing.JButton;
23 import javax.swing.JFileChooser;
24 import javax.swing.JFrame;
25 import javax.swing.JToolBar;
26 import javax.swing.filechooser.FileNameExtensionFilter;
28 import org.simantics.threadlog.Task;
29 import org.simantics.threadlog.ThreadLog;
31 import gnu.trove.map.hash.TLongObjectHashMap;
32 import gnu.trove.procedure.TObjectProcedure;
34 public class ThreadLogVisualizer extends JFrame {
36 private static final long serialVersionUID = 6250996509358338304L;
38 TimeLineViewer viewer = new TimeLineViewer();
39 JToolBar toolbar = new JToolBar("Thread Log Visualizer Tools");
40 JButton saveButton = new JButton(new SaveAction());
44 public ThreadLogVisualizer() {
45 super("Thread log visualizer");
46 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
49 getContentPane().setLayout(new BorderLayout());
50 getContentPane().add(toolbar, BorderLayout.NORTH);
51 getContentPane().add(viewer, BorderLayout.CENTER);
53 toolbar.add(saveButton);
56 class SaveAction extends AbstractAction {
57 private static final long serialVersionUID = 1L;
61 putValue(SHORT_DESCRIPTION, "Save the Current Log");
62 putValue(MNEMONIC_KEY, KeyEvent.VK_S);
66 public void actionPerformed(ActionEvent e) {
67 JFileChooser chooser = new JFileChooser();
68 FileNameExtensionFilter filter = new FileNameExtensionFilter(
69 "Thread Logs (*.tlog)", "tlog", "tlog");
70 chooser.setFileFilter(filter);
71 int returnVal = chooser.showSaveDialog(ThreadLogVisualizer.this);
72 if (returnVal != JFileChooser.APPROVE_OPTION)
76 currentLog.serialize(chooser.getSelectedFile());
77 } catch (IOException ex) {
83 public void setLog(ThreadLog log) {
84 this.currentLog = log;
86 String[] tasks = log.getTasks();
87 double[] times = log.getTimes();
89 // Relativize to the first task
90 double minTime = Double.POSITIVE_INFINITY;
91 double maxTime = Double.NEGATIVE_INFINITY;
93 for(int i=0;i<times.length;i+=2) {
94 double temp = times[i];
102 for(int i=0;i<times.length;++i)
106 // Group intervals by thread
107 TLongObjectHashMap<ArrayList<Interval>> intervals = new TLongObjectHashMap<ArrayList<Interval>>();
108 long[] threads = log.getThreads();
109 for(int i=0;i<tasks.length;++i) {
110 long thread = threads[i];
111 ArrayList<Interval> in = intervals.get(thread);
113 in = new ArrayList<Interval>();
114 intervals.put(thread, in);
116 in.add(new Interval(tasks[i], times[i*2], times[i*2+1]));
121 intervals.forEachValue(new TObjectProcedure<ArrayList<Interval>>() {
124 public boolean execute(ArrayList<Interval> intervals) {
125 Collections.sort(intervals);
126 ArrayList<Lane> lanes = new ArrayList<Lane>();
130 for(Interval in : intervals) {
131 if(curLane == null || in.begin < curLane.getEnd()) {
133 if(curLaneId < lanes.size())
134 curLane = lanes.get(curLaneId);
136 curLane = new Lane();
141 while(curLaneId > 0) {
142 Lane prevLane = lanes.get(curLaneId-1);
143 if(prevLane.getEnd() > in.begin)
149 curLane.addInterval(in);
152 for(Lane lane : lanes)
153 viewer.addLane(lane);
163 public void saveImage() {
167 public static void main(String[] args) throws Exception {
168 ThreadLog.setDefaultThreadLog(new ThreadLog());
171 Task A = ThreadLog.BEGIN("A");
173 Task B = ThreadLog.BEGIN("B");
177 Task C = ThreadLog.BEGIN("C");
183 ThreadLogVisualizer vis = new ThreadLogVisualizer();
184 vis.setLog(ThreadLog.setDefaultThreadLog(null));
185 vis.setVisible(true);