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"
40 typedef llist_t kstat_t;
42 static long long get_zfs_value(kstat_t *zfs_stats __attribute__((unused)),
47 e = llist_search (zfs_stats, name);
50 ERROR ("zfs_arc plugin: `llist_search` failed for key: '%s'.", name);
54 return ((long long int)e->value);
57 #elif !defined(__FreeBSD__) // Solaris
58 extern kstat_ctl_t *kc;
60 static long long get_zfs_value(kstat_t *ksp, char *name)
63 return (get_kstat_value(ksp, name));
66 #include <sys/types.h>
67 #include <sys/sysctl.h>
69 const char zfs_arcstat[] = "kstat.zfs.misc.arcstats.";
75 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
80 size_t valuelen = sizeof(value);
83 ssnprintf (buffer, sizeof (buffer), "%s%s", zfs_arcstat, name);
84 rv = sysctlbyname (buffer, (void *) &value, &valuelen,
85 /* new value = */ NULL, /* new length = */ (size_t) 0);
93 static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len)
95 value_list_t vl = VALUE_LIST_INIT;
98 vl.values_len = values_len;
100 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
101 sstrncpy (vl.plugin, "zfs_arc", sizeof (vl.plugin));
102 sstrncpy (vl.type, type, sizeof (vl.type));
103 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
105 plugin_dispatch_values (&vl);
108 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
113 za_submit (type, type_instance, &vv, 1);
116 static int za_read_derive (kstat_t *ksp, const char *kstat_value,
117 const char *type, const char *type_instance)
122 tmp = get_zfs_value (ksp, (char *)kstat_value);
125 WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
129 v.derive = (derive_t) tmp;
130 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
134 static int za_read_gauge (kstat_t *ksp, const char *kstat_value,
135 const char *type, const char *type_instance)
140 tmp = get_zfs_value (ksp, (char *)kstat_value);
143 WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
147 v.gauge = (gauge_t) tmp;
148 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
152 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
156 if (!isfinite (hits) || (hits < 0.0))
158 if (!isfinite (misses) || (misses < 0.0))
161 if ((hits != 0.0) || (misses != 0.0))
162 ratio = hits / (hits + misses);
164 za_submit_gauge ("cache_ratio", type_instance, ratio);
167 static int za_read (void)
169 gauge_t arc_hits, arc_misses, l2_hits, l2_misses;
174 long long int *llvalues = NULL;
175 char FileContents[1024 * 10];
180 ksp = llist_create ();
183 ERROR ("zfs_arc plugin: `llist_create' failed.");
187 len = read_file_contents (ZOL_ARCSTATS_FILE, FileContents, sizeof(FileContents));
192 char *pnl = FileContents;
195 FileContents[len] = '\0';
199 pnl = strchr(pnl, '\n');
201 if (pnl && (*pnl != '\0'))
208 llvalues = malloc(sizeof(long long int) * i);
214 pnnl = strchr(pnl, '\n');
218 numfields = strsplit (pnl, fields, 4);
221 llvalues[i] = atoll (fields[2]);
223 e = llentry_create (fields[0], &llvalues[i]);
226 ERROR ("zfs_arc plugin: `llentry_create' failed.");
230 llist_append (ksp, e);
240 #elif !defined(__FreeBSD__) // Solaris
241 get_kstat (&ksp, "zfs", 0, "arcstats");
244 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
250 za_read_gauge (ksp, "size", "cache_size", "arc");
251 za_read_gauge (ksp, "l2_size", "cache_size", "L2");
254 za_read_derive (ksp, "deleted", "cache_operation", "deleted");
256 za_read_derive (ksp, "allocated","cache_operation", "allocated");
257 za_read_derive (ksp, "stolen", "cache_operation", "stolen");
260 /* Issue indicators */
261 za_read_derive (ksp, "mutex_miss", "mutex_operations", "miss");
262 za_read_derive (ksp, "hash_collisions", "hash_collisions", "");
265 za_read_derive (ksp, "evict_l2_cached", "cache_eviction", "cached");
266 za_read_derive (ksp, "evict_l2_eligible", "cache_eviction", "eligible");
267 za_read_derive (ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
270 za_read_derive (ksp, "demand_data_hits", "cache_result", "demand_data-hit");
271 za_read_derive (ksp, "demand_metadata_hits", "cache_result", "demand_metadata-hit");
272 za_read_derive (ksp, "prefetch_data_hits", "cache_result", "prefetch_data-hit");
273 za_read_derive (ksp, "prefetch_metadata_hits", "cache_result", "prefetch_metadata-hit");
274 za_read_derive (ksp, "demand_data_misses", "cache_result", "demand_data-miss");
275 za_read_derive (ksp, "demand_metadata_misses", "cache_result", "demand_metadata-miss");
276 za_read_derive (ksp, "prefetch_data_misses", "cache_result", "prefetch_data-miss");
277 za_read_derive (ksp, "prefetch_metadata_misses", "cache_result", "prefetch_metadata-miss");
280 arc_hits = (gauge_t) get_zfs_value(ksp, "hits");
281 arc_misses = (gauge_t) get_zfs_value(ksp, "misses");
282 l2_hits = (gauge_t) get_zfs_value(ksp, "l2_hits");
283 l2_misses = (gauge_t) get_zfs_value(ksp, "l2_misses");
285 za_submit_ratio ("arc", arc_hits, arc_misses);
286 za_submit_ratio ("L2", l2_hits, l2_misses);
289 l2_io[0].derive = get_zfs_value(ksp, "l2_read_bytes");
290 l2_io[1].derive = get_zfs_value(ksp, "l2_write_bytes");
292 za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
294 #if defined(KERNEL_LINUX)
295 if (llvalues != NULL)
309 static int za_init (void) /* {{{ */
311 #if !defined(__FreeBSD__) && !defined(KERNEL_LINUX) // Solaris
312 /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
315 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
321 } /* }}} int za_init */
323 void module_register (void)
325 plugin_register_init ("zfs_arc", za_init);
326 plugin_register_read ("zfs_arc", za_read);
327 } /* void module_register */
329 /* vmi: set sw=8 noexpandtab fdm=marker : */