From 38c589042815e09ce3bda631f3e244f952ed1721 Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Sat, 31 Dec 2016 07:23:55 +0100 Subject: [PATCH] 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. --- collector/devstat_freebsd.c | 5 ++--- collector/devstat_freebsd.go | 10 +++++++++- collector/devstat_freebsd.h | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/collector/devstat_freebsd.c b/collector/devstat_freebsd.c index 80a24e41..c3352e2c 100644 --- a/collector/devstat_freebsd.c +++ b/collector/devstat_freebsd.c @@ -10,10 +10,9 @@ #include -int _get_stats(Stats **stats) { +int _get_stats(struct devinfo *info, Stats **stats) { struct statinfo current; - struct devinfo info = {}; - current.dinfo = &info; + current.dinfo = info; if (devstat_getdevs(NULL, ¤t) == -1) { return -1; diff --git a/collector/devstat_freebsd.go b/collector/devstat_freebsd.go index 34bc53c8..79176026 100644 --- a/collector/devstat_freebsd.go +++ b/collector/devstat_freebsd.go @@ -18,6 +18,7 @@ package collector import ( "errors" "fmt" + "sync" "unsafe" "github.com/prometheus/client_golang/prometheus" @@ -32,6 +33,9 @@ const ( ) type devstatCollector struct { + mu sync.Mutex + devinfo *C.struct_devinfo + bytes typedDesc bytes_total typedDesc transfers typedDesc @@ -48,6 +52,7 @@ func init() { // Device stats. func NewDevstatCollector() (Collector, error) { return &devstatCollector{ + devinfo: &C.struct_devinfo{}, bytes: typedDesc{prometheus.NewDesc( prometheus.BuildFQName(Namespace, devstatSubsystem, "bytes_total"), "The total number of bytes in transactions.", @@ -77,8 +82,11 @@ func NewDevstatCollector() (Collector, error) { } func (c *devstatCollector) Update(ch chan<- prometheus.Metric) error { + c.mu.Lock() + defer c.mu.Unlock() + var stats *C.Stats - n := C._get_stats(&stats) + n := C._get_stats(c.devinfo, &stats) if n == -1 { return errors.New("devstat_getdevs failed") } diff --git a/collector/devstat_freebsd.h b/collector/devstat_freebsd.h index 87f09f0b..4ee1c62d 100644 --- a/collector/devstat_freebsd.h +++ b/collector/devstat_freebsd.h @@ -38,4 +38,4 @@ typedef struct { int _get_ndevs(); -int _get_stats(Stats **stats); +int _get_stats(struct devinfo *info, Stats **stats);