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 extern struct mtx lprof_locks[LPROF_LOCK_SIZE]; 76 extern int lock_prof_enable; 77 78 void _lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime, const char *file, int line); 79 void _lock_profile_update_wait(struct lock_object *lo, uint64_t waitstart); 80 void _lock_profile_release_lock(struct lock_object *lo); 81 82 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) { 83 const char *p; 84 u_int hash = 0; 85 struct lock_profile_object *l = &lo->lo_profile_obj; 86 87 lo->lo_flags = 0; 88 lo->lo_name = name; 89 l->lpo_acqtime = 0; 90 l->lpo_waittime = 0; 91 l->lpo_filename = NULL; 92 l->lpo_lineno = 0; 93 l->lpo_contest_holding = 0; 94 l->lpo_contest_locking = 0; 95 l->lpo_type = class->lc_name; 96 97 /* Hash the mutex name to an int so we don't have to strcmp() it repeatedly */ 98 for (p = name; *p != '\0'; p++) 99 hash = 31 * hash + *p; 100 l->lpo_namehash = hash; 101 #if 0 102 if (opts & MTX_PROFILE) 103 l->lpo_stack = stack_create(); 104 #endif 105 } 106 107 108 static inline void 109 lock_profile_object_destroy(struct lock_object *lo) 110 { 111 #if 0 112 struct lock_profile_object *l = &lo->lo_profile_obj; 113 if (lo->lo_flags & LO_PROFILE) 114 stack_destroy(l->lpo_stack); 115 #endif 116 } 117 118 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested, 119 uint64_t *waittime) 120 { 121 struct lock_profile_object *l = &lo->lo_profile_obj; 122 123 if (lock_prof_enable && *contested == 0) { 124 *waittime = nanoseconds(); 125 atomic_add_int(&l->lpo_contest_holding, 1); 126 *contested = 1; 127 } 128 } 129 130 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime, const char *file, int line) 131 { 132 133 /* don't reset the timer when/if recursing */ 134 if (lock_prof_enable && lo->lo_profile_obj.lpo_acqtime == 0) { 135 #ifdef LOCK_PROFILING_FAST 136 if (contested == 0) 137 return; 138 #endif 139 _lock_profile_obtain_lock_success(lo, contested, waittime, file, line); 140 } 141 } 142 static inline void lock_profile_release_lock(struct lock_object *lo) 143 { 144 struct lock_profile_object *l = &lo->lo_profile_obj; 145 146 if (l->lpo_acqtime) 147 _lock_profile_release_lock(lo); 148 } 149 150 #else /* !LOCK_PROFILING */ 151 static inline void lock_profile_update_wait(struct lock_object *lo, uint64_t waitstart) {;} 152 static inline void lock_profile_update_contest_locking(struct lock_object *lo, int contested) {;} 153 static inline void lock_profile_release_lock(struct lock_object *lo) {;} 154 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested, uint64_t *waittime) {;} 155 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime, 156 const char *file, int line) {;} 157 static inline void lock_profile_object_destroy(struct lock_object *lo) {;} 158 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) {;} 159 160 #endif /* !LOCK_PROFILING */ 161 162 #endif /* _SYS_LOCK_PROFILE_H_ */ 163