From 1d919413b18837c8f9f6fff10f6cc9893503fce6 Mon Sep 17 00:00:00 2001 From: ozaki-r Date: Fri, 16 Mar 2018 04:37:55 +0000 Subject: [PATCH] Add a new command, show lockstat, which shows statistics of locks Currently the command shows the number of allocated locks. The command is useful only if LOCKDEBUG is enabled. --- share/man/man4/ddb.4 | 8 ++++-- sys/ddb/db_command.c | 18 +++++++++++-- sys/kern/subr_lockdebug.c | 54 +++++++++++++++++++++++++++++++++++++-- sys/sys/lockdebug.h | 3 ++- 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/share/man/man4/ddb.4 b/share/man/man4/ddb.4 index 888d19e2a5aa..ce06d71d3305 100644 --- a/share/man/man4/ddb.4 +++ b/share/man/man4/ddb.4 @@ -1,4 +1,4 @@ -.\" $NetBSD: ddb.4,v 1.174 2018/02/19 10:31:53 wiz Exp $ +.\" $NetBSD: ddb.4,v 1.175 2018/03/16 04:37:55 ozaki-r Exp $ .\" .\" Copyright (c) 1997 - 2009 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -56,7 +56,7 @@ .\" any improvements or extensions that they make and grant Carnegie Mellon .\" the rights to redistribute these changes. .\" -.Dd February 17, 2018 +.Dd March 16, 2018 .Dt DDB 4 .Os .Sh NAME @@ -650,6 +650,10 @@ Display information about a lock at .Ar address . This command is useful only if a kernel is compiled with .Cd options LOCKDEBUG . +.It Ic show lockstat +Display information about statistics of locks. +This command is useful only if a kernel is compiled with +.Cd options LOCKDEBUG . .It Ic show map Ns Oo Cm /f Oc Ar address Print the vm_map at .Ar address . diff --git a/sys/ddb/db_command.c b/sys/ddb/db_command.c index 47b5277d2d26..d0678f03ce32 100644 --- a/sys/ddb/db_command.c +++ b/sys/ddb/db_command.c @@ -1,4 +1,4 @@ -/* $NetBSD: db_command.c,v 1.149 2018/03/04 07:14:50 mlelstv Exp $ */ +/* $NetBSD: db_command.c,v 1.150 2018/03/16 04:37:55 ozaki-r Exp $ */ /* * Copyright (c) 1996, 1997, 1998, 1999, 2002, 2009 The NetBSD Foundation, Inc. @@ -60,7 +60,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.149 2018/03/04 07:14:50 mlelstv Exp $"); +__KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.150 2018/03/16 04:37:55 ozaki-r Exp $"); #ifdef _KERNEL_OPT #include "opt_aio.h" @@ -190,6 +190,7 @@ static void db_event_print_cmd(db_expr_t, bool, db_expr_t, const char *); static void db_fncall(db_expr_t, bool, db_expr_t, const char *); static void db_help_print_cmd(db_expr_t, bool, db_expr_t, const char *); static void db_lock_print_cmd(db_expr_t, bool, db_expr_t, const char *); +static void db_show_lockstat(db_expr_t, bool, db_expr_t, const char *); static void db_mount_print_cmd(db_expr_t, bool, db_expr_t, const char *); static void db_mbuf_print_cmd(db_expr_t, bool, db_expr_t, const char *); static void db_map_print_cmd(db_expr_t, bool, db_expr_t, const char *); @@ -248,6 +249,9 @@ static const struct db_command db_show_cmds[] = { "Print the files open by process at address", "[/f] address", NULL) }, { DDB_ADD_CMD("lock", db_lock_print_cmd, 0,NULL,NULL,NULL) }, + { DDB_ADD_CMD("lockstat", + db_show_lockstat, 0, + "Print statistics of locks", NULL, NULL) }, { DDB_ADD_CMD("map", db_map_print_cmd, 0, "Print the vm_map at address.", "[/f] address",NULL) }, { DDB_ADD_CMD("module", db_show_module_cmd, 0, @@ -1226,6 +1230,16 @@ db_lock_print_cmd(db_expr_t addr, bool have_addr, #endif } +static void +db_show_lockstat(db_expr_t addr, bool have_addr, + db_expr_t count, const char *modif) +{ + +#ifdef _KERNEL /* XXX CRASH(8) */ + lockdebug_show_lockstat(db_printf); +#endif +} + /* * Call random function: * !expr(arg,arg,arg) diff --git a/sys/kern/subr_lockdebug.c b/sys/kern/subr_lockdebug.c index bff6924b29c5..ca325b016070 100644 --- a/sys/kern/subr_lockdebug.c +++ b/sys/kern/subr_lockdebug.c @@ -1,4 +1,4 @@ -/* $NetBSD: subr_lockdebug.c,v 1.60 2018/02/20 03:34:52 ozaki-r Exp $ */ +/* $NetBSD: subr_lockdebug.c,v 1.61 2018/03/16 04:43:37 ozaki-r Exp $ */ /*- * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. @@ -34,7 +34,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.60 2018/02/20 03:34:52 ozaki-r Exp $"); +__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.61 2018/03/16 04:43:37 ozaki-r Exp $"); #ifdef _KERNEL_OPT #include "opt_ddb.h" @@ -838,6 +838,56 @@ lockdebug_lock_print(void *addr, void (*pr)(const char *, ...)) (*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n"); #endif /* LOCKDEBUG */ } + +void +lockdebug_show_lockstat(void (*pr)(const char *, ...)) +{ +#ifdef LOCKDEBUG + lockdebug_t *ld; + void *_ld; + uint32_t n_null = 0; + uint32_t n_spin_mutex = 0; + uint32_t n_adaptive_mutex = 0; + uint32_t n_rwlock = 0; + uint32_t n_cv = 0; + uint32_t n_others = 0; + + RB_TREE_FOREACH(_ld, &ld_rb_tree) { + ld = _ld; + if (ld->ld_lock == NULL) { + n_null++; + continue; + } + if (ld->ld_lockops->lo_type == LOCKOPS_CV) { + n_cv++; + continue; + } + if (ld->ld_lockops->lo_name[0] == 'M') { + if (ld->ld_lockops->lo_type == LOCKOPS_SLEEP) + n_adaptive_mutex++; + else + n_spin_mutex++; + continue; + } + if (ld->ld_lockops->lo_name[0] == 'R') { + n_rwlock++; + continue; + } + n_others++; + } + (*pr)( + "condvar: %u\n" + "spin mutex: %u\n" + "adaptive mutex: %u\n" + "rwlock: %u\n" + "null locks: %u\n" + "others: %u\n", + n_cv, n_spin_mutex, n_adaptive_mutex, n_rwlock, + n_null, n_others); +#else + (*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n"); +#endif /* LOCKDEBUG */ +} #endif /* DDB */ /* diff --git a/sys/sys/lockdebug.h b/sys/sys/lockdebug.h index 2ef6ea4c6d05..7774c951a5b7 100644 --- a/sys/sys/lockdebug.h +++ b/sys/sys/lockdebug.h @@ -1,4 +1,4 @@ -/* $NetBSD: lockdebug.h,v 1.16 2017/09/16 23:54:41 christos Exp $ */ +/* $NetBSD: lockdebug.h,v 1.17 2018/03/16 04:37:55 ozaki-r Exp $ */ /*- * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. @@ -58,6 +58,7 @@ void lockdebug_abort(const char *, size_t, const volatile void *, void lockdebug_lock_print(void *, void (*)(const char *, ...) __printflike(1, 2)); +void lockdebug_show_lockstat(void (*)(const char *, ...) __printflike(1, 2)); #ifdef LOCKDEBUG