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 <superq@gmail.com>
This commit is contained in:
Ben Kochie 2020-02-20 12:05:59 +01:00 committed by GitHub
parent ec62141388
commit c4183f9935
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -111,11 +111,22 @@ 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
}
} else {
cpus = make([]int, runtime.NumCPU())
for i := range cpus {
cpus[i] = i
}
}
for _, cpu := range cpus {
// Use -1 to profile all processes on the CPU, see:
// man perf_event_open
@ -137,27 +148,6 @@ func NewPerfCollector(logger log.Logger) (Collector, error) {
}
collector.perfCacheProfilers[cpu] = &cacheProf
}
} 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
swProf := perf.NewSoftwareProfiler(-1, i)
if err := swProf.Start(); err != nil {
return nil, err
}
collector.perfSwProfilers[i] = &swProf
cacheProf := perf.NewCacheProfiler(-1, i)
if err := cacheProf.Start(); err != nil {
return nil, err
}
collector.perfCacheProfilers[i] = &cacheProf
}
}
collector.desc = map[string]*prometheus.Desc{
"cpucycles_total": prometheus.NewDesc(
@ -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