Commit 827b6622 authored by ale's avatar ale
Browse files

Export CPU times in seconds

Use the system's value for HZ (100 by default), which is the unit that
cgroups cpuacct statistics are using.
parent 98226068
Pipeline #6255 passed with stages
in 2 minutes and 59 seconds
......@@ -20,14 +20,24 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tklauser/go-sysconf"
)
var (
addr = flag.String("addr", ":3909", "address to listen on")
updateInterval = flag.Duration("interval", 10*time.Second, "update interval")
doDebug = flag.Bool("debug", false, "log debug messages")
userHZ float64
)
func init() {
userHZ = 100
if clktck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK); err == nil {
userHZ = float64(clktck)
}
}
func debug(s string, args ...interface{}) {
if *doDebug {
log.Printf(s, args...)
......@@ -133,13 +143,13 @@ func (p *cpuParser) parse(path string) ([]prometheus.Metric, error) {
prometheus.MustNewConstMetric(
p.desc,
prometheus.GaugeValue,
float64(usage["user"]),
float64(usage["user"])/userHZ,
"user", slice, name,
),
prometheus.MustNewConstMetric(
p.desc,
prometheus.GaugeValue,
float64(usage["system"]),
float64(usage["system"])/userHZ,
"system", slice, name,
),
}, nil
......
BSD 3-Clause License
Copyright (c) 2018, Tobias Klauser
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# go-sysconf
[![Build Status][1]][2]
[![Go Report Card][3]][4]
[![GoDoc][5]][6]
`sysconf` for Go, without using cgo or external binaries (e.g. getconf).
Supported operating systems: Linux, Darwin, DragonflyBSD, FreeBSD, NetBSD, OpenBSD.
Support for Solaris is planned but not yet implemented.
All POSIX.1 and POSIX.2 variables are supported, see [References](#references) for a complete list.
Additionally, the following non-standard variables are supported on some operating systems:
| Variable | Supported on |
|---|---|
| `SC_PHYS_PAGES` | Linux, Darwin, FreeBSD, NetBSD, OpenBSD |
| `SC_AVPHYS_PAGES` | Linux, OpenBSD |
| `SC_NPROCESSORS_CONF` | Linux, Darwin, FreeBSD, NetBSD, OpenBSD |
| `SC_NPROCESSORS_ONLN` | Linux, Darwin, FreeBSD, NetBSD, OpenBSD |
| `SC_UIO_MAXIOV` | Linux |
## Usage
```Go
package main
import (
"fmt"
"github.com/tklauser/go-sysconf"
)
func main() {
// get clock ticks, this will return the same as C.sysconf(C._SC_CLK_TCK)
clktck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
fmt.Printf("SC_CLK_TCK: %v\n", clktck)
}
}
```
## References
* [POSIX documenation for `sysconf`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html)
* [Linux manpage for `sysconf(3)`](http://man7.org/linux/man-pages/man3/sysconf.3.html)
* [glibc constants for `sysconf` parameters](https://www.gnu.org/software/libc/manual/html_node/Constants-for-Sysconf.html)
[1]: https://travis-ci.org/tklauser/go-sysconf.svg?branch=master
[2]: https://travis-ci.org/tklauser/go-sysconf
[3]: https://goreportcard.com/badge/github.com/tklauser/go-sysconf
[4]: https://goreportcard.com/report/github.com/tklauser/go-sysconf
[5]: https://godoc.org/github.com/tklauser/go-sysconf?status.svg
[6]: https://godoc.org/github.com/tklauser/go-sysconf
module github.com/tklauser/go-sysconf
require (
github.com/tklauser/numcpus v0.0.0-20181213170026-542d6d9638d9
golang.org/x/sys v0.0.0-20181004145325-8469e314837c
)
github.com/tklauser/numcpus v0.0.0-20181213170026-542d6d9638d9 h1:+M3ARWFvem1n+XioV5TtW2DxA/e2/5u3osWH9rWVw74=
github.com/tklauser/numcpus v0.0.0-20181213170026-542d6d9638d9/go.mod h1:Uk6N9CcYOM0f8nBfmdf9874a13Z74bkFHwwvnf5D6/k=
golang.org/x/sys v0.0.0-20181004145325-8469e314837c h1:SJ7JoQNVl3mC7EWkkONgBWgCno8LcABIJwFMkWBC+EY=
golang.org/x/sys v0.0.0-20181004145325-8469e314837c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
// Copyright 2018 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
package main
import (
"fmt"
"go/format"
"io/ioutil"
"os"
"os/exec"
"runtime"
)
func gensysconf() error {
defs := "sysconf_defs_" + runtime.GOOS + ".go"
cmd := exec.Command("go", "tool", "cgo", "-godefs", defs)
defer os.RemoveAll("_obj")
b, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprint(os.Stderr, string(b))
return err
}
b, err = format.Source(b)
if err != nil {
return err
}
zsysconf := "z" + defs
// TODO(tk): differentiate per GOARCH?
if err := ioutil.WriteFile(zsysconf, b, 0644); err != nil {
return err
}
return nil
}
func main() {
if err := gensysconf(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
// Copyright 2018 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sysconf implements the sysconf(3) function and provides the
// associated SC_* constants to query system configuration values.
package sysconf
import (
"errors"
"os"
)
//go:generate go run mksysconf.go
var errInvalid = errors.New("invalid parameter value")
// Sysconf returns the value of a sysconf(3) runtime system parameter.
// The name parameter should be a SC_* constant define in this package. The
// implementation is GOOS-specific and certain SC_* constants might not be
// defined for all GOOSes.
func Sysconf(name int) (int64, error) {
// OS-specific sysconf
if sc, err := sysconf(name); err == nil {
return sc, nil
}
// POSIX default values
if sc, err := sysconfPOSIX(name); err == nil {
return sc, nil
}
switch name {
case SC_BC_BASE_MAX:
return _BC_BASE_MAX, nil
case SC_BC_DIM_MAX:
return _BC_DIM_MAX, nil
case SC_BC_SCALE_MAX:
return _BC_SCALE_MAX, nil
case SC_BC_STRING_MAX:
return _BC_STRING_MAX, nil
case SC_COLL_WEIGHTS_MAX:
return _COLL_WEIGHTS_MAX, nil
case SC_EXPR_NEST_MAX:
return _EXPR_NEST_MAX, nil
case SC_HOST_NAME_MAX:
return _HOST_NAME_MAX, nil
case SC_LINE_MAX:
return _LINE_MAX, nil
case SC_LOGIN_NAME_MAX:
return _LOGIN_NAME_MAX, nil
case SC_PAGESIZE: // same as SC_PAGE_SIZE
return int64(os.Getpagesize()), nil
case SC_RE_DUP_MAX:
return _RE_DUP_MAX, nil
case SC_SYMLOOP_MAX:
return _SYMLOOP_MAX, nil
}
return -1, errInvalid
}
// Copyright 2018 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd netbsd openbsd
package sysconf
import "golang.org/x/sys/unix"
func pathconf(path string, name int) int64 {
if val, err := unix.Pathconf(path, name); err == nil {
return int64(val)
}
return -1
}
func sysctl32(name string) int64 {
if val, err := unix.SysctlUint32(name); err == nil {
return int64(val)
}
return -1
}
func sysctl64(name string) int64 {
if val, err := unix.SysctlUint64(name); err == nil {
return int64(val)
}
return -1
}
func yesno(val int64) int64 {
if val == 0 {
return -1
}
return val
}
// Copyright 2018 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sysconf
import (
"sync"
"golang.org/x/sys/unix"
)
const (
_HOST_NAME_MAX = _MAXHOSTNAMELEN - 1
_LOGIN_NAME_MAX = _MAXLOGNAME
_SYMLOOP_MAX = _MAXSYMLINKS
)
var (
clktck int64
clktckOnce sync.Once
)
// sysconf implements sysconf(3) as in the Darwin libc, version 1244.30.3
// (derived from the FreeBSD libc).
func sysconf(name int) (int64, error) {
switch name {
case SC_AIO_LISTIO_MAX:
fallthrough
case SC_AIO_MAX:
return sysctl32("kern.aiomax"), nil
case SC_AIO_PRIO_DELTA_MAX:
return -1, nil
case SC_ARG_MAX:
return sysctl32("kern.argmax"), nil
case SC_ATEXIT_MAX:
return _INT_MAX, nil
case SC_CHILD_MAX:
var rlim unix.Rlimit
if err := unix.Getrlimit(unix.RLIMIT_NPROC, &rlim); err == nil {
if rlim.Cur != unix.RLIM_INFINITY {
return int64(rlim.Cur), nil
}
}
return -1, nil
case SC_CLK_TCK:
return _CLK_TCK, nil
case SC_DELAYTIMER_MAX:
return -1, nil
case SC_GETGR_R_SIZE_MAX:
return 4096, nil
case SC_GETPW_R_SIZE_MAX:
return 4096, nil
case SC_IOV_MAX:
return _IOV_MAX, nil
case SC_MQ_OPEN_MAX:
return -1, nil
case SC_MQ_PRIO_MAX:
return -1, nil
case SC_NGROUPS_MAX:
return sysctl32("kern.ngroups"), nil
case SC_OPEN_MAX, SC_STREAM_MAX:
var rlim unix.Rlimit
if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlim); err == nil {
if rlim.Cur != unix.RLIM_INFINITY {
return int64(rlim.Cur), nil
}
}
return -1, nil
case SC_RTSIG_MAX:
return -1, nil
case SC_SEM_NSEMS_MAX:
return sysctl32("kern.sysv.semmns"), nil
case SC_SEM_VALUE_MAX:
return _POSIX_SEM_VALUE_MAX, nil
case SC_SIGQUEUE_MAX:
return -1, nil
case SC_THREAD_DESTRUCTOR_ITERATIONS:
return _PTHREAD_DESTRUCTOR_ITERATIONS, nil
case SC_THREAD_KEYS_MAX:
return _PTHREAD_KEYS_MAX, nil
case SC_THREAD_PRIO_INHERIT:
return _POSIX_THREAD_PRIO_INHERIT, nil
case SC_THREAD_PRIO_PROTECT:
return _POSIX_THREAD_PRIO_PROTECT, nil
case SC_THREAD_STACK_MIN:
return _PTHREAD_STACK_MIN, nil
case SC_THREAD_THREADS_MAX:
return -1, nil
case SC_TIMER_MAX:
return -1, nil
case SC_TTY_NAME_MAX:
// should be _PATH_DEV instead of "/"
return pathconf("/", _PC_NAME_MAX), nil
case SC_TZNAME_MAX:
return pathconf(_PATH_ZONEINFO, _PC_NAME_MAX), nil
case SC_IPV6:
if _POSIX_IPV6 == 0 {
fd, err := unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0)
if err == nil && fd >= 0 {
unix.Close(fd)
return int64(200112), nil
}
return 0, nil
}
return _POSIX_IPV6, nil
case SC_MESSAGE_PASSING:
if _POSIX_MESSAGE_PASSING == 0 {
return yesno(sysctl32("p1003_1b.message_passing")), nil
}
return _POSIX_MESSAGE_PASSING, nil
case SC_PRIORITIZED_IO:
if _POSIX_PRIORITIZED_IO == 0 {
return yesno(sysctl32("p1003_1b.prioritized_io")), nil
}
return _POSIX_PRIORITIZED_IO, nil
case SC_PRIORITY_SCHEDULING:
if _POSIX_PRIORITY_SCHEDULING == 0 {
return yesno(sysctl32("p1003_1b.priority_scheduling")), nil
}
return _POSIX_PRIORITY_SCHEDULING, nil
case SC_REALTIME_SIGNALS:
if _POSIX_REALTIME_SIGNALS == 0 {
return yesno(sysctl32("p1003_1b.realtime_signals")), nil
}
return _POSIX_REALTIME_SIGNALS, nil
case SC_SAVED_IDS:
return yesno(sysctl32("kern.saved_ids")), nil
case SC_SEMAPHORES:
if _POSIX_SEMAPHORES == 0 {
return yesno(sysctl32("p1003_1b.semaphores")), nil
}
return _POSIX_SEMAPHORES, nil
case SC_SPAWN:
return _POSIX_SPAWN, nil
case SC_SPIN_LOCKS:
return _POSIX_SPIN_LOCKS, nil
case SC_SPORADIC_SERVER:
return _POSIX_SPORADIC_SERVER, nil
case SC_SS_REPL_MAX:
return _POSIX_SS_REPL_MAX, nil
case SC_SYNCHRONIZED_IO:
if _POSIX_SYNCHRONIZED_IO == 0 {
return yesno(sysctl32("p1003_1b.synchronized_io")), nil
}
return _POSIX_SYNCHRONIZED_IO, nil
case SC_THREAD_ATTR_STACKADDR:
return _POSIX_THREAD_ATTR_STACKADDR, nil
case SC_THREAD_ATTR_STACKSIZE:
return _POSIX_THREAD_ATTR_STACKSIZE, nil
case SC_THREAD_CPUTIME:
return _POSIX_THREAD_CPUTIME, nil
case SC_THREAD_PRIORITY_SCHEDULING:
return _POSIX_THREAD_PRIORITY_SCHEDULING, nil
case SC_THREAD_PROCESS_SHARED:
return _POSIX_THREAD_PROCESS_SHARED, nil
case SC_THREAD_SAFE_FUNCTIONS:
return _POSIX_THREAD_SAFE_FUNCTIONS, nil
case SC_THREAD_SPORADIC_SERVER:
return _POSIX_THREAD_SPORADIC_SERVER, nil
case SC_TIMERS:
if _POSIX_TIMERS == 0 {
return yesno(sysctl32("p1003_1b.timers")), nil
}
return _POSIX_TIMERS, nil
case SC_TRACE:
return _POSIX_TRACE, nil
case SC_TRACE_EVENT_FILTER:
return _POSIX_TRACE_EVENT_FILTER, nil
case SC_TRACE_EVENT_NAME_MAX:
return _POSIX_TRACE_EVENT_NAME_MAX, nil
case SC_TRACE_INHERIT:
return _POSIX_TRACE_INHERIT, nil
case SC_TRACE_LOG:
return _POSIX_TRACE_LOG, nil
case SC_TRACE_NAME_MAX:
return _POSIX_TRACE_NAME_MAX, nil
case SC_TRACE_SYS_MAX:
return _POSIX_TRACE_SYS_MAX, nil
case SC_TRACE_USER_EVENT_MAX:
return _POSIX_TRACE_USER_EVENT_MAX, nil
case SC_TYPED_MEMORY_OBJECTS:
return _POSIX_TYPED_MEMORY_OBJECTS, nil
case SC_VERSION:
// TODO(tk): darwin libc uses sysctl(CTL_KERN, KERN_POSIX1)
return _POSIX_VERSION, nil
case SC_V6_ILP32_OFF32:
if _V6_ILP32_OFF32 == 0 {
if unix.SizeofInt*_CHAR_BIT == 32 &&
unix.SizeofInt == unix.SizeofLong &&
unix.SizeofLong == unix.SizeofPtr &&
unix.SizeofPtr == sizeofOffT {
return 1, nil
} else {
return -1, nil
}
}
return _V6_ILP32_OFF32, nil
case SC_V6_ILP32_OFFBIG:
if _V6_ILP32_OFFBIG == 0 {
if unix.SizeofInt*_CHAR_BIT == 32 &&
unix.SizeofInt == unix.SizeofLong &&
unix.SizeofLong == unix.SizeofPtr &&
sizeofOffT*_CHAR_BIT >= 64 {
return 1, nil
} else {
return -1, nil
}
}
return _V6_ILP32_OFFBIG, nil
case SC_V6_LP64_OFF64:
if _V6_LP64_OFF64 == 0 {
if unix.SizeofInt*_CHAR_BIT == 32 &&
unix.SizeofLong*_CHAR_BIT == 64 &&
unix.SizeofLong == unix.SizeofPtr &&
unix.SizeofPtr == sizeofOffT {
return 1, nil
} else {
return -1, nil
}
}
return _V6_LP64_OFF64, nil
case SC_V6_LPBIG_OFFBIG:
if _V6_LPBIG_OFFBIG == 0 {
if unix.SizeofInt*_CHAR_BIT >= 32 &&
unix.SizeofLong*_CHAR_BIT >= 64 &&
unix.SizeofPtr*_CHAR_BIT >= 64 &&
sizeofOffT*_CHAR_BIT >= 64 {
return 1, nil
} else {
return -1, nil
}
}
return _V6_LPBIG_OFFBIG, nil
case SC_2_CHAR_TERM:
return _POSIX2_CHAR_TERM, nil
case SC_2_PBS,
SC_2_PBS_ACCOUNTING,
SC_2_PBS_CHECKPOINT,
SC_2_PBS_LOCATE,
SC_2_PBS_MESSAGE,
SC_2_PBS_TRACK:
return _POSIX2_PBS, nil
case SC_2_UPE:
return _POSIX2_UPE, nil
case SC_XOPEN_CRYPT:
return _XOPEN_CRYPT, nil
case SC_XOPEN_ENH_I18N:
return _XOPEN_ENH_I18N, nil
case SC_XOPEN_REALTIME:
return _XOPEN_REALTIME, nil
case SC_XOPEN_REALTIME_THREADS:
return _XOPEN_REALTIME_THREADS, nil
case SC_XOPEN_SHM:
return _XOPEN_SHM, nil
case SC_XOPEN_STREAMS:
return -1, nil
case SC_XOPEN_UNIX:
return _XOPEN_UNIX, nil
case SC_XOPEN_VERSION:
return _XOPEN_VERSION, nil
case SC_XOPEN_XCU_VERSION:
return _XOPEN_XCU_VERSION, nil
case SC_PHYS_PAGES:
return sysctl64("hw.memsize") / int64(unix.Getpagesize()), nil
case SC_NPROCESSORS_CONF:
fallthrough
case SC_NPROCESSORS_ONLN:
return sysctl32("hw.ncpu"), nil
}
return -1, errInvalid
}
// Copyright 2018 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
package sysconf
/*
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <sys/param.h>
#include <time.h>
#include <unistd.h>
#ifndef _PATH_ZONEINFO
# define _PATH_ZONEINFO "/usr/share/zoneinfo" // TZDATA from tzcode/stdtime/tzfile.h
#endif
// off_t always __int64_t on darwin, see /usr/include/_types.h
enum {
sizeofOffT = sizeof(__int64_t)
};
*/
import "C"
// sysconf variables
const (
SC_AIO_LISTIO_MAX = C._SC_AIO_LISTIO_MAX
SC_AIO_MAX = C._SC_AIO_MAX
SC_AIO_PRIO_DELTA_MAX = C._SC_AIO_PRIO_DELTA_MAX
SC_ARG_MAX = C._SC_ARG_MAX
SC_ATEXIT_MAX = C._SC_ATEXIT_MAX
SC_BC_BASE_MAX = C._SC_BC_BASE_MAX
SC_BC_DIM_MAX = C._SC_BC_DIM_MAX