Skip to content

Commit

Permalink
lock_ops.h: added support for debugging locks
Browse files Browse the repository at this point in the history
with the flag DBG_LOCK set(from menuconfig for example), with each
lock will be stored information regarding the place from where the
lock was aquired (file, function, line)
  • Loading branch information
ionel-cerghit committed Aug 27, 2015
1 parent 24def0d commit a5e23d8
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 24 deletions.
50 changes: 46 additions & 4 deletions fastlock.h
Expand Up @@ -55,10 +55,23 @@
#endif

/*! The actual lock */
#ifndef DBG_LOCK
typedef volatile int fl_lock_t;
#else
typedef struct fl_lock_t_{
volatile int lock;
char* file;
char* func;
unsigned long line;
} fl_lock_t;
#endif

/*! Initialize a lock, zero is unlocked. */
#define init_lock( l ) (l)=0
#ifndef DBG_LOCK
#define init_lock( l ) (l)=0
#else
#define init_lock( l ) (l).lock = 0
#endif



Expand All @@ -68,7 +81,11 @@ typedef volatile int fl_lock_t;
* \return 1 if the lock is held by someone else, 0 otherwise
* \see get_lock
*/
#ifndef DBG_LOCK
inline static int tsl(fl_lock_t* lock)
#else
inline static int tsl(volatile int* lock)
#endif
{
int val;

Expand Down Expand Up @@ -167,8 +184,15 @@ inline static int tsl(fl_lock_t* lock)
* \param lock the lock that should be set
* \see tsl
*/
#ifndef DBG_LOCK
inline static void get_lock(fl_lock_t* lock)
{
#else
inline static void get_lock(fl_lock_t* lock_struct, const char* file, const char* func, unsigned int line)
{
volatile int *lock = &lock_struct->lock;
#endif

#ifdef ADAPTIVE_WAIT
int i=ADAPTIVE_WAIT_LOOPS;
#endif
Expand All @@ -182,15 +206,32 @@ inline static void get_lock(fl_lock_t* lock)
sched_yield();
#endif
}

#ifdef DBG_LOCK
lock_struct->file = (char*)file;
lock_struct->func = (char*)func;
lock_struct->line = line;
#endif

}


/*! \brief
* Release a lock
* \param lock the lock that should be released
*/
#ifndef DBG_LOCK
inline static void release_lock(fl_lock_t* lock)
{
#else
inline static void release_lock(fl_lock_t* lock_struct)
{
volatile int *lock = &lock_struct->lock;
lock_struct->file = 0;
lock_struct->func = 0;
lock_struct->line = 0;
#endif

#if defined(__CPU_i386) || defined(__CPU_x86_64)
/* char val;
val=0; */
Expand All @@ -200,9 +241,9 @@ inline static void release_lock(fl_lock_t* lock)
);
#elif defined(__CPU_sparc64) || defined(__CPU_sparc)
asm volatile(
#ifndef NOSMP
"membar #LoadStore | #StoreStore \n\t" /*is this really needed?*/
#endif
#ifndef NOSMP
"membar #LoadStore | #StoreStore \n\t" /*is this really needed?*/
#endif
"stb %%g0, [%0] \n\t"
: /*no output*/
: "r" (lock)
Expand Down Expand Up @@ -245,6 +286,7 @@ inline static void release_lock(fl_lock_t* lock)
#else
#error "unknown architecture"
#endif

}


Expand Down
47 changes: 44 additions & 3 deletions futex_lock.h
Expand Up @@ -45,7 +45,16 @@
#include <linux/futex.h>

/*! The actual lock */
#ifndef DBG_LOCK
typedef volatile int fx_lock_t;
#else
typedef struct fx_lock_t_{
volatile int lock;
char* file;
char* func;
unsigned long line;
} fx_lock_t;
#endif

/*
* Possible Lock values:
Expand All @@ -55,8 +64,11 @@ typedef volatile int fx_lock_t;
*/

/*! Initialize a lock, zero is unlocked. */
#define init_lock( l ) (l)=0

#ifndef DBG_LOCK
#define init_lock( l ) (l)=0
#else
#define init_lock( l ) (l).lock = 0
#endif
/*
* Wait on a futex
* param lock - futex to wait on
Expand Down Expand Up @@ -90,7 +102,11 @@ typedef volatile int fx_lock_t;
* param val is the value to write to the lock
* returns previous value of lock
*/
#ifndef DBG_LOCK
inline static int _atomic_xchg(fx_lock_t* lock, int val)
#else
inline static int _atomic_xchg(volatile int *lock, int val)
#endif
{
#if defined(__CPU_i386) || defined(__CPU_x86_64)

Expand Down Expand Up @@ -195,8 +211,15 @@ inline static int _atomic_xchg(fx_lock_t* lock, int val)
* Get a lock.
* \param lock the lock that should be gotten
*/
#ifndef DBG_LOCK
inline static void get_lock(fx_lock_t* lock)
{
#else
inline static void get_lock(fx_lock_t* lock_struct, const char* file, const char* func, unsigned int line)
{
volatile int *lock = &lock_struct->lock;
#endif

int c;
#ifdef ADAPTIVE_WAIT
register int i = ADAPTIVE_WAIT_LOOPS;
Expand Down Expand Up @@ -225,16 +248,34 @@ inline static void get_lock(fx_lock_t* lock)
c = atomic_xchg(lock, 2);
}
}

#ifdef DBG_LOCK
lock_struct->file = (char*)file;
lock_struct->func = (char*)func;
lock_struct->line = line;
#endif

}

/*! \brief
* Release a lock
* \param lock the lock that should be released
*/
#ifndef DBG_LOCK
inline static void release_lock(fx_lock_t* lock)
{
int c;
#else
inline static void release_lock(fx_lock_t* lock_struct)
{
volatile int *lock = &lock_struct->lock;
#endif

int c;
#ifdef DBG_LOCK
lock_struct->file = NULL;
lock_struct->func = NULL;
lock_struct->line = 0;
#endif
c = atomic_xchg(lock, 0);

//Only do wakekup if others are waiting on the lock (value of 2)
Expand Down
39 changes: 24 additions & 15 deletions lock_ops.h
Expand Up @@ -66,17 +66,18 @@

#ifdef FAST_LOCK

#ifdef USE_FUTEX
#include "futex_lock.h"
#ifdef USE_FUTEX
#include "futex_lock.h"

typedef fx_lock_t gen_lock_t;
typedef fx_lock_t gen_lock_t;

#elif defined FAST_LOCK
#include "fastlock.h"
#elif defined FAST_LOCK

typedef fl_lock_t gen_lock_t;
#include "fastlock.h"

#endif
typedef fl_lock_t gen_lock_t;

#endif

#define lock_destroy(lock) /* do nothing */

Expand All @@ -86,9 +87,14 @@ inline static gen_lock_t* lock_init(gen_lock_t* lock)
return lock;
}

#define lock_get(lock) get_lock(lock)
#define lock_release(lock) release_lock(lock)

#ifndef DBG_LOCK
#define lock_get(lock) get_lock(lock)
#else
#define lock_get(lock) get_lock(lock, __FILE__, __FUNCTION__, __LINE__)
#endif

#elif defined USE_PTHREAD_MUTEX
#include <pthread.h>

Expand All @@ -102,11 +108,10 @@ inline static gen_lock_t* lock_init(gen_lock_t* lock)
else return 0;
}


#define lock_get(lock) pthread_mutex_lock(lock)
#define lock_release(lock) pthread_mutex_unlock(lock)



#elif defined USE_POSIX_SEM
#include <semaphore.h>

Expand All @@ -116,14 +121,14 @@ typedef sem_t gen_lock_t;

inline static gen_lock_t* lock_init(gen_lock_t* lock)
{

if (sem_init(lock, 1, 1)<0) return 0;
return lock;
}

#define lock_get(lock) sem_wait(lock)
#define lock_release(lock) sem_post(lock)


#elif defined USE_SYSV_SEM
#include <sys/ipc.h>
#include <sys/sem.h>
Expand All @@ -150,9 +155,6 @@ extern int uid; /* from main.c */

typedef int gen_lock_t;




inline static gen_lock_t* lock_init(gen_lock_t* lock)
{
union semun su;
Expand All @@ -170,13 +172,15 @@ inline static gen_lock_t* lock_init(gen_lock_t* lock)
/* init error*/
return 0;
}

return lock;

}

//TODO if is init
inline static void lock_destroy(gen_lock_t* lock)
{
union semun su;

su.val = 0;
semctl(*lock, 0, IPC_RMID, su);
}
Expand All @@ -198,6 +202,7 @@ inline static void lock_get(gen_lock_t* lock)
LM_CRIT("%s (%d)\n", strerror(errno), errno);
}
}

}

inline static void lock_release(gen_lock_t* lock)
Expand All @@ -208,6 +213,7 @@ inline static void lock_release(gen_lock_t* lock)
sop.sem_op=1; /* up */
sop.sem_flg=0;
tryagain:

if (semop(*lock, &sop, 1)==-1){
if (errno==EINTR){
/* very improbable*/
Expand Down Expand Up @@ -313,6 +319,8 @@ inline static void lock_set_get(gen_lock_set_t* s, int n)
LM_CRIT("%s (%d)\n", strerror(errno), errno);
}
}

s
}

inline static void lock_set_release(gen_lock_set_t* s, int n)
Expand All @@ -331,6 +339,7 @@ inline static void lock_set_release(gen_lock_set_t* s, int n)
LM_CRIT("%s (%d)\n", strerror(errno), errno);
}
}

}
#else
#error "no lock set method selected"
Expand Down
2 changes: 1 addition & 1 deletion net/net_tcp.c
Expand Up @@ -837,7 +837,7 @@ static struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,

error:
if (c) {
if (c->write_lock) lock_destroy(&c->write_lock);
lock_destroy(&c->write_lock);
shm_free(c);
}
return 0;
Expand Down
7 changes: 6 additions & 1 deletion version.h
Expand Up @@ -159,6 +159,11 @@
#define USE_SYSV_SEM_STR ""
#endif

#ifdef DBG_LOCK
#define DBG_LOCK_STR ", DBG_LOCK"
#else
#define DBG_LOCK_STR ""
#endif

#ifdef NOSMP
#define NOSMP_STR "-NOSMP"
Expand All @@ -173,7 +178,7 @@
SHM_MMAP_STR PKG_MALLOC_STR VQ_MALLOC_STR F_MALLOC_STR \
HP_MALLOC_STR USE_SHM_MEM_STR DBG_QM_MALLOC_STR DBG_F_MALLOC_STR \
DEBUG_DMALLOC_STR QM_JOIN_FREE_STR FAST_LOCK_STR NOSMP_STR \
USE_PTHREAD_MUTEX_STR USE_POSIX_SEM_STR USE_SYSV_SEM_STR
USE_PTHREAD_MUTEX_STR USE_POSIX_SEM_STR USE_SYSV_SEM_STR DBG_LOCK_STR


#endif

0 comments on commit a5e23d8

Please sign in to comment.