Merge branch 'collectd-5.6' into collectd-5.7
[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, size_t 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.plugin, "zfs_arc", sizeof(vl.plugin));
140   sstrncpy(vl.type, type, sizeof(vl.type));
141   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
142
143   plugin_dispatch_values(&vl);
144 }
145
146 static void za_submit_gauge(const char *type, const char *type_instance,
147                             gauge_t value) {
148   za_submit(type, type_instance, &(value_t){.gauge = value}, 1);
149 }
150
151 static int za_read_derive(kstat_t *ksp, const char *kstat_value,
152                           const char *type, const char *type_instance) {
153   long long tmp = get_zfs_value(ksp, (char *)kstat_value);
154   if (tmp == -1LL) {
155     WARNING("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
156     return (-1);
157   }
158
159   za_submit(type, type_instance, &(value_t){.derive = (derive_t)tmp},
160             /* values_num = */ 1);
161   return (0);
162 }
163
164 static int za_read_gauge(kstat_t *ksp, const char *kstat_value,
165                          const char *type, const char *type_instance) {
166   long long tmp = get_zfs_value(ksp, (char *)kstat_value);
167   if (tmp == -1LL) {
168     WARNING("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
169     return (-1);
170   }
171
172   za_submit(type, type_instance, &(value_t){.gauge = (gauge_t)tmp},
173             /* values_num = */ 1);
174   return (0);
175 }
176
177 static void za_submit_ratio(const char *type_instance, gauge_t hits,
178                             gauge_t misses) {
179   gauge_t ratio = NAN;
180
181   if (!isfinite(hits) || (hits < 0.0))
182     hits = 0.0;
183   if (!isfinite(misses) || (misses < 0.0))
184     misses = 0.0;
185
186   if ((hits != 0.0) || (misses != 0.0))
187     ratio = hits / (hits + misses);
188
189   za_submit_gauge("cache_ratio", type_instance, ratio);
190 }
191
192 static int za_read(void) {
193   gauge_t arc_hits, arc_misses, l2_hits, l2_misses;
194   kstat_t *ksp = NULL;
195
196 #if defined(KERNEL_LINUX)
197   FILE *fh;
198   char buffer[1024];
199
200   fh = fopen(ZOL_ARCSTATS_FILE, "r");
201   if (fh == NULL) {
202     char errbuf[1024];
203     ERROR("zfs_arc plugin: Opening \"%s\" failed: %s", ZOL_ARCSTATS_FILE,
204           sstrerror(errno, errbuf, sizeof(errbuf)));
205     return (-1);
206   }
207
208   ksp = llist_create();
209   if (ksp == NULL) {
210     ERROR("zfs_arc plugin: `llist_create' failed.");
211     fclose(fh);
212     return (-1);
213   }
214
215   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
216     char *fields[3];
217     value_t v;
218     int status;
219
220     status = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
221     if (status != 3)
222       continue;
223
224     status = parse_value(fields[2], &v, DS_TYPE_DERIVE);
225     if (status != 0)
226       continue;
227
228     put_zfs_value(ksp, fields[0], v);
229   }
230
231   fclose(fh);
232
233 #elif defined(KERNEL_SOLARIS)
234   get_kstat(&ksp, "zfs", 0, "arcstats");
235   if (ksp == NULL) {
236     ERROR("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
237     return (-1);
238   }
239 #endif
240
241   /* Sizes */
242   za_read_gauge(ksp, "anon_size", "cache_size", "anon_size");
243   za_read_gauge(ksp, "c", "cache_size", "c");
244   za_read_gauge(ksp, "c_max", "cache_size", "c_max");
245   za_read_gauge(ksp, "c_min", "cache_size", "c_min");
246   za_read_gauge(ksp, "hdr_size", "cache_size", "hdr_size");
247   za_read_gauge(ksp, "metadata_size", "cache_size", "metadata_size");
248   za_read_gauge(ksp, "mfu_ghost_size", "cache_size", "mfu_ghost_size");
249   za_read_gauge(ksp, "mfu_size", "cache_size", "mfu_size");
250   za_read_gauge(ksp, "mru_ghost_size", "cache_size", "mru_ghost_size");
251   za_read_gauge(ksp, "mru_size", "cache_size", "mru_size");
252   za_read_gauge(ksp, "other_size", "cache_size", "other_size");
253   za_read_gauge(ksp, "p", "cache_size", "p");
254   za_read_gauge(ksp, "size", "cache_size", "arc");
255
256   /* The "l2_size" value has disappeared from Solaris some time in
257    * early 2013, and has only reappeared recently in Solaris 11.2.
258    * Stop trying if we ever fail to read it, so we don't spam the log.
259    */
260   static int l2_size_avail = 1;
261   if (l2_size_avail && za_read_gauge(ksp, "l2_size", "cache_size", "L2") != 0)
262     l2_size_avail = 0;
263
264   /* Operations */
265   za_read_derive(ksp, "deleted", "cache_operation", "deleted");
266 #if defined(KERNEL_FREEBSD)
267   za_read_derive(ksp, "allocated", "cache_operation", "allocated");
268 #endif
269
270   /* Issue indicators */
271   za_read_derive(ksp, "mutex_miss", "mutex_operations", "miss");
272   za_read_derive(ksp, "hash_collisions", "hash_collisions", "");
273   za_read_derive(ksp, "memory_throttle_count", "memory_throttle_count", "");
274
275   /* Evictions */
276   za_read_derive(ksp, "evict_l2_cached", "cache_eviction", "cached");
277   za_read_derive(ksp, "evict_l2_eligible", "cache_eviction", "eligible");
278   za_read_derive(ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
279
280   /* Hits / misses */
281   za_read_derive(ksp, "demand_data_hits", "cache_result", "demand_data-hit");
282   za_read_derive(ksp, "demand_metadata_hits", "cache_result",
283                  "demand_metadata-hit");
284   za_read_derive(ksp, "prefetch_data_hits", "cache_result",
285                  "prefetch_data-hit");
286   za_read_derive(ksp, "prefetch_metadata_hits", "cache_result",
287                  "prefetch_metadata-hit");
288   za_read_derive(ksp, "demand_data_misses", "cache_result", "demand_data-miss");
289   za_read_derive(ksp, "demand_metadata_misses", "cache_result",
290                  "demand_metadata-miss");
291   za_read_derive(ksp, "prefetch_data_misses", "cache_result",
292                  "prefetch_data-miss");
293   za_read_derive(ksp, "prefetch_metadata_misses", "cache_result",
294                  "prefetch_metadata-miss");
295   za_read_derive(ksp, "mfu_hits", "cache_result", "mfu-hit");
296   za_read_derive(ksp, "mfu_ghost_hits", "cache_result", "mfu_ghost-hit");
297   za_read_derive(ksp, "mru_hits", "cache_result", "mru-hit");
298   za_read_derive(ksp, "mru_ghost_hits", "cache_result", "mru_ghost-hit");
299
300   /* Ratios */
301   arc_hits = (gauge_t)get_zfs_value(ksp, "hits");
302   arc_misses = (gauge_t)get_zfs_value(ksp, "misses");
303   l2_hits = (gauge_t)get_zfs_value(ksp, "l2_hits");
304   l2_misses = (gauge_t)get_zfs_value(ksp, "l2_misses");
305
306   za_submit_ratio("arc", arc_hits, arc_misses);
307   za_submit_ratio("L2", l2_hits, l2_misses);
308
309   /* I/O */
310   value_t l2_io[] = {
311       {.derive = (derive_t)get_zfs_value(ksp, "l2_read_bytes")},
312       {.derive = (derive_t)get_zfs_value(ksp, "l2_write_bytes")},
313   };
314   za_submit("io_octets", "L2", l2_io, STATIC_ARRAY_SIZE(l2_io));
315
316 #if defined(KERNEL_LINUX)
317   free_zfs_values(ksp);
318 #endif
319
320   return (0);
321 } /* int za_read */
322
323 static int za_init(void) /* {{{ */
324 {
325 #if defined(KERNEL_SOLARIS)
326   /* kstats chain already opened by update_kstat (using *kc), verify everything
327    * went fine. */
328   if (kc == NULL) {
329     ERROR("zfs_arc plugin: kstat chain control structure not available.");
330     return (-1);
331   }
332 #endif
333
334   return (0);
335 } /* }}} int za_init */
336
337 void module_register(void) {
338   plugin_register_init("zfs_arc", za_init);
339   plugin_register_read("zfs_arc", za_read);
340 } /* void module_register */
341
342 /* vmi: set sw=8 noexpandtab fdm=marker : */