Merge branch 'collectd-5.3' into collectd-5.4
[collectd.git] / src / zfs_arc.c
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  **/
25
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29
30 /*
31  * Global variables
32  */
33
34 #if !defined(__FreeBSD__)
35 extern kstat_ctl_t *kc;
36
37 static long long get_zfs_value(kstat_t *ksp, char *name)
38 {
39
40         return (get_kstat_value(ksp, name));
41 }
42 #else
43 #include <sys/types.h>
44 #include <sys/sysctl.h>
45
46 const char zfs_arcstat[] = "kstat.zfs.misc.arcstats.";
47
48 #if !defined(kstat_t)
49 typedef void kstat_t;
50 #endif
51
52 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
53                 char const *name)
54 {
55         char buffer[256];
56         long long value;
57         size_t valuelen = sizeof(value);
58         int rv;
59
60         ssnprintf (buffer, sizeof (buffer), "%s%s", zfs_arcstat, name);
61         rv = sysctlbyname (buffer, (void *) &value, &valuelen,
62                         /* new value = */ NULL, /* new length = */ (size_t) 0);
63         if (rv == 0)
64                 return (value);
65
66         return (-1);
67 }
68 #endif
69
70 static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len)
71 {
72         value_list_t vl = VALUE_LIST_INIT;
73
74         vl.values = values;
75         vl.values_len = values_len;
76
77         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
78         sstrncpy (vl.plugin, "zfs_arc", sizeof (vl.plugin));
79         sstrncpy (vl.type, type, sizeof (vl.type));
80         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
81
82         plugin_dispatch_values (&vl);
83 }
84
85 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
86 {
87         value_t vv;
88
89         vv.gauge = value;
90         za_submit (type, type_instance, &vv, 1);
91 }
92
93 static int za_read_derive (kstat_t *ksp, const char *kstat_value,
94     const char *type, const char *type_instance)
95 {
96   long long tmp;
97   value_t v;
98
99   tmp = get_zfs_value (ksp, (char *)kstat_value);
100   if (tmp == -1LL)
101   {
102     WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
103     return (-1);
104   }
105
106   v.derive = (derive_t) tmp;
107   za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
108   return (0);
109 }
110
111 static int za_read_gauge (kstat_t *ksp, const char *kstat_value,
112     const char *type, const char *type_instance)
113 {
114   long long tmp;
115   value_t v;
116
117   tmp = get_zfs_value (ksp, (char *)kstat_value);
118   if (tmp == -1LL)
119   {
120     WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
121     return (-1);
122   }
123
124   v.gauge = (gauge_t) tmp;
125   za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
126   return (0);
127 }
128
129 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
130 {
131         gauge_t ratio = NAN;
132
133         if (!isfinite (hits) || (hits < 0.0))
134                 hits = 0.0;
135         if (!isfinite (misses) || (misses < 0.0))
136                 misses = 0.0;
137
138         if ((hits != 0.0) || (misses != 0.0))
139                 ratio = hits / (hits + misses);
140
141         za_submit_gauge ("cache_ratio", type_instance, ratio);
142 }
143
144 static int za_read (void)
145 {
146         gauge_t  arc_hits, arc_misses, l2_hits, l2_misses;
147         value_t  l2_io[2];
148         kstat_t  *ksp   = NULL;
149
150 #if !defined(__FreeBSD__)
151         get_kstat (&ksp, "zfs", 0, "arcstats");
152         if (ksp == NULL)
153         {
154                 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
155                 return (-1);
156         }
157 #endif
158
159         /* Sizes */
160         za_read_gauge (ksp, "size",    "cache_size", "arc");
161
162         /* The "l2_size" value has disappeared from Solaris some time in
163          * early 2013, and has only reappeared recently in Solaris 11.2.
164          * Stop trying if we ever fail to read it, so we don't spam the log.
165          */
166         static int l2_size_avail = 1;
167         if (l2_size_avail && za_read_gauge (ksp, "l2_size", "cache_size", "L2") != 0)
168                 l2_size_avail = 0;
169
170         /* Operations */
171         za_read_derive (ksp, "deleted",  "cache_operation", "deleted");
172 #if __FreeBSD__
173         za_read_derive (ksp, "allocated","cache_operation", "allocated");
174         za_read_derive (ksp, "stolen",   "cache_operation", "stolen");
175 #endif
176
177         /* Issue indicators */
178         za_read_derive (ksp, "mutex_miss", "mutex_operations", "miss");
179         za_read_derive (ksp, "hash_collisions", "hash_collisions", "");
180         
181         /* Evictions */
182         za_read_derive (ksp, "evict_l2_cached",     "cache_eviction", "cached");
183         za_read_derive (ksp, "evict_l2_eligible",   "cache_eviction", "eligible");
184         za_read_derive (ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
185
186         /* Hits / misses */
187         za_read_derive (ksp, "demand_data_hits",         "cache_result", "demand_data-hit");
188         za_read_derive (ksp, "demand_metadata_hits",     "cache_result", "demand_metadata-hit");
189         za_read_derive (ksp, "prefetch_data_hits",       "cache_result", "prefetch_data-hit");
190         za_read_derive (ksp, "prefetch_metadata_hits",   "cache_result", "prefetch_metadata-hit");
191         za_read_derive (ksp, "demand_data_misses",       "cache_result", "demand_data-miss");
192         za_read_derive (ksp, "demand_metadata_misses",   "cache_result", "demand_metadata-miss");
193         za_read_derive (ksp, "prefetch_data_misses",     "cache_result", "prefetch_data-miss");
194         za_read_derive (ksp, "prefetch_metadata_misses", "cache_result", "prefetch_metadata-miss");
195
196         /* Ratios */
197         arc_hits   = (gauge_t) get_zfs_value(ksp, "hits");
198         arc_misses = (gauge_t) get_zfs_value(ksp, "misses");
199         l2_hits    = (gauge_t) get_zfs_value(ksp, "l2_hits");
200         l2_misses  = (gauge_t) get_zfs_value(ksp, "l2_misses");
201
202         za_submit_ratio ("arc", arc_hits, arc_misses);
203         za_submit_ratio ("L2", l2_hits, l2_misses);
204
205         /* I/O */
206         l2_io[0].derive = get_zfs_value(ksp, "l2_read_bytes");
207         l2_io[1].derive = get_zfs_value(ksp, "l2_write_bytes");
208
209         za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
210
211         return (0);
212 } /* int za_read */
213
214 static int za_init (void) /* {{{ */
215 {
216 #if !defined(__FreeBSD__)
217         /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
218         if (kc == NULL)
219         {
220                 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
221                 return (-1);
222         }
223 #endif
224
225         return (0);
226 } /* }}} int za_init */
227
228 void module_register (void)
229 {
230         plugin_register_init ("zfs_arc", za_init);
231         plugin_register_read ("zfs_arc", za_read);
232 } /* void module_register */
233
234 /* vmi: set sw=8 noexpandtab fdm=marker : */