prcountr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef prcountr_h___
  6. #define prcountr_h___
  7. /*----------------------------------------------------------------------------
  8. ** prcountr.h -- NSPR Instrumentation counters
  9. **
  10. ** The NSPR Counter Feature provides a means to "count
  11. ** something." Counters can be dynamically defined, incremented,
  12. ** decremented, set, and deleted under application program
  13. ** control.
  14. **
  15. ** The Counter Feature is intended to be used as instrumentation,
  16. ** not as operational data. If you need a counter for operational
  17. ** data, use native integral types.
  18. **
  19. ** Counters are 32bit unsigned intergers. On overflow, a counter
  20. ** will wrap. No exception is recognized or reported.
  21. **
  22. ** A counter can be dynamically created using a two level naming
  23. ** convention. A "handle" is returned when the counter is
  24. ** created. The counter can subsequently be addressed by its
  25. ** handle. An API is provided to get an existing counter's handle
  26. ** given the names with which it was originally created.
  27. ** Similarly, a counter's name can be retrieved given its handle.
  28. **
  29. ** The counter naming convention is a two-level hierarchy. The
  30. ** QName is the higher level of the hierarchy; RName is the
  31. ** lower level. RNames can be thought of as existing within a
  32. ** QName. The same RName can exist within multiple QNames. QNames
  33. ** are unique. The NSPR Counter is not a near-zero overhead
  34. ** feature. Application designers should be aware of
  35. ** serialization issues when using the Counter API. Creating a
  36. ** counter locks a large asset, potentially causing a stall. This
  37. ** suggest that applications should create counters at component
  38. ** initialization, for example, and not create and destroy them
  39. ** willy-nilly. ... You have been warned.
  40. **
  41. ** Incrementing and Adding to counters uses atomic operations.
  42. ** The performance of these operations will vary from platform
  43. ** to platform. On platforms where atomic operations are not
  44. ** supported the overhead may be substantial.
  45. **
  46. ** When traversing the counter database with FindNext functions,
  47. ** the instantaneous values of any given counter is that at the
  48. ** moment of extraction. The state of the entire counter database
  49. ** may not be viewed as atomic.
  50. **
  51. ** The counter interface may be disabled (No-Op'd) at compile
  52. ** time. When DEBUG is defined at compile time, the Counter
  53. ** Feature is compiled into NSPR and applications invoking it.
  54. ** When DEBUG is not defined, the counter macros compile to
  55. ** nothing. To force the Counter Feature to be compiled into an
  56. ** optimized build, define FORCE_NSPR_COUNTERS at compile time
  57. ** for both NSPR and the application intending to use it.
  58. **
  59. ** Application designers should use the macro form of the Counter
  60. ** Feature methods to minimize performance impact in optimized
  61. ** builds. The macros normally compile to nothing on optimized
  62. ** builds.
  63. **
  64. ** Application designers should be aware of the effects of
  65. ** debug and optimized build differences when using result of the
  66. ** Counter Feature macros in expressions.
  67. **
  68. ** The Counter Feature is thread-safe and SMP safe.
  69. **
  70. ** /lth. 09-Jun-1998.
  71. */
  72. #include "prtypes.h"
  73. PR_BEGIN_EXTERN_C
  74. /*
  75. ** Opaque counter handle type.
  76. ** ... don't even think of looking in here.
  77. **
  78. */
  79. typedef void * PRCounterHandle;
  80. #define PRCOUNTER_NAME_MAX 31
  81. #define PRCOUNTER_DESC_MAX 255
  82. /* -----------------------------------------------------------------------
  83. ** FUNCTION: PR_DEFINE_COUNTER() -- Define a PRCounterHandle
  84. **
  85. ** DESCRIPTION: PR_DEFINE_COUNTER() is used to define a counter
  86. ** handle.
  87. **
  88. */
  89. #define PR_DEFINE_COUNTER(name) PRCounterHandle name
  90. /* -----------------------------------------------------------------------
  91. ** FUNCTION: PR_INIT_COUNTER_HANDLE() -- Set the value of a PRCounterHandle
  92. **
  93. ** DESCRIPTION:
  94. ** PR_INIT_COUNTER_HANDLE() sets the value of a PRCounterHandle
  95. ** to value.
  96. **
  97. */
  98. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  99. #define PR_INIT_COUNTER_HANDLE(handle,value)\
  100. (handle) = (PRCounterHandle)(value)
  101. #else
  102. #define PR_INIT_COUNTER_HANDLE(handle,value)
  103. #endif
  104. /* -----------------------------------------------------------------------
  105. ** FUNCTION: PR_CreateCounter() -- Create a counter
  106. **
  107. ** DESCRIPTION: PR_CreateCounter() creates a counter object and
  108. ** initializes it to zero.
  109. **
  110. ** The macro form takes as its first argument the name of the
  111. ** PRCounterHandle to receive the handle returned from
  112. ** PR_CreateCounter().
  113. **
  114. ** INPUTS:
  115. ** qName: The QName for the counter object. The maximum length
  116. ** of qName is defined by PRCOUNTER_NAME_MAX
  117. **
  118. ** rName: The RName for the counter object. The maximum length
  119. ** of qName is defined by PRCOUNTER_NAME_MAX
  120. **
  121. ** descrioption: The description of the counter object. The
  122. ** maximum length of description is defined by
  123. ** PRCOUNTER_DESC_MAX.
  124. **
  125. ** OUTPUTS:
  126. **
  127. ** RETURNS:
  128. ** PRCounterHandle.
  129. **
  130. ** RESTRICTIONS:
  131. **
  132. */
  133. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  134. #define PR_CREATE_COUNTER(handle,qName,rName,description)\
  135. (handle) = PR_CreateCounter((qName),(rName),(description))
  136. #else
  137. #define PR_CREATE_COUNTER(handle,qName,rName,description)
  138. #endif
  139. NSPR_API(PRCounterHandle)
  140. PR_CreateCounter(
  141. const char *qName,
  142. const char *rName,
  143. const char *description
  144. );
  145. /* -----------------------------------------------------------------------
  146. ** FUNCTION: PR_DestroyCounter() -- Destroy a counter object.
  147. **
  148. ** DESCRIPTION: PR_DestroyCounter() removes a counter and
  149. ** unregisters its handle from the counter database.
  150. **
  151. ** INPUTS:
  152. ** handle: the PRCounterHandle of the counter to be destroyed.
  153. **
  154. ** OUTPUTS:
  155. ** The counter is destroyed.
  156. **
  157. ** RETURNS: void
  158. **
  159. ** RESTRICTIONS:
  160. **
  161. */
  162. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  163. #define PR_DESTROY_COUNTER(handle) PR_DestroyCounter((handle))
  164. #else
  165. #define PR_DESTROY_COUNTER(handle)
  166. #endif
  167. NSPR_API(void)
  168. PR_DestroyCounter(
  169. PRCounterHandle handle
  170. );
  171. /* -----------------------------------------------------------------------
  172. ** FUNCTION: PR_GetCounterHandleFromName() -- Retreive a
  173. ** counter's handle give its name.
  174. **
  175. ** DESCRIPTION: PR_GetCounterHandleFromName() retreives a
  176. ** counter's handle from the counter database, given the name
  177. ** the counter was originally created with.
  178. **
  179. ** INPUTS:
  180. ** qName: Counter's original QName.
  181. ** rName: Counter's original RName.
  182. **
  183. ** OUTPUTS:
  184. **
  185. ** RETURNS:
  186. ** PRCounterHandle or PRCounterError.
  187. **
  188. ** RESTRICTIONS:
  189. **
  190. */
  191. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  192. #define PR_GET_COUNTER_HANDLE_FROM_NAME(handle,qName,rName)\
  193. (handle) = PR_GetCounterHandleFromName((qName),(rName))
  194. #else
  195. #define PR_GET_COUNTER_HANDLE_FROM_NAME(handle,qName,rName)
  196. #endif
  197. NSPR_API(PRCounterHandle)
  198. PR_GetCounterHandleFromName(
  199. const char *qName,
  200. const char *rName
  201. );
  202. /* -----------------------------------------------------------------------
  203. ** FUNCTION: PR_GetCounterNameFromHandle() -- Retreive a
  204. ** counter's name, given its handle.
  205. **
  206. ** DESCRIPTION: PR_GetCounterNameFromHandle() retreives a
  207. ** counter's name given its handle.
  208. **
  209. ** INPUTS:
  210. ** qName: Where to store a pointer to qName.
  211. ** rName: Where to store a pointer to rName.
  212. ** description: Where to store a pointer to description.
  213. **
  214. ** OUTPUTS: Pointers to the Counter Feature's copies of the names
  215. ** used when the counters were created.
  216. **
  217. ** RETURNS: void
  218. **
  219. ** RESTRICTIONS:
  220. **
  221. */
  222. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  223. #define PR_GET_COUNTER_NAME_FROM_HANDLE(handle,qName,rName,description)\
  224. PR_GetCounterNameFromHandle((handle),(qName),(rName),(description))
  225. #else
  226. #define PR_GET_COUNTER_NAME_FROM_HANDLE(handle,qName,rName,description )
  227. #endif
  228. NSPR_API(void)
  229. PR_GetCounterNameFromHandle(
  230. PRCounterHandle handle,
  231. const char **qName,
  232. const char **rName,
  233. const char **description
  234. );
  235. /* -----------------------------------------------------------------------
  236. ** FUNCTION: PR_IncrementCounter() -- Add one to the referenced
  237. ** counter.
  238. **
  239. ** DESCRIPTION: Add one to the referenced counter.
  240. **
  241. ** INPUTS:
  242. ** handle: The PRCounterHandle of the counter to be incremented
  243. **
  244. ** OUTPUTS: The counter is incrementd.
  245. **
  246. ** RETURNS: void
  247. **
  248. ** RESTRICTIONS:
  249. **
  250. */
  251. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  252. #define PR_INCREMENT_COUNTER(handle) PR_IncrementCounter(handle)
  253. #else
  254. #define PR_INCREMENT_COUNTER(handle)
  255. #endif
  256. NSPR_API(void)
  257. PR_IncrementCounter(
  258. PRCounterHandle handle
  259. );
  260. /* -----------------------------------------------------------------------
  261. ** FUNCTION: PR_DecrementCounter() -- Subtract one from the
  262. ** referenced counter
  263. **
  264. ** DESCRIPTION: Subtract one from the referenced counter.
  265. **
  266. ** INPUTS:
  267. ** handle: The PRCounterHandle of the coutner to be
  268. ** decremented.
  269. **
  270. ** OUTPUTS: the counter is decremented.
  271. **
  272. ** RETURNS: void
  273. **
  274. ** RESTRICTIONS:
  275. **
  276. */
  277. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  278. #define PR_DECREMENT_COUNTER(handle) PR_DecrementCounter(handle)
  279. #else
  280. #define PR_DECREMENT_COUNTER(handle)
  281. #endif
  282. NSPR_API(void)
  283. PR_DecrementCounter(
  284. PRCounterHandle handle
  285. );
  286. /* -----------------------------------------------------------------------
  287. ** FUNCTION: PR_AddToCounter() -- Add a value to a counter.
  288. **
  289. ** DESCRIPTION: Add value to the counter referenced by handle.
  290. **
  291. ** INPUTS:
  292. ** handle: the PRCounterHandle of the counter to be added to.
  293. **
  294. ** value: the value to be added to the counter.
  295. **
  296. ** OUTPUTS: new value for counter.
  297. **
  298. ** RETURNS: void
  299. **
  300. ** RESTRICTIONS:
  301. **
  302. */
  303. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  304. #define PR_ADD_TO_COUNTER(handle,value)\
  305. PR_AddToCounter((handle),(value))
  306. #else
  307. #define PR_ADD_TO_COUNTER(handle,value)
  308. #endif
  309. NSPR_API(void)
  310. PR_AddToCounter(
  311. PRCounterHandle handle,
  312. PRUint32 value
  313. );
  314. /* -----------------------------------------------------------------------
  315. ** FUNCTION: PR_SubtractFromCounter() -- A value is subtracted
  316. ** from a counter.
  317. **
  318. ** DESCRIPTION:
  319. ** Subtract a value from a counter.
  320. **
  321. ** INPUTS:
  322. ** handle: the PRCounterHandle of the counter to be subtracted
  323. ** from.
  324. **
  325. ** value: the value to be subtracted from the counter.
  326. **
  327. ** OUTPUTS: new value for counter
  328. **
  329. ** RETURNS: void
  330. **
  331. ** RESTRICTIONS:
  332. **
  333. */
  334. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  335. #define PR_SUBTRACT_FROM_COUNTER(handle,value)\
  336. PR_SubtractFromCounter((handle),(value))
  337. #else
  338. #define PR_SUBTRACT_FROM_COUNTER(handle,value)
  339. #endif
  340. NSPR_API(void)
  341. PR_SubtractFromCounter(
  342. PRCounterHandle handle,
  343. PRUint32 value
  344. );
  345. /* -----------------------------------------------------------------------
  346. ** FUNCTION: PR_GetCounter() -- Retreive the value of a counter
  347. **
  348. ** DESCRIPTION:
  349. ** Retreive the value of a counter.
  350. **
  351. ** INPUTS:
  352. ** handle: the PR_CounterHandle of the counter to be retreived
  353. **
  354. ** OUTPUTS:
  355. **
  356. ** RETURNS: The value of the referenced counter
  357. **
  358. ** RESTRICTIONS:
  359. **
  360. */
  361. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  362. #define PR_GET_COUNTER(counter,handle)\
  363. (counter) = PR_GetCounter((handle))
  364. #else
  365. #define PR_GET_COUNTER(counter,handle) 0
  366. #endif
  367. NSPR_API(PRUint32)
  368. PR_GetCounter(
  369. PRCounterHandle handle
  370. );
  371. /* -----------------------------------------------------------------------
  372. ** FUNCTION: PR_SetCounter() -- Replace the content of counter
  373. ** with value.
  374. **
  375. ** DESCRIPTION: The contents of the referenced counter are
  376. ** replaced by value.
  377. **
  378. ** INPUTS:
  379. ** handle: the PRCounterHandle of the counter whose contents
  380. ** are to be replaced.
  381. **
  382. ** value: the new value of the counter.
  383. **
  384. ** OUTPUTS:
  385. **
  386. ** RETURNS: void
  387. **
  388. ** RESTRICTIONS:
  389. **
  390. */
  391. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  392. #define PR_SET_COUNTER(handle,value) PR_SetCounter((handle),(value))
  393. #else
  394. #define PR_SET_COUNTER(handle,value)
  395. #endif
  396. NSPR_API(void)
  397. PR_SetCounter(
  398. PRCounterHandle handle,
  399. PRUint32 value
  400. );
  401. /* -----------------------------------------------------------------------
  402. ** FUNCTION: PR_FindNextCounterQname() -- Retreive the next QName counter
  403. ** handle iterator
  404. **
  405. ** DESCRIPTION:
  406. ** PR_FindNextCounterQname() retreives the first or next Qname
  407. ** the counter data base, depending on the value of handle. When
  408. ** handle is NULL, the function attempts to retreive the first
  409. ** QName handle in the database. When handle is a handle previosly
  410. ** retreived QName handle, then the function attempts to retreive
  411. ** the next QName handle.
  412. **
  413. ** INPUTS:
  414. ** handle: PRCounterHandle or NULL.
  415. **
  416. ** OUTPUTS: returned
  417. **
  418. ** RETURNS: PRCounterHandle or NULL when no more QName counter
  419. ** handles are present.
  420. **
  421. ** RESTRICTIONS:
  422. ** A concurrent PR_CreateCounter() or PR_DestroyCounter() may
  423. ** cause unpredictable results.
  424. **
  425. ** A PRCounterHandle returned from this function may only be used
  426. ** in another PR_FindNextCounterQname() function call; other
  427. ** operations may cause unpredictable results.
  428. **
  429. */
  430. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  431. #define PR_FIND_NEXT_COUNTER_QNAME(next,handle)\
  432. (next) = PR_FindNextCounterQname((handle))
  433. #else
  434. #define PR_FIND_NEXT_COUNTER_QNAME(next,handle) NULL
  435. #endif
  436. NSPR_API(PRCounterHandle)
  437. PR_FindNextCounterQname(
  438. PRCounterHandle handle
  439. );
  440. /* -----------------------------------------------------------------------
  441. ** FUNCTION: PR_FindNextCounterRname() -- Retreive the next RName counter
  442. ** handle iterator
  443. **
  444. ** DESCRIPTION:
  445. ** PR_FindNextCounterRname() retreives the first or next RNname
  446. ** handle from the counter data base, depending on the
  447. ** value of handle. When handle is NULL, the function attempts to
  448. ** retreive the first RName handle in the database. When handle is
  449. ** a handle previosly retreived RName handle, then the function
  450. ** attempts to retreive the next RName handle.
  451. **
  452. ** INPUTS:
  453. ** handle: PRCounterHandle or NULL.
  454. ** qhandle: PRCounterHandle of a previously aquired via
  455. ** PR_FIND_NEXT_QNAME_HANDLE()
  456. **
  457. ** OUTPUTS: returned
  458. **
  459. ** RETURNS: PRCounterHandle or NULL when no more RName counter
  460. ** handles are present.
  461. **
  462. ** RESTRICTIONS:
  463. ** A concurrent PR_CreateCounter() or PR_DestroyCounter() may
  464. ** cause unpredictable results.
  465. **
  466. ** A PRCounterHandle returned from this function may only be used
  467. ** in another PR_FindNextCounterRname() function call; other
  468. ** operations may cause unpredictable results.
  469. **
  470. */
  471. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  472. #define PR_FIND_NEXT_COUNTER_RNAME(next,rhandle,qhandle)\
  473. (next) = PR_FindNextCounterRname((rhandle),(qhandle))
  474. #else
  475. #define PR_FIND_NEXT_COUNTER_RNAME(next,rhandle,qhandle)
  476. #endif
  477. NSPR_API(PRCounterHandle)
  478. PR_FindNextCounterRname(
  479. PRCounterHandle rhandle,
  480. PRCounterHandle qhandle
  481. );
  482. PR_END_EXTERN_C
  483. #endif /* prcountr_h___ */