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