Show the number of times a lock was acquired in lock profiler
[strongswan.git] / src / libstrongswan / utils / mutex.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #define _GNU_SOURCE
18 #include <pthread.h>
19 #include <stdint.h>
20 #include <time.h>
21 #include <errno.h>
22
23 #include "mutex.h"
24
25 #include <library.h>
26 #include <debug.h>
27
28 typedef struct private_mutex_t private_mutex_t;
29 typedef struct private_r_mutex_t private_r_mutex_t;
30 typedef struct private_condvar_t private_condvar_t;
31 typedef struct private_rwlock_t private_rwlock_t;
32
33 #ifdef LOCK_PROFILER
34
35 /**
36 * Do not report mutexes with an overall waiting time smaller than this (in us)
37 */
38 #define PROFILE_WAIT_TRESHHOLD 10000
39
40 /**
41 * Do not report mutexes with an overall lock count smaller than this
42 */
43 #define PROFILE_LOCK_TRESHHOLD 1000
44
45
46 #include <utils/backtrace.h>
47
48 typedef struct lock_profile_t lock_profile_t;
49
50 struct lock_profile_t {
51
52 /**
53 * how long threads have waited for the lock in this mutex so far
54 */
55 timeval_t waited;
56
57 /**
58 * How many times the lock has been invoked
59 */
60 u_int locked;
61
62 /**
63 * backtrace where mutex has been created
64 */
65 backtrace_t *backtrace;
66 };
67
68 /**
69 * Print and cleanup mutex profiler
70 */
71 static void profiler_cleanup(lock_profile_t *profile)
72 {
73 if (profile->waited.tv_sec > 0 ||
74 profile->waited.tv_usec > PROFILE_WAIT_TRESHHOLD ||
75 profile->locked > PROFILE_LOCK_TRESHHOLD)
76 {
77 fprintf(stderr, "%d.%03ds / %d times in lock created at:",
78 profile->waited.tv_sec, profile->waited.tv_usec, profile->locked);
79 profile->backtrace->log(profile->backtrace, stderr);
80 }
81 profile->backtrace->destroy(profile->backtrace);
82 }
83
84 /**
85 * Initialize mutex profiler
86 */
87 static void profiler_init(lock_profile_t *profile)
88 {
89 profile->backtrace = backtrace_create(2);
90 timerclear(&profile->waited);
91 profile->locked = 0;
92 }
93
94 #define profiler_start(profile) { \
95 struct timeval _start, _end, _diff; \
96 (profile)->locked++; \
97 time_monotonic(&_start);
98
99 #define profiler_end(profile) \
100 time_monotonic(&_end); \
101 timersub(&_end, &_start, &_diff); \
102 timeradd(&(profile)->waited, &_diff, &(profile)->waited); }
103
104 #else /* !LOCK_PROFILER */
105
106 #define lock_profile_t struct {}
107 #define profiler_cleanup(...) {}
108 #define profiler_init(...) {}
109 #define profiler_start(...) {}
110 #define profiler_end(...) {}
111
112 #endif /* LOCK_PROFILER */
113
114 /**
115 * private data of mutex
116 */
117 struct private_mutex_t {
118
119 /**
120 * public functions
121 */
122 mutex_t public;
123
124 /**
125 * wrapped pthread mutex
126 */
127 pthread_mutex_t mutex;
128
129 /**
130 * is this a recursiv emutex, implementing private_r_mutex_t?
131 */
132 bool recursive;
133
134 /**
135 * profiling info, if enabled
136 */
137 lock_profile_t profile;
138 };
139
140 /**
141 * private data of mutex, extended by recursive locking information
142 */
143 struct private_r_mutex_t {
144
145 /**
146 * Extends private_mutex_t
147 */
148 private_mutex_t generic;
149
150 /**
151 * thread which currently owns mutex
152 */
153 pthread_t thread;
154
155 /**
156 * times we have locked the lock, stored per thread
157 */
158 pthread_key_t times;
159 };
160
161 /**
162 * private data of condvar
163 */
164 struct private_condvar_t {
165
166 /**
167 * public functions
168 */
169 condvar_t public;
170
171 /**
172 * wrapped pthread condvar
173 */
174 pthread_cond_t condvar;
175 };
176
177 /**
178 * private data of rwlock
179 */
180 struct private_rwlock_t {
181
182 /**
183 * public functions
184 */
185 rwlock_t public;
186
187 /**
188 * wrapped pthread rwlock
189 */
190 pthread_rwlock_t rwlock;
191
192 /**
193 * profiling info, if enabled
194 */
195 lock_profile_t profile;
196 };
197
198 /**
199 * Implementation of mutex_t.lock.
200 */
201 static void lock(private_mutex_t *this)
202 {
203 int err;
204
205 profiler_start(&this->profile);
206 err = pthread_mutex_lock(&this->mutex);
207 if (err)
208 {
209 DBG1("!!! MUTEX LOCK ERROR: %s !!!", strerror(err));
210 }
211 profiler_end(&this->profile);
212 }
213
214 /**
215 * Implementation of mutex_t.unlock.
216 */
217 static void unlock(private_mutex_t *this)
218 {
219 int err;
220
221 err = pthread_mutex_unlock(&this->mutex);
222 if (err)
223 {
224 DBG1("!!! MUTEX UNLOCK ERROR: %s !!!", strerror(err));
225 }
226 }
227
228 /**
229 * Implementation of mutex_t.lock.
230 */
231 static void lock_r(private_r_mutex_t *this)
232 {
233 pthread_t self = pthread_self();
234
235 if (this->thread == self)
236 {
237 uintptr_t times;
238
239 /* times++ */
240 times = (uintptr_t)pthread_getspecific(this->times);
241 pthread_setspecific(this->times, (void*)times + 1);
242 }
243 else
244 {
245 lock(&this->generic);
246 this->thread = self;
247 /* times = 1 */
248 pthread_setspecific(this->times, (void*)1);
249 }
250 }
251
252 /**
253 * Implementation of mutex_t.unlock.
254 */
255 static void unlock_r(private_r_mutex_t *this)
256 {
257 uintptr_t times;
258
259 /* times-- */
260 times = (uintptr_t)pthread_getspecific(this->times);
261 pthread_setspecific(this->times, (void*)--times);
262
263 if (times == 0)
264 {
265 this->thread = 0;
266 unlock(&this->generic);
267 }
268 }
269
270 /**
271 * Implementation of mutex_t.destroy
272 */
273 static void mutex_destroy(private_mutex_t *this)
274 {
275 profiler_cleanup(&this->profile);
276 pthread_mutex_destroy(&this->mutex);
277 free(this);
278 }
279
280 /**
281 * Implementation of mutex_t.destroy for recursive mutex'
282 */
283 static void mutex_destroy_r(private_r_mutex_t *this)
284 {
285 profiler_cleanup(&this->generic.profile);
286 pthread_mutex_destroy(&this->generic.mutex);
287 pthread_key_delete(this->times);
288 free(this);
289 }
290
291 /*
292 * see header file
293 */
294 mutex_t *mutex_create(mutex_type_t type)
295 {
296 switch (type)
297 {
298 case MUTEX_TYPE_RECURSIVE:
299 {
300 private_r_mutex_t *this = malloc_thing(private_r_mutex_t);
301
302 this->generic.public.lock = (void(*)(mutex_t*))lock_r;
303 this->generic.public.unlock = (void(*)(mutex_t*))unlock_r;
304 this->generic.public.destroy = (void(*)(mutex_t*))mutex_destroy_r;
305
306 pthread_mutex_init(&this->generic.mutex, NULL);
307 pthread_key_create(&this->times, NULL);
308 this->generic.recursive = TRUE;
309 profiler_init(&this->generic.profile);
310 this->thread = 0;
311
312 return &this->generic.public;
313 }
314 case MUTEX_TYPE_DEFAULT:
315 default:
316 {
317 private_mutex_t *this = malloc_thing(private_mutex_t);
318
319 this->public.lock = (void(*)(mutex_t*))lock;
320 this->public.unlock = (void(*)(mutex_t*))unlock;
321 this->public.destroy = (void(*)(mutex_t*))mutex_destroy;
322
323 pthread_mutex_init(&this->mutex, NULL);
324 this->recursive = FALSE;
325 profiler_init(&this->profile);
326
327 return &this->public;
328 }
329 }
330 }
331
332 /**
333 * Implementation of condvar_t.wait.
334 */
335 static void _wait(private_condvar_t *this, private_mutex_t *mutex)
336 {
337 if (mutex->recursive)
338 {
339 private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
340
341 /* mutex owner gets cleared during condvar wait */
342 recursive->thread = 0;
343 pthread_cond_wait(&this->condvar, &mutex->mutex);
344 recursive->thread = pthread_self();
345 }
346 else
347 {
348 pthread_cond_wait(&this->condvar, &mutex->mutex);
349 }
350 }
351
352 /**
353 * Implementation of condvar_t.timed_wait_abs.
354 */
355 static bool timed_wait_abs(private_condvar_t *this, private_mutex_t *mutex,
356 timeval_t time)
357 {
358 struct timespec ts;
359 bool timed_out;
360
361 ts.tv_sec = time.tv_sec;
362 ts.tv_nsec = time.tv_usec * 1000;
363
364 if (mutex->recursive)
365 {
366 private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
367
368 recursive->thread = 0;
369 timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
370 &ts) == ETIMEDOUT;
371 recursive->thread = pthread_self();
372 }
373 else
374 {
375 timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
376 &ts) == ETIMEDOUT;
377 }
378 return timed_out;
379 }
380
381 /**
382 * Implementation of condvar_t.timed_wait.
383 */
384 static bool timed_wait(private_condvar_t *this, private_mutex_t *mutex,
385 u_int timeout)
386 {
387 timeval_t tv;
388 u_int s, ms;
389
390 time_monotonic(&tv);
391
392 s = timeout / 1000;
393 ms = timeout % 1000;
394
395 tv.tv_sec += s;
396 tv.tv_usec += ms * 1000;
397
398 if (tv.tv_usec > 1000000 /* 1s */)
399 {
400 tv.tv_usec -= 1000000;
401 tv.tv_sec++;
402 }
403 return timed_wait_abs(this, mutex, tv);
404 }
405
406 /**
407 * Implementation of condvar_t.signal.
408 */
409 static void _signal(private_condvar_t *this)
410 {
411 pthread_cond_signal(&this->condvar);
412 }
413
414 /**
415 * Implementation of condvar_t.broadcast.
416 */
417 static void broadcast(private_condvar_t *this)
418 {
419 pthread_cond_broadcast(&this->condvar);
420 }
421
422 /**
423 * Implementation of condvar_t.destroy
424 */
425 static void condvar_destroy(private_condvar_t *this)
426 {
427 pthread_cond_destroy(&this->condvar);
428 free(this);
429 }
430
431 /*
432 * see header file
433 */
434 condvar_t *condvar_create(condvar_type_t type)
435 {
436 switch (type)
437 {
438 case CONDVAR_TYPE_DEFAULT:
439 default:
440 {
441 pthread_condattr_t condattr;
442 private_condvar_t *this = malloc_thing(private_condvar_t);
443
444 this->public.wait = (void(*)(condvar_t*, mutex_t *mutex))_wait;
445 this->public.timed_wait = (bool(*)(condvar_t*, mutex_t *mutex, u_int timeout))timed_wait;
446 this->public.timed_wait_abs = (bool(*)(condvar_t*, mutex_t *mutex, timeval_t time))timed_wait_abs;
447 this->public.signal = (void(*)(condvar_t*))_signal;
448 this->public.broadcast = (void(*)(condvar_t*))broadcast;
449 this->public.destroy = (void(*)(condvar_t*))condvar_destroy;
450
451 pthread_condattr_init(&condattr);
452 #ifdef HAVE_CONDATTR_CLOCK_MONOTONIC
453 pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
454 #endif
455 pthread_cond_init(&this->condvar, &condattr);
456 pthread_condattr_destroy(&condattr);
457
458 return &this->public;
459 }
460 }
461 }
462
463 /**
464 * Implementation of rwlock_t.read_lock
465 */
466 static void read_lock(private_rwlock_t *this)
467 {
468 int err;
469
470 profiler_start(&this->profile);
471 err = pthread_rwlock_rdlock(&this->rwlock);
472 if (err != 0)
473 {
474 DBG1("!!! RWLOCK READ LOCK ERROR: %s !!!", strerror(err));
475 }
476 profiler_end(&this->profile);
477 }
478
479 /**
480 * Implementation of rwlock_t.write_lock
481 */
482 static void write_lock(private_rwlock_t *this)
483 {
484 int err;
485
486 profiler_start(&this->profile);
487 err = pthread_rwlock_wrlock(&this->rwlock);
488 if (err != 0)
489 {
490 DBG1("!!! RWLOCK WRITE LOCK ERROR: %s !!!", strerror(err));
491 }
492 profiler_end(&this->profile);
493 }
494
495 /**
496 * Implementation of rwlock_t.try_write_lock
497 */
498 static bool try_write_lock(private_rwlock_t *this)
499 {
500 return pthread_rwlock_trywrlock(&this->rwlock) == 0;
501 }
502
503 /**
504 * Implementation of rwlock_t.unlock
505 */
506 static void rw_unlock(private_rwlock_t *this)
507 {
508 int err;
509
510 err = pthread_rwlock_unlock(&this->rwlock);
511 if (err != 0)
512 {
513 DBG1("!!! RWLOCK UNLOCK ERROR: %s !!!", strerror(err));
514 }
515 }
516
517 /**
518 * Implementation of rwlock_t.destroy
519 */
520 static void rw_destroy(private_rwlock_t *this)
521 {
522 pthread_rwlock_destroy(&this->rwlock);
523 profiler_cleanup(&this->profile);
524 free(this);
525 }
526
527 /*
528 * see header file
529 */
530 rwlock_t *rwlock_create(rwlock_type_t type)
531 {
532 switch (type)
533 {
534 case RWLOCK_TYPE_DEFAULT:
535 default:
536 {
537 private_rwlock_t *this = malloc_thing(private_rwlock_t);
538
539 this->public.read_lock = (void(*)(rwlock_t*))read_lock;
540 this->public.write_lock = (void(*)(rwlock_t*))write_lock;
541 this->public.try_write_lock = (bool(*)(rwlock_t*))try_write_lock;
542 this->public.unlock = (void(*)(rwlock_t*))rw_unlock;
543 this->public.destroy = (void(*)(rwlock_t*))rw_destroy;
544
545 pthread_rwlock_init(&this->rwlock, NULL);
546 profiler_init(&this->profile);
547
548 return &this->public;
549 }
550 }
551 }
552