Merge branch 'collectd-5.4' into collectd-5.5
[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  * Copyright (C) 2014  Wilfried Goesgens
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; only version 2 of the License is applicable.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  *
22  * Authors:
23  *   Anthony Dewhurst <dewhurst at gmail>
24  *   Aurelien Rougemont <beorn at gandi.net>
25  *   Xin Li <delphij at FreeBSD.org>
26  *   Marc Fournier <marc.fournier at camptocamp.com>
27  *   Wilfried Goesgens <dothebart at citadel.org>
28  **/
29
30 #include "collectd.h"
31 #include "common.h"
32 #include "plugin.h"
33
34 /*
35  * Global variables
36  */
37
38 #if defined(KERNEL_LINUX)
39 #include "utils_llist.h"
40 #define ZOL_ARCSTATS_FILE "/proc/spl/kstat/zfs/arcstats"
41
42 typedef llist_t kstat_t;
43
44 static long long get_zfs_value(kstat_t *zfs_stats  __attribute__((unused)),
45                 char *name)
46 {
47         llentry_t *e;
48
49         e = llist_search (zfs_stats, name);
50         if (e == NULL)
51         {
52                 ERROR ("zfs_arc plugin: `llist_search` failed for key: '%s'.", name);
53                 return (-1);
54         }
55
56         return (*(long long int*)e->value);
57 }
58
59 #elif !defined(__FreeBSD__) // Solaris
60 extern kstat_ctl_t *kc;
61
62 static long long get_zfs_value(kstat_t *ksp, char *name)
63 {
64
65         return (get_kstat_value(ksp, name));
66 }
67 #else // FreeBSD
68 #include <sys/types.h>
69 #include <sys/sysctl.h>
70
71 const char zfs_arcstat[] = "kstat.zfs.misc.arcstats.";
72
73 #if !defined(kstat_t)
74 typedef void kstat_t;
75 #endif
76
77 static long long get_zfs_value(kstat_t *dummy __attribute__((unused)),
78                 char const *name)
79 {
80         char buffer[256];
81         long long value;
82         size_t valuelen = sizeof(value);
83         int rv;
84
85         ssnprintf (buffer, sizeof (buffer), "%s%s", zfs_arcstat, name);
86         rv = sysctlbyname (buffer, (void *) &value, &valuelen,
87                         /* new value = */ NULL, /* new length = */ (size_t) 0);
88         if (rv == 0)
89                 return (value);
90
91         return (-1);
92 }
93 #endif
94
95 static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len)
96 {
97         value_list_t vl = VALUE_LIST_INIT;
98
99         vl.values = values;
100         vl.values_len = values_len;
101
102         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
103         sstrncpy (vl.plugin, "zfs_arc", sizeof (vl.plugin));
104         sstrncpy (vl.type, type, sizeof (vl.type));
105         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
106
107         plugin_dispatch_values (&vl);
108 }
109
110 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
111 {
112         value_t vv;
113
114         vv.gauge = value;
115         za_submit (type, type_instance, &vv, 1);
116 }
117
118 static int za_read_derive (kstat_t *ksp, const char *kstat_value,
119     const char *type, const char *type_instance)
120 {
121   long long tmp;
122   value_t v;
123
124   tmp = get_zfs_value (ksp, (char *)kstat_value);
125   if (tmp == -1LL)
126   {
127     WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
128     return (-1);
129   }
130
131   v.derive = (derive_t) tmp;
132   za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
133   return (0);
134 }
135
136 static int za_read_gauge (kstat_t *ksp, const char *kstat_value,
137     const char *type, const char *type_instance)
138 {
139   long long tmp;
140   value_t v;
141
142   tmp = get_zfs_value (ksp, (char *)kstat_value);
143   if (tmp == -1LL)
144   {
145     WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
146     return (-1);
147   }
148
149   v.gauge = (gauge_t) tmp;
150   za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
151   return (0);
152 }
153
154 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
155 {
156         gauge_t ratio = NAN;
157
158         if (!isfinite (hits) || (hits < 0.0))
159                 hits = 0.0;
160         if (!isfinite (misses) || (misses < 0.0))
161                 misses = 0.0;
162
163         if ((hits != 0.0) || (misses != 0.0))
164                 ratio = hits / (hits + misses);
165
166         za_submit_gauge ("cache_ratio", type_instance, ratio);
167 }
168
169 static int za_read (void)
170 {
171         gauge_t  arc_hits, arc_misses, l2_hits, l2_misses;
172         value_t  l2_io[2];
173         kstat_t  *ksp   = NULL;
174
175 #if KERNEL_LINUX
176         long long int *llvalues = NULL;
177         char file_contents[1024 * 10];
178         char *fields[3];
179         int numfields;
180         ssize_t len;
181
182         ksp = llist_create ();
183         if (ksp == NULL)
184         {
185                 ERROR ("zfs_arc plugin: `llist_create' failed.");
186                 return (-1);
187         }
188
189         len = read_file_contents (ZOL_ARCSTATS_FILE, file_contents, sizeof(file_contents) - 1);
190         if (len > 1)
191         {
192
193                 int i=0;
194                 char *pnl = file_contents;
195                 char *pnnl;
196
197                 file_contents[len] = '\0';
198
199                 while (pnl != NULL)
200                 {
201                         pnl = strchr(pnl, '\n');
202                         i++;
203                         if (pnl && (*pnl != '\0'))
204                                 pnl++;
205                 }
206
207                 if (i > 0)
208                 {
209                         llentry_t *e;
210                         llvalues = malloc(sizeof(long long int) * i);
211                         if (llvalues == NULL)
212                         {
213                                 ERROR ("zfs_arc plugin: `malloc' failed.");
214                                 llist_destroy (ksp);
215                                 return (-1);
216                         }
217                         int j = 0;
218
219                         pnl = file_contents;
220                         while (pnl != NULL)
221                         {
222                                 pnnl = strchr(pnl, '\n');
223                                 if (pnnl != NULL)
224                                         *pnnl = '\0';
225
226                                 numfields = strsplit (pnl, fields, 4);
227                                 if (numfields == 3)
228                                 {
229                                         llvalues[j] = atoll (fields[2]);
230
231                                         e = llentry_create (fields[0], &llvalues[j]);
232                                         if (e == NULL)
233                                         {
234                                                 ERROR ("zfs_arc plugin: `llentry_create' failed.");
235                                         }
236                                         else
237                                         {
238                                                 llist_append (ksp, e);
239                                         }
240                                         j++;
241                                 }
242                                 pnl = pnnl;
243                                 if (pnl != NULL)
244                                         pnl ++;
245                         }
246                 }
247         }
248
249 #elif !defined(__FreeBSD__) // Solaris
250         get_kstat (&ksp, "zfs", 0, "arcstats");
251         if (ksp == NULL)
252         {
253                 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
254                 return (-1);
255         }
256 #endif
257
258         /* Sizes */
259         za_read_gauge (ksp, "size",    "cache_size", "arc");
260
261         /* The "l2_size" value has disappeared from Solaris some time in
262          * early 2013, and has only reappeared recently in Solaris 11.2.
263          * Stop trying if we ever fail to read it, so we don't spam the log.
264          */
265         static int l2_size_avail = 1;
266         if (l2_size_avail && za_read_gauge (ksp, "l2_size", "cache_size", "L2") != 0)
267                 l2_size_avail = 0;
268
269         /* Operations */
270         za_read_derive (ksp, "deleted",  "cache_operation", "deleted");
271 #if __FreeBSD__
272         za_read_derive (ksp, "allocated","cache_operation", "allocated");
273 #if defined(__FreeBSD_version) && (__FreeBSD_version < 1002501)
274         /* stolen removed from sysctl kstat.zfs.misc.arcstats on FreeBSD 10.2+ */
275         za_read_derive (ksp, "stolen",   "cache_operation", "stolen");
276 #endif
277 #endif
278
279         /* Issue indicators */
280         za_read_derive (ksp, "mutex_miss", "mutex_operations", "miss");
281         za_read_derive (ksp, "hash_collisions", "hash_collisions", "");
282
283         /* Evictions */
284         za_read_derive (ksp, "evict_l2_cached",     "cache_eviction", "cached");
285         za_read_derive (ksp, "evict_l2_eligible",   "cache_eviction", "eligible");
286         za_read_derive (ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
287
288         /* Hits / misses */
289         za_read_derive (ksp, "demand_data_hits",         "cache_result", "demand_data-hit");
290         za_read_derive (ksp, "demand_metadata_hits",     "cache_result", "demand_metadata-hit");
291         za_read_derive (ksp, "prefetch_data_hits",       "cache_result", "prefetch_data-hit");
292         za_read_derive (ksp, "prefetch_metadata_hits",   "cache_result", "prefetch_metadata-hit");
293         za_read_derive (ksp, "demand_data_misses",       "cache_result", "demand_data-miss");
294         za_read_derive (ksp, "demand_metadata_misses",   "cache_result", "demand_metadata-miss");
295         za_read_derive (ksp, "prefetch_data_misses",     "cache_result", "prefetch_data-miss");
296         za_read_derive (ksp, "prefetch_metadata_misses", "cache_result", "prefetch_metadata-miss");
297
298         /* Ratios */
299         arc_hits   = (gauge_t) get_zfs_value(ksp, "hits");
300         arc_misses = (gauge_t) get_zfs_value(ksp, "misses");
301         l2_hits    = (gauge_t) get_zfs_value(ksp, "l2_hits");
302         l2_misses  = (gauge_t) get_zfs_value(ksp, "l2_misses");
303
304         za_submit_ratio ("arc", arc_hits, arc_misses);
305         za_submit_ratio ("L2", l2_hits, l2_misses);
306
307         /* I/O */
308         l2_io[0].derive = get_zfs_value(ksp, "l2_read_bytes");
309         l2_io[1].derive = get_zfs_value(ksp, "l2_write_bytes");
310
311         za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
312
313 #if defined(KERNEL_LINUX)
314         if (llvalues != NULL)
315         {
316                 free(llvalues);
317         }
318         if (ksp != NULL)
319         {
320                 llist_destroy (ksp);
321         }
322 #endif
323
324         return (0);
325 } /* int za_read */
326
327 static int za_init (void) /* {{{ */
328 {
329 #if !defined(__FreeBSD__) && !defined(KERNEL_LINUX) // Solaris
330         /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
331         if (kc == NULL)
332         {
333                 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
334                 return (-1);
335         }
336 #endif
337
338         return (0);
339 } /* }}} int za_init */
340
341 void module_register (void)
342 {
343         plugin_register_init ("zfs_arc", za_init);
344         plugin_register_read ("zfs_arc", za_read);
345 } /* void module_register */
346
347 /* vmi: set sw=8 noexpandtab fdm=marker : */