]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/isc/pthreads/mutex.c
Update BIND to 9.9.8
[FreeBSD/stable/9.git] / contrib / bind9 / lib / isc / pthreads / mutex.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2008, 2011, 2012, 2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2002  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: mutex.c,v 1.18 2011/01/04 23:47:14 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <time.h>
26 #include <sys/time.h>
27 #include <errno.h>
28
29 #include <isc/mutex.h>
30 #include <isc/util.h>
31 #include <isc/print.h>
32 #include <isc/strerror.h>
33
34 #if ISC_MUTEX_PROFILE
35
36 /*@{*/
37 /*% Operations on timevals; adapted from FreeBSD's sys/time.h */
38 #define timevalclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_usec = 0)
39 #define timevaladd(vvp, uvp)                                            \
40         do {                                                            \
41                 (vvp)->tv_sec += (uvp)->tv_sec;                         \
42                 (vvp)->tv_usec += (uvp)->tv_usec;                       \
43                 if ((vvp)->tv_usec >= 1000000) {                        \
44                         (vvp)->tv_sec++;                                \
45                         (vvp)->tv_usec -= 1000000;                      \
46                 }                                                       \
47         } while (0)
48 #define timevalsub(vvp, uvp)                                            \
49         do {                                                            \
50                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
51                 (vvp)->tv_usec -= (uvp)->tv_usec;                       \
52                 if ((vvp)->tv_usec < 0) {                               \
53                         (vvp)->tv_sec--;                                \
54                         (vvp)->tv_usec += 1000000;                      \
55                 }                                                       \
56         } while (0)
57
58 /*@}*/
59
60 #define ISC_MUTEX_MAX_LOCKERS 32
61
62 typedef struct {
63         const char *            file;
64         int                     line;
65         unsigned                count;
66         struct timeval          locked_total;
67         struct timeval          wait_total;
68 } isc_mutexlocker_t;
69
70 struct isc_mutexstats {
71         const char *            file;   /*%< File mutex was created in. */
72         int                     line;   /*%< Line mutex was created on. */
73         unsigned                count;
74         struct timeval          lock_t;
75         struct timeval          locked_total;
76         struct timeval          wait_total;
77         isc_mutexlocker_t *     cur_locker;
78         isc_mutexlocker_t       lockers[ISC_MUTEX_MAX_LOCKERS];
79 };
80
81 #ifndef ISC_MUTEX_PROFTABLESIZE
82 #define ISC_MUTEX_PROFTABLESIZE (1024 * 1024)
83 #endif
84 static isc_mutexstats_t stats[ISC_MUTEX_PROFTABLESIZE];
85 static int stats_next = 0;
86 static isc_boolean_t stats_init = ISC_FALSE;
87 static pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;
88
89
90 isc_result_t
91 isc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
92         int i, err;
93
94         err = pthread_mutex_init(&mp->mutex, NULL);
95         if (err == ENOMEM)
96                 return (ISC_R_NOMEMORY);
97         if (err != 0)
98                 return (ISC_R_UNEXPECTED);
99
100         RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);
101
102         if (stats_init == ISC_FALSE)
103                 stats_init = ISC_TRUE;
104
105         /*
106          * If all statistics entries have been used, give up and trigger an
107          * assertion failure.  There would be no other way to deal with this
108          * because we'd like to keep record of all locks for the purpose of
109          * debugging and the number of necessary locks is unpredictable.
110          * If this failure is triggered while debugging, named should be
111          * rebuilt with an increased ISC_MUTEX_PROFTABLESIZE.
112          */
113         RUNTIME_CHECK(stats_next < ISC_MUTEX_PROFTABLESIZE);
114         mp->stats = &stats[stats_next++];
115
116         RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);
117
118         mp->stats->file = file;
119         mp->stats->line = line;
120         mp->stats->count = 0;
121         timevalclear(&mp->stats->locked_total);
122         timevalclear(&mp->stats->wait_total);
123         for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
124                 mp->stats->lockers[i].file = NULL;
125                 mp->stats->lockers[i].line = 0;
126                 mp->stats->lockers[i].count = 0;
127                 timevalclear(&mp->stats->lockers[i].locked_total);
128                 timevalclear(&mp->stats->lockers[i].wait_total);
129         }
130
131         return (ISC_R_SUCCESS);
132 }
133
134 isc_result_t
135 isc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
136         struct timeval prelock_t;
137         struct timeval postlock_t;
138         isc_mutexlocker_t *locker = NULL;
139         int i;
140
141         gettimeofday(&prelock_t, NULL);
142
143         if (pthread_mutex_lock(&mp->mutex) != 0)
144                 return (ISC_R_UNEXPECTED);
145
146         gettimeofday(&postlock_t, NULL);
147         mp->stats->lock_t = postlock_t;
148
149         timevalsub(&postlock_t, &prelock_t);
150
151         mp->stats->count++;
152         timevaladd(&mp->stats->wait_total, &postlock_t);
153
154         for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
155                 if (mp->stats->lockers[i].file == NULL) {
156                         locker = &mp->stats->lockers[i];
157                         locker->file = file;
158                         locker->line = line;
159                         break;
160                 } else if (mp->stats->lockers[i].file == file &&
161                            mp->stats->lockers[i].line == line) {
162                         locker = &mp->stats->lockers[i];
163                         break;
164                 }
165         }
166
167         if (locker != NULL) {
168                 locker->count++;
169                 timevaladd(&locker->wait_total, &postlock_t);
170         }
171
172         mp->stats->cur_locker = locker;
173
174         return (ISC_R_SUCCESS);
175 }
176
177 isc_result_t
178 isc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
179         struct timeval unlock_t;
180
181         UNUSED(file);
182         UNUSED(line);
183
184         if (mp->stats->cur_locker != NULL) {
185                 gettimeofday(&unlock_t, NULL);
186                 timevalsub(&unlock_t, &mp->stats->lock_t);
187                 timevaladd(&mp->stats->locked_total, &unlock_t);
188                 timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
189                 mp->stats->cur_locker = NULL;
190         }
191
192         return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \
193                 ISC_R_SUCCESS : ISC_R_UNEXPECTED);
194 }
195
196
197 void
198 isc_mutex_statsprofile(FILE *fp) {
199         isc_mutexlocker_t *locker;
200         int i, j;
201
202         fprintf(fp, "Mutex stats (in us)\n");
203         for (i = 0; i < stats_next; i++) {
204                 fprintf(fp, "%-12s %4d: %10u  %lu.%06lu %lu.%06lu %5d\n",
205                         stats[i].file, stats[i].line, stats[i].count,
206                         stats[i].locked_total.tv_sec,
207                         stats[i].locked_total.tv_usec,
208                         stats[i].wait_total.tv_sec,
209                         stats[i].wait_total.tv_usec,
210                         i);
211                 for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
212                         locker = &stats[i].lockers[j];
213                         if (locker->file == NULL)
214                                 continue;
215                         fprintf(fp, " %-11s %4d: %10u  %lu.%06lu %lu.%06lu %5d\n",
216                                 locker->file, locker->line, locker->count,
217                                 locker->locked_total.tv_sec,
218                                 locker->locked_total.tv_usec,
219                                 locker->wait_total.tv_sec,
220                                 locker->wait_total.tv_usec,
221                                 i);
222                 }
223         }
224 }
225
226 #endif /* ISC_MUTEX_PROFILE */
227
228 #if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
229 isc_result_t
230 isc_mutex_init_errcheck(isc_mutex_t *mp)
231 {
232         pthread_mutexattr_t attr;
233         int err;
234
235         if (pthread_mutexattr_init(&attr) != 0)
236                 return (ISC_R_UNEXPECTED);
237
238         if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0) {
239                 pthread_mutexattr_destroy(&attr);
240                 return (ISC_R_UNEXPECTED);
241         }
242
243         err = pthread_mutex_init(mp, &attr) != 0)
244         pthread_mutexattr_destroy(&attr);
245         if (err == ENOMEM)
246                 return (ISC_R_NOMEMORY);
247         return ((err == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED);
248 }
249 #endif
250
251 #if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
252 pthread_mutexattr_t isc__mutex_attrs = {
253         PTHREAD_MUTEX_ERRORCHECK,       /* m_type */
254         0                               /* m_flags, which appears to be unused. */
255 };
256 #endif
257
258 #if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && !ISC_MUTEX_PROFILE
259 isc_result_t
260 isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
261         char strbuf[ISC_STRERRORSIZE];
262         isc_result_t result = ISC_R_SUCCESS;
263         int err;
264
265         err = pthread_mutex_init(mp, ISC__MUTEX_ATTRS);
266         if (err == ENOMEM)
267                 return (ISC_R_NOMEMORY);
268         if (err != 0) {
269                 isc__strerror(errno, strbuf, sizeof(strbuf));
270                 UNEXPECTED_ERROR(file, line, "isc_mutex_init() failed: %s",
271                                  strbuf);
272                 result = ISC_R_UNEXPECTED;
273         }
274         return (result);
275 }
276 #endif