Blame lib/isc/pthreads/mutex.c

Packit Service ae04f2
/*
Packit Service ae04f2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit Service ae04f2
 *
Packit Service ae04f2
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit Service ae04f2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service ae04f2
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit Service ae04f2
 *
Packit Service ae04f2
 * See the COPYRIGHT file distributed with this work for additional
Packit Service ae04f2
 * information regarding copyright ownership.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
Packit Service ae04f2
/*! \file */
Packit Service ae04f2
Packit Service ae04f2
#include <config.h>
Packit Service ae04f2
Packit Service ae04f2
#include <stdbool.h>
Packit Service ae04f2
#include <stdio.h>
Packit Service ae04f2
#include <time.h>
Packit Service ae04f2
#include <sys/time.h>
Packit Service ae04f2
#include <errno.h>
Packit Service ae04f2
Packit Service ae04f2
#include <isc/mutex.h>
Packit Service ae04f2
#include <isc/util.h>
Packit Service ae04f2
#include <isc/print.h>
Packit Service ae04f2
#include <isc/strerror.h>
Packit Service ae04f2
#include <isc/once.h>
Packit Service ae04f2
Packit Service ae04f2
#if ISC_MUTEX_PROFILE
Packit Service ae04f2
Packit Service ae04f2
/*@{*/
Packit Service ae04f2
/*% Operations on timevals; adapted from FreeBSD's sys/time.h */
Packit Service ae04f2
#define timevalclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_usec = 0)
Packit Service ae04f2
#define timevaladd(vvp, uvp)						\
Packit Service ae04f2
	do {								\
Packit Service ae04f2
		(vvp)->tv_sec += (uvp)->tv_sec;				\
Packit Service ae04f2
		(vvp)->tv_usec += (uvp)->tv_usec;			\
Packit Service ae04f2
		if ((vvp)->tv_usec >= 1000000) {			\
Packit Service ae04f2
			(vvp)->tv_sec++;				\
Packit Service ae04f2
			(vvp)->tv_usec -= 1000000;			\
Packit Service ae04f2
		}							\
Packit Service ae04f2
	} while (0)
Packit Service ae04f2
#define timevalsub(vvp, uvp)						\
Packit Service ae04f2
	do {								\
Packit Service ae04f2
		(vvp)->tv_sec -= (uvp)->tv_sec;				\
Packit Service ae04f2
		(vvp)->tv_usec -= (uvp)->tv_usec;			\
Packit Service ae04f2
		if ((vvp)->tv_usec < 0) {				\
Packit Service ae04f2
			(vvp)->tv_sec--;				\
Packit Service ae04f2
			(vvp)->tv_usec += 1000000;			\
Packit Service ae04f2
		}							\
Packit Service ae04f2
	} while (0)
Packit Service ae04f2
Packit Service ae04f2
/*@}*/
Packit Service ae04f2
Packit Service ae04f2
#define ISC_MUTEX_MAX_LOCKERS 32
Packit Service ae04f2
Packit Service ae04f2
typedef struct {
Packit Service ae04f2
	const char *		file;
Packit Service ae04f2
	int			line;
Packit Service ae04f2
	unsigned		count;
Packit Service ae04f2
	struct timeval		locked_total;
Packit Service ae04f2
	struct timeval		wait_total;
Packit Service ae04f2
} isc_mutexlocker_t;
Packit Service ae04f2
Packit Service ae04f2
struct isc_mutexstats {
Packit Service ae04f2
	const char *		file;	/*%< File mutex was created in. */
Packit Service ae04f2
	int			line;	/*%< Line mutex was created on. */
Packit Service ae04f2
	unsigned		count;
Packit Service ae04f2
	struct timeval		lock_t;
Packit Service ae04f2
	struct timeval		locked_total;
Packit Service ae04f2
	struct timeval		wait_total;
Packit Service ae04f2
	isc_mutexlocker_t *	cur_locker;
Packit Service ae04f2
	isc_mutexlocker_t	lockers[ISC_MUTEX_MAX_LOCKERS];
Packit Service ae04f2
};
Packit Service ae04f2
Packit Service ae04f2
#ifndef ISC_MUTEX_PROFTABLESIZE
Packit Service ae04f2
#define ISC_MUTEX_PROFTABLESIZE (1024 * 1024)
Packit Service ae04f2
#endif
Packit Service ae04f2
static isc_mutexstats_t stats[ISC_MUTEX_PROFTABLESIZE];
Packit Service ae04f2
static int stats_next = 0;
Packit Service ae04f2
static bool stats_init = false;
Packit Service ae04f2
static pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;
Packit Service ae04f2
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
Packit Service ae04f2
	int i, err;
Packit Service ae04f2
Packit Service ae04f2
	err = pthread_mutex_init(&mp->mutex, NULL);
Packit Service ae04f2
	if (err == ENOMEM)
Packit Service ae04f2
		return (ISC_R_NOMEMORY);
Packit Service ae04f2
	if (err != 0)
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
Packit Service ae04f2
	RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);
Packit Service ae04f2
Packit Service ae04f2
	if (stats_init == false)
Packit Service ae04f2
		stats_init = true;
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * If all statistics entries have been used, give up and trigger an
Packit Service ae04f2
	 * assertion failure.  There would be no other way to deal with this
Packit Service ae04f2
	 * because we'd like to keep record of all locks for the purpose of
Packit Service ae04f2
	 * debugging and the number of necessary locks is unpredictable.
Packit Service ae04f2
	 * If this failure is triggered while debugging, named should be
Packit Service ae04f2
	 * rebuilt with an increased ISC_MUTEX_PROFTABLESIZE.
Packit Service ae04f2
	 */
Packit Service ae04f2
	RUNTIME_CHECK(stats_next < ISC_MUTEX_PROFTABLESIZE);
Packit Service ae04f2
	mp->stats = &stats[stats_next++];
Packit Service ae04f2
Packit Service ae04f2
	RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);
Packit Service ae04f2
Packit Service ae04f2
	mp->stats->file = file;
Packit Service ae04f2
	mp->stats->line = line;
Packit Service ae04f2
	mp->stats->count = 0;
Packit Service ae04f2
	timevalclear(&mp->stats->locked_total);
Packit Service ae04f2
	timevalclear(&mp->stats->wait_total);
Packit Service ae04f2
	for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
Packit Service ae04f2
		mp->stats->lockers[i].file = NULL;
Packit Service ae04f2
		mp->stats->lockers[i].line = 0;
Packit Service ae04f2
		mp->stats->lockers[i].count = 0;
Packit Service ae04f2
		timevalclear(&mp->stats->lockers[i].locked_total);
Packit Service ae04f2
		timevalclear(&mp->stats->lockers[i].wait_total);
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
Packit Service ae04f2
	struct timeval prelock_t;
Packit Service ae04f2
	struct timeval postlock_t;
Packit Service ae04f2
	isc_mutexlocker_t *locker = NULL;
Packit Service ae04f2
	int i;
Packit Service ae04f2
Packit Service ae04f2
	gettimeofday(&prelock_t, NULL);
Packit Service ae04f2
Packit Service ae04f2
	if (pthread_mutex_lock(&mp->mutex) != 0)
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
Packit Service ae04f2
	gettimeofday(&postlock_t, NULL);
Packit Service ae04f2
	mp->stats->lock_t = postlock_t;
Packit Service ae04f2
Packit Service ae04f2
	timevalsub(&postlock_t, &prelock_t);
Packit Service ae04f2
Packit Service ae04f2
	mp->stats->count++;
Packit Service ae04f2
	timevaladd(&mp->stats->wait_total, &postlock_t);
Packit Service ae04f2
Packit Service ae04f2
	for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
Packit Service ae04f2
		if (mp->stats->lockers[i].file == NULL) {
Packit Service ae04f2
			locker = &mp->stats->lockers[i];
Packit Service ae04f2
			locker->file = file;
Packit Service ae04f2
			locker->line = line;
Packit Service ae04f2
			break;
Packit Service ae04f2
		} else if (mp->stats->lockers[i].file == file &&
Packit Service ae04f2
			   mp->stats->lockers[i].line == line) {
Packit Service ae04f2
			locker = &mp->stats->lockers[i];
Packit Service ae04f2
			break;
Packit Service ae04f2
		}
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	if (locker != NULL) {
Packit Service ae04f2
		locker->count++;
Packit Service ae04f2
		timevaladd(&locker->wait_total, &postlock_t);
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	mp->stats->cur_locker = locker;
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
Packit Service ae04f2
	struct timeval unlock_t;
Packit Service ae04f2
Packit Service ae04f2
	UNUSED(file);
Packit Service ae04f2
	UNUSED(line);
Packit Service ae04f2
Packit Service ae04f2
	if (mp->stats->cur_locker != NULL) {
Packit Service ae04f2
		gettimeofday(&unlock_t, NULL);
Packit Service ae04f2
		timevalsub(&unlock_t, &mp->stats->lock_t);
Packit Service ae04f2
		timevaladd(&mp->stats->locked_total, &unlock_t);
Packit Service ae04f2
		timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
Packit Service ae04f2
		mp->stats->cur_locker = NULL;
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \
Packit Service ae04f2
		ISC_R_SUCCESS : ISC_R_UNEXPECTED);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_mutex_statsprofile(FILE *fp) {
Packit Service ae04f2
	isc_mutexlocker_t *locker;
Packit Service ae04f2
	int i, j;
Packit Service ae04f2
Packit Service ae04f2
	fprintf(fp, "Mutex stats (in us)\n");
Packit Service ae04f2
	for (i = 0; i < stats_next; i++) {
Packit Service ae04f2
		fprintf(fp, "%-12s %4d: %10u  %lu.%06lu %lu.%06lu %5d\n",
Packit Service ae04f2
			stats[i].file, stats[i].line, stats[i].count,
Packit Service ae04f2
			stats[i].locked_total.tv_sec,
Packit Service ae04f2
			stats[i].locked_total.tv_usec,
Packit Service ae04f2
			stats[i].wait_total.tv_sec,
Packit Service ae04f2
			stats[i].wait_total.tv_usec,
Packit Service ae04f2
			i);
Packit Service ae04f2
		for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
Packit Service ae04f2
			locker = &stats[i].lockers[j];
Packit Service ae04f2
			if (locker->file == NULL)
Packit Service ae04f2
				continue;
Packit Service ae04f2
			fprintf(fp, " %-11s %4d: %10u  %lu.%06lu %lu.%06lu %5d\n",
Packit Service ae04f2
				locker->file, locker->line, locker->count,
Packit Service ae04f2
				locker->locked_total.tv_sec,
Packit Service ae04f2
				locker->locked_total.tv_usec,
Packit Service ae04f2
				locker->wait_total.tv_sec,
Packit Service ae04f2
				locker->wait_total.tv_usec,
Packit Service ae04f2
				i);
Packit Service ae04f2
		}
Packit Service ae04f2
	}
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
#endif /* ISC_MUTEX_PROFILE */
Packit Service ae04f2
Packit Service ae04f2
#if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
Packit Service ae04f2
Packit Service ae04f2
static bool errcheck_initialized = false;
Packit Service ae04f2
static pthread_mutexattr_t errcheck;
Packit Service ae04f2
static isc_once_t once_errcheck = ISC_ONCE_INIT;
Packit Service ae04f2
Packit Service ae04f2
static void
Packit Service ae04f2
initialize_errcheck(void) {
Packit Service ae04f2
	RUNTIME_CHECK(pthread_mutexattr_init(&errcheck) == 0);
Packit Service ae04f2
	RUNTIME_CHECK(pthread_mutexattr_settype
Packit Service ae04f2
		      (&errcheck, PTHREAD_MUTEX_ERRORCHECK) == 0);
Packit Service ae04f2
	errcheck_initialized = true;
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_mutex_init_errcheck(isc_mutex_t *mp) {
Packit Service ae04f2
	isc_result_t result;
Packit Service ae04f2
	int err;
Packit Service ae04f2
Packit Service ae04f2
	result = isc_once_do(&once_errcheck, initialize_errcheck);
Packit Service ae04f2
	RUNTIME_CHECK(result == ISC_R_SUCCESS);
Packit Service ae04f2
Packit Service ae04f2
	err = pthread_mutex_init(mp, &errcheck);
Packit Service ae04f2
	if (err == ENOMEM)
Packit Service ae04f2
		return (ISC_R_NOMEMORY);
Packit Service ae04f2
	return ((err == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED);
Packit Service ae04f2
}
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
#if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
Packit Service ae04f2
pthread_mutexattr_t isc__mutex_attrs = {
Packit Service ae04f2
	PTHREAD_MUTEX_ERRORCHECK,	/* m_type */
Packit Service ae04f2
	0				/* m_flags, which appears to be unused. */
Packit Service ae04f2
};
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
#if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && !ISC_MUTEX_PROFILE
Packit Service ae04f2
Packit Service ae04f2
#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
Packit Service ae04f2
static bool attr_initialized = false;
Packit Service ae04f2
static pthread_mutexattr_t attr;
Packit Service ae04f2
static isc_once_t once_attr = ISC_ONCE_INIT;
Packit Service ae04f2
#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
Packit Service ae04f2
Packit Service ae04f2
#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
Packit Service ae04f2
static void
Packit Service ae04f2
initialize_attr(void) {
Packit Service ae04f2
	RUNTIME_CHECK(pthread_mutexattr_init(&attr) == 0);
Packit Service ae04f2
	RUNTIME_CHECK(pthread_mutexattr_settype
Packit Service ae04f2
		      (&attr, PTHREAD_MUTEX_ADAPTIVE_NP) == 0);
Packit Service ae04f2
	attr_initialized = true;
Packit Service ae04f2
}
Packit Service ae04f2
#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
Packit Service ae04f2
	char strbuf[ISC_STRERRORSIZE];
Packit Service ae04f2
	isc_result_t result = ISC_R_SUCCESS;
Packit Service ae04f2
	int err;
Packit Service ae04f2
Packit Service ae04f2
#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
Packit Service ae04f2
	result = isc_once_do(&once_attr, initialize_attr);
Packit Service ae04f2
	RUNTIME_CHECK(result == ISC_R_SUCCESS);
Packit Service ae04f2
Packit Service ae04f2
	err = pthread_mutex_init(mp, &attr);
Packit Service ae04f2
#else /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
Packit Service ae04f2
	err = pthread_mutex_init(mp, ISC__MUTEX_ATTRS);
Packit Service ae04f2
#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
Packit Service ae04f2
Packit Service ae04f2
	if (err == ENOMEM)
Packit Service ae04f2
		return (ISC_R_NOMEMORY);
Packit Service ae04f2
	if (err != 0) {
Packit Service ae04f2
		isc__strerror(err, strbuf, sizeof(strbuf));
Packit Service ae04f2
		UNEXPECTED_ERROR(file, line, "isc_mutex_init() failed: %s",
Packit Service ae04f2
				 strbuf);
Packit Service ae04f2
		result = ISC_R_UNEXPECTED;
Packit Service ae04f2
	}
Packit Service ae04f2
	return (result);
Packit Service ae04f2
}
Packit Service ae04f2
#endif