Skip to content

Commit

Permalink
Support hmc client to get energy consumption from IBM System Z
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Hui Jiang <[email protected]>
  • Loading branch information
Peng Hui Jiang authored and Peng Hui Jiang committed Oct 30, 2023
1 parent 3d5d046 commit 1a534ca
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 11 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/prometheus/client_model v0.5.0
github.com/prometheus/common v0.44.0
github.com/prometheus/prometheus v0.40.3
github.com/zhmcclient/golang-zhmcclient v0.2.1-0.20231017092628-6bb10a7dd31e
golang.org/x/sys v0.13.0
k8s.io/api v0.25.3
k8s.io/apimachinery v0.25.3
Expand Down Expand Up @@ -68,6 +69,8 @@ require (
github.com/prometheus/procfs v0.11.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/term v0.13.0 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ github.com/emicklei/go-restful/v3 v3.8.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
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 v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
Expand Down Expand Up @@ -141,6 +142,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
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/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.28.0 h1:i2rg/p9n/UqIDAMFUJ6qIUUMcsqOuUHgbpbu235Vr1c=
Expand Down Expand Up @@ -180,6 +183,13 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zhmcclient/golang-zhmcclient v0.2.1-0.20231017092628-6bb10a7dd31e h1:YhJu8iZ3alKXCd25WCmjlXXkUZZsf6ioEYc9hRqjp0g=
github.com/zhmcclient/golang-zhmcclient v0.2.1-0.20231017092628-6bb10a7dd31e/go.mod h1:adYy8E34m1Z0SW4YVtYVj6mSLZkFXLTk0HAap9ViE+Y=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down Expand Up @@ -277,6 +287,7 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
131 changes: 120 additions & 11 deletions pkg/power/platform/source/hmc.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,17 +13,129 @@ limitations under the License.

package source

type PowerHMC struct{}
import (
"os"
"strconv"

"k8s.io/klog/v2"

"github.com/sustainable-computing-io/kepler/pkg/power/components/source"
"github.com/zhmcclient/golang-zhmcclient/pkg/zhmcclient"
)

const (
hmcSensorID string = "hmc"
)

type PowerHMC struct {
}

var hmcManager *zhmcclient.ZhmcManager

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 (a *PowerHMC) StopPower() {
func (r *PowerHMC) GetPlatformEnergy() map[string]float64 {
pkgEnergy, _ := r.GetLiveEnergyFromLpar()
platformEnergies := make(map[string]float64)
platformEnergies[hmcSensorID] = float64(pkgEnergy) * 3
return platformEnergies
}

func (a *PowerHMC) IsSystemCollectionSupported() bool {
return false
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
}
17 changes: 17 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@ github.com/sirupsen/logrus
# github.com/spf13/pflag v1.0.5
## explicit; go 1.12
github.com/spf13/pflag
# 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
Expand Down

0 comments on commit 1a534ca

Please sign in to comment.