JDK-8273060 : Flight Recorder samples too less Event
  • Type: Bug
  • Component: hotspot
  • Sub-Component: jfr
  • Affected Version: 17
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2021-08-18
  • Updated: 2023-08-15
  • Resolved: 2021-08-27
Related Reports
Duplicate :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
JDK 11, JDK 175, JDK 17 tested.

A DESCRIPTION OF THE PROBLEM :
Math.sin() is only hit ~ 0% with jdk.ExecutionSample
StrictMath.sin() is only hit <90% with jdk.NativeMethodSample

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute a Javawith heave Math.sin() use and sample it with flight recorder. There are always less samples then configured.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Produce RunDuration/SamplePeriod Samples. Either as native or java sample.
ACTUAL -
Some math is not shown in neither native nor java samples. (0% java, 86% native)

---------- BEGIN SOURCE ----------
package test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

import jdk.jfr.Configuration;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordingFile;

public class Start {
	private static final Path DESTINATION = Paths.get("mySnapshot.jfr");
	volatile static boolean stop;

	public static void main(String[] args) throws IOException, ParseException, InterruptedException {
		new Thread("calculateJavaNativeExample") {
			@Override
			public void run() {
				while (!stop) {
					if (!calculateJavaNativeExample()) {
						stop = true;
					}
				}
			}

		}.start();
		new Thread("calculateDoubleExample") {
			@Override
			public void run() {
				while (!stop) {
					if (!calculateDoubleExample()) {
						stop = true;
					}
				}
			}

		}.start();

		Configuration configuration = Configuration.getConfiguration("default");
		int millis = 100;
		int samples = 100;
		for (int run = 0; run < 2; run++) { // multiple tries to give the Compiler a chance to compile
			try (jdk.jfr.Recording r = new jdk.jfr.Recording(configuration)) {
				r.setMaxSize(0); // unlimited
				r.setMaxAge(null);// unlimited
				r.setDumpOnExit(false);
				r.setToDisk(false);
				// "Method Profiling Sample":
				r.enable("jdk.ExecutionSample").withPeriod(Duration.ofMillis(millis));
				// "Method Profiling Sample Native":
				r.enable("jdk.NativeMethodSample").withPeriod(Duration.ofMillis(millis));
				r.start();
				Thread.sleep(millis * samples); // => 10sec
				r.stop();
				if (r.getSize() > 0) {
					r.dump(DESTINATION);
				}
			}
		}
		if (stop) {
			System.out.println("ERROR");
		} else {
			stop = true;
			System.out.println("OK");
		}
		List<RecordedEvent> allEvents = new ArrayList<>();
		try (RecordingFile recordingFile = new RecordingFile(DESTINATION)) {
			while (recordingFile.hasMoreEvents()) {
				allEvents.add(recordingFile.readEvent());
			}
		}
		// should be 100 - but ~0:
		long javaSamples = allEvents.stream().filter(t -> t.getEventType().getName().equals("jdk.ExecutionSample")).count();
		System.out.println("#jdk.ExecutionSample:" + javaSamples + " should be " + samples);

		// should be 100 - but <90:
		long nativeSamples = allEvents.stream().filter(t -> t.getEventType().getName().equals("jdk.NativeMethodSample")).count();
		System.out.println("#jdk.NativeMethodSample:" + nativeSamples + " should be " + samples);
	}

	static boolean calculateJavaNativeExample() {
		double x = 0;
		for (long j = 0; j < 10000; j++) {
			x = StrictMath.sin(x); //should result in NativeMethodSample
		}
		return x <= 1.0;
	}

	static boolean calculateSystemNativeExample() {
		File file = new File("does not exit");
		return !file.exists();
	}

	static int i;

	static boolean calculateIntExample() {
		int n = (i & 0xFF) + 1;
		// Collatz Problem
		while (n != 1) {
			if ((n & 1) == 0) {
				n = n / 2;
			} else {
				n = n * 3 + 1;
			}
		}
		i++;
		return n != 0;
	}

	static boolean calculateDoubleExample() {
		double x = 0;
		for (long j = 0; j < 10000; j++) {
			x = Math.sin(x); // @HotSpotIntrinsic //should result in ExecutionSample
		}
		return x <= 1.0;
	}

}

---------- END SOURCE ----------

FREQUENCY : always



Comments
Flight recorder is sampling less events for native samples. OS: Windows 10 JDK 17: Fail Output: OK #jdk.ExecutionSample:141 should be 100 #jdk.NativeMethodSample:89 should be 100 Closing it as duplicate of https://bugs.openjdk.java.net/browse/JDK-8244514
27-08-2021