4190a717e01d264431bbd02412e679f925888adc
[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  * Copyright (C) 2014  Marc Fournier
7  *
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.
11  *
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.
16  *
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
20  *
21  * Authors:
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>
26  **/
27
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31
32 /*
33  * Global variables
34  */
35
36 #if defined(KERNEL_LINUX)
37 #include "utils_llist.h"
38 #define ZOL_ARCSTATS_FILE "/proc/spl/kstat/zfs/arcstats"
39
40 #if !defined(kstat_t)
41 typedef void kstat_t;
42 static llist_t *zfs_stats = NULL;
43 #endif
44
45 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
46                 char *name)
47 {
48         llentry_t *e;
49
50         e = llist_search (zfs_stats, name);
51         if (e == NULL)
52         {
53                 ERROR ("zfs_arc plugin: `llist_search` failed for key: '%s'.", name);
54                 return (-1);
55         }
56
57         return ((long long int)e->value);
58 }
59
60 #elif !defined(__FreeBSD__) // Solaris
61 extern kstat_ctl_t *kc;
62
63 static long long get_zfs_value(kstat_t *ksp, char *name)
64 {
65
66         return (get_kstat_value(ksp, name));
67 }
68 #else // FreeBSD
69 #include <sys/types.h>
70 #include <sys/sysctl.h>
71
72 const char zfs_arcstat[] = "kstat.zfs.misc.arcstats.";
73
74 #if !defined(kstat_t)
75 typedef void kstat_t;
76 #endif
77
78 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
79                 char const *name)
80 {
81         char buffer[256];
82         long long value;
83         size_t valuelen = sizeof(value);
84         int rv;
85
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);
89         if (rv == 0)
90                 return (value);
91
92         return (-1);
93 }
94 #endif
95
96 static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len)
97 {
98         value_list_t vl = VALUE_LIST_INIT;
99
100         vl.values = values;
101         vl.values_len = values_len;
102
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));
107
108         plugin_dispatch_values (&vl);
109 }
110
111 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
112 {
113         value_t vv;
114
115         vv.gauge = value;
116         za_submit (type, type_instance, &vv, 1);
117 }
118
119 static int za_read_derive (kstat_t *ksp, const char *kstat_value,
120     const char *type, const char *type_instance)
121 {
122   long long tmp;
123   value_t v;
124
125   tmp = get_zfs_value (ksp, (char *)kstat_value);
126   if (tmp == -1LL)
127   {
128     WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
129     return (-1);
130   }
131
132   v.derive = (derive_t) tmp;
133   za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
134   return (0);
135 }
136
137 static int za_read_gauge (kstat_t *ksp, const char *kstat_value,
138     const char *type, const char *type_instance)
139 {
140   long long tmp;
141   value_t v;
142
143   tmp = get_zfs_value (ksp, (char *)kstat_value);
144   if (tmp == -1LL)
145   {
146     WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
147     return (-1);
148   }
149
150   v.gauge = (gauge_t) tmp;
151   za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
152   return (0);
153 }
154
155 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
156 {
157         gauge_t ratio = NAN;
158
159         if (!isfinite (hits) || (hits < 0.0))
160                 hits = 0.0;
161         if (!isfinite (misses) || (misses < 0.0))
162                 misses = 0.0;
163
164         if ((hits != 0.0) || (misses != 0.0))
165                 ratio = hits / (hits + misses);
166
167         za_submit_gauge ("cache_ratio", type_instance, ratio);
168 }
169
170 static int za_read (void)
171 {
172         gauge_t  arc_hits, arc_misses, l2_hits, l2_misses;
173         value_t  l2_io[2];
174         kstat_t  *ksp   = NULL;
175
176 #if KERNEL_LINUX
177         FILE *fh;
178
179         char buf[1024];
180         char *fields[3];
181         int numfields;
182
183         if ((fh = fopen (ZOL_ARCSTATS_FILE, "r")) == NULL)
184         {
185                 char errbuf[1024];
186                 ERROR ("zfs_arc plugin: `fopen (%s)' failed: %s",
187                         ZOL_ARCSTATS_FILE,
188                         sstrerror (errno, errbuf, sizeof (errbuf)));
189                 return (-1);
190         }
191
192         zfs_stats = llist_create ();
193         if (zfs_stats == NULL)
194         {
195                 ERROR ("zfs_arc plugin: `llist_create' failed.");
196                 fclose (fh);
197                 return (-1);
198         }
199
200         while (fgets (buf, 1024, fh) != NULL)
201         {
202                 numfields = strsplit (buf, fields, 4);
203                 if (numfields != 3)
204                         continue;
205
206                 char *llkey;
207                 long long int llvalue;
208
209                 llkey = strdup (fields[0]);
210                 if (llkey == NULL) {
211                         char errbuf[1024];
212                         ERROR ("zfs_arc plugin: `strdup' failed: %s",
213                                 sstrerror (errno, errbuf, sizeof (errbuf)));
214                         continue;
215                 }
216
217                 llvalue = atoll (fields[2]);
218
219                 llentry_t *e;
220                 e = llentry_create (llkey, (void *)llvalue);
221                 if (e == NULL)
222                 {
223                         ERROR ("zfs_arc plugin: `llentry_create' failed.");
224                         free (llkey);
225                         continue;
226                 }
227
228                 free (llkey);
229
230                 llist_append (zfs_stats, e);
231         }
232
233         if (fclose (fh))
234         {
235                 char errbuf[1024];
236                 WARNING ("zfs_arc: `fclose' failed: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
237         }
238
239 #elif !defined(__FreeBSD__) // Solaris
240         get_kstat (&ksp, "zfs", 0, "arcstats");
241         if (ksp == NULL)
242         {
243                 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
244                 return (-1);
245         }
246 #endif
247
248         /* Sizes */
249         za_read_gauge (ksp, "size",    "cache_size", "arc");
250         za_read_gauge (ksp, "l2_size", "cache_size", "L2");
251
252         /* Operations */
253         za_read_derive (ksp, "deleted",  "cache_operation", "deleted");
254 #if __FreeBSD__
255         za_read_derive (ksp, "allocated","cache_operation", "allocated");
256         za_read_derive (ksp, "stolen",   "cache_operation", "stolen");
257 #endif
258
259         /* Issue indicators */
260         za_read_derive (ksp, "mutex_miss", "mutex_operations", "miss");
261         za_read_derive (ksp, "hash_collisions", "hash_collisions", "");
262
263         /* Evictions */
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");
267
268         /* Hits / misses */
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");
277
278         /* Ratios */
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");
283
284         za_submit_ratio ("arc", arc_hits, arc_misses);
285         za_submit_ratio ("L2", l2_hits, l2_misses);
286
287         /* I/O */
288         l2_io[0].derive = get_zfs_value(ksp, "l2_read_bytes");
289         l2_io[1].derive = get_zfs_value(ksp, "l2_write_bytes");
290
291         za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
292
293 #if defined(KERNEL_LINUX)
294         if (zfs_stats != NULL)
295         {
296                 llist_destroy (zfs_stats);
297         }
298 #endif
299
300         return (0);
301 } /* int za_read */
302
303 static int za_init (void) /* {{{ */
304 {
305 #if !defined(__FreeBSD__) && !defined(KERNEL_LINUX) // Solaris
306         /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
307         if (kc == NULL)
308         {
309                 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
310                 return (-1);
311         }
312 #endif
313
314         return (0);
315 } /* }}} int za_init */
316
317 void module_register (void)
318 {
319         plugin_register_init ("zfs_arc", za_init);
320         plugin_register_read ("zfs_arc", za_read);
321 } /* void module_register */
322
323 /* vmi: set sw=8 noexpandtab fdm=marker : */