@@ -21,15 +21,21 @@ public class OngoingPowerMeasure extends ProcessorAware implements PowerMeasure
21
21
private final List <RegisteredSyntheticComponent > syntheticComponents ;
22
22
private int samples ;
23
23
private long [] timestamps ;
24
+ private long samplePeriod ;
24
25
25
26
public OngoingPowerMeasure (SensorMetadata metadata , SyntheticComponent ... syntheticComponents ) {
27
+ this (metadata , -1 , syntheticComponents );
28
+ }
29
+
30
+ public OngoingPowerMeasure (SensorMetadata metadata , long samplePeriod , SyntheticComponent ... syntheticComponents ) {
26
31
super (Processors .empty );
27
32
28
33
startedAt = System .currentTimeMillis ();
29
34
final var numComponents = metadata .componentCardinality ();
30
35
measures = new double [numComponents ][DEFAULT_SIZE ];
31
36
nonZeroComponents = new BitSet (numComponents );
32
37
timestamps = new long [DEFAULT_SIZE ];
38
+ this .samplePeriod = samplePeriod ;
33
39
34
40
if (syntheticComponents != null ) {
35
41
final var builder = SensorMetadata .from (metadata );
@@ -47,7 +53,7 @@ public OngoingPowerMeasure(SensorMetadata metadata, SyntheticComponent... synthe
47
53
}
48
54
49
55
@ Override
50
- public int numberOfSamples () {
56
+ public synchronized int numberOfSamples () {
51
57
return samples ;
52
58
}
53
59
@@ -57,19 +63,20 @@ public SensorMetadata metadata() {
57
63
}
58
64
59
65
public void recordMeasure (double [] components ) {
60
- samples ++;
61
66
ensureArraysSize ();
62
67
63
68
final var timestamp = System .currentTimeMillis ();
64
- timestamps [samples - 1 ] = timestamp ;
65
69
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 ;
71
79
}
72
- measures [component ][samples - 1 ] = componentValue ;
73
80
}
74
81
75
82
final var processors = processors ();
@@ -82,7 +89,8 @@ public void recordMeasure(double[] components) {
82
89
}
83
90
}
84
91
85
- private void ensureArraysSize () {
92
+ private synchronized void ensureArraysSize () {
93
+ samples ++;
86
94
final int currentSize = timestamps .length ;
87
95
if (currentSize <= samples ) {
88
96
final var newSize = currentSize * 2 ;
@@ -123,7 +131,7 @@ private static boolean targetComponentExistsAndIsRecorder(int component, Registe
123
131
}
124
132
125
133
@ Override
126
- public TimestampedMeasures getNthTimestampedMeasures (int n ) {
134
+ public synchronized TimestampedMeasures getNthTimestampedMeasures (int n ) {
127
135
n = Math .min (n , samples - 1 );
128
136
final var result = new double [measures .length ];
129
137
for (int i = 0 ; i < measures .length ; i ++) {
0 commit comments