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