mutex.c 7.94 KB
Newer Older
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 2000-2002  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
6 7 8
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
16 17
 */

18
/* $Id: mutex.c,v 1.15 2008/04/04 01:49:09 jinmei Exp $ */
19 20

/*! \file */
21 22 23 24 25 26

#include <config.h>

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
27
#include <errno.h>
28 29 30

#include <isc/mutex.h>
#include <isc/util.h>
31
#include <isc/strerror.h>
32 33 34

#if ISC_MUTEX_PROFILE

35 36
/*@{*/
/*% Operations on timevals; adapted from FreeBSD's sys/time.h */
37 38
#define timevalclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_usec = 0)
#define timevaladd(vvp, uvp)                                            \
39 40
        do {                                                            \
                (vvp)->tv_sec += (uvp)->tv_sec;                         \
41 42
                (vvp)->tv_usec += (uvp)->tv_usec;                       \
                if ((vvp)->tv_usec >= 1000000) {                        \
43
                        (vvp)->tv_sec++;                                \
44
                        (vvp)->tv_usec -= 1000000;                      \
45 46
                }                                                       \
        } while (0)
47
#define timevalsub(vvp, uvp)                                            \
48 49
        do {                                                            \
                (vvp)->tv_sec -= (uvp)->tv_sec;                         \
50 51
                (vvp)->tv_usec -= (uvp)->tv_usec;                       \
                if ((vvp)->tv_usec < 0) {                               \
52
                        (vvp)->tv_sec--;                                \
53
                        (vvp)->tv_usec += 1000000;                      \
54 55 56
                }                                                       \
        } while (0)

57 58
/*@}*/

59 60 61 62 63 64
#define ISC_MUTEX_MAX_LOCKERS 32

typedef struct {
	const char *		file;
	int			line;
	unsigned		count;
65 66 67
	struct timeval		locked_total;
	struct timeval		wait_total;
} isc_mutexlocker_t;
68

69
struct isc_mutexstats {
70 71
	const char *		file;	/*%< File mutex was created in. */
	int 			line;	/*%< Line mutex was created on. */
72
	unsigned		count;
73 74 75 76 77
	struct timeval		lock_t;
	struct timeval		locked_total;
	struct timeval		wait_total;
	isc_mutexlocker_t *	cur_locker;
	isc_mutexlocker_t	lockers[ISC_MUTEX_MAX_LOCKERS];
78 79
};

80 81 82 83 84
#ifndef ISC_MUTEX_PROFTABLESIZE
#define ISC_MUTEX_PROFTABLESIZE (16 * 1024)
#endif
static isc_mutexstats_t stats[ISC_MUTEX_PROFTABLESIZE];
static int stats_next = 0;
85 86 87 88 89 90
static isc_boolean_t stats_init = ISC_FALSE;
static pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;


isc_result_t
isc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
91
	int i, err;
92

93 94 95 96 97
	err = pthread_mutex_init(&mp->mutex, NULL);
	if (err == ENOMEM)
		return (ISC_R_NOMEMORY);
	if (err != 0)
		return (ISC_R_UNEXPECTED);
98 99 100

	RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);

101
	if (stats_init == ISC_FALSE)
102 103
		stats_init = ISC_TRUE;

104 105 106 107 108 109 110 111 112 113
	/*
	 * If all statistics entries have been used, give up and trigger an
	 * assertion failure.  There would be no other way to deal with this
	 * because we'd like to keep record of all locks for the purpose of
	 * debugging and the number of necessary locks is unpredictable.
	 * If this failure is triggered while debugging, named should be
	 * rebuilt with an increased ISC_MUTEX_PROFTABLESIZE.
	 */
	RUNTIME_CHECK(stats_next < ISC_MUTEX_PROFTABLESIZE);
	mp->stats = &stats[stats_next++];
114 115 116 117 118 119

	RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);

	mp->stats->file = file;
	mp->stats->line = line;
	mp->stats->count = 0;
120 121
	timevalclear(&mp->stats->locked_total);
	timevalclear(&mp->stats->wait_total);
122 123 124 125
	for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
		mp->stats->lockers[i].file = NULL;
		mp->stats->lockers[i].line = 0;
		mp->stats->lockers[i].count = 0;
126 127
		timevalclear(&mp->stats->lockers[i].locked_total);
		timevalclear(&mp->stats->lockers[i].wait_total);
128 129
	}

130
	return (ISC_R_SUCCESS);
131 132 133 134
}

isc_result_t
isc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
135 136 137
	struct timeval prelock_t;
	struct timeval postlock_t;
	isc_mutexlocker_t *locker = NULL;
138 139
	int i;

140
	gettimeofday(&prelock_t, NULL);
141 142 143 144

	if (pthread_mutex_lock(&mp->mutex) != 0)
		return (ISC_R_UNEXPECTED);

145
	gettimeofday(&postlock_t, NULL);
146 147
	mp->stats->lock_t = postlock_t;

148
	timevalsub(&postlock_t, &prelock_t);
149 150

	mp->stats->count++;
151
	timevaladd(&mp->stats->wait_total, &postlock_t);
152

153 154 155 156 157 158 159 160 161 162 163 164 165
	for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
		if (mp->stats->lockers[i].file == NULL) {
			locker = &mp->stats->lockers[i];
			locker->file = file;
			locker->line = line;
			break;
		} else if (mp->stats->lockers[i].file == file &&
			   mp->stats->lockers[i].line == line) {
			locker = &mp->stats->lockers[i];
			break;
		}
	}

166 167
	if (locker != NULL) {
		locker->count++;
168
		timevaladd(&locker->wait_total, &postlock_t);
169 170 171 172
	}

	mp->stats->cur_locker = locker;

173
	return (ISC_R_SUCCESS);
174 175 176 177
}

isc_result_t
isc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
178
	struct timeval unlock_t;
179 180 181 182 183

	UNUSED(file);
	UNUSED(line);

	if (mp->stats->cur_locker != NULL) {
184 185 186 187
		gettimeofday(&unlock_t, NULL);
		timevalsub(&unlock_t, &mp->stats->lock_t);
		timevaladd(&mp->stats->locked_total, &unlock_t);
		timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
188 189 190 191 192 193 194 195 196 197
		mp->stats->cur_locker = NULL;
	}

	return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \
		ISC_R_SUCCESS : ISC_R_UNEXPECTED);
}


void
isc_mutex_statsprofile(FILE *fp) {
198
	isc_mutexlocker_t *locker;
199
	int i, j;
200

201
	fprintf(fp, "Mutex stats (in us)\n");
202
	for (i = 0; i < stats_next; i++) {
203 204 205
		fprintf(fp, "%-12s %4d: %10u  %lu.%06lu %lu.%06lu\n",
			stats[i].file, stats[i].line, stats[i].count,
			stats[i].locked_total.tv_sec,
206
			stats[i].locked_total.tv_usec,
207
			stats[i].wait_total.tv_sec,
208
			stats[i].wait_total.tv_usec
209 210 211 212 213 214 215 216
			);
		for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
			locker = &stats[i].lockers[j];
			if (locker->file == NULL)
				continue;
			fprintf(fp, " %-11s %4d: %10u  %lu.%06lu %lu.%06lu\n",
				locker->file, locker->line, locker->count,
				locker->locked_total.tv_sec,
217
				locker->locked_total.tv_usec,
218
				locker->wait_total.tv_sec,
219
				locker->wait_total.tv_usec
220 221 222 223 224
				);
		}
	}
}

225
#endif /* ISC_MUTEX_PROFILE */
226

227 228 229 230 231
#if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
isc_result_t
isc_mutex_init_errcheck(isc_mutex_t *mp)
{
	pthread_mutexattr_t attr;
232
	int err;
233 234

	if (pthread_mutexattr_init(&attr) != 0)
235
		return (ISC_R_UNEXPECTED);
236 237

	if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0)
238
		return (ISC_R_UNEXPECTED);
239
  
240 241 242 243
	err = pthread_mutex_init(mp, &attr) != 0)
	if (err == ENOMEM)
		return (ISC_R_NOMEMORY);
	return ((err == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED);
244 245 246
}
#endif

247
#if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
248 249 250 251 252
pthread_mutexattr_t isc__mutex_attrs = {
	PTHREAD_MUTEX_ERRORCHECK,	/* m_type */
	0				/* m_flags, which appears to be unused. */
};
#endif
253

254
#if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && !ISC_MUTEX_PROFILE
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
isc_result_t
isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
	char strbuf[ISC_STRERRORSIZE];
	isc_result_t result = ISC_R_SUCCESS;
	int err;

	err = pthread_mutex_init(mp, ISC__MUTEX_ATTRS);
	if (err == ENOMEM)
		return (ISC_R_NOMEMORY);
	if (err != 0) {
		isc__strerror(errno, strbuf, sizeof(strbuf));
		UNEXPECTED_ERROR(file, line, "isc_mutex_init() failed: %s",
				 strbuf);
		result = ISC_R_UNEXPECTED;
	}
	return (result);
}
272
#endif