summaryrefslogtreecommitdiff
path: root/net-mgmt/collectd5/files/patch-src_disk.c
blob: fd0525ffa622b7ebef022cf282d3b56f62abf3bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
--- src/disk.c.orig	2016-01-22 09:51:17 UTC
+++ src/disk.c
@@ -53,6 +53,10 @@
 #if HAVE_IOKIT_IOBSD_H
 #  include <IOKit/IOBSD.h>
 #endif
+#if KERNEL_FREEBSD
+#include <devstat.h>
+#include <libgeom.h>
+#endif
 
 #if HAVE_LIMITS_H
 # include <limits.h>
@@ -107,6 +111,9 @@ typedef struct diskstats
 
 static diskstats_t *disklist;
 /* #endif KERNEL_LINUX */
+#elif KERNEL_FREEBSD
+static struct gmesh geom_tree;
+/* #endif KERNEL_FREEBSD */
 
 #elif HAVE_LIBKSTAT
 #define MAX_NUMDISK 1024
@@ -222,6 +229,21 @@ static int disk_init (void)
 	/* do nothing */
 /* #endif KERNEL_LINUX */
 
+#elif KERNEL_FREEBSD
+	int rv;
+
+	rv = geom_gettree(&geom_tree);
+	if (rv != 0) {
+		ERROR ("geom_gettree() failed, returned %d", rv);
+		return (-1);
+	}
+	rv = geom_stats_open();
+	if (rv != 0) {
+		ERROR ("geom_stats_open() failed, returned %d", rv);
+		return (-1);
+	}
+/* #endif KERNEL_FREEBSD */
+
 #elif HAVE_LIBKSTAT
 	kstat_t *ksp_chain;
 
@@ -505,6 +527,114 @@ static int disk_read (void)
 	IOObjectRelease (disk_list);
 /* #endif HAVE_IOKIT_IOKITLIB_H */
 
+#elif KERNEL_FREEBSD
+	int retry, dirty;
+
+	void *snap = NULL;
+	struct devstat *snap_iter;
+
+	struct gident *geom_id;
+
+	const char *disk_name;
+	long double read_time, write_time;
+
+	for (retry = 0, dirty = 1; retry < 5 && dirty == 1; retry++) {
+		if (snap != NULL)
+			geom_stats_snapshot_free(snap);
+
+		/* Get a fresh copy of stats snapshot */
+		snap = geom_stats_snapshot_get();
+		if (snap == NULL) {
+			ERROR("disk plugin: geom_stats_snapshot_get() failed.");
+			return (-1);
+		}
+
+		/* Check if we have dirty read from this snapshot */
+		dirty = 0;
+		geom_stats_snapshot_reset(snap);
+		while ((snap_iter = geom_stats_snapshot_next(snap)) != NULL) {
+			if (snap_iter->id == NULL)
+				continue;
+			geom_id = geom_lookupid(&geom_tree, snap_iter->id);
+
+			/* New device? refresh GEOM tree */
+			if (geom_id == NULL) {
+				geom_deletetree(&geom_tree);
+				if (geom_gettree(&geom_tree) != 0) {
+					ERROR("disk plugin: geom_gettree() failed");
+					geom_stats_snapshot_free(snap);
+					return (-1);
+				}
+				geom_id = geom_lookupid(&geom_tree, snap_iter->id);
+			}
+			/*
+			 * This should be rare: the device come right before we take the
+			 * snapshot and went away right after it.  We will handle this
+			 * case later, so don't mark dirty but silently ignore it.
+			 */
+			if (geom_id == NULL)
+				continue;
+
+			/* Only collect PROVIDER data */
+			if (geom_id->lg_what != ISPROVIDER)
+				continue;
+
+			/* Only collect data when rank is 1 (physical devices) */
+			if (((struct gprovider *)(geom_id->lg_ptr))->lg_geom->lg_rank != 1)
+				continue;
+
+			/* Check if this is a dirty read quit for another try */
+			if (snap_iter->sequence0 != snap_iter->sequence1) {
+				dirty = 1;
+				break;
+			}
+		}
+	}
+
+	/* Reset iterator */
+	geom_stats_snapshot_reset(snap);
+	for (;;) {
+		snap_iter = geom_stats_snapshot_next(snap);
+		if (snap_iter == NULL)
+			break;
+
+		if (snap_iter->id == NULL)
+			continue;
+		geom_id = geom_lookupid(&geom_tree, snap_iter->id);
+		if (geom_id == NULL)
+			continue;
+		if (geom_id->lg_what != ISPROVIDER)
+			continue;
+		if (((struct gprovider *)(geom_id->lg_ptr))->lg_geom->lg_rank != 1)
+			continue;
+		/* Skip dirty reads, if present */
+		if (dirty && (snap_iter->sequence0 != snap_iter->sequence1))
+			continue;
+
+		disk_name = ((struct gprovider *)geom_id->lg_ptr)->lg_name;
+
+		if ((snap_iter->bytes[1] != 0) || (snap_iter->bytes[2] != 0)) {
+			disk_submit(disk_name, "disk_octets",
+					(derive_t)snap_iter->bytes[1],
+					(derive_t)snap_iter->bytes[2]);
+		}
+
+		if ((snap_iter->operations[1] != 0) || (snap_iter->operations[2] != 0)) {
+			disk_submit(disk_name, "disk_ops",
+					(derive_t)snap_iter->operations[1],
+					(derive_t)snap_iter->operations[2]);
+		}
+
+		read_time = devstat_compute_etime(&snap_iter->duration[DEVSTAT_READ], NULL);
+		write_time = devstat_compute_etime(&snap_iter->duration[DEVSTAT_WRITE], NULL);
+		if ((read_time != 0) || (write_time != 0)) {
+			disk_submit (disk_name, "disk_time",
+					(derive_t)(read_time*1000), (derive_t)(write_time*1000));
+		}
+	}
+	geom_stats_snapshot_free(snap);
+/* #endif KERNEL_FREEBSD */
+
 #elif KERNEL_LINUX
 	FILE *fh;
 	char buffer[1024];