Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: read sensor from worker thread #1

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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