Btrace详细指南(JDK7,监控HashMap扩容)
生活随笔
收集整理的這篇文章主要介紹了
Btrace详细指南(JDK7,监控HashMap扩容)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
背景 ? ??JAVA中如何排查疑難雜癥,如何動態(tài)獲取應(yīng)用信息,我們有BTrace! PS:集團(tuán)有大殺器arthas,這里我們先從最原始最廣泛的BTrace開始,后面可以玩玩Greys(開源,強(qiáng)于BTrace)。 應(yīng)用被tracle后,相關(guān)class會被改變,恢復(fù)需要重新編譯,請自行把握。
一、下載安裝BTtrace wiki:https://github.com/jbachorik/btrace/wiki BTrace博客:http://jbachorik.github.io/btrace/ JIRA(深入必看):https://kenai.com/jira/browse/BTRACE/?selectedTab=com.atlassian.jira.jira-projects-plugin:summary-panel 官網(wǎng):https://kenai.com/projects/btrace/ github(1.3):https://github.com/jbachorik/btrace IDE環(huán)境:引入BTrace相關(guān)Jar,在eclipse/IDEA編寫;使用jvisualvm編寫。 代碼自動生成工具(使用必備):https://btrace.org/btrace/
下載最新版(1.3,JDK7以上):https://github.com/jbachorik/btrace/releases/tag/v1.3 BTrace Maven插件:https://github.com/btraceio/btrace-maven BTrace1.2開發(fā)者說明:https://kenai.com/projects/btrace/pages/DeveloperGuide BTrace1.2 API DOC:https://btrace.kenai.com/javadoc/1.2/index.html
配置環(huán)境變量: BTRACE_HOME:~/user/btrace PATH:$BTRACE_HOME/bin JAVA_VERSION:MAC特有,詳見btrace腳本內(nèi)容,后文有說明。
Maven倉庫(只有1.2.X版本,更高版本請自行編譯發(fā)布到私服):
?<!-- 1.2版本--> <dependency> <groupId>com.sun.tools.btrace</groupId> <artifactId>btrace-agent</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.sun.tools.btrace</groupId> <artifactId>btrace-boot</artifactId> <version>1.2.3</version> </dependency>
注意:應(yīng)用程序端不需要引入BTrace相關(guān)Jar,但為了編寫腳本方便(IDE提示),可以以Provided引入它,不必Complie。
二、BTtrace常用腳本 解壓btrace發(fā)現(xiàn)在%BTRACE_HOME%\bin有三個腳本:btrace、btracec、btracer,我們通過這三個腳本使用BTrace。 中文簡述: btrace:對運(yùn)行中的JAVA程序執(zhí)行btrace腳本。 btracec:編譯btrace腳本。 btracer:讓BTrace隨JAVA應(yīng)用啟動加載。
btracer注意事項(xiàng): 原理:java -javaagent:btrace-agent.jar=[<agent-arg>[,<agent-arg>]*]? <launch-args> 場景:When?no custom agent arguments?are required the?btracer?utility script may be used instead. 完整用法:https://github.com/jbachorik/btrace/wiki/btracer。
英文完整說明: <btrace>/bin/btrace <PID> <trace_script>will attach to the java application with the given PID and compile and submit the trace script <btrace>/bin/btracec <trace_script>will compile the provided trace script <btrace>/bin/btracer <compiled_script><args to launch a java app> will start the specified java application with the btrace agent running and the script previously compiled by btracec loaded
詳細(xì)看看btrace(1.3.4)腳本內(nèi)容:
#! /bin/sh if [ -z "$BTRACE_HOME" -o ! -d "$BTRACE_HOME" ] ; then # resolve links - $0 could be a link to btrace's home PRG="$0" progname=`basename "$0"` BTRACE_HOME=`dirname "$PRG"`/.. BTRACE_HOME=`cd "$BTRACE_HOME" && pwd` fi if [ -f "${BTRACE_HOME}/build/btrace-client.jar" ] ; then if [ "${JAVA_HOME}" != "" ]; then TOOLS_JAR="${JAVA_HOME}/lib/tools.jar" if [ ! -f ${TOOLS_JAR} ] ; then case "`uname`" in Darwin*) # In older JDK versions for?Mac OS X, tools.jar is classes.jar # and is kept in a?different location. Check if we can locate # classes.jar based on ${JAVA_VERSION} TOOLS_JAR="/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Classes/classes.jar" # if we can't find, try?relative?path from ${JAVA_HOME}. Usually, # /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home # is JAVA_HOME. (or whatever version beyond 1.6.0!) if [ ! -f ${TOOLS_JAR} ] ; then TOOLS_JAR="${JAVA_HOME} /../Classes/classes.jar" fi # If we still can't find, tell the user to set JAVA_VERSION. # This way, we can avoid zip file errors from the agent side # and "connection refused" message from client. if [ ! -f ${TOOLS_JAR} ] ; then echo "Please set JAVA_VERSION to the target java version" exit 1 fi ;; esac fi ${JAVA_HOME}/bin/java -cp ${BTRACE_HOME}/build/btrace-client.jar:${TOOLS_JAR}:/usr/share/lib/java/dtrace.jar com.sun.btrace.client.Main $* else echo "Please set JAVA_HOME before running this script" exit 1 fi else echo "Please set BTRACE_HOME before running this script" exit 1 fi
MAC 版本如果遇到錯誤,請留意%JAVA_VERSION%這個環(huán)境變量,原因見上面btrace腳本源碼。 【${JAVA_HOME}/bin/java -cp ${BTRACE_HOME}/build/btrace-client.jar:${TOOLS_JAR}:/usr/share/lib/java/dtrace.jar?com.sun.btrace.client.Main?$*】btrace啟動核心靠它!。 三、使用示例(監(jiān)控HashMap擴(kuò)容時的上下文情況) 簡要說明: table:哈希表數(shù)組;newCapacity:哈希表空間容量,即table數(shù)組長度;size:哈希表有效數(shù)據(jù)長度;threshold:有效數(shù)據(jù)個數(shù)閥值;
? ? void addEntry(int hash, K key, V value, int bucketIndex) { if ((size >= threshold) && (null != table[bucketIndex])) { ? ?//達(dá)到閥值 && 當(dāng)前key對應(yīng)的bucket被占用,觸發(fā)擴(kuò)容 resize(2 * table.length); hash = (null != key) ? hash(key) : 0; bucketIndex = indexFor(hash, table.length); } createEntry(hash, key, value, bucketIndex); }
原始代碼:
public class ExpandCapacity { public static final int ONE_MB = 1024 * 1024; public static void main(String[] args) throws Exception { mapExpandCapacity(); } /** * java.util.HashMap#resize(int) * 何時擴(kuò)容,發(fā)生擴(kuò)容時候的上下文情況 */ public static void mapExpandCapacity() throws Exception { new Thread(new Runnable() { public void run() { while (true) { //這是模擬主要邏輯 Map<Integer, Byte[]> map = new HashMap<Integer, Byte[]>();//16,0.75 16*0.75=12 int size = 100; for (int i = 0; i < size; i++) { map.put(i, new Byte[ONE_MB]); } System.out.println("Expand SIZE = " + map.size()); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }, "mapExpandCapacity").start(); TimeUnit.HOURS.sleep(1); } }
trace腳本,特別注意OnMethod的中@Location#Where默認(rèn)值是Before,代表方法被調(diào)用,但還未執(zhí)行其內(nèi)容:
@BTrace public class ExpandCapacityBtrace { @OnMethod(clazz = "java.util.HashMap", method = "resize", location = @Location(value = Kind.CALL, clazz = "/.*/", method = "/.*/")) public static void traceMapExpandCapacity(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, @Self Object self,int newCapacity) { String point = Strings.strcat(Strings.strcat(probeClass, "."), probeMethod);//java/util/HashMap.resize Class clazz = classForName("java.util.HashMap"); println(Strings.strcat(point, "======")); //獲取實(shí)例protected變量 Map.Entry[] table= (Map.Entry[]) get(field(clazz, "table", true), self); int threshold = getInt(field(clazz, "threshold", true), self); int size = getInt(field(clazz, "size", true), self); println(Strings.strcat("newCapacity:", str(newCapacity))); println(Strings.strcat("table.length:", str(table.length))); println(Strings.strcat("size:", str(size))); println(Strings.strcat("threshold:", str(threshold))); println(Strings.strcat(point, "------------")); } }
輸出(摘錄核心,并進(jìn)行格式化):
java/util/HashMap.resize====== newCapacity:32 table.length:16 size:16 threshold:12 java/util/HashMap.resize------------ java/util/HashMap.resizee====== newCapacity:64 table.length:32 size:32 threshold:24 java/util/HashMap.resize------------ java/util/HashMap.resizee====== newCapacity:128 table.length:64 size:64 threshold:48 java/util/HashMap.resize------------
初始(New HashMap()):newCapacity=16,size=0,table.length=16,threshold=12; 添加100個有效數(shù)據(jù),會發(fā)生多次擴(kuò)容,調(diào)用resize,首次調(diào)用:newCapacity=32,size=16,entry.length:16,threshold=12; threshold每次執(zhí)行完resize會改變?yōu)?int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1)。 四、常見問題
Q:btrace默認(rèn)將信息打印到控制臺,如何將信息打印到文件? btrace pid btrace_scrpit.java > console.log >代表覆蓋,>>代表追加,其實(shí)就是把控制臺的東東重定向到文件。
Q:典型錯誤NullPointerException:
...... Exception in thread "main" java.lang.NullPointerException at com.sun.btrace.client.Client.submit(Client.java:361) at com.sun.btrace.client.Main.main(Main.java:189) DEBUG: sending exit command Exception in thread "Thread-0" java.lang.IllegalStateException at com.sun.btrace.client.Client.send(Client.java:466) at com.sun.btrace.client.Client.sendExit(Client.java:400) at com.sun.btrace.client.Main$2.run(Main.java:229) at java.lang.Thread.run(Thread.java:745)
原因一: 應(yīng)用程序端使用BTrace版本和客戶端不一致。 為了方便BTrace腳本編寫,IDE引入BTrace Jar,設(shè)置了Compile范圍,應(yīng)用啟動加載時會加載相關(guān)class。 而運(yùn)行btrace命令時,其BTrace版本和應(yīng)用端的BTrace版本不一致,產(chǎn)生異常。 這里直接將IDE引入的BTrace設(shè)置成Provided。
原因二: 主要由于Btrace默認(rèn)使用%JAVA_HOME%/lib/tools.jar,而應(yīng)用程序類加載路徑(-cp/-classpath)沒有tools.jar,因此我們需要把tools.jar加到類路徑下。 相關(guān)git issue: https://github.com/jbachorik/btrace/issues/182 https://github.com/jbachorik/btrace/issues/169(解決方案) https://github.com/jbachorik/btrace/issues/168 https://github.com/jbachorik/btrace/issues/182(解決方案)
五、BTrace詳解
BTrace?的使用通過Annotations,詳情:https://github.com/jbachorik/btrace/wiki/BTrace-Annotations 中文翻譯(個人水平有限,建議中英對照):
@Location注意點(diǎn): Kind.ENTRY意指進(jìn)入匹配probe點(diǎn),與@Location設(shè)置的clazz和method沒有任何關(guān)系。 Kind.CALL意指從某個匹配probe的方法中調(diào)用了匹配A class method的點(diǎn),一定要和clazz,method配合使用。clazz和method的默認(rèn)值為"",所以不能被匹配。
?Kind.ENTRY只關(guān)注調(diào)用X方法(OnMethod的clazz,method),Kind.CALL還關(guān)注X方法中,還調(diào)用了哪些類哪些方法(Location的class,method)。
如何獲取方法參數(shù)?可參見@Kind#CALL說明。 如方法java.util.HashMap#resize(int newCapacity),要獲取newCapacity,則可以在trace方法當(dāng)參數(shù)注入,如下:
public static void traceMapExpandCapacity(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod,?@Self Object self,int?newCapacity)
其中newCapacity則是resize調(diào)用時的newCapacity參數(shù)。
@TargetInstance 和 @TargetMethodOrField的理解 如果是probe A class方法調(diào)用匹配了B class,@TargetInstance 返回的就是B class,@Self返回的就是A class。
@Retrun:用來指定被trace方法的返回對象,可參考例子Classload.java。
Trace Scripts 說明,主要是一些約束,描述了能做什么,不能做什么:https://github.com/jbachorik/btrace/wiki/Trace-Scripts
samples目錄有很多樣例,想寫又不會寫時,可以進(jìn)去看看,這有中文說明:http://mgoann.iteye.com/blog/1409667
六、參考文獻(xiàn) 1. btrace記憶:http://agapple.iteye.com/blog/962119 2. btrace一些你不知道的事(源碼入手),這篇很有料,請看:http://agapple.iteye.com/blog/1005918 附:思維導(dǎo)圖(轉(zhuǎn)載)
一、下載安裝BTtrace wiki:https://github.com/jbachorik/btrace/wiki BTrace博客:http://jbachorik.github.io/btrace/ JIRA(深入必看):https://kenai.com/jira/browse/BTRACE/?selectedTab=com.atlassian.jira.jira-projects-plugin:summary-panel 官網(wǎng):https://kenai.com/projects/btrace/ github(1.3):https://github.com/jbachorik/btrace IDE環(huán)境:引入BTrace相關(guān)Jar,在eclipse/IDEA編寫;使用jvisualvm編寫。 代碼自動生成工具(使用必備):https://btrace.org/btrace/
下載最新版(1.3,JDK7以上):https://github.com/jbachorik/btrace/releases/tag/v1.3 BTrace Maven插件:https://github.com/btraceio/btrace-maven BTrace1.2開發(fā)者說明:https://kenai.com/projects/btrace/pages/DeveloperGuide BTrace1.2 API DOC:https://btrace.kenai.com/javadoc/1.2/index.html
配置環(huán)境變量: BTRACE_HOME:~/user/btrace PATH:$BTRACE_HOME/bin JAVA_VERSION:MAC特有,詳見btrace腳本內(nèi)容,后文有說明。
Maven倉庫(只有1.2.X版本,更高版本請自行編譯發(fā)布到私服):
?<!-- 1.2版本--> <dependency> <groupId>com.sun.tools.btrace</groupId> <artifactId>btrace-agent</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.sun.tools.btrace</groupId> <artifactId>btrace-boot</artifactId> <version>1.2.3</version> </dependency>
注意:應(yīng)用程序端不需要引入BTrace相關(guān)Jar,但為了編寫腳本方便(IDE提示),可以以Provided引入它,不必Complie。
二、BTtrace常用腳本 解壓btrace發(fā)現(xiàn)在%BTRACE_HOME%\bin有三個腳本:btrace、btracec、btracer,我們通過這三個腳本使用BTrace。 中文簡述: btrace:對運(yùn)行中的JAVA程序執(zhí)行btrace腳本。 btracec:編譯btrace腳本。 btracer:讓BTrace隨JAVA應(yīng)用啟動加載。
btracer注意事項(xiàng): 原理:java -javaagent:btrace-agent.jar=[<agent-arg>[,<agent-arg>]*]? <launch-args> 場景:When?no custom agent arguments?are required the?btracer?utility script may be used instead. 完整用法:https://github.com/jbachorik/btrace/wiki/btracer。
英文完整說明: <btrace>/bin/btrace <PID> <trace_script>will attach to the java application with the given PID and compile and submit the trace script <btrace>/bin/btracec <trace_script>will compile the provided trace script <btrace>/bin/btracer <compiled_script><args to launch a java app> will start the specified java application with the btrace agent running and the script previously compiled by btracec loaded
詳細(xì)看看btrace(1.3.4)腳本內(nèi)容:
#! /bin/sh if [ -z "$BTRACE_HOME" -o ! -d "$BTRACE_HOME" ] ; then # resolve links - $0 could be a link to btrace's home PRG="$0" progname=`basename "$0"` BTRACE_HOME=`dirname "$PRG"`/.. BTRACE_HOME=`cd "$BTRACE_HOME" && pwd` fi if [ -f "${BTRACE_HOME}/build/btrace-client.jar" ] ; then if [ "${JAVA_HOME}" != "" ]; then TOOLS_JAR="${JAVA_HOME}/lib/tools.jar" if [ ! -f ${TOOLS_JAR} ] ; then case "`uname`" in Darwin*) # In older JDK versions for?Mac OS X, tools.jar is classes.jar # and is kept in a?different location. Check if we can locate # classes.jar based on ${JAVA_VERSION} TOOLS_JAR="/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Classes/classes.jar" # if we can't find, try?relative?path from ${JAVA_HOME}. Usually, # /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home # is JAVA_HOME. (or whatever version beyond 1.6.0!) if [ ! -f ${TOOLS_JAR} ] ; then TOOLS_JAR="${JAVA_HOME} /../Classes/classes.jar" fi # If we still can't find, tell the user to set JAVA_VERSION. # This way, we can avoid zip file errors from the agent side # and "connection refused" message from client. if [ ! -f ${TOOLS_JAR} ] ; then echo "Please set JAVA_VERSION to the target java version" exit 1 fi ;; esac fi ${JAVA_HOME}/bin/java -cp ${BTRACE_HOME}/build/btrace-client.jar:${TOOLS_JAR}:/usr/share/lib/java/dtrace.jar com.sun.btrace.client.Main $* else echo "Please set JAVA_HOME before running this script" exit 1 fi else echo "Please set BTRACE_HOME before running this script" exit 1 fi
MAC 版本如果遇到錯誤,請留意%JAVA_VERSION%這個環(huán)境變量,原因見上面btrace腳本源碼。 【${JAVA_HOME}/bin/java -cp ${BTRACE_HOME}/build/btrace-client.jar:${TOOLS_JAR}:/usr/share/lib/java/dtrace.jar?com.sun.btrace.client.Main?$*】btrace啟動核心靠它!。 三、使用示例(監(jiān)控HashMap擴(kuò)容時的上下文情況) 簡要說明: table:哈希表數(shù)組;newCapacity:哈希表空間容量,即table數(shù)組長度;size:哈希表有效數(shù)據(jù)長度;threshold:有效數(shù)據(jù)個數(shù)閥值;
? ? void addEntry(int hash, K key, V value, int bucketIndex) { if ((size >= threshold) && (null != table[bucketIndex])) { ? ?//達(dá)到閥值 && 當(dāng)前key對應(yīng)的bucket被占用,觸發(fā)擴(kuò)容 resize(2 * table.length); hash = (null != key) ? hash(key) : 0; bucketIndex = indexFor(hash, table.length); } createEntry(hash, key, value, bucketIndex); }
原始代碼:
public class ExpandCapacity { public static final int ONE_MB = 1024 * 1024; public static void main(String[] args) throws Exception { mapExpandCapacity(); } /** * java.util.HashMap#resize(int) * 何時擴(kuò)容,發(fā)生擴(kuò)容時候的上下文情況 */ public static void mapExpandCapacity() throws Exception { new Thread(new Runnable() { public void run() { while (true) { //這是模擬主要邏輯 Map<Integer, Byte[]> map = new HashMap<Integer, Byte[]>();//16,0.75 16*0.75=12 int size = 100; for (int i = 0; i < size; i++) { map.put(i, new Byte[ONE_MB]); } System.out.println("Expand SIZE = " + map.size()); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }, "mapExpandCapacity").start(); TimeUnit.HOURS.sleep(1); } }
trace腳本,特別注意OnMethod的中@Location#Where默認(rèn)值是Before,代表方法被調(diào)用,但還未執(zhí)行其內(nèi)容:
@BTrace public class ExpandCapacityBtrace { @OnMethod(clazz = "java.util.HashMap", method = "resize", location = @Location(value = Kind.CALL, clazz = "/.*/", method = "/.*/")) public static void traceMapExpandCapacity(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, @Self Object self,int newCapacity) { String point = Strings.strcat(Strings.strcat(probeClass, "."), probeMethod);//java/util/HashMap.resize Class clazz = classForName("java.util.HashMap"); println(Strings.strcat(point, "======")); //獲取實(shí)例protected變量 Map.Entry[] table= (Map.Entry[]) get(field(clazz, "table", true), self); int threshold = getInt(field(clazz, "threshold", true), self); int size = getInt(field(clazz, "size", true), self); println(Strings.strcat("newCapacity:", str(newCapacity))); println(Strings.strcat("table.length:", str(table.length))); println(Strings.strcat("size:", str(size))); println(Strings.strcat("threshold:", str(threshold))); println(Strings.strcat(point, "------------")); } }
輸出(摘錄核心,并進(jìn)行格式化):
java/util/HashMap.resize====== newCapacity:32 table.length:16 size:16 threshold:12 java/util/HashMap.resize------------ java/util/HashMap.resizee====== newCapacity:64 table.length:32 size:32 threshold:24 java/util/HashMap.resize------------ java/util/HashMap.resizee====== newCapacity:128 table.length:64 size:64 threshold:48 java/util/HashMap.resize------------
初始(New HashMap()):newCapacity=16,size=0,table.length=16,threshold=12; 添加100個有效數(shù)據(jù),會發(fā)生多次擴(kuò)容,調(diào)用resize,首次調(diào)用:newCapacity=32,size=16,entry.length:16,threshold=12; threshold每次執(zhí)行完resize會改變?yōu)?int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1)。 四、常見問題
Q:btrace默認(rèn)將信息打印到控制臺,如何將信息打印到文件? btrace pid btrace_scrpit.java > console.log >代表覆蓋,>>代表追加,其實(shí)就是把控制臺的東東重定向到文件。
Q:典型錯誤NullPointerException:
...... Exception in thread "main" java.lang.NullPointerException at com.sun.btrace.client.Client.submit(Client.java:361) at com.sun.btrace.client.Main.main(Main.java:189) DEBUG: sending exit command Exception in thread "Thread-0" java.lang.IllegalStateException at com.sun.btrace.client.Client.send(Client.java:466) at com.sun.btrace.client.Client.sendExit(Client.java:400) at com.sun.btrace.client.Main$2.run(Main.java:229) at java.lang.Thread.run(Thread.java:745)
原因一: 應(yīng)用程序端使用BTrace版本和客戶端不一致。 為了方便BTrace腳本編寫,IDE引入BTrace Jar,設(shè)置了Compile范圍,應(yīng)用啟動加載時會加載相關(guān)class。 而運(yùn)行btrace命令時,其BTrace版本和應(yīng)用端的BTrace版本不一致,產(chǎn)生異常。 這里直接將IDE引入的BTrace設(shè)置成Provided。
原因二: 主要由于Btrace默認(rèn)使用%JAVA_HOME%/lib/tools.jar,而應(yīng)用程序類加載路徑(-cp/-classpath)沒有tools.jar,因此我們需要把tools.jar加到類路徑下。 相關(guān)git issue: https://github.com/jbachorik/btrace/issues/182 https://github.com/jbachorik/btrace/issues/169(解決方案) https://github.com/jbachorik/btrace/issues/168 https://github.com/jbachorik/btrace/issues/182(解決方案)
五、BTrace詳解
BTrace?的使用通過Annotations,詳情:https://github.com/jbachorik/btrace/wiki/BTrace-Annotations 中文翻譯(個人水平有限,建議中英對照):
- 方法注解說明?
@Location注意點(diǎn): Kind.ENTRY意指進(jìn)入匹配probe點(diǎn),與@Location設(shè)置的clazz和method沒有任何關(guān)系。 Kind.CALL意指從某個匹配probe的方法中調(diào)用了匹配A class method的點(diǎn),一定要和clazz,method配合使用。clazz和method的默認(rèn)值為"",所以不能被匹配。
?Kind.ENTRY只關(guān)注調(diào)用X方法(OnMethod的clazz,method),Kind.CALL還關(guān)注X方法中,還調(diào)用了哪些類哪些方法(Location的class,method)。
- 方法參數(shù)注解說明?
如何獲取方法參數(shù)?可參見@Kind#CALL說明。 如方法java.util.HashMap#resize(int newCapacity),要獲取newCapacity,則可以在trace方法當(dāng)參數(shù)注入,如下:
public static void traceMapExpandCapacity(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod,?@Self Object self,int?newCapacity)
其中newCapacity則是resize調(diào)用時的newCapacity參數(shù)。
@TargetInstance 和 @TargetMethodOrField的理解 如果是probe A class方法調(diào)用匹配了B class,@TargetInstance 返回的就是B class,@Self返回的就是A class。
@Retrun:用來指定被trace方法的返回對象,可參考例子Classload.java。
Trace Scripts 說明,主要是一些約束,描述了能做什么,不能做什么:https://github.com/jbachorik/btrace/wiki/Trace-Scripts
samples目錄有很多樣例,想寫又不會寫時,可以進(jìn)去看看,這有中文說明:http://mgoann.iteye.com/blog/1409667
六、參考文獻(xiàn) 1. btrace記憶:http://agapple.iteye.com/blog/962119 2. btrace一些你不知道的事(源碼入手),這篇很有料,請看:http://agapple.iteye.com/blog/1005918 附:思維導(dǎo)圖(轉(zhuǎn)載)
總結(jié)
以上是生活随笔為你收集整理的Btrace详细指南(JDK7,监控HashMap扩容)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NOIP2016普及组第一题:买铅笔
- 下一篇: eclipse整体替换