From d161bba98c3b841cbb2d98bef357242983908442 Mon Sep 17 00:00:00 2001 From: Sam Yuan Date: Fri, 6 Dec 2024 18:30:03 +0800 Subject: [PATCH] [fix]: try to reduce escapes to heap Signed-off-by: Sam Yuan --- pkg/collector/stats/types/types.go | 4 ++-- pkg/config/config.go | 2 +- pkg/kubelet/kubelet_pod_lister.go | 5 +++-- pkg/model/types/types.go | 10 ++++------ pkg/sensors/components/source/rapl_msr_util.go | 1 - pkg/sensors/components/source/rapl_sysfs.go | 16 ++++++++++++---- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/pkg/collector/stats/types/types.go b/pkg/collector/stats/types/types.go index fa0e24cf6f..66a6d6563c 100644 --- a/pkg/collector/stats/types/types.go +++ b/pkg/collector/stats/types/types.go @@ -14,10 +14,10 @@ type UInt64Stat struct { } func NewUInt64Stat(aggr, delta uint64) *UInt64Stat { - stat := &UInt64Stat{} + stat := UInt64Stat{} stat.aggr.Store(aggr) stat.delta.Store(delta) - return stat + return &stat } func (s *UInt64Stat) String() string { diff --git a/pkg/config/config.go b/pkg/config/config.go index 1025916018..c42904ac22 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -234,7 +234,7 @@ func getBoolConfig(configKey string, defaultBool bool) bool { } func getIntConfig(configKey string, defaultInt int) int { - defaultValue := fmt.Sprintf("%d", defaultInt) + defaultValue := strconv.Itoa(defaultInt) value, err := strconv.Atoi(getConfig(configKey, defaultValue)) if err == nil { return value diff --git a/pkg/kubelet/kubelet_pod_lister.go b/pkg/kubelet/kubelet_pod_lister.go index 30ff95707e..28a1161dc2 100644 --- a/pkg/kubelet/kubelet_pod_lister.go +++ b/pkg/kubelet/kubelet_pod_lister.go @@ -38,6 +38,7 @@ const ( var ( podURL string + client http.Client ) func init() { @@ -50,6 +51,8 @@ func init() { port = "10250" } podURL = "https://" + nodeName + ":" + port + "/pods" + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + client = http.Client{} } func httpGet(url string) (*http.Response, error) { @@ -65,8 +68,6 @@ func httpGet(url string) (*http.Response, error) { return nil, err } req.Header.Add("Authorization", bearer) - http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} - client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("failed to get response from %q: %v", url, err) diff --git a/pkg/model/types/types.go b/pkg/model/types/types.go index 51b5a215ee..6f92783be4 100644 --- a/pkg/model/types/types.go +++ b/pkg/model/types/types.go @@ -57,18 +57,16 @@ var ( LogisticTrainer, ExponentialTrainer, } + ModelOutputTypeConverter = []string{"AbsPower", "DynPower"} + ModelTypeConverter = []string{"Ratio", "Regressor", "EstimatorSidecar"} ) func getModelOutputTypeConverter() []string { - return []string{ - "AbsPower", "DynPower", - } + return ModelOutputTypeConverter } func getModelTypeConverter() []string { - return []string{ - "Ratio", "Regressor", "EstimatorSidecar", - } + return ModelTypeConverter } func (s ModelOutputType) String() string { diff --git a/pkg/sensors/components/source/rapl_msr_util.go b/pkg/sensors/components/source/rapl_msr_util.go index e761bb449e..c9c997e74a 100644 --- a/pkg/sensors/components/source/rapl_msr_util.go +++ b/pkg/sensors/components/source/rapl_msr_util.go @@ -116,7 +116,6 @@ func ReadMSR(packageID int, msr int64) (uint64, error) { } msrVal := byteOrder.Uint64(buf) - return msrVal, nil } diff --git a/pkg/sensors/components/source/rapl_sysfs.go b/pkg/sensors/components/source/rapl_sysfs.go index e94919dfab..3de1772bf1 100644 --- a/pkg/sensors/components/source/rapl_sysfs.go +++ b/pkg/sensors/components/source/rapl_sysfs.go @@ -21,6 +21,7 @@ import ( "os" "strconv" "strings" + "sync" "k8s.io/klog/v2" ) @@ -43,7 +44,9 @@ const ( ) var ( - eventPaths map[string]map[string]string + eventPaths map[string]map[string]string + once sync.Once + systemCollectionSupported bool ) func init() { @@ -125,9 +128,14 @@ func (PowerSysfs) GetName() string { } func (r *PowerSysfs) IsSystemCollectionSupported() bool { - path := fmt.Sprintf(packageNamePathTemplate, 0) - _, err := os.ReadFile(path + energyFile) - return err == nil + // use a hard code to reduce escapes to heap + // there are parts of code invokes this function + // use once to reduce IO + once.Do(func() { + _, err := os.ReadFile("/sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj") + systemCollectionSupported = (err == nil) + }) + return systemCollectionSupported } func (r *PowerSysfs) GetAbsEnergyFromDram() (uint64, error) {