Merge branch 'collectd-5.7' into collectd-5.8
[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 static value_to_rate_state_t arc_hits_state;
39 static value_to_rate_state_t arc_misses_state;
40 static value_to_rate_state_t l2_hits_state;
41 static value_to_rate_state_t l2_misses_state;
42
43 #if defined(KERNEL_LINUX)
44 #include "utils_llist.h"
45 #define ZOL_ARCSTATS_FILE "/proc/spl/kstat/zfs/arcstats"
46
47 typedef llist_t kstat_t;
48
49 static int put_zfs_value(kstat_t *ksp, char const *k, value_t v) {
50   llentry_t *e;
51   char *k_copy;
52   value_t *v_copy;
53
54   k_copy = strdup(k);
55   if (k_copy == NULL)
56     return ENOMEM;
57
58   v_copy = malloc(sizeof(*v_copy));
59   if (v_copy == NULL) {
60     sfree(k_copy);
61     return ENOMEM;
62   }
63   *v_copy = v;
64
65   e = llentry_create(k_copy, v_copy);
66   if (e == NULL) {
67     sfree(v_copy);
68     sfree(k_copy);
69     return ENOMEM;
70   }
71
72   llist_append(ksp, e);
73   return 0;
74 }
75
76 static long long get_zfs_value(kstat_t *ksp, const char *key) {
77   llentry_t *e;
78   value_t *v;
79
80   e = llist_search(ksp, key);
81   if (e == NULL) {
82     return -1;
83   }
84
85   v = e->value;
86   return (long long)v->derive;
87 }
88
89 static void free_zfs_values(kstat_t *ksp) {
90   if (ksp == NULL)
91     return;
92
93   for (llentry_t *e = llist_head(ksp); e != NULL; e = e->next) {
94     sfree(e->key);
95     sfree(e->value);
96   }
97
98   llist_destroy(ksp);
99 }
100
101 #elif defined(KERNEL_SOLARIS)
102
103 #if HAVE_KSTAT_H
104 #include <kstat.h>
105 #endif
106
107 extern kstat_ctl_t *kc;
108
109 static long long get_zfs_value(kstat_t *ksp, char *name) {
110
111   return get_kstat_value(ksp, name);
112 }
113 #elif defined(KERNEL_FREEBSD)
114 #include <sys/sysctl.h>
115 #include <sys/types.h>
116
117 const char zfs_arcstat[] = "kstat.zfs.misc.arcstats.";
118
119 #if !defined(kstat_t)
120 typedef void kstat_t;
121 #endif
122
123 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
124                                char const *name) {
125   char buffer[256];
126   long long value;
127   size_t valuelen = sizeof(value);
128   int rv;
129
130   snprintf(buffer, sizeof(buffer), "%s%s", zfs_arcstat, name);
131   rv = sysctlbyname(buffer, (void *)&value, &valuelen,
132                     /* new value = */ NULL, /* new length = */ (size_t)0);
133   if (rv == 0)
134     return value;
135
136   return -1;
137 }
138 #endif
139
140 static void za_submit(const char *type, const char *type_instance,
141                       value_t *values, size_t values_len) {
142   value_list_t vl = VALUE_LIST_INIT;
143
144   vl.values = values;
145   vl.values_len = values_len;
146
147   sstrncpy(vl.plugin, "zfs_arc", sizeof(vl.plugin));
148   sstrncpy(vl.type, type, sizeof(vl.type));
149   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
150
151   plugin_dispatch_values(&vl);
152 }
153
154 static void za_submit_gauge(const char *type, const char *type_instance,
155                             gauge_t value) {
156   za_submit(type, type_instance, &(value_t){.gauge = value}, 1);
157 }
158
159 static int za_read_derive(kstat_t *ksp, const char *kstat_value,
160                           const char *type, const char *type_instance) {
161   long long tmp = get_zfs_value(ksp, (char *)kstat_value);
162   if (tmp == -1LL) {
163     DEBUG("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
164     return -1;
165   }
166
167   za_submit(type, type_instance, &(value_t){.derive = (derive_t)tmp},
168             /* values_num = */ 1);
169   return 0;
170 }
171
172 static int za_read_gauge(kstat_t *ksp, const char *kstat_value,
173                          const char *type, const char *type_instance) {
174   long long tmp = get_zfs_value(ksp, (char *)kstat_value);
175   if (tmp == -1LL) {
176     DEBUG("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
177     return -1;
178   }
179
180   za_submit(type, type_instance, &(value_t){.gauge = (gauge_t)tmp},
181             /* values_num = */ 1);
182   return 0;
183 }
184
185 static void za_submit_ratio(const char *type_instance, gauge_t hits,
186                             gauge_t misses) {
187   gauge_t ratio = NAN;
188
189   if (!isfinite(hits) || (hits < 0.0))
190     hits = 0.0;
191   if (!isfinite(misses) || (misses < 0.0))
192     misses = 0.0;
193
194   if ((hits != 0.0) || (misses != 0.0))
195     ratio = hits / (hits + misses);
196
197   za_submit_gauge("cache_ratio", type_instance, ratio);
198 }
199
200 static int za_read(void) {
201   gauge_t arc_hits, arc_misses, l2_hits, l2_misses;
202   kstat_t *ksp = NULL;
203
204 #if defined(KERNEL_LINUX)
205   FILE *fh;
206   char buffer[1024];
207
208   fh = fopen(ZOL_ARCSTATS_FILE, "r");
209   if (fh == NULL) {
210     char errbuf[1024];
211     ERROR("zfs_arc plugin: Opening \"%s\" failed: %s", ZOL_ARCSTATS_FILE,
212           sstrerror(errno, errbuf, sizeof(errbuf)));
213     return -1;
214   }
215
216   /* Ignore the first two lines because they contain information about the rest
217    * of the file.
218    * See kstat_seq_show_headers module/spl/spl-kstat.c of the spl kernel module.
219    */
220   if ((fgets(buffer, sizeof(buffer), fh) == NULL) ||
221       (fgets(buffer, sizeof(buffer), fh) == NULL)) {
222     ERROR("zfs_arc plugin: \"%s\" does not contain at least two lines.",
223           ZOL_ARCSTATS_FILE);
224     fclose(fh);
225     return -1;
226   }
227
228   ksp = llist_create();
229   if (ksp == NULL) {
230     ERROR("zfs_arc plugin: `llist_create' failed.");
231     fclose(fh);
232     return -1;
233   }
234
235   // Ignore the first two lines because they contain information about
236   // the rest of the file.
237   // See kstat_seq_show_headers module/spl/spl-kstat.c of the spl kernel
238   // module.
239   if (fgets(buffer, sizeof(buffer), fh) == NULL) {
240     ERROR("zfs_arc plugin: \"%s\" does not contain a single line.",
241           ZOL_ARCSTATS_FILE);
242     fclose(fh);
243     return (-1);
244   }
245   if (fgets(buffer, sizeof(buffer), fh) == NULL) {
246     ERROR("zfs_arc plugin: \"%s\" does not contain at least two lines.",
247           ZOL_ARCSTATS_FILE);
248     fclose(fh);
249     return (-1);
250   }
251
252   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
253     char *fields[3];
254     value_t v;
255     int status;
256
257     status = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
258     if (status != 3)
259       continue;
260
261     status = parse_value(fields[2], &v, DS_TYPE_DERIVE);
262     if (status != 0)
263       continue;
264
265     put_zfs_value(ksp, fields[0], v);
266   }
267
268   fclose(fh);
269
270 #elif defined(KERNEL_SOLARIS)
271   get_kstat(&ksp, "zfs", 0, "arcstats");
272   if (ksp == NULL) {
273     ERROR("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
274     return -1;
275   }
276 #endif
277
278   /* Sizes */
279   za_read_gauge(ksp, "anon_size", "cache_size", "anon_size");
280   za_read_gauge(ksp, "c", "cache_size", "c");
281   za_read_gauge(ksp, "c_max", "cache_size", "c_max");
282   za_read_gauge(ksp, "c_min", "cache_size", "c_min");
283   za_read_gauge(ksp, "hdr_size", "cache_size", "hdr_size");
284   za_read_gauge(ksp, "metadata_size", "cache_size", "metadata_size");
285   za_read_gauge(ksp, "mfu_ghost_size", "cache_size", "mfu_ghost_size");
286   za_read_gauge(ksp, "mfu_size", "cache_size", "mfu_size");
287   za_read_gauge(ksp, "mru_ghost_size", "cache_size", "mru_ghost_size");
288   za_read_gauge(ksp, "mru_size", "cache_size", "mru_size");
289   za_read_gauge(ksp, "other_size", "cache_size", "other_size");
290   za_read_gauge(ksp, "p", "cache_size", "p");
291   za_read_gauge(ksp, "size", "cache_size", "arc");
292
293   /* The "l2_size" value has disappeared from Solaris some time in
294    * early 2013, and has only reappeared recently in Solaris 11.2.
295    * Stop trying if we ever fail to read it, so we don't spam the log.
296    */
297   static int l2_size_avail = 1;
298   if (l2_size_avail && za_read_gauge(ksp, "l2_size", "cache_size", "L2") != 0)
299     l2_size_avail = 0;
300
301   /* Operations */
302   za_read_derive(ksp, "deleted", "cache_operation", "deleted");
303 #if defined(KERNEL_FREEBSD)
304   za_read_derive(ksp, "allocated", "cache_operation", "allocated");
305 #endif
306
307   /* Issue indicators */
308   za_read_derive(ksp, "mutex_miss", "mutex_operations", "miss");
309   za_read_derive(ksp, "hash_collisions", "hash_collisions", "");
310   za_read_derive(ksp, "memory_throttle_count", "memory_throttle_count", "");
311
312   /* Evictions */
313   za_read_derive(ksp, "evict_l2_cached", "cache_eviction", "cached");
314   za_read_derive(ksp, "evict_l2_eligible", "cache_eviction", "eligible");
315   za_read_derive(ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
316
317   /* Hits / misses */
318   za_read_derive(ksp, "demand_data_hits", "cache_result", "demand_data-hit");
319   za_read_derive(ksp, "demand_metadata_hits", "cache_result",
320                  "demand_metadata-hit");
321   za_read_derive(ksp, "prefetch_data_hits", "cache_result",
322                  "prefetch_data-hit");
323   za_read_derive(ksp, "prefetch_metadata_hits", "cache_result",
324                  "prefetch_metadata-hit");
325   za_read_derive(ksp, "demand_data_misses", "cache_result", "demand_data-miss");
326   za_read_derive(ksp, "demand_metadata_misses", "cache_result",
327                  "demand_metadata-miss");
328   za_read_derive(ksp, "prefetch_data_misses", "cache_result",
329                  "prefetch_data-miss");
330   za_read_derive(ksp, "prefetch_metadata_misses", "cache_result",
331                  "prefetch_metadata-miss");
332   za_read_derive(ksp, "mfu_hits", "cache_result", "mfu-hit");
333   za_read_derive(ksp, "mfu_ghost_hits", "cache_result", "mfu_ghost-hit");
334   za_read_derive(ksp, "mru_hits", "cache_result", "mru-hit");
335   za_read_derive(ksp, "mru_ghost_hits", "cache_result", "mru_ghost-hit");
336
337   cdtime_t now = cdtime();
338
339   /* Ratios */
340   if ((value_to_rate(&arc_hits, (value_t){.derive = get_zfs_value(ksp, "hits")},
341                      DS_TYPE_DERIVE, now, &arc_hits_state) == 0) &&
342       (value_to_rate(&arc_misses,
343                      (value_t){.derive = get_zfs_value(ksp, "misses")},
344                      DS_TYPE_DERIVE, now, &arc_misses_state) == 0)) {
345     za_submit_ratio("arc", arc_hits, arc_misses);
346   }
347
348   if ((value_to_rate(&l2_hits,
349                      (value_t){.derive = get_zfs_value(ksp, "l2_hits")},
350                      DS_TYPE_DERIVE, now, &l2_hits_state) == 0) &&
351       (value_to_rate(&l2_misses,
352                      (value_t){.derive = get_zfs_value(ksp, "l2_misses")},
353                      DS_TYPE_DERIVE, now, &l2_misses_state) == 0)) {
354     za_submit_ratio("L2", l2_hits, l2_misses);
355   }
356
357   /* I/O */
358   value_t l2_io[] = {
359       {.derive = (derive_t)get_zfs_value(ksp, "l2_read_bytes")},
360       {.derive = (derive_t)get_zfs_value(ksp, "l2_write_bytes")},
361   };
362   za_submit("io_octets", "L2", l2_io, STATIC_ARRAY_SIZE(l2_io));
363
364 #if defined(KERNEL_LINUX)
365   free_zfs_values(ksp);
366 #endif
367
368   return 0;
369 } /* int za_read */
370
371 static int za_init(void) /* {{{ */
372 {
373 #if defined(KERNEL_SOLARIS)
374   /* kstats chain already opened by update_kstat (using *kc), verify everything
375    * went fine. */
376   if (kc == NULL) {
377     ERROR("zfs_arc plugin: kstat chain control structure not available.");
378     return -1;
379   }
380 #endif
381
382   return 0;
383 } /* }}} int za_init */
384
385 void module_register(void) {
386   plugin_register_init("zfs_arc", za_init);
387   plugin_register_read("zfs_arc", za_read);
388 } /* void module_register */
389
390 /* vmi: set sw=8 noexpandtab fdm=marker : */