Skip to content

Commit dd3f8ee

Browse files
committed
feat: record measures for an application by its name
1 parent ca2e300 commit dd3f8ee

File tree

10 files changed

+57
-37
lines changed

10 files changed

+57
-37
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
* with the sensor.
77
*
88
* @param components an array recording the power consumption reported by each component of this sensor
9-
* @param tick the ordinal tick associated with this measure
9+
* @param startMs the start timestamp in milliseconds for this measure
10+
* @param endMs the end timestamp in milliseconds for this measure
1011
*/
11-
public record SensorMeasure(double[] components, long tick, long timestamp, long duration) {
12+
public record SensorMeasure(double[] components, long startMs, long endMs) {
1213
/**
1314
* Represents an invalid or somehow missed measure.
1415
*/
15-
public static final SensorMeasure missing = new SensorMeasure(new double[] { -1.0 }, -1, -1, -1);
16+
public static final SensorMeasure missing = new SensorMeasure(new double[] { -1.0 }, -1, -1);
1617
}

server/src/main/java/net/laprun/sustainability/power/PowerMeasurer.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.laprun.sustainability.power.persistence.Persistence;
1313
import net.laprun.sustainability.power.sensors.Measures;
1414
import net.laprun.sustainability.power.sensors.PowerSensor;
15+
import net.laprun.sustainability.power.sensors.RegisteredPID;
1516

1617
@ApplicationScoped
1718
public class PowerMeasurer {
@@ -25,7 +26,16 @@ public class PowerMeasurer {
2526

2627
private Multi<Measures> periodicSensorCheck;
2728

28-
public Multi<SensorMeasure> startTracking(String pid) throws Exception {
29+
public Multi<SensorMeasure> stream(String pid) throws Exception {
30+
final var registeredPID = track(pid);
31+
return periodicSensorCheck.map(measures -> measures.getOrDefault(registeredPID));
32+
}
33+
34+
public void startTrackingApp(String appName, String pid) throws Exception {
35+
stream(pid).subscribe().with(m -> Persistence.save(m, appName));
36+
}
37+
38+
private RegisteredPID track(String pid) throws Exception {
2939
// first make sure that the process with that pid exists
3040
final var parsedPID = validPIDOrFail(pid);
3141

@@ -42,10 +52,8 @@ public Multi<SensorMeasure> startTracking(String pid) throws Exception {
4252
final var registeredPID = sensor.register(parsedPID);
4353
// todo: the timing of things could make it so that the pid has been removed before the map operation occurs so
4454
// currently return -1 instead of null but this needs to be properly addressed
45-
return periodicSensorCheck
46-
.map(measures -> measures.getOrDefault(registeredPID))
47-
.onItem().invoke(sm -> Persistence.save(sm, parsedPID))
48-
.onCancellation().invoke(() -> sensor.unregister(registeredPID));
55+
periodicSensorCheck = periodicSensorCheck.onCancellation().invoke(() -> sensor.unregister(registeredPID));
56+
return registeredPID;
4957
}
5058

5159
protected long validPIDOrFail(String pid) {

server/src/main/java/net/laprun/sustainability/power/PowerResource.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
import jakarta.enterprise.event.Observes;
77
import jakarta.inject.Inject;
8-
import jakarta.ws.rs.*;
8+
import jakarta.ws.rs.GET;
9+
import jakarta.ws.rs.NotFoundException;
10+
import jakarta.ws.rs.POST;
11+
import jakarta.ws.rs.Path;
12+
import jakarta.ws.rs.PathParam;
913
import jakarta.ws.rs.core.MediaType;
1014

1115
import org.jboss.resteasy.reactive.RestStreamElementType;
@@ -27,10 +31,20 @@ public void onStartup(@Observes StartupEvent event) {
2731

2832
@GET
2933
@RestStreamElementType(MediaType.APPLICATION_JSON)
30-
@Path("{pid}")
31-
public Multi<SensorMeasure> powerFor(@PathParam("pid") String pid) throws Exception {
34+
@Path("stream/{pid}")
35+
public Multi<SensorMeasure> streamMeasuresFor(@PathParam("pid") String pid) throws Exception {
3236
try {
33-
return measurer.startTracking(pid);
37+
return measurer.stream(pid);
38+
} catch (IllegalArgumentException e) {
39+
throw new NotFoundException("Unknown process: " + pid);
40+
}
41+
}
42+
43+
@POST
44+
@Path("start/{appName}/{pid}")
45+
public void startMeasure(@PathParam("appName") String appName, @PathParam("pid") String pid) throws Exception {
46+
try {
47+
measurer.startTrackingApp(appName, pid);
3448
} catch (IllegalArgumentException e) {
3549
throw new NotFoundException("Unknown process: " + pid);
3650
}
@@ -49,14 +63,8 @@ public Duration samplingPeriod() {
4963
}
5064

5165
@GET
52-
@Path("data/{pid}")
53-
public List<Measure> measures(@PathParam("pid") String pid) throws Exception {
54-
return Measure.forPID(Long.parseLong(pid));
55-
}
56-
57-
@GET
58-
@Path("pids")
59-
public List<String> pids() {
60-
return Measure.all().stream().map(m -> "" + m.pid).toList();
66+
@Path("measures/{appName}")
67+
public List<SensorMeasure> measures(@PathParam("appName") String appName) throws Exception {
68+
return Measure.forApplication(appName).stream().map(Measure::asSensorMeasure).toList();
6169
}
6270
}

server/src/main/java/net/laprun/sustainability/power/persistence/Measure.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@
55
import jakarta.persistence.Entity;
66

77
import io.quarkus.hibernate.orm.panache.PanacheEntity;
8+
import net.laprun.sustainability.power.SensorMeasure;
89

910
@Entity
1011
public class Measure extends PanacheEntity {
11-
public long pid;
12+
public String appName;
1213
public long startTime;
1314
public long endTime;
1415
public double[] components;
1516

16-
public static List<Measure> forPID(long pid) {
17-
return find("pid", pid).list();
17+
public static List<Measure> forApplication(String appName) {
18+
return find("appName", appName).list();
1819
}
1920

2021
public static List<Measure> all() {
2122
return Measure.findAll().list();
2223
}
24+
25+
public SensorMeasure asSensorMeasure() {
26+
return new SensorMeasure(components, startTime, endTime);
27+
}
2328
}

server/src/main/java/net/laprun/sustainability/power/persistence/Persistence.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public enum Persistence {
88
;
99

1010
@Transactional
11-
public static Measure save(SensorMeasure measure, long parsedPID) {
11+
public static Measure save(SensorMeasure measure, String appName) {
1212
final var persisted = new Measure();
1313
persisted.components = measure.components();
14-
persisted.pid = parsedPID;
15-
persisted.startTime = measure.timestamp();
16-
persisted.endTime = measure.timestamp() + measure.duration();
14+
persisted.appName = appName;
15+
persisted.startTime = measure.startMs();
16+
persisted.endTime = measure.endMs();
1717
persisted.persist();
1818
return persisted;
1919
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ public Measures update(Long tick) {
141141
measure[i] = newComponentValue;
142142
lastMeasuredSensorValues[i] = newComponentValue;
143143
}
144-
final long timestamp = System.currentTimeMillis();
145-
measures.singleMeasure(new SensorMeasure(measure, tick, timestamp, timestamp - start));
144+
measures.singleMeasure(new SensorMeasure(measure, start, System.currentTimeMillis()));
146145
return measures;
147146
}
148147
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ Measures extractPowerMeasure(InputStream powerMeasureInput, Long tick) {
197197
}
198198
});
199199

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void start(long samplingFrequencyInMillis) {
4848
@Override
4949
public Measures update(Long tick) {
5050
measures.trackedPIDs().forEach(pid -> measures.record(pid,
51-
new SensorMeasure(new double[] { Math.random() }, tick, System.currentTimeMillis(), 0)));
51+
new SensorMeasure(new double[] { Math.random() }, System.currentTimeMillis(), System.currentTimeMillis())));
5252
return measures;
5353
}
5454
}

server/src/test/java/net/laprun/sustainability/power/CIPowerResourceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ protected long getPid() {
2222
public void testPowerEndpoint() {
2323
final var pid = getPid();
2424
given()
25-
.when().get("/power/" + pid)
25+
.when().post("/power/start/cipowerresourcetest/" + pid)
2626
.then()
27-
.statusCode(200);
27+
.statusCode(204);
2828
}
2929

3030
@Test

server/src/test/java/net/laprun/sustainability/power/PowerResourceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public class PowerResourceTest {
2020
public void testPowerEndpoint() {
2121
final var pid = getPid();
2222
given()
23-
.when().get("/power/" + pid)
23+
.when().post("/power/start/powerresourcetest/" + pid)
2424
.then()
25-
.statusCode(200);
25+
.statusCode(204);
2626
}
2727

2828
@Test

0 commit comments

Comments
 (0)