/contrib/bind9/lib/isc/pthreads/mutex.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 275 lines · 200 code · 46 blank · 29 comment · 48 complexity · cb2cba1746a3a237a4456808231b268a MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2008, 2011, 2012 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. /* $Id: mutex.c,v 1.18 2011/01/04 23:47:14 tbox Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <time.h>
  22. #include <sys/time.h>
  23. #include <errno.h>
  24. #include <isc/mutex.h>
  25. #include <isc/util.h>
  26. #include <isc/strerror.h>
  27. #if ISC_MUTEX_PROFILE
  28. /*@{*/
  29. /*% Operations on timevals; adapted from FreeBSD's sys/time.h */
  30. #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
  31. #define timevaladd(vvp, uvp) \
  32. do { \
  33. (vvp)->tv_sec += (uvp)->tv_sec; \
  34. (vvp)->tv_usec += (uvp)->tv_usec; \
  35. if ((vvp)->tv_usec >= 1000000) { \
  36. (vvp)->tv_sec++; \
  37. (vvp)->tv_usec -= 1000000; \
  38. } \
  39. } while (0)
  40. #define timevalsub(vvp, uvp) \
  41. do { \
  42. (vvp)->tv_sec -= (uvp)->tv_sec; \
  43. (vvp)->tv_usec -= (uvp)->tv_usec; \
  44. if ((vvp)->tv_usec < 0) { \
  45. (vvp)->tv_sec--; \
  46. (vvp)->tv_usec += 1000000; \
  47. } \
  48. } while (0)
  49. /*@}*/
  50. #define ISC_MUTEX_MAX_LOCKERS 32
  51. typedef struct {
  52. const char * file;
  53. int line;
  54. unsigned count;
  55. struct timeval locked_total;
  56. struct timeval wait_total;
  57. } isc_mutexlocker_t;
  58. struct isc_mutexstats {
  59. const char * file; /*%< File mutex was created in. */
  60. int line; /*%< Line mutex was created on. */
  61. unsigned count;
  62. struct timeval lock_t;
  63. struct timeval locked_total;
  64. struct timeval wait_total;
  65. isc_mutexlocker_t * cur_locker;
  66. isc_mutexlocker_t lockers[ISC_MUTEX_MAX_LOCKERS];
  67. };
  68. #ifndef ISC_MUTEX_PROFTABLESIZE
  69. #define ISC_MUTEX_PROFTABLESIZE (1024 * 1024)
  70. #endif
  71. static isc_mutexstats_t stats[ISC_MUTEX_PROFTABLESIZE];
  72. static int stats_next = 0;
  73. static isc_boolean_t stats_init = ISC_FALSE;
  74. static pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;
  75. isc_result_t
  76. isc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
  77. int i, err;
  78. err = pthread_mutex_init(&mp->mutex, NULL);
  79. if (err == ENOMEM)
  80. return (ISC_R_NOMEMORY);
  81. if (err != 0)
  82. return (ISC_R_UNEXPECTED);
  83. RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);
  84. if (stats_init == ISC_FALSE)
  85. stats_init = ISC_TRUE;
  86. /*
  87. * If all statistics entries have been used, give up and trigger an
  88. * assertion failure. There would be no other way to deal with this
  89. * because we'd like to keep record of all locks for the purpose of
  90. * debugging and the number of necessary locks is unpredictable.
  91. * If this failure is triggered while debugging, named should be
  92. * rebuilt with an increased ISC_MUTEX_PROFTABLESIZE.
  93. */
  94. RUNTIME_CHECK(stats_next < ISC_MUTEX_PROFTABLESIZE);
  95. mp->stats = &stats[stats_next++];
  96. RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);
  97. mp->stats->file = file;
  98. mp->stats->line = line;
  99. mp->stats->count = 0;
  100. timevalclear(&mp->stats->locked_total);
  101. timevalclear(&mp->stats->wait_total);
  102. for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
  103. mp->stats->lockers[i].file = NULL;
  104. mp->stats->lockers[i].line = 0;
  105. mp->stats->lockers[i].count = 0;
  106. timevalclear(&mp->stats->lockers[i].locked_total);
  107. timevalclear(&mp->stats->lockers[i].wait_total);
  108. }
  109. return (ISC_R_SUCCESS);
  110. }
  111. isc_result_t
  112. isc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
  113. struct timeval prelock_t;
  114. struct timeval postlock_t;
  115. isc_mutexlocker_t *locker = NULL;
  116. int i;
  117. gettimeofday(&prelock_t, NULL);
  118. if (pthread_mutex_lock(&mp->mutex) != 0)
  119. return (ISC_R_UNEXPECTED);
  120. gettimeofday(&postlock_t, NULL);
  121. mp->stats->lock_t = postlock_t;
  122. timevalsub(&postlock_t, &prelock_t);
  123. mp->stats->count++;
  124. timevaladd(&mp->stats->wait_total, &postlock_t);
  125. for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
  126. if (mp->stats->lockers[i].file == NULL) {
  127. locker = &mp->stats->lockers[i];
  128. locker->file = file;
  129. locker->line = line;
  130. break;
  131. } else if (mp->stats->lockers[i].file == file &&
  132. mp->stats->lockers[i].line == line) {
  133. locker = &mp->stats->lockers[i];
  134. break;
  135. }
  136. }
  137. if (locker != NULL) {
  138. locker->count++;
  139. timevaladd(&locker->wait_total, &postlock_t);
  140. }
  141. mp->stats->cur_locker = locker;
  142. return (ISC_R_SUCCESS);
  143. }
  144. isc_result_t
  145. isc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
  146. struct timeval unlock_t;
  147. UNUSED(file);
  148. UNUSED(line);
  149. if (mp->stats->cur_locker != NULL) {
  150. gettimeofday(&unlock_t, NULL);
  151. timevalsub(&unlock_t, &mp->stats->lock_t);
  152. timevaladd(&mp->stats->locked_total, &unlock_t);
  153. timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
  154. mp->stats->cur_locker = NULL;
  155. }
  156. return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \
  157. ISC_R_SUCCESS : ISC_R_UNEXPECTED);
  158. }
  159. void
  160. isc_mutex_statsprofile(FILE *fp) {
  161. isc_mutexlocker_t *locker;
  162. int i, j;
  163. fprintf(fp, "Mutex stats (in us)\n");
  164. for (i = 0; i < stats_next; i++) {
  165. fprintf(fp, "%-12s %4d: %10u %lu.%06lu %lu.%06lu %5d\n",
  166. stats[i].file, stats[i].line, stats[i].count,
  167. stats[i].locked_total.tv_sec,
  168. stats[i].locked_total.tv_usec,
  169. stats[i].wait_total.tv_sec,
  170. stats[i].wait_total.tv_usec,
  171. i);
  172. for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
  173. locker = &stats[i].lockers[j];
  174. if (locker->file == NULL)
  175. continue;
  176. fprintf(fp, " %-11s %4d: %10u %lu.%06lu %lu.%06lu %5d\n",
  177. locker->file, locker->line, locker->count,
  178. locker->locked_total.tv_sec,
  179. locker->locked_total.tv_usec,
  180. locker->wait_total.tv_sec,
  181. locker->wait_total.tv_usec,
  182. i);
  183. }
  184. }
  185. }
  186. #endif /* ISC_MUTEX_PROFILE */
  187. #if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
  188. isc_result_t
  189. isc_mutex_init_errcheck(isc_mutex_t *mp)
  190. {
  191. pthread_mutexattr_t attr;
  192. int err;
  193. if (pthread_mutexattr_init(&attr) != 0)
  194. return (ISC_R_UNEXPECTED);
  195. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0) {
  196. pthread_mutexattr_destroy(&attr);
  197. return (ISC_R_UNEXPECTED);
  198. }
  199. err = pthread_mutex_init(mp, &attr) != 0)
  200. pthread_mutexattr_destroy(&attr);
  201. if (err == ENOMEM)
  202. return (ISC_R_NOMEMORY);
  203. return ((err == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED);
  204. }
  205. #endif
  206. #if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
  207. pthread_mutexattr_t isc__mutex_attrs = {
  208. PTHREAD_MUTEX_ERRORCHECK, /* m_type */
  209. 0 /* m_flags, which appears to be unused. */
  210. };
  211. #endif
  212. #if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && !ISC_MUTEX_PROFILE
  213. isc_result_t
  214. isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
  215. char strbuf[ISC_STRERRORSIZE];
  216. isc_result_t result = ISC_R_SUCCESS;
  217. int err;
  218. err = pthread_mutex_init(mp, ISC__MUTEX_ATTRS);
  219. if (err == ENOMEM)
  220. return (ISC_R_NOMEMORY);
  221. if (err != 0) {
  222. isc__strerror(errno, strbuf, sizeof(strbuf));
  223. UNEXPECTED_ERROR(file, line, "isc_mutex_init() failed: %s",
  224. strbuf);
  225. result = ISC_R_UNEXPECTED;
  226. }
  227. return (result);
  228. }
  229. #endif