アリ診断ツールarthasの使い方

序章

Ali によるオープンソースの Java 診断ツールで、動的追跡技術をさらに簡素化し、クラスのロード情報、JVM 情報、スレッド スタック情報、追跡メソッドの実行ステータス、逆コンパイルされたクラス ファイルなどを直接表示できます。基本的には、Java コマンド ライン ツールの機能と BTrace の機能について上記をカバーしています。非常に便利な Arthas が提供するコマンドに慣れるだけで、余分な作業は必要ありません。実際、Arthas の最下層は BTrace と同じで、jvmAgent メソッドに基づいて、Instrumentation メソッドを使用してバイトコード メソッドを変更し、実行して出力します。この記事では Arthas の使い方を紹介します. 公式ドキュメントには非常に詳細に書かれているので, 公式ドキュメントを直接読んで学ぶことをお勧めします. この記事は初心者向けです. 公式サイトアドレス: https: //alibaba.github.io/arthas

Arthas のインストールと操作

アーサスをダウンロード

公式の推奨事項は、jar を直接ダウンロードして実行することです。

wget https://alibaba.github.io/arthas/arthas-boot.jar

アーサスを実行

ダウンロードしたarthas-boot.jarパッケージを、監視したいJavaアプリケーションのあるサーバーに置き、SpringBootアプリケーションと同様にjavaコマンドで直接実行することができます。

java -jar arthas-boot.jar

知らせ:

  • 初めて実行する場合、ダウンロードが遅い場合は、 --repo-mirror aliyun–use-http を使用できます
  • 起動後、現在の Java アプリケーション リスト (jps-l に少し似ています) を一覧表示し、監視するアプリケーションを選択するためのシリアル番号を出力します。

起動後、Arthas のコマンド ライン インターフェイスを使用でき、Arthas が提供するコマンドを使用して、監視する必要がある機能を実現できます。以下の図に示すように、監視対象の Java アプリケーションは、java-monitor-example の例です。
ここに画像の説明を挿入

やめる

現在の接続を終了するだけの場合は、quit または exit コマンドを使用できます。ターゲット プロセスにアタッチされた Arthas は引き続き実行され、ポートは開いたままになり、次回の接続時に直接接続できます。
arthas を完全に終了したい場合は、shutdown コマンドを実行できます。

アーサスの使用

Arthas の使用は、Arthas が提供するコマンド機能の使い方を学ぶことであり、主にいくつかのカテゴリに分類されます。

  • 基本的なコマンド: help、cat、pwd、history、quit などは Linux コマンドに似ています。
  • Jvm 関連: ダッシュボード、スレッド、jvm、sysenv など。主に JVM 情報を監視します。これは、以前に Java コマンドライン ツール jinfo、jmap、jstack などを学習するのと似ています。
  • class/classloader 関連: sc、sm、jad、dump、classloader など。
  • モニター/ウォッチ/トレース関連: モニター、ウォッチ、トレース、スタックなど。これらの関数は、タイミング検出、メソッド パラメーター、戻り値、呼び出し期間など、BTrace で実装されている関数を基本的にカバーしています。

よく使用されるコマンドのいくつかを以下に説明します. コマンドの詳細なリストについては、公式ドキュメントを参照してください.

概要: ダッシュボード

Arthas を起動したら、-h を使用して使用法ヘルプを表示します。

$ dashboard -h

 USAGE:

   dashboard [-b] [-h] [-i <value>] [-n <value>]

 SUMMARY:

   Overview of target jvm's thread, memory, gc, vm, tomcat info.


 EXAMPLES:

   dashboard

   dashboard -n 10

   dashboard -i 2000

概要に相当し、スレッド、メモリ、gc ステータス、vm ステータス、および tomcat 情報を 1 つのインターフェイスに表示します。次の図に示すように (公式ドキュメントの例の図):
ここに画像の説明を挿入

この概要の情報は、デフォルトで 5 秒ごとに更新されます。メモリの変化、スレッドの使用状況、GC 時間を一目で確認できます。ctrl+c を使用して終了します。

スレッド情報: スレッド

jstack を思い出してください。最初にスレッド ID を見つけ、それを使用してスレッド スタックをエクスポートし、次にスレッド ID を使用してそれを表示する必要があります。Arthas でははるかに便利です。上記のダッシュボードには既に ID があり、スレッド ID を直接使用するだけです。-h ヘルプ ドキュメントを表示するには:

$ thread -h

 USAGE:

   thread [-h] [-b] [-i <value>] [-n <value>] [id]

 SUMMARY:

   Display thread info, thread stack

 EXAMPLES:

   thread

   thread 51

   thread -n -1

   thread -n 5

   thread -b

   thread -i 2000




 OPTIONS:

 -h, --help  this help

 -b, --include-blocking-thread  Find the thread who is holding a lock that blocks the most number of threads.

 -i, --sample-interval <value>  Specify the sampling interval (in ms) when calculating cpu usage.

 -n, --top-n-threads <value>   The number of thread(s) to show, ordered by cpu utilization, -1 to show all.

 <id>  Show thread stack

上記の例に示されているように、thread コマンドを使用すると、CPU を最も多く占有している上位 N 個のスレッドを見つけることができ (-n)、指定されたスレッドの実行中のスタック (id) を出力して、そのスレッドを見つけることができます。現在、他のスレッドをブロックしています (-b)。これは、スレッドの問題を分析するのに非常に便利です。

JVM 情報: jvm

jvm コマンドは非常に単純で、パラメーターはありません. 出力される情報には、実行中のパラメーター、クラスのロード情報、メモリの状態、システム情報、スレッド番号の情報、ファイル記述子などが含まれます。jvisualvm の概要に似ていますが、それよりも詳細です。

逆コンパイル: jad

オンラインで実行中のアプリケーションで、新しいコードが使用されているか、更新されているかを確認する必要がある場合があります.ロードされたクラスを逆コンパイルして、ソースコードが最新であるかどうかを確認できます.このとき、jadは非常に便利です. -h 使い方のヘルプを表示:

$ jad -h

 USAGE:

   jad [-c <value>] [-h] [-E] [--source-only] class-pattern [method-name]


 EXAMPLES:

   jad java.lang.String

   jad java.lang.String toString

   jad --source-only java.lang.String

   jad -c 39eb305e org/apache/log4j/Logger

   jad -c 39eb305e -E org\\.apache\\.*\\.StringUtils


 OPTIONS:

 -c, --code <value>  The hash code of the special class's classLoader

 -h, --help          this help

 -E, --regex         Enable regular expression to match (wildcard matching by default)

     --source-only   Output source code only

 <class-pattern>     Class name pattern, use either '.' or '/' as separator

 <method-name>       method name pattern, decompile a specific method instead of the whole class

上記の例に示すように、jad はクラス (class-pattern) を逆コンパイルし、クラスの特定のメソッド (method-name) を逆コンパイルできます。複数の classLoader がある場合、-c を使用して表示するものを選択することもできます等

メソッド実行監視:監視

モニターは、呼び出し回数、成功回数、失敗回数、平均継続時間、失敗率など、モニタリング用のメソッドの実行を定期的に出力できます。BTrace の @Timer に少し似ていますが、もっと便利。-h 使い方のヘルプを表示:

$ monitor -h

 USAGE:

   monitor [-c <value>] [-h] [-n <value>] [-E] class-pattern method-pattern

 SUMMARY:

   Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc.

 Examples:

   monitor org.apache.commons.lang.StringUtils isBlank

   monitor org.apache.commons.lang.StringUtils isBlank -c 5

   monitor -E org\.apache\.commons\.lang\.StringUtils isBlank

 OPTIONS:

 -c, --cycle <value>    The monitor interval (in seconds), 60 seconds by default

 -h, --help             this help

 -n, --limits <value>   Threshold of execution times

 -E, --regex            Enable regular expression to match (wildcard matching by default)

 <class-pattern>        Path and classname of Pattern Matching

 <method-pattern>       Method of Pattern Matching

上記の例に示すように, メソッドの実行を監視できます. デフォルトでは 60 秒ごとに 1 回出力されます. -c を使用して出力間隔を変更できます.

メソッド実行データのモニタリング: watch

BTrace の @OnMethod と同様に、オンライン アプリケーションでメソッドのパラメータ、戻り値、および例外情報を実行する場合は、watch コマンドが非常に適しています。-h ヘルプを使用:

$ watch -h

 USAGE:

   watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class-pattern method-pattern express [condition-express]

 SUMMARY:

   Display the input/output parameter, return object, and thrown exception of specified method invocation

   The express may be one of the following expression (evaluated dynamically):

           target : the object

            clazz : the object's class

           method : the constructor or method

           params : the parameters array of method

     params[0..n] : the element of parameters array

        returnObj : the returned object of method

         throwExp : the throw exception of method

         isReturn : the method ended by return

          isThrow : the method ended by throwing exception

            #cost : the execution time in ms of method invocation

 Examples:

   watch -b org.apache.commons.lang.StringUtils isBlank params

   watch -f org.apache.commons.lang.StringUtils isBlank returnObj

   watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj}' -x 2

   watch -bf *StringUtils isBlank params

   watch *StringUtils isBlank params[0]

   watch *StringUtils isBlank params[0] params[0].length==1

   watch *StringUtils isBlank params '#cost>100'

   watch -E -b org\.apache\.commons\.lang\.StringUtils isBlank params[0]

 OPTIONS:

 -b, --before         Watch before invocation

 -e, --exception      Watch after throw exception

 -x, --expand <value>    Expand level of object (1 by default)

 -f, --finish         Watch after invocation, enable by default

 -h, --help           this help

 -n, --limits <value>   Threshold of execution times

 -E, --regex    Enable regular expression to match (wildcard matching by default)

 -M, --sizeLimit <value>   Upper size limit in bytes for the result (10 * 1024 * 1024 by default)

 -s, --success      Watch after successful invocation

 <class-pattern>    The full qualified class name you want to watch

 <method-pattern>   The method name you want to watch

 <express>          the content you want to watch, written by ognl.

                    Examples:

                          params 

                          params[0]

                          'params[0]+params[1]'

                          '{params[0], target, returnObj}'

                          returnObj

                          throwExp

                          target

                          clazz

                          method

上記の例にあるように、監視タイミングは、メソッド実行前 (-b)、メソッド実行終了 (-f、デフォルト値)、およびメソッド実行成功 (-s) です。監視内容には、パラメータ(params)、戻り値(returnObj)、例外(throwExp)などがあります。

要約する

Arthas の強力な機能は、基本的に Java アプリケーションを包括的に監視できます. これは、オンライン アプリケーション監視の優れたヘルパーです. この記事では、Arthas のインストールと使用について比較的簡単な紹介を提供します. Arthas の公式 Web サイトにアクセスして、さらに詳しく知ることができれば幸いです. study. には、各コマンドの詳細な説明と例が既に用意されており、独自の Java 監視技術をより強固なものにすることができます。

おすすめ

転載: blog.csdn.net/h295928126/article/details/129919246