nssilock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * nssilock.c - NSS lock instrumentation wrapper functions
  6. *
  7. * NOTE - These are not public interfaces
  8. *
  9. * Implementation Notes:
  10. * I've tried to make the instrumentation relatively non-intrusive.
  11. * To do this, I have used a single PR_LOG() call in each
  12. * instrumented function. There's room for improvement.
  13. *
  14. *
  15. */
  16. #include "prinit.h"
  17. #include "prerror.h"
  18. #include "prlock.h"
  19. #include "prmem.h"
  20. #include "prenv.h"
  21. #include "prcvar.h"
  22. #include "prio.h"
  23. #if defined(NEED_NSS_ILOCK)
  24. #include "prlog.h"
  25. #include "nssilock.h"
  26. /*
  27. ** Declare the instrumented PZLock
  28. */
  29. struct pzlock_s {
  30. PRLock *lock; /* the PZLock to be instrumented */
  31. PRIntervalTime time; /* timestamp when the lock was aquired */
  32. nssILockType ltype;
  33. };
  34. /*
  35. ** Declare the instrumented PZMonitor
  36. */
  37. struct pzmonitor_s {
  38. PRMonitor *mon; /* the PZMonitor to be instrumented */
  39. PRIntervalTime time; /* timestamp when the monitor was aquired */
  40. nssILockType ltype;
  41. };
  42. /*
  43. ** Declare the instrumented PZCondVar
  44. */
  45. struct pzcondvar_s {
  46. PRCondVar *cvar; /* the PZCondVar to be instrumented */
  47. nssILockType ltype;
  48. };
  49. /*
  50. ** Define a CallOnce type to ensure serialized self-initialization
  51. */
  52. static PRCallOnceType coNssILock; /* CallOnce type */
  53. static PRIntn nssILockInitialized; /* initialization done when 1 */
  54. static PRLogModuleInfo *nssILog; /* Log instrumentation to this handle */
  55. #define NUM_TT_ENTRIES 6000000
  56. static PRInt32 traceIndex = -1; /* index into trace table */
  57. static struct pzTrace_s *tt; /* pointer to trace table */
  58. static PRInt32 ttBufSize = (NUM_TT_ENTRIES * sizeof(struct pzTrace_s));
  59. static PRCondVar *ttCVar;
  60. static PRLock *ttLock;
  61. static PRFileDesc *ttfd; /* trace table file */
  62. /*
  63. ** Vtrace() -- Trace events, write events to external media
  64. **
  65. ** Vtrace() records traced events in an in-memory trace table
  66. ** when the trace table fills, Vtrace writes the entire table
  67. ** to a file.
  68. **
  69. ** data can be lost!
  70. **
  71. */
  72. static void
  73. Vtrace(
  74. nssILockOp op,
  75. nssILockType ltype,
  76. PRIntervalTime callTime,
  77. PRIntervalTime heldTime,
  78. void *lock,
  79. PRIntn line,
  80. char *file)
  81. {
  82. PRInt32 idx;
  83. struct pzTrace_s *tp;
  84. RetryTrace:
  85. idx = PR_ATOMIC_INCREMENT(&traceIndex);
  86. while (NUM_TT_ENTRIES <= idx || op == FlushTT) {
  87. if (NUM_TT_ENTRIES == idx || op == FlushTT) {
  88. int writeSize = idx * sizeof(struct pzTrace_s);
  89. PR_Lock(ttLock);
  90. PR_Write(ttfd, tt, writeSize);
  91. traceIndex = -1;
  92. PR_NotifyAllCondVar(ttCVar);
  93. PR_Unlock(ttLock);
  94. goto RetryTrace;
  95. } else {
  96. PR_Lock(ttLock);
  97. while (NUM_TT_ENTRIES < idx)
  98. PR_WaitCondVar(ttCVar, PR_INTERVAL_NO_WAIT);
  99. PR_Unlock(ttLock);
  100. goto RetryTrace;
  101. }
  102. } /* end while() */
  103. /* create the trace entry */
  104. tp = tt + idx;
  105. tp->threadID = PR_GetThreadID(PR_GetCurrentThread());
  106. tp->op = op;
  107. tp->ltype = ltype;
  108. tp->callTime = callTime;
  109. tp->heldTime = heldTime;
  110. tp->lock = lock;
  111. tp->line = line;
  112. strcpy(tp->file, file);
  113. return;
  114. } /* --- end Vtrace() --- */
  115. /*
  116. ** pz_TraceFlush() -- Force trace table write to file
  117. **
  118. */
  119. extern void
  120. pz_TraceFlush(void)
  121. {
  122. Vtrace(FlushTT, nssILockSelfServ, 0, 0, NULL, 0, "");
  123. return;
  124. } /* --- end pz_TraceFlush() --- */
  125. /*
  126. ** nssILockInit() -- Initialization for nssilock
  127. **
  128. ** This function is called from the CallOnce mechanism.
  129. */
  130. static PRStatus
  131. nssILockInit(void)
  132. {
  133. int i;
  134. nssILockInitialized = 1;
  135. /* new log module */
  136. nssILog = PR_NewLogModule("nssilock");
  137. if (NULL == nssILog) {
  138. return (PR_FAILURE);
  139. }
  140. tt = PR_Calloc(NUM_TT_ENTRIES, sizeof(struct pzTrace_s));
  141. if (NULL == tt) {
  142. fprintf(stderr, "nssilock: can't allocate trace table\n");
  143. exit(1);
  144. }
  145. ttfd = PR_Open("xxxTTLog", PR_CREATE_FILE | PR_WRONLY, 0666);
  146. if (NULL == ttfd) {
  147. fprintf(stderr, "Oh Drat! Can't open 'xxxTTLog'\n");
  148. exit(1);
  149. }
  150. ttLock = PR_NewLock();
  151. ttCVar = PR_NewCondVar(ttLock);
  152. return (PR_SUCCESS);
  153. } /* --- end nssILockInit() --- */
  154. extern PZLock *
  155. pz_NewLock(
  156. nssILockType ltype,
  157. char *file,
  158. PRIntn line)
  159. {
  160. PRStatus rc;
  161. PZLock *lock;
  162. /* Self Initialize the nssILock feature */
  163. if (!nssILockInitialized) {
  164. rc = PR_CallOnce(&coNssILock, nssILockInit);
  165. if (PR_FAILURE == rc) {
  166. PR_SetError(PR_UNKNOWN_ERROR, 0);
  167. return (NULL);
  168. }
  169. }
  170. lock = PR_NEWZAP(PZLock);
  171. if (NULL != lock) {
  172. lock->ltype = ltype;
  173. lock->lock = PR_NewLock();
  174. if (NULL == lock->lock) {
  175. PR_DELETE(lock);
  176. PORT_SetError(SEC_ERROR_NO_MEMORY);
  177. }
  178. } else {
  179. PORT_SetError(SEC_ERROR_NO_MEMORY);
  180. }
  181. Vtrace(NewLock, ltype, 0, 0, lock, line, file);
  182. return (lock);
  183. } /* --- end pz_NewLock() --- */
  184. extern void
  185. pz_Lock(
  186. PZLock *lock,
  187. char *file,
  188. PRIntn line)
  189. {
  190. PRIntervalTime callTime;
  191. callTime = PR_IntervalNow();
  192. PR_Lock(lock->lock);
  193. lock->time = PR_IntervalNow();
  194. callTime = lock->time - callTime;
  195. Vtrace(Lock, lock->ltype, callTime, 0, lock, line, file);
  196. return;
  197. } /* --- end pz_Lock() --- */
  198. extern PRStatus
  199. pz_Unlock(
  200. PZLock *lock,
  201. char *file,
  202. PRIntn line)
  203. {
  204. PRStatus rc;
  205. PRIntervalTime callTime, now, heldTime;
  206. callTime = PR_IntervalNow();
  207. rc = PR_Unlock(lock->lock);
  208. now = PR_IntervalNow();
  209. callTime = now - callTime;
  210. heldTime = now - lock->time;
  211. Vtrace(Unlock, lock->ltype, callTime, heldTime, lock, line, file);
  212. return (rc);
  213. } /* --- end pz_Unlock() --- */
  214. extern void
  215. pz_DestroyLock(
  216. PZLock *lock,
  217. char *file,
  218. PRIntn line)
  219. {
  220. Vtrace(DestroyLock, lock->ltype, 0, 0, lock, line, file);
  221. PR_DestroyLock(lock->lock);
  222. PR_DELETE(lock);
  223. return;
  224. } /* --- end pz_DestroyLock() --- */
  225. extern PZCondVar *
  226. pz_NewCondVar(
  227. PZLock *lock,
  228. char *file,
  229. PRIntn line)
  230. {
  231. PZCondVar *cvar;
  232. cvar = PR_NEWZAP(PZCondVar);
  233. if (NULL == cvar) {
  234. PORT_SetError(SEC_ERROR_NO_MEMORY);
  235. } else {
  236. cvar->ltype = lock->ltype;
  237. cvar->cvar = PR_NewCondVar(lock->lock);
  238. if (NULL == cvar->cvar) {
  239. PR_DELETE(cvar);
  240. PORT_SetError(SEC_ERROR_NO_MEMORY);
  241. }
  242. }
  243. Vtrace(NewCondVar, lock->ltype, 0, 0, cvar, line, file);
  244. return (cvar);
  245. } /* --- end pz_NewCondVar() --- */
  246. extern void
  247. pz_DestroyCondVar(
  248. PZCondVar *cvar,
  249. char *file,
  250. PRIntn line)
  251. {
  252. Vtrace(DestroyCondVar, cvar->ltype, 0, 0, cvar, line, file);
  253. PR_DestroyCondVar(cvar->cvar);
  254. PR_DELETE(cvar);
  255. } /* --- end pz_DestroyCondVar() --- */
  256. extern PRStatus
  257. pz_WaitCondVar(
  258. PZCondVar *cvar,
  259. PRIntervalTime timeout,
  260. char *file,
  261. PRIntn line)
  262. {
  263. PRStatus rc;
  264. PRIntervalTime callTime;
  265. callTime = PR_IntervalNow();
  266. rc = PR_WaitCondVar(cvar->cvar, timeout);
  267. callTime = PR_IntervalNow() - callTime;
  268. Vtrace(WaitCondVar, cvar->ltype, callTime, 0, cvar, line, file);
  269. return (rc);
  270. } /* --- end pz_WaitCondVar() --- */
  271. extern PRStatus
  272. pz_NotifyCondVar(
  273. PZCondVar *cvar,
  274. char *file,
  275. PRIntn line)
  276. {
  277. PRStatus rc;
  278. rc = PR_NotifyCondVar(cvar->cvar);
  279. Vtrace(NotifyCondVar, cvar->ltype, 0, 0, cvar, line, file);
  280. return (rc);
  281. } /* --- end pz_NotifyCondVar() --- */
  282. extern PRStatus
  283. pz_NotifyAllCondVar(
  284. PZCondVar *cvar,
  285. char *file,
  286. PRIntn line)
  287. {
  288. PRStatus rc;
  289. rc = PR_NotifyAllCondVar(cvar->cvar);
  290. Vtrace(NotifyAllCondVar, cvar->ltype, 0, 0, cvar, line, file);
  291. return (rc);
  292. } /* --- end pz_NotifyAllCondVar() --- */
  293. extern PZMonitor *
  294. pz_NewMonitor(
  295. nssILockType ltype,
  296. char *file,
  297. PRIntn line)
  298. {
  299. PRStatus rc;
  300. PZMonitor *mon;
  301. /* Self Initialize the nssILock feature */
  302. if (!nssILockInitialized) {
  303. rc = PR_CallOnce(&coNssILock, nssILockInit);
  304. if (PR_FAILURE == rc) {
  305. PR_SetError(PR_UNKNOWN_ERROR, 0);
  306. return (NULL);
  307. }
  308. }
  309. mon = PR_NEWZAP(PZMonitor);
  310. if (NULL != mon) {
  311. mon->ltype = ltype;
  312. mon->mon = PR_NewMonitor();
  313. if (NULL == mon->mon) {
  314. PR_DELETE(mon);
  315. PORT_SetError(SEC_ERROR_NO_MEMORY);
  316. }
  317. } else {
  318. PORT_SetError(SEC_ERROR_NO_MEMORY);
  319. }
  320. Vtrace(NewMonitor, ltype, 0, 0, mon, line, file);
  321. return (mon);
  322. } /* --- end pz_NewMonitor() --- */
  323. extern void
  324. pz_DestroyMonitor(
  325. PZMonitor *mon,
  326. char *file,
  327. PRIntn line)
  328. {
  329. Vtrace(DestroyMonitor, mon->ltype, 0, 0, mon, line, file);
  330. PR_DestroyMonitor(mon->mon);
  331. PR_DELETE(mon);
  332. return;
  333. } /* --- end pz_DestroyMonitor() --- */
  334. extern void
  335. pz_EnterMonitor(
  336. PZMonitor *mon,
  337. char *file,
  338. PRIntn line)
  339. {
  340. PRIntervalTime callTime, now;
  341. callTime = PR_IntervalNow();
  342. PR_EnterMonitor(mon->mon);
  343. now = PR_IntervalNow();
  344. callTime = now - callTime;
  345. if (PR_GetMonitorEntryCount(mon->mon) == 1) {
  346. mon->time = now;
  347. }
  348. Vtrace(EnterMonitor, mon->ltype, callTime, 0, mon, line, file);
  349. return;
  350. } /* --- end pz_EnterMonitor() --- */
  351. extern PRStatus
  352. pz_ExitMonitor(
  353. PZMonitor *mon,
  354. char *file,
  355. PRIntn line)
  356. {
  357. PRStatus rc;
  358. PRIntervalTime callTime, now, heldTime;
  359. PRIntn mec = PR_GetMonitorEntryCount(mon->mon);
  360. heldTime = (PRIntervalTime)-1;
  361. callTime = PR_IntervalNow();
  362. rc = PR_ExitMonitor(mon->mon);
  363. now = PR_IntervalNow();
  364. callTime = now - callTime;
  365. if (mec == 1)
  366. heldTime = now - mon->time;
  367. Vtrace(ExitMonitor, mon->ltype, callTime, heldTime, mon, line, file);
  368. return (rc);
  369. } /* --- end pz_ExitMonitor() --- */
  370. extern PRIntn
  371. pz_GetMonitorEntryCount(
  372. PZMonitor *mon,
  373. char *file,
  374. PRIntn line)
  375. {
  376. return (PR_GetMonitorEntryCount(mon->mon));
  377. } /* --- end pz_GetMonitorEntryCount() --- */
  378. extern PRStatus
  379. pz_Wait(
  380. PZMonitor *mon,
  381. PRIntervalTime ticks,
  382. char *file,
  383. PRIntn line)
  384. {
  385. PRStatus rc;
  386. PRIntervalTime callTime;
  387. callTime = PR_IntervalNow();
  388. rc = PR_Wait(mon->mon, ticks);
  389. callTime = PR_IntervalNow() - callTime;
  390. Vtrace(Wait, mon->ltype, callTime, 0, mon, line, file);
  391. return (rc);
  392. } /* --- end pz_Wait() --- */
  393. extern PRStatus
  394. pz_Notify(
  395. PZMonitor *mon,
  396. char *file,
  397. PRIntn line)
  398. {
  399. PRStatus rc;
  400. PRIntervalTime callTime;
  401. callTime = PR_IntervalNow();
  402. rc = PR_Notify(mon->mon);
  403. callTime = PR_IntervalNow() - callTime;
  404. Vtrace(Notify, mon->ltype, callTime, 0, mon, line, file);
  405. return (rc);
  406. } /* --- end pz_Notify() --- */
  407. extern PRStatus
  408. pz_NotifyAll(
  409. PZMonitor *mon,
  410. char *file,
  411. PRIntn line)
  412. {
  413. PRStatus rc;
  414. PRIntervalTime callTime;
  415. callTime = PR_IntervalNow();
  416. rc = PR_NotifyAll(mon->mon);
  417. callTime = PR_IntervalNow() - callTime;
  418. Vtrace(NotifyAll, mon->ltype, callTime, 0, mon, line, file);
  419. return (rc);
  420. } /* --- end pz_NotifyAll() --- */
  421. #endif /* NEED_NSS_ILOCK */
  422. /* --- end nssilock.c --------------------------------- */