make/src/classes/build/tools/jfr/GenerateJfrFiles.java
author herrick
Tue, 12 Nov 2019 10:25:55 -0500
branchJDK-8200758-branch
changeset 59034 76bec73a91f0
parent 51968 5bdf60cd0ed0
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50266
8090a68b6af5 8203221: Makefile fixes after Flight Recorder
ihse
parents: 50124
diff changeset
     1
package build.tools.jfr;
8090a68b6af5 8203221: Makefile fixes after Flight Recorder
ihse
parents: 50124
diff changeset
     2
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     3
import java.io.BufferedOutputStream;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     4
import java.io.File;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     5
import java.io.FileNotFoundException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     6
import java.io.FileOutputStream;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     7
import java.io.IOException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     8
import java.io.PrintStream;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     9
import java.util.ArrayList;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    10
import java.util.HashMap;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    11
import java.util.LinkedHashMap;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    12
import java.util.List;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    13
import java.util.Map;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    14
import java.util.StringJoiner;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    15
import java.util.function.Predicate;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    16
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    17
import javax.xml.XMLConstants;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    18
import javax.xml.parsers.ParserConfigurationException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    19
import javax.xml.parsers.SAXParser;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    20
import javax.xml.parsers.SAXParserFactory;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    21
import javax.xml.validation.SchemaFactory;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    22
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    23
import org.xml.sax.Attributes;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    24
import org.xml.sax.SAXException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
import org.xml.sax.SAXParseException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
import org.xml.sax.helpers.DefaultHandler;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
public class GenerateJfrFiles {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
    public static void main(String... args) throws Exception {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
        if (args.length != 3) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
            System.err.println("Incorrect number of command line arguments.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
            System.err.println("Usage:");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
            System.err.println("java GenerateJfrFiles[.java] <path-to-metadata.xml> <path-to-metadata.xsd> <output-directory>");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
            System.exit(1);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
            File metadataXml = new File(args[0]);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
            File metadataSchema = new File(args[1]);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
            File outputDirectory = new File(args[2]);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
            Metadata metadata = new Metadata(metadataXml, metadataSchema);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
            metadata.verify();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
            metadata.wireUpTypes();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
            printJfrPeriodicHpp(metadata, outputDirectory);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
            printJfrEventIdsHpp(metadata, outputDirectory);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
            printJfrEventControlHpp(metadata, outputDirectory);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
            printJfrTypesHpp(metadata, outputDirectory);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
            printJfrEventClassesHpp(metadata, outputDirectory);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
        } catch (Exception e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
            e.printStackTrace();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
            System.exit(1);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
    static class XmlType {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
        final String fieldType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
        final String parameterType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
        XmlType(String fieldType, String parameterType) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
            this.fieldType = fieldType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
            this.parameterType = parameterType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
    static class TypeElement {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
        List<FieldElement> fields = new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
        String name;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
        String fieldType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
        String parameterType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
        boolean supportStruct;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
    static class Metadata {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
        final Map<String, TypeElement> types = new LinkedHashMap<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
        final Map<String, XmlType> xmlTypes = new HashMap<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
        Metadata(File metadataXml, File metadataSchema) throws ParserConfigurationException, SAXException, FileNotFoundException, IOException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
            SAXParserFactory factory = SAXParserFactory.newInstance();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
            factory.setSchema(schemaFactory.newSchema(metadataSchema));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
            SAXParser sp = factory.newSAXParser();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
            sp.parse(metadataXml, new MetadataHandler(this));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
        List<EventElement> getEvents() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
            return getList(t -> t.getClass() == EventElement.class);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
        List<TypeElement> getEventsAndStructs() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
            return getList(t -> t.getClass() == EventElement.class || t.supportStruct);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
        List<TypeElement> getTypesAndStructs() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
            return getList(t -> t.getClass() == TypeElement.class || t.supportStruct);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
        @SuppressWarnings("unchecked")
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
        <T> List<T> getList(Predicate<? super TypeElement> pred) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
            List<T> result = new ArrayList<>(types.size());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
            for (TypeElement t : types.values()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
                if (pred.test(t)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
                    result.add((T) t);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
            return result;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
        List<EventElement> getPeriodicEvents() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
            return getList(t -> t.getClass() == EventElement.class && ((EventElement) t).periodic);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
        List<TypeElement> getNonEventsAndNonStructs() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
            return getList(t -> t.getClass() != EventElement.class && !t.supportStruct);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
        List<TypeElement> getTypes() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
            return getList(t -> t.getClass() == TypeElement.class && !t.supportStruct);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
        List<TypeElement> getStructs() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
            return getList(t -> t.getClass() == TypeElement.class && t.supportStruct);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
        void verify()  {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
            for (TypeElement t : types.values()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
                for (FieldElement f : t.fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
                    if (!xmlTypes.containsKey(f.typeName)) { // ignore primitives
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
                        if (!types.containsKey(f.typeName)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
                            throw new IllegalStateException("Could not find definition of type '" + f.typeName + "' used by " + t.name + "#" + f.name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
                        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
                    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   133
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   134
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
        void wireUpTypes() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
            for (TypeElement t : types.values()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
                for (FieldElement f : t.fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
                    TypeElement type = types.get(f.typeName);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   141
                    if (f.struct) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   142
                        type.supportStruct = true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   143
                    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   144
                    f.type = type;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   145
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   146
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   147
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   148
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   149
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   150
    static class EventElement extends TypeElement {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   151
        String representation;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   152
        boolean thread;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   153
        boolean stackTrace;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
        boolean startTime;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
        boolean periodic;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
        boolean cutoff;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
    static class FieldElement {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
        final Metadata metadata;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
        TypeElement type;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
        String name;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   163
        String typeName;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   164
        boolean struct;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   165
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
        FieldElement(Metadata metadata) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
            this.metadata = metadata;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   168
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
        String getParameterType() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   171
            if (struct) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   172
                return "const JfrStruct" + typeName + "&";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   173
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   174
            XmlType xmlType = metadata.xmlTypes.get(typeName);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   175
            if (xmlType != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   176
                return xmlType.parameterType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   177
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
            return type != null ? "u8" : typeName;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
        String getParameterName() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
            return struct ? "value" : "new_value";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   185
        String getFieldType() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   186
            if (struct) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   187
                return "JfrStruct" + typeName;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   188
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   189
            XmlType xmlType = metadata.xmlTypes.get(typeName);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   190
            if (xmlType != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   191
                return xmlType.fieldType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   192
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   193
            return type != null ? "u8" : typeName;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   194
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   195
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   196
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   197
    static class MetadataHandler extends DefaultHandler {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   198
        final Metadata metadata;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   199
        FieldElement currentField;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   200
        TypeElement currentType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   201
        MetadataHandler(Metadata metadata) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   202
            this.metadata = metadata;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   203
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   204
        @Override
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   205
        public void error(SAXParseException e) throws SAXException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   206
          throw e;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   207
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   208
        @Override
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   209
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   210
            switch (qName) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   211
            case "XmlType":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   212
                String name = attributes.getValue("name");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   213
                String parameterType = attributes.getValue("parameterType");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   214
                String fieldType = attributes.getValue("fieldType");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   215
                metadata.xmlTypes.put(name, new XmlType(fieldType, parameterType));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   216
                break;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   217
            case "Type":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   218
                currentType = new TypeElement();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   219
                currentType.name = attributes.getValue("name");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   220
                break;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   221
            case "Event":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   222
                EventElement eventtType = new EventElement();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   223
                eventtType.name = attributes.getValue("name");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   224
                eventtType.thread = getBoolean(attributes, "thread", false);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   225
                eventtType.stackTrace = getBoolean(attributes, "stackTrace", false);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   226
                eventtType.startTime = getBoolean(attributes, "startTime", true);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   227
                eventtType.periodic = attributes.getValue("period") != null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   228
                eventtType.cutoff = getBoolean(attributes, "cutoff", false);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   229
                currentType = eventtType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   230
                break;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   231
            case "Field":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   232
                currentField = new FieldElement(metadata);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   233
                currentField.struct = getBoolean(attributes, "struct", false);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   234
                currentField.name = attributes.getValue("name");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   235
                currentField.typeName = attributes.getValue("type");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   236
                break;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   237
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   238
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   239
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   240
        private boolean getBoolean(Attributes attributes, String name, boolean defaultValue) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   241
            String value = attributes.getValue(name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   242
            return value == null ? defaultValue : Boolean.valueOf(value);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   243
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   244
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   245
        @Override
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   246
        public void endElement(String uri, String localName, String qName) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   247
            switch (qName) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   248
            case "Type":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   249
            case "Event":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   250
                metadata.types.put(currentType.name, currentType);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   251
                currentType = null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   252
                break;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   253
            case "Field":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   254
                currentType.fields.add(currentField);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   255
                currentField = null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   256
                break;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   257
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   258
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   259
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   260
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   261
    static class Printer implements AutoCloseable {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   262
        final PrintStream out;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   263
        Printer(File outputDirectory, String filename) throws FileNotFoundException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   264
            out = new PrintStream(new BufferedOutputStream(new FileOutputStream(new File(outputDirectory, filename))));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   265
            write("/* AUTOMATICALLY GENERATED FILE - DO NOT EDIT */");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   266
            write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   267
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   268
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   269
        void write(String text) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   270
            out.print(text);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   271
            out.print("\n"); // Don't use Windows line endings
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   272
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   273
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   274
        @Override
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   275
        public void close() throws Exception {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   276
            out.close();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   277
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   278
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   279
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   280
    private static void printJfrPeriodicHpp(Metadata metadata, File outputDirectory) throws Exception {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   281
        try (Printer out = new Printer(outputDirectory, "jfrPeriodic.hpp")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   282
            out.write("#ifndef JFRFILES_JFRPERIODICEVENTSET_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   283
            out.write("#define JFRFILES_JFRPERIODICEVENTSET_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   284
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   285
            out.write("#include \"utilities/macros.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   286
            out.write("#if INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   287
            out.write("#include \"jfrfiles/jfrEventIds.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   288
            out.write("#include \"memory/allocation.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   289
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   290
            out.write("class JfrPeriodicEventSet : public AllStatic {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   291
            out.write(" public:");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   292
            out.write("  static void requestEvent(JfrEventId id) {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   293
            out.write("    switch(id) {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   294
            out.write("  ");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   295
            for (EventElement e : metadata.getPeriodicEvents()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   296
                out.write("      case Jfr" + e.name + "Event:");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   297
                out.write("        request" + e.name + "();");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   298
                out.write("        break;");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   299
                out.write("  ");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   300
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   301
            out.write("      default:");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   302
            out.write("        break;");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   303
            out.write("      }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   304
            out.write("    }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   305
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   306
            out.write(" private:");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   307
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   308
            for (EventElement e : metadata.getPeriodicEvents()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   309
                out.write("  static void request" + e.name + "(void);");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   310
                out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   311
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   312
            out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   313
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   314
            out.write("#endif // INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   315
            out.write("#endif // JFRFILES_JFRPERIODICEVENTSET_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   316
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   317
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   318
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   319
    private static void printJfrEventControlHpp(Metadata metadata, File outputDirectory) throws Exception {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   320
        try (Printer out = new Printer(outputDirectory, "jfrEventControl.hpp")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   321
            out.write("#ifndef JFRFILES_JFR_NATIVE_EVENTSETTING_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   322
            out.write("#define JFRFILES_JFR_NATIVE_EVENTSETTING_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   323
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   324
            out.write("#include \"utilities/macros.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   325
            out.write("#if INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   326
            out.write("#include \"jfrfiles/jfrEventIds.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   327
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   328
            out.write("/**");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   329
            out.write(" * Event setting. We add some padding so we can use our");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   330
            out.write(" * event IDs as indexes into this.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   331
            out.write(" */");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   332
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   333
            out.write("struct jfrNativeEventSetting {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   334
            out.write("  jlong  threshold_ticks;");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   335
            out.write("  jlong  cutoff_ticks;");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   336
            out.write("  u1     stacktrace;");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   337
            out.write("  u1     enabled;");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   338
            out.write("  u1     pad[6]; // Because GCC on linux ia32 at least tries to pack this.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   339
            out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   340
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   341
            out.write("union JfrNativeSettings {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   342
            out.write("  // Array version.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   343
            out.write("  jfrNativeEventSetting bits[MaxJfrEventId];");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   344
            out.write("  // Then, to make it easy to debug,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   345
            out.write("  // add named struct members also.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   346
            out.write("  struct {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   347
            out.write("    jfrNativeEventSetting pad[NUM_RESERVED_EVENTS];");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   348
            for (TypeElement t : metadata.getEventsAndStructs()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   349
                out.write("    jfrNativeEventSetting " + t.name + ";");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   350
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   351
            out.write("  } ev;");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   352
            out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   353
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   354
            out.write("#endif // INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   355
            out.write("#endif // JFRFILES_JFR_NATIVE_EVENTSETTING_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   356
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   357
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   358
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   359
    private static void printJfrEventIdsHpp(Metadata metadata, File outputDirectory) throws Exception {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   360
        try (Printer out = new Printer(outputDirectory, "jfrEventIds.hpp")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   361
            out.write("#ifndef JFRFILES_JFREVENTIDS_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   362
            out.write("#define JFRFILES_JFREVENTIDS_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   363
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   364
            out.write("#include \"utilities/macros.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   365
            out.write("#if INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   366
            out.write("#include \"jfrfiles/jfrTypes.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   367
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   368
            out.write("/**");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   369
            out.write(" * Enum of the event types in the JVM");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   370
            out.write(" */");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   371
            out.write("enum JfrEventId {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   372
            out.write("  _jfreventbase = (NUM_RESERVED_EVENTS-1), // Make sure we start at right index.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   373
            out.write("  ");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   374
            out.write("  // Events -> enum entry");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   375
            for (TypeElement t : metadata.getEventsAndStructs()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   376
                out.write("  Jfr" + t.name + "Event,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   377
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   378
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   379
            out.write("  MaxJfrEventId");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   380
            out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   381
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   382
            out.write("/**");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   383
            out.write(" * Struct types in the JVM");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   384
            out.write(" */");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   385
            out.write("enum JfrStructId {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   386
            for (TypeElement t : metadata.getNonEventsAndNonStructs()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   387
                out.write("  Jfr" + t.name + "Struct,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   388
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   389
            for (TypeElement t : metadata.getEventsAndStructs()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   390
                out.write("  Jfr" + t.name + "Struct,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   391
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   392
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   393
            out.write("  MaxJfrStructId");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   394
            out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   395
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   396
            out.write("typedef enum JfrEventId JfrEventId;");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   397
            out.write("typedef enum JfrStructId JfrStructId;");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   398
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   399
            out.write("#endif // INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   400
            out.write("#endif // JFRFILES_JFREVENTIDS_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   401
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   402
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   403
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   404
    private static void printJfrTypesHpp(Metadata metadata, File outputDirectory) throws Exception {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   405
        List<String> knownTypes = List.of("Thread", "StackTrace", "Class", "StackFrame");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   406
        try (Printer out = new Printer(outputDirectory, "jfrTypes.hpp")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   407
            out.write("#ifndef JFRFILES_JFRTYPES_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   408
            out.write("#define JFRFILES_JFRTYPES_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   409
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   410
            out.write("#include \"utilities/macros.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   411
            out.write("#if INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   412
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   413
            out.write("enum JfrTypeId {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   414
            out.write("  TYPE_NONE             = 0,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   415
            out.write("  TYPE_CLASS            = 20,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   416
            out.write("  TYPE_STRING           = 21,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   417
            out.write("  TYPE_THREAD           = 22,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   418
            out.write("  TYPE_STACKTRACE       = 23,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   419
            out.write("  TYPE_BYTES            = 24,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   420
            out.write("  TYPE_EPOCHMILLIS      = 25,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   421
            out.write("  TYPE_MILLIS           = 26,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   422
            out.write("  TYPE_NANOS            = 27,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   423
            out.write("  TYPE_TICKS            = 28,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   424
            out.write("  TYPE_ADDRESS          = 29,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   425
            out.write("  TYPE_PERCENTAGE       = 30,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   426
            out.write("  TYPE_DUMMY,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   427
            out.write("  TYPE_DUMMY_1,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   428
            for (TypeElement type : metadata.getTypes()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   429
                if (!knownTypes.contains(type.name)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   430
                    out.write("  TYPE_" + type.name.toUpperCase() + ",");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   431
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   432
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   433
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   434
            out.write("  NUM_JFR_TYPES,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   435
            out.write("  TYPES_END             = 255");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   436
            out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   437
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   438
            out.write("enum ReservedEvent {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   439
            out.write("  EVENT_METADATA,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   440
            out.write("  EVENT_CHECKPOINT,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   441
            out.write("  EVENT_BUFFERLOST,");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   442
            out.write("  NUM_RESERVED_EVENTS = TYPES_END");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   443
            out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   444
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   445
            out.write("#endif // INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   446
            out.write("#endif // JFRFILES_JFRTYPES_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   447
          };
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   448
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   449
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   450
    private static void printJfrEventClassesHpp(Metadata metadata, File outputDirectory) throws Exception {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   451
        try (Printer out = new Printer(outputDirectory, "jfrEventClasses.hpp")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   452
            out.write("#ifndef JFRFILES_JFREVENTCLASSES_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   453
            out.write("#define JFRFILES_JFREVENTCLASSES_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   454
            out.write("");
50124
781f36c0831e 8203285: Minimal VM fails to build after JDK-8199712 (Flight Recorder)
shade
parents: 50113
diff changeset
   455
            out.write("#include \"oops/klass.hpp\"");
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   456
            out.write("#include \"jfrfiles/jfrTypes.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   457
            out.write("#include \"jfr/utilities/jfrTypes.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   458
            out.write("#include \"utilities/macros.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   459
            out.write("#include \"utilities/ticks.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   460
            out.write("#if INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   461
            out.write("#include \"jfr/recorder/service/jfrEvent.hpp\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   462
            out.write("/*");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   463
            out.write(" * Each event class has an assert member function verify() which is invoked");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   464
            out.write(" * just before the engine writes the event and its fields to the data stream.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   465
            out.write(" * The purpose of verify() is to ensure that all fields in the event are initialized");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   466
            out.write(" * and set before attempting to commit.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   467
            out.write(" *");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   468
            out.write(" * We enforce this requirement because events are generally stack allocated and therefore");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   469
            out.write(" * *not* initialized to default values. This prevents us from inadvertently committing");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   470
            out.write(" * uninitialized values to the data stream.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   471
            out.write(" *");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   472
            out.write(" * The assert message contains both the index (zero based) as well as the name of the field.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   473
            out.write(" */");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   474
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   475
            printTypes(out, metadata, false);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   476
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   477
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   478
            out.write("#else // !INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   479
            out.write("");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   480
            out.write("template <typename T>");
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   481
            out.write("class JfrEvent {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   482
            out.write(" public:");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   483
            out.write("  JfrEvent() {}");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   484
            out.write("  void set_starttime(const Ticks&) const {}");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   485
            out.write("  void set_endtime(const Ticks&) const {}");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   486
            out.write("  bool should_commit() const { return false; }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   487
            out.write("  static bool is_enabled() { return false; }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   488
            out.write("  void commit() {}");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   489
            out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   490
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   491
            printTypes(out, metadata, true);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   492
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   493
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   494
            out.write("#endif // INCLUDE_JFR");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   495
            out.write("#endif // JFRFILES_JFREVENTCLASSES_HPP");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   496
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   497
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   498
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   499
    private static void printTypes(Printer out, Metadata metadata, boolean empty) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   500
        for (TypeElement t : metadata.getStructs()) {
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   501
            printType(out, t, empty);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   502
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   503
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   504
        for (EventElement e : metadata.getEvents()) {
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   505
            printEvent(out, e, empty);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   506
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   507
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   508
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   509
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   510
    private static void printType(Printer out, TypeElement t, boolean empty) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   511
        out.write("struct JfrStruct" + t.name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   512
        out.write("{");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   513
        if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   514
          out.write(" private:");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   515
          for (FieldElement f : t.fields) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   516
              printField(out, f);
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   517
          }
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   518
          out.write("");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   519
        }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   520
        out.write(" public:");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   521
        for (FieldElement f : t.fields) {
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   522
           printTypeSetter(out, f, empty);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   523
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   524
        out.write("");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   525
        if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   526
          printWriteData(out, t.fields);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   527
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   528
        out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   529
        out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   530
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   531
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   532
    private static void printEvent(Printer out, EventElement event, boolean empty) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   533
        out.write("class Event" + event.name + " : public JfrEvent<Event" + event.name + ">");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   534
        out.write("{");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   535
        if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   536
          out.write(" private:");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   537
          for (FieldElement f : event.fields) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   538
              printField(out, f);
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   539
          }
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   540
          out.write("");
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   541
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   542
        out.write(" public:");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   543
        if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   544
          out.write("  static const bool hasThread = " + event.thread + ";");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   545
          out.write("  static const bool hasStackTrace = " + event.stackTrace + ";");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   546
          out.write("  static const bool isInstant = " + !event.startTime + ";");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   547
          out.write("  static const bool hasCutoff = " + event.cutoff + ";");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   548
          out.write("  static const bool isRequestable = " + event.periodic + ";");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   549
          out.write("  static const JfrEventId eventId = Jfr" + event.name + "Event;");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   550
          out.write("");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   551
        }
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   552
        if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   553
          out.write("  Event" + event.name + "(EventStartTime timing=TIMED) : JfrEvent<Event" + event.name + ">(timing) {}");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   554
        } else {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   555
          out.write("  Event" + event.name + "(EventStartTime timing=TIMED) {}");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   556
        }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   557
        out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   558
        int index = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   559
        for (FieldElement f : event.fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   560
            out.write("  void set_" + f.name + "(" + f.getParameterType() + " " + f.getParameterName() + ") {");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   561
            if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   562
              out.write("    this->_" + f.name + " = " + f.getParameterName() + ";");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   563
              out.write("    DEBUG_ONLY(set_field_bit(" + index++ + "));");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   564
            }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   565
            out.write("  }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   566
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   567
        out.write("");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   568
        if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   569
          printWriteData(out, event.fields);
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   570
          out.write("");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   571
        }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   572
        out.write("  using JfrEvent<Event" + event.name + ">::commit; // else commit() is hidden by overloaded versions in this class");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   573
        printConstructor2(out, event, empty);
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   574
        printCommitMethod(out, event, empty);
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   575
        if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   576
          printVerify(out, event.fields);
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   577
        }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   578
        out.write("};");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   579
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   580
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   581
    private static void printWriteData(Printer out, List<FieldElement> fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   582
        out.write("  template <typename Writer>");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   583
        out.write("  void writeData(Writer& w) {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   584
        for (FieldElement field : fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   585
            if (field.struct) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   586
                out.write("    _" + field.name + ".writeData(w);");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   587
            } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   588
                out.write("    w.write(_" + field.name + ");");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   589
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   590
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   591
        out.write("  }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   592
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   593
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   594
    private static void printTypeSetter(Printer out, FieldElement field, boolean empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   595
        if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   596
          out.write("  void set_" + field.name + "(" + field.getParameterType() + " new_value) { this->_" + field.name + " = new_value; }");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   597
        } else {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   598
          out.write("  void set_" + field.name + "(" + field.getParameterType() + " new_value) { }");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   599
        }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   600
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   601
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   602
    private static void printVerify(Printer out, List<FieldElement> fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   603
        out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   604
        out.write("#ifdef ASSERT");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   605
        out.write("  void verify() const {");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   606
        int index = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   607
        for (FieldElement f : fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   608
            out.write("    assert(verify_field_bit(" + index++ + "), \"Attempting to write an uninitialized event field: %s\", \"_" + f.name + "\");");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   609
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   610
        out.write("  }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   611
        out.write("#endif");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   612
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   613
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   614
    private static void printCommitMethod(Printer out, EventElement event, boolean empty) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   615
        if (event.startTime) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   616
            StringJoiner sj = new StringJoiner(",\n              ");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   617
            for (FieldElement f : event.fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   618
                sj.add(f.getParameterType() + " " + f.name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   619
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   620
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   621
            out.write("  void commit(" + sj.toString() + ") {");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   622
            if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   623
              out.write("    if (should_commit()) {");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   624
              for (FieldElement f : event.fields) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   625
                  out.write("      set_" + f.name + "(" + f.name + ");");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   626
              }
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   627
              out.write("      commit();");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   628
              out.write("    }");
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   629
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   630
            out.write("  }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   631
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   632
        out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   633
        StringJoiner sj = new StringJoiner(",\n                     ");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   634
        if (event.startTime) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   635
            sj.add("const Ticks& startTicks");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   636
            sj.add("const Ticks& endTicks");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   637
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   638
        for (FieldElement f : event.fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   639
            sj.add(f.getParameterType() + " " + f.name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   640
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   641
        out.write("  static void commit(" + sj.toString() + ") {");
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   642
        if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   643
          out.write("    Event" + event.name + " me(UNTIMED);");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   644
          out.write("");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   645
          out.write("    if (me.should_commit()) {");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   646
          if (event.startTime) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   647
              out.write("      me.set_starttime(startTicks);");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   648
              out.write("      me.set_endtime(endTicks);");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   649
          }
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   650
          for (FieldElement f : event.fields) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   651
              out.write("      me.set_" + f.name + "(" + f.name + ");");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   652
          }
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   653
          out.write("      me.commit();");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   654
          out.write("    }");
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   655
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   656
        out.write("  }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   657
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   658
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   659
    private static void printConstructor2(Printer out, EventElement event, boolean empty) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   660
        if (!event.startTime) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   661
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   662
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   663
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   664
        if (event.startTime) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   665
            out.write("");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   666
            out.write("  Event" + event.name + "(");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   667
            StringJoiner sj = new StringJoiner(",\n    ");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   668
            for (FieldElement f : event.fields) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   669
                sj.add(f.getParameterType() + " " + f.name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   670
            }
51968
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   671
            if (!empty) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   672
              out.write("    " + sj.toString() + ") : JfrEvent<Event" + event.name + ">(TIMED) {");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   673
              out.write("    if (should_commit()) {");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   674
              for (FieldElement f : event.fields) {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   675
                  out.write("      set_" + f.name + "(" + f.name + ");");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   676
              }
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   677
              out.write("    }");
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   678
            } else {
5bdf60cd0ed0 8211239: Build fails without JFR: empty JFR events signatures mismatch
shade
parents: 50266
diff changeset
   679
              out.write("    " + sj.toString() + ") {");
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   680
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   681
            out.write("  }");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   682
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   683
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   684
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   685
    private static void printField(Printer out, FieldElement field) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   686
        out.write("  " + field.getFieldType() + " _" + field.name + ";");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   687
    }
50124
781f36c0831e 8203285: Minimal VM fails to build after JDK-8199712 (Flight Recorder)
shade
parents: 50113
diff changeset
   688
}