/*-------------------------------------------------------------------------
*
* Copyright (c) 2003-2015, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/

package org.postgresql.benchmark.profilers;

import org.openjdk.jmh.infra.BenchmarkParams;
import org.openjdk.jmh.infra.IterationParams;
import org.openjdk.jmh.profile.ExternalProfiler;
import org.openjdk.jmh.results.BenchmarkResult;
import org.openjdk.jmh.results.Result;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * Captures Flight Recorder log. Note: Flight Recorder is available in OracleJDK only. Usage of
 * Flight Recorder in production requires a LICENSE FEE, however Flight Recorder is free for use in
 * test systems. It is assumed you would not use pgjdbc benchmarks for running a production system,
 * thus it is believed to be safe.
 */
public class FlightRecorderProfiler implements ExternalProfiler {
  @Override
  public Collection<String> addJVMInvokeOptions(BenchmarkParams params) {
    return Collections.emptyList();
  }

  @Override
  public Collection<String> addJVMOptions(BenchmarkParams params) {
    StringBuilder sb = new StringBuilder();
    for (String param : params.getParamsKeys()) {
      if (sb.length() != 0) {
        sb.append('-');
      }
      sb.append(param).append('-').append(params.getParam(param));
    }

    long duration =
        getDurationSeconds(params.getWarmup()) + getDurationSeconds(params.getMeasurement());
    return Arrays.asList(
        "-XX:+UnlockCommercialFeatures", "-XX:+FlightRecorder",
        "-XX:StartFlightRecording=settings=profile,duration=" + duration + "s,filename="
            + params.getBenchmark() + "_" + sb + ".jfr");
  }

  private static long getDurationSeconds(IterationParams warmup) {
    return warmup.getTime().convertTo(TimeUnit.SECONDS) * warmup.getCount();
  }

  @Override
  public void beforeTrial(BenchmarkParams benchmarkParams) {

  }

  @Override
  public Collection<? extends Result> afterTrial(BenchmarkResult br, long pid, File stdOut,
      File stdErr) {
    return Collections.emptyList();
  }

  @Override
  public boolean allowPrintOut() {
    return true;
  }

  @Override
  public boolean allowPrintErr() {
    return true;
  }

  @Override
  public String getDescription() {
    return "Collects Java Flight Recorder profile";
  }
}

@@ -534,17 +534,20 @@
 il.Array
-s
+List
 ;%0Aimport
@@ -599,16 +599,39 @@
 ctions;%0A
+import java.util.List;%0A
 import j
@@ -1625,38 +1625,60 @@
     
-return Arrays.asList(%0A        
+List%3CString%3E opts = new ArrayList%3C%3E();%0A    opts.add(
 %22-XX
@@ -1704,18 +1704,32 @@
 eatures%22
-, 
+);%0A    opts.add(
 %22-XX:+Fl
@@ -1745,18 +1745,238 @@
 der%22
-,
+);%0A    if (!System.getProperty(%22java.version%22).startsWith(%221.7%22)) %7B%0A      // DebugNonSafepoints requires java 1.8u40+%0A      opts.add(%22-XX:+UnlockDiagnosticVMOptions%22);%0A      opts.add(%22-XX:+DebugNonSafepoints%22);
 %0A    
+%7D%0A
     
+opts.add(
 %22-XX
@@ -2060,20 +2060,16 @@
         
-    
 + params
@@ -2106,16 +2106,33 @@
 .jfr%22);%0A
+    return opts;%0A
   %7D%0A%0A  p

add DebugNonSafepoints to FlightRecorderProfiler