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>
pull/1614/head
Ben Kochie 5 years ago committed by GitHub
parent ec62141388
commit c4183f9935
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -111,52 +111,42 @@ func NewPerfCollector(logger log.Logger) (Collector, error) {
logger: logger, logger: logger,
} }
var (
cpus []int
err error
)
if perfCPUsFlag != nil && *perfCPUsFlag != "" { if perfCPUsFlag != nil && *perfCPUsFlag != "" {
cpus, err := perfCPUFlagToCPUs(*perfCPUsFlag) cpus, err = perfCPUFlagToCPUs(*perfCPUsFlag)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, cpu := range cpus { } else {
// Use -1 to profile all processes on the CPU, see: cpus = make([]int, runtime.NumCPU())
// man perf_event_open for i := range cpus {
hwProf := perf.NewHardwareProfiler(-1, cpu) cpus[i] = i
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
cacheProf := perf.NewCacheProfiler(-1, cpu) for _, cpu := range cpus {
if err := cacheProf.Start(); err != nil { // Use -1 to profile all processes on the CPU, see:
return nil, err // man perf_event_open
} hwProf := perf.NewHardwareProfiler(-1, cpu)
collector.perfCacheProfilers[cpu] = &cacheProf if err := hwProf.Start(); err != nil {
return nil, err
} }
} else { collector.perfHwProfilers[cpu] = &hwProf
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) swProf := perf.NewSoftwareProfiler(-1, cpu)
if err := swProf.Start(); err != nil { if err := swProf.Start(); err != nil {
return nil, err return nil, err
} }
collector.perfSwProfilers[i] = &swProf collector.perfSwProfilers[cpu] = &swProf
cacheProf := perf.NewCacheProfiler(-1, i) cacheProf := perf.NewCacheProfiler(-1, cpu)
if err := cacheProf.Start(); err != nil { if err := cacheProf.Start(); err != nil {
return nil, err return nil, err
}
collector.perfCacheProfilers[i] = &cacheProf
} }
collector.perfCacheProfilers[cpu] = &cacheProf
} }
collector.desc = map[string]*prometheus.Desc{ 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 { func (c *perfCollector) updateHardwareStats(ch chan<- prometheus.Metric) error {
for _, profiler := range c.perfHwProfilers { for _, profiler := range c.perfHwProfilers {
cpuid := c.hwProfilerCPUMap[profiler] cpuid := c.hwProfilerCPUMap[profiler]
cpuStr := fmt.Sprintf("%d", cpuid) cpuStr := strconv.Itoa(cpuid)
hwProfile, err := (*profiler).Profile() hwProfile, err := (*profiler).Profile()
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save