OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / traceview / src / com / android / traceview / MainWindow.java
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.traceview;
18
19 import com.android.sdkstats.SdkStatsService;
20
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;
31
32 import java.io.File;
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;
40
41 public class MainWindow extends ApplicationWindow {
42
43     private final static String PING_NAME = "Traceview";
44     private final static String PING_VERSION = "1.0";
45
46     private TraceReader mReader;
47     private String mTraceName;
48
49     // A global cache of string names.
50     public static HashMap<String, String> sStringCache = new HashMap<String, String>();
51
52     public MainWindow(String traceName, TraceReader reader) {
53         super(null);
54         mReader = reader;
55         mTraceName = traceName;
56     }
57
58     public void run() {
59         setBlockOnOpen(true);
60         open();
61         Display.getCurrent().dispose();
62     }
63
64     @Override
65     protected void configureShell(Shell shell) {
66         super.configureShell(shell);
67         shell.setText("Traceview: " + mTraceName);
68         shell.setBounds(100, 10, 1282, 900);
69     }
70
71     @Override
72     protected Control createContents(Composite parent) {
73         ColorController.assignMethodColors(parent.getDisplay(), mReader.getMethods());
74         SelectionController selectionController = new SelectionController();
75
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);
82
83         Display display = parent.getDisplay();
84         Color darkGray = display.getSystemColor(SWT.COLOR_DARK_GRAY);
85
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);
93
94         // Create the timeline view
95         new TimeLineView(sashForm1, mReader, selectionController);
96
97         // Create the profile view
98         new ProfileView(sashForm1, mReader, selectionController);
99         return sashForm1;
100     }
101
102     /**
103      * Convert the old two-file format into the current concatenated one.
104      *
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
108      */
109     private static String makeTempTraceFile(String base) throws IOException {
110         // Make a temporary file that will go away on exit and prepare to
111         // write into it.
112         File temp = File.createTempFile(base, ".trace");
113         temp.deleteOnExit();
114         FileChannel dstChannel = new FileOutputStream(temp).getChannel();
115
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());
119         srcChannel.close();
120
121         // Then concatenate the data file.
122         srcChannel = new FileInputStream(base + ".data").getChannel();
123         dstChannel.transferFrom(srcChannel, size, srcChannel.size());
124
125         // Clean up.
126         srcChannel.close();
127         dstChannel.close();
128
129         // Return the path of the temp file.
130         return temp.getPath();
131     }
132
133     /**
134      * Returns the tools revision number.
135      */
136     private static String getRevision() {
137         Properties p = new Properties();
138         try{
139             String toolsdir = System.getProperty("com.android.traceview.toolsdir"); //$NON-NLS-1$
140             File sourceProp;
141             if (toolsdir == null || toolsdir.length() == 0) {
142                 sourceProp = new File("source.properties"); //$NON-NLS-1$
143             } else {
144                 sourceProp = new File(toolsdir, "source.properties"); //$NON-NLS-1$
145             }
146             p.load(new FileInputStream(sourceProp));
147             String revision = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
148             if (revision != null && revision.length() > 0) {
149                 return revision;
150             }
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.
155         }
156
157         return null;
158     }
159
160
161     public static void main(String[] args) {
162         TraceReader reader = null;
163         boolean regression = false;
164
165         // ping the usage server
166
167         String revision = getRevision();
168         if (revision != null) {
169             SdkStatsService.ping(PING_NAME, revision, null);
170         }
171
172         // Process command line arguments
173         int argc = 0;
174         int len = args.length;
175         while (argc < len) {
176             String arg = args[argc];
177             if (arg.charAt(0) != '-') {
178                 break;
179             }
180             if (arg.equals("-r")) {
181                 regression = true;
182             } else {
183                 break;
184             }
185             argc++;
186         }
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");
190             return;
191         }
192
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");
197             System.exit(1);
198             // reader = new QtraceReader(traceName);
199         } else {
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()) {
208                     try {
209                         traceName = makeTempTraceFile(traceName);
210                     } catch (IOException e) {
211                         System.err.printf("cannot convert old trace file '%s'\n", traceName);
212                         System.exit(1);
213                     }
214                 // Otherwise, give up.
215                 } else {
216                     System.err.printf("trace file '%s' not found\n", traceName);
217                     System.exit(1);
218                 }
219             }
220
221             reader = new DmTraceReader(traceName, regression);
222         }
223         reader.getTraceUnits().setTimeScale(TraceUnits.TimeScale.MilliSeconds);
224         new MainWindow(traceName, reader).run();
225     }
226 }