]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/lock_profile.h
This commit was generated by cvs2svn to compensate for changes in r170754,
[FreeBSD/FreeBSD.git] / sys / sys / lock_profile.h
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         l->lpo_acqtime = 0;
88         l->lpo_waittime = 0;
89         l->lpo_filename = NULL;
90         l->lpo_lineno = 0;
91         l->lpo_contest_holding = 0;
92         l->lpo_contest_locking = 0;
93         l->lpo_type = class->lc_name;
94
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;
99 #if 0
100         if (opts & MTX_PROFILE)
101                 l->lpo_stack = stack_create();
102 #endif
103 }
104
105
106 static inline void 
107 lock_profile_object_destroy(struct lock_object *lo) 
108 {
109 #if 0
110         struct lock_profile_object *l = &lo->lo_profile_obj;
111         if (lo->lo_flags & LO_PROFILE)
112                 stack_destroy(l->lpo_stack);
113 #endif
114 }
115
116 static inline void lock_profile_obtain_lock_failed(struct lock_object *lo, int *contested,
117     uint64_t *waittime) 
118 {
119         struct lock_profile_object *l = &lo->lo_profile_obj;
120
121         if (lock_prof_enable && *contested == 0) {
122                 *waittime = nanoseconds();
123                 atomic_add_int(&l->lpo_contest_holding, 1);
124                 *contested = 1;
125         }
126 }
127
128 static inline void lock_profile_obtain_lock_success(struct lock_object *lo, int contested, uint64_t waittime, const char *file, int line) 
129 {
130         
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
134                if (contested == 0)
135                        return;
136 #endif
137                _lock_profile_obtain_lock_success(lo, contested, waittime, file, line);
138         }
139 }
140 static inline void lock_profile_release_lock(struct lock_object *lo)
141 {
142         struct lock_profile_object *l = &lo->lo_profile_obj;
143
144         if (l->lpo_acqtime) 
145                 _lock_profile_release_lock(lo);
146 }
147
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) {;}
157
158 #endif  /* !LOCK_PROFILING */
159
160 #endif /* _SYS_LOCK_PROFILE_H_ */