src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/ParserFactory.java
branchJEP-349-branch
changeset 58145 bc54ed8d908a
parent 57470 025c9b8eaefd
child 58197 0ef79bd7fb5c
equal deleted inserted replaced
58129:7b751fe181a5 58145:bc54ed8d908a
       
     1 /*
       
     2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.jfr.internal.consumer;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.util.ArrayList;
       
    30 import java.util.List;
       
    31 
       
    32 import jdk.jfr.EventType;
       
    33 import jdk.jfr.ValueDescriptor;
       
    34 import jdk.jfr.internal.LongMap;
       
    35 import jdk.jfr.internal.MetadataDescriptor;
       
    36 import jdk.jfr.internal.PrivateAccess;
       
    37 import jdk.jfr.internal.Type;
       
    38 import jdk.jfr.internal.consumer.Parser;
       
    39 import jdk.jfr.internal.consumer.RecordingInput;
       
    40 
       
    41 /**
       
    42  * Class that create parsers suitable for reading events and constant pools
       
    43  */
       
    44 final class ParserFactory {
       
    45     private final LongMap<Parser> parsers = new LongMap<>();
       
    46     private final TimeConverter timeConverter;
       
    47     private final LongMap<Type> types = new LongMap<>();
       
    48     private final LongMap<ConstantLookup> constantLookups;
       
    49 
       
    50     public ParserFactory(MetadataDescriptor metadata, LongMap<ConstantLookup> constantLookups, TimeConverter timeConverter) throws IOException {
       
    51         this.constantLookups = constantLookups;
       
    52         this.timeConverter = timeConverter;
       
    53         for (Type t : metadata.getTypes()) {
       
    54             types.put(t.getId(), t);
       
    55         }
       
    56         List<Type> typeList = new ArrayList<>();
       
    57         types.forEach(typeList::add);
       
    58         for (Type t : typeList) {
       
    59             if (!t.getFields().isEmpty()) { // Avoid primitives
       
    60                 CompositeParser cp = createCompositeParser(t, false);
       
    61                 if (t.isSimpleType()) { // Reduce to nested parser
       
    62                     parsers.put(t.getId(), cp.parsers[0]);
       
    63                 }
       
    64 
       
    65             }
       
    66         }
       
    67         // Override event types with event parsers
       
    68         for (EventType t : metadata.getEventTypes()) {
       
    69             parsers.put(t.getId(), createEventParser(t));
       
    70         }
       
    71     }
       
    72 
       
    73     public LongMap<Parser> getParsers() {
       
    74         return parsers;
       
    75     }
       
    76 
       
    77     public LongMap<Type> getTypeMap() {
       
    78         return types;
       
    79     }
       
    80 
       
    81     private EventParser createEventParser(EventType eventType) throws IOException {
       
    82         List<Parser> parsers = new ArrayList<Parser>();
       
    83         for (ValueDescriptor f : eventType.getFields()) {
       
    84             parsers.add(createParser(f, true));
       
    85         }
       
    86         return new EventParser(timeConverter, eventType, parsers.toArray(new Parser[0]));
       
    87     }
       
    88 
       
    89     private Parser createParser(ValueDescriptor v, boolean event) throws IOException {
       
    90         boolean constantPool = PrivateAccess.getInstance().isConstantPool(v);
       
    91         if (v.isArray()) {
       
    92             Type valueType = PrivateAccess.getInstance().getType(v);
       
    93             ValueDescriptor element = PrivateAccess.getInstance().newValueDescriptor(v.getName(), valueType, v.getAnnotationElements(), 0, constantPool, null);
       
    94             return new ArrayParser(createParser(element, event));
       
    95         }
       
    96         long id = v.getTypeId();
       
    97         Type type = types.get(id);
       
    98         if (type == null) {
       
    99             throw new IOException("Type '" + v.getTypeName() + "' is not defined");
       
   100         }
       
   101         if (constantPool) {
       
   102             ConstantLookup lookup = constantLookups.get(id);
       
   103             if (lookup == null) {
       
   104                 ConstantMap pool = new ConstantMap(ObjectFactory.create(type, timeConverter), type.getName());
       
   105                 lookup = new ConstantLookup(pool, type);
       
   106                 constantLookups.put(id, lookup);
       
   107             }
       
   108             if (event) {
       
   109                 return new EventValueConstantParser(lookup);
       
   110             }
       
   111             return new ConstantValueParser(lookup);
       
   112         }
       
   113         Parser parser = parsers.get(id);
       
   114         if (parser == null) {
       
   115             if (!v.getFields().isEmpty()) {
       
   116                 return createCompositeParser(type, event);
       
   117             } else {
       
   118                 return registerParserType(type, createPrimitiveParser(type, constantPool));
       
   119             }
       
   120         }
       
   121         return parser;
       
   122     }
       
   123 
       
   124     private Parser createPrimitiveParser(Type type, boolean event) throws IOException {
       
   125         switch (type.getName()) {
       
   126         case "int":
       
   127             return new IntegerParser();
       
   128         case "long":
       
   129             return new LongParser();
       
   130         case "float":
       
   131             return new FloatParser();
       
   132         case "double":
       
   133             return new DoubleParser();
       
   134         case "char":
       
   135             return new CharacterParser();
       
   136         case "boolean":
       
   137             return new BooleanParser();
       
   138         case "short":
       
   139             return new ShortParser();
       
   140         case "byte":
       
   141             return new ByteParser();
       
   142         case "java.lang.String":
       
   143             ConstantMap pool = new ConstantMap(ObjectFactory.create(type, timeConverter), type.getName());
       
   144             ConstantLookup lookup = new ConstantLookup(pool, type);
       
   145             constantLookups.put(type.getId(), lookup);
       
   146             return new StringParser(lookup, event);
       
   147         default:
       
   148             throw new IOException("Unknown primitive type " + type.getName());
       
   149         }
       
   150     }
       
   151 
       
   152     private Parser registerParserType(Type t, Parser parser) {
       
   153         Parser p = parsers.get(t.getId());
       
   154         // check if parser exists (known type)
       
   155         if (p != null) {
       
   156             return p;
       
   157         }
       
   158         parsers.put(t.getId(), parser);
       
   159         return parser;
       
   160     }
       
   161 
       
   162     private CompositeParser createCompositeParser(Type type, boolean event) throws IOException {
       
   163         List<ValueDescriptor> vds = type.getFields();
       
   164         Parser[] parsers = new Parser[vds.size()];
       
   165         CompositeParser composite = new CompositeParser(parsers);
       
   166         // need to pre-register so recursive types can be handled
       
   167         registerParserType(type, composite);
       
   168 
       
   169         int index = 0;
       
   170         for (ValueDescriptor vd : vds) {
       
   171             parsers[index++] = createParser(vd, event);
       
   172         }
       
   173         return composite;
       
   174     }
       
   175 
       
   176     private static final class BooleanParser extends Parser {
       
   177         @Override
       
   178         public Object parse(RecordingInput input) throws IOException {
       
   179             return input.readBoolean() ? Boolean.TRUE : Boolean.FALSE;
       
   180         }
       
   181 
       
   182         @Override
       
   183         public void skip(RecordingInput input) throws IOException {
       
   184             input.skipBytes(1);
       
   185         }
       
   186     }
       
   187 
       
   188     private static final class ByteParser extends Parser {
       
   189         @Override
       
   190         public Object parse(RecordingInput input) throws IOException {
       
   191             return Byte.valueOf(input.readByte());
       
   192         }
       
   193 
       
   194         @Override
       
   195         public void skip(RecordingInput input) throws IOException {
       
   196             input.skipBytes(1);
       
   197         }
       
   198     }
       
   199 
       
   200     private static final class LongParser extends Parser {
       
   201         private Object lastLongObject = Long.valueOf(0);
       
   202         private long last = 0;
       
   203 
       
   204         @Override
       
   205         public Object parse(RecordingInput input) throws IOException {
       
   206             long l = input.readLong();
       
   207             if (l == last) {
       
   208                 return lastLongObject;
       
   209             }
       
   210             last = l;
       
   211             lastLongObject = Long.valueOf(l);
       
   212             return lastLongObject;
       
   213         }
       
   214 
       
   215         @Override
       
   216         public void skip(RecordingInput input) throws IOException {
       
   217             input.readLong();
       
   218         }
       
   219     }
       
   220 
       
   221     private static final class IntegerParser extends Parser {
       
   222         private Integer lastIntegergObject = Integer.valueOf(0);
       
   223         private int last = 0;
       
   224 
       
   225         @Override
       
   226         public Object parse(RecordingInput input) throws IOException {
       
   227             int i = input.readInt();
       
   228             if (i != last) {
       
   229                 last = i;
       
   230                 lastIntegergObject = Integer.valueOf(i);
       
   231             }
       
   232             return lastIntegergObject;
       
   233         }
       
   234 
       
   235         @Override
       
   236         public void skip(RecordingInput input) throws IOException {
       
   237             input.readInt();
       
   238         }
       
   239     }
       
   240 
       
   241     private static final class ShortParser extends Parser {
       
   242         @Override
       
   243         public Object parse(RecordingInput input) throws IOException {
       
   244             return Short.valueOf(input.readShort());
       
   245         }
       
   246 
       
   247         @Override
       
   248         public void skip(RecordingInput input) throws IOException {
       
   249             input.readShort();
       
   250         }
       
   251     }
       
   252 
       
   253     private static final class CharacterParser extends Parser {
       
   254         @Override
       
   255         public Object parse(RecordingInput input) throws IOException {
       
   256             return Character.valueOf(input.readChar());
       
   257         }
       
   258 
       
   259         @Override
       
   260         public void skip(RecordingInput input) throws IOException {
       
   261             input.readChar();
       
   262         }
       
   263     }
       
   264 
       
   265     private static final class FloatParser extends Parser {
       
   266         @Override
       
   267         public Object parse(RecordingInput input) throws IOException {
       
   268             return Float.valueOf(input.readFloat());
       
   269         }
       
   270 
       
   271         @Override
       
   272         public void skip(RecordingInput input) throws IOException {
       
   273             input.skipBytes(Float.SIZE);
       
   274         }
       
   275     }
       
   276 
       
   277     private static final class DoubleParser extends Parser {
       
   278         @Override
       
   279         public Object parse(RecordingInput input) throws IOException {
       
   280             return Double.valueOf(input.readDouble());
       
   281         }
       
   282 
       
   283         @Override
       
   284         public void skip(RecordingInput input) throws IOException {
       
   285             input.skipBytes(Double.SIZE);
       
   286         }
       
   287     }
       
   288 
       
   289     private final static class ArrayParser extends Parser {
       
   290         private final Parser elementParser;
       
   291 
       
   292         public ArrayParser(Parser elementParser) {
       
   293             this.elementParser = elementParser;
       
   294         }
       
   295 
       
   296         @Override
       
   297         public Object parse(RecordingInput input) throws IOException {
       
   298             final int size = input.readInt();
       
   299             final Object[] array = new Object[size];
       
   300             for (int i = 0; i < size; i++) {
       
   301                 array[i] = elementParser.parse(input);
       
   302             }
       
   303             return array;
       
   304         }
       
   305 
       
   306         @Override
       
   307         public void skip(RecordingInput input) throws IOException {
       
   308             final int size = input.readInt();
       
   309             for (int i = 0; i < size; i++) {
       
   310                 elementParser.skip(input);
       
   311             }
       
   312         }
       
   313     }
       
   314 
       
   315     final static class CompositeParser extends Parser {
       
   316         private final Parser[] parsers;
       
   317 
       
   318         public CompositeParser(Parser[] valueParsers) {
       
   319             this.parsers = valueParsers;
       
   320         }
       
   321 
       
   322         @Override
       
   323         public Object parse(RecordingInput input) throws IOException {
       
   324             final Object[] values = new Object[parsers.length];
       
   325             for (int i = 0; i < values.length; i++) {
       
   326                 values[i] = parsers[i].parse(input);
       
   327             }
       
   328             return values;
       
   329         }
       
   330 
       
   331         @Override
       
   332         public void skip(RecordingInput input) throws IOException {
       
   333             for (int i = 0; i < parsers.length; i++) {
       
   334                 parsers[i].skip(input);
       
   335             }
       
   336         }
       
   337     }
       
   338 
       
   339     public static final class EventValueConstantParser extends Parser {
       
   340         private final ConstantLookup lookup;
       
   341         private Object lastValue = 0;
       
   342         private long lastKey = -1;
       
   343         EventValueConstantParser(ConstantLookup lookup) {
       
   344             this.lookup = lookup;
       
   345         }
       
   346 
       
   347         @Override
       
   348         public Object parse(RecordingInput input) throws IOException {
       
   349             long key = input.readLong();
       
   350             if (key == lastKey) {
       
   351                 return lastValue;
       
   352             }
       
   353             lastKey = key;
       
   354             lastValue = lookup.getCurrentResolved(key);
       
   355             return lastValue;
       
   356         }
       
   357 
       
   358         @Override
       
   359         public void skip(RecordingInput input) throws IOException {
       
   360             input.readLong();
       
   361         }
       
   362     }
       
   363 
       
   364     public static final class ConstantValueParser extends Parser {
       
   365         private final ConstantLookup lookup;
       
   366         ConstantValueParser(ConstantLookup lookup) {
       
   367             this.lookup = lookup;
       
   368         }
       
   369 
       
   370         @Override
       
   371         public Object parse(RecordingInput input) throws IOException {
       
   372             return lookup.getCurrent(input.readLong());
       
   373         }
       
   374 
       
   375         @Override
       
   376         public void skip(RecordingInput input) throws IOException {
       
   377             input.readLong();
       
   378         }
       
   379     }
       
   380 }