From 5e220c1665e1cdbdb69cdcf6789f176ca907bd6e Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Sat, 31 Dec 2016 04:15:25 +0100 Subject: [PATCH] Move cgo portions of FreeBSD devstat collector into own file Embedding 100 lines of code in a comment doesn't make for good reading, editing or code quality. --- collector/devstat_freebsd.c | 74 ++++++++++++++++++++++++ collector/devstat_freebsd.go | 106 +---------------------------------- collector/devstat_freebsd.h | 41 ++++++++++++++ 3 files changed, 117 insertions(+), 104 deletions(-) create mode 100644 collector/devstat_freebsd.c create mode 100644 collector/devstat_freebsd.h diff --git a/collector/devstat_freebsd.c b/collector/devstat_freebsd.c new file mode 100644 index 00000000..3a8e6d31 --- /dev/null +++ b/collector/devstat_freebsd.c @@ -0,0 +1,74 @@ +// +build !nodevstat + +#include +#include +#include +#include +#include +#include +#include +#include + +int _get_ndevs() { + struct statinfo current; + struct devinfo info = {}; + current.dinfo = &info; + + devstat_checkversion(NULL); + + if (devstat_getdevs(NULL, ¤t) == -1) + return -1; + + return current.dinfo->numdevs; +} + +Stats _get_stats(int i) { + struct statinfo current; + struct devinfo info = {}; + current.dinfo = &info; + + devstat_getdevs(NULL, ¤t); + + Stats stats; + uint64_t bytes_read, bytes_write, bytes_free; + uint64_t transfers_other, transfers_read, transfers_write, transfers_free; + long double duration_other, duration_read, duration_write, duration_free; + long double busy_time; + uint64_t blocks; + + strcpy(stats.device, current.dinfo->devices[i].device_name); + stats.unit = current.dinfo->devices[i].unit_number; + devstat_compute_statistics(¤t.dinfo->devices[i], + NULL, + 1.0, + DSM_TOTAL_BYTES_READ, &bytes_read, + DSM_TOTAL_BYTES_WRITE, &bytes_write, + DSM_TOTAL_BYTES_FREE, &bytes_free, + DSM_TOTAL_TRANSFERS_OTHER, &transfers_other, + DSM_TOTAL_TRANSFERS_READ, &transfers_read, + DSM_TOTAL_TRANSFERS_WRITE, &transfers_write, + DSM_TOTAL_TRANSFERS_FREE, &transfers_free, + DSM_TOTAL_DURATION_OTHER, &duration_other, + DSM_TOTAL_DURATION_READ, &duration_read, + DSM_TOTAL_DURATION_WRITE, &duration_write, + DSM_TOTAL_DURATION_FREE, &duration_free, + DSM_TOTAL_BUSY_TIME, &busy_time, + DSM_TOTAL_BLOCKS, &blocks, + DSM_NONE); + + stats.bytes.read = bytes_read; + stats.bytes.write = bytes_write; + stats.bytes.free = bytes_free; + stats.transfers.other = transfers_other; + stats.transfers.read = transfers_read; + stats.transfers.write = transfers_write; + stats.transfers.free = transfers_free; + stats.duration.other = duration_other; + stats.duration.read = duration_read; + stats.duration.write = duration_write; + stats.duration.free = duration_free; + stats.busyTime = busy_time; + stats.blocks = blocks; + + return stats; +} diff --git a/collector/devstat_freebsd.go b/collector/devstat_freebsd.go index 7ea5a22a..6f3b3e91 100644 --- a/collector/devstat_freebsd.go +++ b/collector/devstat_freebsd.go @@ -22,110 +22,8 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -/* -#cgo LDFLAGS: -ldevstat -lkvm -#include -#include -#include -#include -#include -#include -#include - -typedef struct { - uint64_t read; - uint64_t write; - uint64_t free; -} Bytes; - -typedef struct { - uint64_t other; - uint64_t read; - uint64_t write; - uint64_t free; -} Transfers; - -typedef struct { - double other; - double read; - double write; - double free; -} Duration; - -typedef struct { - char device[DEVSTAT_NAME_LEN]; - int unit; - Bytes bytes; - Transfers transfers; - Duration duration; - long busyTime; - uint64_t blocks; -} Stats; - -int _get_ndevs() { - struct statinfo current; - struct devinfo info = {}; - current.dinfo = &info; - - devstat_checkversion(NULL); - - if (devstat_getdevs(NULL, ¤t) == -1) - return -1; - - return current.dinfo->numdevs; -} - -Stats _get_stats(int i) { - struct statinfo current; - struct devinfo info = {}; - current.dinfo = &info; - - devstat_getdevs(NULL, ¤t); - - Stats stats; - uint64_t bytes_read, bytes_write, bytes_free; - uint64_t transfers_other, transfers_read, transfers_write, transfers_free; - long double duration_other, duration_read, duration_write, duration_free; - long double busy_time; - uint64_t blocks; - - strcpy(stats.device, current.dinfo->devices[i].device_name); - stats.unit = current.dinfo->devices[i].unit_number; - devstat_compute_statistics(¤t.dinfo->devices[i], - NULL, - 1.0, - DSM_TOTAL_BYTES_READ, &bytes_read, - DSM_TOTAL_BYTES_WRITE, &bytes_write, - DSM_TOTAL_BYTES_FREE, &bytes_free, - DSM_TOTAL_TRANSFERS_OTHER, &transfers_other, - DSM_TOTAL_TRANSFERS_READ, &transfers_read, - DSM_TOTAL_TRANSFERS_WRITE, &transfers_write, - DSM_TOTAL_TRANSFERS_FREE, &transfers_free, - DSM_TOTAL_DURATION_OTHER, &duration_other, - DSM_TOTAL_DURATION_READ, &duration_read, - DSM_TOTAL_DURATION_WRITE, &duration_write, - DSM_TOTAL_DURATION_FREE, &duration_free, - DSM_TOTAL_BUSY_TIME, &busy_time, - DSM_TOTAL_BLOCKS, &blocks, - DSM_NONE); - - stats.bytes.read = bytes_read; - stats.bytes.write = bytes_write; - stats.bytes.free = bytes_free; - stats.transfers.other = transfers_other; - stats.transfers.read = transfers_read; - stats.transfers.write = transfers_write; - stats.transfers.free = transfers_free; - stats.duration.other = duration_other; - stats.duration.read = duration_read; - stats.duration.write = duration_write; - stats.duration.free = duration_free; - stats.busyTime = busy_time; - stats.blocks = blocks; - - return stats; -} -*/ +// #cgo LDFLAGS: -ldevstat -lkvm +// #include "devstat_freebsd.h" import "C" const ( diff --git a/collector/devstat_freebsd.h b/collector/devstat_freebsd.h new file mode 100644 index 00000000..13ea4bc6 --- /dev/null +++ b/collector/devstat_freebsd.h @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include +#include + +typedef struct { + uint64_t read; + uint64_t write; + uint64_t free; +} Bytes; + +typedef struct { + uint64_t other; + uint64_t read; + uint64_t write; + uint64_t free; +} Transfers; + +typedef struct { + double other; + double read; + double write; + double free; +} Duration; + +typedef struct { + char device[DEVSTAT_NAME_LEN]; + int unit; + Bytes bytes; + Transfers transfers; + Duration duration; + long busyTime; + uint64_t blocks; +} Stats; + + +int _get_ndevs(); +Stats _get_stats(int i);