1*eabc0478Schristos /* $NetBSD: mutex.c,v 1.2 2024/08/18 20:47:15 christos Exp $ */ 2897be3a4Schristos 3897be3a4Schristos /* 4897be3a4Schristos * Copyright (C) 2004, 2005, 2007, 2008, 2011, 2012 Internet Systems Consortium, Inc. ("ISC") 5897be3a4Schristos * Copyright (C) 2000-2002 Internet Software Consortium. 6897be3a4Schristos * 7897be3a4Schristos * Permission to use, copy, modify, and/or distribute this software for any 8897be3a4Schristos * purpose with or without fee is hereby granted, provided that the above 9897be3a4Schristos * copyright notice and this permission notice appear in all copies. 10897be3a4Schristos * 11897be3a4Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12897be3a4Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13897be3a4Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14897be3a4Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15897be3a4Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16897be3a4Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17897be3a4Schristos * PERFORMANCE OF THIS SOFTWARE. 18897be3a4Schristos */ 19897be3a4Schristos 20897be3a4Schristos /* Id: mutex.c,v 1.18 2011/01/04 23:47:14 tbox Exp */ 21897be3a4Schristos 22897be3a4Schristos /*! \file */ 23897be3a4Schristos 24897be3a4Schristos #include <config.h> 25897be3a4Schristos 26897be3a4Schristos #include <stdio.h> 27897be3a4Schristos #include <time.h> 28897be3a4Schristos #include <sys/time.h> 29897be3a4Schristos #include <errno.h> 30897be3a4Schristos 31897be3a4Schristos #include <isc/mutex.h> 32897be3a4Schristos #include <isc/util.h> 33897be3a4Schristos #include <isc/strerror.h> 34897be3a4Schristos 35897be3a4Schristos #if HAVE_PTHREADS < 5 /* HP-UX 10.20 has 4, needs this */ 36897be3a4Schristos # define pthread_mutex_init(m, a) \ 37897be3a4Schristos pthread_mutex_init(m, (a) \ 38897be3a4Schristos ? *(const pthread_mutexattr_t *)(a) \ 39897be3a4Schristos : pthread_mutexattr_default) 40897be3a4Schristos # define PTHREAD_MUTEX_RECURSIVE MUTEX_RECURSIVE_NP 41897be3a4Schristos # define pthread_mutexattr_settype pthread_mutexattr_setkind_np 42897be3a4Schristos #endif 43897be3a4Schristos 44897be3a4Schristos #if ISC_MUTEX_PROFILE 45897be3a4Schristos 46897be3a4Schristos /*@{*/ 47897be3a4Schristos /*% Operations on timevals; adapted from FreeBSD's sys/time.h */ 48897be3a4Schristos #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 49897be3a4Schristos #define timevaladd(vvp, uvp) \ 50897be3a4Schristos do { \ 51897be3a4Schristos (vvp)->tv_sec += (uvp)->tv_sec; \ 52897be3a4Schristos (vvp)->tv_usec += (uvp)->tv_usec; \ 53897be3a4Schristos if ((vvp)->tv_usec >= 1000000) { \ 54897be3a4Schristos (vvp)->tv_sec++; \ 55897be3a4Schristos (vvp)->tv_usec -= 1000000; \ 56897be3a4Schristos } \ 57897be3a4Schristos } while (0) 58897be3a4Schristos #define timevalsub(vvp, uvp) \ 59897be3a4Schristos do { \ 60897be3a4Schristos (vvp)->tv_sec -= (uvp)->tv_sec; \ 61897be3a4Schristos (vvp)->tv_usec -= (uvp)->tv_usec; \ 62897be3a4Schristos if ((vvp)->tv_usec < 0) { \ 63897be3a4Schristos (vvp)->tv_sec--; \ 64897be3a4Schristos (vvp)->tv_usec += 1000000; \ 65897be3a4Schristos } \ 66897be3a4Schristos } while (0) 67897be3a4Schristos 68897be3a4Schristos /*@}*/ 69897be3a4Schristos 70897be3a4Schristos #define ISC_MUTEX_MAX_LOCKERS 32 71897be3a4Schristos 72897be3a4Schristos typedef struct { 73897be3a4Schristos const char * file; 74897be3a4Schristos int line; 75897be3a4Schristos unsigned count; 76897be3a4Schristos struct timeval locked_total; 77897be3a4Schristos struct timeval wait_total; 78897be3a4Schristos } isc_mutexlocker_t; 79897be3a4Schristos 80897be3a4Schristos struct isc_mutexstats { 81897be3a4Schristos const char * file; /*%< File mutex was created in. */ 82897be3a4Schristos int line; /*%< Line mutex was created on. */ 83897be3a4Schristos unsigned count; 84897be3a4Schristos struct timeval lock_t; 85897be3a4Schristos struct timeval locked_total; 86897be3a4Schristos struct timeval wait_total; 87897be3a4Schristos isc_mutexlocker_t * cur_locker; 88897be3a4Schristos isc_mutexlocker_t lockers[ISC_MUTEX_MAX_LOCKERS]; 89897be3a4Schristos }; 90897be3a4Schristos 91897be3a4Schristos #ifndef ISC_MUTEX_PROFTABLESIZE 92897be3a4Schristos #define ISC_MUTEX_PROFTABLESIZE (1024 * 1024) 93897be3a4Schristos #endif 94897be3a4Schristos static isc_mutexstats_t stats[ISC_MUTEX_PROFTABLESIZE]; 95897be3a4Schristos static int stats_next = 0; 96897be3a4Schristos static isc_boolean_t stats_init = ISC_FALSE; 97897be3a4Schristos static pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER; 98897be3a4Schristos 99897be3a4Schristos 100897be3a4Schristos isc_result_t 101897be3a4Schristos isc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) { 102897be3a4Schristos int i, err; 103897be3a4Schristos 104897be3a4Schristos err = pthread_mutex_init(&mp->mutex, NULL); 105897be3a4Schristos if (err == ENOMEM) 106897be3a4Schristos return (ISC_R_NOMEMORY); 107897be3a4Schristos if (err != 0) 108897be3a4Schristos return (ISC_R_UNEXPECTED); 109897be3a4Schristos 110897be3a4Schristos RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0); 111897be3a4Schristos 112897be3a4Schristos if (stats_init == ISC_FALSE) 113897be3a4Schristos stats_init = ISC_TRUE; 114897be3a4Schristos 115897be3a4Schristos /* 116897be3a4Schristos * If all statistics entries have been used, give up and trigger an 117897be3a4Schristos * assertion failure. There would be no other way to deal with this 118897be3a4Schristos * because we'd like to keep record of all locks for the purpose of 119897be3a4Schristos * debugging and the number of necessary locks is unpredictable. 120897be3a4Schristos * If this failure is triggered while debugging, named should be 121897be3a4Schristos * rebuilt with an increased ISC_MUTEX_PROFTABLESIZE. 122897be3a4Schristos */ 123897be3a4Schristos RUNTIME_CHECK(stats_next < ISC_MUTEX_PROFTABLESIZE); 124897be3a4Schristos mp->stats = &stats[stats_next++]; 125897be3a4Schristos 126897be3a4Schristos RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0); 127897be3a4Schristos 128897be3a4Schristos mp->stats->file = file; 129897be3a4Schristos mp->stats->line = line; 130897be3a4Schristos mp->stats->count = 0; 131897be3a4Schristos timevalclear(&mp->stats->locked_total); 132897be3a4Schristos timevalclear(&mp->stats->wait_total); 133897be3a4Schristos for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) { 134897be3a4Schristos mp->stats->lockers[i].file = NULL; 135897be3a4Schristos mp->stats->lockers[i].line = 0; 136897be3a4Schristos mp->stats->lockers[i].count = 0; 137897be3a4Schristos timevalclear(&mp->stats->lockers[i].locked_total); 138897be3a4Schristos timevalclear(&mp->stats->lockers[i].wait_total); 139897be3a4Schristos } 140897be3a4Schristos 141897be3a4Schristos return (ISC_R_SUCCESS); 142897be3a4Schristos } 143897be3a4Schristos 144897be3a4Schristos isc_result_t 145897be3a4Schristos isc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) { 146897be3a4Schristos struct timeval prelock_t; 147897be3a4Schristos struct timeval postlock_t; 148897be3a4Schristos isc_mutexlocker_t *locker = NULL; 149897be3a4Schristos int i; 150897be3a4Schristos 151897be3a4Schristos gettimeofday(&prelock_t, NULL); 152897be3a4Schristos 153897be3a4Schristos if (pthread_mutex_lock(&mp->mutex) != 0) 154897be3a4Schristos return (ISC_R_UNEXPECTED); 155897be3a4Schristos 156897be3a4Schristos gettimeofday(&postlock_t, NULL); 157897be3a4Schristos mp->stats->lock_t = postlock_t; 158897be3a4Schristos 159897be3a4Schristos timevalsub(&postlock_t, &prelock_t); 160897be3a4Schristos 161897be3a4Schristos mp->stats->count++; 162897be3a4Schristos timevaladd(&mp->stats->wait_total, &postlock_t); 163897be3a4Schristos 164897be3a4Schristos for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) { 165897be3a4Schristos if (mp->stats->lockers[i].file == NULL) { 166897be3a4Schristos locker = &mp->stats->lockers[i]; 167897be3a4Schristos locker->file = file; 168897be3a4Schristos locker->line = line; 169897be3a4Schristos break; 170897be3a4Schristos } else if (mp->stats->lockers[i].file == file && 171897be3a4Schristos mp->stats->lockers[i].line == line) { 172897be3a4Schristos locker = &mp->stats->lockers[i]; 173897be3a4Schristos break; 174897be3a4Schristos } 175897be3a4Schristos } 176897be3a4Schristos 177897be3a4Schristos if (locker != NULL) { 178897be3a4Schristos locker->count++; 179897be3a4Schristos timevaladd(&locker->wait_total, &postlock_t); 180897be3a4Schristos } 181897be3a4Schristos 182897be3a4Schristos mp->stats->cur_locker = locker; 183897be3a4Schristos 184897be3a4Schristos return (ISC_R_SUCCESS); 185897be3a4Schristos } 186897be3a4Schristos 187897be3a4Schristos isc_result_t 188897be3a4Schristos isc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) { 189897be3a4Schristos struct timeval unlock_t; 190897be3a4Schristos 191897be3a4Schristos UNUSED(file); 192897be3a4Schristos UNUSED(line); 193897be3a4Schristos 194897be3a4Schristos if (mp->stats->cur_locker != NULL) { 195897be3a4Schristos gettimeofday(&unlock_t, NULL); 196897be3a4Schristos timevalsub(&unlock_t, &mp->stats->lock_t); 197897be3a4Schristos timevaladd(&mp->stats->locked_total, &unlock_t); 198897be3a4Schristos timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t); 199897be3a4Schristos mp->stats->cur_locker = NULL; 200897be3a4Schristos } 201897be3a4Schristos 202897be3a4Schristos return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \ 203897be3a4Schristos ISC_R_SUCCESS : ISC_R_UNEXPECTED); 204897be3a4Schristos } 205897be3a4Schristos 206897be3a4Schristos 207897be3a4Schristos void 208897be3a4Schristos isc_mutex_statsprofile(FILE *fp) { 209897be3a4Schristos isc_mutexlocker_t *locker; 210897be3a4Schristos int i, j; 211897be3a4Schristos 212897be3a4Schristos fprintf(fp, "Mutex stats (in us)\n"); 213897be3a4Schristos for (i = 0; i < stats_next; i++) { 214897be3a4Schristos fprintf(fp, "%-12s %4d: %10u %lu.%06lu %lu.%06lu %5d\n", 215897be3a4Schristos stats[i].file, stats[i].line, stats[i].count, 216897be3a4Schristos stats[i].locked_total.tv_sec, 217897be3a4Schristos stats[i].locked_total.tv_usec, 218897be3a4Schristos stats[i].wait_total.tv_sec, 219897be3a4Schristos stats[i].wait_total.tv_usec, 220897be3a4Schristos i); 221897be3a4Schristos for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) { 222897be3a4Schristos locker = &stats[i].lockers[j]; 223897be3a4Schristos if (locker->file == NULL) 224897be3a4Schristos continue; 225897be3a4Schristos fprintf(fp, " %-11s %4d: %10u %lu.%06lu %lu.%06lu %5d\n", 226897be3a4Schristos locker->file, locker->line, locker->count, 227897be3a4Schristos locker->locked_total.tv_sec, 228897be3a4Schristos locker->locked_total.tv_usec, 229897be3a4Schristos locker->wait_total.tv_sec, 230897be3a4Schristos locker->wait_total.tv_usec, 231897be3a4Schristos i); 232897be3a4Schristos } 233897be3a4Schristos } 234897be3a4Schristos } 235897be3a4Schristos 236897be3a4Schristos #endif /* ISC_MUTEX_PROFILE */ 237897be3a4Schristos 238897be3a4Schristos #if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK) 239897be3a4Schristos isc_result_t 240897be3a4Schristos isc_mutex_init_errcheck(isc_mutex_t *mp) 241897be3a4Schristos { 242897be3a4Schristos pthread_mutexattr_t attr; 243897be3a4Schristos int err; 244897be3a4Schristos 245897be3a4Schristos if (pthread_mutexattr_init(&attr) != 0) 246897be3a4Schristos return (ISC_R_UNEXPECTED); 247897be3a4Schristos 248897be3a4Schristos if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0) { 249897be3a4Schristos pthread_mutexattr_destroy(&attr); 250897be3a4Schristos return (ISC_R_UNEXPECTED); 251897be3a4Schristos } 252897be3a4Schristos 253897be3a4Schristos err = pthread_mutex_init(mp, &attr) != 0) 254897be3a4Schristos pthread_mutexattr_destroy(&attr); 255897be3a4Schristos if (err == ENOMEM) 256897be3a4Schristos return (ISC_R_NOMEMORY); 257897be3a4Schristos return ((err == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED); 258897be3a4Schristos } 259897be3a4Schristos #endif 260897be3a4Schristos 261897be3a4Schristos #if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK) 262897be3a4Schristos pthread_mutexattr_t isc__mutex_attrs = { 263897be3a4Schristos PTHREAD_MUTEX_ERRORCHECK, /* m_type */ 264897be3a4Schristos 0 /* m_flags, which appears to be unused. */ 265897be3a4Schristos }; 266897be3a4Schristos #endif 267897be3a4Schristos 268897be3a4Schristos #if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && !ISC_MUTEX_PROFILE 269897be3a4Schristos isc_result_t 270897be3a4Schristos isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) { 271897be3a4Schristos char strbuf[ISC_STRERRORSIZE]; 272897be3a4Schristos isc_result_t result = ISC_R_SUCCESS; 273897be3a4Schristos int err; 274897be3a4Schristos 275897be3a4Schristos err = pthread_mutex_init(mp, ISC__MUTEX_ATTRS); 276897be3a4Schristos if (err == ENOMEM) 277897be3a4Schristos return (ISC_R_NOMEMORY); 278897be3a4Schristos if (err != 0) { 279897be3a4Schristos isc__strerror(errno, strbuf, sizeof(strbuf)); 280897be3a4Schristos UNEXPECTED_ERROR(file, line, "isc_mutex_init() failed: %s", 281897be3a4Schristos strbuf); 282897be3a4Schristos result = ISC_R_UNEXPECTED; 283897be3a4Schristos } 284897be3a4Schristos return (result); 285897be3a4Schristos } 286897be3a4Schristos #endif 287