usecase.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 用法
  2. show-busy-java-threads
  3. # 从所有运行的Java进程中找出最消耗CPU的线程(缺省5个),打印出其线程栈
  4. # 缺省会自动从所有的Java进程中找出最消耗CPU的线程,这样用更方便
  5. # 当然你可以手动指定要分析的Java进程Id,以保证只会显示你关心的那个Java进程的信息
  6. show-busy-java-threads -p <指定的Java进程Id>
  7. show-busy-java-threads -c <要显示的线程栈数>
  8. show-busy-java-threads <重复执行的间隔秒数> [<重复执行的次数>]
  9. # 多次执行;这2个参数的使用方式类似vmstat命令
  10. show-busy-java-threads -a <运行输出的记录到的文件>
  11. # 记录到文件以方便回溯查看
  12. show-busy-java-threads -S <存储jstack输出文件的目录>
  13. # 指定jstack输出文件的存储目录,方便记录以后续分析
  14. ##############################
  15. # 注意:
  16. ##############################
  17. # 如果Java进程的用户 与 执行脚本的当前用户 不同,则jstack不了这个Java进程
  18. # 为了能切换到Java进程的用户,需要加sudo来执行,即可以解决:
  19. sudo show-busy-java-threads
  20. show-busy-java-threads -s <指定jstack命令的全路径>
  21. # 对于sudo方式的运行,JAVA_HOME环境变量不能传递给root,
  22. # 而root用户往往没有配置JAVA_HOME且不方便配置,
  23. # 显式指定jstack命令的路径就反而显得更方便了
  24. # -m选项:执行jstack命令时加上-m选项,显示上Native的栈帧,一般应用排查不需要使用
  25. show-busy-java-threads -m
  26. # -F选项:执行jstack命令时加上 -F 选项(如果直接jstack无响应时,用于强制jstack),一般情况不需要使用
  27. show-busy-java-threads -F
  28. # -l选项:执行jstack命令时加上 -l 选项,显示上更多相关锁的信息,一般情况不需要使用
  29. # 注意:和 -m -F 选项一起使用时,可能会大大增加jstack操作的耗时
  30. show-busy-java-threads -l
  31. # 帮助信息
  32. $ show-busy-java-threads -h
  33. Usage: show-busy-java-threads [OPTION]... [delay [count]]
  34. Find out the highest cpu consumed threads of java processes,
  35. and print the stack of these threads.
  36. Example:
  37. show-busy-java-threads # show busy java threads info
  38. show-busy-java-threads 1 # update every 1 second, (stop by eg: CTRL+C)
  39. show-busy-java-threads 3 10 # update every 3 seconds, update 10 times
  40. Output control:
  41. -p, --pid <java pid> find out the highest cpu consumed threads from
  42. the specified java process.
  43. default from all java process.
  44. -c, --count <num> set the thread count to show, default is 5.
  45. -a, --append-file <file> specifies the file to append output as log.
  46. -S, --store-dir <dir> specifies the directory for storing
  47. the intermediate files, and keep files.
  48. default store intermediate files at tmp dir,
  49. and auto remove after run. use this option to keep
  50. files so as to review jstack/top/ps output later.
  51. delay the delay between updates in seconds.
  52. count the number of updates.
  53. delay/count arguments imitates the style of
  54. vmstat command.
  55. jstack control:
  56. -s, --jstack-path <path> specifies the path of jstack command.
  57. -F, --force set jstack to force a thread dump. use when jstack
  58. does not respond (process is hung).
  59. -m, --mix-native-frames set jstack to print both java and native frames
  60. (mixed mode).
  61. -l, --lock-info set jstack with long listing.
  62. prints additional information about locks.
  63. CPU usage calculation control:
  64. -d, --top-delay specifies the delay between top samples.
  65. default is 0.5 (second). get thread cpu percentage
  66. during this delay interval.
  67. more info see top -d option. eg: -d 1 (1 second).
  68. -P, --use-ps use ps command to find busy thread(cpu usage)
  69. instead of top command.
  70. default use top command, because cpu usage of
  71. ps command is expressed as the percentage of
  72. time spent running during the *entire lifetime*
  73. of a process, this is not ideal in general.
  74. Miscellaneous:
  75. -h, --help display this help and exit.
  76. 示例
  77. $ show-busy-java-threads
  78. [1] Busy(57.0%) thread(23355/0x5b3b) stack of java process(23269) under user(admin):
  79. "pool-1-thread-1" prio=10 tid=0x000000005b5c5000 nid=0x5b3b runnable [0x000000004062c000]
  80. java.lang.Thread.State: RUNNABLE
  81. at java.text.DateFormat.format(DateFormat.java:316)
  82. at com.xxx.foo.services.common.DateFormatUtil.format(DateFormatUtil.java:41)
  83. at com.xxx.foo.shared.monitor.schedule.AppMonitorDataAvgScheduler.run(AppMonitorDataAvgScheduler.java:127)
  84. at com.xxx.foo.services.common.utils.AliTimer$2.run(AliTimer.java:128)
  85. at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  86. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  87. at java.lang.Thread.run(Thread.java:662)
  88. [2] Busy(26.1%) thread(24018/0x5dd2) stack of java process(23269) under user(admin):
  89. "pool-1-thread-2" prio=10 tid=0x000000005a968800 nid=0x5dd2 runnable [0x00000000420e9000]
  90. java.lang.Thread.State: RUNNABLE
  91. at java.util.Arrays.copyOf(Arrays.java:2882)
  92. at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
  93. at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:572)
  94. at java.lang.StringBuffer.append(StringBuffer.java:320)
  95. - locked <0x00000007908d0030> (a java.lang.StringBuffer)
  96. at java.text.SimpleDateFormat.format(SimpleDateFormat.java:890)
  97. at java.text.SimpleDateFormat.format(SimpleDateFormat.java:869)
  98. at java.text.DateFormat.format(DateFormat.java:316)
  99. at com.xxx.foo.services.common.DateFormatUtil.format(DateFormatUtil.java:41)
  100. at com.xxx.foo.shared.monitor.schedule.AppMonitorDataAvgScheduler.run(AppMonitorDataAvgScheduler.java:126)
  101. at com.xxx.foo.services.common.utils.AliTimer$2.run(AliTimer.java:128)
  102. at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  103. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  104. at java.lang.Thread.run(Thread.java:662)
  105. ......