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
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Anthony Dewhurst <dewhurst at gmail>
23 * Aurelien Rougemont <beorn at gandi.net>
24 * Xin Li <delphij at FreeBSD.org>
25 * Marc Fournier <marc.fournier at camptocamp.com>
36 #if defined(KERNEL_LINUX)
37 #include "utils_llist.h"
38 #define ZOL_ARCSTATS_FILE "/proc/spl/kstat/zfs/arcstats"
42 static llist_t *zfs_stats = NULL;
45 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
50 e = llist_search (zfs_stats, name);
53 ERROR ("zfs_arc plugin: `llist_search` failed for key: '%s'.", name);
57 return ((long long int)e->value);
60 #elif !defined(__FreeBSD__) // Solaris
61 extern kstat_ctl_t *kc;
63 static long long get_zfs_value(kstat_t *ksp, char *name)
66 return (get_kstat_value(ksp, name));
69 #include <sys/types.h>
70 #include <sys/sysctl.h>
72 const char zfs_arcstat[] = "kstat.zfs.misc.arcstats.";
78 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
83 size_t valuelen = sizeof(value);
86 ssnprintf (buffer, sizeof (buffer), "%s%s", zfs_arcstat, name);
87 rv = sysctlbyname (buffer, (void *) &value, &valuelen,
88 /* new value = */ NULL, /* new length = */ (size_t) 0);
96 static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len)
98 value_list_t vl = VALUE_LIST_INIT;
101 vl.values_len = values_len;
103 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
104 sstrncpy (vl.plugin, "zfs_arc", sizeof (vl.plugin));
105 sstrncpy (vl.type, type, sizeof (vl.type));
106 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
108 plugin_dispatch_values (&vl);
111 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
116 za_submit (type, type_instance, &vv, 1);
119 static int za_read_derive (kstat_t *ksp, const char *kstat_value,
120 const char *type, const char *type_instance)
125 tmp = get_zfs_value (ksp, (char *)kstat_value);
128 WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
132 v.derive = (derive_t) tmp;
133 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
137 static int za_read_gauge (kstat_t *ksp, const char *kstat_value,
138 const char *type, const char *type_instance)
143 tmp = get_zfs_value (ksp, (char *)kstat_value);
146 WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
150 v.gauge = (gauge_t) tmp;
151 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
155 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
159 if (!isfinite (hits) || (hits < 0.0))
161 if (!isfinite (misses) || (misses < 0.0))
164 if ((hits != 0.0) || (misses != 0.0))
165 ratio = hits / (hits + misses);
167 za_submit_gauge ("cache_ratio", type_instance, ratio);
170 static int za_read (void)
172 gauge_t arc_hits, arc_misses, l2_hits, l2_misses;
183 if ((fh = fopen (ZOL_ARCSTATS_FILE, "r")) == NULL)
186 ERROR ("zfs_arc plugin: `fopen (%s)' failed: %s",
188 sstrerror (errno, errbuf, sizeof (errbuf)));
192 zfs_stats = llist_create ();
193 if (zfs_stats == NULL)
195 ERROR ("zfs_arc plugin: `llist_create' failed.");
200 while (fgets (buf, 1024, fh) != NULL)
202 numfields = strsplit (buf, fields, 4);
207 long long int llvalue;
209 llkey = strdup (fields[0]);
212 ERROR ("zfs_arc plugin: `strdup' failed: %s",
213 sstrerror (errno, errbuf, sizeof (errbuf)));
217 llvalue = atoll (fields[2]);
220 e = llentry_create (llkey, (void *)llvalue);
223 ERROR ("zfs_arc plugin: `llentry_create' failed.");
230 llist_append (zfs_stats, e);
236 WARNING ("zfs_arc: `fclose' failed: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
239 #elif !defined(__FreeBSD__) // Solaris
240 get_kstat (&ksp, "zfs", 0, "arcstats");
243 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
249 za_read_gauge (ksp, "size", "cache_size", "arc");
250 za_read_gauge (ksp, "l2_size", "cache_size", "L2");
253 za_read_derive (ksp, "deleted", "cache_operation", "deleted");
255 za_read_derive (ksp, "allocated","cache_operation", "allocated");
256 za_read_derive (ksp, "stolen", "cache_operation", "stolen");
259 /* Issue indicators */
260 za_read_derive (ksp, "mutex_miss", "mutex_operations", "miss");
261 za_read_derive (ksp, "hash_collisions", "hash_collisions", "");
264 za_read_derive (ksp, "evict_l2_cached", "cache_eviction", "cached");
265 za_read_derive (ksp, "evict_l2_eligible", "cache_eviction", "eligible");
266 za_read_derive (ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
269 za_read_derive (ksp, "demand_data_hits", "cache_result", "demand_data-hit");
270 za_read_derive (ksp, "demand_metadata_hits", "cache_result", "demand_metadata-hit");
271 za_read_derive (ksp, "prefetch_data_hits", "cache_result", "prefetch_data-hit");
272 za_read_derive (ksp, "prefetch_metadata_hits", "cache_result", "prefetch_metadata-hit");
273 za_read_derive (ksp, "demand_data_misses", "cache_result", "demand_data-miss");
274 za_read_derive (ksp, "demand_metadata_misses", "cache_result", "demand_metadata-miss");
275 za_read_derive (ksp, "prefetch_data_misses", "cache_result", "prefetch_data-miss");
276 za_read_derive (ksp, "prefetch_metadata_misses", "cache_result", "prefetch_metadata-miss");
279 arc_hits = (gauge_t) get_zfs_value(ksp, "hits");
280 arc_misses = (gauge_t) get_zfs_value(ksp, "misses");
281 l2_hits = (gauge_t) get_zfs_value(ksp, "l2_hits");
282 l2_misses = (gauge_t) get_zfs_value(ksp, "l2_misses");
284 za_submit_ratio ("arc", arc_hits, arc_misses);
285 za_submit_ratio ("L2", l2_hits, l2_misses);
288 l2_io[0].derive = get_zfs_value(ksp, "l2_read_bytes");
289 l2_io[1].derive = get_zfs_value(ksp, "l2_write_bytes");
291 za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
293 #if defined(KERNEL_LINUX)
294 if (zfs_stats != NULL)
296 llist_destroy (zfs_stats);
303 static int za_init (void) /* {{{ */
305 #if !defined(__FreeBSD__) && !defined(KERNEL_LINUX) // Solaris
306 /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
309 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
315 } /* }}} int za_init */
317 void module_register (void)
319 plugin_register_init ("zfs_arc", za_init);
320 plugin_register_read ("zfs_arc", za_read);
321 } /* void module_register */
323 /* vmi: set sw=8 noexpandtab fdm=marker : */