|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 2025, 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 | +package com.oracle.svm.core.genscavenge; |
| 26 | + |
| 27 | +import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE; |
| 28 | +import static com.oracle.svm.core.genscavenge.AdaptiveWeightedAverage.OLD_THRESHOLD; |
| 29 | + |
| 30 | +import org.graalvm.nativeimage.c.struct.RawField; |
| 31 | +import org.graalvm.nativeimage.c.struct.RawStructure; |
| 32 | +import org.graalvm.word.PointerBase; |
| 33 | +import org.graalvm.word.UnsignedWord; |
| 34 | + |
| 35 | +import com.oracle.svm.core.Uninterruptible; |
| 36 | +import com.oracle.svm.core.jdk.UninterruptibleUtils; |
| 37 | +import com.oracle.svm.core.util.UnsignedUtils; |
| 38 | + |
| 39 | +/** |
| 40 | + * This class provides a raw structure implementation of {@link AdaptiveWeightedAverage}. For |
| 41 | + * further information see {@link AdaptiveWeightedAverage}. |
| 42 | + */ |
| 43 | +class AdaptiveWeightedAverageStruct { |
| 44 | + |
| 45 | + @RawStructure |
| 46 | + interface Data extends PointerBase { |
| 47 | + |
| 48 | + @RawField |
| 49 | + void setWeight(double weight); |
| 50 | + |
| 51 | + @RawField |
| 52 | + double getWeight(); |
| 53 | + |
| 54 | + @RawField |
| 55 | + void setAverage(double average); |
| 56 | + |
| 57 | + @RawField |
| 58 | + double getAverage(); |
| 59 | + |
| 60 | + @RawField |
| 61 | + void setSampleCount(long sampleCount); |
| 62 | + |
| 63 | + @RawField |
| 64 | + long getSampleCount(); |
| 65 | + |
| 66 | + @RawField |
| 67 | + void setIsOld(boolean isOld); |
| 68 | + |
| 69 | + @RawField |
| 70 | + boolean getIsOld(); |
| 71 | + } |
| 72 | + |
| 73 | + @Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true) |
| 74 | + static void initialize(Data data, double weight) { |
| 75 | + initialize(data, weight, 0); |
| 76 | + } |
| 77 | + |
| 78 | + @Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true) |
| 79 | + static void initialize(Data data, double weight, double avg) { |
| 80 | + assert weight > 0 && weight <= 100; |
| 81 | + data.setWeight(weight); |
| 82 | + data.setAverage(avg); |
| 83 | + } |
| 84 | + |
| 85 | + @Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true) |
| 86 | + public static double getAverage(Data data) { |
| 87 | + return data.getAverage(); |
| 88 | + } |
| 89 | + |
| 90 | + @Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true) |
| 91 | + public static void sample(Data data, double value) { |
| 92 | + data.setSampleCount(data.getSampleCount() + 1); |
| 93 | + if (!data.getIsOld() && data.getSampleCount() > OLD_THRESHOLD) { |
| 94 | + data.setIsOld(true); |
| 95 | + } |
| 96 | + data.setAverage(computeAdaptiveAverage(data, value, data.getAverage())); |
| 97 | + } |
| 98 | + |
| 99 | + @Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true) |
| 100 | + public static void sample(Data data, UnsignedWord value) { |
| 101 | + sample(data, UnsignedUtils.toDouble(value)); |
| 102 | + } |
| 103 | + |
| 104 | + @Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true) |
| 105 | + protected static double computeAdaptiveAverage(Data data, double sample, double avg) { |
| 106 | + /* |
| 107 | + * We smoothen the samples by not using weight directly until we've had enough data to make |
| 108 | + * it meaningful. We'd like the first weight used to be 1, the second to be 1/2, etc until |
| 109 | + * we have OLD_THRESHOLD/weight samples. |
| 110 | + */ |
| 111 | + double countWeight = 0; |
| 112 | + if (!data.getIsOld()) { // avoid division by zero if the counter wraps |
| 113 | + countWeight = OLD_THRESHOLD / (double) data.getSampleCount(); |
| 114 | + } |
| 115 | + double adaptiveWeight = UninterruptibleUtils.Math.max(data.getWeight(), countWeight); |
| 116 | + return AdaptiveWeightedAverage.expAvg(avg, sample, adaptiveWeight); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments