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.parser.synthetic;
  34 
  35 import static org.openjdk.jmc.common.item.Attribute.attr;
  36 import static org.openjdk.jmc.common.unit.UnitLookup.FLAG;
  37 import static org.openjdk.jmc.common.unit.UnitLookup.TIMESPAN;
  38 
  39 import java.util.ArrayList;
  40 import java.util.Arrays;
  41 import java.util.HashMap;
  42 import java.util.List;
  43 import java.util.Map;
  44 
  45 import org.openjdk.jmc.common.item.IAttribute;
  46 import org.openjdk.jmc.common.unit.IQuantity;
  47 import org.openjdk.jmc.common.util.LabeledIdentifier;
  48 import org.openjdk.jmc.flightrecorder.JfrAttributes;
  49 import org.openjdk.jmc.flightrecorder.jdk.JdkAttributes;
  50 import org.openjdk.jmc.flightrecorder.jdk.JdkTypeIDs;
  51 import org.openjdk.jmc.flightrecorder.parser.IEventSink;
  52 import org.openjdk.jmc.flightrecorder.parser.IEventSinkFactory;
  53 import org.openjdk.jmc.flightrecorder.parser.ValueField;
  54 
  55 /**
  56  * Event sink that transforms pre JDK 9 event types to their equivalent JDK 9 types. JDK 9 input
  57  * data will be passed through mostly untouched.
  58  */
  59 class SettingsTransformer implements IEventSink {
  60 
  61         /**
  62          * Fix for JDK-8157024, the code cache stats unallocatedCapacity event is written as KiB but
  63          * reported as B. This is fixed in JDK9, but we need to perform this transformation for
  64          * recordings from JDK8 and earlier.
  65          */
  66         private static class FixCodeCacheSink implements IEventSink {
  67 
  68                 private int unallocatedFieldIndex;
  69                 private IEventSink subSink;
  70 
  71                 public FixCodeCacheSink(int unallocatedFieldIndex, IEventSink subSink) {
  72                         this.unallocatedFieldIndex = unallocatedFieldIndex;
  73                         this.subSink = subSink;
  74                 }
  75 
  76                 @Override
  77                 public void addEvent(Object[] values) {
  78                         if (values[unallocatedFieldIndex] instanceof IQuantity) {
  79                                 values[unallocatedFieldIndex] = ((IQuantity) values[unallocatedFieldIndex]).multiply(1024);
  80                         }
  81                         subSink.addEvent(values);
  82                 }
  83 
  84         }
  85 
  86         // TODO: Break out constants?
  87         static final String REC_SETTING_NAME_ENABLED = "enabled"; //$NON-NLS-1$
  88         static final String REC_SETTING_NAME_STACKTRACE = "stacktrace"; //$NON-NLS-1$
  89         static final String REC_SETTING_NAME_THRESHOLD = "threshold"; //$NON-NLS-1$
  90         static final String REC_SETTING_NAME_PERIOD = "period"; //$NON-NLS-1$
  91 
  92         static final String REC_SETTING_PERIOD_EVERY_CHUNK = "everyChunk"; //$NON-NLS-1$
  93 
  94         private static final IAttribute<Boolean> REC_SETTINGS_ATTR_ENABLED = attr("enabled", //$NON-NLS-1$
  95                         Messages.getString(Messages.SettingsTransformer_REC_SETTINGS_ATTR_ENABLED), FLAG);
  96         private static final IAttribute<Boolean> REC_SETTINGS_ATTR_STACKTRACE = attr("stacktrace", //$NON-NLS-1$
  97                         Messages.getString(Messages.SettingsTransformer_REC_SETTINGS_ATTR_STACKTRACE), FLAG);
  98         static final IAttribute<IQuantity> REC_SETTINGS_ATTR_THRESHOLD = attr("threshold", //$NON-NLS-1$
  99                         Messages.getString(Messages.SettingsTransformer_REC_SETTINGS_ATTR_THRESHOLD), TIMESPAN);
 100         static final IAttribute<IQuantity> REC_SETTINGS_ATTR_PERIOD = attr("period", //$NON-NLS-1$
 101                         Messages.getString(Messages.SettingsTransformer_REC_SETTINGS_ATTR_PERIOD), TIMESPAN);
 102         private static final List<ValueField> FIELDS = Arrays.asList(new ValueField(JfrAttributes.END_TIME),
 103                         new ValueField(SyntheticAttributeExtension.REC_SETTING_EVENT_ID_ATTRIBUTE),
 104                         new ValueField(JdkAttributes.REC_SETTING_NAME), new ValueField(JdkAttributes.REC_SETTING_VALUE));
 105 
 106         // Renamed attributes from pre JDK 9: <event id, <pre 9 attribute id, 9 attribute id>>
 107         private static final Map<String, Map<String, String>> attributeRenameMap;
 108 
 109         // JDK-8157024 constant for the field id
 110         private static final String UNALLOCATED_CAPACITY_FIELD_ID = "unallocatedCapacity"; //$NON-NLS-1$
 111 
 112         private final IEventSink sink;
 113         private final Object[] reusableArray = new Object[FIELDS.size()];
 114         private int endTimeIndex = -1;
 115         private int typeIndex = -1;
 116         private int enabledIndex = -1;
 117         private int stacktraceIndex = -1;
 118         private int thresholdIndex = -1;
 119         private int periodIndex = -1;
 120 
 121         static {
 122                 attributeRenameMap = buildRenameMap();
 123         }
 124 
 125         @SuppressWarnings("nls")
 126         private static HashMap<String, Map<String, String>> buildRenameMap() {
 127                 // NOTE: Replace the last string argument with an identifier reference if a matching one is added to JfrAttributes.
 128                 HashMap<String, Map<String, String>> map = new HashMap<>();
 129                 addRenameEntry(map, JdkTypeIDsPreJdk9.THREAD_PARK, "klass", "parkedClass");
 130                 addRenameEntry(map, JdkTypeIDsPreJdk9.MONITOR_ENTER, "klass", JdkAttributes.MONITOR_CLASS.getIdentifier());
 131                 addRenameEntry(map, JdkTypeIDsPreJdk9.MONITOR_WAIT, "klass", JdkAttributes.MONITOR_CLASS.getIdentifier());
 132                 addRenameEntry(map, JdkTypeIDsPreJdk9.INT_FLAG_CHANGED, "old_value", "oldValue");
 133                 addRenameEntry(map, JdkTypeIDsPreJdk9.INT_FLAG_CHANGED, "new_value", "newValue");
 134                 addRenameEntry(map, JdkTypeIDsPreJdk9.UINT_FLAG_CHANGED, "old_value", "oldValue");
 135                 addRenameEntry(map, JdkTypeIDsPreJdk9.UINT_FLAG_CHANGED, "new_value", "newValue");
 136                 addRenameEntry(map, JdkTypeIDsPreJdk9.LONG_FLAG_CHANGED, "old_value", "oldValue");
 137                 addRenameEntry(map, JdkTypeIDsPreJdk9.LONG_FLAG_CHANGED, "new_value", "newValue");
 138                 addRenameEntry(map, JdkTypeIDsPreJdk9.ULONG_FLAG_CHANGED, "old_value", "oldValue");
 139                 addRenameEntry(map, JdkTypeIDsPreJdk9.ULONG_FLAG_CHANGED, "new_value", "newValue");
 140                 addRenameEntry(map, JdkTypeIDsPreJdk9.DOUBLE_FLAG_CHANGED, "old_value", "oldValue");
 141                 addRenameEntry(map, JdkTypeIDsPreJdk9.DOUBLE_FLAG_CHANGED, "new_value", "newValue");
 142                 addRenameEntry(map, JdkTypeIDsPreJdk9.BOOLEAN_FLAG_CHANGED, "old_value", "oldValue");
 143                 addRenameEntry(map, JdkTypeIDsPreJdk9.BOOLEAN_FLAG_CHANGED, "new_value", "newValue");
 144                 addRenameEntry(map, JdkTypeIDsPreJdk9.STRING_FLAG_CHANGED, "old_value", "oldValue");
 145                 addRenameEntry(map, JdkTypeIDsPreJdk9.STRING_FLAG_CHANGED, "new_value", "newValue");
 146                 addRenameEntry(map, JdkTypeIDsPreJdk9.GC_DETAILED_EVACUATION_INFO, "allocRegionsUsedBefore",
 147                                 "allocationRegionsUsedBefore");
 148                 addRenameEntry(map, JdkTypeIDsPreJdk9.GC_DETAILED_EVACUATION_INFO, "allocRegionsUsedAfter",
 149                                 "allocationRegionsUsedAfter");
 150                 addRenameEntry(map, JdkTypeIDsPreJdk9.SWEEP_CODE_CACHE, "sweepIndex", "sweepId");
 151                 addRenameEntry(map, JdkTypeIDsPreJdk9.ALLOC_INSIDE_TLAB, "class",
 152                                 JdkAttributes.ALLOCATION_CLASS.getIdentifier());
 153                 addRenameEntry(map, JdkTypeIDsPreJdk9.ALLOC_OUTSIDE_TLAB, "class",
 154                                 JdkAttributes.ALLOCATION_CLASS.getIdentifier());
 155                 addRenameEntry(map, JdkTypeIDsPreJdk9.OBJECT_COUNT, "class", JdkAttributes.OBJECT_CLASS.getIdentifier());
 156                 addRenameEntry(map, JdkTypeIDsPreJdk9.COMPILER_PHASE, "compileID",
 157                                 JdkAttributes.COMPILER_COMPILATION_ID.getIdentifier());
 158                 addRenameEntry(map, JdkTypeIDsPreJdk9.COMPILATION, "compileID",
 159                                 JdkAttributes.COMPILER_COMPILATION_ID.getIdentifier());
 160                 addRenameEntry(map, JdkTypeIDsPreJdk9.COMPILER_FAILURE, "compileID",
 161                                 JdkAttributes.COMPILER_COMPILATION_ID.getIdentifier());
 162                 addRenameEntry(map, JdkTypeIDsPreJdk9.COMPILER_FAILURE, "failure",
 163                                 JdkAttributes.COMPILER_FAILED_MESSAGE.getIdentifier());
 164                 addRenameEntry(map, JdkTypeIDsPreJdk9.GC_DETAILED_OBJECT_COUNT_AFTER_GC, "class",
 165                                 JdkAttributes.OBJECT_CLASS.getIdentifier());
 166                 return map;
 167         }
 168 
 169         private static void addRenameEntry(
 170                 Map<String, Map<String, String>> renameMap, String eventId, String pre9AttrId, String attrId) {
 171                 Map<String, String> attrMap = renameMap.get(eventId);
 172                 if (attrMap == null) {
 173                         attrMap = new HashMap<>();
 174                         renameMap.put(eventId, attrMap);
 175                 }
 176                 attrMap.put(pre9AttrId, attrId);
 177         }
 178 
 179         SettingsTransformer(IEventSinkFactory sinkFactory, String label, String[] category, String description,
 180                         List<ValueField> dataStructure) {
 181                 sink = sinkFactory.create(JdkTypeIDs.RECORDING_SETTING, label, category, description, FIELDS);
 182                 for (int i = 0; i < dataStructure.size(); i++) {
 183                         ValueField vf = dataStructure.get(i);
 184                         if (vf.matches(JfrAttributes.END_TIME)) {
 185                                 endTimeIndex = i;
 186                         } else if (vf.matches(SyntheticAttributeExtension.REC_SETTING_EVENT_ID_ATTRIBUTE)) {
 187                                 typeIndex = i;
 188                         } else if (vf.matches(REC_SETTINGS_ATTR_ENABLED)) {
 189                                 enabledIndex = i;
 190                         } else if (vf.matches(REC_SETTINGS_ATTR_STACKTRACE)) {
 191                                 stacktraceIndex = i;
 192                         } else if (vf.matches(REC_SETTINGS_ATTR_THRESHOLD)) {
 193                                 thresholdIndex = i;
 194                         } else if (vf.matches(REC_SETTINGS_ATTR_PERIOD)) {
 195                                 periodIndex = i;
 196                         }
 197                 }
 198         }
 199 
 200         boolean isValid() {
 201                 return endTimeIndex >= 0 && typeIndex >= 0 && enabledIndex >= 0 && stacktraceIndex >= 0 && thresholdIndex >= 0
 202                                 && periodIndex >= 0;
 203         }
 204 
 205         @Override
 206         public void addEvent(Object[] values) {
 207                 LabeledIdentifier type = (LabeledIdentifier) values[typeIndex];
 208                 if (type != null) {
 209                         type = new LabeledIdentifier(JdkTypeIDsPreJdk9.translate(type.getInterfaceId()), type.getImplementationId(),
 210                                         type.getName(), type.getDeclaredDescription());
 211                 }
 212                 Object startTime = values[endTimeIndex];
 213 
 214                 addSettingEvent(startTime, type, REC_SETTING_NAME_ENABLED, values[enabledIndex]);
 215                 addSettingEvent(startTime, type, REC_SETTING_NAME_STACKTRACE, values[stacktraceIndex]);
 216                 addThresholdSettingEvent(startTime, type, (IQuantity) values[thresholdIndex]);
 217                 addPeriodSettingEvent(startTime, type, (IQuantity) values[periodIndex]);
 218         }
 219 
 220         private boolean addThresholdSettingEvent(Object startTime, LabeledIdentifier type, IQuantity quantity) {
 221                 // Remove thresholds with Long.MIN_VALUE ns duration as these are just padding for
 222                 // event types that cannot have thresholds. (At least JDK 7u75 used Long.MAX_VALUE.)
 223                 if (quantity != null) {
 224                         long numQuantity = quantity.longValue();
 225                         if ((numQuantity != Long.MIN_VALUE) && (numQuantity != Long.MAX_VALUE)) {
 226                                 addSettingEvent(startTime, type, REC_SETTING_NAME_THRESHOLD, quantity.persistableString());
 227                         }
 228                 }
 229                 return false;
 230         }
 231 
 232         private boolean addPeriodSettingEvent(Object startTime, LabeledIdentifier type, IQuantity quantity) {
 233                 // Similar to threshold. Seems to be similar but almost the opposite for period, which at least in JDK 8u40
 234                 // uses Long.MIN_VALUE ms for event types that can have a period, but none have been defined.
 235                 if (quantity != null) {
 236                         long numQuantity = quantity.longValue();
 237                         if (numQuantity == 0L) {
 238                                 addSettingEvent(startTime, type, REC_SETTING_NAME_PERIOD, REC_SETTING_PERIOD_EVERY_CHUNK);
 239                         } else if ((numQuantity != Long.MIN_VALUE) && (numQuantity != Long.MAX_VALUE)) {
 240                                 addSettingEvent(startTime, type, REC_SETTING_NAME_PERIOD, quantity.persistableString());
 241                         }
 242                 }
 243                 return false;
 244         }
 245 
 246         private void addSettingEvent(Object startTime, LabeledIdentifier type, String settingName, Object settingValue) {
 247                 reusableArray[0] = startTime;
 248                 reusableArray[1] = type;
 249                 reusableArray[2] = settingName;
 250                 reusableArray[3] = settingValue == null ? null : settingValue.toString();
 251                 sink.addEvent(reusableArray);
 252         }
 253 
 254         /*
 255          * FIXME: Weird to explicitly wrap when the parser does exactly that.
 256          *
 257          * This class should be refactored into a parser extension although this may require a change to
 258          * the API by adding priorities so that type transformation occurs before synthetic attributes
 259          * are added.
 260          */
 261         static IEventSinkFactory wrapSinkFactory(final IEventSinkFactory subFactory) {
 262                 return new IEventSinkFactory() {
 263 
 264                         @Override
 265                         public IEventSink create(
 266                                 String identifier, String label, String[] category, String description,
 267                                 List<ValueField> dataStructure) {
 268                                 if (JdkTypeIDsPreJdk9.RECORDING_SETTING.equals(identifier)) {
 269                                         SettingsTransformer st = new SettingsTransformer(subFactory, label, category, description,
 270                                                         dataStructure);
 271                                         if (st.isValid()) {
 272                                                 return st;
 273                                         } else {
 274                                                 // FIXME: Avoid System.err.println
 275                                                 System.err
 276                                                                 .println("Cannot create SettingsTransformer from fields: " + dataStructure.toString()); //$NON-NLS-1$
 277                                         }
 278                                 } else if (JdkTypeIDsPreJdk9.RECORDINGS.equals(identifier)) {
 279                                         /*
 280                                          * NOTE: Renaming 'duration' and 'startTime' attributes for JDK 8 'Recording'
 281                                          * events so that they won't conflict with general attributes with the same
 282                                          * names in JDK 9+ recordings.
 283                                          */
 284                                         ValueField[] struct = new ValueField[dataStructure.size()];
 285                                         for (int i = 0; i < struct.length; i++) {
 286                                                 ValueField vf = dataStructure.get(i);
 287                                                 if (vf.matches(JfrAttributes.START_TIME)) {
 288                                                         vf = new ValueField(JdkAttributes.RECORDING_START);
 289                                                 } else if (vf.matches(JfrAttributes.DURATION)) {
 290                                                         vf = new ValueField(JdkAttributes.RECORDING_DURATION);
 291                                                 }
 292                                                 struct[i] = vf;
 293                                         }
 294                                         return subFactory.create(JdkTypeIDs.RECORDINGS, label, category, description,
 295                                                         Arrays.asList(struct));
 296                                 } else if (JdkTypeIDsPreJdk9.CODE_CACHE_STATISTICS.equals(identifier)) {
 297                                         for (int i = 0; i < dataStructure.size(); i++) {
 298                                                 if (UNALLOCATED_CAPACITY_FIELD_ID.equals(dataStructure.get(i).getIdentifier())) {
 299                                                         return new FixCodeCacheSink(i, subFactory.create(JdkTypeIDsPreJdk9.translate(identifier),
 300                                                                         label, category, description, dataStructure));
 301                                                 }
 302                                         }
 303                                 }
 304                                 return subFactory.create(JdkTypeIDsPreJdk9.translate(identifier), label, category, description,
 305                                                 translate(identifier, dataStructure));
 306                         }
 307 
 308                         private List<ValueField> translate(String identifier, List<ValueField> dataStructure) {
 309                                 Map<String, String> attrMap = attributeRenameMap.get(identifier);
 310                                 if (attrMap == null) {
 311                                         return dataStructure;
 312                                 }
 313                                 List<ValueField> renamedDataStructure = new ArrayList<>();
 314                                 for (ValueField vf : dataStructure) {
 315                                         String renamedId = attrMap.get(vf.getIdentifier());
 316                                         if (renamedId == null) {
 317                                                 renamedDataStructure.add(vf);
 318                                         } else {
 319                                                 renamedDataStructure
 320                                                                 .add(new ValueField(renamedId, vf.getName(), vf.getDescription(), vf.getContentType()));
 321                                         }
 322                                 }
 323                                 return renamedDataStructure;
 324                         }
 325 
 326                         @Override
 327                         public void flush() {
 328                                 subFactory.flush();
 329                         }
 330                 };
 331         }
 332 
 333 }