Annotation of embedaddon/strongswan/src/libstrongswan/threading/lock_profiler.h, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2008 Tobias Brunner
                      3:  * Copyright (C) 2008 Martin Willi
                      4:  * HSR 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: #ifndef THREADING_LOCK_PROFILER_H_
                     18: #define THREADING_LOCK_PROFILER_H_
                     19: 
                     20: #ifdef LOCK_PROFILER
                     21: 
                     22: #include <time.h>
                     23: 
                     24: /**
                     25:  * Do not report mutexes with an overall waiting time smaller than this (in us)
                     26:  */
                     27: #define PROFILE_WAIT_TRESHOLD 10000
                     28: 
                     29: /**
                     30:  * Do not report mutexes with an overall lock count smaller than this
                     31:  */
                     32: #define PROFILE_LOCK_TRESHOLD 1000
                     33: 
                     34: #include <utils/backtrace.h>
                     35: 
                     36: typedef struct lock_profile_t lock_profile_t;
                     37: 
                     38: struct lock_profile_t {
                     39:        /**
                     40:         * how long threads have waited for the lock in this mutex so far
                     41:         */
                     42:        timeval_t waited;
                     43: 
                     44:        /**
                     45:         * How many times the lock has been invoked
                     46:         */
                     47:        u_int locked;
                     48: 
                     49:        /**
                     50:         * backtrace where mutex has been created
                     51:         */
                     52:        backtrace_t *backtrace;
                     53: };
                     54: 
                     55: /**
                     56:  * Print and cleanup mutex profiler
                     57:  */
                     58: static inline void profiler_cleanup(lock_profile_t *profile)
                     59: {
                     60:        if (profile->waited.tv_sec > 0 ||
                     61:                profile->waited.tv_usec > PROFILE_WAIT_TRESHOLD ||
                     62:                profile->locked > PROFILE_LOCK_TRESHOLD)
                     63:        {
                     64:                fprintf(stderr, "%d.%03ds / %d times in lock created at:",
                     65:                        profile->waited.tv_sec, profile->waited.tv_usec, profile->locked);
                     66:                profile->backtrace->log(profile->backtrace, stderr, TRUE);
                     67:        }
                     68:        profile->backtrace->destroy(profile->backtrace);
                     69: }
                     70: 
                     71: /**
                     72:  * Initialize mutex profiler
                     73:  */
                     74: static inline void profiler_init(lock_profile_t *profile)
                     75: {
                     76:        profile->backtrace = backtrace_create(2);
                     77:        timerclear(&profile->waited);
                     78:        profile->locked = 0;
                     79: }
                     80: 
                     81: #define profiler_start(profile) { \
                     82:        struct timeval _start, _end, _diff; \
                     83:        (profile)->locked++; \
                     84:        time_monotonic(&_start);
                     85: 
                     86: #define profiler_end(profile) \
                     87:        time_monotonic(&_end); \
                     88:        timersub(&_end, &_start, &_diff); \
                     89:        timeradd(&(profile)->waited, &_diff, &(profile)->waited); }
                     90: 
                     91: #else /* !LOCK_PROFILER */
                     92: 
                     93: #define lock_profile_t struct {}
                     94: #define profiler_cleanup(...) {}
                     95: #define profiler_init(...) {}
                     96: #define profiler_start(...) {}
                     97: #define profiler_end(...) {}
                     98: 
                     99: #endif /* LOCK_PROFILER */
                    100: 
                    101: #endif /* THREADING_LOCK_PROFILER_H_ */
                    102: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>