在 Mac 上分析 Java 中的 CPU 使用情况
我正在寻找一种方法来测量我的java代码中不同方法的CPU使用率。我知道这可以使用 JNI 和 C 来实现,但我不知道从哪里开始......
这样做的目的是比较不同的算法,并提供定性结果。
I'm looking for a way to measure the cpu usage for different methods in my java code. I understand that this can be achieved using JNI and C, but I wouldn't know where to start...
The purpose of this is to compare different algorithms, and provide qualitative results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最常见的方法可能是使用采样。 JVM 提供了一些工具来询问所有线程(或您感兴趣的线程)的当前堆栈跟踪,以及它们消耗了多少 CPU。所以你定期这样做。在每次调用时,如果线程位于您感兴趣的方法内,则假设自该方法内上次轮询以来它已花费了报告的 CPU 时间的一半。
如果这种方法听起来合适,不久前我写了一些关于 Java 5 分析工具的材料 这可能对你有帮助。
Java 5 还提供了一个 Instrumentation 框架,通过该框架,您可以在加载类时对其进行修改,以包括对给定方法的入口和出口的调用,以便您可以测量该方法内部的 CPU 使用情况。然而,这编程起来有点复杂,因为您需要在加载实际的类二进制文件时对其进行修改。
Probably the most common way is to use sampling. The JVM provides facilities to ask it the current stack trace of all threads (or ones you're interested in), along with how much CPU they've consumed. So you periodically do this. On each call, if a thread is inside the method you're interested in, then assume that it's spent half of the reported CPU time since the last poll inside that method.
If this method sounds appropriate, a little while back I wrote some material on the Java 5 profiling facilities that might help you.
Java 5 also provides an Instrumentation framework, by which you can doctor classes as they're being loaded in to include calls on the entry and exit to your given method, so you can measure CPU usage just inside that method. However, this is a little more complex to program because you need to doctor the actual class binaries as they're being loaded.
我认为您无法使用当前的分析器范围真正识别方法级别的 CPU 使用情况。对于大多数方法来说,这是非常明显的(如果该方法是计算密集型且单线程的,那么它将使用 100% 的 CPU,并由操作系统分配)。
不过,您可能想要识别热点(消耗 CPU 的方法比您预期的更多 - 或者可能更少?),我建议您查看 YourKit 是一个易于配置的分析器。
如果做不到这一点,请查看 JVM 分析界面 ( JVMPI),这可能会给您一些进一步的指导。
I don't think you can really identify CPU usage down to the method level with the current range of profilers. For most methods it's pretty obvious (if the method is compute-bound and single-threaded then it'll use 100% of CPU subject to allocation by the OS).
You may want to identify hot-spots though (methods consuming more CPU than you'd anticipate - or possibly less?) and I'd recommend looking at YourKit for an easy-to-configure profiler.
Failing that, take a look at the JVM Profiling Interface (JVMPI), which may give you some further pointers.
Sun VisualVM 集成在最新的 JDK 中,其 分析功能此处解释。请注意,如果我理解这个正确。
Netbeans 上有基本上相同的分析机制,我不知道这是否有任何帮助。
Sun VisualVM is integrated in recent JDK's and its profiling capabilities are explained here. Note that it seems to require a pretty up to date version of OSX if I understand this correctly.
Netbeans has basically the same profiling machinery on board, I don't know if that's of any help.
如果您想使用现有的分析工具之一,那么您可以尝试 鲨鱼
If you want to use one of already available profiling tools, then you can try Shark
看看
OperatingSystemMXBean
也许你可以看看你的方法之前和之后的东西只Java6
Have a look at the
OperatingSystemMXBean
perhaps you could look at something before and after your methodJava6 only
我不确定这是否是您想要的,但我过去曾使用 jrat 进行分析取得了不错的成绩。
I I'm not sure if this is what you want but I've used jrat for profiling in the past with decent results.
检查 JaMon。非常容易使用。
Check JaMon. Is very easy to use.
在单独的线程中调用您的方法并测量给定过程执行之前和之后的时间增量。
要测量进程调用的时间:
Call your methods in separate threads and measure the delta of time before and after execution of given procedure.
To measure the time of a process call: