Line 0
Link Here
|
|
|
1 |
--- src/zfs_arc.c.orig 2012-11-11 11:43:05.000000000 +0100 |
2 |
+++ src/zfs_arc.c 2012-12-31 00:59:33.000000000 +0100 |
3 |
@@ -23,11 +23,58 @@ |
4 |
#include "common.h" |
5 |
#include "plugin.h" |
6 |
|
7 |
+#if !defined(HAVE_LIBKSTAT) && defined(HAVE_SYSCTLBYNAME) |
8 |
+#include <sys/sysctl.h> |
9 |
+#endif |
10 |
+ |
11 |
/* |
12 |
* Global variables |
13 |
*/ |
14 |
|
15 |
-extern kstat_ctl_t *kc; |
16 |
+#if defined(HAVE_LIBKSTAT) |
17 |
+ extern kstat_ctl_t *kc; |
18 |
+#endif |
19 |
+ |
20 |
+/* |
21 |
+ * Context |
22 |
+ */ |
23 |
+struct za_context { |
24 |
+#if defined(HAVE_LIBKSTAT) |
25 |
+ kstat_t *ksp; |
26 |
+#endif |
27 |
+}; |
28 |
+ |
29 |
+static int za_init_context(struct za_context *ctx) |
30 |
+{ |
31 |
+#if defined(HAVE_LIBKSTAT) |
32 |
+ get_kstat (&ctx->ksp, "zfs", 0, "arcstats"); |
33 |
+ if (ctx->ksp == NULL) |
34 |
+ { |
35 |
+ ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat."); |
36 |
+ return (-1); |
37 |
+ } |
38 |
+#endif |
39 |
+ return 0; |
40 |
+} |
41 |
+ |
42 |
+static long long za_get_value(struct za_context *ctx, const char* name) |
43 |
+{ |
44 |
+#if defined(HAVE_LIBKSTAT) |
45 |
+ return get_kstat_value(ctx->ksp, name); |
46 |
+#elif defined(HAVE_SYSCTLBYNAME) |
47 |
+ // kstat values are available on FreeBSD through sysctl |
48 |
+ char fullname[512]; |
49 |
+ long long result = 0; |
50 |
+ size_t size = sizeof(result); |
51 |
+ ssnprintf(fullname, sizeof(fullname), "kstat.zfs.misc.arcstats.%s", name); |
52 |
+ if (sysctlbyname(fullname, &result, &size, NULL, 0) != 0 || size != sizeof(result)) |
53 |
+ { |
54 |
+ ERROR ("zfs_arc plugin: Cannot find stats using sysctl"); |
55 |
+ result = 0; |
56 |
+ } |
57 |
+ return result; |
58 |
+#endif |
59 |
+} |
60 |
|
61 |
static void za_submit (const char* type, const char* type_instance, value_t* values, int values_len) |
62 |
{ |
63 |
@@ -90,42 +137,40 @@ |
64 |
|
65 |
static int za_read (void) |
66 |
{ |
67 |
- gauge_t arcsize, targetsize, minlimit, maxlimit, hits, misses, l2_size, l2_hits, l2_misses; |
68 |
- counter_t demand_data_hits, demand_metadata_hits, prefetch_data_hits, prefetch_metadata_hits; |
69 |
- counter_t demand_data_misses, demand_metadata_misses, prefetch_data_misses, prefetch_metadata_misses; |
70 |
- counter_t l2_read_bytes, l2_write_bytes; |
71 |
- kstat_t *ksp = NULL; |
72 |
+ long long arcsize, targetsize, minlimit, maxlimit, hits, misses, l2_size, l2_hits, l2_misses; |
73 |
+ long long demand_data_hits, demand_metadata_hits, prefetch_data_hits, prefetch_metadata_hits; |
74 |
+ long long demand_data_misses, demand_metadata_misses, prefetch_data_misses, prefetch_metadata_misses; |
75 |
+ long long l2_read_bytes, l2_write_bytes; |
76 |
+ struct za_context ctx; |
77 |
|
78 |
- get_kstat (&ksp, "zfs", 0, "arcstats"); |
79 |
- if (ksp == NULL) |
80 |
+ if (za_init_context (&ctx) < 0) |
81 |
{ |
82 |
- ERROR ("zfs_arc plugin: Cannot find zfs:0:arcstats kstat."); |
83 |
return (-1); |
84 |
} |
85 |
|
86 |
- arcsize = get_kstat_value(ksp, "size"); |
87 |
- targetsize = get_kstat_value(ksp, "c"); |
88 |
- minlimit = get_kstat_value(ksp, "c_min"); |
89 |
- maxlimit = get_kstat_value(ksp, "c_max"); |
90 |
- |
91 |
- demand_data_hits = get_kstat_value(ksp, "demand_data_hits"); |
92 |
- demand_metadata_hits = get_kstat_value(ksp, "demand_metadata_hits"); |
93 |
- prefetch_data_hits = get_kstat_value(ksp, "prefetch_data_hits"); |
94 |
- prefetch_metadata_hits = get_kstat_value(ksp, "prefetch_metadata_hits"); |
95 |
- |
96 |
- demand_data_misses = get_kstat_value(ksp, "demand_data_misses"); |
97 |
- demand_metadata_misses = get_kstat_value(ksp, "demand_metadata_misses"); |
98 |
- prefetch_data_misses = get_kstat_value(ksp, "prefetch_data_misses"); |
99 |
- prefetch_metadata_misses = get_kstat_value(ksp, "prefetch_metadata_misses"); |
100 |
- |
101 |
- hits = get_kstat_value(ksp, "hits"); |
102 |
- misses = get_kstat_value(ksp, "misses"); |
103 |
- |
104 |
- l2_size = get_kstat_value(ksp, "l2_size"); |
105 |
- l2_read_bytes = get_kstat_value(ksp, "l2_read_bytes"); |
106 |
- l2_write_bytes = get_kstat_value(ksp, "l2_write_bytes"); |
107 |
- l2_hits = get_kstat_value(ksp, "l2_hits"); |
108 |
- l2_misses = get_kstat_value(ksp, "l2_misses"); |
109 |
+ arcsize = za_get_value(&ctx, "size"); |
110 |
+ targetsize = za_get_value(&ctx, "c"); |
111 |
+ minlimit = za_get_value(&ctx, "c_min"); |
112 |
+ maxlimit = za_get_value(&ctx, "c_max"); |
113 |
+ |
114 |
+ demand_data_hits = za_get_value(&ctx, "demand_data_hits"); |
115 |
+ demand_metadata_hits = za_get_value(&ctx, "demand_metadata_hits"); |
116 |
+ prefetch_data_hits = za_get_value(&ctx, "prefetch_data_hits"); |
117 |
+ prefetch_metadata_hits = za_get_value(&ctx, "prefetch_metadata_hits"); |
118 |
+ |
119 |
+ demand_data_misses = za_get_value(&ctx, "demand_data_misses"); |
120 |
+ demand_metadata_misses = za_get_value(&ctx, "demand_metadata_misses"); |
121 |
+ prefetch_data_misses = za_get_value(&ctx, "prefetch_data_misses"); |
122 |
+ prefetch_metadata_misses = za_get_value(&ctx, "prefetch_metadata_misses"); |
123 |
+ |
124 |
+ hits = za_get_value(&ctx, "hits"); |
125 |
+ misses = za_get_value(&ctx, "misses"); |
126 |
+ |
127 |
+ l2_size = za_get_value(&ctx, "l2_size"); |
128 |
+ l2_read_bytes = za_get_value(&ctx, "l2_read_bytes"); |
129 |
+ l2_write_bytes = za_get_value(&ctx, "l2_write_bytes"); |
130 |
+ l2_hits = za_get_value(&ctx, "l2_hits"); |
131 |
+ l2_misses = za_get_value(&ctx, "l2_misses"); |
132 |
|
133 |
|
134 |
za_submit_size (arcsize, targetsize, minlimit, maxlimit); |
135 |
@@ -146,12 +191,23 @@ |
136 |
|
137 |
static int za_init (void) /* {{{ */ |
138 |
{ |
139 |
- /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */ |
140 |
- if (kc == NULL) |
141 |
+#if defined(HAVE_LIBKSTAT) |
142 |
+ /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */ |
143 |
+ if (kc == NULL) |
144 |
+ { |
145 |
+ ERROR ("zfs_arc plugin: kstat chain control structure not available."); |
146 |
+ return (-1); |
147 |
+ } |
148 |
+#elif defined(HAVE_SYSCTLBYNAME) |
149 |
+ /* make sure ARC is available (arc_size is not null) */ |
150 |
+ unsigned long long arc_size; |
151 |
+ size_t size = sizeof(arc_size); |
152 |
+ if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arc_size, &size, NULL, 0) != 0 || arc_size == 0) |
153 |
{ |
154 |
- ERROR ("zfs_arc plugin: kstat chain control structure not available."); |
155 |
+ ERROR ("zfs_arc plugin: could not get ARC size using sysctl, is ARC enabled?"); |
156 |
return (-1); |
157 |
} |
158 |
+#endif |
159 |
|
160 |
return (0); |
161 |
} /* }}} int za_init */ |