ひでっぷの技術メモ

はてなダイアリーから移行しました

phoneMEでスレッドダンプを取得する

LinuxJavaを動かしている場合、各スレッドのスレッドダンプを取得することができる。
Javaの親スレッドを

pstree -p | grep java*1

で探し出して、
kill -3 プロセス番号

とするとJavaの標準出力に各スレッドのスレッドダンプが表示される。

phoneMEではデフォルトではスレッドダンプが表示されないのでソースを修正&再コンパイルしてやる必要がある。

やり方がこちらのMark Lam's Blog-Async Thread Dumps on CVMにあったので試してみた。

1.src/linux/javavm/runtime/sync_md.cに以下の関数を追加する

/* BEGIN for Debug Use only */
#include "javavm/include/interpreter.h"
/* Warning: This thread dumper is only for the use of debugging code.
There's a risk that it can potentially crash the VM if invoked at
the wrong time. Hence, this is not to be incorporated into a
production build. It is only for assisting in debugging efforts
when needed.
*/
static void threadDumpHandler(int sig)
{
CVMExecEnv *ee = CVMgetEE();
CVMBool success;
int threadCount = 1;

success = CVMsysMutexTryLock(ee, &CVMglobals.threadLock);
if (!success) {
return;
}

CVMconsolePrintf("\nStart thread dump:\n");
CVMconsolePrintf("======================================\n");
CVM_WALK_ALL_THREADS(ee, threadEE, {
CVMconsolePrintf("Thread %d: ee 0x%x", threadCount, threadEE);
CVMdumpStack(&threadEE->interpreterStack,0,0,0);
CVMconsolePrintf("======================================\n");
threadCount++;
});
CVMconsolePrintf("End thread dump\n\n");

CVMsysMutexUnlock(ee, &CVMglobals.threadLock);
}
/* END for Debug Use only */

2.同じくsrc/linux/javavm/runtime/sync_md.c内の関数linuxSyncInit()に以下の行を追加する。


/* BEGIN for Debug Use only */
{SIGQUIT, threadDumpHandler, SA_RESTART},
/* END for Debug Use only */

3.Armadillo-9で使用するphoneMEのコンパイル方法を元にphoneMEをコンパイル。ただしmakeオプションに

CVM_DEBUG_DUMPSTACK=true

を追加すること。じゃないとコンパイルが通りません。
後は今動いているphoneMEと入れ替えるだけ。
きちんとスレッドダンプが表示されるようになりました。

CVM_DEBUG=trueにした時のように負荷もないし、今のところ快適に動いています。

*1:phoneMEの場合はcvm