1
1
/*
2
- Copyright 2022.
3
-
2
+ Copyright 2023.
4
3
Licensed under the Apache License, Version 2.0 (the "License");
5
4
you may not use this file except in compliance with the License.
6
5
You may obtain a copy of the License at
7
-
8
6
http://www.apache.org/licenses/LICENSE-2.0
9
-
10
7
Unless required by applicable law or agreed to in writing, software
11
8
distributed under the License is distributed on an "AS IS" BASIS,
12
9
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,17 +13,129 @@ limitations under the License.
16
13
17
14
package source
18
15
19
- type PowerHMC struct {}
16
+ import (
17
+ "os"
18
+ "strconv"
19
+
20
+ "k8s.io/klog/v2"
21
+
22
+ "github.com/sustainable-computing-io/kepler/pkg/power/components/source"
23
+ "github.com/zhmcclient/golang-zhmcclient/pkg/zhmcclient"
24
+ )
25
+
26
+ const (
27
+ hmcSensorID string = "hmc"
28
+ )
29
+
30
+ type PowerHMC struct {
31
+ }
32
+
33
+ var hmcManager * zhmcclient.ZhmcManager
34
+
35
+ func (r * PowerHMC ) GetHMCManager () * zhmcclient.ZhmcManager {
36
+ if hmcManager == nil {
37
+ endpoint := os .Getenv ("HMC_ENDPOINT" )
38
+ username := os .Getenv ("HMC_USERNAME" )
39
+ password := os .Getenv ("HMC_PASSWORD" )
40
+ cacert := os .Getenv ("CA_CERT" )
41
+ skipCert := os .Getenv ("SKIP_CERT" )
42
+ isSkipCert , _ := strconv .ParseBool (skipCert )
43
+
44
+ creds := & zhmcclient.Options {Username : username , Password : password , CaCert : cacert , SkipCert : isSkipCert , Trace : false }
45
+ client , err := zhmcclient .NewClient (endpoint , creds , nil )
46
+ if err != nil {
47
+ klog .V (1 ).Infof ("Error getting client connection %v" , err .Message )
48
+ }
49
+ if client != nil {
50
+ zhmcAPI := zhmcclient .NewManagerFromClient (client )
51
+ hmcManager , _ := zhmcAPI .(* zhmcclient.ZhmcManager )
52
+ return hmcManager
53
+ }
54
+ }
55
+ return hmcManager
56
+ }
57
+
58
+ func (r * PowerHMC ) GetEnergyFromLpar () (uint64 , error ) {
59
+ hmcManager := r .GetHMCManager ()
60
+ lparURI := "api/logical-partitions/" + os .Getenv ("LPAR_ID" )
61
+ props := & zhmcclient.EnergyRequestPayload {
62
+ Range : "last-day" ,
63
+ Resolution : "fifteen-minutes" ,
64
+ }
65
+ energy , _ , err := hmcManager .GetEnergyDetailsforLPAR (lparURI , props )
66
+ if err != nil {
67
+ klog .V (1 ).Infof ("Error getting energy data: %v" , err .Message )
68
+ }
69
+ klog .V (1 ).Infof ("Get energy data successfully" )
70
+ return energy , err
71
+ }
72
+
73
+ func (r * PowerHMC ) GetLiveEnergyFromLpar () (uint64 , error ) {
74
+ hmcManager := r .GetHMCManager ()
75
+ lparURI := "/api/logical-partitions/" + os .Getenv ("LPAR_ID" )
76
+ energy , _ , err := hmcManager .GetLiveEnergyDetailsforLPAR (lparURI )
77
+ if err != nil {
78
+ klog .V (1 ).Infof ("Error getting energy data: %v" , err .Message )
79
+ } else {
80
+ klog .V (1 ).Infof ("Get energy data successfully with power: %v" , energy )
81
+ }
82
+ return energy , err
83
+ }
84
+
85
+ func (r * PowerHMC ) IsSystemCollectionSupported () bool {
86
+ return true
87
+ }
88
+
89
+ func (r * PowerHMC ) StopPower () {
90
+ }
91
+
92
+ func (r * PowerHMC ) GetEnergyFromDram () (uint64 , error ) {
93
+ return 0 , nil
94
+ }
95
+
96
+ func (r * PowerHMC ) GetEnergyFromCore () (uint64 , error ) {
97
+ return 0 , nil
98
+ }
99
+
100
+ func (r * PowerHMC ) GetEnergyFromUncore () (uint64 , error ) {
101
+ return 0 , nil
102
+ }
103
+
104
+ func (r * PowerHMC ) GetEnergyFromPackage () (uint64 , error ) {
105
+ return 0 , nil
106
+ }
107
+
108
+ func (r * PowerHMC ) GetNodeComponentsEnergy () map [int ]source.NodeComponentsEnergy {
109
+ pkgEnergy , _ := r .GetLiveEnergyFromLpar ()
110
+ pkgEnergy = pkgEnergy * 3
111
+ coreEnergy := uint64 (0 )
112
+ dramEnergy := uint64 (0 )
113
+ uncoreEnergy := uint64 (0 )
114
+ componentsEnergies := make (map [int ]source.NodeComponentsEnergy )
115
+ componentsEnergies [0 ] = source.NodeComponentsEnergy {
116
+ Core : coreEnergy ,
117
+ DRAM : dramEnergy ,
118
+ Uncore : uncoreEnergy ,
119
+ Pkg : pkgEnergy ,
120
+ }
121
+ return componentsEnergies
122
+ }
20
123
21
- func (a * PowerHMC ) StopPower () {
124
+ func (r * PowerHMC ) GetPlatformEnergy () map [string ]float64 {
125
+ pkgEnergy , _ := r .GetLiveEnergyFromLpar ()
126
+ platformEnergies := make (map [string ]float64 )
127
+ platformEnergies [hmcSensorID ] = float64 (pkgEnergy ) * 3
128
+ return platformEnergies
22
129
}
23
130
24
- func (a * PowerHMC ) IsSystemCollectionSupported () bool {
25
- return false
131
+ func (r * PowerHMC ) IsPlatformCollectionSupported () bool {
132
+ return true
26
133
}
27
134
28
135
// GetEnergyFromHost returns the accumulated energy consumption
29
- func (a * PowerHMC ) GetAbsEnergyFromPlatform () (map [string ]float64 , error ) {
30
- power := map [string ]float64 {}
31
- return power , nil
136
+ func (r * PowerHMC ) GetAbsEnergyFromPlatform () (map [string ]float64 , error ) {
137
+ pkgEnergy , _ := r .GetLiveEnergyFromLpar ()
138
+ platformEnergies := make (map [string ]float64 )
139
+ platformEnergies [hmcSensorID ] = float64 (pkgEnergy ) * 3
140
+ return platformEnergies , nil
32
141
}
0 commit comments