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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 /*
27 * Global variables
28 */
30 extern kstat_ctl_t *kc;
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;
36 vl.values = values;
37 vl.values_len = values_len;
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));
44 plugin_dispatch_values (&vl);
45 }
47 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
48 {
49 value_t values[1];
51 values[0].gauge = value;
53 za_submit (type, type_instance, values, STATIC_ARRAY_SIZE(values));
54 }
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];
60 values[0].gauge = size;
61 values[1].gauge = size_target;
62 values[2].gauge = limit_min;
63 values[3].gauge = limit_max;
65 za_submit ("arc_size", "", values, STATIC_ARRAY_SIZE(values));
66 }
68 static void za_submit_bytes (counter_t read, counter_t write)
69 {
70 value_t values[2];
72 values[0].counter = read;
73 values[1].counter = write;
75 za_submit ("arc_l2_bytes", "", values, STATIC_ARRAY_SIZE(values));
76 }
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];
83 values[0].counter = demand_data;
84 values[1].counter = demand_metadata;
85 values[2].counter = prefetch_data;
86 values[3].counter = prefetch_metadata;
88 za_submit ("arc_counts", type_instance, values, STATIC_ARRAY_SIZE(values));
89 }
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 kstat_t *ksp = NULL;
99 get_kstat (&ksp, "zfs", 0, "arcstats");
100 if (ksp == NULL)
101 {
102 ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat.");
103 return (-1);
104 }
106 arcsize = get_kstat_value(ksp, "size");
107 targetsize = get_kstat_value(ksp, "c");
108 minlimit = get_kstat_value(ksp, "c_min");
109 maxlimit = get_kstat_value(ksp, "c_max");
111 demand_data_hits = get_kstat_value(ksp, "demand_data_hits");
112 demand_metadata_hits = get_kstat_value(ksp, "demand_metadata_hits");
113 prefetch_data_hits = get_kstat_value(ksp, "prefetch_data_hits");
114 prefetch_metadata_hits = get_kstat_value(ksp, "prefetch_metadata_hits");
116 demand_data_misses = get_kstat_value(ksp, "demand_data_misses");
117 demand_metadata_misses = get_kstat_value(ksp, "demand_metadata_misses");
118 prefetch_data_misses = get_kstat_value(ksp, "prefetch_data_misses");
119 prefetch_metadata_misses = get_kstat_value(ksp, "prefetch_metadata_misses");
121 hits = get_kstat_value(ksp, "hits");
122 misses = get_kstat_value(ksp, "misses");
124 l2_size = get_kstat_value(ksp, "l2_size");
125 l2_read_bytes = get_kstat_value(ksp, "l2_read_bytes");
126 l2_write_bytes = get_kstat_value(ksp, "l2_write_bytes");
127 l2_hits = get_kstat_value(ksp, "l2_hits");
128 l2_misses = get_kstat_value(ksp, "l2_misses");
131 za_submit_size (arcsize, targetsize, minlimit, maxlimit);
132 za_submit_gauge ("arc_l2_size", "", l2_size);
134 za_submit_counts ("hits", demand_data_hits, demand_metadata_hits,
135 prefetch_data_hits, prefetch_metadata_hits);
136 za_submit_counts ("misses", demand_data_misses, demand_metadata_misses,
137 prefetch_data_misses, prefetch_metadata_misses);
139 za_submit_gauge ("arc_ratio", "L1", hits / (hits + misses));
140 za_submit_gauge ("arc_ratio", "L2", l2_hits / (l2_hits + l2_misses));
142 za_submit_bytes (l2_read_bytes, l2_write_bytes);
144 return (0);
145 }
147 static int za_init (void) /* {{{ */
148 {
149 /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
150 if (kc == NULL)
151 {
152 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
153 return (-1);
154 }
156 return (0);
157 } /* }}} int za_init */
159 void module_register (void)
160 {
161 plugin_register_init ("zfs_arc", za_init);
162 plugin_register_read ("zfs_arc", za_read);
163 } /* void module_register */
165 /* vmi: set sw=8 noexpandtab fdm=marker : */