Skip to content

Commit

Permalink
fix: read sensor from worker thread (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Nov 24, 2023
1 parent 94d1476 commit e95e0a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/main/java/io/github/metacosm/power/PowerMeasurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import io.github.metacosm.power.sensors.PowerSensor;
import io.github.metacosm.power.sensors.RegisteredPID;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.infrastructure.Infrastructure;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@ApplicationScoped
public class PowerMeasurer {
Expand All @@ -16,7 +19,7 @@ public class PowerMeasurer {
@Inject
PowerSensor sensor;

private Multi<Map<RegisteredPID, double[]>> stream;
private Multi<Map<RegisteredPID, double[]>> periodicSensorCheck;

public Multi<double[]> startTracking(String pid) throws Exception {
// first make sure that the process with that pid exists
Expand All @@ -25,10 +28,13 @@ public Multi<double[]> startTracking(String pid) throws Exception {

if(!sensor.isStarted()) {
sensor.start(SAMPLING_FREQUENCY_IN_MILLIS);
stream = Multi.createFrom().ticks().every(Duration.ofMillis(SAMPLING_FREQUENCY_IN_MILLIS)).map(sensor::update);
periodicSensorCheck = Multi.createFrom().ticks()
.every(Duration.ofMillis(SAMPLING_FREQUENCY_IN_MILLIS))
.emitOn(Infrastructure.getDefaultWorkerPool())
.map(sensor::update);
}
final var registeredPID = sensor.register(parsedPID);
return stream.map(registeredPIDMap -> registeredPIDMap.get(registeredPID))
return periodicSensorCheck.map(registeredPIDMap -> registeredPIDMap.get(registeredPID))
.onCancellation().invoke(() -> sensor.unregister(registeredPID));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MacOSPowermetricsSensor implements PowerSensor {
public static final String CPU = "cpu";
Expand All @@ -21,7 +22,7 @@ public class MacOSPowermetricsSensor implements PowerSensor {
private final SensorMetadata.ComponentMetadata gpu = new SensorMetadata.ComponentMetadata(GPU, 1, "GPU power", true, "mW");
private final SensorMetadata.ComponentMetadata ane = new SensorMetadata.ComponentMetadata(ANE, 2, "Apple Neural Engine power", false, "mW");
private final SensorMetadata.ComponentMetadata cpuShare = new SensorMetadata.ComponentMetadata(CPU_SHARE, 3, "Computed share of CPU", false, "decimal percentage");
private final Map<String, RegisteredPID> trackedPIDs = new HashMap<>();
private final Map<String, RegisteredPID> trackedPIDs = new ConcurrentHashMap<>();

private final SensorMetadata metadata = new SensorMetadata() {
@Override
Expand Down

0 comments on commit e95e0a4

Please sign in to comment.