Blame nss/lib/util/nssilock.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * nssilock.c - NSS lock instrumentation wrapper functions
Packit 40b132
 *
Packit 40b132
 * NOTE - These are not public interfaces
Packit 40b132
 *
Packit 40b132
 * Implementation Notes:
Packit 40b132
 * I've tried to make the instrumentation relatively non-intrusive.
Packit 40b132
 * To do this, I have used a single PR_LOG() call in each
Packit 40b132
 * instrumented function. There's room for improvement.
Packit 40b132
 *
Packit 40b132
 *
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "prinit.h"
Packit 40b132
#include "prerror.h"
Packit 40b132
#include "prlock.h"
Packit 40b132
#include "prmem.h"
Packit 40b132
#include "prenv.h"
Packit 40b132
#include "prcvar.h"
Packit 40b132
#include "prio.h"
Packit 40b132
Packit 40b132
#if defined(NEED_NSS_ILOCK)
Packit 40b132
#include "prlog.h"
Packit 40b132
#include "nssilock.h"
Packit 40b132
Packit 40b132
/*
Packit 40b132
** Declare the instrumented PZLock 
Packit 40b132
*/
Packit 40b132
struct pzlock_s {
Packit 40b132
    PRLock *lock;  /* the PZLock to be instrumented */
Packit 40b132
    PRIntervalTime time; /* timestamp when the lock was aquired */
Packit 40b132
    nssILockType ltype;
Packit 40b132
};
Packit 40b132
Packit 40b132
/*
Packit 40b132
** Declare the instrumented PZMonitor 
Packit 40b132
*/
Packit 40b132
struct pzmonitor_s {
Packit 40b132
    PRMonitor *mon;   /* the PZMonitor to be instrumented */
Packit 40b132
    PRIntervalTime time; /* timestamp when the monitor was aquired */
Packit 40b132
    nssILockType ltype;
Packit 40b132
};
Packit 40b132
Packit 40b132
/*
Packit 40b132
** Declare the instrumented PZCondVar
Packit 40b132
*/
Packit 40b132
struct pzcondvar_s  {
Packit 40b132
    PRCondVar   *cvar;  /* the PZCondVar to be instrumented */
Packit 40b132
    nssILockType ltype;
Packit 40b132
};
Packit 40b132
Packit 40b132
Packit 40b132
/*
Packit 40b132
** Define a CallOnce type to ensure serialized self-initialization
Packit 40b132
*/
Packit 40b132
static PRCallOnceType coNssILock;     /* CallOnce type */
Packit 40b132
static PRIntn  nssILockInitialized;   /* initialization done when 1 */
Packit 40b132
static PRLogModuleInfo *nssILog;      /* Log instrumentation to this handle */
Packit 40b132
Packit 40b132
Packit 40b132
#define NUM_TT_ENTRIES 6000000
Packit 40b132
static PRInt32  traceIndex = -1;      /* index into trace table */
Packit 40b132
static struct pzTrace_s *tt;          /* pointer to trace table */
Packit 40b132
static PRInt32  ttBufSize = (NUM_TT_ENTRIES * sizeof(struct pzTrace_s ));
Packit 40b132
static PRCondVar *ttCVar;
Packit 40b132
static PRLock    *ttLock;
Packit 40b132
static PRFileDesc *ttfd;              /* trace table file */
Packit 40b132
Packit 40b132
/*
Packit 40b132
** Vtrace() -- Trace events, write events to external media
Packit 40b132
**
Packit 40b132
** Vtrace() records traced events in an in-memory trace table
Packit 40b132
** when the trace table fills, Vtrace writes the entire table
Packit 40b132
** to a file.
Packit 40b132
**
Packit 40b132
** data can be lost!
Packit 40b132
**
Packit 40b132
*/
Packit 40b132
static void Vtrace(
Packit 40b132
    nssILockOp      op,
Packit 40b132
    nssILockType    ltype,
Packit 40b132
    PRIntervalTime  callTime,
Packit 40b132
    PRIntervalTime  heldTime,
Packit 40b132
    void            *lock,
Packit 40b132
    PRIntn          line,
Packit 40b132
    char            *file
Packit 40b132
)  {
Packit 40b132
    PRInt32 idx;
Packit 40b132
    struct pzTrace_s *tp;
Packit 40b132
Packit 40b132
RetryTrace:
Packit 40b132
    idx = PR_ATOMIC_INCREMENT( &traceIndex );
Packit 40b132
    while( NUM_TT_ENTRIES <= idx || op == FlushTT ) {
Packit 40b132
        if( NUM_TT_ENTRIES == idx  || op == FlushTT )  {
Packit 40b132
            int writeSize = idx * sizeof(struct pzTrace_s);
Packit 40b132
            PR_Lock(ttLock);
Packit 40b132
            PR_Write( ttfd, tt, writeSize );
Packit 40b132
            traceIndex = -1;
Packit 40b132
            PR_NotifyAllCondVar( ttCVar );
Packit 40b132
            PR_Unlock(ttLock);
Packit 40b132
            goto RetryTrace;
Packit 40b132
        } else {
Packit 40b132
            PR_Lock(ttLock);
Packit 40b132
            while( NUM_TT_ENTRIES < idx )
Packit 40b132
                PR_WaitCondVar(ttCVar, PR_INTERVAL_NO_WAIT);
Packit 40b132
            PR_Unlock(ttLock);
Packit 40b132
            goto RetryTrace;
Packit 40b132
        }
Packit 40b132
    } /* end while() */
Packit 40b132
Packit 40b132
    /* create the trace entry */
Packit 40b132
    tp = tt + idx;
Packit 40b132
    tp->threadID = PR_GetThreadID(PR_GetCurrentThread());
Packit 40b132
    tp->op = op;
Packit 40b132
    tp->ltype = ltype;
Packit 40b132
    tp->callTime = callTime;
Packit 40b132
    tp->heldTime = heldTime;
Packit 40b132
    tp->lock = lock;
Packit 40b132
    tp ->line = line;
Packit 40b132
    strcpy(tp->file, file );
Packit 40b132
    return;
Packit 40b132
} /* --- end Vtrace() --- */
Packit 40b132
Packit 40b132
/*
Packit 40b132
** pz_TraceFlush() -- Force trace table write to file
Packit 40b132
**
Packit 40b132
*/
Packit 40b132
extern void pz_TraceFlush( void )
Packit 40b132
{
Packit 40b132
    Vtrace( FlushTT, nssILockSelfServ, 0, 0, NULL, 0, "" );
Packit 40b132
    return;
Packit 40b132
} /* --- end pz_TraceFlush() --- */
Packit 40b132
Packit 40b132
/*
Packit 40b132
** nssILockInit() -- Initialization for nssilock
Packit 40b132
**
Packit 40b132
** This function is called from the CallOnce mechanism.
Packit 40b132
*/
Packit 40b132
static PRStatus
Packit 40b132
    nssILockInit( void ) 
Packit 40b132
{   
Packit 40b132
    int i;
Packit 40b132
    nssILockInitialized = 1;
Packit 40b132
Packit 40b132
    /* new log module */
Packit 40b132
    nssILog = PR_NewLogModule("nssilock");
Packit 40b132
    if ( NULL == nssILog )  {
Packit 40b132
        return(PR_FAILURE);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    tt = PR_Calloc( NUM_TT_ENTRIES, sizeof(struct pzTrace_s));
Packit 40b132
    if (NULL == tt ) {
Packit 40b132
        fprintf(stderr, "nssilock: can't allocate trace table\n");
Packit 40b132
        exit(1);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    ttfd = PR_Open( "xxxTTLog", PR_CREATE_FILE | PR_WRONLY, 0666 );
Packit 40b132
    if ( NULL == ttfd )  {
Packit 40b132
        fprintf( stderr, "Oh Drat! Can't open 'xxxTTLog'\n");
Packit 40b132
        exit(1);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    ttLock = PR_NewLock();
Packit 40b132
    ttCVar = PR_NewCondVar(ttLock);
Packit 40b132
Packit 40b132
    return(PR_SUCCESS);
Packit 40b132
} /* --- end nssILockInit() --- */
Packit 40b132
Packit 40b132
extern PZLock * pz_NewLock( 
Packit 40b132
    nssILockType ltype,
Packit 40b132
    char *file,  
Packit 40b132
    PRIntn line )
Packit 40b132
{
Packit 40b132
    PRStatus rc;
Packit 40b132
    PZLock  *lock;
Packit 40b132
    
Packit 40b132
    /* Self Initialize the nssILock feature */
Packit 40b132
    if (!nssILockInitialized)  {
Packit 40b132
        rc = PR_CallOnce( &coNssILock, nssILockInit );
Packit 40b132
        if ( PR_FAILURE == rc ) {
Packit 40b132
            PR_SetError( PR_UNKNOWN_ERROR, 0 );
Packit 40b132
            return( NULL );
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
Packit 40b132
    lock = PR_NEWZAP( PZLock );
Packit 40b132
    if ( NULL != lock )  {
Packit 40b132
        lock->ltype = ltype;
Packit 40b132
        lock->lock = PR_NewLock();
Packit 40b132
        if ( NULL == lock->lock )  {
Packit 40b132
            PR_DELETE( lock );
Packit 40b132
            PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
        }
Packit 40b132
    } else {
Packit 40b132
            PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    Vtrace( NewLock, ltype, 0, 0, lock, line, file );
Packit 40b132
    return(lock);
Packit 40b132
} /* --- end pz_NewLock() --- */
Packit 40b132
Packit 40b132
extern void
Packit 40b132
    pz_Lock(
Packit 40b132
        PZLock *lock,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{            
Packit 40b132
    PRIntervalTime callTime;
Packit 40b132
Packit 40b132
    callTime = PR_IntervalNow();
Packit 40b132
    PR_Lock( lock->lock );
Packit 40b132
    lock->time = PR_IntervalNow();
Packit 40b132
    callTime = lock->time - callTime;
Packit 40b132
Packit 40b132
    Vtrace( Lock, lock->ltype, callTime, 0, lock, line, file );
Packit 40b132
    return;
Packit 40b132
} /* --- end  pz_Lock() --- */
Packit 40b132
Packit 40b132
extern PRStatus
Packit 40b132
    pz_Unlock(
Packit 40b132
        PZLock *lock,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    ) 
Packit 40b132
{
Packit 40b132
    PRStatus rc;
Packit 40b132
    PRIntervalTime callTime, now, heldTime;
Packit 40b132
Packit 40b132
    callTime = PR_IntervalNow();
Packit 40b132
    rc = PR_Unlock( lock->lock );
Packit 40b132
    now = PR_IntervalNow(); 
Packit 40b132
    callTime = now - callTime;
Packit 40b132
    heldTime = now - lock->time;
Packit 40b132
    Vtrace( Unlock, lock->ltype, callTime, heldTime, lock, line, file );
Packit 40b132
    return( rc );
Packit 40b132
} /* --- end  pz_Unlock() --- */
Packit 40b132
Packit 40b132
extern void
Packit 40b132
    pz_DestroyLock(
Packit 40b132
        PZLock *lock,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    Vtrace( DestroyLock, lock->ltype, 0, 0, lock, line, file );
Packit 40b132
    PR_DestroyLock( lock->lock );
Packit 40b132
    PR_DELETE( lock );
Packit 40b132
    return;
Packit 40b132
} /* --- end  pz_DestroyLock() --- */
Packit 40b132
Packit 40b132
Packit 40b132
Packit 40b132
extern PZCondVar *
Packit 40b132
    pz_NewCondVar(
Packit 40b132
        PZLock *lock,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PZCondVar *cvar;
Packit 40b132
Packit 40b132
    cvar = PR_NEWZAP( PZCondVar );
Packit 40b132
    if ( NULL == cvar ) {
Packit 40b132
        PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
    } else {
Packit 40b132
        cvar->ltype = lock->ltype; 
Packit 40b132
        cvar->cvar = PR_NewCondVar( lock->lock );
Packit 40b132
        if ( NULL == cvar->cvar )  {
Packit 40b132
            PR_DELETE( cvar );
Packit 40b132
            PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
        }
Packit 40b132
Packit 40b132
    }
Packit 40b132
    Vtrace( NewCondVar, lock->ltype, 0, 0, cvar, line, file );
Packit 40b132
    return( cvar );
Packit 40b132
} /* --- end  pz_NewCondVar() --- */
Packit 40b132
Packit 40b132
extern void
Packit 40b132
    pz_DestroyCondVar(
Packit 40b132
        PZCondVar *cvar,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    Vtrace( DestroyCondVar, cvar->ltype, 0, 0, cvar, line, file );
Packit 40b132
    PR_DestroyCondVar( cvar->cvar );
Packit 40b132
    PR_DELETE( cvar );
Packit 40b132
} /* --- end  pz_DestroyCondVar() --- */
Packit 40b132
Packit 40b132
extern PRStatus
Packit 40b132
    pz_WaitCondVar(
Packit 40b132
        PZCondVar *cvar,
Packit 40b132
        PRIntervalTime timeout,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PRStatus    rc;
Packit 40b132
    PRIntervalTime callTime;
Packit 40b132
Packit 40b132
    callTime = PR_IntervalNow();
Packit 40b132
    rc = PR_WaitCondVar( cvar->cvar, timeout );
Packit 40b132
    callTime = PR_IntervalNow() - callTime;
Packit 40b132
    
Packit 40b132
    Vtrace( WaitCondVar, cvar->ltype, callTime, 0, cvar, line, file );
Packit 40b132
    return(rc);
Packit 40b132
} /* --- end  pz_WaitCondVar() --- */
Packit 40b132
Packit 40b132
extern PRStatus
Packit 40b132
    pz_NotifyCondVar(
Packit 40b132
        PZCondVar *cvar,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PRStatus    rc;
Packit 40b132
    
Packit 40b132
    rc = PR_NotifyCondVar( cvar->cvar );
Packit 40b132
    
Packit 40b132
    Vtrace( NotifyCondVar, cvar->ltype, 0, 0, cvar, line, file );
Packit 40b132
    return(rc);
Packit 40b132
} /* --- end  pz_NotifyCondVar() --- */
Packit 40b132
Packit 40b132
extern PRStatus
Packit 40b132
    pz_NotifyAllCondVar(
Packit 40b132
        PZCondVar *cvar,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PRStatus    rc;
Packit 40b132
    
Packit 40b132
    rc = PR_NotifyAllCondVar( cvar->cvar );
Packit 40b132
    
Packit 40b132
    Vtrace( NotifyAllCondVar, cvar->ltype, 0, 0, cvar, line, file );
Packit 40b132
    return(rc);
Packit 40b132
} /* --- end  pz_NotifyAllCondVar() --- */
Packit 40b132
Packit 40b132
extern PZMonitor *
Packit 40b132
    pz_NewMonitor( 
Packit 40b132
        nssILockType ltype,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PRStatus rc;
Packit 40b132
    PZMonitor   *mon;
Packit 40b132
Packit 40b132
    /* Self Initialize the nssILock feature */
Packit 40b132
    if (!nssILockInitialized)  {
Packit 40b132
        rc = PR_CallOnce( &coNssILock, nssILockInit );
Packit 40b132
        if ( PR_FAILURE == rc ) {
Packit 40b132
            PR_SetError( PR_UNKNOWN_ERROR, 0 );
Packit 40b132
            return( NULL );
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
Packit 40b132
    mon = PR_NEWZAP( PZMonitor );
Packit 40b132
    if ( NULL != mon )  {
Packit 40b132
        mon->ltype = ltype;
Packit 40b132
        mon->mon = PR_NewMonitor();
Packit 40b132
        if ( NULL == mon->mon )  {
Packit 40b132
            PR_DELETE( mon );
Packit 40b132
            PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
        }
Packit 40b132
    } else {
Packit 40b132
        PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    Vtrace( NewMonitor, ltype, 0, 0, mon, line, file );
Packit 40b132
    return(mon);
Packit 40b132
} /* --- end  pz_NewMonitor() --- */
Packit 40b132
Packit 40b132
extern void
Packit 40b132
    pz_DestroyMonitor(
Packit 40b132
        PZMonitor *mon,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    Vtrace( DestroyMonitor, mon->ltype, 0, 0, mon, line, file );
Packit 40b132
    PR_DestroyMonitor( mon->mon );
Packit 40b132
    PR_DELETE( mon );
Packit 40b132
    return;                
Packit 40b132
} /* --- end  pz_DestroyMonitor() --- */
Packit 40b132
Packit 40b132
extern void
Packit 40b132
    pz_EnterMonitor(
Packit 40b132
        PZMonitor *mon,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PRIntervalTime callTime, now;
Packit 40b132
Packit 40b132
    callTime = PR_IntervalNow();
Packit 40b132
    PR_EnterMonitor( mon->mon );
Packit 40b132
    now = PR_IntervalNow();
Packit 40b132
    callTime = now - callTime;
Packit 40b132
    if ( PR_GetMonitorEntryCount(mon->mon) == 1 )  {
Packit 40b132
        mon->time = now;
Packit 40b132
    }
Packit 40b132
    Vtrace( EnterMonitor, mon->ltype, callTime, 0, mon, line, file );
Packit 40b132
    return;
Packit 40b132
} /* --- end  pz_EnterMonitor() --- */
Packit 40b132
Packit 40b132
extern PRStatus
Packit 40b132
    pz_ExitMonitor(
Packit 40b132
        PZMonitor *mon,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PRStatus rc;
Packit 40b132
    PRIntervalTime callTime, now, heldTime;
Packit 40b132
    PRIntn  mec = PR_GetMonitorEntryCount( mon->mon );
Packit 40b132
   
Packit 40b132
    heldTime = (PRIntervalTime)-1; 
Packit 40b132
    callTime = PR_IntervalNow();
Packit 40b132
    rc = PR_ExitMonitor( mon->mon );
Packit 40b132
    now = PR_IntervalNow();
Packit 40b132
    callTime = now - callTime;
Packit 40b132
    if ( mec == 1 )
Packit 40b132
        heldTime = now - mon->time;
Packit 40b132
    Vtrace( ExitMonitor, mon->ltype, callTime, heldTime, mon, line, file );
Packit 40b132
    return( rc );
Packit 40b132
} /* --- end  pz_ExitMonitor() --- */
Packit 40b132
Packit 40b132
extern PRIntn
Packit 40b132
    pz_GetMonitorEntryCount(
Packit 40b132
        PZMonitor *mon,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    return( PR_GetMonitorEntryCount(mon->mon));
Packit 40b132
} /* --- end pz_GetMonitorEntryCount() --- */
Packit 40b132
Packit 40b132
Packit 40b132
extern PRStatus
Packit 40b132
    pz_Wait(
Packit 40b132
        PZMonitor *mon,
Packit 40b132
        PRIntervalTime ticks,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PRStatus rc;
Packit 40b132
    PRIntervalTime callTime;
Packit 40b132
Packit 40b132
    callTime = PR_IntervalNow();
Packit 40b132
    rc = PR_Wait( mon->mon, ticks );
Packit 40b132
    callTime = PR_IntervalNow() - callTime;
Packit 40b132
    Vtrace( Wait, mon->ltype, callTime, 0, mon, line, file );
Packit 40b132
    return( rc );
Packit 40b132
} /* --- end  pz_Wait() --- */
Packit 40b132
Packit 40b132
extern PRStatus
Packit 40b132
    pz_Notify(
Packit 40b132
        PZMonitor *mon,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PRStatus rc;
Packit 40b132
    PRIntervalTime callTime;
Packit 40b132
Packit 40b132
    callTime = PR_IntervalNow();
Packit 40b132
    rc = PR_Notify( mon->mon );
Packit 40b132
    callTime = PR_IntervalNow() - callTime;
Packit 40b132
    Vtrace( Notify, mon->ltype, callTime, 0, mon, line, file );
Packit 40b132
    return( rc );
Packit 40b132
} /* --- end  pz_Notify() --- */
Packit 40b132
Packit 40b132
extern PRStatus
Packit 40b132
    pz_NotifyAll(
Packit 40b132
        PZMonitor *mon,
Packit 40b132
        char *file,
Packit 40b132
        PRIntn line
Packit 40b132
    )
Packit 40b132
{
Packit 40b132
    PRStatus rc;
Packit 40b132
    PRIntervalTime callTime;
Packit 40b132
Packit 40b132
    callTime = PR_IntervalNow();
Packit 40b132
    rc = PR_NotifyAll( mon->mon );
Packit 40b132
    callTime = PR_IntervalNow() - callTime;
Packit 40b132
    Vtrace( NotifyAll, mon->ltype, callTime, 0, mon, line, file );
Packit 40b132
    return( rc );
Packit 40b132
} /* --- end  pz_NotifyAll() --- */
Packit 40b132
Packit 40b132
#endif /* NEED_NSS_ILOCK */
Packit 40b132
/* --- end nssilock.c --------------------------------- */