Path: blob/main/sys/contrib/openzfs/module/zfs/dbuf_stats.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122#include <sys/zfs_context.h>23#include <sys/dbuf.h>24#include <sys/dmu_objset.h>2526/*27* Calculate the index of the arc header for the state, disabled by default.28*/29int zfs_dbuf_state_index = 0;3031/*32* ==========================================================================33* Dbuf Hash Read Routines34* ==========================================================================35*/36typedef struct dbuf_stats_t {37kmutex_t lock;38kstat_t *kstat;39dbuf_hash_table_t *hash;40int idx;41} dbuf_stats_t;4243static dbuf_stats_t dbuf_stats_hash_table;4445static int46dbuf_stats_hash_table_headers(char *buf, size_t size)47{48(void) snprintf(buf, size,49"%-105s | %-119s | %s\n"50"%-16s %-8s %-8s %-8s %-8s %-10s %-8s %-8s %-5s %-5s %-7s %3s | "51"%-5s %-5s %-9s %-6s %-8s %-12s "52"%-6s %-6s %-6s %-6s %-6s %-8s %-8s %-8s %-6s | "53"%-6s %-6s %-8s %-8s %-6s %-6s %-6s %-8s %-8s\n",54"dbuf", "arcbuf", "dnode", "pool", "objset", "object", "level",55"blkid", "offset", "dbsize", "usize", "meta", "state", "dbholds",56"dbc", "list", "atype", "flags", "count", "asize", "access",57"mru", "gmru", "mfu", "gmfu", "l2", "l2_dattr", "l2_asize",58"l2_comp", "aholds", "dtype", "btype", "data_bs", "meta_bs",59"bsize", "lvls", "dholds", "blocks", "dsize");6061return (0);62}6364static int65__dbuf_stats_hash_table_data(char *buf, size_t size, dmu_buf_impl_t *db)66{67arc_buf_info_t abi = { 0 };68dmu_object_info_t doi = { 0 };69dnode_t *dn = DB_DNODE(db);70size_t nwritten;7172if (db->db_buf)73arc_buf_info(db->db_buf, &abi, zfs_dbuf_state_index);7475__dmu_object_info_from_dnode(dn, &doi);7677nwritten = snprintf(buf, size,78"%-16s %-8llu %-8lld %-8lld %-8lld %-10llu %-8llu %-8llu "79"%-5d %-5d %-7lu %-3d | %-5d %-5d 0x%-7x %-6lu %-8llu %-12llu "80"%-6lu %-6lu %-6lu %-6lu %-6lu %-8llu %-8llu %-8d %-6lu | "81"%-6d %-6d %-8lu %-8lu %-6llu %-6lu %-6lu %-8llu %-8llu\n",82/* dmu_buf_impl_t */83spa_name(dn->dn_objset->os_spa),84(u_longlong_t)dmu_objset_id(db->db_objset),85(longlong_t)db->db.db_object,86(longlong_t)db->db_level,87(longlong_t)db->db_blkid,88(u_longlong_t)db->db.db_offset,89(u_longlong_t)db->db.db_size,90(u_longlong_t)dmu_buf_user_size(&db->db),91!!dbuf_is_metadata(db),92db->db_state,93(ulong_t)zfs_refcount_count(&db->db_holds),94multilist_link_active(&db->db_cache_link),95/* arc_buf_info_t */96abi.abi_state_type,97abi.abi_state_contents,98abi.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,109abi.abi_l2arc_compress,110(ulong_t)abi.abi_holds,111/* dmu_object_info_t */112doi.doi_type,113doi.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)zfs_refcount_count(&dn->dn_holds),119(u_longlong_t)doi.doi_fill_count,120(u_longlong_t)doi.doi_max_offset);121122if (nwritten >= size)123return (size);124125return (nwritten + 1);126}127128static int129dbuf_stats_hash_table_data(char *buf, size_t size, void *data)130{131dbuf_stats_t *dsh = (dbuf_stats_t *)data;132dbuf_hash_table_t *h = dsh->hash;133dmu_buf_impl_t *db;134int length, error = 0;135136ASSERT3S(dsh->idx, >=, 0);137ASSERT3S(dsh->idx, <=, h->hash_table_mask);138if (size)139buf[0] = 0;140141mutex_enter(DBUF_HASH_MUTEX(h, dsh->idx));142for (db = h->hash_table[dsh->idx]; db != NULL; db = db->db_hash_next) {143/*144* Returning ENOMEM will cause the data and header functions145* to be called with a larger scratch buffers.146*/147if (size < 512) {148error = SET_ERROR(ENOMEM);149break;150}151152mutex_enter(&db->db_mtx);153154if (db->db_state != DB_EVICTING) {155length = __dbuf_stats_hash_table_data(buf, size, db);156buf += length;157size -= length;158}159160mutex_exit(&db->db_mtx);161}162mutex_exit(DBUF_HASH_MUTEX(h, dsh->idx));163164return (error);165}166167static void *168dbuf_stats_hash_table_addr(kstat_t *ksp, loff_t n)169{170dbuf_stats_t *dsh = ksp->ks_private;171172ASSERT(MUTEX_HELD(&dsh->lock));173174if (n <= dsh->hash->hash_table_mask) {175dsh->idx = n;176return (dsh);177}178179return (NULL);180}181182static void183dbuf_stats_hash_table_init(dbuf_hash_table_t *hash)184{185dbuf_stats_t *dsh = &dbuf_stats_hash_table;186kstat_t *ksp;187188mutex_init(&dsh->lock, NULL, MUTEX_DEFAULT, NULL);189dsh->hash = hash;190191ksp = kstat_create("zfs", 0, "dbufs", "misc",192KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);193dsh->kstat = ksp;194195if (ksp) {196ksp->ks_lock = &dsh->lock;197ksp->ks_ndata = UINT32_MAX;198ksp->ks_private = dsh;199kstat_set_raw_ops(ksp, dbuf_stats_hash_table_headers,200dbuf_stats_hash_table_data, dbuf_stats_hash_table_addr);201kstat_install(ksp);202}203}204205static void206dbuf_stats_hash_table_destroy(void)207{208dbuf_stats_t *dsh = &dbuf_stats_hash_table;209kstat_t *ksp;210211ksp = dsh->kstat;212if (ksp)213kstat_delete(ksp);214215mutex_destroy(&dsh->lock);216}217218void219dbuf_stats_init(dbuf_hash_table_t *hash)220{221dbuf_stats_hash_table_init(hash);222}223224void225dbuf_stats_destroy(void)226{227dbuf_stats_hash_table_destroy();228}229230ZFS_MODULE_PARAM(zfs, zfs_, dbuf_state_index, INT, ZMOD_RW,231"Calculate arc header index");232233234