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)])
75 extern struct mtx lprof_locks[LPROF_LOCK_SIZE];
76 extern int lock_prof_enable;
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);
82 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) {
85 struct lock_profile_object *l = &lo->lo_profile_obj;
89 l->lpo_filename = NULL;
91 l->lpo_contest_holding = 0;
92 l->lpo_contest_locking = 0;
93 l->lpo_type = class->lc_name;
95 /* Hash the mutex name to an int so we don't have to strcmp() it repeatedly */
96 for (p = name; *p != '\0'; p++)
97 hash = 31 * hash + *p;
98 l->lpo_namehash = hash;
100 if (opts & MTX_PROFILE)
101 l->lpo_stack = stack_create();
107 lock_profile_object_destroy(struct lock_object *lo)
110 struct lock_profile_object *l = &lo->lo_profile_obj;
111 if (lo->lo_flags & LO_PROFILE)
112 stack_destroy(l->lpo_stack);
116 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested,
119 struct lock_profile_object *l = &lo->lo_profile_obj;
121 if (lock_prof_enable && *contested == 0) {
122 *waittime = nanoseconds();
123 atomic_add_int(&l->lpo_contest_holding, 1);
128 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime, const char *file, int line)
131 /* don't reset the timer when/if recursing */
132 if (lock_prof_enable && lo->lo_profile_obj.lpo_acqtime == 0) {
133 #ifdef LOCK_PROFILING_FAST
137 _lock_profile_obtain_lock_success(lo, contested, waittime, file, line);
140 static inline void lock_profile_release_lock(struct lock_object *lo)
142 struct lock_profile_object *l = &lo->lo_profile_obj;
145 _lock_profile_release_lock(lo);
148 #else /* !LOCK_PROFILING */
149 static inline void lock_profile_update_wait(struct lock_object *lo, uint64_t waitstart) {;}
150 static inline void lock_profile_update_contest_locking(struct lock_object *lo, int contested) {;}
151 static inline void lock_profile_release_lock(struct lock_object *lo) {;}
152 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested, uint64_t *waittime) {;}
153 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime,
154 const char *file, int line) {;}
155 static inline void lock_profile_object_destroy(struct lock_object *lo) {;}
156 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) {;}
158 #endif /* !LOCK_PROFILING */
160 #endif /* _SYS_LOCK_PROFILE_H_ */