Skip to content

Commit

Permalink
use setPredefinedConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-nr committed Sep 30, 2020
1 parent b00583b commit 993080b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.management.InstanceNotFoundException;
import javax.management.JMException;
import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.ReflectionException;
Expand Down Expand Up @@ -58,7 +59,7 @@ void setup() {
}
}

void loop(Duration harvestInterval) throws IOException {
void loop(Duration harvestInterval) throws IOException, JMException {
while (!shutdown) {
try {
TimeUnit.MILLISECONDS.sleep(harvestInterval.toMillis());
Expand Down Expand Up @@ -90,9 +91,7 @@ void loop(Duration harvestInterval) throws IOException {
executorService.shutdown();
}

private void restartRecording()
throws IOException, MalformedObjectNameException, ReflectionException,
InstanceNotFoundException, MBeanException, OpenDataException {
private void restartRecording() throws IOException, JMException {
recorder = JFRJMXRecorder.connectWithBackOff(config);
recorder.startRecordingWithBackOff();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
Expand All @@ -21,7 +20,6 @@
import javax.management.*;
import javax.management.openmbean.*;
import javax.management.remote.JMXServiceURL;
import jdk.jfr.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -63,15 +61,11 @@ private static JFRJMXRecorder connectWithBackOff(DaemonConfig config, int backof
}
}

public void startRecordingWithBackOff()
throws MalformedObjectNameException, ReflectionException, IOException,
InstanceNotFoundException, MBeanException, OpenDataException {
public void startRecordingWithBackOff() throws JMException, IOException {
startRecordingWithBackOff(0);
}

public void startRecordingWithBackOff(int backoffIndex)
throws MalformedObjectNameException, ReflectionException, IOException,
InstanceNotFoundException, MBeanException, OpenDataException {
public void startRecordingWithBackOff(int backoffIndex) throws JMException, IOException {
try {
startRecording();
} catch (Exception e) {
Expand Down Expand Up @@ -113,28 +107,17 @@ static JFRJMXRecorder connect(DaemonConfig config) throws IOException {
return new JFRJMXRecorder(connection, config.getHarvestInterval(), config.streamFromJmx());
}

void startRecording()
throws MalformedObjectNameException, MBeanException, InstanceNotFoundException,
ReflectionException, IOException, OpenDataException {
void startRecording() throws JMException, IOException {
logger.debug("In startRecording()");

final var objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder");
ObjectName objectName = makeFlightRecorderObjectName();
var o = connection.invoke(objectName, "newRecording", new Object[] {}, new String[] {});
if (!(o instanceof Long)) {
throw new RuntimeException("JMX returned something that wasn't a Long: " + o);
}
recordingId = (Long) o;

try {
final String content = Configuration.getConfiguration("profile").getContents();
connection.invoke(
objectName,
"setConfiguration",
new Object[] {recordingId, content},
new String[] {"long", "java.lang.String"});
} catch (ParseException e) {
// TODO: Something
}
configureDefaultProfile();

var maxAge = (harvestCycleDuration.toSeconds() + 10) + "s";
Map<String, String> options = new HashMap<>();
Expand All @@ -152,6 +135,19 @@ void startRecording()
objectName, "startRecording", new Object[] {recordingId}, new String[] {"long"});
}

private void configureDefaultProfile() throws IOException, JMException {
ObjectName objectName = makeFlightRecorderObjectName();
connection.invoke(
objectName,
"setPredefinedConfiguration",
new Object[] {recordingId, "profile"},
new String[] {"long", "java.lang.String"});
}

private ObjectName makeFlightRecorderObjectName() throws MalformedObjectNameException {
return new ObjectName("jdk.management.jfr:type=FlightRecorder");
}

/**
* Stores JFR recording data in a local file on disk. The data is either streamed over JMX or
* copied from a shared filesystem.
Expand Down Expand Up @@ -185,7 +181,7 @@ Path streamRecordingToFile()
throws MalformedObjectNameException, ReflectionException, MBeanException,
InstanceNotFoundException, IOException, OpenDataException {

var objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder");
ObjectName objectName = makeFlightRecorderObjectName();
var oClone =
connection.invoke(
objectName,
Expand Down Expand Up @@ -273,7 +269,7 @@ private TabularDataSupport makeOpenData(final Map<String, String> options)
Path copyRecordingToFile()
throws MalformedObjectNameException, MBeanException, InstanceNotFoundException,
ReflectionException, IOException {
var objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder");
ObjectName objectName = makeFlightRecorderObjectName();

var oClone =
connection.invoke(
Expand Down

0 comments on commit 993080b

Please sign in to comment.