diff --git a/go.mod b/go.mod index 4c2b59bf17..26053b1113 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/sustainable-computing-io/kepler -go 1.20 +go 1.21 + +toolchain go1.21.1 require ( github.com/NVIDIA/go-dcgm v0.0.0-20240118201113-3385e277e49f diff --git a/go.sum b/go.sum index cf7192d91d..0d7044ae93 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,7 @@ github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9 github.com/aquasecurity/libbpfgo v0.4.9-libbpf-1.2.0 h1:pk9L7I6wF1nTfO42+jjXhA8ozRjvtj2ZvHV/i/YC0dE= github.com/aquasecurity/libbpfgo v0.4.9-libbpf-1.2.0/go.mod h1:UD3Mfr+JZ/ASK2VMucI/zAdEhb35LtvYXvAUdrdqE9s= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= @@ -35,6 +36,7 @@ github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD github.com/emicklei/go-restful/v3 v3.10.2 h1:hIovbnmBTLjHXkqEBUz3HGpXZdM7ZrE9fJIZIqlJLqE= github.com/emicklei/go-restful/v3 v3.10.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss= +github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= @@ -92,6 +94,7 @@ github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwA github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jszwec/csvutil v1.8.0 h1:G7vS2LGdpZZDH1HmHeNbxOaJ/ZnJlpwGFvOkTkJzzNk= @@ -102,6 +105,7 @@ github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/4 github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -124,6 +128,7 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs= github.com/onsi/ginkgo/v2 v2.13.2/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM= github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8= @@ -148,7 +153,9 @@ github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/ github.com/prometheus/prometheus v0.48.1 h1:CTszphSNTXkuCG6O0IfpKdHcJkvvnAAE1GbELKS+NFk= github.com/prometheus/prometheus v0.48.1/go.mod h1:SRw624aMAxTfryAcP8rOjg4S/sHHaetx2lyJJ2nM83g= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646 h1:RpforrEYXWkmGwJHIGnLZ3tTWStkjVVstwzNGqxX2Ds= +github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= @@ -174,6 +181,7 @@ golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQz golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= diff --git a/pkg/sensors/platform/source/hmc.go b/pkg/sensors/platform/source/hmc.go index 47e30071a1..1bc9875c14 100644 --- a/pkg/sensors/platform/source/hmc.go +++ b/pkg/sensors/platform/source/hmc.go @@ -1,12 +1,9 @@ /* -Copyright 2022. - +Copyright 2023. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,21 +13,131 @@ limitations under the License. package source +import ( + "os" + "strconv" + + "k8s.io/klog/v2" + + "github.com/sustainable-computing-io/kepler/pkg/power/components/source" + "github.com/zhmcclient/golang-zhmcclient/pkg/zhmcclient" +) + +var hmcManager *zhmcclient.ZhmcManager + type PowerHMC struct{} func (a *PowerHMC) GetName() string { - return "hmc" + return "hmc" } func (a *PowerHMC) StopPower() { } -func (a *PowerHMC) IsSystemCollectionSupported() bool { - return false +func (r *PowerHMC) GetHMCManager() *zhmcclient.ZhmcManager { + if hmcManager == nil { + endpoint := os.Getenv("HMC_ENDPOINT") + username := os.Getenv("HMC_USERNAME") + password := os.Getenv("HMC_PASSWORD") + cacert := os.Getenv("CA_CERT") + skipCert := os.Getenv("SKIP_CERT") + isSkipCert, _ := strconv.ParseBool(skipCert) + + creds := &zhmcclient.Options{Username: username, Password: password, CaCert: cacert, SkipCert: isSkipCert, Trace: false} + client, err := zhmcclient.NewClient(endpoint, creds, nil) + if err != nil { + klog.V(1).Infof("Error getting client connection %v", err.Message) + } + if client != nil { + zhmcAPI := zhmcclient.NewManagerFromClient(client) + hmcManager, _ := zhmcAPI.(*zhmcclient.ZhmcManager) + return hmcManager + } + } + return hmcManager +} + +func (r *PowerHMC) GetEnergyFromLpar() (uint64, error) { + hmcManager := r.GetHMCManager() + lparURI := "api/logical-partitions/" + os.Getenv("LPAR_ID") + props := &zhmcclient.EnergyRequestPayload{ + Range: "last-day", + Resolution: "fifteen-minutes", + } + energy, _, err := hmcManager.GetEnergyDetailsforLPAR(lparURI, props) + if err != nil { + klog.V(1).Infof("Error getting energy data: %v", err.Message) + } + klog.V(1).Infof("Get energy data successfully") + return energy, err +} + +func (r *PowerHMC) GetLiveEnergyFromLpar() (uint64, error) { + hmcManager := r.GetHMCManager() + lparURI := "/api/logical-partitions/" + os.Getenv("LPAR_ID") + energy, _, err := hmcManager.GetLiveEnergyDetailsforLPAR(lparURI) + if err != nil { + klog.V(1).Infof("Error getting energy data: %v", err.Message) + } else { + klog.V(1).Infof("Get energy data successfully with power: %v", energy) + } + return energy, err +} + +func (r *PowerHMC) IsSystemCollectionSupported() bool { + return true +} + +func (r *PowerHMC) StopPower() { +} + +func (r *PowerHMC) GetEnergyFromDram() (uint64, error) { + return 0, nil +} + +func (r *PowerHMC) GetEnergyFromCore() (uint64, error) { + return 0, nil +} + +func (r *PowerHMC) GetEnergyFromUncore() (uint64, error) { + return 0, nil +} + +func (r *PowerHMC) GetEnergyFromPackage() (uint64, error) { + return 0, nil +} + +func (r *PowerHMC) GetNodeComponentsEnergy() map[int]source.NodeComponentsEnergy { + pkgEnergy, _ := r.GetLiveEnergyFromLpar() + pkgEnergy = pkgEnergy * 3 + coreEnergy := uint64(0) + dramEnergy := uint64(0) + uncoreEnergy := uint64(0) + componentsEnergies := make(map[int]source.NodeComponentsEnergy) + componentsEnergies[0] = source.NodeComponentsEnergy{ + Core: coreEnergy, + DRAM: dramEnergy, + Uncore: uncoreEnergy, + Pkg: pkgEnergy, + } + return componentsEnergies +} + +func (r *PowerHMC) GetPlatformEnergy() map[string]float64 { + pkgEnergy, _ := r.GetLiveEnergyFromLpar() + platformEnergies := make(map[string]float64) + platformEnergies[hmcSensorID] = float64(pkgEnergy) * 3 + return platformEnergies +} + +func (r *PowerHMC) IsPlatformCollectionSupported() bool { + return true } // GetEnergyFromHost returns the accumulated energy consumption -func (a *PowerHMC) GetAbsEnergyFromPlatform() (map[string]float64, error) { - power := map[string]float64{} - return power, nil +func (r *PowerHMC) GetAbsEnergyFromPlatform() (map[string]float64, error) { + pkgEnergy, _ := r.GetLiveEnergyFromLpar() + platformEnergies := make(map[string]float64) + platformEnergies[hmcSensorID] = float64(pkgEnergy) * 3 + return platformEnergies, nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index cbe311bb66..eefeb8fbcb 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -302,6 +302,23 @@ github.com/stretchr/testify/assert ## explicit; go 1.20 golang.org/x/exp/constraints golang.org/x/exp/slices +# github.com/zhmcclient/golang-zhmcclient v0.2.1-0.20231017092628-6bb10a7dd31e +## explicit; go 1.20 +github.com/zhmcclient/golang-zhmcclient/pkg/zhmcclient +# go.uber.org/multierr v1.10.0 +## explicit; go 1.19 +go.uber.org/multierr +# go.uber.org/zap v1.26.0 +## explicit; go 1.19 +go.uber.org/zap +go.uber.org/zap/buffer +go.uber.org/zap/internal +go.uber.org/zap/internal/bufferpool +go.uber.org/zap/internal/color +go.uber.org/zap/internal/exit +go.uber.org/zap/internal/pool +go.uber.org/zap/internal/stacktrace +go.uber.org/zap/zapcore # golang.org/x/net v0.17.0 ## explicit; go 1.17 golang.org/x/net/context