Skip to content

Commit

Permalink
[fix]: use same function naming conventions and behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Yuan <[email protected]>
  • Loading branch information
SamYuan1990 committed Nov 22, 2024
1 parent 9507012 commit c7129c7
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cmd/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func main() {
platform.InitPowerImpl()
defer platform.StopPower()

if config.EnabledGPU() {
if config.IsEnabledGPU() {
r := accelerator.GetRegistry()
if a, err := accelerator.New(config.GPU, true); err == nil {
r.MustRegister(a) // Register the accelerator with the registry
Expand Down
4 changes: 2 additions & 2 deletions pkg/bpf/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (e *exporter) attach() error {
return fmt.Errorf("error attaching sched_switch tracepoint: %v", err)
}

if config.ExposeIRQCounterMetrics() {
if config.IsExposeIRQCounterMetrics() {
e.irqLink, err = link.AttachTracing(link.TracingOptions{
Program: e.bpfObjects.KeplerIrqTrace,
AttachType: ebpf.AttachTraceRawTp,
Expand Down Expand Up @@ -145,7 +145,7 @@ func (e *exporter) attach() error {
}

// Return early if hardware counters are not enabled
if !config.ExposeHardwareCounterMetrics() {
if !config.IsExposeHardwareCounterMetrics() {
klog.Infof("Hardware counter metrics are disabled")
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/bpf/fake_mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (e *exporter) attach() error {
return fmt.Errorf("error attaching sched_switch tracepoint: %v", err)
}

if config.ExposeIRQCounterMetrics() {
if config.IsExposeIRQCounterMetrics() {
e.irqLink, err = link.AttachTracing(link.TracingOptions{
Program: e.bpfObjects.KeplerIrqTrace,
AttachType: ebpf.AttachTraceRawTp,
Expand Down Expand Up @@ -144,7 +144,7 @@ func (e *exporter) attach() error {
}

// Return early if hardware counters are not enabled
if !config.ExposeHardwareCounterMetrics() {
if !config.IsExposeHardwareCounterMetrics() {
klog.Infof("Hardware counter metrics are disabled")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/energy/node_energy_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func UpdateNodeComponentsEnergy(nodeStats *stats.NodeStats, wg *sync.WaitGroup)
// UpdateNodeGPUEnergy updates each GPU power consumption. Right now we don't support other types of accelerators
func UpdateNodeGPUEnergy(nodeStats *stats.NodeStats, wg *sync.WaitGroup) {
defer wg.Done()
if config.EnabledGPU() {
if config.IsEnabledGPU() {
if gpu := acc.GetActiveAcceleratorByType(config.GPU); gpu != nil {
gpuEnergy := gpu.Device().AbsEnergyFromDevice()
for gpu, energy := range gpuEnergy {
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/metric_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (c *Collector) updateProcessResourceUtilizationMetrics(wg *sync.WaitGroup)
// update process metrics regarding the resource utilization to be used to calculate the energy consumption
// we first updates the bpf which is responsible to include new processes in the ProcessStats collection
resourceBpf.UpdateProcessBPFMetrics(c.bpfExporter, c.ProcessStats)
if config.EnabledGPU() {
if config.IsEnabledGPU() {
if acc.GetActiveAcceleratorByType(config.GPU) != nil {
accelerator.UpdateProcessGPUUtilizationMetrics(c.ProcessStats)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/stats/node_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (ne *NodeStats) ResetDeltaValues() {

func (ne *NodeStats) UpdateIdleEnergyWithMinValue(isComponentsSystemCollectionSupported bool) {
// gpu metric
if config.EnabledGPU() {
if config.IsEnabledGPU() {
if acc.GetActiveAcceleratorByType(config.GPU) != nil {
ne.CalcIdleEnergy(config.AbsEnergyInGPU, config.IdleEnergyInGPU, config.GPUComputeUtilization)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/collector/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewStats() *Stats {
stats.ResourceUsage[metricName] = types.NewUInt64StatCollection()
}

if config.EnabledGPU() {
if config.IsEnabledGPU() {
if acc.GetActiveAcceleratorByType(config.GPU) != nil {
stats.ResourceUsage[config.GPUComputeUtilization] = types.NewUInt64StatCollection()
stats.ResourceUsage[config.GPUMemUtilization] = types.NewUInt64StatCollection()
Expand Down Expand Up @@ -140,7 +140,7 @@ func (s *Stats) UpdateDynEnergy() {
s.CalcDynEnergy(config.AbsEnergyInPlatform, config.IdleEnergyInPlatform, config.DynEnergyInPlatform, sensorID)
}
// GPU metric
if config.EnabledGPU() {
if config.IsEnabledGPU() {
if acc.GetActiveAcceleratorByType(config.GPU) != nil {
for gpuID := range s.EnergyUsage[config.AbsEnergyInGPU] {
s.CalcDynEnergy(config.AbsEnergyInGPU, config.IdleEnergyInGPU, config.DynEnergyInGPU, gpuID)
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/stats/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func GetProcessFeatureNames() []string {
klog.V(3).Infof("Available ebpf counters: %v", metrics)

// gpu metric
if config.EnabledGPU() {
if config.IsEnabledGPU() {
if acc.GetActiveAcceleratorByType(config.GPU) != nil {
gpuMetrics := []string{config.GPUComputeUtilization, config.GPUMemUtilization}
metrics = append(metrics, gpuMetrics...)
Expand Down
16 changes: 7 additions & 9 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func SetEnabledEBPFCgroupID(enabled bool) {
// SetEnabledHardwareCounterMetrics enables the exposure of hardware counter metrics
func SetEnabledHardwareCounterMetrics(enabled bool) {
// set to false is any config source set it to false
instance.Kepler.ExposeHardwareCounterMetrics = enabled && instance.Kepler.ExposeHardwareCounterMetrics
instance.Kepler.ExposeHardwareCounterMetrics = enabled
}

// SetEnabledIdlePower allows enabling idle power exposure in Kepler's metrics. When direct power metrics access is available,
Expand All @@ -331,15 +331,14 @@ func SetEnabledHardwareCounterMetrics(enabled bool) {
// Know the number of running VMs becomes crucial for achieving a fair distribution of idle power, particularly when following the GHG (Greenhouse Gas) protocol.
func SetEnabledIdlePower(enabled bool) {
// set to true is any config source set it to true or if system power metrics are available
instance.Kepler.ExposeIdlePowerMetrics = enabled || instance.Kepler.ExposeIdlePowerMetrics
instance.Kepler.ExposeIdlePowerMetrics = enabled
if instance.Kepler.ExposeIdlePowerMetrics {
klog.Infoln("The Idle power will be exposed. Are you running on Baremetal or using single VM per node?")
}
}

// SetEnabledGPU enables the exposure of gpu metrics
func SetEnabledGPU(enabled bool) {
// set to true if any config source set it to true
instance.Kepler.EnabledGPU = enabled
}

Expand All @@ -349,8 +348,7 @@ func SetModelServerEnable(enabled bool) {

// SetEnabledMSR enables the exposure of MSR metrics
func SetEnabledMSR(enabled bool) {
// set to true if any config source set it to true
instance.Kepler.EnabledMSR = enabled || instance.Kepler.EnabledMSR
instance.Kepler.EnabledMSR = enabled
}

// SetKubeConfig set kubeconfig file
Expand Down Expand Up @@ -527,7 +525,7 @@ func GetLibvirtMetadataToken() string {
return instance.Libvirt.MetadataToken
}

func ExposeIRQCounterMetrics() bool {
func IsExposeIRQCounterMetrics() bool {
return instance.Kepler.ExposeIRQCounterMetrics
}

Expand Down Expand Up @@ -555,11 +553,11 @@ func GetMockACPIPowerPath() string {
return instance.Kepler.MockACPIPowerPath
}

func ExposeHardwareCounterMetrics() bool {
func IsExposeHardwareCounterMetrics() bool {
return instance.Kepler.ExposeHardwareCounterMetrics
}

func EnabledGPU() bool {
func IsEnabledGPU() bool {
return instance.Kepler.EnabledGPU
}

Expand Down Expand Up @@ -621,7 +619,7 @@ func ProcessComponentsPowerKey() string {
return instance.Model.ProcessComponentsPowerKey
}

func APIServerEnabled() bool {
func IsAPIServerEnabled() bool {
return instance.Kepler.EnableAPIServer
}

Expand Down
48 changes: 46 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,55 @@ var _ = Describe("Test Configuration", func() {
Expect(err).NotTo(HaveOccurred())
Expect(Config.Kepler).NotTo(BeNil())
Expect(Config.KernelVersion).To(Equal(float32(0)))
Expect(IsExposeProcessStatsEnabled()).To(BeFalse())
Expect(IsExposeContainerStatsEnabled()).To(BeTrue())
Expect(IsExposeVMStatsEnabled()).To(BeTrue())
Expect(IsExposeBPFMetricsEnabled()).To(BeTrue())
Expect(IsExposeComponentPowerEnabled()).To(BeTrue())
Expect(IsExposeIRQCounterMetrics()).To(BeTrue())
Expect(GetBPFSampleRate()).To(Equal(0))

})
It("test init by set func and Is Enable functions", func() {
Config, err := Initialize(".")
Expect(err).NotTo(HaveOccurred())
// test set and is enable functions.
SetEnabledGPU(true)
Expect(Config.Kepler.EnabledGPU).To(BeTrue())
Expect(EnabledGPU()).To(BeTrue())
Expect(IsEnabledGPU()).To(BeTrue())
SetEnabledGPU(false)
Expect(Config.Kepler.EnabledGPU).To(BeFalse())
Expect(EnabledGPU()).To(BeFalse())
Expect(IsEnabledGPU()).To(BeFalse())

SetEnabledMSR(true)
Expect(Config.Kepler.EnabledMSR).To(BeTrue())
Expect(IsEnabledMSR()).To(BeTrue())
SetEnabledMSR(false)
Expect(Config.Kepler.EnabledMSR).To(BeFalse())
Expect(IsEnabledMSR()).To(BeFalse())

SetEnableAPIServer(true)
Expect(Config.Kepler.EnableAPIServer).To(BeTrue())
Expect(IsAPIServerEnabled()).To(BeTrue())
SetEnableAPIServer(false)
Expect(Config.Kepler.EnableAPIServer).To(BeFalse())
Expect(IsAPIServerEnabled()).To(BeFalse())

SetMachineSpecFilePath("dummy")
Expect(Config.Kepler.MachineSpecFilePath).To(Equal("dummy"))

SetEnabledIdlePower(true)
Expect(Config.Kepler.ExposeIdlePowerMetrics).To(BeTrue())
Expect(IsIdlePowerEnabled()).To(BeTrue())
SetEnabledIdlePower(false)
Expect(Config.Kepler.ExposeIdlePowerMetrics).To(BeFalse())
Expect(IsIdlePowerEnabled()).To(BeFalse())

SetEnabledHardwareCounterMetrics(true)
Expect(Config.Kepler.ExposeHardwareCounterMetrics).To(BeTrue())
Expect(IsExposeHardwareCounterMetrics()).To(BeTrue())
SetEnabledHardwareCounterMetrics(false)
Expect(Config.Kepler.ExposeHardwareCounterMetrics).To(BeFalse())
Expect(IsExposeHardwareCounterMetrics()).To(BeFalse())
})
})
2 changes: 1 addition & 1 deletion pkg/kubernetes/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func NewObjListWatcher(bpfSupportedMetrics bpf.SupportedMetrics) (*ObjListWatche
bpfSupportedMetrics: bpfSupportedMetrics,
workqueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
}
if w.k8sCli == nil || !config.APIServerEnabled() {
if w.k8sCli == nil || !config.IsAPIServerEnabled() {
return w, nil
}
optionsModifier := func(options *metav1.ListOptions) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/metricfactory/metric_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func SCMetricsPromDesc(context string, bpfSupportedMetrics bpf.SupportedMetrics)

func GPUUsageMetricsPromDesc(context string) (descriptions map[string]*prometheus.Desc) {
descriptions = make(map[string]*prometheus.Desc)
if config.EnabledGPU() {
if config.IsEnabledGPU() {
if gpu := acc.GetActiveAcceleratorByType(config.GPU); gpu != nil {
for _, name := range consts.GPUMetricNames {
descriptions[name] = resMetricsPromDesc(context, name, gpu.Device().Name())
Expand Down
4 changes: 2 additions & 2 deletions pkg/metrics/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func CollectEnergyMetrics(ch chan<- prometheus.Metric, instance interface{}, col
if config.IsExposeComponentPowerEnabled() {
// collect the dynamic energy metrics
for i, collectorName := range consts.EnergyMetricNames {
if collectorName == config.GPU && !config.EnabledGPU() {
if collectorName == config.GPU && !config.IsEnabledGPU() {
continue
}
collectEnergy(ch, instance, consts.DynEnergyMetricNames[i], "dynamic", collectors[collectorName])
Expand All @@ -57,7 +57,7 @@ func CollectResUtilizationMetrics(ch chan<- prometheus.Metric, instance interfac
for collectorName := range bpfSupportedMetrics.HardwareCounters {
CollectResUtil(ch, instance, collectorName, collectors[collectorName])
}
if config.EnabledGPU() {
if config.IsEnabledGPU() {
if gpu := acc.GetActiveAcceleratorByType(config.GPU); gpu != nil {
for _, collectorName := range consts.GPUMetricNames {
CollectResUtil(ch, instance, collectorName, collectors[collectorName])
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/process_energy.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func addEstimatedEnergy(processIDList []uint64, processesMetrics map[uint64]*sta
klog.V(5).Infoln("Could not estimate the Process Components Power")
}
// estimate the associated power consumption of GPU for each process
if config.EnabledGPU() {
if config.IsEnabledGPU() {
if gpu := acc.GetActiveAcceleratorByType(config.GPU); gpu != nil {
processGPUPower, errGPU = processComponentPowerModel.GetGPUPower(isIdlePower)
if errGPU != nil {
Expand Down

0 comments on commit c7129c7

Please sign in to comment.