Is there a gprof-like profiler for Java that can be run from the terminal in Linux?
All tools I have found are GUI programs and I need run it from the terminal.
The JVM has a built-in profiler called HPROF. You can enable it on the command line like this:
java -agentlib:hprof=file=hprof.txt,cpu=samples MyClass
This will dump profile information out to a text file when the program finishes. In addition to profiling CPU usage, it can also track heap usage.
The open-source tool jvmtop contains a terminal profiler and might be worth a look:
JvmTop 0.7.0 alpha - 15:16:34, amd64, 8 cpus, Linux 2.6.32-27, load avg 0.41
http://code.google.com/p/jvmtop
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()
6.25% ( 9.95s) net.sf.json.JSONObject.write()
3.13% ( 4.98s) ....kohsuke.stapler.jelly.CustomTagLibrary.loadJellyScri()
JXInsight/OpenCore has term/shell reporting plugins (top, queues, stacks,...) that will output its metering and metrics data at regular intervals. It is also possible to access this information using the Open API which allows inspection of the model in real-time within the JVM or offline using a snapshot file handle. Both are supported via Plugin API which it how the top, queues,... ones work.
http://www.jinspired.com/products/opencore (commercial)
Is there a technical reason you can't use a GUI? Is it just a preference driven by a workflow habit? If not then you can always try out our FREE JXInsight/Opus Java Edition - a highly efficient and scalable code level latency performance measurement solution for rapidly identifying hotspots within Java and JRuby applications.
http://www.jinspired.com/products/opus
Note: I am the product architect of both products.
I use jconsole for that. http://docs.oracle.com/javase/6/docs/technotes/tools/share/jconsole.html
If you want it for profiling and monitoring. You can use Jvisualvm.
from App site:
DESCRIPTION
Java VisualVM is an intuitive graphical user interface that provides detailed information about Java technology-based applications (Java applications) while they are running on a given Java Virtual Machine (JVM*). The name Java VisualVM comes from the fact that Java VisualVM provides information about the JVM software visually.
Java VisualVM combines several monitoring, troubleshooting, and profiling utilities into a single tool. For example, most of the functionality offered by the standalone tools jmap, jinfo, jstat and jstack have been integrated into Java VisualVM. Other functionalities, such as some of those offered by the JConsole tool, can be added as optional plug-ins.
EDIT:
As you want a terminal approach.Refer this link Triggering a Javadump.
It describes creation of java dump.
Related
Attaching the image for the Full GC. What I am unclear is what else this tool Java Mission control will provide that will help me to understand where it has occurred and why? Like the functions that it was trying to execute at this time.
Any pointers from pundits who perform gc analysis.
Java Mission Control (JMC) is a set of tools helping to analyze what happens in the JVM. It's composed of 3 parts:
JMX console - monitors JVM activity in real-time and allows to
change some of JVM's properties without restarting it. Moreover, it
can be used as alerting system to send notifications when the
threshold of some metrics is reached (e.g. CPU use is equal to 90%)
Java Flight Recorder (JFR) - JFR collects data over time. It can be used separately from JMC but the most often both are used together. Please note that the JFR can be freely used only for evaluation purposes. Used in production servers it requires a commercial licence.
external plugins - as in the case of JVisualVM, JMC also makes possible to extend its features with additional plugins
After I connect to weblogic process through JConsole, I see some overall statistics but not the method-wise breakup of performance (time required for each method/class). Besides, as of now, I don't see how particular methods may be profiled using JConsole. Am I looking at the right tool? Or should I go for JProfiler instead?
You are using the wrong tool.
JConsole is a montiring tool. It will help you look at metrics like classes loaded, Heap Space, Perm Space, Threads Live, Collections etc etc...
What you want to do is profiling, not monitoring, try jvisualvm you can find it under JAVA_HOME/bin right next to JConsole.
BE aware that these tools have limited capability, I believe jvisualvm will make it to profile an application up to 65k classes, if you want to go bigger you should try JProfiler, Netbeans Profiler, or Yourkit maybe even Solaris Studio. It all depends on your platform and taste.
In Java profiling, it seems like all (free) roads nowadays lead to the VisualVM profiler included with JDK6. It looks like a fine program, and everyone touts how you can "attach it to a running process" as a major feature. The problem is, that seems to be the only way to use it on a local process. I want to be able to start my program in the profiler, and track its entire execution.
I have tried using the -Xrunjdwp option described in how to profile application startup with visualvm, but between the two transport methods (shared memory and server), neither is useful for me. VisualVM doesn't seem to have any integration with the former, and VisualVM refuses to connect to localhost or 127.0.0.1, so the latter is no good either. I also tried inserting a simple read of System.in into my program to insert a pause in execution, but in that case VisualVM blocks until the read completes, and doesn't allow you to start profiling until after execution is under way. I have also tried looking into the Eclipse plugin but the website is full of dead links and the launcher just crashes with a NullPointerException when I try to use it (this may no longer be accurate).
Coming from C, this doesn't seem like a particularly difficult task to me. Am I just missing something or is this really an impossible request? I'm open to any kinds of suggestions, including using a different (also free) profiler, and I'm not averse to the command line.
Consider using HPROF and opening the data file with a tool like HPjmeter - or just reading the resulting text file in your favorite editor.
Command used: javac -J-agentlib:hprof=heap=sites Hello.java
SITES BEGIN (ordered by live bytes) Fri Oct 22 11:52:24 2004
percent live alloc'ed stack class rank self accum bytes objs bytes objs trace name
1 44.73% 44.73% 1161280 14516 1161280 14516 302032 java.util.zip.ZipEntry
2 8.95% 53.67% 232256 14516 232256 14516 302033 com.sun.tools.javac.util.List
3 5.06% 58.74% 131504 2 131504 2 301029 com.sun.tools.javac.util.Name[]
4 5.05% 63.79% 131088 1 131088 1 301030 byte[]
5 5.05% 68.84% 131072 1 131072 1 301710 byte[]
HPROF is capable of presenting CPU usage, heap allocation statistics,
and monitor contention profiles. In addition, it can also report
complete heap dumps and states of all the monitors and threads in the
Java virtual machine.
The best way to solve this problem without modifying your application, is to not use VisualVM at all. As far as other free options are concerned, you could use either Eclipse TPTP or the Netbeans profiler, or whatever comes with your IDE.
If you can modify your application, to suspend it's state while you setup the profiler in VisualVM, it is quite possible to do so, using the VisualVM Eclipse plugin. I'm not sure why you are getting the NullPointerException, since it appears to work on my workstation. You'll need to configure the plugin by providing the path to the jvisualvm binary and the path of the JDK; this is done by visiting the VisualVM configuration dialog at Windows -> Preferences -> Run/Debug - > Launching -> VisualVM Configuration (as shown in the below screenshot).
You'll also need to configure your application to start with the VisualVM launcher, instead of the default JDT launcher.
All application launches from Eclipse, will now result in VisualVM tracking the new local JVM automatically, provided that VisualVM is already running. If you do not have VisualVM running, then the plugin will launch VisualVM, but it will also continue running the application.
Inferring from the previous sentence, it is evident that having the application halt in the main() method before performing any processing is quite useful. But, that is not the main reason for suspending the application. Apparently, VisualVM or its Eclipse plugin does not allow for automatically starting the CPU or memory profilers. This would mean that these profilers would have to be started manually, thereby necessitating the need to suspend the application.
Additionally, it is worth noting that adding the flags: -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y to the JVM startup will not help you in the case of VisualVM, to suspend the application and setup up the profilers. The flags are meant to help you in the case of profilers that can actually connect to the open port of the JVM, using the JDWP protocol. VisualVM does not use this protocol and therefore you would have to connect to the application using JDB or a remote debugger; but that would not resolve the problem associated with profiler configuration, as VisualVM (at least as of Java 6 update 26) does not allow you to configure the profilers on a suspended process as it simply does not display the Profiler tab.
This is now possible with the startup profiler plugin to VisualVM.
The advice with -Xrunjdwp is incorrect. It just enables debugger and with suspend=y it waits for debugger to attach. Since VisualVM is not debugger, it does not help you. However inserting System.in or Thread.sleep() will pause the startup and allows VisualVM to attach to your application. Be sure to read Profiling with VisualVM 1 and Profiling with VisualVM 2 to better understand profiler settings. Note also that instead of profiling, you can use 'Sampler' tab in VisualVM, which is more suitable for profiling entire java program execution. As other mentioned you can also use NetBeans Profiler, which directly support profiling of the application startup.
I am trying to use VisualVM to profile a Java (Sun JDK 1.6) standalone application. I have a scripted performance test environment, where I can run my application and get it to report some metrics I care about.
Is there some way to get JVM to collect some CPU profiling snapshot which I can later analyze with VisualVM?
I am looking for something similar to -XX:+HeapDumpOnOutOfMemoryError flag which writes a heap dump to disk just before an OutOfMemoryError is thrown.
There is the hprof tool built into the JVM (http://java.sun.com/developer/technicalArticles/Programming/HPROF.html) which allows you to capture basic profiling information, its dog slow and produces massive files.
VisualVM AFAIK does not yet have these abilities, but yourkit has the ability to do what you want though its agent, and programmatically.
Yourkit via agent line (-agentlib:yjpagent=onexit=snapshot)
http://www.yourkit.com/docs/80/help/additional_agent_options.jsp
Programmatically
http://www.yourkit.com/docs/80/api/index.html
As an aside I would suggest that you are careful with measuring CPU along with performance testing as it will definatly skew your results, have you considered looking at something like https://japex.dev.java.net/ around your core code ?
We have a curious problem with our java processes dying.
The application doesn't stacktrace, or write anything to the logs, the process just randomly dies. It's a heavily used application, but the problem only appears about once a month.
We're currently looking into using Process Monitor but any other suggestions would be welcome.
Edit:
It's a distributed Java application, running on Weblogic with an in-house web framework (Yes, this is a terrible idea, but it's been running for eight years), connecting to Oracle.
-
Out of Memory?
Our logs would catch java.lang.OutOfMemoryException, according to Brian Agnew.
Write crashes to a log? I don't think Java ever gets the chance, the death is happening at a process level, rather than Java exiting.
Can you wrap it in some shell script that captures the log files (stdout/stderr) and the exit code (which should give some indication as to how it died) ? On JVM exit you can also capture machine level stats using WMI
IF the VM itself is crashing it'll leave behind an hs_err_pid... file that contains stacktraces, machine-level debug info. You can then use that to diagnose the VM issue. See this blog entry for further information.
If the problem is related to the app's behaviour, it may be worth looking at JConsole, although from your description of the issue, this sounds much more like a low level VM issue.
(I assume you're on the latest VM for your Java version number etc.)
You can use a Linux NAGIOS Server to monitor the health of your Windows machines and services! Have a look at: nagios-monitoring-windows.
If you have such problems with your java app! You should test it and debug it! Applications shouldn't die without a trace! Look for logfiles! From which vendor is the app? Or is it self written? Try to enforce another Log4J/Logger/Debug Level. Monitor your System with cacti etc. to reduce the possibilities for such a crash. Talk to the software vendor.
Is enogh memory available? Maybe the app runs out of memory? Is it a standalone java process or a java process from a tomcat/jboss server?
Have you written down the crash times to a log? Appear they in different time-slices? Or appear they nearly time-circular?
VisualVM is a new tool which makes monitoring Java applications easier:
https://visualvm.dev.java.net/description.html
"VisualVM is a tool that provides detailed information about Java applications while they are running. It provides an intuitive graphical user interface that allows you to easily see information about multiple Java applications."