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