简体   繁体   English

在命令行中分析正在运行的 Java 应用程序

[英]Profiling a running Java application in command line

I profile running Java applications often with VisualVM but it needs X to run on the machine.我经常使用 VisualVM 分析运行 Java 应用程序,但它需要 X 才能在机器上运行。

I know I can connect through management port but that will be an offline sampled profiling which is not enough for me.我知道我可以通过管理端口连接,但这将是离线采样分析,这对我来说还不够。

So I'm looking for a solution with which I can profile the CPU usage of the methods of a running Java application from command-line.因此,我正在寻找一种解决方案,通过该解决方案,我可以从命令行分析正在运行的 Java 应用程序的方法的 CPU 使用率。 It's enough for me to collect data on the server and then the collected data can be analyzed on a different machine.我在服务器上收集数据就足够了,然后收集到的数据可以在不同的机器上进行分析。

Update:更新:

It seems I need to be more specific.看来我需要更具体一些。 I want to profile a running Java application from command line, I don't want to stop it and rerun it.我想从命令行分析正在运行的 Java 应用程序,我不想停止它并重新运行它。

The jvmtop application is a convenient tool for profiling from the commandline. jvmtop应用程序是用于从命令行进行分析的便捷工具。 No need to stop the jvm.无需停止 jvm。 Usage:用法:

jvmtop.sh --profile <PID>

Will give you output like this which will be updating while the app runs:将为您提供这样的输出,该输出将在应用程序运行时更新:

  Profiling PID 24015: org.apache.catalina.startup.Bootstrap
  36.16% (    57.57s) hudson.model.AbstractBuild.calcChangeSet()
  30.36% (    48.33s) hudson.scm.SubversionChangeLogParser.parse()
   7.14% (    11.37s) org.kohsuke.stapler.jelly.JellyClassTearOff.parseScript()
  ...

The advantage is that it does not take the use of instrumentation.优点是不使用仪器。 The classes of the to-be-profiled jvm will not be altered.待分析的 jvm 的类不会改变。

If you are looking for something more visual then have a look at jvm-mon which is based on jvmtop如果你正在寻找更直观的东西,那么看看基于 jvmtop 的jvm-mon

Looks like the "built-in" way to profile a java app from the command line is to start it with profiling command line parameters, like this看起来从命令行分析 Java 应用程序的“内置”方法是使用分析命令行参数启动它,就像这样

$ java -Xrunhprof:cpu=samples,file=myprogram.hprof ...

Then examine the file "myprogram.hprof" with some GUI tool (or web server tool like jhat ) or command line tool after the process exits (and the file is created at that time).然后在进程退出后,使用一些 GUI 工具(或 web 服务器工具,如jhat )或命令行工具检查文件“myprogram.hprof”(此时文件已创建)。

If you use the "QUIT" signal trick, mentioned https://stackoverflow.com/a/2344436/32453 then you can generate a file at will without exiting the JVM (it appears to append to the previous output file).如果您使用https://stackoverflow.com/a/2344436/32453提到的“QUIT”信号技巧,那么您可以随意生成一个文件而无需退出 JVM(它似乎附加到先前的输出文件)。 Or wait until the process exits and it will generate the file.或者等到进程退出,它会生成文件。

This (built-in) profiler does a sample infrequently so typically low slowdown/impact overall.这个(内置的)分析器很少做一个样本,所以总体上通常会降低速度/影响。

ref: http://web.archive.org/web/20160623224137/https://thunderguy.com/semicolon/2004/04/18/profiling-a-java-program-easily/参考: http ://web.archive.org/web/20160623224137/https: //thunderguy.com/semicolon/2004/04/18/profiling-a-java-program-easily/

You could also just do the "poor man's profiler" by collecting lots of jstacks and dumping them into ex: a flamegraph or some other analyzer/conglomerator...您也可以通过收集大量 jstack 并将它们转储到 ex:火焰图或其他一些分析器/聚合器中来做“穷人的分析器”...

Can you collect 10 or 20 stack samples with jstack ?你能用jstack收集 10 或 20 个堆栈样本吗? Then if Foo is a method, its overall time usage is the fraction of samples containing it.那么如果Foo是一个方法,它的总时间使用是包含它的样本的比例。 Its CPU usage is the fraction of those samples that don't terminate in I/O or a system call.它的 CPU 使用率是那些不以 I/O 或系统调用终止的样本的比例。 Its "self time" is the fraction of samples in which it itself is the terminus.它的“自身时间”是样本的一部分,它本身就是终点。

I don't need anything pretty.我不需要漂亮的东西。 I either run it under the IDE and collect them that way, or use something like jstack that snapshots the stack of a running app.我要么在 IDE 下运行它并以这种方式收集它们,要么使用像jstack这样的东西来对正在运行的应用程序的堆栈进行快照。

That's the random-pause technique.这就是随机暂停技术。

We have used hprof on our servers and it definitely is better than sysouts in case you can't run a full fledged VisualVM session.我们在我们的服务器上使用了hprof ,它绝对比 sysouts 更好,以防您无法运行完整的 VisualVM 会话。

Examples of using hprof are plenty out there:使用 hprof 的例子很多:

The most precise profiling can be achieved with https://github.com/jvm-profiling-tools/async-profiler .使用https://github.com/jvm-profiling-tools/async-profiler可以实现最精确的分析。

This project is a low overhead sampling profiler for Java that does not suffer from Safepoint bias problem.这个项目是一个低开销的 Java 采样分析器,不受安全点偏差问题的影响。 It features HotSpot-specific APIs to collect stack traces and to track memory allocations.它具有特定于 HotSpot 的 API 来收集堆栈跟踪和跟踪内存分配。 The profiler works with OpenJDK, Oracle JDK and other Java runtimes based on HotSpot JVM.分析器与 OpenJDK、Oracle JDK 和其他基于 HotSpot JVM 的 Java 运行时一起工作。

Here is my script to install and run it from command-line:这是我从命令行安装和运行它的脚本:

async-profiler.sh异步分析器.sh

if [ ! -d profiler ]; then
  mkdir profiler && cd profiler && curl -L https://github.com/jvm-profiling-tools/async-profiler/releases/download/v1.6-ea/async-profiler-1.6-ea-linux-x64.tar.gz | tar xvz
  echo 1 > /proc/sys/kernel/perf_event_paranoid
  echo 0 > /proc/sys/kernel/kptr_restrict
  #apt install openjdk-8-dbg
else
  cd profiler
fi

#jps

./profiler.sh -d 60 -f dump_`date +%Y-%m-%d_%H-%M-%S`.jfr `jps -q`

It assumes that app is run under same user and there is a single java process PID to be listed by jps.它假设应用程序在同一用户下运行,并且 jps 会列出一个 java 进程 PID。 Profiling duration is 60 seconds.分析持续时间为 60 秒。

No modification of app's startup options or app restart is needed.无需修改应用程序的启动选项或重启应用程序。

GUI for examining dumps is built-in into IntelliJ IDEA Ultimate: https://www.jetbrains.com/help/idea/cpu-profiler.html .用于检查转储的 GUI 内置于 IntelliJ IDEA Ultimate: https : //www.jetbrains.com/help/idea/cpu-profiler.html

One way to profile an "already started" JVM is to aggregate multiple jstacks taken over time.分析“已经启动”的 JVM 的一种方法是聚合多个随着时间推移获取的 jstack。

You can for instance parse and display them as a FlameGraph (see details at the various answers for that link, I won't redundantly include them here).例如,您可以将它们解析并显示为FlameGraph (请参阅该链接的各种答案中的详细信息,我不会在这里多余地包含它们)。

You can run most commercial profilers remotely so an agent is run on the server then connect to that agent through a client on your dev machine.您可以远程运行大多数商业分析器,以便在服务器上运行代理,然后通过开发机器上的客户端连接到该代理。 My absolute favorite profiler is JProfiler.我最喜欢的分析器是 JProfiler。 It's fairly reasonable purchase, and it's very stable (which not all commercial profilers that's true).这是相当合理的购买,而且非常稳定(并非所有商业分析器都是如此)。

http://www.ej-technologies.com/products/jprofiler/overview.html http://www.ej-technologies.com/products/jprofiler/overview.html

Other commercial profilers that are stable, but not my favorite are YourKIT.其他稳定但不是我最喜欢的商业分析器是 YourKIT。

http://www.yourkit.com/ http://www.yourkit.com/

Those smaller vendors make good tools.那些较小的供应商提供了很好的工具。 These tools will provide you tons of information about method timings, memory use, GC, etc. Much more than jconsole.这些工具将为您提供大量有关方法计时、内存使用、GC 等的信息。远不止 jconsole。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM