2015-09-26 17:36:40 +02:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-06-24 14:45:40 +02:00
|
|
|
// +build !nocpu
|
2015-05-12 09:13:08 +02:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-05-20 09:52:00 +02:00
|
|
|
"os"
|
2015-05-12 09:13:08 +02:00
|
|
|
"strconv"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo LDFLAGS: -lkvm
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <kvm.h>
|
2015-09-27 00:08:18 +02:00
|
|
|
#include <stdlib.h>
|
2015-05-12 09:13:08 +02:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/pcpu.h>
|
|
|
|
#include <sys/resource.h>
|
2015-09-27 00:08:18 +02:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
long _clockrate() {
|
|
|
|
struct clockinfo clockrate;
|
|
|
|
size_t size = sizeof(clockrate);
|
|
|
|
int res = sysctlbyname("kern.clockrate", &clockrate, &size, NULL, 0);
|
|
|
|
if (res == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (size != sizeof(clockrate)) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
return clockrate.stathz > 0 ? clockrate.stathz : clockrate.hz;
|
|
|
|
}
|
|
|
|
|
2015-05-12 09:13:08 +02:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
type statCollector struct {
|
2015-06-23 15:49:18 +02:00
|
|
|
cpu *prometheus.CounterVec
|
2015-05-12 09:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["cpu"] = NewStatCollector
|
|
|
|
}
|
|
|
|
|
2015-06-23 22:14:52 +02:00
|
|
|
// Takes a prometheus registry and returns a new Collector exposing
|
|
|
|
// CPU stats.
|
2015-06-23 15:49:18 +02:00
|
|
|
func NewStatCollector() (Collector, error) {
|
2015-05-12 09:13:08 +02:00
|
|
|
return &statCollector{
|
|
|
|
cpu: prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: Namespace,
|
2015-06-23 22:14:52 +02:00
|
|
|
Name: "cpu_seconds_total",
|
|
|
|
Help: "Seconds the CPU spent in each mode.",
|
2015-05-12 09:13:08 +02:00
|
|
|
},
|
|
|
|
[]string{"cpu", "mode"},
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-06-23 22:14:52 +02:00
|
|
|
// Expose CPU stats using KVM.
|
2015-05-12 09:13:08 +02:00
|
|
|
func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2015-05-20 09:52:00 +02:00
|
|
|
if os.Geteuid() != 0 && os.Getegid() != 2 {
|
2015-06-23 22:14:52 +02:00
|
|
|
return errors.New("caller should be either root user or kmem group to access /dev/mem")
|
2015-05-20 09:52:00 +02:00
|
|
|
}
|
|
|
|
|
2015-05-12 09:13:08 +02:00
|
|
|
var errbuf *C.char
|
|
|
|
kd := C.kvm_open(nil, nil, nil, C.O_RDONLY, errbuf)
|
|
|
|
if errbuf != nil {
|
2015-07-13 23:29:55 +02:00
|
|
|
return errors.New("failed to call kvm_open()")
|
2015-05-12 09:13:08 +02:00
|
|
|
}
|
|
|
|
defer C.kvm_close(kd)
|
|
|
|
|
2015-09-27 00:08:18 +02:00
|
|
|
// The cp_time variable is an array of CPUSTATES long integers -- in
|
|
|
|
// the same format as the kern.cp_time sysctl. According to the
|
|
|
|
// comments in sys/kern/kern_clock.c, the frequency of this timer will
|
|
|
|
// be stathz (or hz, if stathz is zero).
|
|
|
|
clockrate, err := getClockRate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-12 09:13:08 +02:00
|
|
|
ncpus := C.kvm_getncpus(kd)
|
|
|
|
for i := 0; i < int(ncpus); i++ {
|
|
|
|
pcpu := C.kvm_getpcpu(kd, C.int(i))
|
|
|
|
cp_time := ((*C.struct_pcpu)(unsafe.Pointer(pcpu))).pc_cp_time
|
2015-12-18 15:08:52 +01:00
|
|
|
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "user"}).Set(float64(cp_time[C.CP_USER]) / clockrate)
|
|
|
|
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "nice"}).Set(float64(cp_time[C.CP_NICE]) / clockrate)
|
|
|
|
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "system"}).Set(float64(cp_time[C.CP_SYS]) / clockrate)
|
|
|
|
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "interrupt"}).Set(float64(cp_time[C.CP_INTR]) / clockrate)
|
|
|
|
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "idle"}).Set(float64(cp_time[C.CP_IDLE]) / clockrate)
|
2015-05-12 09:13:08 +02:00
|
|
|
}
|
|
|
|
c.cpu.Collect(ch)
|
|
|
|
return err
|
|
|
|
}
|
2015-09-27 00:08:18 +02:00
|
|
|
|
|
|
|
func getClockRate() (float64, error) {
|
|
|
|
clockrate := C._clockrate()
|
|
|
|
if clockrate == -1 {
|
|
|
|
return 0, errors.New("sysctl(kern.clockrate) failed")
|
|
|
|
} else if clockrate == -2 {
|
|
|
|
return 0, errors.New("sysctl(kern.clockrate) failed, wrong buffer size")
|
|
|
|
} else if clockrate <= 0 {
|
|
|
|
return 0, errors.New("sysctl(kern.clockrate) bad clocktime")
|
|
|
|
}
|
|
|
|
return float64(clockrate), nil
|
|
|
|
}
|