cd0c8414b114eadb605c1e39a4ace65e1ea92399
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 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Anthony Dewhurst <dewhurst at gmail>
22 * Aurelien Rougemont <beorn at gandi.net>
23 * Xin Li <delphij at FreeBSD.org>
24 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
30 /*
31 * Global variables
32 */
34 #if !defined(__FreeBSD__)
35 extern kstat_ctl_t *kc;
37 static long long get_zfs_value(kstat_t *ksp, char *name)
38 {
40 return (get_kstat_value(ksp, name));
41 }
42 #else
43 #include <sys/types.h>
44 #include <sys/sysctl.h>
46 const char zfs_arcstat[] = "kstat.zfs.misc.arcstats.";
48 #if !defined(kstat_t)
49 typedef void kstat_t;
50 #endif
52 static long long get_zfs_value(void * dummy __unused, const char *kstat_value)
53 {
54 long long value;
55 size_t valuelen = sizeof(value);
56 int rv;
57 char *key;
59 key = ssnprintf_alloc("%s%s", zfs_arcstat, kstat_value);
60 if (key != NULL) {
61 if (strlen(key) > 0) {
62 rv = sysctlbyname(key, (void *)&value, &valuelen, NULL, (size_t)0);
63 free(key);
64 if (rv == 0)
65 return (value);
66 } else
67 free(key);
68 }
70 return (-1);
71 }
72 #endif
74 static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len)
75 {
76 value_list_t vl = VALUE_LIST_INIT;
78 vl.values = values;
79 vl.values_len = values_len;
81 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
82 sstrncpy (vl.plugin, "zfs_arc", sizeof (vl.plugin));
83 sstrncpy (vl.type, type, sizeof (vl.type));
84 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
86 plugin_dispatch_values (&vl);
87 }
89 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
90 {
91 value_t vv;
93 vv.gauge = value;
94 za_submit (type, type_instance, &vv, 1);
95 }
97 static int za_read_derive (kstat_t *ksp, const char *kstat_value,
98 const char *type, const char *type_instance)
99 {
100 long long tmp;
101 value_t v;
103 tmp = get_zfs_value (ksp, (char *)kstat_value);
104 if (tmp == -1LL)
105 {
106 ERROR ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
107 return (-1);
108 }
110 v.derive = (derive_t) tmp;
111 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
112 return (0);
113 }
115 static int za_read_gauge (kstat_t *ksp, const char *kstat_value,
116 const char *type, const char *type_instance)
117 {
118 long long tmp;
119 value_t v;
121 tmp = get_zfs_value (ksp, (char *)kstat_value);
122 if (tmp == -1LL)
123 {
124 ERROR ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
125 return (-1);
126 }
128 v.gauge = (gauge_t) tmp;
129 za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
130 return (0);
131 }
133 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
134 {
135 gauge_t ratio = NAN;
137 if (!isfinite (hits) || (hits < 0.0))
138 hits = 0.0;
139 if (!isfinite (misses) || (misses < 0.0))
140 misses = 0.0;
142 if ((hits != 0.0) || (misses != 0.0))
143 ratio = hits / (hits + misses);
145 za_submit_gauge ("cache_ratio", type_instance, ratio);
146 }
148 static int za_read (void)
149 {
150 gauge_t arc_hits, arc_misses, l2_hits, l2_misses;
151 value_t l2_io[2];
152 kstat_t *ksp = NULL;
154 #if !defined(__FreeBSD__)
155 get_kstat (&ksp, "zfs", 0, "arcstats");
156 if (ksp == NULL)
157 {
158 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
159 return (-1);
160 }
161 #endif
163 /* Sizes */
164 za_read_gauge (ksp, "size", "cache_size", "arc");
165 za_read_gauge (ksp, "l2_size", "cache_size", "L2");
167 /* Operations */
168 za_read_derive (ksp, "allocated","cache_operation", "allocated");
169 za_read_derive (ksp, "deleted", "cache_operation", "deleted");
170 za_read_derive (ksp, "stolen", "cache_operation", "stolen");
172 /* Issue indicators */
173 za_read_derive (ksp, "mutex_miss", "mutex_operations", "miss");
174 za_read_derive (ksp, "hash_collisions", "hash_collisions", "");
176 /* Evictions */
177 za_read_derive (ksp, "evict_l2_cached", "cache_eviction", "cached");
178 za_read_derive (ksp, "evict_l2_eligible", "cache_eviction", "eligible");
179 za_read_derive (ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
181 /* Hits / misses */
182 za_read_derive (ksp, "demand_data_hits", "cache_result", "demand_data-hit");
183 za_read_derive (ksp, "demand_metadata_hits", "cache_result", "demand_metadata-hit");
184 za_read_derive (ksp, "prefetch_data_hits", "cache_result", "prefetch_data-hit");
185 za_read_derive (ksp, "prefetch_metadata_hits", "cache_result", "prefetch_metadata-hit");
186 za_read_derive (ksp, "demand_data_misses", "cache_result", "demand_data-miss");
187 za_read_derive (ksp, "demand_metadata_misses", "cache_result", "demand_metadata-miss");
188 za_read_derive (ksp, "prefetch_data_misses", "cache_result", "prefetch_data-miss");
189 za_read_derive (ksp, "prefetch_metadata_misses", "cache_result", "prefetch_metadata-miss");
191 /* Ratios */
192 arc_hits = (gauge_t) get_zfs_value(ksp, "hits");
193 arc_misses = (gauge_t) get_zfs_value(ksp, "misses");
194 l2_hits = (gauge_t) get_zfs_value(ksp, "l2_hits");
195 l2_misses = (gauge_t) get_zfs_value(ksp, "l2_misses");
197 za_submit_ratio ("arc", arc_hits, arc_misses);
198 za_submit_ratio ("L2", l2_hits, l2_misses);
200 /* I/O */
201 l2_io[0].derive = get_zfs_value(ksp, "l2_read_bytes");
202 l2_io[1].derive = get_zfs_value(ksp, "l2_write_bytes");
204 za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
206 return (0);
207 } /* int za_read */
209 static int za_init (void) /* {{{ */
210 {
211 #if !defined(__FreeBSD__)
212 /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
213 if (kc == NULL)
214 {
215 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
216 return (-1);
217 }
218 #endif
220 return (0);
221 } /* }}} int za_init */
223 void module_register (void)
224 {
225 plugin_register_init ("zfs_arc", za_init);
226 plugin_register_read ("zfs_arc", za_read);
227 } /* void module_register */
229 /* vmi: set sw=8 noexpandtab fdm=marker : */