2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.traceview;
19 import com.android.sdkstats.SdkStatsService;
21 import org.eclipse.jface.window.ApplicationWindow;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.custom.SashForm;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Shell;
33 import java.io.FileInputStream;
34 import java.io.FileNotFoundException;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.nio.channels.FileChannel;
38 import java.util.HashMap;
39 import java.util.Properties;
41 public class MainWindow extends ApplicationWindow {
43 private final static String PING_NAME = "Traceview";
44 private final static String PING_VERSION = "1.0";
46 private TraceReader mReader;
47 private String mTraceName;
49 // A global cache of string names.
50 public static HashMap<String, String> sStringCache = new HashMap<String, String>();
52 public MainWindow(String traceName, TraceReader reader) {
55 mTraceName = traceName;
61 Display.getCurrent().dispose();
65 protected void configureShell(Shell shell) {
66 super.configureShell(shell);
67 shell.setText("Traceview: " + mTraceName);
68 shell.setBounds(100, 10, 1282, 900);
72 protected Control createContents(Composite parent) {
73 ColorController.assignMethodColors(parent.getDisplay(), mReader.getMethods());
74 SelectionController selectionController = new SelectionController();
76 GridLayout gridLayout = new GridLayout(1, false);
77 gridLayout.marginWidth = 0;
78 gridLayout.marginHeight = 0;
79 gridLayout.horizontalSpacing = 0;
80 gridLayout.verticalSpacing = 0;
81 parent.setLayout(gridLayout);
83 Display display = parent.getDisplay();
84 Color darkGray = display.getSystemColor(SWT.COLOR_DARK_GRAY);
86 // Create a sash form to separate the timeline view (on top)
87 // and the profile view (on bottom)
88 SashForm sashForm1 = new SashForm(parent, SWT.VERTICAL);
89 sashForm1.setBackground(darkGray);
90 sashForm1.SASH_WIDTH = 3;
91 GridData data = new GridData(GridData.FILL_BOTH);
92 sashForm1.setLayoutData(data);
94 // Create the timeline view
95 new TimeLineView(sashForm1, mReader, selectionController);
97 // Create the profile view
98 new ProfileView(sashForm1, mReader, selectionController);
103 * Convert the old two-file format into the current concatenated one.
105 * @param base Base path of the two files, i.e. base.key and base.data
106 * @return Path to a temporary file that will be deleted on exit.
107 * @throws IOException
109 private static String makeTempTraceFile(String base) throws IOException {
110 // Make a temporary file that will go away on exit and prepare to
112 File temp = File.createTempFile(base, ".trace");
114 FileChannel dstChannel = new FileOutputStream(temp).getChannel();
116 // First copy the contents of the key file into our temp file.
117 FileChannel srcChannel = new FileInputStream(base + ".key").getChannel();
118 long size = dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
121 // Then concatenate the data file.
122 srcChannel = new FileInputStream(base + ".data").getChannel();
123 dstChannel.transferFrom(srcChannel, size, srcChannel.size());
129 // Return the path of the temp file.
130 return temp.getPath();
134 * Returns the tools revision number.
136 private static String getRevision() {
137 Properties p = new Properties();
139 String toolsdir = System.getProperty("com.android.traceview.toolsdir"); //$NON-NLS-1$
141 if (toolsdir == null || toolsdir.length() == 0) {
142 sourceProp = new File("source.properties"); //$NON-NLS-1$
144 sourceProp = new File(toolsdir, "source.properties"); //$NON-NLS-1$
146 p.load(new FileInputStream(sourceProp));
147 String revision = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
148 if (revision != null && revision.length() > 0) {
151 } catch (FileNotFoundException e) {
152 // couldn't find the file? don't ping.
153 } catch (IOException e) {
154 // couldn't find the file? don't ping.
161 public static void main(String[] args) {
162 TraceReader reader = null;
163 boolean regression = false;
165 // ping the usage server
167 String revision = getRevision();
168 if (revision != null) {
169 SdkStatsService.ping(PING_NAME, revision, null);
172 // Process command line arguments
174 int len = args.length;
176 String arg = args[argc];
177 if (arg.charAt(0) != '-') {
180 if (arg.equals("-r")) {
187 if (argc != len - 1) {
188 System.out.printf("Usage: java %s [-r] trace%n", MainWindow.class.getName());
189 System.out.printf(" -r regression only%n");
193 String traceName = args[len - 1];
194 File file = new File(traceName);
195 if (file.exists() && file.isDirectory()) {
196 System.out.printf("Qemu trace files not supported yet.\n");
198 // reader = new QtraceReader(traceName);
200 // If the filename as given doesn't exist...
201 if (!file.exists()) {
202 // Try appending .trace.
203 if (new File(traceName + ".trace").exists()) {
204 traceName = traceName + ".trace";
205 // Next, see if it is the old two-file trace.
206 } else if (new File(traceName + ".data").exists()
207 && new File(traceName + ".key").exists()) {
209 traceName = makeTempTraceFile(traceName);
210 } catch (IOException e) {
211 System.err.printf("cannot convert old trace file '%s'\n", traceName);
214 // Otherwise, give up.
216 System.err.printf("trace file '%s' not found\n", traceName);
221 reader = new DmTraceReader(traceName, regression);
223 reader.getTraceUnits().setTimeScale(TraceUnits.TimeScale.MilliSeconds);
224 new MainWindow(traceName, reader).run();