From 25c2ae4910906c95f1e2f9d96d5da2077bef6224 Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Mon, 7 Feb 2022 11:12:03 +0100 Subject: [PATCH] Avoid int -> int64 conversions --- collectors/README.md | 5 ++- collectors/cpufreqCpuinfoMetric.go | 39 +++++++++++--------- collectors/cpufreqMetric.go | 50 ++++++++++++++------------ collectors/gpfsMetric.go | 58 ++++++++++++++---------------- 4 files changed, 80 insertions(+), 72 deletions(-) diff --git a/collectors/README.md b/collectors/README.md index 8423c95..cabb74a 100644 --- a/collectors/README.md +++ b/collectors/README.md @@ -103,9 +103,8 @@ func (m *SampleCollector) Read(interval time.Duration, output chan lp.CCMetric) } // Each metric has exactly one field: value ! - value := map[string]interface{}{"value": int(x)} - y, err := lp.New("sample_metric", tags, m.meta, value, time.Now()) - if err == nil { + value := map[string]interface{}{"value": int64(x)} + if y, err := lp.New("sample_metric", tags, m.meta, value, time.Now()); err == nil { output <- y } } diff --git a/collectors/cpufreqCpuinfoMetric.go b/collectors/cpufreqCpuinfoMetric.go index c77a981..5d3d4b5 100644 --- a/collectors/cpufreqCpuinfoMetric.go +++ b/collectors/cpufreqCpuinfoMetric.go @@ -23,14 +23,14 @@ import ( type CPUFreqCpuInfoCollectorTopology struct { processor string // logical processor number (continuous, starting at 0) coreID string // socket local core ID - coreID_int int + coreID_int int64 physicalPackageID string // socket / package ID - physicalPackageID_int int + physicalPackageID_int int64 numPhysicalPackages string // number of sockets / packages - numPhysicalPackages_int int + numPhysicalPackages_int int64 isHT bool numNonHT string // number of non hyperthreading processors - numNonHT_int int + numNonHT_int int64 tagSet map[string]string } @@ -40,26 +40,32 @@ type CPUFreqCpuInfoCollector struct { } func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error { + // Check if already initialized + if m.init { + return nil + } + m.name = "CPUFreqCpuInfoCollector" m.meta = map[string]string{ "source": m.name, - "group": "cpufreq", + "group": "CPU", + "unit": "MHz", } const cpuInfoFile = "/proc/cpuinfo" file, err := os.Open(cpuInfoFile) if err != nil { - return fmt.Errorf("Failed to open '%s': %v", cpuInfoFile, err) + return fmt.Errorf("Failed to open file '%s': %v", cpuInfoFile, err) } defer file.Close() // Collect topology information from file cpuinfo foundFreq := false processor := "" - numNonHT_int := 0 + var numNonHT_int int64 = 0 coreID := "" physicalPackageID := "" - maxPhysicalPackageID := 0 + var maxPhysicalPackageID int64 = 0 m.topology = make([]CPUFreqCpuInfoCollectorTopology, 0) coreSeenBefore := make(map[string]bool) scanner := bufio.NewScanner(file) @@ -87,13 +93,13 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error { len(coreID) > 0 && len(physicalPackageID) > 0 { - coreID_int, err := strconv.Atoi(coreID) + coreID_int, err := strconv.ParseInt(coreID, 10, 64) if err != nil { - return fmt.Errorf("Unable to convert coreID to int: %v", err) + return fmt.Errorf("Unable to convert coreID '%s' to int64: %v", coreID, err) } - physicalPackageID_int, err := strconv.Atoi(physicalPackageID) + physicalPackageID_int, err := strconv.ParseInt(physicalPackageID, 10, 64) if err != nil { - return fmt.Errorf("Unable to convert physicalPackageID to int: %v", err) + return fmt.Errorf("Unable to convert physicalPackageID '%s' to int64: %v", physicalPackageID, err) } // increase maximun socket / package ID, when required @@ -152,15 +158,17 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error { } func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CCMetric) { + // Check if already initialized if !m.init { return } + const cpuInfoFile = "/proc/cpuinfo" file, err := os.Open(cpuInfoFile) if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to open '%s': %v", cpuInfoFile, err)) + fmt.Sprintf("Read(): Failed to open file '%s': %v", cpuInfoFile, err)) return } defer file.Close() @@ -181,11 +189,10 @@ func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CC if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert cpu MHz to float: %v", err)) + fmt.Sprintf("Read(): Failed to convert cpu MHz '%s' to float64: %v", lineSplit[1], err)) return } - y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": value}, now) - if err == nil { + if y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": value}, now); err == nil { output <- y } } diff --git a/collectors/cpufreqMetric.go b/collectors/cpufreqMetric.go index b464160..5f47ce5 100644 --- a/collectors/cpufreqMetric.go +++ b/collectors/cpufreqMetric.go @@ -35,14 +35,14 @@ func readOneLine(filename string) (text string, ok bool) { type CPUFreqCollectorTopology struct { processor string // logical processor number (continuous, starting at 0) coreID string // socket local core ID - coreID_int int + coreID_int int64 physicalPackageID string // socket / package ID - physicalPackageID_int int + physicalPackageID_int int64 numPhysicalPackages string // number of sockets / packages - numPhysicalPackages_int int + numPhysicalPackages_int int64 isHT bool numNonHT string // number of non hyperthreading processors - numNonHT_int int + numNonHT_int int64 scalingCurFreqFile string tagSet map[string]string } @@ -64,6 +64,11 @@ type CPUFreqCollector struct { } func (m *CPUFreqCollector) Init(config json.RawMessage) error { + // Check if already initialized + if m.init { + return nil + } + m.name = "CPUFreqCollector" m.setup() if len(config) > 0 { @@ -74,7 +79,8 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error { } m.meta = map[string]string{ "source": m.name, - "group": "CPU Frequency", + "group": "CPU", + "unit": "MHz", } // Loop for all CPU directories @@ -82,48 +88,48 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error { globPattern := filepath.Join(baseDir, "cpu[0-9]*") cpuDirs, err := filepath.Glob(globPattern) if err != nil { - return fmt.Errorf("CPUFreqCollector.Init() unable to glob files with pattern %s: %v", globPattern, err) + return fmt.Errorf("Unable to glob files with pattern '%s': %v", globPattern, err) } if cpuDirs == nil { - return fmt.Errorf("CPUFreqCollector.Init() unable to find any files with pattern %s", globPattern) + return fmt.Errorf("Unable to find any files with pattern '%s'", globPattern) } // Initialize CPU topology m.topology = make([]CPUFreqCollectorTopology, len(cpuDirs)) for _, cpuDir := range cpuDirs { processor := strings.TrimPrefix(cpuDir, "/sys/devices/system/cpu/cpu") - processor_int, err := strconv.Atoi(processor) + processor_int, err := strconv.ParseInt(processor, 10, 64) if err != nil { - return fmt.Errorf("CPUFreqCollector.Init() unable to convert cpuID to int: %v", err) + return fmt.Errorf("Unable to convert cpuID '%s' to int64: %v", processor, err) } // Read package ID physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id") physicalPackageID, ok := readOneLine(physicalPackageIDFile) if !ok { - return fmt.Errorf("CPUFreqCollector.Init() unable to read physical package ID from %s", physicalPackageIDFile) + return fmt.Errorf("Unable to read physical package ID from file '%s'", physicalPackageIDFile) } - physicalPackageID_int, err := strconv.Atoi(physicalPackageID) + physicalPackageID_int, err := strconv.ParseInt(physicalPackageID, 10, 64) if err != nil { - return fmt.Errorf("CPUFreqCollector.Init() unable to convert packageID to int: %v", err) + return fmt.Errorf("Unable to convert packageID '%s' to int64: %v", physicalPackageID, err) } // Read core ID coreIDFile := filepath.Join(cpuDir, "topology", "core_id") coreID, ok := readOneLine(coreIDFile) if !ok { - return fmt.Errorf("CPUFreqCollector.Init() unable to read core ID from %s", coreIDFile) + return fmt.Errorf("Unable to read core ID from file '%s'", coreIDFile) } - coreID_int, err := strconv.Atoi(coreID) + coreID_int, err := strconv.ParseInt(coreID, 10, 64) if err != nil { - return fmt.Errorf("CPUFreqCollector.Init() unable to convert coreID to int: %v", err) + return fmt.Errorf("Unable to convert coreID '%s' to int64: %v", coreID, err) } // Check access to current frequency file scalingCurFreqFile := filepath.Join(cpuDir, "cpufreq", "scaling_cur_freq") err = unix.Access(scalingCurFreqFile, unix.R_OK) if err != nil { - return fmt.Errorf("CPUFreqCollector.Init() unable to access %s: %v", scalingCurFreqFile, err) + return fmt.Errorf("Unable to access file '%s': %v", scalingCurFreqFile, err) } t := &m.topology[processor_int] @@ -146,8 +152,8 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error { } // number of non hyper thread cores and packages / sockets - numNonHT_int := 0 - maxPhysicalPackageID := 0 + var numNonHT_int int64 = 0 + var maxPhysicalPackageID int64 = 0 for i := range m.topology { t := &m.topology[i] @@ -184,6 +190,7 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error { } func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric) { + // Check if already initialized if !m.init { return } @@ -205,16 +212,15 @@ func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric) fmt.Sprintf("Read(): Failed to read one line from file '%s'", t.scalingCurFreqFile)) continue } - cpuFreq, err := strconv.Atoi(line) + cpuFreq, err := strconv.ParseInt(line, 10, 64) if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert CPU frequency '%s': %v", line, err)) + fmt.Sprintf("Read(): Failed to convert CPU frequency '%s' to int64: %v", line, err)) continue } - y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": cpuFreq}, now) - if err == nil { + if y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": cpuFreq}, now); err == nil { output <- y } } diff --git a/collectors/gpfsMetric.go b/collectors/gpfsMetric.go index 8055d4c..ffb2fac 100644 --- a/collectors/gpfsMetric.go +++ b/collectors/gpfsMetric.go @@ -26,6 +26,11 @@ type GpfsCollector struct { } func (m *GpfsCollector) Init(config json.RawMessage) error { + // Check if already initialized + if m.init { + return nil + } + var err error m.name = "GpfsCollector" m.setup() @@ -53,16 +58,16 @@ func (m *GpfsCollector) Init(config json.RawMessage) error { // GPFS / IBM Spectrum Scale file system statistics can only be queried by user root user, err := user.Current() if err != nil { - return fmt.Errorf("GpfsCollector.Init(): Failed to get current user: %v", err) + return fmt.Errorf("Failed to get current user: %v", err) } if user.Uid != "0" { - return fmt.Errorf("GpfsCollector.Init(): GPFS file system statistics can only be queried by user root") + return fmt.Errorf("GPFS file system statistics can only be queried by user root") } // Check if mmpmon is in executable search path _, err = exec.LookPath(m.config.Mmpmon) if err != nil { - return fmt.Errorf("GpfsCollector.Init(): Failed to find mmpmon binary '%s': %v", m.config.Mmpmon, err) + return fmt.Errorf("Failed to find mmpmon binary '%s': %v", m.config.Mmpmon, err) } m.init = true @@ -70,6 +75,7 @@ func (m *GpfsCollector) Init(config json.RawMessage) error { } func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { + // Check if already initialized if !m.init { return } @@ -135,7 +141,7 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if rc != 0 { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Filesystem %s not ok.", filesystem)) + fmt.Sprintf("Read(): Filesystem '%s' is not ok.", filesystem)) continue } @@ -143,14 +149,14 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert seconds '%s' to int: %v", key_value["_t_"], err)) + fmt.Sprintf("Read(): Failed to convert seconds '%s' to int64: %v", key_value["_t_"], err)) continue } msec, err := strconv.ParseInt(key_value["_tu_"], 10, 64) if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert micro seconds '%s' to int: %v", key_value["_tu_"], err)) + fmt.Sprintf("Read(): Failed to convert micro seconds '%s' to int64: %v", key_value["_tu_"], err)) continue } timestamp := time.Unix(sec, msec*1000) @@ -160,12 +166,10 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert bytes read '%s' to int: %v", key_value["_br_"], err)) + fmt.Sprintf("Read(): Failed to convert bytes read '%s' to int64: %v", key_value["_br_"], err)) continue } - - y, err := lp.New("gpfs_bytes_read", m.tags, m.meta, map[string]interface{}{"value": bytesRead}, timestamp) - if err == nil { + if y, err := lp.New("gpfs_bytes_read", m.tags, m.meta, map[string]interface{}{"value": bytesRead}, timestamp); err == nil { output <- y } @@ -174,12 +178,10 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert bytes written '%s' to int: %v", key_value["_bw_"], err)) + fmt.Sprintf("Read(): Failed to convert bytes written '%s' to int64: %v", key_value["_bw_"], err)) continue } - - y, err = lp.New("gpfs_bytes_written", m.tags, m.meta, map[string]interface{}{"value": bytesWritten}, timestamp) - if err == nil { + if y, err := lp.New("gpfs_bytes_written", m.tags, m.meta, map[string]interface{}{"value": bytesWritten}, timestamp); err == nil { output <- y } @@ -188,11 +190,10 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert number of opens '%s' to int: %v", key_value["_oc_"], err)) + fmt.Sprintf("Read(): Failed to convert number of opens '%s' to int64: %v", key_value["_oc_"], err)) continue } - y, err = lp.New("gpfs_num_opens", m.tags, m.meta, map[string]interface{}{"value": numOpens}, timestamp) - if err == nil { + if y, err := lp.New("gpfs_num_opens", m.tags, m.meta, map[string]interface{}{"value": numOpens}, timestamp); err == nil { output <- y } @@ -201,11 +202,10 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert number of closes: '%s' to int: %v", key_value["_cc_"], err)) + fmt.Sprintf("Read(): Failed to convert number of closes: '%s' to int64: %v", key_value["_cc_"], err)) continue } - y, err = lp.New("gpfs_num_closes", m.tags, m.meta, map[string]interface{}{"value": numCloses}, timestamp) - if err == nil { + if y, err := lp.New("gpfs_num_closes", m.tags, m.meta, map[string]interface{}{"value": numCloses}, timestamp); err == nil { output <- y } @@ -214,11 +214,10 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert number of reads: '%s' to int: %v", key_value["_rdc_"], err)) + fmt.Sprintf("Read(): Failed to convert number of reads: '%s' to int64: %v", key_value["_rdc_"], err)) continue } - y, err = lp.New("gpfs_num_reads", m.tags, m.meta, map[string]interface{}{"value": numReads}, timestamp) - if err == nil { + if y, err := lp.New("gpfs_num_reads", m.tags, m.meta, map[string]interface{}{"value": numReads}, timestamp); err == nil { output <- y } @@ -227,11 +226,10 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert number of writes: '%s' to int: %v", key_value["_wc_"], err)) + fmt.Sprintf("Read(): Failed to convert number of writes: '%s' to int64: %v", key_value["_wc_"], err)) continue } - y, err = lp.New("gpfs_num_writes", m.tags, m.meta, map[string]interface{}{"value": numWrites}, timestamp) - if err == nil { + if y, err := lp.New("gpfs_num_writes", m.tags, m.meta, map[string]interface{}{"value": numWrites}, timestamp); err == nil { output <- y } @@ -240,11 +238,10 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if err != nil { cclog.ComponentError( m.name, - fmt.Sprintf("Read(): Failed to convert number of read directories: '%s' to int: %v", key_value["_dir_"], err)) + fmt.Sprintf("Read(): Failed to convert number of read directories: '%s' to int64: %v", key_value["_dir_"], err)) continue } - y, err = lp.New("gpfs_num_readdirs", m.tags, m.meta, map[string]interface{}{"value": numReaddirs}, timestamp) - if err == nil { + if y, err := lp.New("gpfs_num_readdirs", m.tags, m.meta, map[string]interface{}{"value": numReaddirs}, timestamp); err == nil { output <- y } @@ -256,8 +253,7 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { fmt.Sprintf("Read(): Failed to convert number of inode updates: '%s' to int: %v", key_value["_iu_"], err)) continue } - y, err = lp.New("gpfs_num_inode_updates", m.tags, m.meta, map[string]interface{}{"value": numInodeUpdates}, timestamp) - if err == nil { + if y, err := lp.New("gpfs_num_inode_updates", m.tags, m.meta, map[string]interface{}{"value": numInodeUpdates}, timestamp); err == nil { output <- y } }