Skip to content

Commit 280303e

Browse files
committed
initial commit
0 parents  commit 280303e

16 files changed

+255
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
out/
2+
/.idea/workspace.xml

.idea/artifacts/mtr_jar.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/lib.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/common-25.3.2.jar

117 KB
Binary file not shown.

lib/commons-cli-1.4.jar

52.6 KB
Binary file not shown.

lib/ddmlib-25.3.2.jar

313 KB
Binary file not shown.

lib/ddms-25.3.2.jar

348 Bytes
Binary file not shown.

lib/ddmuilib-25.3.2.jar

578 KB
Binary file not shown.

lib/guava-18.0.jar

2.15 MB
Binary file not shown.

mtr.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" exported="" name="lib" level="project" />
11+
</component>
12+
</module>

src/META-INF/MANIFEST.MF

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: MethodTraceRunner
3+

src/MethodTraceRunner.java

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
import com.android.ddmlib.*;
2+
import org.apache.commons.cli.*;
3+
4+
import java.io.BufferedOutputStream;
5+
import java.io.File;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.util.Arrays;
9+
import java.util.concurrent.TimeUnit;
10+
11+
/**
12+
* Created by likaci on 29/09/2017
13+
*/
14+
public class MethodTraceRunner {
15+
private static final String TAG = "MethodTraceRunner";
16+
private static String packageName;
17+
private static String outputFile;
18+
private static int traceDuration;
19+
20+
public static void main(String[] args) throws IOException {
21+
parseArgs(args);
22+
23+
//bridge
24+
AndroidDebugBridge.init(true);
25+
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();
26+
27+
//device
28+
waitForDevice(bridge);
29+
IDevice[] devices = bridge.getDevices();
30+
if (devices.length > 1) {
31+
exitWithError("more than one device");
32+
} else if (devices.length == 0) {
33+
exitWithError("no device found");
34+
}
35+
IDevice device = devices[0];
36+
Log.d(TAG, "device: " + device);
37+
38+
//client
39+
waitForClient(device, packageName);
40+
Client client = device.getClient(packageName);
41+
Log.d(TAG, "client: " + client);
42+
43+
ClientData.setMethodProfilingHandler(new ClientData.IMethodProfilingHandler() {
44+
@Override
45+
public void onSuccess(String s, Client client) {
46+
Log.d(TAG, "onSuccess: " + s + " " + client);
47+
}
48+
49+
@Override
50+
public void onSuccess(byte[] bytes, Client client) {
51+
Log.d(TAG, "onSuccess: " + client);
52+
53+
BufferedOutputStream bs = null;
54+
55+
try {
56+
FileOutputStream fs = new FileOutputStream(new File(outputFile));
57+
bs = new BufferedOutputStream(fs);
58+
bs.write(bytes);
59+
bs.close();
60+
bs = null;
61+
} catch (Exception e) {
62+
e.printStackTrace();
63+
}
64+
65+
if (bs != null) try {
66+
bs.close();
67+
} catch (Exception ignored) {
68+
}
69+
70+
Log.d(TAG, "trace file: " + outputFile);
71+
System.out.println("trace file saved at " + System.getProperty("user.dir") + File.separator + outputFile);
72+
System.exit(0);
73+
}
74+
75+
@Override
76+
public void onStartFailure(Client client, String s) {
77+
exitWithError("onStartFailure: " + client + " " + s);
78+
}
79+
80+
@Override
81+
public void onEndFailure(Client client, String s) {
82+
exitWithError("onEndFailure: " + client + " " + s);
83+
}
84+
85+
});
86+
87+
System.out.printf("will profile %s, device: %s, client: %s, for %d seconds%n", packageName, device, client, traceDuration);
88+
Log.d(TAG, "start Sampling Profiler");
89+
client.startSamplingProfiler(10, TimeUnit.MILLISECONDS);
90+
91+
try {
92+
Log.d(TAG, "wait " + traceDuration + " seconds");
93+
Thread.sleep(traceDuration * 1000);
94+
} catch (InterruptedException e) {
95+
e.printStackTrace();
96+
}
97+
98+
Log.d(TAG, "stop Sampling Profiler");
99+
client.stopSamplingProfiler();
100+
}
101+
102+
private static void parseArgs(String[] args) {
103+
Log.d(TAG, "args: " + Arrays.toString(args));
104+
105+
Options options = new Options();
106+
options.addOption("h", false, "show help msg");
107+
options.addOption("v", false, "verbose log");
108+
options.addOption("p", true, "package name");
109+
options.addOption("o", true, "output file");
110+
options.addOption("t", true, "trace time");
111+
112+
try {
113+
CommandLine cmd = new DefaultParser().parse(options, args);
114+
115+
if (cmd.hasOption("h")) {
116+
printHelpMsg(options);
117+
System.exit(0);
118+
}
119+
120+
if (cmd.hasOption("v")) {
121+
DdmPreferences.setLogLevel(Log.LogLevel.DEBUG.getStringValue());
122+
}
123+
124+
packageName = cmd.getOptionValue("p", "");
125+
if (packageName.isEmpty()) {
126+
exitWithError("PackageName not specified, use -p arg to set");
127+
}
128+
Log.i(TAG, "packageName: " + packageName);
129+
130+
outputFile = cmd.getOptionValue("o", "t.trace");
131+
Log.i(TAG, "outputFile: " + outputFile);
132+
133+
traceDuration = Integer.parseInt(cmd.getOptionValue("t", "5"));
134+
Log.d(TAG, "traceDuration: " + traceDuration);
135+
136+
} catch (Exception e) {
137+
printHelpMsg(options);
138+
exitWithError(e);
139+
}
140+
141+
}
142+
143+
private static void printHelpMsg(Options options) {
144+
new HelpFormatter().printHelp("mtr", options);
145+
}
146+
147+
private static void waitForDevice(AndroidDebugBridge bridge) {
148+
int count = 0;
149+
while (!bridge.hasInitialDeviceList()) {
150+
try {
151+
Thread.sleep(100);
152+
count++;
153+
} catch (InterruptedException ignored) {
154+
}
155+
if (count > 100) {
156+
exitWithError("wait for device time out");
157+
break;
158+
}
159+
}
160+
}
161+
162+
private static void waitForClient(IDevice device, String packageName) {
163+
int count = 0;
164+
while (device.getClient(packageName) == null) {
165+
try {
166+
Thread.sleep(100);
167+
count++;
168+
} catch (InterruptedException ignored) {
169+
}
170+
if (count > 100) {
171+
exitWithError("wait for client time out");
172+
break;
173+
}
174+
}
175+
}
176+
177+
private static void exitWithError(String msg) {
178+
exitWithError(new Exception(msg));
179+
}
180+
181+
private static void exitWithError(Exception exception) {
182+
if (exception != null) {
183+
exception.printStackTrace();
184+
}
185+
System.exit(1);
186+
}
187+
188+
}

0 commit comments

Comments
 (0)