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.rules.jdk.memory;
  34 
  35 import java.text.MessageFormat;
  36 import java.util.Collection;
  37 import java.util.Collections;
  38 import java.util.List;
  39 import java.util.concurrent.Callable;
  40 import java.util.concurrent.FutureTask;
  41 import java.util.concurrent.RunnableFuture;
  42 
  43 import org.openjdk.jmc.common.IMCType;
  44 import org.openjdk.jmc.common.collection.MapToolkit.IntEntry;
  45 import org.openjdk.jmc.common.item.IItemCollection;
  46 import org.openjdk.jmc.common.item.IItemFilter;
  47 import org.openjdk.jmc.common.item.ItemFilters;
  48 import org.openjdk.jmc.common.util.FormatToolkit;
  49 import org.openjdk.jmc.common.util.IPreferenceValueProvider;
  50 import org.openjdk.jmc.common.util.TypedPreference;
  51 import org.openjdk.jmc.flightrecorder.jdk.JdkAttributes;
  52 import org.openjdk.jmc.flightrecorder.jdk.JdkFilters;
  53 import org.openjdk.jmc.flightrecorder.jdk.JdkQueries;
  54 import org.openjdk.jmc.flightrecorder.jdk.JdkTypeIDs;
  55 import org.openjdk.jmc.flightrecorder.rules.IRule;
  56 import org.openjdk.jmc.flightrecorder.rules.Result;
  57 import org.openjdk.jmc.flightrecorder.rules.jdk.dataproviders.StacktraceDataProvider;
  58 import org.openjdk.jmc.flightrecorder.rules.jdk.messages.internal.Messages;
  59 import org.openjdk.jmc.flightrecorder.rules.util.JfrRuleTopics;
  60 import org.openjdk.jmc.flightrecorder.rules.util.RulesToolkit;
  61 import org.openjdk.jmc.flightrecorder.rules.util.RulesToolkit.EventAvailability;
  62 import org.openjdk.jmc.flightrecorder.stacktrace.FrameSeparator;
  63 import org.openjdk.jmc.flightrecorder.stacktrace.FrameSeparator.FrameCategorization;
  64 import org.openjdk.jmc.flightrecorder.stacktrace.StacktraceModel;
  65 import org.openjdk.jmc.flightrecorder.stacktrace.StacktraceModel.Fork;
  66 import org.owasp.encoder.Encode;
  67 
  68 public class AllocationByClassRule implements IRule {
  69         private static final String CLASS_RESULT_ID = "Allocations.class"; //$NON-NLS-1$
  70 
  71         private Result getResult(IItemCollection items, IPreferenceValueProvider valueProvider) {
  72                 EventAvailability eventAvailabilityInside = RulesToolkit.getEventAvailability(items,
  73                                 JdkTypeIDs.ALLOC_INSIDE_TLAB);
  74                 EventAvailability eventAvailabilityOutside = RulesToolkit.getEventAvailability(items,
  75                                 JdkTypeIDs.ALLOC_OUTSIDE_TLAB);
  76                 if (!RulesToolkit.isEventsEnabled(eventAvailabilityInside, eventAvailabilityOutside)) {
  77                         return RulesToolkit.getEventAvailabilityResult(this, items,
  78                                         RulesToolkit.getLeastAvailable(eventAvailabilityInside, eventAvailabilityOutside),
  79                                         JdkTypeIDs.ALLOC_INSIDE_TLAB, JdkTypeIDs.ALLOC_OUTSIDE_TLAB);
  80                 }
  81                 if (!(eventAvailabilityInside == EventAvailability.AVAILABLE
  82                                 || eventAvailabilityOutside == EventAvailability.AVAILABLE)) {
  83                         return RulesToolkit.getNotApplicableResult(this,
  84                                         MessageFormat.format(Messages.getString(Messages.General_RULE_REQUIRES_EVENTS_FROM_ONE_OF_MANY),
  85                                                         JdkTypeIDs.ALLOC_INSIDE_TLAB + ", " + JdkTypeIDs.ALLOC_OUTSIDE_TLAB));
  86                 }
  87 
  88                 List<IntEntry<IMCType>> entries = RulesToolkit.calculateGroupingScore(items.apply(JdkFilters.ALLOC_ALL),
  89                                 JdkAttributes.ALLOCATION_CLASS);
  90 
  91                 double balance = RulesToolkit.calculateBalanceScore(entries);
  92                 IntEntry<IMCType> mostSignificant = entries.get(entries.size() - 1);
  93                 // FIXME: Configuration attribute instead of hard coded 1000 tlabs => relevance 50
  94                 double relevance = RulesToolkit.mapExp100Y(mostSignificant.getValue(), 1000, 50);
  95                 double score = balance * relevance * 0.74; // ceiling at 74;
  96 
  97                 IItemFilter significantFilter = ItemFilters.and(JdkFilters.ALLOC_ALL,
  98                                 ItemFilters.equals(JdkAttributes.ALLOCATION_CLASS, mostSignificant.getKey()));
  99                 StacktraceModel stacktraceModel = new StacktraceModel(false,
 100                                 new FrameSeparator(FrameCategorization.METHOD, false), items.apply(significantFilter));
 101                 Fork rootFork = stacktraceModel.getRootFork();
 102                 String generatedHtmlTraces = rootFork.getBranchCount() == 0
 103                                 ? Messages.getString(Messages.General_NO_STACK_TRACE_AVAILABLE)
 104                                 : StacktraceDataProvider.getRelevantTraceHtmlList(rootFork.getBranch(0), rootFork.getItemsInFork());
 105                 String message = MessageFormat.format(Messages.getString(Messages.AllocationByClassRule_TEXT_MESSAGE),
 106                                 Encode.forHtml(FormatToolkit.getType(mostSignificant.getKey(), true)), generatedHtmlTraces);
 107                 String longMessage = message + "<p>" + Messages.getString(Messages.AllocationRuleFactory_TEXT_CLASS_INFO_LONG); //$NON-NLS-1$
 108                 return new Result(this, score, message, longMessage, JdkQueries.ALLOC_INSIDE_TLAB_BY_CLASS);
 109         }
 110 
 111         @Override
 112         public RunnableFuture<Result> evaluate(final IItemCollection items, final IPreferenceValueProvider valueProvider) {
 113                 FutureTask<Result> evaluationTask = new FutureTask<>(new Callable<Result>() {
 114                         @Override
 115                         public Result call() throws Exception {
 116                                 return getResult(items, valueProvider);
 117                         }
 118                 });
 119                 return evaluationTask;
 120         }
 121 
 122         @Override
 123         public Collection<TypedPreference<?>> getConfigurationAttributes() {
 124                 return Collections.emptyList();
 125         }
 126 
 127         @Override
 128         public String getId() {
 129                 return CLASS_RESULT_ID;
 130         }
 131 
 132         @Override
 133         public String getName() {
 134                 return Messages.getString(Messages.AllocationByClassRule_RULE_NAME);
 135         }
 136 
 137         @Override
 138         public String getTopic() {
 139                 return JfrRuleTopics.HEAP_TOPIC;
 140         }
 141 }