Skip to content

Commit 1456fce

Browse files
committed
feat: record timestamp and duration on sensor measure
1 parent eaffbb2 commit 1456fce

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

metadata/src/main/java/net/laprun/sustainability/power/SensorMeasure.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
* @param components an array recording the power consumption reported by each component of this sensor
99
* @param tick the ordinal tick associated with this measure
1010
*/
11-
public record SensorMeasure(double[] components, long tick) {
11+
public record SensorMeasure(double[] components, long tick, long timestamp, long duration) {
1212
/**
1313
* Represents an invalid or somehow missed measure.
1414
*/
15-
public static final SensorMeasure missing = new SensorMeasure(new double[] { -1.0 }, -1);
15+
public static final SensorMeasure missing = new SensorMeasure(new double[] { -1.0 }, -1, -1, -1);
1616
}

server/src/main/java/net/laprun/sustainability/power/sensors/linux/rapl/IntelRAPLSensor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,16 @@ public boolean isStarted() {
133133

134134
@Override
135135
public Measures update(Long tick) {
136+
final long start = System.currentTimeMillis();
136137
final var measure = new double[raplFiles.length];
137138
for (int i = 0; i < raplFiles.length; i++) {
138139
final var value = raplFiles[i].extractEnergyInMicroJoules();
139140
final var newComponentValue = computePowerInMilliWatt(i, value);
140141
measure[i] = newComponentValue;
141142
lastMeasuredSensorValues[i] = newComponentValue;
142143
}
143-
measures.singleMeasure(new SensorMeasure(measure, tick));
144+
final long timestamp = System.currentTimeMillis();
145+
measures.singleMeasure(new SensorMeasure(measure, tick, timestamp, timestamp - start));
144146
return measures;
145147
}
146148
}

server/src/main/java/net/laprun/sustainability/power/sensors/macos/powermetrics/MacOSPowermetricsSensor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public ProcessRecord(String line) throws IllegalArgumentException {
118118
}
119119

120120
Measures extractPowerMeasure(InputStream powerMeasureInput, Long tick) {
121+
final long start = System.currentTimeMillis();
121122
try {
122123
// Should not be closed since it closes the process
123124
BufferedReader input = new BufferedReader(new InputStreamReader(powerMeasureInput));
@@ -196,7 +197,8 @@ Measures extractPowerMeasure(InputStream powerMeasureInput, Long tick) {
196197
}
197198
});
198199

199-
measures.record(pid, new SensorMeasure(measure, tick));
200+
final long timestamp = System.currentTimeMillis();
201+
measures.record(pid, new SensorMeasure(measure, tick, timestamp, timestamp - start));
200202
});
201203
} catch (Exception exception) {
202204
throw new RuntimeException(exception);

server/src/main/java/net/laprun/sustainability/power/sensors/test/TestPowerSensor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public void start(long samplingFrequencyInMillis) {
4747

4848
@Override
4949
public Measures update(Long tick) {
50-
measures.trackedPIDs().forEach(pid -> measures.record(pid, new SensorMeasure(new double[] { Math.random() }, tick)));
50+
measures.trackedPIDs().forEach(pid -> measures.record(pid,
51+
new SensorMeasure(new double[] { Math.random() }, tick, System.currentTimeMillis(), 0)));
5152
return measures;
5253
}
5354
}

0 commit comments

Comments
 (0)