DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * nssilock.c - NSS lock instrumentation wrapper functions
 *
 * NOTE - These are not public interfaces
 *
 * Implementation Notes:
 * I've tried to make the instrumentation relatively non-intrusive.
 * To do this, I have used a single PR_LOG() call in each
 * instrumented function. There's room for improvement.
 *
 *
 */

#include "prinit.h"
#include "prerror.h"
#include "prlock.h"
#include "prmem.h"
#include "prenv.h"
#include "prcvar.h"
#include "prio.h"

#if defined(NEED_NSS_ILOCK)
#include "prlog.h"
#include "nssilock.h"

/*
** Declare the instrumented PZLock
*/
struct pzlock_s {
    PRLock *lock;        /* the PZLock to be instrumented */
    PRIntervalTime time; /* timestamp when the lock was aquired */
    nssILockType ltype;
};

/*
** Declare the instrumented PZMonitor
*/
struct pzmonitor_s {
    PRMonitor *mon;      /* the PZMonitor to be instrumented */
    PRIntervalTime time; /* timestamp when the monitor was aquired */
    nssILockType ltype;
};

/*
** Declare the instrumented PZCondVar
*/
struct pzcondvar_s {
    PRCondVar *cvar; /* the PZCondVar to be instrumented */
    nssILockType ltype;
};

/*
** Define a CallOnce type to ensure serialized self-initialization
*/
static PRCallOnceType coNssILock;  /* CallOnce type */
static PRIntn nssILockInitialized; /* initialization done when 1 */
static PRLogModuleInfo *nssILog;   /* Log instrumentation to this handle */

#define NUM_TT_ENTRIES 6000000
static PRInt32 traceIndex = -1; /* index into trace table */
static struct pzTrace_s *tt;    /* pointer to trace table */
static PRInt32 ttBufSize = (NUM_TT_ENTRIES * sizeof(struct pzTrace_s));
static PRCondVar *ttCVar;
static PRLock *ttLock;
static PRFileDesc *ttfd; /* trace table file */

/*
** Vtrace() -- Trace events, write events to external media
**
** Vtrace() records traced events in an in-memory trace table
** when the trace table fills, Vtrace writes the entire table
** to a file.
**
** data can be lost!
**
*/
static void
Vtrace(
    nssILockOp op,
    nssILockType ltype,
    PRIntervalTime callTime,
    PRIntervalTime heldTime,
    void *lock,
    PRIntn line,
    char *file)
{
    PRInt32 idx;
    struct pzTrace_s *tp;

RetryTrace:
    idx = PR_ATOMIC_INCREMENT(&traceIndex);
    while (NUM_TT_ENTRIES <= idx || op == FlushTT) {
        if (NUM_TT_ENTRIES == idx || op == FlushTT) {
            int writeSize = idx * sizeof(struct pzTrace_s);
            PR_Lock(ttLock);
            PR_Write(ttfd, tt, writeSize);
            traceIndex = -1;
            PR_NotifyAllCondVar(ttCVar);
            PR_Unlock(ttLock);
            goto RetryTrace;
        } else {
            PR_Lock(ttLock);
            while (NUM_TT_ENTRIES < idx)
                PR_WaitCondVar(ttCVar, PR_INTERVAL_NO_WAIT);
            PR_Unlock(ttLock);
            goto RetryTrace;
        }
    } /* end while() */

    /* create the trace entry */
    tp = tt + idx;
    tp->threadID = PR_GetThreadID(PR_GetCurrentThread());
    tp->op = op;
    tp->ltype = ltype;
    tp->callTime = callTime;
    tp->heldTime = heldTime;
    tp->lock = lock;
    tp->line = line;
    strcpy(tp->file, file);
    return;
} /* --- end Vtrace() --- */

/*
** pz_TraceFlush() -- Force trace table write to file
**
*/
extern void
pz_TraceFlush(void)
{
    Vtrace(FlushTT, nssILockSelfServ, 0, 0, NULL, 0, "");
    return;
} /* --- end pz_TraceFlush() --- */

/*
** nssILockInit() -- Initialization for nssilock
**
** This function is called from the CallOnce mechanism.
*/
static PRStatus
nssILockInit(void)
{
    int i;
    nssILockInitialized = 1;

    /* new log module */
    nssILog = PR_NewLogModule("nssilock");
    if (NULL == nssILog) {
        return (PR_FAILURE);
    }

    tt = PR_Calloc(NUM_TT_ENTRIES, sizeof(struct pzTrace_s));
    if (NULL == tt) {
        fprintf(stderr, "nssilock: can't allocate trace table\n");
        exit(1);
    }

    ttfd = PR_Open("xxxTTLog", PR_CREATE_FILE | PR_WRONLY, 0666);
    if (NULL == ttfd) {
        fprintf(stderr, "Oh Drat! Can't open 'xxxTTLog'\n");
        exit(1);
    }

    ttLock = PR_NewLock();
    ttCVar = PR_NewCondVar(ttLock);

    return (PR_SUCCESS);
} /* --- end nssILockInit() --- */

extern PZLock *
pz_NewLock(
    nssILockType ltype,
    char *file,
    PRIntn line)
{
    PRStatus rc;
    PZLock *lock;

    /* Self Initialize the nssILock feature */
    if (!nssILockInitialized) {
        rc = PR_CallOnce(&coNssILock, nssILockInit);
        if (PR_FAILURE == rc) {
            PR_SetError(PR_UNKNOWN_ERROR, 0);
            return (NULL);
        }
    }

    lock = PR_NEWZAP(PZLock);
    if (NULL != lock) {
        lock->ltype = ltype;
        lock->lock = PR_NewLock();
        if (NULL == lock->lock) {
            PR_DELETE(lock);
            PORT_SetError(SEC_ERROR_NO_MEMORY);
        }
    } else {
        PORT_SetError(SEC_ERROR_NO_MEMORY);
    }

    Vtrace(NewLock, ltype, 0, 0, lock, line, file);
    return (lock);
} /* --- end pz_NewLock() --- */

extern void
pz_Lock(
    PZLock *lock,
    char *file,
    PRIntn line)
{
    PRIntervalTime callTime;

    callTime = PR_IntervalNow();
    PR_Lock(lock->lock);
    lock->time = PR_IntervalNow();
    callTime = lock->time - callTime;

    Vtrace(Lock, lock->ltype, callTime, 0, lock, line, file);
    return;
} /* --- end  pz_Lock() --- */

extern PRStatus
pz_Unlock(
    PZLock *lock,
    char *file,
    PRIntn line)
{
    PRStatus rc;
    PRIntervalTime callTime, now, heldTime;

    callTime = PR_IntervalNow();
    rc = PR_Unlock(lock->lock);
    now = PR_IntervalNow();
    callTime = now - callTime;
    heldTime = now - lock->time;
    Vtrace(Unlock, lock->ltype, callTime, heldTime, lock, line, file);
    return (rc);
} /* --- end  pz_Unlock() --- */

extern void
pz_DestroyLock(
    PZLock *lock,
    char *file,
    PRIntn line)
{
    Vtrace(DestroyLock, lock->ltype, 0, 0, lock, line, file);
    PR_DestroyLock(lock->lock);
    PR_DELETE(lock);
    return;
} /* --- end  pz_DestroyLock() --- */

extern PZCondVar *
pz_NewCondVar(
    PZLock *lock,
    char *file,
    PRIntn line)
{
    PZCondVar *cvar;

    cvar = PR_NEWZAP(PZCondVar);
    if (NULL == cvar) {
        PORT_SetError(SEC_ERROR_NO_MEMORY);
    } else {
        cvar->ltype = lock->ltype;
        cvar->cvar = PR_NewCondVar(lock->lock);
        if (NULL == cvar->cvar) {
            PR_DELETE(cvar);
            PORT_SetError(SEC_ERROR_NO_MEMORY);
        }
    }
    Vtrace(NewCondVar, lock->ltype, 0, 0, cvar, line, file);
    return (cvar);
} /* --- end  pz_NewCondVar() --- */

extern void
pz_DestroyCondVar(
    PZCondVar *cvar,
    char *file,
    PRIntn line)
{
    Vtrace(DestroyCondVar, cvar->ltype, 0, 0, cvar, line, file);
    PR_DestroyCondVar(cvar->cvar);
    PR_DELETE(cvar);
} /* --- end  pz_DestroyCondVar() --- */

extern PRStatus
pz_WaitCondVar(
    PZCondVar *cvar,
    PRIntervalTime timeout,
    char *file,
    PRIntn line)
{
    PRStatus rc;
    PRIntervalTime callTime;

    callTime = PR_IntervalNow();
    rc = PR_WaitCondVar(cvar->cvar, timeout);
    callTime = PR_IntervalNow() - callTime;

    Vtrace(WaitCondVar, cvar->ltype, callTime, 0, cvar, line, file);
    return (rc);
} /* --- end  pz_WaitCondVar() --- */

extern PRStatus
pz_NotifyCondVar(
    PZCondVar *cvar,
    char *file,
    PRIntn line)
{
    PRStatus rc;

    rc = PR_NotifyCondVar(cvar->cvar);

    Vtrace(NotifyCondVar, cvar->ltype, 0, 0, cvar, line, file);
    return (rc);
} /* --- end  pz_NotifyCondVar() --- */

extern PRStatus
pz_NotifyAllCondVar(
    PZCondVar *cvar,
    char *file,
    PRIntn line)
{
    PRStatus rc;

    rc = PR_NotifyAllCondVar(cvar->cvar);

    Vtrace(NotifyAllCondVar, cvar->ltype, 0, 0, cvar, line, file);
    return (rc);
} /* --- end  pz_NotifyAllCondVar() --- */

extern PZMonitor *
pz_NewMonitor(
    nssILockType ltype,
    char *file,
    PRIntn line)
{
    PRStatus rc;
    PZMonitor *mon;

    /* Self Initialize the nssILock feature */
    if (!nssILockInitialized) {
        rc = PR_CallOnce(&coNssILock, nssILockInit);
        if (PR_FAILURE == rc) {
            PR_SetError(PR_UNKNOWN_ERROR, 0);
            return (NULL);
        }
    }

    mon = PR_NEWZAP(PZMonitor);
    if (NULL != mon) {
        mon->ltype = ltype;
        mon->mon = PR_NewMonitor();
        if (NULL == mon->mon) {
            PR_DELETE(mon);
            PORT_SetError(SEC_ERROR_NO_MEMORY);
        }
    } else {
        PORT_SetError(SEC_ERROR_NO_MEMORY);
    }

    Vtrace(NewMonitor, ltype, 0, 0, mon, line, file);
    return (mon);
} /* --- end  pz_NewMonitor() --- */

extern void
pz_DestroyMonitor(
    PZMonitor *mon,
    char *file,
    PRIntn line)
{
    Vtrace(DestroyMonitor, mon->ltype, 0, 0, mon, line, file);
    PR_DestroyMonitor(mon->mon);
    PR_DELETE(mon);
    return;
} /* --- end  pz_DestroyMonitor() --- */

extern void
pz_EnterMonitor(
    PZMonitor *mon,
    char *file,
    PRIntn line)
{
    PRIntervalTime callTime, now;

    callTime = PR_IntervalNow();
    PR_EnterMonitor(mon->mon);
    now = PR_IntervalNow();
    callTime = now - callTime;
    if (PR_GetMonitorEntryCount(mon->mon) == 1) {
        mon->time = now;
    }
    Vtrace(EnterMonitor, mon->ltype, callTime, 0, mon, line, file);
    return;
} /* --- end  pz_EnterMonitor() --- */

extern PRStatus
pz_ExitMonitor(
    PZMonitor *mon,
    char *file,
    PRIntn line)
{
    PRStatus rc;
    PRIntervalTime callTime, now, heldTime;
    PRIntn mec = PR_GetMonitorEntryCount(mon->mon);

    heldTime = (PRIntervalTime)-1;
    callTime = PR_IntervalNow();
    rc = PR_ExitMonitor(mon->mon);
    now = PR_IntervalNow();
    callTime = now - callTime;
    if (mec == 1)
        heldTime = now - mon->time;
    Vtrace(ExitMonitor, mon->ltype, callTime, heldTime, mon, line, file);
    return (rc);
} /* --- end  pz_ExitMonitor() --- */

extern PRIntn
pz_GetMonitorEntryCount(
    PZMonitor *mon,
    char *file,
    PRIntn line)
{
    return (PR_GetMonitorEntryCount(mon->mon));
} /* --- end pz_GetMonitorEntryCount() --- */

extern PRStatus
pz_Wait(
    PZMonitor *mon,
    PRIntervalTime ticks,
    char *file,
    PRIntn line)
{
    PRStatus rc;
    PRIntervalTime callTime;

    callTime = PR_IntervalNow();
    rc = PR_Wait(mon->mon, ticks);
    callTime = PR_IntervalNow() - callTime;
    Vtrace(Wait, mon->ltype, callTime, 0, mon, line, file);
    return (rc);
} /* --- end  pz_Wait() --- */

extern PRStatus
pz_Notify(
    PZMonitor *mon,
    char *file,
    PRIntn line)
{
    PRStatus rc;
    PRIntervalTime callTime;

    callTime = PR_IntervalNow();
    rc = PR_Notify(mon->mon);
    callTime = PR_IntervalNow() - callTime;
    Vtrace(Notify, mon->ltype, callTime, 0, mon, line, file);
    return (rc);
} /* --- end  pz_Notify() --- */

extern PRStatus
pz_NotifyAll(
    PZMonitor *mon,
    char *file,
    PRIntn line)
{
    PRStatus rc;
    PRIntervalTime callTime;

    callTime = PR_IntervalNow();
    rc = PR_NotifyAll(mon->mon);
    callTime = PR_IntervalNow() - callTime;
    Vtrace(NotifyAll, mon->ltype, callTime, 0, mon, line, file);
    return (rc);
} /* --- end  pz_NotifyAll() --- */

#endif /* NEED_NSS_ILOCK */
/* --- end nssilock.c --------------------------------- */