]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dbuf_stats.c
MFV r339226 (peter): Record merge of serf-1.3.9.
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / dbuf_stats.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21
22 #include <sys/zfs_context.h>
23 #include <sys/dbuf.h>
24 #include <sys/dmu_objset.h>
25
26 /*
27  * Calculate the index of the arc header for the state, disabled by default.
28  */
29 int zfs_dbuf_state_index = 0;
30
31 /*
32  * ==========================================================================
33  * Dbuf Hash Read Routines
34  * ==========================================================================
35  */
36 typedef struct dbuf_stats_t {
37         kmutex_t                lock;
38         kstat_t                 *kstat;
39         dbuf_hash_table_t       *hash;
40         int                     idx;
41 } dbuf_stats_t;
42
43 static dbuf_stats_t dbuf_stats_hash_table;
44
45 static int
46 dbuf_stats_hash_table_headers(char *buf, size_t size)
47 {
48         size = snprintf(buf, size - 1,
49             "%-88s | %-124s | %s\n"
50             "%-16s %-8s %-8s %-8s %-8s %-8s %-8s %-5s %-5s %5s | "
51             "%-5s %-5s %-6s %-8s %-6s %-8s %-12s "
52             "%-6s %-6s %-6s %-6s %-6s %-8s %-8s %-8s %-5s | "
53             "%-6s %-6s %-8s %-8s %-6s %-6s %-5s %-8s %-8s\n",
54             "dbuf", "arcbuf", "dnode", "pool", "objset", "object", "level",
55             "blkid", "offset", "dbsize", "meta", "state", "dbholds", "list",
56             "atype", "index", "flags", "count", "asize", "access", "mru", "gmru",
57             "mfu", "gmfu", "l2", "l2_dattr", "l2_asize", "l2_comp", "aholds",
58             "dtype", "btype", "data_bs", "meta_bs", "bsize",
59             "lvls", "dholds", "blocks", "dsize");
60         buf[size] = '\0';
61
62         return (0);
63 }
64
65 int
66 __dbuf_stats_hash_table_data(char *buf, size_t size, dmu_buf_impl_t *db)
67 {
68         arc_buf_info_t abi = { 0 };
69         dmu_object_info_t doi = { 0 };
70         dnode_t *dn = DB_DNODE(db);
71
72         if (db->db_buf)
73                 arc_buf_info(db->db_buf, &abi, zfs_dbuf_state_index);
74
75         if (dn)
76                 __dmu_object_info_from_dnode(dn, &doi);
77
78         size = snprintf(buf, size - 1,
79             "%-16s %-8llu %-8lld %-8lld %-8lld %-8llu %-8llu %-5d %-5d %-5lu | "
80             "%-5d %-5d %-6lld 0x%-6x %-6lu %-8llu %-12llu "
81             "%-6lu %-6lu %-6lu %-6lu %-6lu %-8llu %-8llu %-8d %-5lu | "
82             "%-6d %-6d %-8lu %-8lu %-6llu %-6lu %-5lu %-8llu %-8llu\n",
83             /* dmu_buf_impl_t */
84             spa_name(dn->dn_objset->os_spa),
85             (u_longlong_t)dmu_objset_id(db->db_objset),
86             (longlong_t)db->db.db_object,
87             (longlong_t)db->db_level,
88             (longlong_t)db->db_blkid,
89             (u_longlong_t)db->db.db_offset,
90             (u_longlong_t)db->db.db_size,
91             !!dbuf_is_metadata(db),
92             db->db_state,
93             (ulong_t)refcount_count(&db->db_holds),
94             /* arc_buf_info_t */
95             abi.abi_state_type,
96             abi.abi_state_contents,
97             (longlong_t)abi.abi_state_index,
98             abi.abi_flags,
99             (ulong_t)abi.abi_bufcnt,
100             (u_longlong_t)abi.abi_size,
101             (u_longlong_t)abi.abi_access,
102             (ulong_t)abi.abi_mru_hits,
103             (ulong_t)abi.abi_mru_ghost_hits,
104             (ulong_t)abi.abi_mfu_hits,
105             (ulong_t)abi.abi_mfu_ghost_hits,
106             (ulong_t)abi.abi_l2arc_hits,
107             (u_longlong_t)abi.abi_l2arc_dattr,
108             (u_longlong_t)abi.abi_l2arc_asize,
109             abi.abi_l2arc_compress,
110             (ulong_t)abi.abi_holds,
111             /* dmu_object_info_t */
112             doi.doi_type,
113             doi.doi_bonus_type,
114             (ulong_t)doi.doi_data_block_size,
115             (ulong_t)doi.doi_metadata_block_size,
116             (u_longlong_t)doi.doi_bonus_size,
117             (ulong_t)doi.doi_indirection,
118             (ulong_t)refcount_count(&dn->dn_holds),
119             (u_longlong_t)doi.doi_fill_count,
120             (u_longlong_t)doi.doi_max_offset);
121         buf[size] = '\0';
122
123         return (size);
124 }
125
126 static int
127 dbuf_stats_hash_table_data(char *buf, size_t size, void *data)
128 {
129         dbuf_stats_t *dsh = (dbuf_stats_t *)data;
130         dbuf_hash_table_t *h = dsh->hash;
131         dmu_buf_impl_t *db;
132         int length, error = 0;
133
134         ASSERT3S(dsh->idx, >=, 0);
135         ASSERT3S(dsh->idx, <=, h->hash_table_mask);
136         memset(buf, 0, size);
137
138         mutex_enter(DBUF_HASH_MUTEX(h, dsh->idx));
139         for (db = h->hash_table[dsh->idx]; db != NULL; db = db->db_hash_next) {
140                 /*
141                  * Returning ENOMEM will cause the data and header functions
142                  * to be called with a larger scratch buffers.
143                  */
144                 if (size < 512) {
145                         error = ENOMEM;
146                         break;
147                 }
148
149                 mutex_enter(&db->db_mtx);
150                 mutex_exit(DBUF_HASH_MUTEX(h, dsh->idx));
151
152                 length = __dbuf_stats_hash_table_data(buf, size, db);
153                 buf += length;
154                 size -= length;
155
156                 mutex_exit(&db->db_mtx);
157                 mutex_enter(DBUF_HASH_MUTEX(h, dsh->idx));
158         }
159         mutex_exit(DBUF_HASH_MUTEX(h, dsh->idx));
160
161         return (error);
162 }
163
164 static void *
165 dbuf_stats_hash_table_addr(kstat_t *ksp, off_t n)
166 {
167         dbuf_stats_t *dsh = ksp->ks_private;
168
169         ASSERT(MUTEX_HELD(&dsh->lock));
170
171         if (n <= dsh->hash->hash_table_mask) {
172                 dsh->idx = n;
173                 return (dsh);
174         }
175
176         return (NULL);
177 }
178
179 #ifndef __FreeBSD__
180 /*
181  * XXX The FreeBSD SPL is missing support for KSTAT_TYPE_RAW
182  * we can enable this as soon as that's implemented. See the
183  * lindebugfs module for similar callback semantics.
184  */
185 static void
186 dbuf_stats_hash_table_init(dbuf_hash_table_t *hash)
187 {
188         dbuf_stats_t *dsh = &dbuf_stats_hash_table;
189         kstat_t *ksp;
190
191         mutex_init(&dsh->lock, NULL, MUTEX_DEFAULT, NULL);
192         dsh->hash = hash;
193
194         ksp = kstat_create("zfs", 0, "dbufs", "misc",
195             KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
196         dsh->kstat = ksp;
197
198         if (ksp) {
199                 ksp->ks_lock = &dsh->lock;
200                 ksp->ks_ndata = UINT32_MAX;
201                 ksp->ks_private = dsh;
202                 kstat_set_raw_ops(ksp, dbuf_stats_hash_table_headers,
203                     dbuf_stats_hash_table_data, dbuf_stats_hash_table_addr);
204                 kstat_install(ksp);
205         }
206 }
207
208 static void
209 dbuf_stats_hash_table_destroy(void)
210 {
211         dbuf_stats_t *dsh = &dbuf_stats_hash_table;
212         kstat_t *ksp;
213
214         ksp = dsh->kstat;
215         if (ksp)
216                 kstat_delete(ksp);
217
218         mutex_destroy(&dsh->lock);
219 }
220 #else
221 static void
222 dbuf_stats_hash_table_init(dbuf_hash_table_t *hash)
223 {
224 }
225
226 static void
227 dbuf_stats_hash_table_destroy(void)
228 {
229 }
230 #endif
231
232 void
233 dbuf_stats_init(dbuf_hash_table_t *hash)
234 {
235         dbuf_stats_hash_table_init(hash);
236 }
237
238 void
239 dbuf_stats_destroy(void)
240 {
241         dbuf_stats_hash_table_destroy();
242 }