Skip to content

Commit 32bad3b

Browse files
committed
fix: better synchronization
1 parent 1456fce commit 32bad3b

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

measure/src/main/java/net/laprun/sustainability/power/measure/OngoingPowerMeasure.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@ public class OngoingPowerMeasure extends ProcessorAware implements PowerMeasure
2121
private final List<RegisteredSyntheticComponent> syntheticComponents;
2222
private int samples;
2323
private long[] timestamps;
24+
private long samplePeriod;
2425

2526
public OngoingPowerMeasure(SensorMetadata metadata, SyntheticComponent... syntheticComponents) {
27+
this(metadata, -1, syntheticComponents);
28+
}
29+
30+
public OngoingPowerMeasure(SensorMetadata metadata, long samplePeriod, SyntheticComponent... syntheticComponents) {
2631
super(Processors.empty);
2732

2833
startedAt = System.currentTimeMillis();
2934
final var numComponents = metadata.componentCardinality();
3035
measures = new double[numComponents][DEFAULT_SIZE];
3136
nonZeroComponents = new BitSet(numComponents);
3237
timestamps = new long[DEFAULT_SIZE];
38+
this.samplePeriod = samplePeriod;
3339

3440
if (syntheticComponents != null) {
3541
final var builder = SensorMetadata.from(metadata);
@@ -47,7 +53,7 @@ public OngoingPowerMeasure(SensorMetadata metadata, SyntheticComponent... synthe
4753
}
4854

4955
@Override
50-
public int numberOfSamples() {
56+
public synchronized int numberOfSamples() {
5157
return samples;
5258
}
5359

@@ -57,19 +63,20 @@ public SensorMetadata metadata() {
5763
}
5864

5965
public void recordMeasure(double[] components) {
60-
samples++;
6166
ensureArraysSize();
6267

6368
final var timestamp = System.currentTimeMillis();
64-
timestamps[samples - 1] = timestamp;
6569

66-
for (int component = 0; component < components.length; component++) {
67-
final var componentValue = components[component];
68-
// record that the value is not zero
69-
if (componentValue != 0) {
70-
nonZeroComponents.set(component);
70+
synchronized (this) {
71+
timestamps[samples - 1] = timestamp;
72+
for (int component = 0; component < components.length; component++) {
73+
final var componentValue = components[component];
74+
// record that the value is not zero
75+
if (componentValue != 0) {
76+
nonZeroComponents.set(component);
77+
}
78+
measures[component][samples - 1] = componentValue;
7179
}
72-
measures[component][samples - 1] = componentValue;
7380
}
7481

7582
final var processors = processors();
@@ -82,7 +89,8 @@ public void recordMeasure(double[] components) {
8289
}
8390
}
8491

85-
private void ensureArraysSize() {
92+
private synchronized void ensureArraysSize() {
93+
samples++;
8694
final int currentSize = timestamps.length;
8795
if (currentSize <= samples) {
8896
final var newSize = currentSize * 2;
@@ -123,7 +131,7 @@ private static boolean targetComponentExistsAndIsRecorder(int component, Registe
123131
}
124132

125133
@Override
126-
public TimestampedMeasures getNthTimestampedMeasures(int n) {
134+
public synchronized TimestampedMeasures getNthTimestampedMeasures(int n) {
127135
n = Math.min(n, samples - 1);
128136
final var result = new double[measures.length];
129137
for (int i = 0; i < measures.length; i++) {

0 commit comments

Comments
 (0)