From c4183f993534aad9e8d6b9bc890e05304eb1f5a6 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Thu, 20 Feb 2020 12:05:59 +0100 Subject: [PATCH] Minor cleanup in perf collector (#1616) * Use `strconv.Itoa()` instead of `fmt.Sprintf()` for simple conversion. * Eliminate copy-paste in collector setup. Signed-off-by: Ben Kochie --- collector/perf_linux.go | 66 +++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/collector/perf_linux.go b/collector/perf_linux.go index b67f970d..bb8716ad 100644 --- a/collector/perf_linux.go +++ b/collector/perf_linux.go @@ -111,52 +111,42 @@ func NewPerfCollector(logger log.Logger) (Collector, error) { logger: logger, } + var ( + cpus []int + err error + ) if perfCPUsFlag != nil && *perfCPUsFlag != "" { - cpus, err := perfCPUFlagToCPUs(*perfCPUsFlag) + cpus, err = perfCPUFlagToCPUs(*perfCPUsFlag) if err != nil { return nil, err } - for _, cpu := range cpus { - // Use -1 to profile all processes on the CPU, see: - // man perf_event_open - hwProf := perf.NewHardwareProfiler(-1, cpu) - if err := hwProf.Start(); err != nil { - return nil, err - } - collector.perfHwProfilers[cpu] = &hwProf - - swProf := perf.NewSoftwareProfiler(-1, cpu) - if err := swProf.Start(); err != nil { - return nil, err - } - collector.perfSwProfilers[cpu] = &swProf + } else { + cpus = make([]int, runtime.NumCPU()) + for i := range cpus { + cpus[i] = i + } + } - cacheProf := perf.NewCacheProfiler(-1, cpu) - if err := cacheProf.Start(); err != nil { - return nil, err - } - collector.perfCacheProfilers[cpu] = &cacheProf + for _, cpu := range cpus { + // Use -1 to profile all processes on the CPU, see: + // man perf_event_open + hwProf := perf.NewHardwareProfiler(-1, cpu) + if err := hwProf.Start(); err != nil { + return nil, err } - } else { - for i := 0; i < runtime.NumCPU(); i++ { - hwProf := perf.NewHardwareProfiler(-1, i) - if err := hwProf.Start(); err != nil { - return nil, err - } - collector.perfHwProfilers[i] = &hwProf + collector.perfHwProfilers[cpu] = &hwProf - swProf := perf.NewSoftwareProfiler(-1, i) - if err := swProf.Start(); err != nil { - return nil, err - } - collector.perfSwProfilers[i] = &swProf + swProf := perf.NewSoftwareProfiler(-1, cpu) + if err := swProf.Start(); err != nil { + return nil, err + } + collector.perfSwProfilers[cpu] = &swProf - cacheProf := perf.NewCacheProfiler(-1, i) - if err := cacheProf.Start(); err != nil { - return nil, err - } - collector.perfCacheProfilers[i] = &cacheProf + cacheProf := perf.NewCacheProfiler(-1, cpu) + if err := cacheProf.Start(); err != nil { + return nil, err } + collector.perfCacheProfilers[cpu] = &cacheProf } collector.desc = map[string]*prometheus.Desc{ @@ -425,7 +415,7 @@ func (c *perfCollector) Update(ch chan<- prometheus.Metric) error { func (c *perfCollector) updateHardwareStats(ch chan<- prometheus.Metric) error { for _, profiler := range c.perfHwProfilers { cpuid := c.hwProfilerCPUMap[profiler] - cpuStr := fmt.Sprintf("%d", cpuid) + cpuStr := strconv.Itoa(cpuid) hwProfile, err := (*profiler).Profile() if err != nil { return err