aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/systrace/SystraceOptionsDialogV1.java
blob: b462ada2900041f8cb742a47ededbee065051424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.ddms.systrace;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import java.io.File;

public class SystraceOptionsDialogV1 extends TitleAreaDialog implements ISystraceOptionsDialog {
    private static final String TITLE = "Android System Trace";
    private static final String DEFAULT_MESSAGE =
            "Settings to use while capturing system level trace";
    private static final String DEFAULT_TRACE_FNAME = "trace.html"; //$NON-NLS-1$

    private Text mDestinationText;
    private String mDestinationPath;
    private Text mTraceDurationText;
    private Text mTraceBufferSizeText;

    private static String sSaveToFolder = System.getProperty("user.home"); //$NON-NLS-1$
    private static String sTraceDuration = "";
    private static String sTraceBufferSize = "";

    private Button mTraceCpuFreqBtn;
    private Button mTraceCpuIdleBtn;
    private Button mTraceCpuLoadBtn;
    private Button mTraceDiskIoBtn;
    private Button mTraceKernelWorkqueuesBtn;
    private Button mTraceCpuSchedulerBtn;

    private static boolean sTraceCpuFreq;
    private static boolean sTraceCpuIdle;
    private static boolean sTraceCpuLoad;
    private static boolean sTraceDiskIo;
    private static boolean sTraceKernelWorkqueues;
    private static boolean sTraceCpuScheduler;

    private Button mGfxTagBtn;
    private Button mInputTagBtn;
    private Button mViewTagBtn;
    private Button mWebViewTagBtn;
    private Button mWmTagBtn;
    private Button mAmTagBtn;
    private Button mSyncTagBtn;
    private Button mAudioTagBtn;
    private Button mVideoTagBtn;
    private Button mCameraTagBtn;

    private static boolean sGfxTag;
    private static boolean sInputTag;
    private static boolean sViewTag;
    private static boolean sWebViewTag;
    private static boolean sWmTag;
    private static boolean sAmTag;
    private static boolean sSyncTag;
    private static boolean sAudioTag;
    private static boolean sVideoTag;
    private static boolean sCameraTag;

    private final SystraceOptions mOptions = new SystraceOptions();

    public SystraceOptionsDialogV1(Shell parentShell) {
        super(parentShell);
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        setTitle(TITLE);
        setMessage(DEFAULT_MESSAGE);

        Composite c = new Composite(parent, SWT.BORDER);
        c.setLayout(new GridLayout(3, false));
        c.setLayoutData(new GridData(GridData.FILL_BOTH));

        Label l = new Label(c, SWT.NONE);
        l.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        l.setText("Destination File: ");

        mDestinationText = new Text(c, SWT.BORDER);
        mDestinationText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        mDestinationText.setText(sSaveToFolder + File.separator + DEFAULT_TRACE_FNAME);

        final Button browse = new Button(c, SWT.NONE);
        browse.setText("Browse...");
        browse.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                String path = openBrowseDialog(browse.getShell());
                if (path != null) mDestinationText.setText(path);
            }
        });

        Label lblTraceDurationseconds = new Label(c, SWT.NONE);
        lblTraceDurationseconds.setLayoutData(
                new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        lblTraceDurationseconds.setText("Trace duration (seconds): ");

        mTraceDurationText = new Text(c, SWT.BORDER);
        mTraceDurationText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
        mTraceDurationText.setText(sTraceDuration);

        Label lblTraceBufferSize = new Label(c, SWT.NONE);
        lblTraceBufferSize.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        lblTraceBufferSize.setText("Trace Buffer Size (kb): ");

        mTraceBufferSizeText = new Text(c, SWT.BORDER);
        mTraceBufferSizeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
        mTraceBufferSizeText.setText(sTraceBufferSize);

        Label separator = new Label(c, SWT.SEPARATOR | SWT.HORIZONTAL);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        separator.setLayoutData(gd);

        Group grpTraceEvents = new Group(c, SWT.BORDER);
        grpTraceEvents.setLayout(new GridLayout(3, false));
        grpTraceEvents.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 2, 1));
        grpTraceEvents.setText("Trace Events");

        mTraceCpuFreqBtn = new Button(grpTraceEvents, SWT.CHECK);
        mTraceCpuFreqBtn.setText("CPU Frequency Changes");
        mTraceCpuFreqBtn.setSelection(sTraceCpuFreq);

        mTraceCpuIdleBtn = new Button(grpTraceEvents, SWT.CHECK);
        mTraceCpuIdleBtn.setText("CPU Idle Events");
        mTraceCpuIdleBtn.setSelection(sTraceCpuIdle);

        mTraceCpuLoadBtn = new Button(grpTraceEvents, SWT.CHECK);
        mTraceCpuLoadBtn.setText("CPU Load");
        mTraceCpuLoadBtn.setSelection(sTraceCpuLoad);

        mTraceCpuSchedulerBtn = new Button(grpTraceEvents, SWT.CHECK);
        mTraceCpuSchedulerBtn.setText("CPU Scheduler");
        mTraceCpuSchedulerBtn.setSelection(sTraceCpuScheduler);

        Group grpTraceRootEvents = new Group(c, SWT.BORDER);
        grpTraceRootEvents.setLayout(new GridLayout(2, false));
        grpTraceRootEvents.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 2, 1));
        grpTraceRootEvents.setText("Trace Events that require root privileges on device");

        mTraceDiskIoBtn = new Button(grpTraceRootEvents, SWT.CHECK);
        mTraceDiskIoBtn.setText("Disk I/O");
        mTraceDiskIoBtn.setSelection(sTraceDiskIo);

        mTraceKernelWorkqueuesBtn = new Button(grpTraceRootEvents, SWT.CHECK);
        mTraceKernelWorkqueuesBtn.setText("Kernel Workqueues (requires root)");
        mTraceKernelWorkqueuesBtn.setSelection(sTraceKernelWorkqueues);

        Group grpTraceTags = new Group(c, SWT.BORDER);
        grpTraceTags.setLayout(new GridLayout(5, false));
        grpTraceTags.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1));
        grpTraceTags.setText("Trace Tags");

        mGfxTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mGfxTagBtn.setText("gfx");
        mGfxTagBtn.setSelection(sGfxTag);

        mInputTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mInputTagBtn.setText("input");
        mInputTagBtn.setSelection(sInputTag);

        mViewTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mViewTagBtn.setText("view");
        mViewTagBtn.setSelection(sViewTag);

        mWebViewTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mWebViewTagBtn.setText("webview");
        mWebViewTagBtn.setSelection(sWebViewTag);

        mWmTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mWmTagBtn.setText("wm");
        mWmTagBtn.setSelection(sWmTag);

        mAmTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mAmTagBtn.setText("am");
        mAmTagBtn.setSelection(sAmTag);

        mSyncTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mSyncTagBtn.setText("sync");
        mSyncTagBtn.setSelection(sSyncTag);

        mAudioTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mAudioTagBtn.setText("audio");
        mAudioTagBtn.setSelection(sAudioTag);

        mVideoTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mVideoTagBtn.setText("video");
        mVideoTagBtn.setSelection(sVideoTag);

        mCameraTagBtn = new Button(grpTraceTags, SWT.CHECK);
        mCameraTagBtn.setText("camera");
        mCameraTagBtn.setSelection(sCameraTag);

        Label lblTraceTagsWarning = new Label(grpTraceTags, SWT.NONE);
        lblTraceTagsWarning.setText(
                "Changes to trace tags will likely need a restart of the Android framework to take effect:\n"
                + "    $ adb shell stop\n"
                + "    $ adb shell start");
        lblTraceTagsWarning.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 5, 1));

        ModifyListener m = new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                validateFields();
            }
        };

        mDestinationText.addModifyListener(m);
        mTraceBufferSizeText.addModifyListener(m);
        mTraceDurationText.addModifyListener(m);

        return c;
    }

    private void validateFields() {
        // validate trace destination path
        String msg = validatePath(mDestinationText.getText());
        if (msg != null) {
            setErrorMessage(msg);
            getButton(OK).setEnabled(false);
            return;
        }

        // validate the trace duration
        if (!validateInteger(mTraceDurationText.getText())) {
            setErrorMessage("Trace Duration should be a valid integer (seconds)");
            getButton(OK).setEnabled(false);
            return;
        }

        // validate the trace buffer size
        if (!validateInteger(mTraceBufferSizeText.getText())) {
            setErrorMessage("Trace Buffer Size should be a valid integer (kilobytes)");
            getButton(OK).setEnabled(false);
            return;
        }

        getButton(OK).setEnabled(true);
        setErrorMessage(null);
    }

    private boolean validateInteger(String text) {
        if (text == null || text.isEmpty()) {
            return true;
        }

        try {
            Integer.parseInt(text);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    private String validatePath(String path) {
        if (path == null || path.isEmpty()) {
            return null;
        }

        File f = new File(path);
        if (f.isDirectory()) {
            return String.format("The path '%s' points to a folder", path);
        }

        if (!f.exists()) { // if such a file doesn't exist, make sure the parent folder is valid
            if (!f.getParentFile().isDirectory()) {
                return String.format("That path '%s' is not a valid folder.", f.getParent());
            }
        }

        return null;
    }

    private String openBrowseDialog(Shell parentShell) {
        FileDialog fd = new FileDialog(parentShell, SWT.SAVE);

        fd.setText("Save To");
        fd.setFileName(DEFAULT_TRACE_FNAME);

        fd.setFilterPath(sSaveToFolder);
        fd.setFilterExtensions(new String[] { "*.html" }); //$NON-NLS-1$

        String fname = fd.open();
        if (fname == null || fname.trim().length() == 0) {
            return null;
        }

        sSaveToFolder = fd.getFilterPath();
        return fname;
    }

    @Override
    protected void okPressed() {
        mDestinationPath = mDestinationText.getText().trim();

        sTraceDuration = mTraceDurationText.getText();
        if (!sTraceDuration.isEmpty()) {
            mOptions.mTraceDuration = Integer.parseInt(sTraceDuration);
        }

        sTraceBufferSize = mTraceBufferSizeText.getText();
        if (!sTraceBufferSize.isEmpty()) {
            mOptions.mTraceBufferSize = Integer.parseInt(sTraceBufferSize);
        }

        mOptions.mTraceCpuFreq = mTraceCpuFreqBtn.getSelection();
        mOptions.mTraceCpuIdle = mTraceCpuIdleBtn.getSelection();
        mOptions.mTraceCpuLoad = mTraceCpuLoadBtn.getSelection();
        mOptions.mTraceDiskIo = mTraceDiskIoBtn.getSelection();
        mOptions.mTraceKernelWorkqueues = mTraceKernelWorkqueuesBtn.getSelection();
        mOptions.mTraceCpuScheduler = mTraceCpuSchedulerBtn.getSelection();

        if (mGfxTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_GFX);
        if (mInputTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_INPUT);
        if (mViewTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_VIEW);
        if (mWebViewTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_WEBVIEW);
        if (mWmTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_WM);
        if (mAmTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_AM);
        if (mSyncTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_SYNC);
        if (mAudioTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_AUDIO);
        if (mVideoTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_VIDEO);
        if (mCameraTagBtn.getSelection()) mOptions.enableTag(SystraceOptions.TAG_CAMERA);

        // save current selections to be restored if the dialog is invoked again
        sTraceCpuFreq = mTraceCpuFreqBtn.getSelection();
        sTraceCpuIdle = mTraceCpuIdleBtn.getSelection();
        sTraceCpuLoad = mTraceCpuLoadBtn.getSelection();
        sTraceDiskIo = mTraceDiskIoBtn.getSelection();
        sTraceKernelWorkqueues = mTraceKernelWorkqueuesBtn.getSelection();
        sTraceCpuScheduler = mTraceCpuSchedulerBtn.getSelection();

        sGfxTag = mGfxTagBtn.getSelection();
        sInputTag = mInputTagBtn.getSelection();
        sViewTag = mViewTagBtn.getSelection();
        sWebViewTag = mWebViewTagBtn.getSelection();
        sWmTag = mWmTagBtn.getSelection();
        sAmTag = mAmTagBtn.getSelection();
        sSyncTag = mSyncTagBtn.getSelection();
        sAudioTag = mAudioTagBtn.getSelection();
        sVideoTag = mVideoTagBtn.getSelection();
        sCameraTag = mCameraTagBtn.getSelection();

        super.okPressed();
    }

    @Override
    public SystraceOptions getSystraceOptions() {
        return mOptions;
    }

    @Override
    public String getTraceFilePath() {
        return mDestinationPath;
    }

    private class SystraceOptions implements ISystraceOptions {
        // This list is based on the tags in frameworks/native/include/utils/Trace.h
        private static final int TAG_GFX = 1 << 1;
        private static final int TAG_INPUT = 1 << 2;
        private static final int TAG_VIEW = 1 << 3;
        private static final int TAG_WEBVIEW = 1 << 4;
        private static final int TAG_WM = 1 << 5;
        private static final int TAG_AM = 1 << 6;
        private static final int TAG_SYNC = 1 << 7;
        private static final int TAG_AUDIO = 1 << 8;
        private static final int TAG_VIDEO = 1 << 9;
        private static final int TAG_CAMERA = 1 << 10;

        private int mTraceBufferSize;
        private int mTraceDuration;

        private boolean mTraceCpuFreq;
        private boolean mTraceCpuIdle;
        private boolean mTraceCpuLoad;
        private boolean mTraceDiskIo;
        private boolean mTraceKernelWorkqueues;
        private boolean mTraceCpuScheduler;

        private int mTag;

        private void enableTag(int tag) {
            mTag |= tag;
        }

        @Override
        public String getTags() {
            return mTag == 0 ? null : "0x" + Integer.toHexString(mTag);
        }

        @Override
        public String getOptions() {
            StringBuilder sb = new StringBuilder(20);

            if (mTraceCpuFreq) sb.append("-f "); //$NON-NLS-1$
            if (mTraceCpuIdle) sb.append("-i "); //$NON-NLS-1$
            if (mTraceCpuLoad) sb.append("-l "); //$NON-NLS-1$
            if (mTraceDiskIo) sb.append("-d ");  //$NON-NLS-1$
            if (mTraceKernelWorkqueues) sb.append("-w "); //$NON-NLS-1$
            if (mTraceCpuScheduler) sb.append("-s "); //$NON-NLS-1$

            if (mTraceDuration > 0) {
                sb.append("-t");    //$NON-NLS-1$
                sb.append(mTraceDuration);
                sb.append(' ');
            }

            if (mTraceBufferSize > 0) {
                sb.append("-b ");	//$NON-NLS-1$
                sb.append(mTraceBufferSize);
                sb.append(' ');
            }

            return sb.toString().trim();
        }
    }
}