Skip to content

Commit

Permalink
feat: expose metadata endpoint (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm authored Nov 24, 2023
1 parent e95e0a4 commit 5f9165c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/github/metacosm/power/PowerMeasurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public Multi<double[]> startTracking(String pid) throws Exception {
return periodicSensorCheck.map(registeredPIDMap -> registeredPIDMap.get(registeredPID))
.onCancellation().invoke(() -> sensor.unregister(registeredPID));
}

public SensorMetadata metadata() {
return sensor.metadata();
}
}
8 changes: 7 additions & 1 deletion src/main/java/io/github/metacosm/power/PowerResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ public class PowerResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("{pid}")
public Multi<String> hello(@PathParam("pid") String pid) throws Exception {
public Multi<String> powerFor(@PathParam("pid") String pid) throws Exception {
try {
return measurer.startTracking(pid).map(Arrays::toString);
} catch (IllegalArgumentException e) {
throw new NotFoundException("Unknown process: " + pid);
}
}

@GET
@Path("metadata")
public SensorMetadata metadata() {
return measurer.metadata();
}
}
7 changes: 7 additions & 0 deletions src/main/java/io/github/metacosm/power/SensorMetadata.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package io.github.metacosm.power;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Map;

public interface SensorMetadata {
record ComponentMetadata(String name, int index, String description, boolean isAttributed, String unit){};

ComponentMetadata metadataFor(String component);

int componentCardinality();

@JsonProperty("metadata")
Map<String, ComponentMetadata> getComponents();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,31 @@ public IntelRAPLSensor() {
throw new RuntimeException("Failed to get RAPL energy readings, probably due to lack of read access ");

raplFiles = files.values().toArray(new RAPLFile[0]);
final var metadata = new HashMap<String, Integer>(files.size());
final var metadata = new HashMap<String, SensorMetadata.ComponentMetadata>(files.size());
int fileNb = 0;
for (String name : files.keySet()) {
metadata.put(name, fileNb++);
metadata.put(name, new SensorMetadata.ComponentMetadata(name, fileNb++, name, false, "µJ"));
}
this.metadata = new SensorMetadata() {

@Override
public ComponentMetadata metadataFor(String component) {
final var index = metadata.get(component);
if (index == null) {
final var componentMetadata = metadata.get(component);
if (componentMetadata == null) {
throw new IllegalArgumentException("Unknown component: " + component);
}
return new ComponentMetadata(component, index, component, false, "µJ");
return componentMetadata;
}

@Override
public int componentCardinality() {
return metadata.size();
}

@Override
public Map<String, ComponentMetadata> getComponents() {
return metadata;
}
};
lastMeasuredSensorValues = new double[raplFiles.length];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public ComponentMetadata metadataFor(String component) {
public int componentCardinality() {
return 4;
}

@Override
public Map<String, ComponentMetadata> getComponents() {
return Map.of(cpu.name(), cpu, gpu.name(), gpu, ane.name(), ane, cpuShare.name(), cpuShare);
}
};

@Override
Expand Down

0 comments on commit 5f9165c

Please sign in to comment.