cc988625b32178a005222befd9dccae0898270e5
[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  *
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  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 /*
29  * Global variables
30  */
31
32 extern kstat_ctl_t *kc;
33
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;
37
38         vl.values = values;
39         vl.values_len = values_len;
40
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));
45
46         plugin_dispatch_values (&vl);
47 }
48
49 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
50 {
51         value_t vv;
52
53         vv.gauge = value;
54         za_submit (type, type_instance, &vv, 1);
55 }
56
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;
62
63   tmp = get_kstat_value (ksp, (char *)kstat_value);
64   if (tmp == -1LL)
65   {
66     WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
67     return (-1);
68   }
69
70   v.derive = (derive_t) tmp;
71   za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
72   return (0);
73 }
74
75 static int za_read_gauge (kstat_t *ksp, const char *kstat_value,
76     const char *type, const char *type_instance)
77 {
78   long long tmp;
79   value_t v;
80
81   tmp = get_kstat_value (ksp, (char *)kstat_value);
82   if (tmp == -1LL)
83   {
84     WARNING ("zfs_arc plugin: Reading kstat value \"%s\" failed.", kstat_value);
85     return (-1);
86   }
87
88   v.gauge = (gauge_t) tmp;
89   za_submit (type, type_instance, /* values = */ &v, /* values_num = */ 1);
90   return (0);
91 }
92
93 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
94 {
95         gauge_t ratio = NAN;
96
97         if (!isfinite (hits) || (hits < 0.0))
98                 hits = 0.0;
99         if (!isfinite (misses) || (misses < 0.0))
100                 misses = 0.0;
101
102         if ((hits != 0.0) || (misses != 0.0))
103                 ratio = hits / (hits + misses);
104
105         za_submit_gauge ("cache_ratio", type_instance, ratio);
106 }
107
108 static int za_read (void)
109 {
110         gauge_t  arc_hits, arc_misses, l2_hits, l2_misses;
111         value_t  l2_io[2];
112         kstat_t  *ksp   = NULL;
113
114         get_kstat (&ksp, "zfs", 0, "arcstats");
115         if (ksp == NULL)
116         {
117                 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
118                 return (-1);
119         }
120
121         /* Sizes */
122         za_read_gauge (ksp, "size",    "cache_size", "arc");
123         za_read_gauge (ksp, "l2_size", "cache_size", "L2");
124
125         /* Operations */
126         za_read_derive (ksp, "deleted",  "cache_operation", "deleted");
127 #if __FreeBSD__
128         za_read_derive (ksp, "allocated","cache_operation", "allocated");
129         za_read_derive (ksp, "stolen",   "cache_operation", "stolen");
130 #endif
131
132         /* Issue indicators */
133         za_read_derive (ksp, "mutex_miss", "mutex_operations", "miss");
134         za_read_derive (ksp, "hash_collisions", "hash_collisions", "");
135         
136         /* Evictions */
137         za_read_derive (ksp, "evict_l2_cached",     "cache_eviction", "cached");
138         za_read_derive (ksp, "evict_l2_eligible",   "cache_eviction", "eligible");
139         za_read_derive (ksp, "evict_l2_ineligible", "cache_eviction", "ineligible");
140
141         /* Hits / misses */
142         za_read_derive (ksp, "demand_data_hits",         "cache_result", "demand_data-hit");
143         za_read_derive (ksp, "demand_metadata_hits",     "cache_result", "demand_metadata-hit");
144         za_read_derive (ksp, "prefetch_data_hits",       "cache_result", "prefetch_data-hit");
145         za_read_derive (ksp, "prefetch_metadata_hits",   "cache_result", "prefetch_metadata-hit");
146         za_read_derive (ksp, "demand_data_misses",       "cache_result", "demand_data-miss");
147         za_read_derive (ksp, "demand_metadata_misses",   "cache_result", "demand_metadata-miss");
148         za_read_derive (ksp, "prefetch_data_misses",     "cache_result", "prefetch_data-miss");
149         za_read_derive (ksp, "prefetch_metadata_misses", "cache_result", "prefetch_metadata-miss");
150
151         /* Ratios */
152         arc_hits   = (gauge_t) get_kstat_value(ksp, "hits");
153         arc_misses = (gauge_t) get_kstat_value(ksp, "misses");
154         l2_hits    = (gauge_t) get_kstat_value(ksp, "l2_hits");
155         l2_misses  = (gauge_t) get_kstat_value(ksp, "l2_misses");
156
157         za_submit_ratio ("arc", arc_hits, arc_misses);
158         za_submit_ratio ("L2", l2_hits, l2_misses);
159
160         /* I/O */
161         l2_io[0].derive = get_kstat_value(ksp, "l2_read_bytes");
162         l2_io[1].derive = get_kstat_value(ksp, "l2_write_bytes");
163
164         za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
165
166         return (0);
167 } /* int za_read */
168
169 static int za_init (void) /* {{{ */
170 {
171         /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
172         if (kc == NULL)
173         {
174                 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
175                 return (-1);
176         }
177
178         return (0);
179 } /* }}} int za_init */
180
181 void module_register (void)
182 {
183         plugin_register_init ("zfs_arc", za_init);
184         plugin_register_read ("zfs_arc", za_read);
185 } /* void module_register */
186
187 /* vmi: set sw=8 noexpandtab fdm=marker : */