2

我希望能够以文本格式提取通过分析 JFR 记录检测到的“热点”的某种摘要。确切地说,我想提取由于运行某些特定功能或某些特定类而产生的热点。像这样的东西:

<some tool> myfile.jfr --look-at-class='org/me/MyClass' --limit 10 --order-by self-time

MyClass并获取从in 中调用的最耗时的 10 个方法的表格org.me。我尝试查看jfr命令行工具,但它没有这样的功能。或者,江铃只有图形界面,没有命令行界面。还有其他方法可以获得这样的结果吗?

4

1 回答 1

2

使用JFR Parsing API创建这样的工具很容易。

import jdk.jfr.consumer.RecordingFile;

import java.nio.file.Paths;
import java.util.HashMap;

public class JfrTop {

    public static void main(String[] args) throws Exception {
        var fileName = args[0];
        var packageName = args.length > 1 ? args[1] : "";
        var top = args.length > 2 ? Integer.parseInt(args[2]) : 10;

        var hotMethods = new HashMap<String, Long>();
        long total = 0;

        try (var recording = new RecordingFile(Paths.get(fileName))) {
            while (recording.hasMoreEvents()) {
                var event = recording.readEvent();
                if (event.getEventType().getName().equals("jdk.ExecutionSample")) {
                    var stackTrace = event.getStackTrace();
                    if (stackTrace != null && stackTrace.getFrames().size() > 0) {
                        var method = stackTrace.getFrames().get(0).getMethod();
                        var className = method.getType().getName();
                        if (className.startsWith(packageName)) {
                            var fullName = className + '.' + method.getName() + method.getDescriptor();
                            hotMethods.compute(fullName, (key, val) -> val == null ? 1L : (val + 1));
                        }
                    }
                    total++;
                }
            }
        }

        double percent = 100.0 / total;
        hotMethods.entrySet().stream()
                .sorted((e1, e2) -> Long.compare(e2.getValue(), e1.getValue()))
                .limit(top)
                .forEachOrdered(e -> System.out.printf("%5.2f%% %s\n", e.getValue() * percent, e.getKey()));
    }
}

如何运行:

java JfrTop idea.jfr com.intellij.openapi.editor 10

样本输出:

20,35% com.intellij.openapi.editor.impl.RangeHighlighterTree$RHNode.recalculateRenderFlags()V
 4,20% com.intellij.openapi.editor.impl.IntervalTreeImpl.maxEndOf(Lcom/intellij/openapi/editor/impl/IntervalTreeImpl$IntervalNode;I)I
 3,19% com.intellij.openapi.editor.impl.IntervalTreeImpl.assertAllDeltasAreNull(Lcom/intellij/openapi/editor/impl/IntervalTreeImpl$IntervalNode;)V
 2,19% com.intellij.openapi.editor.impl.IntervalTreeImpl$IntervalNode.computeDeltaUpToRoot()I
 1,94% com.intellij.openapi.editor.impl.IntervalTreeImpl.pushDelta(Lcom/intellij/openapi/editor/impl/IntervalTreeImpl$IntervalNode;)Z
 1,63% com.intellij.openapi.editor.impl.IntervalTreeImpl$IntervalNode.hasAliveKey(Z)Z
 1,50% com.intellij.openapi.editor.impl.IntervalTreeImpl.correctMax(Lcom/intellij/openapi/editor/impl/IntervalTreeImpl$IntervalNode;I)V
 1,31% com.intellij.openapi.editor.impl.IntervalTreeImpl$1.hasNext()Z
 0,88% com.intellij.openapi.editor.impl.IntervalTreeImpl$IntervalNode.tryToSetCachedValues(IZI)Z
 0,63% com.intellij.openapi.editor.impl.IntervalTreeImpl.findOrInsert(Lcom/intellij/openapi/editor/impl/IntervalTreeImpl$IntervalNode;)Lcom/intellij/openapi/editor/impl/IntervalTreeImpl$IntervalNode;
于 2020-04-20T19:32:48.087 回答