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
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
31 #ifndef _SYS_LOCK_PROFILE_H_
32 #define _SYS_LOCK_PROFILE_H_
35 #include <sys/stdint.h>
37 #include <sys/mutex.h>
38 #include <machine/atomic.h>
39 #include <machine/cpufunc.h>
41 #ifndef LPROF_HASH_SIZE
42 #define LPROF_HASH_SIZE 4096
43 #define LPROF_HASH_MASK (LPROF_HASH_SIZE - 1)
46 #ifndef USE_CPU_NANOSECONDS
47 u_int64_t nanoseconds(void);
60 uintmax_t cnt_contest_holding;
61 uintmax_t cnt_contest_locking;
64 extern struct lock_prof lprof_buf[LPROF_HASH_SIZE];
65 #define LPROF_SBUF_SIZE 256 * 400
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)
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)])
76 extern struct mtx lprof_locks[LPROF_LOCK_SIZE];
77 extern int lock_prof_enable;
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);
83 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) {
86 struct lock_profile_object *l = &lo->lo_profile_obj;
90 l->lpo_filename = NULL;
92 l->lpo_contest_holding = 0;
93 l->lpo_contest_locking = 0;
94 l->lpo_type = class->lc_name;
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;
101 if (opts & MTX_PROFILE)
102 l->lpo_stack = stack_create();
108 lock_profile_object_destroy(struct lock_object *lo)
111 struct lock_profile_object *l = &lo->lo_profile_obj;
112 if (lo->lo_flags & LO_PROFILE)
113 stack_destroy(l->lpo_stack);
117 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested,
120 struct lock_profile_object *l = &lo->lo_profile_obj;
122 if (!(lo->lo_flags & LO_NOPROFILE) && lock_prof_enable &&
124 *waittime = nanoseconds();
125 atomic_add_int(&l->lpo_contest_holding, 1);
130 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime, const char *file, int line)
133 /* don't reset the timer when/if recursing */
134 if (!(lo->lo_flags & LO_NOPROFILE) && lock_prof_enable &&
135 lo->lo_profile_obj.lpo_acqtime == 0) {
136 #ifdef LOCK_PROFILING_FAST
140 _lock_profile_obtain_lock_success(lo, contested, waittime, file, line);
143 static inline void lock_profile_release_lock(struct lock_object *lo)
145 struct lock_profile_object *l = &lo->lo_profile_obj;
147 if (!(lo->lo_flags & LO_NOPROFILE) && l->lpo_acqtime)
148 _lock_profile_release_lock(lo);
153 #else /* !LOCK_PROFILING */
156 static inline void lock_profile_update_wait(struct lock_object *lo, uint64_t waitstart) {;}
157 static inline void lock_profile_update_contest_locking(struct lock_object *lo, int contested) {;}
158 static inline void lock_profile_release_lock(struct lock_object *lo) {;}
159 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested, uint64_t *waittime) {;}
160 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime,
161 const char *file, int line) {;}
162 static inline void lock_profile_object_destroy(struct lock_object *lo) {;}
163 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) {;}
167 #endif /* !LOCK_PROFILING */
169 #endif /* _SYS_LOCK_PROFILE_H_ */