Changeset 4575

Show
Ignore:
Timestamp:
11/05/08 08:57:26 (2 months ago)
Author:
martin
Message:

simple mutex profiler

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/libstrongswan/utils/mutex.c

    r4449 r4575  
    4747    pthread_mutex_t mutex; 
    4848     
     49#ifdef LOCK_PROFILER 
     50    /** 
     51     * how long threads have waited for the lock in this mutex so far 
     52     */ 
     53    struct timeval waited; 
     54     
     55    /** 
     56     * creator of the mutex 
     57     */ 
     58    void *stack[10]; 
     59     
     60    /** 
     61     * number of pointers in stack 
     62     */ 
     63    int stack_size; 
     64#endif /* LOCK_PROFILER */ 
     65     
    4966    /** 
    5067     * is this a recursiv emutex, implementing private_r_mutex_t? 
     
    90107}; 
    91108 
     109#ifdef LOCK_PROFILER 
     110 
     111#include <execinfo.h> 
     112 
     113/** 
     114 * print mutex locking statistics 
     115 */ 
     116static void print_stats(private_mutex_t *this) 
     117{ 
     118    int i; 
     119 
     120    DBG1("waited %d.%06ds in mutex, created at:", 
     121         this->waited.tv_sec, this->waited.tv_usec); 
     122    for (i = 0; i < this->stack_size; i++) 
     123    { 
     124        DBG1("  %p", this->stack[i]); 
     125    } 
     126} 
     127 
     128static void init_stats(private_mutex_t *this) 
     129{ 
     130    this->stack_size = backtrace(this->stack, countof(this->stack)); 
     131    timerclear(&this->waited); 
     132} 
     133 
    92134/** 
    93135 * Implementation of mutex_t.lock. 
     
    95137static void lock(private_mutex_t *this) 
    96138{ 
     139    struct timeval start, end, diff; 
     140 
     141    gettimeofday(&start, NULL); 
    97142    if (pthread_mutex_lock(&this->mutex)) 
    98143    { 
    99144        DBG1("!!!! MUTEX %sLOCK ERROR, your code is buggy !!!", ""); 
    100145    } 
    101 
     146    gettimeofday(&end, NULL); 
     147     
     148    timersub(&end, &start, &diff); 
     149    timeradd(&this->waited, &diff, &this->waited); 
     150
     151#else /* !LOCK_PROFILER */ 
     152 
     153/** dummy implementations */ 
     154static void print_stats(private_mutex_t *this) {} 
     155static void init_stats(private_mutex_t *this) {} 
     156 
     157/** 
     158 * Implementation of mutex_t.lock. 
     159 */ 
     160static void lock(private_mutex_t *this) 
     161
     162    if (pthread_mutex_lock(&this->mutex)) 
     163    { 
     164        DBG1("!!!! MUTEX %sLOCK ERROR, your code is buggy !!!", ""); 
     165    } 
     166
     167#endif /* LOCK_PROFILER */ 
    102168 
    103169/** 
     
    159225static void mutex_destroy(private_mutex_t *this) 
    160226{ 
     227    print_stats(this); 
    161228    pthread_mutex_destroy(&this->mutex); 
    162229    free(this); 
     
    168235static void mutex_destroy_r(private_r_mutex_t *this) 
    169236{ 
     237    print_stats(&this->generic); 
    170238    pthread_mutex_destroy(&this->generic.mutex); 
    171239    pthread_key_delete(this->times); 
     
    191259            pthread_key_create(&this->times, NULL); 
    192260            this->generic.recursive = TRUE; 
     261            init_stats(&this->generic); 
    193262            this->thread = 0; 
    194263             
     
    206275            pthread_mutex_init(&this->mutex, NULL); 
    207276            this->recursive = FALSE; 
     277            init_stats(this); 
    208278             
    209279            return &this->public;