1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  *
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  *
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  *
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  *
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.flightrecorder.internal.parser.v1;
  34 
  35 import java.io.IOException;
  36 import java.util.List;
  37 
  38 import org.openjdk.jmc.flightrecorder.CouldNotLoadRecordingException;
  39 import org.openjdk.jmc.flightrecorder.internal.ChunkInfo;
  40 import org.openjdk.jmc.flightrecorder.internal.IChunkLoader;
  41 import org.openjdk.jmc.flightrecorder.internal.InvalidJfrFileException;
  42 import org.openjdk.jmc.flightrecorder.internal.parser.Chunk;
  43 import org.openjdk.jmc.flightrecorder.internal.parser.LoaderContext;
  44 import org.openjdk.jmc.flightrecorder.internal.parser.v1.ChunkMetadata.ClassElement;
  45 import org.openjdk.jmc.flightrecorder.internal.util.ParserToolkit;
  46 
  47 public class ChunkLoaderV1 implements IChunkLoader {
  48 
  49         private final static long CONSTANT_POOL_EVENT_TYPE = 1;
  50 
  51         private final ChunkStructure header;
  52         private final byte[] data;
  53         private final LoaderContext context;
  54 
  55         public ChunkLoaderV1(ChunkStructure header, byte[] data, LoaderContext context) {
  56                 this.header = header;
  57                 this.data = data;
  58                 this.context = context;
  59         }
  60 
  61         @Override
  62         public byte[] call() throws Exception {
  63                 SeekableInputStream input = SeekableInputStream.build(data, header.isIntegersCompressed());
  64 
  65                 // Read metadata
  66                 input.seek(header.getMetadataOffset());
  67                 List<ClassElement> classes = ChunkMetadata.readMetadata(input).metadata.classes;
  68                 TypeManager manager = new TypeManager(classes, context, header);
  69 
  70                 // Read constants
  71                 long constantPoolOffset = 0;
  72                 // An initial constantPoolOffset of 0 indicates no constant pools.
  73                 long delta = header.getConstantPoolOffset();
  74                 while (delta != 0) {
  75                         constantPoolOffset += delta;
  76                         input.seek(constantPoolOffset);
  77                         delta = readConstantPoolEvent(input, manager);
  78                 }
  79                 manager.resolveConstants();
  80 
  81                 // Read events
  82                 long index = header.getBodyStartOffset();
  83                 while (index < header.getChunkSize()) {
  84                         input.seek(index);
  85                         int size = input.readInt();
  86                         long type = input.readLong();
  87                         if (type != CONSTANT_POOL_EVENT_TYPE && type != ChunkMetadata.METADATA_EVENT_TYPE) {
  88                                 manager.readEvent(type, input);
  89                         }
  90                         index += size;
  91                 }
  92                 return data;
  93         }
  94 
  95         private static long readConstantPoolEvent(IDataInput input, TypeManager manager)
  96                         throws IOException, InvalidJfrFileException {
  97                 input.readInt(); // size
  98                 ParserToolkit.assertValue(input.readLong(), CONSTANT_POOL_EVENT_TYPE); // type;
  99                 input.readLong(); // start
 100                 input.readLong(); // duration
 101                 long delta = input.readLong();
 102                 input.readBoolean(); // flush
 103                 int poolCount = input.readInt();
 104                 for (int i = 0; i < poolCount; i++) {
 105                         long classId = input.readLong();
 106                         int constantCount = input.readInt();
 107                         manager.readConstants(classId, input, constantCount);
 108                 }
 109                 return delta;
 110         }
 111 
 112         public static IChunkLoader create(Chunk input, LoaderContext context)
 113                         throws IOException, CouldNotLoadRecordingException {
 114                 ChunkStructure header = new ChunkStructure(input);
 115                 byte[] data = input.fill(header.getChunkSize());
 116                 return new ChunkLoaderV1(header, data, context);
 117         }
 118 
 119         public static ChunkInfo getInfo(Chunk input, long position) throws IOException, CouldNotLoadRecordingException {
 120                 ChunkStructure header = new ChunkStructure(input);
 121                 return new ChunkInfo(position, header.getChunkSize(), header.getChunkRange());
 122         }
 123 
 124         @Override
 125         public long getTimestamp() {
 126                 return header.getStartTimeNanos();
 127         }
 128 }