mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-11-09 23:24:09 -08:00
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:
parent
ea55d0f5cb
commit
38c5890428
|
@ -10,10 +10,9 @@
|
|||
#include <devstat_freebsd.h>
|
||||
|
||||
|
||||
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;
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -38,4 +38,4 @@ typedef struct {
|
|||
|
||||
|
||||
int _get_ndevs();
|
||||
int _get_stats(Stats **stats);
|
||||
int _get_stats(struct devinfo *info, Stats **stats);
|
||||
|
|
Loading…
Reference in a new issue