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