Skip to content

Commit

Permalink
8216303: JFR: Simplify generated files
Browse files Browse the repository at this point in the history
Reviewed-by: erikj, mgronlun
  • Loading branch information
egahlin committed May 29, 2020
1 parent 02fbf44 commit 6fd4490
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 153 deletions.
201 changes: 131 additions & 70 deletions make/src/classes/build/tools/jfr/GenerateJfrFiles.java
Expand Up @@ -7,10 +7,10 @@
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringJoiner;
import java.util.function.Predicate;

Expand Down Expand Up @@ -43,10 +43,11 @@ public static void main(String... args) throws Exception {
metadata.verify();
metadata.wireUpTypes();

TypeCounter typeCounter = new TypeCounter();
printJfrEventIdsHpp(metadata, typeCounter, outputDirectory);
printJfrTypesHpp(metadata, typeCounter, outputDirectory);
printJfrPeriodicHpp(metadata, outputDirectory);
printJfrEventIdsHpp(metadata, outputDirectory);
printJfrEventControlHpp(metadata, outputDirectory);
printJfrTypesHpp(metadata, outputDirectory);
printJfrEventControlHpp(metadata, typeCounter, outputDirectory);
printJfrEventClassesHpp(metadata, outputDirectory);

} catch (Exception e) {
Expand All @@ -55,12 +56,78 @@ public static void main(String... args) throws Exception {
}
}

static class TypeCounter {
final static long RESERVED_EVENT_COUNT = 2;
long typeId = -1;
long eventId = -1;
long eventCount = 0;
String firstTypeName;
String lastTypeName;
String firstEventName;
String lastEventname;

public long nextEventId(String name) {
eventCount++;
if (eventId == -1) {
eventId = firstEventId();
firstEventName = lastEventname = name;
return eventId;
}
lastEventname = name;
return ++eventId;
}

public long nextTypeId(String typeName) {
if (typeId == -1) {
lastTypeName = firstTypeName = typeName;
typeId = lastEventId();
}
lastTypeName = typeName;
return ++typeId;
}

public long firstEventId() {
return RESERVED_EVENT_COUNT;
}

public long lastEventId() {
return eventId == -1 ? firstEventId() : eventId;
}

public long eventCount() {
return eventCount;
}

public String firstTypeName() {
return firstTypeName;
}

public String lastTypeName() {
return lastTypeName;
}

public String firstEventName() {
return firstEventName;
}

public String lastEventName() {
return lastEventname;
}
}

static class XmlType {
final String name;
final String fieldType;
final String parameterType;
XmlType(String fieldType, String parameterType) {
final String javaType;
final boolean unsigned;

XmlType(String name, String fieldType, String parameterType, String javaType, boolean unsigned) {
this.name = name;
this.fieldType = fieldType;
this.parameterType = parameterType;
this.javaType = javaType;
this.unsigned = unsigned;
}
}

Expand All @@ -74,7 +141,7 @@ static class TypeElement {

static class Metadata {
final Map<String, TypeElement> types = new LinkedHashMap<>();
final Map<String, XmlType> xmlTypes = new HashMap<>();
final Map<String, XmlType> xmlTypes = new LinkedHashMap<>();
Metadata(File metadataXml, File metadataSchema) throws ParserConfigurationException, SAXException, FileNotFoundException, IOException {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
SAXParserFactory factory = SAXParserFactory.newInstance();
Expand Down Expand Up @@ -110,12 +177,8 @@ List<EventElement> getPeriodicEvents() {
return getList(t -> t.getClass() == EventElement.class && ((EventElement) t).periodic);
}

List<TypeElement> getNonEventsAndNonStructs() {
return getList(t -> t.getClass() != EventElement.class && !t.supportStruct);
}

List<TypeElement> getTypes() {
return getList(t -> t.getClass() == TypeElement.class && !t.supportStruct);
return getList(t -> t.getClass() == TypeElement.class);
}

List<TypeElement> getStructs() {
Expand Down Expand Up @@ -213,8 +276,11 @@ public void startElement(String uri, String localName, String qName, Attributes
String name = attributes.getValue("name");
String parameterType = attributes.getValue("parameterType");
String fieldType = attributes.getValue("fieldType");
metadata.xmlTypes.put(name, new XmlType(fieldType, parameterType));
String javaType = attributes.getValue("javaType");
boolean unsigned = getBoolean(attributes, "unsigned", false);
metadata.xmlTypes.put(name, new XmlType(name, fieldType, parameterType, javaType, unsigned));
break;
case "Relation":
case "Type":
currentType = new TypeElement();
currentType.name = attributes.getValue("name");
Expand Down Expand Up @@ -247,6 +313,7 @@ private boolean getBoolean(Attributes attributes, String name, boolean defaultVa
@Override
public void endElement(String uri, String localName, String qName) {
switch (qName) {
case "Relation":
case "Type":
case "Event":
metadata.types.put(currentType.name, currentType);
Expand Down Expand Up @@ -318,7 +385,7 @@ private static void printJfrPeriodicHpp(Metadata metadata, File outputDirectory)
}
}

private static void printJfrEventControlHpp(Metadata metadata, File outputDirectory) throws Exception {
private static void printJfrEventControlHpp(Metadata metadata, TypeCounter typeCounter, File outputDirectory) throws Exception {
try (Printer out = new Printer(outputDirectory, "jfrEventControl.hpp")) {
out.write("#ifndef JFRFILES_JFR_NATIVE_EVENTSETTING_HPP");
out.write("#define JFRFILES_JFR_NATIVE_EVENTSETTING_HPP");
Expand All @@ -342,11 +409,11 @@ private static void printJfrEventControlHpp(Metadata metadata, File outputDirect
out.write("");
out.write("union JfrNativeSettings {");
out.write(" // Array version.");
out.write(" jfrNativeEventSetting bits[MaxJfrEventId];");
out.write(" jfrNativeEventSetting bits[NUMBER_OF_EVENTS];");
out.write(" // Then, to make it easy to debug,");
out.write(" // add named struct members also.");
out.write(" struct {");
out.write(" jfrNativeEventSetting pad[NUM_RESERVED_EVENTS];");
out.write(" jfrNativeEventSetting pad[NUMBER_OF_RESERVED_EVENTS];");
for (TypeElement t : metadata.getEventsAndStructs()) {
out.write(" jfrNativeEventSetting " + t.name + ";");
}
Expand All @@ -358,95 +425,89 @@ private static void printJfrEventControlHpp(Metadata metadata, File outputDirect
}
}

private static void printJfrEventIdsHpp(Metadata metadata, File outputDirectory) throws Exception {
private static void printJfrEventIdsHpp(Metadata metadata, TypeCounter typeCounter, File outputDirectory) throws Exception {
try (Printer out = new Printer(outputDirectory, "jfrEventIds.hpp")) {
out.write("#ifndef JFRFILES_JFREVENTIDS_HPP");
out.write("#define JFRFILES_JFREVENTIDS_HPP");
out.write("");
out.write("#include \"utilities/macros.hpp\"");
out.write("#if INCLUDE_JFR");
out.write("#include \"jfrfiles/jfrTypes.hpp\"");
out.write("");
out.write("/**");
out.write(" * Enum of the event types in the JVM");
out.write(" */");
out.write("enum JfrEventId {");
out.write(" _jfreventbase = (NUM_RESERVED_EVENTS-1), // Make sure we start at right index.");
out.write(" ");
out.write(" // Events -> enum entry");
for (TypeElement t : metadata.getEventsAndStructs()) {
out.write(" Jfr" + t.name + "Event,");
out.write(" JfrMetadataEvent = 0,");
out.write(" JfrCheckpointEvent = 1,");
for (TypeElement t : metadata.getEvents()) {
String name = "Jfr" + t.name +"Event";
out.write(" " + name + " = " + typeCounter.nextEventId(name) + ",");
}
out.write("");
out.write(" MaxJfrEventId");
out.write("};");
out.write("");
out.write("/**");
out.write(" * Struct types in the JVM");
out.write(" */");
out.write("enum JfrStructId {");
for (TypeElement t : metadata.getNonEventsAndNonStructs()) {
out.write(" Jfr" + t.name + "Struct,");
}
for (TypeElement t : metadata.getEventsAndStructs()) {
out.write(" Jfr" + t.name + "Struct,");
}
out.write("");
out.write(" MaxJfrStructId");
out.write("};");
out.write("");
out.write("typedef enum JfrEventId JfrEventId;");
out.write("typedef enum JfrStructId JfrStructId;");
out.write("");
out.write("static const JfrEventId FIRST_EVENT_ID = " + typeCounter.firstEventName() + ";");
out.write("static const JfrEventId LAST_EVENT_ID = " + typeCounter.lastEventName() + ";");
out.write("static const int NUMBER_OF_EVENTS = " + typeCounter.eventCount() + ";");
out.write("static const int NUMBER_OF_RESERVED_EVENTS = " + TypeCounter.RESERVED_EVENT_COUNT + ";");
out.write("#endif // INCLUDE_JFR");
out.write("#endif // JFRFILES_JFREVENTIDS_HPP");
}
}

private static void printJfrTypesHpp(Metadata metadata, File outputDirectory) throws Exception {
List<String> knownTypes = List.of("Thread", "StackTrace", "Class", "StackFrame");
private static void printJfrTypesHpp(Metadata metadata, TypeCounter typeCounter, File outputDirectory) throws Exception {
try (Printer out = new Printer(outputDirectory, "jfrTypes.hpp")) {
out.write("#ifndef JFRFILES_JFRTYPES_HPP");
out.write("#define JFRFILES_JFRTYPES_HPP");
out.write("");
out.write("#include \"utilities/macros.hpp\"");
out.write("#if INCLUDE_JFR");
out.write("");
out.write("#include <string.h>");
out.write("#include \"memory/allocation.hpp\"");
out.write("");
out.write("enum JfrTypeId {");
out.write(" TYPE_NONE = 0,");
out.write(" TYPE_CLASS = 20,");
out.write(" TYPE_STRING = 21,");
out.write(" TYPE_THREAD = 22,");
out.write(" TYPE_STACKTRACE = 23,");
out.write(" TYPE_BYTES = 24,");
out.write(" TYPE_EPOCHMILLIS = 25,");
out.write(" TYPE_MILLIS = 26,");
out.write(" TYPE_NANOS = 27,");
out.write(" TYPE_TICKS = 28,");
out.write(" TYPE_ADDRESS = 29,");
out.write(" TYPE_PERCENTAGE = 30,");
out.write(" TYPE_DUMMY,");
out.write(" TYPE_DUMMY_1,");
Map<String, XmlType> javaTypes = new LinkedHashMap<>();
for (var t : metadata.xmlTypes.entrySet()) {
String name = t.getKey();
XmlType xmlType = t.getValue();
if (xmlType.javaType != null && !xmlType.unsigned) {
String typeName = "TYPE_" + name.toUpperCase();
long typeId = typeCounter.nextTypeId(typeName);
out.write(" " + typeName + " = " + typeId + ",");
javaTypes.put(name, xmlType);
}
}
for (TypeElement type : metadata.getTypes()) {
if (!knownTypes.contains(type.name)) {
out.write(" TYPE_" + type.name.toUpperCase() + ",");
String name = type.name;
if (!javaTypes.containsKey(name)) {
String typeName = "TYPE_" + name.toUpperCase();
long typeId = typeCounter.nextTypeId(typeName);
out.write(" " + typeName + " = " + typeId + ",");
}
}
out.write("");
out.write(" NUM_JFR_TYPES,");
out.write(" TYPES_END = 255");
out.write("};");
out.write("");
out.write("enum ReservedEvent {");
out.write(" EVENT_METADATA,");
out.write(" EVENT_CHECKPOINT,");
out.write(" EVENT_BUFFERLOST,");
out.write(" NUM_RESERVED_EVENTS = TYPES_END");
out.write("static const JfrTypeId FIRST_TYPE_ID = " + typeCounter.firstTypeName() + ";");
out.write("static const JfrTypeId LAST_TYPE_ID = " + typeCounter.lastTypeName() + ";");

out.write("");
out.write("class JfrType : public AllStatic {");
out.write(" public:");
out.write(" static jlong name_to_id(const char* type_name) {");
for (Entry<String, XmlType> m : javaTypes.entrySet()) {
XmlType xmlType = m.getValue();
String javaName = xmlType.javaType;
String typeName = xmlType.name.toUpperCase();
out.write(" if (strcmp(type_name, \"" + javaName + "\") == 0) {");
out.write(" return TYPE_" + typeName + ";");
out.write(" }");
}
out.write(" return -1;");
out.write(" }");
out.write("};");
out.write("");
out.write("#endif // INCLUDE_JFR");
out.write("#endif // JFRFILES_JFRTYPES_HPP");
};
}
;
}

private static void printJfrEventClassesHpp(Metadata metadata, File outputDirectory) throws Exception {
Expand Down
7 changes: 7 additions & 0 deletions src/hotspot/share/jfr/jni/jfrJniMethod.cpp
Expand Up @@ -49,6 +49,7 @@
#include "jfr/utilities/jfrTime.hpp"
#include "jfr/writers/jfrJavaEventWriter.hpp"
#include "jfrfiles/jfrPeriodic.hpp"
#include "jfrfiles/jfrTypes.hpp"
#include "logging/log.hpp"
#include "memory/resourceArea.hpp"
#include "runtime/interfaceSupport.inline.hpp"
Expand Down Expand Up @@ -349,4 +350,10 @@ JVM_ENTRY_NO_ENV(jboolean, jfr_set_handler(JNIEnv * env, jobject jvm, jobject cl
return JfrJavaSupport::set_handler(clazz, handler, thread);
JVM_END

NO_TRANSITION(jlong, jfr_get_type_id_from_string(JNIEnv * env, jobject jvm, jstring type))
const char* type_name= env->GetStringUTFChars(type, NULL);
jlong id = JfrType::name_to_id(type_name);
env->ReleaseStringUTFChars(type, type_name);
return id;
NO_TRANSITION_END

1 change: 1 addition & 0 deletions src/hotspot/share/jfr/jni/jfrJniMethod.hpp
Expand Up @@ -148,6 +148,7 @@ jobject JNICALL jfr_get_handler(JNIEnv* env, jobject jvm, jobject clazz);

jboolean JNICALL jfr_set_handler(JNIEnv* env, jobject jvm, jobject clazz, jobject handler);

jlong JNICALL jfr_get_type_id_from_string(JNIEnv* env, jobject jvm, jstring type);

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion src/hotspot/share/jfr/jni/jfrJniMethodRegistration.cpp
Expand Up @@ -88,7 +88,8 @@ JfrJniMethodRegistration::JfrJniMethodRegistration(JNIEnv* env) {
(char*)"isExcluded", (char*)"(Ljava/lang/Thread;)Z", (void*)jfr_is_thread_excluded,
(char*)"getChunkStartNanos", (char*)"()J", (void*)jfr_chunk_start_nanos,
(char*)"getHandler", (char*)"(Ljava/lang/Class;)Ljava/lang/Object;", (void*)jfr_get_handler,
(char*)"setHandler", (char*)"(Ljava/lang/Class;Ljdk/jfr/internal/handlers/EventHandler;)Z", (void*)jfr_set_handler
(char*)"setHandler", (char*)"(Ljava/lang/Class;Ljdk/jfr/internal/handlers/EventHandler;)Z", (void*)jfr_set_handler,
(char*)"getTypeId", (char*)"(Ljava/lang/String;)J", (void*)jfr_get_type_id_from_string
};

const size_t method_array_length = sizeof(method) / sizeof(JNINativeMethod);
Expand Down
5 changes: 3 additions & 2 deletions src/hotspot/share/jfr/metadata/metadata.xml
Expand Up @@ -1304,8 +1304,8 @@
<XmlType name="ClassLoader" parameterType="const ClassLoaderData*" fieldType="const ClassLoaderData*"/>
<XmlType name="Method" parameterType="const Method*" fieldType="const Method*"/>
<XmlType name="Thread" javaType="java.lang.Thread" parameterType="u8" fieldType="u8"/>
<XmlType name="Tickspan" contentType="tickspan" javaType="long" parameterType="const Tickspan&amp;" fieldType="Tickspan"/>
<XmlType name="Ticks" contentType="tickstamp" javaType="long" parameterType="const Ticks&amp;" fieldType="Ticks"/>
<XmlType name="Tickspan" contentType="tickspan" unsigned="true" javaType="long" parameterType="const Tickspan&amp;" fieldType="Tickspan"/>
<XmlType name="Ticks" contentType="tickstamp" unsigned="true" javaType="long" parameterType="const Ticks&amp;" fieldType="Ticks"/>
<XmlType name="ulong" javaType="long" unsigned="true" parameterType="u8" fieldType="u8"/>
<XmlType name="uint" javaType="int" unsigned="true" parameterType="unsigned" fieldType="unsigned"/>
<XmlType name="ushort" javaType="short" unsigned="true" parameterType="u2" fieldType="u2"/>
Expand All @@ -1319,6 +1319,7 @@
<XmlType name="boolean" javaType="boolean" parameterType="bool" fieldType="bool"/>
<XmlType name="char" javaType="char" parameterType="char" fieldType="char"/>
<XmlType name="string" javaType="java.lang.String" parameterType="const char*" fieldType="const char*"/>
<XmlType name="StackTrace" javaType="jdk.types.StackTrace" parameterType="u8" fieldType="u8"/>

<XmlContentType name="bytes" annotation="jdk.jfr.DataAmount(BYTES)" />
<XmlContentType name="tickstamp" annotation="jdk.jfr.Timestamp(TICKS)" />
Expand Down
Expand Up @@ -126,7 +126,8 @@ void JfrCheckpointWriter::release() {
}

void JfrCheckpointWriter::write_type(JfrTypeId type_id) {
assert(type_id < TYPES_END, "invariant");
assert(type_id <= LAST_TYPE_ID, "type id overflow invariant");
assert(type_id >= FIRST_TYPE_ID, "type id underflow invariant");
write<u8>(type_id);
increment();
}
Expand Down

0 comments on commit 6fd4490

Please sign in to comment.