Main.java revision f8bdd4e783842577e49f418a0b5962ba49dfdd93
1/*
2 * Copyright (C) 2014 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
17import java.io.File;
18import java.io.IOException;
19import java.lang.reflect.Method;
20
21public class Main {
22    public static void main(String[] args) throws Exception {
23        String name = System.getProperty("java.vm.name");
24        if (!"Dalvik".equals(name)) {
25            System.out.println("This test is not supported on " + name);
26            return;
27        }
28        testMethodTracing();
29    }
30
31    private static void testMethodTracing() throws Exception {
32        String tempFileName;
33        if (new File("/tmp").isDirectory()) {
34            tempFileName = "/tmp/test.trace";
35        } else if (new File("/sdcard").isDirectory()) {
36            tempFileName = "/sdcard/test.trace";
37        } else {
38            System.out.println("Can't find proper output directory for trace file");
39            return;
40        }
41        File tempFile = new File(tempFileName);
42        tempFile.delete();
43
44        System.out.println("Confirm enable/disable");
45        System.out.println("status=" + VMDebug.getMethodTracingMode());
46        VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
47        System.out.println("status=" + VMDebug.getMethodTracingMode());
48        VMDebug.stopMethodTracing();
49        System.out.println("status=" + VMDebug.getMethodTracingMode());
50        if (tempFile.length() == 0) {
51            System.out.println("ERROR: tracing output file is empty");
52        }
53
54        System.out.println("Confirm sampling");
55        VMDebug.startMethodTracing(tempFileName, 0, 0, true, 1000);
56        System.out.println("status=" + VMDebug.getMethodTracingMode());
57        VMDebug.stopMethodTracing();
58        System.out.println("status=" + VMDebug.getMethodTracingMode());
59        if (tempFile.length() == 0) {
60            System.out.println("ERROR: sample tracing output file is empty");
61        }
62
63        System.out.println("Test starting when already started");
64        VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
65        System.out.println("status=" + VMDebug.getMethodTracingMode());
66        VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
67        System.out.println("status=" + VMDebug.getMethodTracingMode());
68
69        System.out.println("Test stopping when already stopped");
70        VMDebug.stopMethodTracing();
71        System.out.println("status=" + VMDebug.getMethodTracingMode());
72        VMDebug.stopMethodTracing();
73        System.out.println("status=" + VMDebug.getMethodTracingMode());
74
75        System.out.println("Test tracing with empty filename");
76        try {
77            VMDebug.startMethodTracing("", 0, 0, false, 0);
78            System.out.println("Should have thrown an exception");
79        } catch (Exception e) {
80            System.out.println("Got expected exception");
81        }
82
83        System.out.println("Test tracing with bogus (< 1024 && != 0) filesize");
84        try {
85            VMDebug.startMethodTracing(tempFileName, 1000, 0, false, 0);
86            System.out.println("Should have thrown an exception");
87        } catch (Exception e) {
88            System.out.println("Got expected exception");
89        }
90
91        System.out.println("Test sampling with bogus (<= 0) interval");
92        try {
93            VMDebug.startMethodTracing(tempFileName, 0, 0, true, 0);
94            System.out.println("Should have thrown an exception");
95        } catch (Exception e) {
96            System.out.println("Got expected exception");
97        }
98
99        tempFile.delete();
100    }
101
102    private static class VMDebug {
103        private static final Method startMethodTracingMethod;
104        private static final Method stopMethodTracingMethod;
105        private static final Method getMethodTracingModeMethod;
106        static {
107            try {
108                Class c = Class.forName("dalvik.system.VMDebug");
109                startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
110                        Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE);
111                stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
112                getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
113            } catch (Exception e) {
114                throw new RuntimeException(e);
115            }
116        }
117
118        public static void startMethodTracing(String filename, int bufferSize, int flags,
119                boolean samplingEnabled, int intervalUs) throws Exception {
120            startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled,
121                    intervalUs);
122        }
123        public static void stopMethodTracing() throws Exception {
124            stopMethodTracingMethod.invoke(null);
125        }
126        public static int getMethodTracingMode() throws Exception {
127            return (int) getMethodTracingModeMethod.invoke(null);
128        }
129    }
130}
131