1 /**
2 * collectd - src/zfs_arc.c
3 * Copyright (C) 2009 Anthony Dewhurst
4 * Copyright (C) 2012 Aurelien Rougemont
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Anthony Dewhurst <dewhurst at gmail>
21 * Aurelien Rougemont <beorn at gandi.net>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 /*
29 * Global variables
30 */
31 static kstat_t *ksp;
32 extern kstat_ctl_t *kc;
34 static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len)
35 {
36 value_list_t vl = VALUE_LIST_INIT;
38 vl.values = values;
39 vl.values_len = values_len;
41 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
42 sstrncpy (vl.plugin, "zfs_arc", sizeof (vl.plugin));
43 sstrncpy (vl.type, type, sizeof (vl.type));
44 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
46 plugin_dispatch_values (&vl);
47 }
49 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
50 {
51 value_t vv;
53 vv.gauge = value;
54 za_submit (type, type_instance, &vv, 1);
55 }
57 static int za_read_derive (kstat_t *ksp, const char *kstat_value,
58 const char *type, const char *type_instance)
59 {
60 long long tmp;
61 value_t v;
63 tmp = get_kstat_value (ksp, kstat_value);
64 if (tmp == -1LL)
65 {
66 ERROR ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
67 return (-1);
68 }
70 v.derive = (derive_t) tmp;
71 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
72 }
74 static int za_read_gauge (kstat_t *ksp, const char *kstat_value,
75 const char *type, const char *type_instance)
76 {
77 long long tmp;
78 value_t v;
80 tmp = get_kstat_value (ksp, kstat_value);
81 if (tmp == -1LL)
82 {
83 ERROR ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
84 return (-1);
85 }
87 v.gauge = (gauge_t) tmp;
88 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
89 }
91 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
92 {
93 gauge_t ratio = NAN;
95 if (!isfinite (hits) || (hits < 0.0))
96 hits = 0.0;
97 if (!isfinite (misses) || (misses < 0.0))
98 misses = 0.0;
100 if ((hits != 0.0) || (misses != 0.0))
101 ratio = hits / (hits + misses);
103 za_submit_gauge ("cache_ratio", type_instance, ratio);
104 }
106 static int za_read (void)
107 {
108 gauge_t arc_hits, arc_misses, l2_hits, l2_misses;
109 value_t l2_io[2];
111 get_kstat (&ksp, "zfs", 0, "arcstats");
112 if (ksp == NULL)
113 {
114 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
115 return (-1);
116 }
118 /* Sizes */
119 za_read_gauge (ksp, "size", "cache_size", "arc");
120 za_read_gauge (ksp, "l2_size", "cache_size", "L2");
122 /* Operations */
123 za_read_derive (ksp, "allocated","cache_operation", "allocated");
124 za_read_derive (ksp, "deleted", "cache_operation", "deleted");
125 za_read_derive (ksp, "stolen", "cache_operation", "stolen");
127 /* Issue indicators */
128 za_read_derive (ksp, "mutex_miss", "mutex_operation", "miss");
129 za_read_derive (ksp, "hash_collisions", "hash_collisions", "");
131 /* Evictions */
132 za_read_derive (ksp, "evict_l2_cached", "cache_eviction", "cached");
133 za_read_derive (ksp, "evict_l2_eligible", "cache_eviction", "eligible");
134 za_read_derive (ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
136 /* Hits / misses */
137 za_read_derive (ksp, "demand_data_hits", "cache_result", "demand_data-hit");
138 za_read_derive (ksp, "demand_metadata_hits", "cache_result", "demand_metadata-hit");
139 za_read_derive (ksp, "prefetch_data_hits", "cache_result", "prefetch_data-hit");
140 za_read_derive (ksp, "prefetch_metadata_hits", "cache_result", "prefetch_metadata-hit");
141 za_read_derive (ksp, "demand_data_misses", "cache_result", "demand_data-miss");
142 za_read_derive (ksp, "demand_metadata_misses", "cache_result", "demand_metadata-miss");
143 za_read_derive (ksp, "prefetch_data_misses", "cache_result", "prefetch_data-miss");
144 za_read_derive (ksp, "prefetch_metadata_misses", "cache_result", "prefetch_metadata-miss");
146 /* Ratios */
147 arc_hits = (gauge_t) get_kstat_value(ksp, "hits");
148 arc_misses = (gauge_t) get_kstat_value(ksp, "misses");
149 l2_hits = (gauge_t) get_kstat_value(ksp, "l2_hits");
150 l2_misses = (gauge_t) get_kstat_value(ksp, "l2_misses");
152 za_submit_ratio ("arc", arc_hits, arc_misses);
153 za_submit_ratio ("L2", l2_hits, l2_misses);
155 /* I/O */
156 l2_io[0].derive = get_kstat_value(ksp, "l2_read_bytes");
157 l2_io[1].derive = get_kstat_value(ksp, "l2_write_bytes");
159 za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
161 return (0);
162 } /* int za_read */
164 static int za_init (void) /* {{{ */
165 {
166 ksp = NULL;
168 /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
169 if (kc == NULL)
170 {
171 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
172 return (-1);
173 }
175 return (0);
176 } /* }}} int za_init */
178 void module_register (void)
179 {
180 plugin_register_init ("zfs_arc", za_init);
181 plugin_register_read ("zfs_arc", za_read);
182 } /* void module_register */
184 /* vmi: set sw=8 noexpandtab fdm=marker : */