1# fieldcount
2
3fieldcount is a JVMTI agent designed to investigate the types being held by specific fields and
4how large the objects referenced by these fields are.
5
6Note that just by using the agent some fields might be written (for example fields related to
7keeping track of jfieldIDs). Users should be aware of this.
8
9# Usage
10### Build
11>    `m libfieldcount libfieldcounts`
12
13The libraries will be built for 32-bit, 64-bit, host and target. Below examples
14assume you want to use the 64-bit version.
15
16### Command Line
17
18The agent is loaded using -agentpath like normal. It takes arguments in the
19following format:
20>     `Lname/of/class;.nameOfField:Ltype/of/field;[,...]`
21
22#### ART
23```shell
24art -Xplugin:$ANDROID_HOST_OUT/lib64/libopenjdkjvmti.so '-agentpath:libfieldcount.so=Ljava/lang/Class;.extData:Ldalvik/system/ClassExt;,Ldalvik/system/ClassExt;.jmethodIDs:Ljava/lang/Object;' -cp tmp/java/helloworld.dex -Xint helloworld
25```
26
27* `-Xplugin` and `-agentpath` need to be used, otherwise the agent will fail during init.
28* If using `libartd.so`, make sure to use the debug version of jvmti.
29
30```shell
31adb shell setenforce 0
32
33adb push $ANDROID_PRODUCT_OUT/system/lib64/libfieldcounts.so /data/local/tmp/
34
35adb shell am start-activity --attach-agent '/data/local/tmp/libfieldcounts.so=Ljava/lang/Class;.extData:Ldalvik/system/ClassExt;,Ldalvik/system/ClassExt;.jmethodIDs:Ljava/lang/Object;' some.debuggable.apps/.the.app.MainActivity
36```
37
38#### RI
39>    `java '-agentpath:libfieldcount.so=Lname/of/class;.nameOfField:Ltype/of/field;' -cp tmp/helloworld/classes helloworld`
40
41### Printing the Results
42All statistics gathered during the trace are printed automatically when the
43program normally exits. In the case of Android applications, they are always
44killed, so we need to manually print the results.
45
46>    `kill -SIGQUIT $(pid com.littleinc.orm_benchmark)`
47
48Will initiate a dump of the counts (to logcat).
49
50The dump will look something like this.
51
52```
53dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:60] listing field Ljava/lang/Class;.extData:Ldalvik/system/ClassExt;
54dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:60] listing field Ldalvik/system/ClassExt;.jmethodIDs:Ljava/lang/Object;
55Hello, world!
56dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:97] Dumping counts of fields.
57dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:98]     Field name      Type    Count   Total Size
58dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:155]    Ljava/lang/Class;.extData:Ldalvik/system/ClassExt;      <ALL TYPES>     2800    3024
59dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:161]    Ljava/lang/Class;.extData:Ldalvik/system/ClassExt;      Ldalvik/system/ClassExt;        64      3024
60dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:161]    Ljava/lang/Class;.extData:Ldalvik/system/ClassExt;      <null>  2738    0
61dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:155]    Ldalvik/system/ClassExt;.jmethodIDs:Ljava/lang/Object;  <ALL TYPES>     63      10008
62dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:161]    Ldalvik/system/ClassExt;.jmethodIDs:Ljava/lang/Object;  <null>  26      0
63dalvikvm64 I 06-27 14:24:59 183155 183155 fieldcount.cc:161]    Ldalvik/system/ClassExt;.jmethodIDs:Ljava/lang/Object;  [J      39      10008
64```
65