src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/ChunkParser.java
changeset 58863 c16ac7a2eba4
parent 52850 f527b24990d7
child 59226 a0f39cc47387
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
       
     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.Collection;
       
    30 import java.util.List;
       
    31 import java.util.StringJoiner;
       
    32 
       
    33 import jdk.jfr.EventType;
       
    34 import jdk.jfr.consumer.RecordedEvent;
       
    35 import jdk.jfr.consumer.RecordedObject;
       
    36 import jdk.jfr.internal.LogLevel;
       
    37 import jdk.jfr.internal.LogTag;
       
    38 import jdk.jfr.internal.Logger;
       
    39 import jdk.jfr.internal.LongMap;
       
    40 import jdk.jfr.internal.MetadataDescriptor;
       
    41 import jdk.jfr.internal.Type;
       
    42 import jdk.jfr.internal.Utils;
       
    43 
       
    44 /**
       
    45  * Parses a chunk.
       
    46  *
       
    47  */
       
    48 public final class ChunkParser {
       
    49 
       
    50     static final class ParserConfiguration {
       
    51         private final boolean reuse;
       
    52         private final boolean ordered;
       
    53         private final ParserFilter eventFilter;
       
    54 
       
    55         long filterStart;
       
    56         long filterEnd;
       
    57 
       
    58         ParserConfiguration(long filterStart, long filterEnd, boolean reuse, boolean ordered, ParserFilter filter) {
       
    59             this.filterStart = filterStart;
       
    60             this.filterEnd = filterEnd;
       
    61             this.reuse = reuse;
       
    62             this.ordered = ordered;
       
    63             this.eventFilter = filter;
       
    64         }
       
    65 
       
    66         public ParserConfiguration() {
       
    67             this(0, Long.MAX_VALUE, false, false, ParserFilter.ACCEPT_ALL);
       
    68         }
       
    69 
       
    70         public boolean isOrdered() {
       
    71             return ordered;
       
    72         }
       
    73     }
       
    74 
       
    75     private enum CheckPointType {
       
    76         // Checkpoint that finishes a flush segment
       
    77         FLUSH(1),
       
    78         // Checkpoint contains chunk header information in the first pool
       
    79         CHUNK_HEADER(2),
       
    80         // Checkpoint contains only statics that will not change from chunk to chunk
       
    81         STATICS(4),
       
    82         // Checkpoint contains thread related information
       
    83         THREAD(8);
       
    84         private final int mask;
       
    85         private CheckPointType(int mask) {
       
    86             this.mask = mask;
       
    87         }
       
    88 
       
    89         private boolean is(int flags) {
       
    90             return (mask & flags) != 0;
       
    91         }
       
    92     }
       
    93 
       
    94     private static final long CONSTANT_POOL_TYPE_ID = 1;
       
    95     private static final String CHUNKHEADER = "jdk.types.ChunkHeader";
       
    96     private final RecordingInput input;
       
    97     private final ChunkHeader chunkHeader;
       
    98     private final MetadataDescriptor metadata;
       
    99     private final TimeConverter timeConverter;
       
   100     private final MetadataDescriptor previousMetadata;
       
   101     private final LongMap<ConstantLookup> constantLookups;
       
   102 
       
   103     private LongMap<Type> typeMap;
       
   104     private LongMap<Parser> parsers;
       
   105     private boolean chunkFinished;
       
   106 
       
   107     private Runnable flushOperation;
       
   108     private ParserConfiguration configuration;
       
   109 
       
   110     public ChunkParser(RecordingInput input) throws IOException {
       
   111         this(input, new ParserConfiguration());
       
   112     }
       
   113 
       
   114     ChunkParser(RecordingInput input, ParserConfiguration pc) throws IOException {
       
   115        this(new ChunkHeader(input), null, pc);
       
   116     }
       
   117 
       
   118     private ChunkParser(ChunkParser previous) throws IOException {
       
   119         this(new ChunkHeader(previous.input), previous, new ParserConfiguration());
       
   120      }
       
   121 
       
   122     private ChunkParser(ChunkHeader header, ChunkParser previous, ParserConfiguration pc) throws IOException {
       
   123         this.configuration = pc;
       
   124         this.input = header.getInput();
       
   125         this.chunkHeader = header;
       
   126         if (previous == null) {
       
   127             this.constantLookups = new LongMap<>();
       
   128             this.previousMetadata = null;
       
   129         } else {
       
   130             this.constantLookups = previous.constantLookups;
       
   131             this.previousMetadata = previous.metadata;
       
   132             this.configuration = previous.configuration;
       
   133         }
       
   134         this.metadata = header.readMetadata(previousMetadata);
       
   135         this.timeConverter = new TimeConverter(chunkHeader, metadata.getGMTOffset());
       
   136         if (metadata != previousMetadata) {
       
   137             ParserFactory factory = new ParserFactory(metadata, constantLookups, timeConverter);
       
   138             parsers = factory.getParsers();
       
   139             typeMap = factory.getTypeMap();
       
   140             updateConfiguration();
       
   141         } else {
       
   142             parsers = previous.parsers;
       
   143             typeMap = previous.typeMap;
       
   144         }
       
   145         constantLookups.forEach(c -> c.newPool());
       
   146         fillConstantPools(0);
       
   147         constantLookups.forEach(c -> c.getLatestPool().setResolving());
       
   148         constantLookups.forEach(c -> c.getLatestPool().resolve());
       
   149         constantLookups.forEach(c -> c.getLatestPool().setResolved());
       
   150 
       
   151         input.position(chunkHeader.getEventStart());
       
   152     }
       
   153 
       
   154     public ChunkParser nextChunkParser() throws IOException {
       
   155         return new ChunkParser(chunkHeader.nextHeader(), this, configuration);
       
   156     }
       
   157 
       
   158     private void updateConfiguration() {
       
   159         updateConfiguration(configuration, false);
       
   160     }
       
   161 
       
   162     void updateConfiguration(ParserConfiguration configuration, boolean resetEventCache) {
       
   163         this.configuration = configuration;
       
   164         parsers.forEach(p -> {
       
   165             if (p instanceof EventParser) {
       
   166                 EventParser ep = (EventParser) p;
       
   167                 if (resetEventCache) {
       
   168                     ep.resetCache();
       
   169                 }
       
   170                 String name = ep.getEventType().getName();
       
   171                 ep.setOrdered(configuration.ordered);
       
   172                 ep.setReuse(configuration.reuse);
       
   173                 ep.setFilterStart(configuration.filterStart);
       
   174                 ep.setFilterEnd(configuration.filterEnd);
       
   175                 long threshold = configuration.eventFilter.getThreshold(name);
       
   176                 if (threshold >= 0) {
       
   177                     ep.setEnabled(true);
       
   178                     ep.setThresholdNanos(threshold);
       
   179                 } else {
       
   180                     ep.setEnabled(false);
       
   181                     ep.setThresholdNanos(Long.MAX_VALUE);
       
   182                 }
       
   183             }
       
   184         });
       
   185     }
       
   186 
       
   187     /**
       
   188      * Reads an event and returns null when segment or chunk ends.
       
   189      *
       
   190      * @param awaitNewEvents wait for new data.
       
   191      */
       
   192     RecordedEvent readStreamingEvent(boolean awaitNewEvents) throws IOException {
       
   193         long absoluteChunkEnd = chunkHeader.getEnd();
       
   194         while (true) {
       
   195             RecordedEvent event = readEvent();
       
   196             if (event != null) {
       
   197                 return event;
       
   198             }
       
   199             if (!awaitNewEvents) {
       
   200                 return null;
       
   201             }
       
   202             long lastValid = absoluteChunkEnd;
       
   203             long metadataPoistion = chunkHeader.getMetataPosition();
       
   204             long contantPosition = chunkHeader.getConstantPoolPosition();
       
   205             chunkFinished = awaitUpdatedHeader(absoluteChunkEnd);
       
   206             if (chunkFinished) {
       
   207                 Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "At chunk end");
       
   208                 return null;
       
   209             }
       
   210             absoluteChunkEnd = chunkHeader.getEnd();
       
   211             // Read metadata and constant pools for the next segment
       
   212             if (chunkHeader.getMetataPosition() != metadataPoistion) {
       
   213                 Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Found new metadata in chunk. Rebuilding types and parsers");
       
   214                 MetadataDescriptor metadata = chunkHeader.readMetadata(previousMetadata);
       
   215                 ParserFactory factory = new ParserFactory(metadata, constantLookups, timeConverter);
       
   216                 parsers = factory.getParsers();
       
   217                 typeMap = factory.getTypeMap();
       
   218                 updateConfiguration();;
       
   219             }
       
   220             if (contantPosition != chunkHeader.getConstantPoolPosition()) {
       
   221                 Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Found new constant pool data. Filling up pools with new values");
       
   222                 constantLookups.forEach(c -> c.getLatestPool().setAllResolved(false));
       
   223                 fillConstantPools(contantPosition + chunkHeader.getAbsoluteChunkStart());
       
   224                 constantLookups.forEach(c -> c.getLatestPool().setResolving());
       
   225                 constantLookups.forEach(c -> c.getLatestPool().resolve());
       
   226                 constantLookups.forEach(c -> c.getLatestPool().setResolved());
       
   227             }
       
   228             input.position(lastValid);
       
   229         }
       
   230     }
       
   231 
       
   232     /**
       
   233      * Reads an event and returns null when the chunk ends
       
   234      */
       
   235     public RecordedEvent readEvent() throws IOException {
       
   236         long absoluteChunkEnd = chunkHeader.getEnd();
       
   237         while (input.position() < absoluteChunkEnd) {
       
   238             long pos = input.position();
       
   239             int size = input.readInt();
       
   240             if (size == 0) {
       
   241                 throw new IOException("Event can't have zero size");
       
   242             }
       
   243             long typeId = input.readLong();
       
   244             Parser p = parsers.get(typeId);
       
   245             if (p instanceof EventParser) {
       
   246                 // Fast path
       
   247                 EventParser ep = (EventParser) p;
       
   248                 RecordedEvent event = ep.parse(input);
       
   249                 if (event != null) {
       
   250                     input.position(pos + size);
       
   251                     return event;
       
   252                 }
       
   253                 // Not accepted by filter
       
   254             } else {
       
   255                 if (typeId == 1) { // checkpoint event
       
   256                     if (flushOperation != null) {
       
   257                         parseCheckpoint();
       
   258                     }
       
   259                 } else {
       
   260                     if (typeId != 0) { // Not metadata event
       
   261                         Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Unknown event type " + typeId);
       
   262                     }
       
   263                 }
       
   264             }
       
   265             input.position(pos + size);
       
   266         }
       
   267         return null;
       
   268     }
       
   269 
       
   270     private void parseCheckpoint() throws IOException {
       
   271         // Content has been parsed previously. This
       
   272         // is to trigger flush
       
   273         input.readLong(); // timestamp
       
   274         input.readLong(); // duration
       
   275         input.readLong(); // delta
       
   276         byte typeFlags = input.readByte();
       
   277         if (CheckPointType.FLUSH.is(typeFlags)) {
       
   278             flushOperation.run();
       
   279         }
       
   280     }
       
   281 
       
   282     private boolean awaitUpdatedHeader(long absoluteChunkEnd) throws IOException {
       
   283         if (Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO)) {
       
   284             Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Waiting for more data (streaming). Read so far: " + chunkHeader.getChunkSize() + " bytes");
       
   285         }
       
   286         while (true) {
       
   287             chunkHeader.refresh();
       
   288             if (absoluteChunkEnd != chunkHeader.getEnd()) {
       
   289                 return false;
       
   290             }
       
   291             if (chunkHeader.isFinished()) {
       
   292                 return true;
       
   293             }
       
   294             Utils.waitFlush(1000);
       
   295         }
       
   296     }
       
   297 
       
   298     private void fillConstantPools(long abortCP) throws IOException {
       
   299         long thisCP = chunkHeader.getConstantPoolPosition() + chunkHeader.getAbsoluteChunkStart();
       
   300         long lastCP = -1;
       
   301         long delta = -1;
       
   302         boolean logTrace = Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE);
       
   303         while (thisCP != abortCP && delta != 0) {
       
   304             input.position(thisCP);
       
   305             lastCP = thisCP;
       
   306             int size = input.readInt(); // size
       
   307             long typeId = input.readLong();
       
   308             if (typeId != CONSTANT_POOL_TYPE_ID) {
       
   309                 throw new IOException("Expected check point event (id = 1) at position " + lastCP + ", but found type id = " + typeId);
       
   310             }
       
   311             input.readLong(); // timestamp
       
   312             input.readLong(); // duration
       
   313             delta = input.readLong();
       
   314             thisCP += delta;
       
   315             boolean flush = input.readBoolean();
       
   316             int poolCount = input.readInt();
       
   317             final long logLastCP = lastCP;
       
   318             final long logDelta = delta;
       
   319             if (logTrace) {
       
   320                 Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, () -> {
       
   321                     return "New constant pool: startPosition=" + logLastCP + ", size=" + size + ", deltaToNext=" + logDelta + ", flush=" + flush + ", poolCount=" + poolCount;
       
   322                 });
       
   323             }
       
   324             for (int i = 0; i < poolCount; i++) {
       
   325                 long id = input.readLong(); // type id
       
   326                 ConstantLookup lookup = constantLookups.get(id);
       
   327                 Type type = typeMap.get(id);
       
   328                 if (lookup == null) {
       
   329                     if (type == null) {
       
   330                         throw new IOException(
       
   331                                 "Error parsing constant pool type " + getName(id) + " at position " + input.position() + " at check point between [" + lastCP + ", " + lastCP + size + "]");
       
   332                     }
       
   333                     if (type.getName() != CHUNKHEADER) {
       
   334                         Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Found constant pool(" + id + ") that is never used");
       
   335                     }
       
   336                     ConstantMap pool = new ConstantMap(ObjectFactory.create(type, timeConverter), type.getName());
       
   337                     lookup = new ConstantLookup(pool, type);
       
   338                     constantLookups.put(type.getId(), lookup);
       
   339                 }
       
   340                 Parser parser = parsers.get(id);
       
   341                 if (parser == null) {
       
   342                     throw new IOException("Could not find constant pool type with id = " + id);
       
   343                 }
       
   344                 try {
       
   345                     int count = input.readInt();
       
   346                     if (count == 0) {
       
   347                         throw new InternalError("Pool " + type.getName() + " must contain at least one element ");
       
   348                     }
       
   349                     if (logTrace) {
       
   350                         Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, "Constant Pool " + i + ": " + type.getName());
       
   351                     }
       
   352                     for (int j = 0; j < count; j++) {
       
   353                         long key = input.readLong();
       
   354                         Object resolved = lookup.getPreviousResolved(key);
       
   355                         if (resolved == null) {
       
   356                             Object v = parser.parse(input);
       
   357                             logConstant(key, v, false);
       
   358                             lookup.getLatestPool().put(key, v);
       
   359                         } else {
       
   360                             parser.skip(input);
       
   361                             logConstant(key, resolved, true);
       
   362                             lookup.getLatestPool().putResolved(key, resolved);
       
   363                         }
       
   364                     }
       
   365                 } catch (Exception e) {
       
   366                     throw new IOException("Error parsing constant pool type " + getName(id) + " at position " + input.position() + " at check point between [" + lastCP + ", " + lastCP + size + "]",
       
   367                             e);
       
   368                 }
       
   369             }
       
   370             if (input.position() != lastCP + size) {
       
   371                 throw new IOException("Size of check point event doesn't match content");
       
   372             }
       
   373         }
       
   374     }
       
   375 
       
   376     private void logConstant(long key, Object v, boolean preresolved) {
       
   377         if (!Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE)) {
       
   378             return;
       
   379         }
       
   380         String valueText;
       
   381         if (v.getClass().isArray()) {
       
   382             Object[] array = (Object[]) v;
       
   383             StringJoiner sj = new StringJoiner(", ", "{", "}");
       
   384             for (int i = 0; i < array.length; i++) {
       
   385                 sj.add(textify(array[i]));
       
   386             }
       
   387             valueText = sj.toString();
       
   388         } else {
       
   389             valueText = textify(v);
       
   390         }
       
   391         String suffix  = preresolved ? " (presolved)" :"";
       
   392         Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, "Constant: " + key + " = " + valueText + suffix);
       
   393     }
       
   394 
       
   395     private String textify(Object o) {
       
   396         if (o == null) { // should not happen
       
   397             return "null";
       
   398         }
       
   399         if (o instanceof String) {
       
   400             return "\"" + String.valueOf(o) + "\"";
       
   401         }
       
   402         if (o instanceof RecordedObject) {
       
   403             return o.getClass().getName();
       
   404         }
       
   405         if (o.getClass().isArray()) {
       
   406             Object[] array = (Object[]) o;
       
   407             if (array.length > 0) {
       
   408                 return textify(array[0]) + "[]"; // can it be recursive?
       
   409             }
       
   410         }
       
   411         return String.valueOf(o);
       
   412     }
       
   413 
       
   414     private String getName(long id) {
       
   415         Type type = typeMap.get(id);
       
   416         return type == null ? ("unknown(" + id + ")") : type.getName();
       
   417     }
       
   418 
       
   419     public Collection<Type> getTypes() {
       
   420         return metadata.getTypes();
       
   421     }
       
   422 
       
   423     public List<EventType> getEventTypes() {
       
   424         return metadata.getEventTypes();
       
   425     }
       
   426 
       
   427     public boolean isLastChunk() throws IOException {
       
   428         return chunkHeader.isLastChunk();
       
   429     }
       
   430 
       
   431     ChunkParser newChunkParser() throws IOException {
       
   432         return new ChunkParser(this);
       
   433     }
       
   434 
       
   435     public boolean isChunkFinished() {
       
   436         return chunkFinished;
       
   437     }
       
   438 
       
   439     public void setFlushOperation(Runnable flushOperation) {
       
   440         this.flushOperation = flushOperation;
       
   441     }
       
   442 
       
   443     public long getChunkDuration() {
       
   444         return chunkHeader.getDurationNanos();
       
   445     }
       
   446 
       
   447     public long getStartNanos() {
       
   448         return chunkHeader.getStartNanos();
       
   449     }
       
   450 
       
   451 }