[PATCH] perf c2c jmh profiler

Carter Kozak ckozak at ckozak.net
Tue May 26 18:22:19 UTC 2020


My OCA has been submitted and successfully processed.
I can add additional configuration flags matching the perf profiler if you like, or we can improve later. I defer to your judgement.
I didn't see any unit testing for other perf profilers, so I haven't added anything for this one as perf requires system configuration that may not be enabled on test instances.
Feedback is appreciated, thanks!

# HG changeset patch
# User Carter Kozak <ckozak at ckozak.net>
# Date 1590515846 14400
#      Tue May 26 13:57:26 2020 -0400
# Node ID 81ede3eec17301b0f7684e66c5614e92f1925219
# Parent  b6f87aa2a687549200ee54ff0840622787fd2aed
Implement LinuxPerfCacheToCacheProfiler wrapper around perf c2c

Perf c2c is a relatively new perf component which provides a
means to analyze cache contention.

This is a simple implementation with limited configurability in
the initial version.

diff -r b6f87aa2a687 -r 81ede3eec173 jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfCacheToCacheProfiler.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfCacheToCacheProfiler.java	Tue May 26 13:57:26 2020 -0400
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.profile;
+
+import org.openjdk.jmh.infra.BenchmarkParams;
+import org.openjdk.jmh.results.AggregationPolicy;
+import org.openjdk.jmh.results.Aggregator;
+import org.openjdk.jmh.results.BenchmarkResult;
+import org.openjdk.jmh.results.Defaults;
+import org.openjdk.jmh.results.Result;
+import org.openjdk.jmh.results.ResultRole;
+import org.openjdk.jmh.util.FileUtils;
+import org.openjdk.jmh.util.TempFile;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.concurrent.TimeUnit;
+
+public final class LinuxPerfCacheToCacheProfiler implements ExternalProfiler {
+
+    protected final TempFile perfBinData;
+
+    public LinuxPerfCacheToCacheProfiler() throws ProfilerException {
+        try {
+            perfBinData = FileUtils.weakTempFile("perf-c2c-bin");
+        } catch (IOException e) {
+            throw new ProfilerException(e);
+        }
+    }
+
+    @Override
+    public Collection<String> addJVMInvokeOptions(BenchmarkParams params) {
+        long delay = TimeUnit.NANOSECONDS.toMillis(params.getWarmup().getCount() *
+                params.getWarmup().getTime().convertTo(TimeUnit.NANOSECONDS))
+                + TimeUnit.SECONDS.toMillis(1); // loosely account for the JVM lag
+        return new ArrayList<>(Arrays.asList(
+                "perf", "c2c", "record", "-o", perfBinData.getAbsolutePath(), "--", "--delay", String.valueOf(delay)));
+    }
+
+    @Override
+    public Collection<String> addJVMOptions(BenchmarkParams params) {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public void beforeTrial(BenchmarkParams _params) {}
+
+    @Override
+    public Collection<? extends Result> afterTrial(BenchmarkResult br, long pid, File stdOut, File stdErr) {
+        try {
+            Process process = new ProcessBuilder("perf", "c2c", "report", "--stats", "-i", perfBinData.getAbsolutePath())
+                    .redirectErrorStream(true)
+                    .start();
+            try (InputStream stream = process.getInputStream();
+                 Reader isReader = new InputStreamReader(stream, StandardCharsets.UTF_8);
+                 BufferedReader reader = new BufferedReader(isReader)) {
+                StringBuilder output = new StringBuilder();
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    output.append(line).append('\n');
+                }
+                return Collections.singleton(new PerfCacheToCaceResult(output.toString()));
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public boolean allowPrintOut() {
+        return false;
+    }
+
+    @Override
+    public boolean allowPrintErr() {
+        return false;
+    }
+
+    @Override
+    public String getDescription() {
+        return "Linux perf cache to cache (c2c) statistics";
+    }
+
+    static class PerfCacheToCaceResult extends Result<PerfCacheToCaceResult> {
+        private static final long serialVersionUID = 1L;
+
+        private final String output;
+
+        public PerfCacheToCaceResult(String output) {
+            super(ResultRole.SECONDARY, Defaults.PREFIX + "perf-c2c", of(Double.NaN), "---", AggregationPolicy.AVG);
+            this.output = output;
+        }
+
+        @Override
+        protected Aggregator<PerfCacheToCaceResult> getThreadAggregator() {
+            return new PerfResultAggregator();
+        }
+
+        @Override
+        protected Aggregator<PerfCacheToCaceResult> getIterationAggregator() {
+            return new PerfResultAggregator();
+        }
+
+        @Override
+        protected Collection<? extends Result> getDerivativeResults() {
+            return Collections.emptyList();
+        }
+
+        @Override
+        public String toString() {
+            return "perf c2c";
+        }
+
+        @Override
+        public String extendedInfo() {
+            return "Perf C2C report (stats):\n--------------------------------------------------\n" + output;
+        }
+    }
+
+    static class PerfResultAggregator implements Aggregator<PerfCacheToCaceResult> {
+
+        @Override
+        public PerfCacheToCaceResult aggregate(Collection<PerfCacheToCaceResult> results) {
+            return new PerfCacheToCaceResult(joinOutput(results));
+        }
+
+        private static String joinOutput(Collection<PerfCacheToCaceResult> results) {
+            StringBuilder buf = new StringBuilder();
+            Iterator<PerfCacheToCaceResult> resultIterator = results.iterator();
+            while (resultIterator.hasNext()) {
+                buf.append(resultIterator.next().output);
+                if (resultIterator.hasNext()) {
+                    buf.append("\n========================================\n");
+                }
+            }
+            return buf.toString();
+        }
+    }
+
+}
+


More information about the jmh-dev mailing list