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