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