xref: /freebsd/sys/sys/lock_profile.h (revision 57c4583f70ab9d25b3aed17f20ec7843f9673539)
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/cpufunc.h>
39 
40 #ifndef LPROF_HASH_SIZE
41 #define LPROF_HASH_SIZE		4096
42 #define LPROF_HASH_MASK		(LPROF_HASH_SIZE - 1)
43 #endif
44 
45 #ifndef USE_CPU_NANOSECONDS
46 u_int64_t nanoseconds(void);
47 #endif
48 
49 struct lock_prof {
50 	const char	*name;
51 	const char      *type;
52 	const char	*file;
53 	u_int		 namehash;
54 	int		line;
55 	uintmax_t	cnt_max;
56 	uintmax_t	cnt_tot;
57 	uintmax_t       cnt_wait;
58 	uintmax_t	cnt_cur;
59 	uintmax_t	cnt_contest_holding;
60 	uintmax_t	cnt_contest_locking;
61 };
62 
63 extern struct lock_prof lprof_buf[LPROF_HASH_SIZE];
64 extern int allocated_lprof_buf;
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 extern int lock_prof_records;
78 extern int lock_prof_rejected;
79 extern int lock_prof_collisions;
80 
81 void _lock_profile_obtain_lock_success(struct lock_object *lo, uint64_t waittime, const char *file, int line);
82 void _lock_profile_update_wait(struct lock_object *lo, uint64_t waitstart);
83 void _lock_profile_release_lock(struct lock_object *lo);
84 
85 static inline void lock_profile_init(void)
86 {
87         int i;
88         /* Initialize the mutex profiling locks */
89         for (i = 0; i < LPROF_LOCK_SIZE; i++) {
90                 mtx_init(&lprof_locks[i], "mprof lock",
91                     NULL, MTX_SPIN|MTX_QUIET|MTX_NOPROFILE);
92         }
93 }
94 
95 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) {
96 	const char *p;
97 	u_int hash = 0;
98 	struct lock_profile_object *l = &lo->lo_profile_obj;
99 
100 	lo->lo_flags = 0;
101 	lo->lo_name = name;
102 	l->lpo_acqtime = 0;
103 	l->lpo_waittime = 0;
104 	l->lpo_filename = NULL;
105 	l->lpo_lineno = 0;
106 	l->lpo_contest_holding = 0;
107 	l->lpo_contest_locking = 0;
108 	l->lpo_type = class->lc_name;
109 
110 	/* Hash the mutex name to an int so we don't have to strcmp() it repeatedly */
111 	for (p = name; *p != '\0'; p++)
112 		hash = 31 * hash + *p;
113 	l->lpo_namehash = hash;
114 #if 0
115 	if (opts & MTX_PROFILE)
116 		l->lpo_stack = stack_create();
117 #endif
118 }
119 
120 
121 static inline void
122 lock_profile_object_destroy(struct lock_object *lo)
123 {
124 #if 0
125 	struct lock_profile_object *l = &lo->lo_profile_obj;
126 	if (lo->lo_flags & LO_PROFILE)
127 		stack_destroy(l->lpo_stack);
128 #endif
129 }
130 
131 static inline void lock_profile_waitstart(uint64_t *waittime)
132 {
133 	if (lock_prof_enable)
134 		*waittime = nanoseconds();
135 }
136 
137 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested)
138 {
139 	struct lock_profile_object *l = &lo->lo_profile_obj;
140 	if (lock_prof_enable) {
141 		*contested = 1;
142 		atomic_add_int(&l->lpo_contest_holding, 1);
143 	}
144 }
145 
146 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, uint64_t waittime, const char *file, int line)
147 {
148 	if (lock_prof_enable)
149 		_lock_profile_obtain_lock_success(lo, waittime, file, line);
150 }
151 
152 static inline void lock_profile_update_wait(struct lock_object *lo, uint64_t waitstart)
153 {
154 	if (lock_prof_enable)
155 		_lock_profile_update_wait(lo, waitstart);
156 }
157 
158 static inline void lock_profile_release_lock(struct lock_object *lo)
159 {
160 	struct lock_profile_object *l = &lo->lo_profile_obj;
161 	if (lock_prof_enable || l->lpo_acqtime)
162 		_lock_profile_release_lock(lo);
163 }
164 
165 #else /* !LOCK_PROFILING */
166 
167 static inline void lock_profile_init(void) {;}
168 static inline void lock_profile_update_wait(struct lock_object *lo, uint64_t waitstart) {;}
169 static inline void lock_profile_waitstart(uint64_t *waittime) {;}
170 static inline void lock_profile_release_lock(struct lock_object *lo) {;}
171 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested) {;}
172 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, uint64_t waittime,
173 						    const char *file, int line) {;}
174 static inline void lock_profile_object_destroy(struct lock_object *lo) {;}
175 static inline void lock_profile_object_init(struct lock_object *lo, struct lock_class *class, const char *name) {;}
176 
177 #endif  /* !LOCK_PROFILING */
178 
179 #endif /* _SYS_LOCK_PROFILE_H_ */
180