Home | History | Annotate | Line # | Download | only in pthreads
mutex.c revision 1.1.2.2
      1 /*	$NetBSD: mutex.c,v 1.1.2.2 2024/02/24 13:07:29 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0.  If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 /*! \file */
     17 
     18 #include <errno.h>
     19 #include <stdbool.h>
     20 #include <stdio.h>
     21 #include <sys/time.h>
     22 #include <time.h>
     23 
     24 #include <isc/mutex.h>
     25 #include <isc/once.h>
     26 #include <isc/print.h>
     27 #include <isc/strerr.h>
     28 #include <isc/string.h>
     29 #include <isc/util.h>
     30 
     31 #if ISC_MUTEX_PROFILE
     32 
     33 /*@{*/
     34 /*% Operations on timevals; adapted from FreeBSD's sys/time.h */
     35 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
     36 #define timevaladd(vvp, uvp)                       \
     37 	do {                                       \
     38 		(vvp)->tv_sec += (uvp)->tv_sec;    \
     39 		(vvp)->tv_usec += (uvp)->tv_usec;  \
     40 		if ((vvp)->tv_usec >= 1000000) {   \
     41 			(vvp)->tv_sec++;           \
     42 			(vvp)->tv_usec -= 1000000; \
     43 		}                                  \
     44 	} while (0)
     45 #define timevalsub(vvp, uvp)                       \
     46 	do {                                       \
     47 		(vvp)->tv_sec -= (uvp)->tv_sec;    \
     48 		(vvp)->tv_usec -= (uvp)->tv_usec;  \
     49 		if ((vvp)->tv_usec < 0) {          \
     50 			(vvp)->tv_sec--;           \
     51 			(vvp)->tv_usec += 1000000; \
     52 		}                                  \
     53 	} while (0)
     54 
     55 /*@}*/
     56 
     57 #define ISC_MUTEX_MAX_LOCKERS 32
     58 
     59 typedef struct {
     60 	const char *file;
     61 	int line;
     62 	unsigned count;
     63 	struct timeval locked_total;
     64 	struct timeval wait_total;
     65 } isc_mutexlocker_t;
     66 
     67 struct isc_mutexstats {
     68 	const char *file; /*%< File mutex was created in. */
     69 	int line;	  /*%< Line mutex was created on. */
     70 	unsigned count;
     71 	struct timeval lock_t;
     72 	struct timeval locked_total;
     73 	struct timeval wait_total;
     74 	isc_mutexlocker_t *cur_locker;
     75 	isc_mutexlocker_t lockers[ISC_MUTEX_MAX_LOCKERS];
     76 };
     77 
     78 #ifndef ISC_MUTEX_PROFTABLESIZE
     79 #define ISC_MUTEX_PROFTABLESIZE (1024 * 1024)
     80 #endif /* ifndef ISC_MUTEX_PROFTABLESIZE */
     81 static isc_mutexstats_t stats[ISC_MUTEX_PROFTABLESIZE];
     82 static int stats_next = 0;
     83 static bool stats_init = false;
     84 static pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;
     85 
     86 void
     87 isc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
     88 	int i, err;
     89 
     90 	err = pthread_mutex_init(&mp->mutex, NULL);
     91 	if (err != 0) {
     92 		strerror_r(err, strbuf, sizeof(strbuf));
     93 		isc_error_fatal(file, line, "pthread_mutex_init failed: %s",
     94 				strbuf);
     95 	}
     96 
     97 	RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);
     98 
     99 	if (!stats_init) {
    100 		stats_init = true;
    101 	}
    102 
    103 	/*
    104 	 * If all statistics entries have been used, give up and trigger an
    105 	 * assertion failure.  There would be no other way to deal with this
    106 	 * because we'd like to keep record of all locks for the purpose of
    107 	 * debugging and the number of necessary locks is unpredictable.
    108 	 * If this failure is triggered while debugging, named should be
    109 	 * rebuilt with an increased ISC_MUTEX_PROFTABLESIZE.
    110 	 */
    111 	RUNTIME_CHECK(stats_next < ISC_MUTEX_PROFTABLESIZE);
    112 	mp->stats = &stats[stats_next++];
    113 
    114 	RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);
    115 
    116 	mp->stats->file = file;
    117 	mp->stats->line = line;
    118 	mp->stats->count = 0;
    119 	timevalclear(&mp->stats->locked_total);
    120 	timevalclear(&mp->stats->wait_total);
    121 	for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
    122 		mp->stats->lockers[i].file = NULL;
    123 		mp->stats->lockers[i].line = 0;
    124 		mp->stats->lockers[i].count = 0;
    125 		timevalclear(&mp->stats->lockers[i].locked_total);
    126 		timevalclear(&mp->stats->lockers[i].wait_total);
    127 	}
    128 }
    129 
    130 isc_result_t
    131 isc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
    132 	struct timeval prelock_t;
    133 	struct timeval postlock_t;
    134 	isc_mutexlocker_t *locker = NULL;
    135 	int i;
    136 
    137 	gettimeofday(&prelock_t, NULL);
    138 
    139 	if (pthread_mutex_lock(&mp->mutex) != 0) {
    140 		return (ISC_R_UNEXPECTED);
    141 	}
    142 
    143 	gettimeofday(&postlock_t, NULL);
    144 	mp->stats->lock_t = postlock_t;
    145 
    146 	timevalsub(&postlock_t, &prelock_t);
    147 
    148 	mp->stats->count++;
    149 	timevaladd(&mp->stats->wait_total, &postlock_t);
    150 
    151 	for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
    152 		if (mp->stats->lockers[i].file == NULL) {
    153 			locker = &mp->stats->lockers[i];
    154 			locker->file = file;
    155 			locker->line = line;
    156 			break;
    157 		} else if (mp->stats->lockers[i].file == file &&
    158 			   mp->stats->lockers[i].line == line)
    159 		{
    160 			locker = &mp->stats->lockers[i];
    161 			break;
    162 		}
    163 	}
    164 
    165 	if (locker != NULL) {
    166 		locker->count++;
    167 		timevaladd(&locker->wait_total, &postlock_t);
    168 	}
    169 
    170 	mp->stats->cur_locker = locker;
    171 
    172 	return (ISC_R_SUCCESS);
    173 }
    174 
    175 isc_result_t
    176 isc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
    177 	struct timeval unlock_t;
    178 
    179 	UNUSED(file);
    180 	UNUSED(line);
    181 
    182 	if (mp->stats->cur_locker != NULL) {
    183 		gettimeofday(&unlock_t, NULL);
    184 		timevalsub(&unlock_t, &mp->stats->lock_t);
    185 		timevaladd(&mp->stats->locked_total, &unlock_t);
    186 		timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
    187 		mp->stats->cur_locker = NULL;
    188 	}
    189 
    190 	return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? ISC_R_SUCCESS
    191 							  : ISC_R_UNEXPECTED);
    192 }
    193 
    194 void
    195 isc_mutex_statsprofile(FILE *fp) {
    196 	isc_mutexlocker_t *locker;
    197 	int i, j;
    198 
    199 	fprintf(fp, "Mutex stats (in us)\n");
    200 	for (i = 0; i < stats_next; i++) {
    201 		fprintf(fp, "%-12s %4d: %10u  %lu.%06lu %lu.%06lu %5d\n",
    202 			stats[i].file, stats[i].line, stats[i].count,
    203 			stats[i].locked_total.tv_sec,
    204 			stats[i].locked_total.tv_usec,
    205 			stats[i].wait_total.tv_sec, stats[i].wait_total.tv_usec,
    206 			i);
    207 		for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
    208 			locker = &stats[i].lockers[j];
    209 			if (locker->file == NULL) {
    210 				continue;
    211 			}
    212 			fprintf(fp,
    213 				" %-11s %4d: %10u  %lu.%06lu %lu.%06lu %5d\n",
    214 				locker->file, locker->line, locker->count,
    215 				locker->locked_total.tv_sec,
    216 				locker->locked_total.tv_usec,
    217 				locker->wait_total.tv_sec,
    218 				locker->wait_total.tv_usec, i);
    219 		}
    220 	}
    221 }
    222 
    223 #endif /* ISC_MUTEX_PROFILE */
    224 
    225 #if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
    226 
    227 static bool errcheck_initialized = false;
    228 static pthread_mutexattr_t errcheck;
    229 static isc_once_t once_errcheck = ISC_ONCE_INIT;
    230 
    231 static void
    232 initialize_errcheck(void) {
    233 	RUNTIME_CHECK(pthread_mutexattr_init(&errcheck) == 0);
    234 	RUNTIME_CHECK(pthread_mutexattr_settype(&errcheck,
    235 						PTHREAD_MUTEX_ERRORCHECK) == 0);
    236 	errcheck_initialized = true;
    237 }
    238 
    239 void
    240 isc_mutex_init_errcheck(isc_mutex_t *mp) {
    241 	isc_result_t result;
    242 	int err;
    243 
    244 	result = isc_once_do(&once_errcheck, initialize_errcheck);
    245 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
    246 
    247 	err = pthread_mutex_init(mp, &errcheck);
    248 	if (err != 0) {
    249 		strerror_r(err, strbuf, sizeof(strbuf));
    250 		isc_error_fatal(file, line, "pthread_mutex_init failed: %s",
    251 				strbuf);
    252 	}
    253 }
    254 #endif /* if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK) */
    255 
    256 #if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
    257 pthread_mutexattr_t isc__mutex_attrs = {
    258 	PTHREAD_MUTEX_ERRORCHECK, /* m_type */
    259 	0			  /* m_flags, which appears to be unused. */
    260 };
    261 #endif /* if ISC_MUTEX_DEBUG && defined(__NetBSD__) && \
    262 	* defined(PTHREAD_MUTEX_ERRORCHECK) */
    263 
    264 #if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && \
    265 	!ISC_MUTEX_PROFILE
    266 
    267 #ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
    268 static bool attr_initialized = false;
    269 static pthread_mutexattr_t attr;
    270 static isc_once_t once_attr = ISC_ONCE_INIT;
    271 #endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
    272 
    273 #ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
    274 static void
    275 initialize_attr(void) {
    276 	RUNTIME_CHECK(pthread_mutexattr_init(&attr) == 0);
    277 	RUNTIME_CHECK(pthread_mutexattr_settype(
    278 			      &attr, PTHREAD_MUTEX_ADAPTIVE_NP) == 0);
    279 	attr_initialized = true;
    280 }
    281 #endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
    282 
    283 void
    284 isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
    285 	int err;
    286 
    287 #ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
    288 	isc_result_t result = ISC_R_SUCCESS;
    289 	result = isc_once_do(&once_attr, initialize_attr);
    290 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
    291 
    292 	err = pthread_mutex_init(mp, &attr);
    293 #else  /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
    294 	err = pthread_mutex_init(mp, ISC__MUTEX_ATTRS);
    295 #endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
    296 	if (err != 0) {
    297 		char strbuf[ISC_STRERRORSIZE];
    298 		strerror_r(err, strbuf, sizeof(strbuf));
    299 		isc_error_fatal(file, line, "pthread_mutex_init failed: %s",
    300 				strbuf);
    301 	}
    302 }
    303 #endif /* if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && \
    304 	* !ISC_MUTEX_PROFILE */
    305