Code

zfs_arc plugin: Add copyright header for Aurelien Rougemont.
[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  **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 /*
29  * Global variables
30  */
31 static kstat_t *ksp;
32 extern kstat_ctl_t *kc;
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;
38         vl.values = values;
39         vl.values_len = values_len;
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));
46         plugin_dispatch_values (&vl);
47 }
49 static void za_submit_gauge (const char* type, const char* type_instance, gauge_t value)
50 {
51         value_t vv;
53         vv.gauge = value;
54         za_submit (type, type_instance, &vv, 1);
55 }
57 static void za_submit_derive (const char* type, const char* type_instance, derive_t dv)
58 {
59         value_t vv;
61         vv.derive = dv;
62         za_submit (type, type_instance, &vv, 1);
63 }
65 static void za_submit_ratio (const char* type_instance, gauge_t hits, gauge_t misses)
66 {
67         gauge_t ratio = NAN;
69         if (!isfinite (hits) || (hits < 0.0))
70                 hits = 0.0;
71         if (!isfinite (misses) || (misses < 0.0))
72                 misses = 0.0;
74         if ((hits != 0.0) || (misses != 0.0))
75                 ratio = hits / (hits + misses);
77         za_submit_gauge ("cache_ratio", type_instance, ratio);
78 }
80 static int za_read (void)
81 {
82         gauge_t  arc_size, l2_size;
83         derive_t demand_data_hits,
84                  demand_metadata_hits,
85                  prefetch_data_hits,
86                  prefetch_metadata_hits,
87                  demand_data_misses,
88                  demand_metadata_misses,
89                  prefetch_data_misses,
90                  prefetch_metadata_misses;
91         gauge_t  arc_hits, arc_misses, l2_hits, l2_misses;
92         value_t  l2_io[2];
93         derive_t mutex_miss;
94         derive_t allocated, deleted, stolen;
95         derive_t evict_l2_cached, evict_l2_eligible, evict_l2_ineligible;
96         derive_t hash_collisions;
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         }
105         /* Sizes */
106         arc_size   = get_kstat_value(ksp, "size");
107         l2_size    = get_kstat_value(ksp, "l2_size");
109         za_submit_gauge ("cache_size", "arc", arc_size);
110         za_submit_gauge ("cache_size", "L2", l2_size);
112         mutex_miss = get_kstat_value (ksp, "mutex_miss");
113         za_submit_derive ("mutex_operation", "miss", mutex_miss);
115         allocated = get_kstat_value(ksp, "allocated");
116         deleted   = get_kstat_value(ksp, "deleted");
117         stolen    = get_kstat_value(ksp, "stolen");
118         za_submit_derive ("cache_operation", "allocated", allocated);
119         za_submit_derive ("cache_operation", "deleted",   deleted);
120         za_submit_derive ("cache_operation", "stolen",    stolen);
122         hash_collisions = get_kstat_value(ksp, "hash_collisions");
123         za_submit_derive ("hash_collisions", "", hash_collisions);
124         
125         evict_l2_cached          = get_kstat_value(ksp, "evict_l2_cached");
126         evict_l2_eligible        = get_kstat_value(ksp, "evict_l2_eligible");
127         evict_l2_ineligible      = get_kstat_value(ksp, "evict_l2_ineligible");
128         
129         za_submit_derive ("cache_eviction", "cached",     evict_l2_cached);
130         za_submit_derive ("cache_eviction", "eligible",   evict_l2_eligible);
131         za_submit_derive ("cache_eviction", "ineligible", evict_l2_ineligible);
133         /* Hits / misses */
134         demand_data_hits       = get_kstat_value(ksp, "demand_data_hits");
135         demand_metadata_hits   = get_kstat_value(ksp, "demand_metadata_hits");
136         prefetch_data_hits     = get_kstat_value(ksp, "prefetch_data_hits");
137         prefetch_metadata_hits = get_kstat_value(ksp, "prefetch_metadata_hits");
139         demand_data_misses       = get_kstat_value(ksp, "demand_data_misses");
140         demand_metadata_misses   = get_kstat_value(ksp, "demand_metadata_misses");
141         prefetch_data_misses     = get_kstat_value(ksp, "prefetch_data_misses");
142         prefetch_metadata_misses = get_kstat_value(ksp, "prefetch_metadata_misses");
144         za_submit_derive ("cache_result", "demand_data-hit",       demand_data_hits);
145         za_submit_derive ("cache_result", "demand_metadata-hit",   demand_metadata_hits);
146         za_submit_derive ("cache_result", "prefetch_data-hit",     prefetch_data_hits);
147         za_submit_derive ("cache_result", "prefetch_metadata-hit", prefetch_metadata_hits);
149         za_submit_derive ("cache_result", "demand_data-miss",       demand_data_misses);
150         za_submit_derive ("cache_result", "demand_metadata-miss",   demand_metadata_misses);
151         za_submit_derive ("cache_result", "prefetch_data-miss",     prefetch_data_misses);
152         za_submit_derive ("cache_result", "prefetch_metadata-miss", prefetch_metadata_misses);
154         /* Ratios */
155         arc_hits   = (gauge_t) get_kstat_value(ksp, "hits");
156         arc_misses = (gauge_t) get_kstat_value(ksp, "misses");
157         l2_hits    = (gauge_t) get_kstat_value(ksp, "l2_hits");
158         l2_misses  = (gauge_t) get_kstat_value(ksp, "l2_misses");
160         za_submit_ratio ("arc", arc_hits, arc_misses);
161         za_submit_ratio ("L2", l2_hits, l2_misses);
163         /* I/O */
164         l2_io[0].derive = get_kstat_value(ksp, "l2_read_bytes");
165         l2_io[1].derive = get_kstat_value(ksp, "l2_write_bytes");
167         za_submit ("io_octets", "L2", l2_io, /* num values = */ 2);
169         return (0);
170 } /* int za_read */
172 static int za_init (void) /* {{{ */
174         ksp = NULL;
176         /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
177         if (kc == NULL)
178         {
179                 ERROR ("zfs_arc plugin: kstat chain control structure not available.");
180                 return (-1);
181         }
183         return (0);
184 } /* }}} int za_init */
186 void module_register (void)
188         plugin_register_init ("zfs_arc", za_init);
189         plugin_register_read ("zfs_arc", za_read);
190 } /* void module_register */
192 /* vmi: set sw=8 noexpandtab fdm=marker : */