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;
  27 
  28 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_CONSTANT_POOL;
  29 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_DEFAULT_VALUE;
  30 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_DIMENSION;
  31 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_GMT_OFFSET;
  32 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_ID;
  33 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_LOCALE;
  34 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_NAME;
  35 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_SIMPLE_TYPE;
  36 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_SUPER_TYPE;
  37 import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_TYPE_ID;
  38 import static jdk.jfr.internal.MetadataDescriptor.ELEMENT_ANNOTATION;
  39 import static jdk.jfr.internal.MetadataDescriptor.ELEMENT_FIELD;
  40 import static jdk.jfr.internal.MetadataDescriptor.ELEMENT_SETTING;
  41 import static jdk.jfr.internal.MetadataDescriptor.ELEMENT_TYPE;
  42 
  43 import java.io.DataOutput;
  44 import java.io.IOException;
  45 import java.util.HashMap;
  46 import java.util.HashSet;
  47 import java.util.List;
  48 import java.util.Set;
  49 
  50 import jdk.jfr.AnnotationElement;
  51 import jdk.jfr.SettingDescriptor;
  52 import jdk.jfr.ValueDescriptor;
  53 import jdk.jfr.internal.MetadataDescriptor.Attribute;
  54 import jdk.jfr.internal.MetadataDescriptor.Element;
  55 import jdk.jfr.internal.consumer.RecordingInput;
  56 
  57 /**
  58  * Class responsible for converting a list of types into a format that can be
  59  * parsed by a client.
  60  *
  61  */
  62 final class MetadataWriter {
  63 
  64     private final Element metadata = new Element("metadata");
  65     private final Element root = new Element("root");
  66 
  67     public MetadataWriter(MetadataDescriptor descriptor) {
  68         descriptor.getTypes().forEach(type -> makeTypeElement(metadata, type));
  69         root.add(metadata);
  70         Element region = new Element("region");
  71         region.addAttribute(ATTRIBUTE_LOCALE, descriptor.locale);
  72         region.addAttribute(ATTRIBUTE_GMT_OFFSET, descriptor.gmtOffset);
  73         root.add(region);
  74     }
  75 
  76     public void writeBinary(DataOutput output) throws IOException {
  77         Set<String> stringPool = new HashSet<>(1000);
  78         // Possible improvement, sort string by how often they occur.
  79         // and assign low number to the most frequently used.
  80         buildStringPool(root, stringPool);
  81         HashMap<String, Integer> lookup = new HashMap<>(stringPool.size());
  82         int index = 0;
  83         int poolSize = stringPool.size();
  84         writeInt(output, poolSize);
  85         for (String s : stringPool) {
  86             lookup.put(s, index);
  87             writeString(output, s);
  88             index++;
  89         }
  90         write(output, root, lookup);
  91     }
  92 
  93     private void writeString(DataOutput out, String s) throws IOException {
  94         if (s == null ) {
  95             out.writeByte(RecordingInput.STRING_ENCODING_NULL);
  96             return;
  97         }
  98         out.writeByte(RecordingInput.STRING_ENCODING_CHAR_ARRAY); // encoding UTF-16
  99         int length = s.length();
 100         writeInt(out, length);
 101             for (int i = 0; i < length; i++) {
 102                 writeInt(out, s.charAt(i));
 103             }
 104     }
 105 
 106     private void writeInt(DataOutput out, int v) throws IOException {
 107 
 108         long s = v & 0xffffffffL;
 109         if (s < 1 << 7) {
 110             out.write((byte) (s));
 111             return;
 112         }
 113         out.write((byte) (s | 0x80)); // first byte written
 114         s >>= 7;
 115         if (s < 1 << 7) {
 116             out.write((byte) (s));
 117             return;
 118         }
 119         out.write((byte) (s | 0x80)); // second byte written
 120         s >>= 7;
 121         if (s < 1 << 7) {
 122             out.write((byte) (s));
 123             return;
 124         }
 125         out.write((byte) (s | 0x80)); // third byte written
 126         s >>= 7;
 127         if (s < 1 << 7) {
 128             out.write((byte) (s));
 129             return;
 130         }
 131         s >>= 7;
 132         out.write((byte) (s));// fourth byte written
 133     }
 134 
 135     private void buildStringPool(Element element, Set<String> pool) {
 136         pool.add(element.name);
 137         for (Attribute a : element.attributes) {
 138             pool.add(a.name);
 139             pool.add(a.value);
 140         }
 141         for (Element child : element.elements) {
 142             buildStringPool(child, pool);
 143         }
 144     }
 145 
 146     private void write(DataOutput output,Element element, HashMap<String, Integer> lookup) throws IOException {
 147         writeInt(output, lookup.get(element.name));
 148         writeInt(output, element.attributes.size());
 149         for (Attribute a : element.attributes) {
 150             writeInt(output, lookup.get(a.name));
 151             writeInt(output, lookup.get(a.value));
 152         }
 153         writeInt(output, element.elements.size());
 154         for (Element child : element.elements) {
 155             write(output, child, lookup);
 156         }
 157     }
 158 
 159     private void makeTypeElement(Element root, Type type) {
 160         Element element = root.newChild(ELEMENT_TYPE);
 161         element.addAttribute(ATTRIBUTE_NAME, type.getName());
 162         String superType = type.getSuperType();
 163         if (superType != null) {
 164             element.addAttribute(ATTRIBUTE_SUPER_TYPE, superType);
 165         }
 166         if (type.isSimpleType()) {
 167             element.addAttribute(ATTRIBUTE_SIMPLE_TYPE, true);
 168         }
 169         element.addAttribute(ATTRIBUTE_ID, type.getId());
 170         if (type instanceof PlatformEventType) {
 171             for (SettingDescriptor v : ((PlatformEventType)type).getSettings()) {
 172                 makeSettingElement(element, v);
 173             }
 174         }
 175         for (ValueDescriptor v : type.getFields()) {
 176             makeFieldElement(element, v);
 177         }
 178         for (AnnotationElement a : type.getAnnotationElements()) {
 179             makeAnnotation(element, a);
 180         }
 181     }
 182 
 183     private void makeSettingElement(Element typeElement, SettingDescriptor s) {
 184         Element element = typeElement.newChild(ELEMENT_SETTING);
 185         element.addAttribute(ATTRIBUTE_NAME, s.getName());
 186         element.addAttribute(ATTRIBUTE_TYPE_ID, s.getTypeId());
 187         element.addAttribute(ATTRIBUTE_DEFAULT_VALUE, s.getDefaultValue());
 188         for (AnnotationElement a : s.getAnnotationElements()) {
 189             makeAnnotation(element, a);
 190         }
 191     }
 192 
 193     private void makeFieldElement(Element typeElement, ValueDescriptor v) {
 194         Element element = typeElement.newChild(ELEMENT_FIELD);
 195         element.addAttribute(ATTRIBUTE_NAME, v.getName());
 196         element.addAttribute(ATTRIBUTE_TYPE_ID, v.getTypeId());
 197         if (v.isArray()) {
 198             element.addAttribute(ATTRIBUTE_DIMENSION, 1);
 199         }
 200         if (PrivateAccess.getInstance().isConstantPool(v)) {
 201             element.addAttribute(ATTRIBUTE_CONSTANT_POOL, true);
 202         }
 203         for (AnnotationElement a : v.getAnnotationElements()) {
 204             makeAnnotation(element, a);
 205         }
 206     }
 207 
 208     private void makeAnnotation(Element entity, AnnotationElement annotation) {
 209         Element element = entity.newChild(ELEMENT_ANNOTATION);
 210         element.addAttribute(ATTRIBUTE_TYPE_ID, annotation.getTypeId());
 211         List<Object> values = annotation.getValues();
 212         int index = 0;
 213         for (ValueDescriptor v : annotation.getValueDescriptors()) {
 214             Object value = values.get(index++);
 215             if (v.isArray()) {
 216                 element.addArrayAttribute(element, v.getName(), value);
 217             } else {
 218                 element.addAttribute(v.getName(), value);
 219             }
 220         }
 221     }
 222 
 223 }