1 /*- 2 * Copyright (c) 2006 Kip Macy kmacy@FreeBSD.org 3 * Copyright (c) 2006 Kris Kennaway kris@FreeBSD.org 4 * Copyright (c) 2006 Dag-Erling Smorgrav des@des.no 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHAL THE AUTHORS BE LIABLE FOR ANY 19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 31 #ifndef _SYS_LOCK_PROFILE_H_ 32 #define _SYS_LOCK_PROFILE_H_ 33 34 #ifdef LOCK_PROFILING 35 #include <sys/stdint.h> 36 #include <sys/ktr.h> 37 #include <sys/mutex.h> 38 #include <machine/atomic.h> 39 #include <machine/cpufunc.h> 40 41 #ifndef LPROF_HASH_SIZE 42 #define LPROF_HASH_SIZE 4096 43 #define LPROF_HASH_MASK (LPROF_HASH_SIZE - 1) 44 #endif 45 46 #ifndef USE_CPU_NANOSECONDS 47 u_int64_t nanoseconds(void); 48 #endif 49 50 struct lock_prof { 51 const char *name; 52 const char *type; 53 const char *file; 54 u_int namehash; 55 int line; 56 uintmax_t cnt_max; 57 uintmax_t cnt_tot; 58 uintmax_t cnt_wait; 59 uintmax_t cnt_cur; 60 uintmax_t cnt_contest_holding; 61 uintmax_t cnt_contest_locking; 62 }; 63 64 extern struct lock_prof lprof_buf[LPROF_HASH_SIZE]; 65 #define LPROF_SBUF_SIZE 256 * 400 66 67 /* We keep a smaller pool of spin mutexes for protecting the lprof hash entries */ 68 #define LPROF_LOCK_SIZE 16 69 #define LPROF_LOCK_MASK (LPROF_LOCK_SIZE - 1) 70 #define LPROF_LHASH(hash) ((hash) & LPROF_LOCK_MASK) 71 72 #define LPROF_LOCK(hash) mtx_lock_spin(&lprof_locks[LPROF_LHASH(hash)]) 73 #define LPROF_UNLOCK(hash) mtx_unlock_spin(&lprof_locks[LPROF_LHASH(hash)]) 74 75 #ifdef _KERNEL 76 extern struct mtx lprof_locks[LPROF_LOCK_SIZE]; 77 extern int lock_prof_enable; 78 79 void _lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime, const char *file, int line); 80 void _lock_profile_update_wait(struct lock_object *lo, uint64_t waitstart); 81 void _lock_profile_release_lock(struct lock_object *lo); 82 83 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) { 84 const char *p; 85 u_int hash = 0; 86 struct lock_profile_object *l = &lo->lo_profile_obj; 87 88 l->lpo_acqtime = 0; 89 l->lpo_waittime = 0; 90 l->lpo_filename = NULL; 91 l->lpo_lineno = 0; 92 l->lpo_contest_holding = 0; 93 l->lpo_contest_locking = 0; 94 l->lpo_type = class->lc_name; 95 96 /* Hash the mutex name to an int so we don't have to strcmp() it repeatedly */ 97 for (p = name; *p != '\0'; p++) 98 hash = 31 * hash + *p; 99 l->lpo_namehash = hash; 100 #if 0 101 if (opts & MTX_PROFILE) 102 l->lpo_stack = stack_create(); 103 #endif 104 } 105 106 107 static inline void 108 lock_profile_object_destroy(struct lock_object *lo) 109 { 110 #if 0 111 struct lock_profile_object *l = &lo->lo_profile_obj; 112 if (lo->lo_flags & LO_PROFILE) 113 stack_destroy(l->lpo_stack); 114 #endif 115 } 116 117 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested, 118 uint64_t *waittime) 119 { 120 struct lock_profile_object *l = &lo->lo_profile_obj; 121 122 if (lock_prof_enable && *contested == 0) { 123 *waittime = nanoseconds(); 124 atomic_add_int(&l->lpo_contest_holding, 1); 125 *contested = 1; 126 } 127 } 128 129 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime, const char *file, int line) 130 { 131 132 /* don't reset the timer when/if recursing */ 133 if (lock_prof_enable && lo->lo_profile_obj.lpo_acqtime == 0) { 134 #ifdef LOCK_PROFILING_FAST 135 if (contested == 0) 136 return; 137 #endif 138 _lock_profile_obtain_lock_success(lo, contested, waittime, file, line); 139 } 140 } 141 static inline void lock_profile_release_lock(struct lock_object *lo) 142 { 143 struct lock_profile_object *l = &lo->lo_profile_obj; 144 145 if (l->lpo_acqtime) 146 _lock_profile_release_lock(lo); 147 } 148 149 #endif /* _KERNEL */ 150 151 #else /* !LOCK_PROFILING */ 152 153 #ifdef _KERNEL 154 static inline void lock_profile_update_wait(struct lock_object *lo, uint64_t waitstart) {;} 155 static inline void lock_profile_update_contest_locking(struct lock_object *lo, int contested) {;} 156 static inline void lock_profile_release_lock(struct lock_object *lo) {;} 157 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested, uint64_t *waittime) {;} 158 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime, 159 const char *file, int line) {;} 160 static inline void lock_profile_object_destroy(struct lock_object *lo) {;} 161 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) {;} 162 163 #endif /* _KERNEL */ 164 165 #endif /* !LOCK_PROFILING */ 166 167 #endif /* _SYS_LOCK_PROFILE_H_ */ 168