Reuse devinfo struct

The devstat API expects us to reuse one devinfo for many invocations of
devstat_getstats. In particular, it allocates and resizes memory
referenced by devinfo.
This commit is contained in:
Dominik Honnef 2016-12-31 07:23:55 +01:00
parent ea55d0f5cb
commit 38c5890428
3 changed files with 12 additions and 5 deletions

View file

@ -10,10 +10,9 @@
#include <devstat_freebsd.h> #include <devstat_freebsd.h>
int _get_stats(Stats **stats) { int _get_stats(struct devinfo *info, Stats **stats) {
struct statinfo current; struct statinfo current;
struct devinfo info = {}; current.dinfo = info;
current.dinfo = &info;
if (devstat_getdevs(NULL, &current) == -1) { if (devstat_getdevs(NULL, &current) == -1) {
return -1; return -1;

View file

@ -18,6 +18,7 @@ package collector
import ( import (
"errors" "errors"
"fmt" "fmt"
"sync"
"unsafe" "unsafe"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -32,6 +33,9 @@ const (
) )
type devstatCollector struct { type devstatCollector struct {
mu sync.Mutex
devinfo *C.struct_devinfo
bytes typedDesc bytes typedDesc
bytes_total typedDesc bytes_total typedDesc
transfers typedDesc transfers typedDesc
@ -48,6 +52,7 @@ func init() {
// Device stats. // Device stats.
func NewDevstatCollector() (Collector, error) { func NewDevstatCollector() (Collector, error) {
return &devstatCollector{ return &devstatCollector{
devinfo: &C.struct_devinfo{},
bytes: typedDesc{prometheus.NewDesc( bytes: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, devstatSubsystem, "bytes_total"), prometheus.BuildFQName(Namespace, devstatSubsystem, "bytes_total"),
"The total number of bytes in transactions.", "The total number of bytes in transactions.",
@ -77,8 +82,11 @@ func NewDevstatCollector() (Collector, error) {
} }
func (c *devstatCollector) Update(ch chan<- prometheus.Metric) error { func (c *devstatCollector) Update(ch chan<- prometheus.Metric) error {
c.mu.Lock()
defer c.mu.Unlock()
var stats *C.Stats var stats *C.Stats
n := C._get_stats(&stats) n := C._get_stats(c.devinfo, &stats)
if n == -1 { if n == -1 {
return errors.New("devstat_getdevs failed") return errors.New("devstat_getdevs failed")
} }

View file

@ -38,4 +38,4 @@ typedef struct {
int _get_ndevs(); int _get_ndevs();
int _get_stats(Stats **stats); int _get_stats(struct devinfo *info, Stats **stats);