5f14e90981adda089fb2b94cd85b5094a4e451b2
[collectd.git] / src / zfs_arc.c
1 /**
2  * collectd - src/zfs_arc.c
3  * Copyright (C) 2009  Anthony Dewhurst
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Anthony Dewhurst <dewhurst at gmail>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 /*
27  * Global variables
28  */
29 static kstat_t *ksp;
30 extern kstat_ctl_t *kc;
31
32 static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len)
33 {
34         value_list_t vl = VALUE_LIST_INIT;
35
36         vl.values = values;
37         vl.values_len = values_len;
38
39         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
40         sstrncpy (vl.plugin, "zfs_arc", sizeof (vl.plugin));
41         sstrncpy (vl.type, type, sizeof (vl.type));
42         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
43
44         plugin_dispatch_values (&vl);
45 }
46
47 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
48 {
49         value_t values[1];
50
51         values[0].gauge = value;
52
53         za_submit (type, type_instance, values, STATIC_ARRAY_SIZE(values));
54 }
55
56 static void za_submit_size (gauge_t size, gauge_t size_target, gauge_t limit_min, gauge_t limit_max)
57 {
58         value_t values[4];
59
60         values[0].gauge = size;
61         values[1].gauge = size_target;
62         values[2].gauge = limit_min;
63         values[3].gauge = limit_max;
64
65         za_submit ("arc_size", "", values, STATIC_ARRAY_SIZE(values));
66 }
67
68 static void za_submit_bytes (counter_t read, counter_t write)
69 {
70         value_t values[2];
71
72         values[0].counter = read;
73         values[1].counter = write;
74
75         za_submit ("arc_l2_bytes", "", values, STATIC_ARRAY_SIZE(values));
76 }
77
78 static void za_submit_counts (char *type_instance, counter_t demand_data, counter_t demand_metadata,
79         counter_t prefetch_data, counter_t prefetch_metadata)
80 {
81         value_t values[4];
82
83         values[0].counter = demand_data;
84         values[1].counter = demand_metadata;
85         values[2].counter = prefetch_data;
86         values[3].counter = prefetch_metadata;
87
88         za_submit ("arc_counts", type_instance, values, STATIC_ARRAY_SIZE(values));
89 }
90
91 static int za_read (void)
92 {
93         gauge_t   arcsize, targetsize, minlimit, maxlimit, hits, misses, l2_size, l2_hits, l2_misses;
94         counter_t demand_data_hits, demand_metadata_hits, prefetch_data_hits, prefetch_metadata_hits;
95         counter_t demand_data_misses, demand_metadata_misses, prefetch_data_misses, prefetch_metadata_misses;
96         counter_t l2_read_bytes, l2_write_bytes;
97
98         get_kstat (&ksp, "zfs", 0, "arcstats");
99         if (ksp == NULL)
100         {
101                 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
102                 return (-1);
103         }
104
105         arcsize    = get_kstat_value(ksp, "size");
106         targetsize = get_kstat_value(ksp, "c");
107         minlimit   = get_kstat_value(ksp, "c_min");
108         maxlimit   = get_kstat_value(ksp, "c_max");
109
110         demand_data_hits       = get_kstat_value(ksp, "demand_data_hits");
111         demand_metadata_hits   = get_kstat_value(ksp, "demand_metadata_hits");
112         prefetch_data_hits     = get_kstat_value(ksp, "prefetch_data_hits");
113         prefetch_metadata_hits = get_kstat_value(ksp, "prefetch_metadata_hits");
114
115         demand_data_misses       = get_kstat_value(ksp, "demand_data_misses");
116         demand_metadata_misses   = get_kstat_value(ksp, "demand_metadata_misses");
117         prefetch_data_misses     = get_kstat_value(ksp, "prefetch_data_misses");
118         prefetch_metadata_misses = get_kstat_value(ksp, "prefetch_metadata_misses");
119
120         hits   = get_kstat_value(ksp, "hits");
121         misses = get_kstat_value(ksp, "misses");
122
123         l2_size        = get_kstat_value(ksp, "l2_size");
124         l2_read_bytes  = get_kstat_value(ksp, "l2_read_bytes");
125         l2_write_bytes = get_kstat_value(ksp, "l2_write_bytes");
126         l2_hits        = get_kstat_value(ksp, "l2_hits");
127         l2_misses      = get_kstat_value(ksp, "l2_misses");
128
129
130         za_submit_size (arcsize, targetsize, minlimit, maxlimit);
131         za_submit_gauge ("arc_l2_size", "", l2_size);
132
133         za_submit_counts ("hits",   demand_data_hits,     demand_metadata_hits,
134                                     prefetch_data_hits,   prefetch_metadata_hits);
135         za_submit_counts ("misses", demand_data_misses,   demand_metadata_misses,
136                                     prefetch_data_misses, prefetch_metadata_misses);
137
138         za_submit_gauge ("arc_ratio", "L1", hits / (hits + misses));
139         za_submit_gauge ("arc_ratio", "L2", l2_hits / (l2_hits + l2_misses));
140
141         za_submit_bytes (l2_read_bytes, l2_write_bytes);
142
143         return (0);
144 }
145
146 static int za_init (void) /* {{{ */
147 {
148         ksp = NULL;
149
150         /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
151         if (kc == NULL)
152         {
153                 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
154                 return (-1);
155         }
156
157         return (0);
158 } /* }}} int za_init */
159
160 void module_register (void)
161 {
162         plugin_register_init ("zfs_arc", za_init);
163         plugin_register_read ("zfs_arc", za_read);
164 } /* void module_register */
165
166 /* vmi: set sw=8 noexpandtab fdm=marker : */