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.general; 34 35 import java.text.MessageFormat; 36 import java.util.Arrays; 37 import java.util.Collection; 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.IDisplayable; 44 import org.openjdk.jmc.common.item.Aggregators; 45 import org.openjdk.jmc.common.item.IItemCollection; 46 import org.openjdk.jmc.common.unit.IQuantity; 47 import org.openjdk.jmc.common.unit.UnitLookup; 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.JdkAggregators; 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.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 62 public class ClassLoadingRule implements IRule { 63 64 private static final String RESULT_ID = "ClassLoading"; //$NON-NLS-1$ 65 public static final TypedPreference<IQuantity> MAX_DURATION_LIMIT = new TypedPreference<>( 66 "classloading.duration.max.limit", //$NON-NLS-1$ 67 Messages.getString(Messages.ClassLoadingRule_CONFIG_DURATION_LIMIT), 68 Messages.getString(Messages.ClassLoadingRule_CONFIG_DURATION_LIMIT_LONG), UnitLookup.TIMESPAN, 69 UnitLookup.MILLISECOND.quantity(1000L)); 70 public static final TypedPreference<IQuantity> RATIO_OF_TOTAL_LIMIT = new TypedPreference<>( 71 "classloading.ratio-to-total.limit", //$NON-NLS-1$ 72 Messages.getString(Messages.ClassLoadingRule_CONFIG_RATIO_LIMIT), 73 Messages.getString(Messages.ClassLoadingRule_CONFIG_RATIO_LIMIT_LONG), UnitLookup.NUMBER, 74 UnitLookup.NUMBER_UNITY.quantity(0.10)); 75 76 private static final List<TypedPreference<?>> CONFIG_ATTRIBUTES = Arrays 77 .<TypedPreference<?>> asList(MAX_DURATION_LIMIT, RATIO_OF_TOTAL_LIMIT); 78 79 private Result getResult(IItemCollection items, IPreferenceValueProvider valueProvider) { 80 EventAvailability eventAvailability = RulesToolkit.getEventAvailability(items, JdkTypeIDs.CLASS_LOAD); 81 if (eventAvailability == EventAvailability.UNKNOWN || eventAvailability == EventAvailability.DISABLED) { 82 return RulesToolkit.getEventAvailabilityResult(this, items, eventAvailability, JdkTypeIDs.CLASS_LOAD); 83 } 84 85 IQuantity maxDurationLimit = valueProvider.getPreferenceValue(MAX_DURATION_LIMIT); 86 IQuantity ratioOfTotalLimit = valueProvider.getPreferenceValue(RATIO_OF_TOTAL_LIMIT); 87 88 IItemCollection events = items.apply(JdkFilters.CLASS_LOAD); 89 90 IQuantity startTime = events.getAggregate(JdkAggregators.FIRST_ITEM_START); 91 IQuantity endTime = events.getAggregate(JdkAggregators.LAST_ITEM_END); 92 if (startTime != null && endTime != null) { 93 IQuantity totalTime = endTime.subtract(startTime); 94 IQuantity max = events.getAggregate(Aggregators.max(JfrAttributes.DURATION)); 95 IQuantity sum = events.getAggregate(Aggregators.sum(JfrAttributes.DURATION)); 96 // FIXME: Consider using a score function instead of set value. 97 if ((max.compareTo(maxDurationLimit) > 0) || (sum.ratioTo(totalTime) > ratioOfTotalLimit.doubleValue())) { 98 String totalTimeString = sum.displayUsing(IDisplayable.AUTO); 99 String maxTimeString = max.displayUsing(IDisplayable.AUTO); 100 String loadCountString = events.getAggregate(Aggregators.count()).displayUsing(IDisplayable.AUTO); 101 return new Result(this, 50, 102 MessageFormat.format(Messages.getString(Messages.ClassLoadingRuleFactory_TEXT_INFO), 103 totalTimeString, loadCountString), 104 MessageFormat.format(Messages.getString(Messages.ClassLoadingRuleFactory_TEXT_INFO_LONG), 105 totalTimeString, loadCountString, maxTimeString), 106 JdkQueries.CLASS_LOAD); 107 } 108 } 109 return new Result(this, 0, Messages.getString(Messages.ClassLoadingRuleFactory_RULE_TEXT_OK)); 110 } 111 112 @Override 113 public RunnableFuture<Result> evaluate(final IItemCollection items, final IPreferenceValueProvider valueProvider) { 114 FutureTask<Result> evaluationTask = new FutureTask<>(new Callable<Result>() { 115 @Override 116 public Result call() throws Exception { 117 return getResult(items, valueProvider); 118 } 119 }); 120 return evaluationTask; 121 } 122 123 @Override 124 public Collection<TypedPreference<?>> getConfigurationAttributes() { 125 return CONFIG_ATTRIBUTES; 126 } 127 128 @Override 129 public String getId() { 130 return RESULT_ID; 131 } 132 133 @Override 134 public String getName() { 135 return Messages.getString(Messages.ClassLoadingRuleFactory_RULE_NAME); 136 } 137 138 @Override 139 public String getTopic() { 140 return JfrRuleTopics.CLASS_LOADING_TOPIC; 141 } 142 143 }