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
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.
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.
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
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>
38 #if defined(KERNEL_LINUX)
39 #include "utils_llist.h"
40 #define ZOL_ARCSTATS_FILE "/proc/spl/kstat/zfs/arcstats"
42 typedef llist_t kstat_t;
44 static int put_zfs_value (kstat_t *ksp, char const *k, value_t v)
54 v_copy = malloc (sizeof (*v_copy));
62 e = llentry_create (k_copy, v_copy);
70 llist_append (ksp, e);
74 static long long get_zfs_value(kstat_t *ksp, char *key)
79 e = llist_search (ksp, key);
82 ERROR ("zfs_arc plugin: `llist_search` failed for key: '%s'.", key);
87 return ((long long) v->derive);
90 static void free_zfs_values (kstat_t *ksp)
97 for (e = llist_head (ksp); e != NULL; e = e->next)
106 #elif !defined(__FreeBSD__) // Solaris
107 extern kstat_ctl_t *kc;
109 static long long get_zfs_value(kstat_t *ksp, char *name)
112 return (get_kstat_value(ksp, name));
115 #include <sys/types.h>
116 #include <sys/sysctl.h>
118 const char zfs_arcstat[] = "kstat.zfs.misc.arcstats.";
120 #if !defined(kstat_t)
121 typedef void kstat_t;
124 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
129 size_t valuelen = sizeof(value);
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);
142 static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len)
144 value_list_t vl = VALUE_LIST_INIT;
147 vl.values_len = values_len;
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));
154 plugin_dispatch_values (&vl);
157 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
162 za_submit (type, type_instance, &vv, 1);
165 static int za_read_derive (kstat_t *ksp, const char *kstat_value,
166 const char *type, const char *type_instance)
171 tmp = get_zfs_value (ksp, (char *)kstat_value);
174 WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
178 v.derive = (derive_t) tmp;
179 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
183 static int za_read_gauge (kstat_t *ksp, const char *kstat_value,
184 const char *type, const char *type_instance)
189 tmp = get_zfs_value (ksp, (char *)kstat_value);
192 WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
196 v.gauge = (gauge_t) tmp;
197 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
201 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
205 if (!isfinite (hits) || (hits < 0.0))
207 if (!isfinite (misses) || (misses < 0.0))
210 if ((hits != 0.0) || (misses != 0.0))
211 ratio = hits / (hits + misses);
213 za_submit_gauge ("cache_ratio", type_instance, ratio);
216 static int za_read (void)
218 gauge_t arc_hits, arc_misses, l2_hits, l2_misses;
226 fh = fopen (ZOL_ARCSTATS_FILE, "r");
230 ERROR ("zfs_arc plugin: Opening \"%s\" failed: %s", ZOL_ARCSTATS_FILE,
231 sstrerror (errno, errbuf, sizeof (errbuf)));
235 ksp = llist_create ();
238 ERROR ("zfs_arc plugin: `llist_create' failed.");
243 while (fgets (buffer, sizeof (buffer), fh) != NULL)
249 status = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
253 status = parse_value (fields[2], &v, DS_TYPE_DERIVE);
257 put_zfs_value (ksp, fields[0], v);
262 #elif !defined(__FreeBSD__) // Solaris
263 get_kstat (&ksp, "zfs", 0, "arcstats");
266 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
272 za_read_gauge (ksp, "size", "cache_size", "arc");
273 za_read_gauge (ksp, "c", "cache_size", "c");
274 za_read_gauge (ksp, "c_min", "cache_size", "c_min");
275 za_read_gauge (ksp, "c_max", "cache_size", "c_max");
277 /* The "l2_size" value has disappeared from Solaris some time in
278 * early 2013, and has only reappeared recently in Solaris 11.2.
279 * Stop trying if we ever fail to read it, so we don't spam the log.
281 static int l2_size_avail = 1;
282 if (l2_size_avail && za_read_gauge (ksp, "l2_size", "cache_size", "L2") != 0)
286 za_read_derive (ksp, "deleted", "cache_operation", "deleted");
288 za_read_derive (ksp, "allocated","cache_operation", "allocated");
289 za_read_derive (ksp, "stolen", "cache_operation", "stolen");
292 /* Issue indicators */
293 za_read_derive (ksp, "mutex_miss", "mutex_operations", "miss");
294 za_read_derive (ksp, "hash_collisions", "hash_collisions", "");
297 za_read_derive (ksp, "evict_l2_cached", "cache_eviction", "cached");
298 za_read_derive (ksp, "evict_l2_eligible", "cache_eviction", "eligible");
299 za_read_derive (ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
302 za_read_derive (ksp, "demand_data_hits", "cache_result", "demand_data-hit");
303 za_read_derive (ksp, "demand_metadata_hits", "cache_result", "demand_metadata-hit");
304 za_read_derive (ksp, "prefetch_data_hits", "cache_result", "prefetch_data-hit");
305 za_read_derive (ksp, "prefetch_metadata_hits", "cache_result", "prefetch_metadata-hit");
306 za_read_derive (ksp, "demand_data_misses", "cache_result", "demand_data-miss");
307 za_read_derive (ksp, "demand_metadata_misses", "cache_result", "demand_metadata-miss");
308 za_read_derive (ksp, "prefetch_data_misses", "cache_result", "prefetch_data-miss");
309 za_read_derive (ksp, "prefetch_metadata_misses", "cache_result", "prefetch_metadata-miss");
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");
317 za_submit_ratio ("arc", arc_hits, arc_misses);
318 za_submit_ratio ("L2", l2_hits, l2_misses);
321 l2_io[0].derive = get_zfs_value(ksp, "l2_read_bytes");
322 l2_io[1].derive = get_zfs_value(ksp, "l2_write_bytes");
324 za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
326 #if defined(KERNEL_LINUX)
327 free_zfs_values (ksp);
333 static int za_init (void) /* {{{ */
335 #if !defined(__FreeBSD__) && !defined(KERNEL_LINUX) // Solaris
336 /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
339 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
345 } /* }}} int za_init */
347 void module_register (void)
349 plugin_register_init ("zfs_arc", za_init);
350 plugin_register_read ("zfs_arc", za_read);
351 } /* void module_register */
353 /* vmi: set sw=8 noexpandtab fdm=marker : */