Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / zfs_arc.c
1 /**
2  * collectd - src/zfs_arc.c
3  * Copyright (C) 2009  Anthony Dewhurst
4  * Copyright (C) 2012  Aurelien Rougemont
5  * Copyright (C) 2013  Xin Li
6  * Copyright (C) 2014  Marc Fournier
7  * Copyright (C) 2014  Wilfried Goesgens
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; only version 2 of the License is applicable.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  *
22  * Authors:
23  *   Anthony Dewhurst <dewhurst at gmail>
24  *   Aurelien Rougemont <beorn at gandi.net>
25  *   Xin Li <delphij at FreeBSD.org>
26  *   Marc Fournier <marc.fournier at camptocamp.com>
27  *   Wilfried Goesgens <dothebart at citadel.org>
28  **/
29
30 #include "collectd.h"
31
32 #include "common.h"
33 #include "plugin.h"
34
35 /*
36  * Global variables
37  */
38
39 #if defined(KERNEL_LINUX)
40 #include "utils_llist.h"
41 #define ZOL_ARCSTATS_FILE "/proc/spl/kstat/zfs/arcstats"
42
43 typedef llist_t kstat_t;
44
45 static int put_zfs_value(kstat_t *ksp, char const *k, value_t v) {
46   llentry_t *e;
47   char *k_copy;
48   value_t *v_copy;
49
50   k_copy = strdup(k);
51   if (k_copy == NULL)
52     return ENOMEM;
53
54   v_copy = malloc(sizeof(*v_copy));
55   if (v_copy == NULL) {
56     sfree(k_copy);
57     return ENOMEM;
58   }
59   *v_copy = v;
60
61   e = llentry_create(k_copy, v_copy);
62   if (e == NULL) {
63     sfree(v_copy);
64     sfree(k_copy);
65     return ENOMEM;
66   }
67
68   llist_append(ksp, e);
69   return 0;
70 }
71
72 static long long get_zfs_value(kstat_t *ksp, const char *key) {
73   llentry_t *e;
74   value_t *v;
75
76   e = llist_search(ksp, key);
77   if (e == NULL) {
78     ERROR("zfs_arc plugin: `llist_search` failed for key: '%s'.", key);
79     return (-1);
80   }
81
82   v = e->value;
83   return ((long long)v->derive);
84 }
85
86 static void free_zfs_values(kstat_t *ksp) {
87   if (ksp == NULL)
88     return;
89
90   for (llentry_t *e = llist_head(ksp); e != NULL; e = e->next) {
91     sfree(e->key);
92     sfree(e->value);
93   }
94
95   llist_destroy(ksp);
96 }
97
98 #elif defined(KERNEL_SOLARIS)
99 extern kstat_ctl_t *kc;
100
101 static long long get_zfs_value(kstat_t *ksp, char *name) {
102
103   return (get_kstat_value(ksp, name));
104 }
105 #elif defined(KERNEL_FREEBSD)
106 #include <sys/sysctl.h>
107 #include <sys/types.h>
108
109 const char zfs_arcstat[] = "kstat.zfs.misc.arcstats.";
110
111 #if !defined(kstat_t)
112 typedef void kstat_t;
113 #endif
114
115 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
116                                char const *name) {
117   char buffer[256];
118   long long value;
119   size_t valuelen = sizeof(value);
120   int rv;
121
122   ssnprintf(buffer, sizeof(buffer), "%s%s", zfs_arcstat, name);
123   rv = sysctlbyname(buffer, (void *)&value, &valuelen,
124                     /* new value = */ NULL, /* new length = */ (size_t)0);
125   if (rv == 0)
126     return (value);
127
128   return (-1);
129 }
130 #endif
131
132 static void za_submit(const char *type, const char *type_instance,
133                       value_t *values, int values_len) {
134   value_list_t vl = VALUE_LIST_INIT;
135
136   vl.values = values;
137   vl.values_len = values_len;
138
139   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
140   sstrncpy(vl.plugin, "zfs_arc", sizeof(vl.plugin));
141   sstrncpy(vl.type, type, sizeof(vl.type));
142   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
143
144   plugin_dispatch_values(&vl);
145 }
146
147 static void za_submit_gauge(const char *type, const char *type_instance,
148                             gauge_t value) {
149   value_t vv;
150
151   vv.gauge = value;
152   za_submit(type, type_instance, &vv, 1);
153 }
154
155 static int za_read_derive(kstat_t *ksp, const char *kstat_value,
156                           const char *type, const char *type_instance) {
157   long long tmp;
158   value_t v;
159
160   tmp = get_zfs_value(ksp, (char *)kstat_value);
161   if (tmp == -1LL) {
162     WARNING("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
163     return (-1);
164   }
165
166   v.derive = (derive_t)tmp;
167   za_submit(type, type_instance, /* values = */ &v, /* values_num = */ 1);
168   return (0);
169 }
170
171 static int za_read_gauge(kstat_t *ksp, const char *kstat_value,
172                          const char *type, const char *type_instance) {
173   long long tmp;
174   value_t v;
175
176   tmp = get_zfs_value(ksp, (char *)kstat_value);
177   if (tmp == -1LL) {
178     WARNING("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
179     return (-1);
180   }
181
182   v.gauge = (gauge_t)tmp;
183   za_submit(type, type_instance, /* values = */ &v, /* values_num = */ 1);
184   return (0);
185 }
186
187 static void za_submit_ratio(const char *type_instance, gauge_t hits,
188                             gauge_t misses) {
189   gauge_t ratio = NAN;
190
191   if (!isfinite(hits) || (hits < 0.0))
192     hits = 0.0;
193   if (!isfinite(misses) || (misses < 0.0))
194     misses = 0.0;
195
196   if ((hits != 0.0) || (misses != 0.0))
197     ratio = hits / (hits + misses);
198
199   za_submit_gauge("cache_ratio", type_instance, ratio);
200 }
201
202 static int za_read(void) {
203   gauge_t arc_hits, arc_misses, l2_hits, l2_misses;
204   value_t l2_io[2];
205   kstat_t *ksp = NULL;
206
207 #if defined(KERNEL_LINUX)
208   FILE *fh;
209   char buffer[1024];
210
211   fh = fopen(ZOL_ARCSTATS_FILE, "r");
212   if (fh == NULL) {
213     char errbuf[1024];
214     ERROR("zfs_arc plugin: Opening \"%s\" failed: %s", ZOL_ARCSTATS_FILE,
215           sstrerror(errno, errbuf, sizeof(errbuf)));
216     return (-1);
217   }
218
219   ksp = llist_create();
220   if (ksp == NULL) {
221     ERROR("zfs_arc plugin: `llist_create' failed.");
222     fclose(fh);
223     return (-1);
224   }
225
226   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
227     char *fields[3];
228     value_t v;
229     int status;
230
231     status = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
232     if (status != 3)
233       continue;
234
235     status = parse_value(fields[2], &v, DS_TYPE_DERIVE);
236     if (status != 0)
237       continue;
238
239     put_zfs_value(ksp, fields[0], v);
240   }
241
242   fclose(fh);
243
244 #elif defined(KERNEL_SOLARIS)
245   get_kstat(&ksp, "zfs", 0, "arcstats");
246   if (ksp == NULL) {
247     ERROR("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
248     return (-1);
249   }
250 #endif
251
252   /* Sizes */
253   za_read_gauge(ksp, "anon_size", "cache_size", "anon_size");
254   za_read_gauge(ksp, "c", "cache_size", "c");
255   za_read_gauge(ksp, "c_max", "cache_size", "c_max");
256   za_read_gauge(ksp, "c_min", "cache_size", "c_min");
257   za_read_gauge(ksp, "hdr_size", "cache_size", "hdr_size");
258   za_read_gauge(ksp, "metadata_size", "cache_size", "metadata_size");
259   za_read_gauge(ksp, "mfu_ghost_size", "cache_size", "mfu_ghost_size");
260   za_read_gauge(ksp, "mfu_size", "cache_size", "mfu_size");
261   za_read_gauge(ksp, "mru_ghost_size", "cache_size", "mru_ghost_size");
262   za_read_gauge(ksp, "mru_size", "cache_size", "mru_size");
263   za_read_gauge(ksp, "other_size", "cache_size", "other_size");
264   za_read_gauge(ksp, "p", "cache_size", "p");
265   za_read_gauge(ksp, "size", "cache_size", "arc");
266
267   /* The "l2_size" value has disappeared from Solaris some time in
268    * early 2013, and has only reappeared recently in Solaris 11.2.
269    * Stop trying if we ever fail to read it, so we don't spam the log.
270    */
271   static int l2_size_avail = 1;
272   if (l2_size_avail && za_read_gauge(ksp, "l2_size", "cache_size", "L2") != 0)
273     l2_size_avail = 0;
274
275   /* Operations */
276   za_read_derive(ksp, "deleted", "cache_operation", "deleted");
277 #if defined(KERNEL_FREEBSD)
278   za_read_derive(ksp, "allocated", "cache_operation", "allocated");
279 #endif
280
281   /* Issue indicators */
282   za_read_derive(ksp, "mutex_miss", "mutex_operations", "miss");
283   za_read_derive(ksp, "hash_collisions", "hash_collisions", "");
284   za_read_derive(ksp, "memory_throttle_count", "memory_throttle_count", "");
285
286   /* Evictions */
287   za_read_derive(ksp, "evict_l2_cached", "cache_eviction", "cached");
288   za_read_derive(ksp, "evict_l2_eligible", "cache_eviction", "eligible");
289   za_read_derive(ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
290
291   /* Hits / misses */
292   za_read_derive(ksp, "demand_data_hits", "cache_result", "demand_data-hit");
293   za_read_derive(ksp, "demand_metadata_hits", "cache_result",
294                  "demand_metadata-hit");
295   za_read_derive(ksp, "prefetch_data_hits", "cache_result",
296                  "prefetch_data-hit");
297   za_read_derive(ksp, "prefetch_metadata_hits", "cache_result",
298                  "prefetch_metadata-hit");
299   za_read_derive(ksp, "demand_data_misses", "cache_result", "demand_data-miss");
300   za_read_derive(ksp, "demand_metadata_misses", "cache_result",
301                  "demand_metadata-miss");
302   za_read_derive(ksp, "prefetch_data_misses", "cache_result",
303                  "prefetch_data-miss");
304   za_read_derive(ksp, "prefetch_metadata_misses", "cache_result",
305                  "prefetch_metadata-miss");
306   za_read_derive(ksp, "mfu_hits", "cache_result", "mfu-hit");
307   za_read_derive(ksp, "mfu_ghost_hits", "cache_result", "mfu_ghost-hit");
308   za_read_derive(ksp, "mru_hits", "cache_result", "mru-hit");
309   za_read_derive(ksp, "mru_ghost_hits", "cache_result", "mru_ghost-hit");
310
311   /* Ratios */
312   arc_hits = (gauge_t)get_zfs_value(ksp, "hits");
313   arc_misses = (gauge_t)get_zfs_value(ksp, "misses");
314   l2_hits = (gauge_t)get_zfs_value(ksp, "l2_hits");
315   l2_misses = (gauge_t)get_zfs_value(ksp, "l2_misses");
316
317   za_submit_ratio("arc", arc_hits, arc_misses);
318   za_submit_ratio("L2", l2_hits, l2_misses);
319
320   /* I/O */
321   l2_io[0].derive = get_zfs_value(ksp, "l2_read_bytes");
322   l2_io[1].derive = get_zfs_value(ksp, "l2_write_bytes");
323
324   za_submit("io_octets", "L2", l2_io, /* num values = */ 2);
325
326 #if defined(KERNEL_LINUX)
327   free_zfs_values(ksp);
328 #endif
329
330   return (0);
331 } /* int za_read */
332
333 static int za_init(void) /* {{{ */
334 {
335 #if defined(KERNEL_SOLARIS)
336   /* kstats chain already opened by update_kstat (using *kc), verify everything
337    * went fine. */
338   if (kc == NULL) {
339     ERROR("zfs_arc plugin: kstat chain control structure not available.");
340     return (-1);
341   }
342 #endif
343
344   return (0);
345 } /* }}} int za_init */
346
347 void module_register(void) {
348   plugin_register_init("zfs_arc", za_init);
349   plugin_register_read("zfs_arc", za_read);
350 } /* void module_register */
351
352 /* vmi: set sw=8 noexpandtab fdm=marker : */