Skip to content

Commit

Permalink
[Profiling] Fix CO2 calculation with user-provided PUE and CO2PerKWH (e…
Browse files Browse the repository at this point in the history
…lastic#102884)

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
rockdaboot and elasticmachine authored Dec 5, 2023
1 parent 72afbc1 commit 85311b2
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package org.elasticsearch.xpack.profiling;

import java.util.Collections;
import java.util.Map;

import static java.util.Map.entry;
Expand All @@ -19,7 +18,6 @@ final class CO2Calculator {
private static final double DEFAULT_KILOWATTS_PER_CORE_ARM64 = 2.8d / 1000.0d; // unit: watt / core
private static final double DEFAULT_KILOWATTS_PER_CORE = DEFAULT_KILOWATTS_PER_CORE_X86; // unit: watt / core
private static final double DEFAULT_DATACENTER_PUE = 1.7d;
private static final Provider DEFAULT_PROVIDER = new Provider(DEFAULT_DATACENTER_PUE, Collections.emptyMap());
private final InstanceTypeService instanceTypeService;
private final Map<String, HostMetadata> hostMetadata;
private final double samplingDurationInSeconds;
Expand Down Expand Up @@ -76,12 +74,13 @@ private double getKiloWattsPerCore(HostMetadata host) {
}

private double getCO2TonsPerKWH(HostMetadata host) {
Provider provider = PROVIDERS.getOrDefault(host.instanceType.provider, DEFAULT_PROVIDER);
return provider.co2TonsPerKWH.getOrDefault(host.instanceType.region, customCO2PerKWH);
Provider provider = PROVIDERS.get(host.instanceType.provider);
return provider == null ? customCO2PerKWH : provider.co2TonsPerKWH.getOrDefault(host.instanceType.region, customCO2PerKWH);
}

private static double getDatacenterPUE(HostMetadata host) {
return PROVIDERS.getOrDefault(host.instanceType.provider, DEFAULT_PROVIDER).pue;
private double getDatacenterPUE(HostMetadata host) {
Provider provider = PROVIDERS.get(host.instanceType.provider);
return provider == null ? customDatacenterPUE : provider.pue;
}

private record Provider(double pue, Map<String, Double> co2TonsPerKWH) {}
Expand Down

0 comments on commit 85311b2

Please sign in to comment.