Skip to content

Commit 6fd4490

Browse files
committedMay 29, 2020
8216303: JFR: Simplify generated files
Reviewed-by: erikj, mgronlun
1 parent 02fbf44 commit 6fd4490

File tree

16 files changed

+245
-153
lines changed

16 files changed

+245
-153
lines changed
 

‎make/src/classes/build/tools/jfr/GenerateJfrFiles.java

+131-70
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import java.io.IOException;
88
import java.io.PrintStream;
99
import java.util.ArrayList;
10-
import java.util.HashMap;
1110
import java.util.LinkedHashMap;
1211
import java.util.List;
1312
import java.util.Map;
13+
import java.util.Map.Entry;
1414
import java.util.StringJoiner;
1515
import java.util.function.Predicate;
1616

@@ -43,10 +43,11 @@ public static void main(String... args) throws Exception {
4343
metadata.verify();
4444
metadata.wireUpTypes();
4545

46+
TypeCounter typeCounter = new TypeCounter();
47+
printJfrEventIdsHpp(metadata, typeCounter, outputDirectory);
48+
printJfrTypesHpp(metadata, typeCounter, outputDirectory);
4649
printJfrPeriodicHpp(metadata, outputDirectory);
47-
printJfrEventIdsHpp(metadata, outputDirectory);
48-
printJfrEventControlHpp(metadata, outputDirectory);
49-
printJfrTypesHpp(metadata, outputDirectory);
50+
printJfrEventControlHpp(metadata, typeCounter, outputDirectory);
5051
printJfrEventClassesHpp(metadata, outputDirectory);
5152

5253
} catch (Exception e) {
@@ -55,12 +56,78 @@ public static void main(String... args) throws Exception {
5556
}
5657
}
5758

59+
static class TypeCounter {
60+
final static long RESERVED_EVENT_COUNT = 2;
61+
long typeId = -1;
62+
long eventId = -1;
63+
long eventCount = 0;
64+
String firstTypeName;
65+
String lastTypeName;
66+
String firstEventName;
67+
String lastEventname;
68+
69+
public long nextEventId(String name) {
70+
eventCount++;
71+
if (eventId == -1) {
72+
eventId = firstEventId();
73+
firstEventName = lastEventname = name;
74+
return eventId;
75+
}
76+
lastEventname = name;
77+
return ++eventId;
78+
}
79+
80+
public long nextTypeId(String typeName) {
81+
if (typeId == -1) {
82+
lastTypeName = firstTypeName = typeName;
83+
typeId = lastEventId();
84+
}
85+
lastTypeName = typeName;
86+
return ++typeId;
87+
}
88+
89+
public long firstEventId() {
90+
return RESERVED_EVENT_COUNT;
91+
}
92+
93+
public long lastEventId() {
94+
return eventId == -1 ? firstEventId() : eventId;
95+
}
96+
97+
public long eventCount() {
98+
return eventCount;
99+
}
100+
101+
public String firstTypeName() {
102+
return firstTypeName;
103+
}
104+
105+
public String lastTypeName() {
106+
return lastTypeName;
107+
}
108+
109+
public String firstEventName() {
110+
return firstEventName;
111+
}
112+
113+
public String lastEventName() {
114+
return lastEventname;
115+
}
116+
}
117+
58118
static class XmlType {
119+
final String name;
59120
final String fieldType;
60121
final String parameterType;
61-
XmlType(String fieldType, String parameterType) {
122+
final String javaType;
123+
final boolean unsigned;
124+
125+
XmlType(String name, String fieldType, String parameterType, String javaType, boolean unsigned) {
126+
this.name = name;
62127
this.fieldType = fieldType;
63128
this.parameterType = parameterType;
129+
this.javaType = javaType;
130+
this.unsigned = unsigned;
64131
}
65132
}
66133

@@ -74,7 +141,7 @@ static class TypeElement {
74141

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

113-
List<TypeElement> getNonEventsAndNonStructs() {
114-
return getList(t -> t.getClass() != EventElement.class && !t.supportStruct);
115-
}
116-
117180
List<TypeElement> getTypes() {
118-
return getList(t -> t.getClass() == TypeElement.class && !t.supportStruct);
181+
return getList(t -> t.getClass() == TypeElement.class);
119182
}
120183

121184
List<TypeElement> getStructs() {
@@ -213,8 +276,11 @@ public void startElement(String uri, String localName, String qName, Attributes
213276
String name = attributes.getValue("name");
214277
String parameterType = attributes.getValue("parameterType");
215278
String fieldType = attributes.getValue("fieldType");
216-
metadata.xmlTypes.put(name, new XmlType(fieldType, parameterType));
279+
String javaType = attributes.getValue("javaType");
280+
boolean unsigned = getBoolean(attributes, "unsigned", false);
281+
metadata.xmlTypes.put(name, new XmlType(name, fieldType, parameterType, javaType, unsigned));
217282
break;
283+
case "Relation":
218284
case "Type":
219285
currentType = new TypeElement();
220286
currentType.name = attributes.getValue("name");
@@ -247,6 +313,7 @@ private boolean getBoolean(Attributes attributes, String name, boolean defaultVa
247313
@Override
248314
public void endElement(String uri, String localName, String qName) {
249315
switch (qName) {
316+
case "Relation":
250317
case "Type":
251318
case "Event":
252319
metadata.types.put(currentType.name, currentType);
@@ -318,7 +385,7 @@ private static void printJfrPeriodicHpp(Metadata metadata, File outputDirectory)
318385
}
319386
}
320387

321-
private static void printJfrEventControlHpp(Metadata metadata, File outputDirectory) throws Exception {
388+
private static void printJfrEventControlHpp(Metadata metadata, TypeCounter typeCounter, File outputDirectory) throws Exception {
322389
try (Printer out = new Printer(outputDirectory, "jfrEventControl.hpp")) {
323390
out.write("#ifndef JFRFILES_JFR_NATIVE_EVENTSETTING_HPP");
324391
out.write("#define JFRFILES_JFR_NATIVE_EVENTSETTING_HPP");
@@ -342,11 +409,11 @@ private static void printJfrEventControlHpp(Metadata metadata, File outputDirect
342409
out.write("");
343410
out.write("union JfrNativeSettings {");
344411
out.write(" // Array version.");
345-
out.write(" jfrNativeEventSetting bits[MaxJfrEventId];");
412+
out.write(" jfrNativeEventSetting bits[NUMBER_OF_EVENTS];");
346413
out.write(" // Then, to make it easy to debug,");
347414
out.write(" // add named struct members also.");
348415
out.write(" struct {");
349-
out.write(" jfrNativeEventSetting pad[NUM_RESERVED_EVENTS];");
416+
out.write(" jfrNativeEventSetting pad[NUMBER_OF_RESERVED_EVENTS];");
350417
for (TypeElement t : metadata.getEventsAndStructs()) {
351418
out.write(" jfrNativeEventSetting " + t.name + ";");
352419
}
@@ -358,95 +425,89 @@ private static void printJfrEventControlHpp(Metadata metadata, File outputDirect
358425
}
359426
}
360427

361-
private static void printJfrEventIdsHpp(Metadata metadata, File outputDirectory) throws Exception {
428+
private static void printJfrEventIdsHpp(Metadata metadata, TypeCounter typeCounter, File outputDirectory) throws Exception {
362429
try (Printer out = new Printer(outputDirectory, "jfrEventIds.hpp")) {
363430
out.write("#ifndef JFRFILES_JFREVENTIDS_HPP");
364431
out.write("#define JFRFILES_JFREVENTIDS_HPP");
365432
out.write("");
366433
out.write("#include \"utilities/macros.hpp\"");
367434
out.write("#if INCLUDE_JFR");
368-
out.write("#include \"jfrfiles/jfrTypes.hpp\"");
369435
out.write("");
370-
out.write("/**");
371-
out.write(" * Enum of the event types in the JVM");
372-
out.write(" */");
373436
out.write("enum JfrEventId {");
374-
out.write(" _jfreventbase = (NUM_RESERVED_EVENTS-1), // Make sure we start at right index.");
375-
out.write(" ");
376-
out.write(" // Events -> enum entry");
377-
for (TypeElement t : metadata.getEventsAndStructs()) {
378-
out.write(" Jfr" + t.name + "Event,");
437+
out.write(" JfrMetadataEvent = 0,");
438+
out.write(" JfrCheckpointEvent = 1,");
439+
for (TypeElement t : metadata.getEvents()) {
440+
String name = "Jfr" + t.name +"Event";
441+
out.write(" " + name + " = " + typeCounter.nextEventId(name) + ",");
379442
}
380-
out.write("");
381-
out.write(" MaxJfrEventId");
382-
out.write("};");
383-
out.write("");
384-
out.write("/**");
385-
out.write(" * Struct types in the JVM");
386-
out.write(" */");
387-
out.write("enum JfrStructId {");
388-
for (TypeElement t : metadata.getNonEventsAndNonStructs()) {
389-
out.write(" Jfr" + t.name + "Struct,");
390-
}
391-
for (TypeElement t : metadata.getEventsAndStructs()) {
392-
out.write(" Jfr" + t.name + "Struct,");
393-
}
394-
out.write("");
395-
out.write(" MaxJfrStructId");
396443
out.write("};");
397-
out.write("");
398444
out.write("typedef enum JfrEventId JfrEventId;");
399-
out.write("typedef enum JfrStructId JfrStructId;");
400445
out.write("");
446+
out.write("static const JfrEventId FIRST_EVENT_ID = " + typeCounter.firstEventName() + ";");
447+
out.write("static const JfrEventId LAST_EVENT_ID = " + typeCounter.lastEventName() + ";");
448+
out.write("static const int NUMBER_OF_EVENTS = " + typeCounter.eventCount() + ";");
449+
out.write("static const int NUMBER_OF_RESERVED_EVENTS = " + TypeCounter.RESERVED_EVENT_COUNT + ";");
401450
out.write("#endif // INCLUDE_JFR");
402451
out.write("#endif // JFRFILES_JFREVENTIDS_HPP");
403452
}
404453
}
405454

406-
private static void printJfrTypesHpp(Metadata metadata, File outputDirectory) throws Exception {
407-
List<String> knownTypes = List.of("Thread", "StackTrace", "Class", "StackFrame");
455+
private static void printJfrTypesHpp(Metadata metadata, TypeCounter typeCounter, File outputDirectory) throws Exception {
408456
try (Printer out = new Printer(outputDirectory, "jfrTypes.hpp")) {
409457
out.write("#ifndef JFRFILES_JFRTYPES_HPP");
410458
out.write("#define JFRFILES_JFRTYPES_HPP");
411459
out.write("");
412460
out.write("#include \"utilities/macros.hpp\"");
413461
out.write("#if INCLUDE_JFR");
414462
out.write("");
463+
out.write("#include <string.h>");
464+
out.write("#include \"memory/allocation.hpp\"");
465+
out.write("");
415466
out.write("enum JfrTypeId {");
416-
out.write(" TYPE_NONE = 0,");
417-
out.write(" TYPE_CLASS = 20,");
418-
out.write(" TYPE_STRING = 21,");
419-
out.write(" TYPE_THREAD = 22,");
420-
out.write(" TYPE_STACKTRACE = 23,");
421-
out.write(" TYPE_BYTES = 24,");
422-
out.write(" TYPE_EPOCHMILLIS = 25,");
423-
out.write(" TYPE_MILLIS = 26,");
424-
out.write(" TYPE_NANOS = 27,");
425-
out.write(" TYPE_TICKS = 28,");
426-
out.write(" TYPE_ADDRESS = 29,");
427-
out.write(" TYPE_PERCENTAGE = 30,");
428-
out.write(" TYPE_DUMMY,");
429-
out.write(" TYPE_DUMMY_1,");
467+
Map<String, XmlType> javaTypes = new LinkedHashMap<>();
468+
for (var t : metadata.xmlTypes.entrySet()) {
469+
String name = t.getKey();
470+
XmlType xmlType = t.getValue();
471+
if (xmlType.javaType != null && !xmlType.unsigned) {
472+
String typeName = "TYPE_" + name.toUpperCase();
473+
long typeId = typeCounter.nextTypeId(typeName);
474+
out.write(" " + typeName + " = " + typeId + ",");
475+
javaTypes.put(name, xmlType);
476+
}
477+
}
430478
for (TypeElement type : metadata.getTypes()) {
431-
if (!knownTypes.contains(type.name)) {
432-
out.write(" TYPE_" + type.name.toUpperCase() + ",");
479+
String name = type.name;
480+
if (!javaTypes.containsKey(name)) {
481+
String typeName = "TYPE_" + name.toUpperCase();
482+
long typeId = typeCounter.nextTypeId(typeName);
483+
out.write(" " + typeName + " = " + typeId + ",");
433484
}
434485
}
435-
out.write("");
436-
out.write(" NUM_JFR_TYPES,");
437-
out.write(" TYPES_END = 255");
438486
out.write("};");
439487
out.write("");
440-
out.write("enum ReservedEvent {");
441-
out.write(" EVENT_METADATA,");
442-
out.write(" EVENT_CHECKPOINT,");
443-
out.write(" EVENT_BUFFERLOST,");
444-
out.write(" NUM_RESERVED_EVENTS = TYPES_END");
488+
out.write("static const JfrTypeId FIRST_TYPE_ID = " + typeCounter.firstTypeName() + ";");
489+
out.write("static const JfrTypeId LAST_TYPE_ID = " + typeCounter.lastTypeName() + ";");
490+
491+
out.write("");
492+
out.write("class JfrType : public AllStatic {");
493+
out.write(" public:");
494+
out.write(" static jlong name_to_id(const char* type_name) {");
495+
for (Entry<String, XmlType> m : javaTypes.entrySet()) {
496+
XmlType xmlType = m.getValue();
497+
String javaName = xmlType.javaType;
498+
String typeName = xmlType.name.toUpperCase();
499+
out.write(" if (strcmp(type_name, \"" + javaName + "\") == 0) {");
500+
out.write(" return TYPE_" + typeName + ";");
501+
out.write(" }");
502+
}
503+
out.write(" return -1;");
504+
out.write(" }");
445505
out.write("};");
446506
out.write("");
447507
out.write("#endif // INCLUDE_JFR");
448508
out.write("#endif // JFRFILES_JFRTYPES_HPP");
449-
};
509+
}
510+
;
450511
}
451512

452513
private static void printJfrEventClassesHpp(Metadata metadata, File outputDirectory) throws Exception {

‎src/hotspot/share/jfr/jni/jfrJniMethod.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "jfr/utilities/jfrTime.hpp"
5050
#include "jfr/writers/jfrJavaEventWriter.hpp"
5151
#include "jfrfiles/jfrPeriodic.hpp"
52+
#include "jfrfiles/jfrTypes.hpp"
5253
#include "logging/log.hpp"
5354
#include "memory/resourceArea.hpp"
5455
#include "runtime/interfaceSupport.inline.hpp"
@@ -349,4 +350,10 @@ JVM_ENTRY_NO_ENV(jboolean, jfr_set_handler(JNIEnv * env, jobject jvm, jobject cl
349350
return JfrJavaSupport::set_handler(clazz, handler, thread);
350351
JVM_END
351352

353+
NO_TRANSITION(jlong, jfr_get_type_id_from_string(JNIEnv * env, jobject jvm, jstring type))
354+
const char* type_name= env->GetStringUTFChars(type, NULL);
355+
jlong id = JfrType::name_to_id(type_name);
356+
env->ReleaseStringUTFChars(type, type_name);
357+
return id;
358+
NO_TRANSITION_END
352359

‎src/hotspot/share/jfr/jni/jfrJniMethod.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ jobject JNICALL jfr_get_handler(JNIEnv* env, jobject jvm, jobject clazz);
148148

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

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

152153
#ifdef __cplusplus
153154
}

‎src/hotspot/share/jfr/jni/jfrJniMethodRegistration.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ JfrJniMethodRegistration::JfrJniMethodRegistration(JNIEnv* env) {
8888
(char*)"isExcluded", (char*)"(Ljava/lang/Thread;)Z", (void*)jfr_is_thread_excluded,
8989
(char*)"getChunkStartNanos", (char*)"()J", (void*)jfr_chunk_start_nanos,
9090
(char*)"getHandler", (char*)"(Ljava/lang/Class;)Ljava/lang/Object;", (void*)jfr_get_handler,
91-
(char*)"setHandler", (char*)"(Ljava/lang/Class;Ljdk/jfr/internal/handlers/EventHandler;)Z", (void*)jfr_set_handler
91+
(char*)"setHandler", (char*)"(Ljava/lang/Class;Ljdk/jfr/internal/handlers/EventHandler;)Z", (void*)jfr_set_handler,
92+
(char*)"getTypeId", (char*)"(Ljava/lang/String;)J", (void*)jfr_get_type_id_from_string
9293
};
9394

9495
const size_t method_array_length = sizeof(method) / sizeof(JNINativeMethod);

‎src/hotspot/share/jfr/metadata/metadata.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1304,8 +1304,8 @@
13041304
<XmlType name="ClassLoader" parameterType="const ClassLoaderData*" fieldType="const ClassLoaderData*"/>
13051305
<XmlType name="Method" parameterType="const Method*" fieldType="const Method*"/>
13061306
<XmlType name="Thread" javaType="java.lang.Thread" parameterType="u8" fieldType="u8"/>
1307-
<XmlType name="Tickspan" contentType="tickspan" javaType="long" parameterType="const Tickspan&amp;" fieldType="Tickspan"/>
1308-
<XmlType name="Ticks" contentType="tickstamp" javaType="long" parameterType="const Ticks&amp;" fieldType="Ticks"/>
1307+
<XmlType name="Tickspan" contentType="tickspan" unsigned="true" javaType="long" parameterType="const Tickspan&amp;" fieldType="Tickspan"/>
1308+
<XmlType name="Ticks" contentType="tickstamp" unsigned="true" javaType="long" parameterType="const Ticks&amp;" fieldType="Ticks"/>
13091309
<XmlType name="ulong" javaType="long" unsigned="true" parameterType="u8" fieldType="u8"/>
13101310
<XmlType name="uint" javaType="int" unsigned="true" parameterType="unsigned" fieldType="unsigned"/>
13111311
<XmlType name="ushort" javaType="short" unsigned="true" parameterType="u2" fieldType="u2"/>
@@ -1319,6 +1319,7 @@
13191319
<XmlType name="boolean" javaType="boolean" parameterType="bool" fieldType="bool"/>
13201320
<XmlType name="char" javaType="char" parameterType="char" fieldType="char"/>
13211321
<XmlType name="string" javaType="java.lang.String" parameterType="const char*" fieldType="const char*"/>
1322+
<XmlType name="StackTrace" javaType="jdk.types.StackTrace" parameterType="u8" fieldType="u8"/>
13221323

13231324
<XmlContentType name="bytes" annotation="jdk.jfr.DataAmount(BYTES)" />
13241325
<XmlContentType name="tickstamp" annotation="jdk.jfr.Timestamp(TICKS)" />

‎src/hotspot/share/jfr/recorder/checkpoint/jfrCheckpointWriter.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ void JfrCheckpointWriter::release() {
126126
}
127127

128128
void JfrCheckpointWriter::write_type(JfrTypeId type_id) {
129-
assert(type_id < TYPES_END, "invariant");
129+
assert(type_id <= LAST_TYPE_ID, "type id overflow invariant");
130+
assert(type_id >= FIRST_TYPE_ID, "type id underflow invariant");
130131
write<u8>(type_id);
131132
increment();
132133
}

‎src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceId.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "classfile/javaClasses.inline.hpp"
2828
#include "classfile/symbolTable.hpp"
2929
#include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
30+
#include "jfrfiles/jfrTypes.hpp"
3031
#include "jfr/utilities/jfrTypes.hpp"
3132
#include "oops/arrayKlass.inline.hpp"
3233
#include "oops/klass.inline.hpp"
@@ -52,7 +53,7 @@ static traceid atomic_inc(traceid volatile* const dest) {
5253
}
5354

5455
static traceid next_class_id() {
55-
static volatile traceid class_id_counter = MaxJfrEventId + 100;
56+
static volatile traceid class_id_counter = LAST_TYPE_ID + 1;
5657
return atomic_inc(&class_id_counter) << TRACE_ID_SHIFT;
5758
}
5859

‎src/hotspot/share/jfr/recorder/jfrEventSetting.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -55,7 +55,10 @@ void JfrEventSetting::set_enabled(jlong id, bool enabled) {
5555

5656
#ifdef ASSERT
5757
bool JfrEventSetting::bounds_check_event(jlong id) {
58-
if ((unsigned)id < NUM_RESERVED_EVENTS || (unsigned)id >= MaxJfrEventId) {
58+
if ((unsigned)id < FIRST_EVENT_ID) {
59+
return false;
60+
}
61+
if ((unsigned)id > LAST_EVENT_ID) {
5962
return false;
6063
}
6164
return true;

‎src/hotspot/share/jfr/recorder/repository/jfrChunkWriter.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
#include "precompiled.hpp"
26+
#include "jfrfiles/jfrTypes.hpp"
2627
#include "jfr/recorder/repository/jfrChunk.hpp"
2728
#include "jfr/recorder/repository/jfrChunkWriter.hpp"
2829
#include "jfr/utilities/jfrTime.hpp"

‎src/hotspot/share/jfr/recorder/service/jfrEvent.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ class JfrEvent {
192192
JfrEventVerifier _verifier;
193193

194194
void assert_precondition() {
195-
assert(T::eventId >= (JfrEventId)NUM_RESERVED_EVENTS, "event id underflow invariant");
196-
assert(T::eventId < MaxJfrEventId, "event id overflow invariant");
195+
assert(T::eventId >= FIRST_EVENT_ID, "event id underflow invariant");
196+
assert(T::eventId <= LAST_EVENT_ID, "event id overflow invariant");
197197
DEBUG_ONLY(static_cast<T*>(this)->verify());
198198
}
199199

‎src/hotspot/share/jfr/utilities/jfrTypes.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define SHARE_JFR_UTILITIES_JFRTYPES_HPP
2727

2828
#include "jfrfiles/jfrEventIds.hpp"
29+
#include "jfrfiles/jfrTypes.hpp"
2930
#include "utilities/globalDefinitions.hpp"
3031

3132
typedef u8 traceid;
@@ -46,6 +47,11 @@ inline int sort_traceid(traceid* lhs, traceid* rhs) {
4647
return compare_traceid(*lhs, *rhs);
4748
}
4849

50+
enum ReservedEvent {
51+
EVENT_METADATA = 0,
52+
EVENT_CHECKPOINT = 1
53+
};
54+
4955
enum EventStartTime {
5056
UNTIMED,
5157
TIMED

‎src/jdk.jfr/share/classes/jdk/jfr/internal/JVM.java

+9
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,13 @@ public boolean hasNativeJFR() {
577577
* @return the handler, may be {@code null}
578578
*/
579579
public native Object getHandler(Class<? extends jdk.internal.event.Event> eventClass);
580+
581+
/**
582+
* Returns the id for the Java types defined in metadata.xml.
583+
*
584+
* @param name the name of the type
585+
*
586+
* @return the id, or a negative value if it does not exists.
587+
*/
588+
public native long getTypeId(String name);
580589
}

‎src/jdk.jfr/share/classes/jdk/jfr/internal/MetadataHandler.java

+39-42
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.InputStream;
3131
import java.lang.annotation.Annotation;
3232
import java.util.ArrayList;
33+
import java.util.Collection;
3334
import java.util.Collections;
3435
import java.util.HashMap;
3536
import java.util.LinkedHashMap;
@@ -59,6 +60,9 @@
5960

6061
final class MetadataHandler extends DefaultHandler implements EntityResolver {
6162

63+
// Metadata and Checkpoint event
64+
private final long RESERVED_EVENT_COUNT = 2;
65+
6266
static class TypeElement {
6367
List<FieldElement> fields = new ArrayList<>();
6468
String name;
@@ -72,6 +76,7 @@ static class TypeElement {
7276
boolean stackTrace;
7377
boolean cutoff;
7478
boolean isEvent;
79+
boolean isRelation;
7580
boolean experimental;
7681
boolean valueType;
7782
}
@@ -99,13 +104,11 @@ static class XmlType {
99104
}
100105

101106
final Map<String, TypeElement> types = new LinkedHashMap<>(200);
102-
final Map<String, XmlType> xmlTypes = new HashMap<>(20);
103-
final Map<String, List<AnnotationElement>> xmlContentTypes = new HashMap<>(20);
104-
final List<String> relations = new ArrayList<>();
105-
long eventTypeId = 255;
106-
long structTypeId = 33;
107+
final Map<String, XmlType> xmlTypes = new LinkedHashMap<>(20);
108+
final Map<String, List<AnnotationElement>> xmlContentTypes = new LinkedHashMap<>(20);
107109
FieldElement currentField;
108110
TypeElement currentType;
111+
long eventCount;
109112

110113
@Override
111114
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
@@ -118,6 +121,7 @@ public void startElement(String uri, String localName, String qName, Attributes
118121
xmlType.unsigned = Boolean.valueOf(attributes.getValue("unsigned"));
119122
xmlTypes.put(xmlType.name, xmlType);
120123
break;
124+
case "Relation":
121125
case "Type":
122126
case "Event":
123127
currentType = new TypeElement();
@@ -132,6 +136,7 @@ public void startElement(String uri, String localName, String qName, Attributes
132136
currentType.cutoff = getBoolean(attributes, "cutoff", false);
133137
currentType.experimental = getBoolean(attributes, "experimental", false);
134138
currentType.isEvent = qName.equals("Event");
139+
currentType.isRelation = qName.equals("Relation");
135140
break;
136141
case "Field":
137142
currentField = new FieldElement();
@@ -151,10 +156,6 @@ public void startElement(String uri, String localName, String qName, Attributes
151156
String annotation = attributes.getValue("annotation");
152157
xmlContentTypes.put(name, createAnnotationElements(annotation));
153158
break;
154-
case "Relation":
155-
String n = attributes.getValue("name");
156-
relations.add(n);
157-
break;
158159
}
159160
}
160161

@@ -202,7 +203,11 @@ public void endElement(String uri, String localName, String qName) {
202203
switch (qName) {
203204
case "Type":
204205
case "Event":
206+
case "Relation":
205207
types.put(currentType.name, currentType);
208+
if (currentType.isEvent) {
209+
eventCount++;
210+
}
206211
currentType = null;
207212
break;
208213
case "Field":
@@ -221,7 +226,6 @@ public static List<Type> createTypes() throws IOException {
221226
parser.parse(is, t);
222227
return t.buildTypes();
223228
} catch (Exception e) {
224-
e.printStackTrace();
225229
throw new IOException(e);
226230
}
227231
}
@@ -237,12 +241,12 @@ private List<Type> buildTypes() {
237241

238242
private Map<String, AnnotationElement> buildRelationMap(Map<String, Type> typeMap) {
239243
Map<String, AnnotationElement> relationMap = new HashMap<>();
240-
for (String relation : relations) {
241-
Type relationType = new Type(Type.TYPES_PREFIX + relation, Type.SUPER_TYPE_ANNOTATION, eventTypeId++);
242-
relationType.setAnnotations(Collections.singletonList(new AnnotationElement(Relational.class)));
243-
AnnotationElement ae = PrivateAccess.getInstance().newAnnotation(relationType, Collections.emptyList(), true);
244-
relationMap.put(relation, ae);
245-
typeMap.put(relationType.getName(), relationType);
244+
for (TypeElement t : types.values()) {
245+
if (t.isRelation) {
246+
Type relationType = typeMap.get(t.name);
247+
AnnotationElement ae = PrivateAccess.getInstance().newAnnotation(relationType, Collections.emptyList(), true);
248+
relationMap.put(t.name, ae);
249+
}
246250
}
247251
return relationMap;
248252
}
@@ -276,7 +280,9 @@ private void addFields(Map<String, Type> lookup, Map<String, AnnotationElement>
276280
aes.addAll(Objects.requireNonNull(xmlContentTypes.get(f.contentType)));
277281
}
278282
if (f.relation != null) {
279-
aes.add(Objects.requireNonNull(relationMap.get(f.relation)));
283+
String relationTypeName = Type.TYPES_PREFIX + f.relation;
284+
AnnotationElement t = relationMap.get(relationTypeName);
285+
aes.add(Objects.requireNonNull(t));
280286
}
281287
if (f.label != null) {
282288
aes.add(new AnnotationElement(Label.class, f.label));
@@ -301,10 +307,13 @@ private void addFields(Map<String, Type> lookup, Map<String, AnnotationElement>
301307

302308
private Map<String, Type> buildTypeMap() {
303309
Map<String, Type> typeMap = new HashMap<>();
304-
for (Type type : Type.getKnownTypes()) {
305-
typeMap.put(type.getName(), type);
310+
Map<String, Type> knownTypeMap = new HashMap<>();
311+
for (Type kt :Type.getKnownTypes()) {
312+
typeMap.put(kt.getName(), kt);
313+
knownTypeMap.put(kt.getName(), kt);
306314
}
307-
315+
long eventTypeId = RESERVED_EVENT_COUNT;
316+
long typeId = RESERVED_EVENT_COUNT + eventCount + knownTypeMap.size();
308317
for (TypeElement t : types.values()) {
309318
List<AnnotationElement> aes = new ArrayList<>();
310319
if (t.category != null) {
@@ -339,35 +348,23 @@ private Map<String, Type> buildTypeMap() {
339348
aes.add(new AnnotationElement(Enabled.class, false));
340349
type = new PlatformEventType(t.name, eventTypeId++, false, true);
341350
} else {
342-
// Struct types had their own XML-element in the past. To have id assigned in the
343-
// same order as generated .hpp file do some tweaks here.
344-
boolean valueType = t.name.endsWith("StackFrame") || t.valueType;
345-
type = new Type(t.name, null, valueType ? eventTypeId++ : nextTypeId(t.name), false);
351+
if (knownTypeMap.containsKey(t.name)) {
352+
type = knownTypeMap.get(t.name);
353+
} else {
354+
if (t.isRelation) {
355+
type = new Type(t.name, Type.SUPER_TYPE_ANNOTATION, typeId++);
356+
aes.add(new AnnotationElement(Relational.class));
357+
} else {
358+
type = new Type(t.name, null, typeId++);
359+
}
360+
}
346361
}
347362
type.setAnnotations(aes);
348363
typeMap.put(t.name, type);
349364
}
350365
return typeMap;
351366
}
352367

353-
private long nextTypeId(String name) {
354-
if (Type.THREAD.getName().equals(name)) {
355-
return Type.THREAD.getId();
356-
}
357-
if (Type.STRING.getName().equals(name)) {
358-
return Type.STRING.getId();
359-
}
360-
if (Type.CLASS.getName().equals(name)) {
361-
return Type.CLASS.getId();
362-
}
363-
for (Type type : Type.getKnownTypes()) {
364-
if (type.getName().equals(name)) {
365-
return type.getId();
366-
}
367-
}
368-
return structTypeId++;
369-
}
370-
371368
private String[] buildCategoryArray(String category) {
372369
List<String> categories = new ArrayList<>();
373370
StringBuilder sb = new StringBuilder();

‎src/jdk.jfr/share/classes/jdk/jfr/internal/MetadataReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private void declareTypes(Element metadata) {
261261
if (Type.SUPER_TYPE_EVENT.equals(superType)) {
262262
t = new PlatformEventType(typeName, id, false, false);
263263
} else {
264-
t = new Type(typeName, superType, id, false, simpleType);
264+
t = new Type(typeName, superType, id, simpleType);
265265
}
266266
types.put(id, t);
267267
descriptor.types.add(t);

‎src/jdk.jfr/share/classes/jdk/jfr/internal/Type.java

+30-31
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Collection;
3030
import java.util.Collections;
3131
import java.util.HashMap;
32+
import java.util.LinkedHashMap;
3233
import java.util.List;
3334
import java.util.Map;
3435
import java.util.Objects;
@@ -53,25 +54,37 @@ public class Type implements Comparable<Type> {
5354
public static final String SETTINGS_PREFIX = "jdk.settings.";
5455

5556

56-
// Initialization of known types
57-
private final static Map<Type, Class<?>> knownTypes = new HashMap<>();
58-
static final Type BOOLEAN = register(boolean.class, new Type("boolean", null, 4));
59-
static final Type CHAR = register(char.class, new Type("char", null, 5));
60-
static final Type FLOAT = register(float.class, new Type("float", null, 6));
61-
static final Type DOUBLE = register(double.class, new Type("double", null, 7));
62-
static final Type BYTE = register(byte.class, new Type("byte", null, 8));
63-
static final Type SHORT = register(short.class, new Type("short", null, 9));
64-
static final Type INT = register(int.class, new Type("int", null, 10));
65-
static final Type LONG = register(long.class, new Type("long", null, 11));
66-
static final Type CLASS = register(Class.class, new Type("java.lang.Class", null, 20));
67-
static final Type STRING = register(String.class, new Type("java.lang.String", null, 21));
68-
static final Type THREAD = register(Thread.class, new Type("java.lang.Thread", null, 22));
69-
static final Type STACK_TRACE = register(null, new Type(TYPES_PREFIX + "StackTrace", null, 23));
57+
// To bootstrap the type system, the supported Java types
58+
// are available here as statics. When metadata.xml is parsed
59+
// fields are added to THREAD and STACK_TRACE.
60+
private final static Map<Type, Class<?>> knownTypes = new LinkedHashMap<>();
61+
static final Type BOOLEAN = createKnownType(boolean.class);
62+
static final Type CHAR = createKnownType(char.class);
63+
static final Type FLOAT = createKnownType(float.class);
64+
static final Type DOUBLE = createKnownType(double.class);
65+
static final Type BYTE = createKnownType(byte.class);
66+
static final Type SHORT = createKnownType(short.class);
67+
static final Type INT = createKnownType(int.class);
68+
static final Type LONG = createKnownType(long.class);
69+
static final Type CLASS = createKnownType(Class.class);
70+
static final Type STRING = createKnownType(String.class);
71+
static final Type THREAD = createKnownType(Thread.class);
72+
static final Type STACK_TRACE = createKnownType(TYPES_PREFIX + "StackTrace", null);
73+
74+
private static Type createKnownType(Class<?> clazz) {
75+
return createKnownType(clazz.getName(), clazz);
76+
}
77+
78+
private static Type createKnownType(String name, Class<?> clazz) {
79+
long id = JVM.getJVM().getTypeId(name);
80+
Type t = new Type(name, null, id);
81+
knownTypes.put(t, clazz);
82+
return t;
83+
}
7084

7185
private final AnnotationConstruct annos = new AnnotationConstruct();
7286
private final String name;
7387
private final String superType;
74-
private final boolean constantPool;
7588
private List<ValueDescriptor> fields = new ArrayList<>();
7689
private Boolean simpleType; // calculated lazy
7790
private boolean remove = true;
@@ -86,20 +99,15 @@ public class Type implements Comparable<Type> {
8699
*
87100
*/
88101
public Type(String javaTypeName, String superType, long typeId) {
89-
this(javaTypeName, superType, typeId, false);
90-
}
91-
92-
Type(String javaTypeName, String superType, long typeId, boolean constantPool) {
93-
this(javaTypeName, superType, typeId, constantPool, null);
102+
this(javaTypeName, superType, typeId, null);
94103
}
95104

96-
Type(String javaTypeName, String superType, long typeId, boolean constantPool, Boolean simpleType) {
105+
Type(String javaTypeName, String superType, long typeId, Boolean simpleType) {
97106
Objects.requireNonNull(javaTypeName);
98107

99108
if (!isValidJavaIdentifier(javaTypeName)) {
100109
throw new IllegalArgumentException(javaTypeName + " is not a valid Java identifier");
101110
}
102-
this.constantPool = constantPool;
103111
this.superType = superType;
104112
this.name = javaTypeName;
105113
this.id = typeId;
@@ -210,11 +218,6 @@ public boolean isDefinedByJVM() {
210218
return id < JVM.RESERVED_CLASS_ID_LIMIT;
211219
}
212220

213-
private static Type register(Class<?> clazz, Type type) {
214-
knownTypes.put(type, clazz);
215-
return type;
216-
}
217-
218221
public void add(ValueDescriptor valueDescriptor) {
219222
Objects.requireNonNull(valueDescriptor);
220223
fields.add(valueDescriptor);
@@ -236,10 +239,6 @@ public long getId() {
236239
return id;
237240
}
238241

239-
public boolean isConstantPool() {
240-
return constantPool;
241-
}
242-
243242
public String getLabel() {
244243
return annos.getLabel();
245244
}

‎test/jdk/jdk/jfr/event/metadata/TestEventMetadata.java

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public class TestEventMetadata {
9292
*/
9393
public static void main(String[] args) throws Exception {
9494
Set<String> types = new HashSet<>();
95+
// These types contains reserved keywords (unfortunately) so
96+
// exclude them from the check.
97+
types.add("jdk.types.StackTrace");
98+
types.add("java.lang.Class");
9599
List<EventType> eventTypes = FlightRecorder.getFlightRecorder().getEventTypes();
96100
Set<String> eventNames= new HashSet<>();
97101
for (EventType eventType : eventTypes) {

0 commit comments

Comments
 (0)
Please sign in to comment.