Skip to content

Commit ba37569

Browse files
author
Thomas Schrott
committed
Add raw struct variant of AdaptiveWeightedAverage
1 parent 1181773 commit ba37569

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

substratevm/src/com.oracle.svm.core.genscavenge/src/com/oracle/svm/core/genscavenge/AdaptiveWeightedAverage.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
*/
2525
package com.oracle.svm.core.genscavenge;
2626

27+
import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;
28+
2729
import org.graalvm.word.UnsignedWord;
2830

31+
import com.oracle.svm.core.Uninterruptible;
2932
import com.oracle.svm.core.util.UnsignedUtils;
3033

3134
/**
@@ -101,7 +104,8 @@ protected double computeAdaptiveAverage(double sample, double avg) {
101104
return expAvg(avg, sample, adaptiveWeight);
102105
}
103106

104-
private static double expAvg(double avg, double sample, double adaptiveWeight) {
107+
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
108+
static double expAvg(double avg, double sample, double adaptiveWeight) {
105109
assert adaptiveWeight > 0 && adaptiveWeight <= 100 : "weight must be a percentage";
106110
return (100.0 - adaptiveWeight) * avg / 100.0 + adaptiveWeight * sample / 100.0;
107111
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/UninterruptibleUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,18 @@ public static long max(long a, long b) {
427427
return (a >= b) ? a : b;
428428
}
429429

430+
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
431+
public static double max(double a, double b) {
432+
if (a != a) {
433+
return a; // a is NaN
434+
}
435+
if ((a == 0.0d) && (b == 0.0d) && (Double.doubleToRawLongBits(a) == Double.doubleToRawLongBits(-0.0d))) {
436+
// Raw conversion ok since NaN can't map to -0.0.
437+
return b;
438+
}
439+
return (a >= b) ? a : b;
440+
}
441+
430442
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
431443
public static int clamp(int value, int min, int max) {
432444
assert min <= max;

0 commit comments

Comments
 (0)