summaryrefslogtreecommitdiff
path: root/pr/src/misc/prcountr.c
blob: d133139265c5d13552a2f0ff90753b39e405434b (plain)
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * The contents of this file are subject to the Netscape Public License
 * Version 1.0 (the "NPL"); you may not use this file except in
 * compliance with the NPL.  You may obtain a copy of the NPL at
 * http://www.mozilla.org/NPL/
 * 
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 * for the specific language governing rights and limitations under the
 * NPL.
 * 
 * The Initial Developer of this code under the NPL is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 * Reserved.
 */

/*
** prcountr.c -- NSPR Instrumentation Counters
**
** Implement the interface defined in prcountr.h
**
** Design Notes:
**
** The Counter Facility (CF) has a single anchor: qNameList.      
** The anchor is a PRCList. qNameList is a list of links in QName
** structures. From qNameList any QName structure and its
** associated RName structure can be located. 
** 
** For each QName, a list of RName structures is anchored at
** rnLink in the QName structure.
** 
** The counter itself is embedded in the RName structure.
** 
** For manipulating the counter database, single lock is used to
** protect the entire list: counterLock.
**
** A PRCounterHandle, defined in prcountr.h, is really a pointer
** to a RName structure. References by PRCounterHandle are
** dead-reconed to the RName structure. The PRCounterHandle is
** "overloaded" for traversing the QName structures; only the
** function PR_FindNextQnameHandle() uses this overloading.
**
** 
** ToDo (lth): decide on how to lock or atomically update
** individual counters. Candidates are: the global lock; a lock
** per RName structure; Atomic operations (Note that there are
** not adaquate atomic operations (yet) to achieve this goal). At
** this writing (6/19/98) , the update of the counter variable in
** a QName structure is unprotected.
**
*/

#if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
#include "prcountr.h"
#include "prclist.h"
#include "prlock.h"
#include "prlog.h"
#include "prmem.h"
#include <string.h>

/*
**
*/
typedef struct QName
{
    PRCList link;
    PRCList rNameList;
    char    name[PRCOUNTER_NAME_MAX+1];
} QName;

/*
**
*/
typedef struct RName
{
    PRCList link;
    QName   *qName;
    PRLock  *lock;
    volatile PRUint32   counter;    
    char    name[PRCOUNTER_NAME_MAX+1]; 
    char    desc[PRCOUNTER_DESC_MAX+1]; 
} RName;


/*
** Define the Counter Facility database
*/
static PRLock  *counterLock;
static PRCList qNameList;
static PRLogModuleInfo *lm;

/*
** _PR_CounterInitialize() -- Initialize the Counter Facility
**
*/
static void _PR_CounterInitialize( void )
{
    /*
    ** This function should be called only once
    */
    PR_ASSERT( counterLock == NULL );
    
    counterLock = PR_NewLock();
    PR_INIT_CLIST( &qNameList );
    lm = PR_NewLogModule("counters");
    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: Initialization complete"));

    return;
} /* end _PR_CounterInitialize() */

/*
** PR_CreateCounter() -- Create a counter
**
**  ValidateArguments
**  Lock
**  if (qName not already in database)
**      NewQname
**  if (rName already in database )
**      Assert
**  else NewRname
**  NewCounter
**  link 'em up
**  Unlock
**
*/
PR_IMPLEMENT(PRCounterHandle) 
	PR_CreateCounter( 
		const char *qName, 
    	const char *rName, 
        const char *description 
) 
{
    QName   *qnp;
    RName   *rnp;
    PRBool  matchQname = PR_FALSE;

    /* Self initialize, if necessary */
    if ( counterLock == NULL )
        _PR_CounterInitialize();

    /* Validate input arguments */
    PR_ASSERT( strlen(qName) <= PRCOUNTER_NAME_MAX );
    PR_ASSERT( strlen(rName) <= PRCOUNTER_NAME_MAX );
    PR_ASSERT( strlen(description) <= PRCOUNTER_DESC_MAX );

    /* Lock the Facility */
    PR_Lock( counterLock );

    /* Do we already have a matching QName? */
    if (!PR_CLIST_IS_EMPTY( &qNameList ))
    {
        qnp = (QName *) PR_LIST_HEAD( &qNameList );
        do {
            if ( strcmp(qnp->name, qName) == 0)
            {
                matchQname = PR_TRUE;
                break;
            }
            qnp = (QName *)PR_NEXT_LINK( &qnp->link );
        } while( qnp != (QName *)PR_LIST_HEAD( &qNameList ));
    }
    /*
    ** If we did not find a matching QName,
    **    allocate one and initialize it.
    **    link it onto the qNameList.
    **
    */
    if ( matchQname != PR_TRUE )
    {
        qnp = PR_NEWZAP( QName );
        PR_ASSERT( qnp != NULL );
        PR_INIT_CLIST( &qnp->link ); 
        PR_INIT_CLIST( &qnp->rNameList ); 
        strcpy( qnp->name, qName );
        PR_APPEND_LINK( &qnp->link, &qNameList ); 
    }

    /* Do we already have a matching RName? */
    if (!PR_CLIST_IS_EMPTY( &qnp->rNameList ))
    {
        rnp = (RName *) PR_LIST_HEAD( &qnp->rNameList );
        do {
            /*
            ** No duplicate RNames are allowed within a QName
            **
            */
            PR_ASSERT( strcmp(rnp->name, rName));
            rnp = (RName *)PR_NEXT_LINK( &rnp->link );
        } while( rnp != (RName *)PR_LIST_HEAD( &qnp->rNameList ));
    }

    /* Get a new RName structure; initialize its members */
    rnp = PR_NEWZAP( RName );
    PR_ASSERT( rnp != NULL );
    PR_INIT_CLIST( &rnp->link );
    strcpy( rnp->name, rName );
    strcpy( rnp->desc, description );
    rnp->lock = PR_NewLock();
    if ( rnp->lock == NULL )
    {
        PR_ASSERT(0);
    }

    PR_APPEND_LINK( &rnp->link, &qnp->rNameList ); /* add RName to QName's rnList */    
    rnp->qName = qnp;                       /* point the RName to the QName */

    /* Unlock the Facility */
    PR_Unlock( counterLock );
    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: Create: QName: %s %p, RName: %s %p\n\t",
        qName, qnp, rName, rnp ));

    return((PRCounterHandle)rnp);
} /*  end PR_CreateCounter() */
  

/*
**
*/
PR_IMPLEMENT(void) 
	PR_DestroyCounter( 
		PRCounterHandle handle 
)
{
    RName   *rnp = (RName *)handle;
    QName   *qnp = rnp->qName;

    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: Deleting: QName: %s, RName: %s", 
        qnp->name, rnp->name));

    /* Lock the Facility */
    PR_Lock( counterLock );

    /*
    ** Remove RName from the list of RNames in QName
    ** and free RName
    */
    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: Deleting RName: %s, %p", 
        rnp->name, rnp));
    PR_REMOVE_LINK( &rnp->link );
    PR_Free( rnp->lock );
    PR_DELETE( rnp );

    /*
    ** If this is the last RName within QName
    **   remove QName from the qNameList and free it
    */
    if ( PR_CLIST_IS_EMPTY( &qnp->rNameList ) )
    {
        PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: Deleting unused QName: %s, %p", 
            qnp->name, qnp));
        PR_REMOVE_LINK( &qnp->link );
        PR_DELETE( qnp );
    } 

    /* Unlock the Facility */
    PR_Unlock( counterLock );
    return;
} /*  end PR_DestroyCounter() */

/*
**
*/
PR_IMPLEMENT(PRCounterHandle) 
	PR_GetCounterHandleFromName( 
    	const char *qName, 
    	const char *rName 
)
{
    const char    *qn, *rn, *desc;
    PRCounterHandle     qh, rh;
    RName   *rnp = NULL;

    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: GetCounterHandleFromName:\n\t"
        "QName: %s, RName: %s", qName, rName ));

    qh = PR_FindNextCounterQname( NULL );
    while (qh != NULL)
    {
        rh = PR_FindNextCounterRname( NULL, qh );
        while ( rh != NULL )
        {
            PR_GetCounterNameFromHandle( rh, &qn, &rn, &desc );
            if ( (strcmp( qName, qn ) == 0)
                && (strcmp( rName, rn ) == 0 ))
            {
                rnp = (RName *)rh;
                goto foundIt;
            }
            rh = PR_FindNextCounterRname( rh, qh );
        }
        qh = PR_FindNextCounterQname( NULL );
    }

foundIt:
    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: GetConterHandleFromName: %p", rnp ));
    return(rh);
} /*  end PR_GetCounterHandleFromName() */

/*
**
*/
PR_IMPLEMENT(void) 
	PR_GetCounterNameFromHandle( 
    	PRCounterHandle handle,  
	    const char **qName, 
	    const char **rName, 
		const char **description 
)
{
    RName   *rnp = (RName *)handle;
    QName   *qnp = rnp->qName;

    *qName = qnp->name;
    *rName = rnp->name;
    *description = rnp->desc;

    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: GetConterNameFromHandle: "
        "QNp: %p, RNp: %p,\n\tQName: %s, RName: %s, Desc: %s", 
        qnp, rnp, qnp->name, rnp->name, rnp->desc ));

    return;
} /*  end PR_GetCounterNameFromHandle() */


/*
**
*/
PR_IMPLEMENT(void) 
	PR_IncrementCounter( 
		PRCounterHandle handle
)
{
    PR_Lock(((RName *)handle)->lock);
    ((RName *)handle)->counter++;
    PR_Unlock(((RName *)handle)->lock);

    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: Increment: %p, %ld", 
        handle, ((RName *)handle)->counter ));

    return;
} /*  end PR_IncrementCounter() */



/*
**
*/
PR_IMPLEMENT(void) 
	PR_DecrementCounter( 
		PRCounterHandle handle
)
{
    PR_Lock(((RName *)handle)->lock);
    ((RName *)handle)->counter--;
    PR_Unlock(((RName *)handle)->lock);

    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: Decrement: %p, %ld", 
        handle, ((RName *)handle)->counter ));

    return;
} /*  end PR_DecrementCounter()  */


/*
**
*/
PR_IMPLEMENT(void) 
	PR_AddToCounter( 
    	PRCounterHandle handle, 
	    PRUint32 value 
)
{
    PR_Lock(((RName *)handle)->lock);
    ((RName *)handle)->counter += value;
    PR_Unlock(((RName *)handle)->lock);

    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: AddToCounter: %p, %ld", 
        handle, ((RName *)handle)->counter ));

    return;
} /*  end PR_AddToCounter() */


/*
**
*/
PR_IMPLEMENT(void) 
	PR_SubtractFromCounter( 
    	PRCounterHandle handle, 
	    PRUint32 value 
)
{
    PR_Lock(((RName *)handle)->lock);
    ((RName *)handle)->counter -= value;
    PR_Unlock(((RName *)handle)->lock);
    
    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: SubtractFromCounter: %p, %ld", 
        handle, ((RName *)handle)->counter ));

    return;
} /*  end  PR_SubtractFromCounter() */

/*
**
*/
PR_IMPLEMENT(PRUint32) 
	PR_GetCounter( 
		PRCounterHandle handle 
)
{
    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: GetCounter: %p, %ld", 
        handle, ((RName *)handle)->counter ));

    return(((RName *)handle)->counter);
} /*  end  PR_GetCounter() */

/*
**
*/
PR_IMPLEMENT(void) 
	PR_SetCounter( 
		PRCounterHandle handle, 
		PRUint32 value 
)
{
    ((RName *)handle)->counter = value;

    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: SetCounter: %p, %ld", 
        handle, ((RName *)handle)->counter ));

    return;
} /*  end  PR_SetCounter() */

/*
**
*/
PR_IMPLEMENT(PRCounterHandle) 
	PR_FindNextCounterQname( 
        PRCounterHandle handle
)
{
    QName *qnp = (QName *)handle;

    if ( PR_CLIST_IS_EMPTY( &qNameList ))
            qnp = NULL;
    else if ( qnp == NULL )
        qnp = (QName *)PR_LIST_HEAD( &qNameList );
    else if ( PR_NEXT_LINK( &qnp->link ) ==  &qNameList )
        qnp = NULL;
    else  
        qnp = (QName *)PR_NEXT_LINK( &qnp->link );

    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: FindNextQname: Handle: %p, Returns: %p", 
        handle, qnp ));

    return((PRCounterHandle)qnp);
} /*  end  PR_FindNextCounterQname() */


/*
**
*/
PR_IMPLEMENT(PRCounterHandle) 
	PR_FindNextCounterRname( 
        PRCounterHandle rhandle, 
        PRCounterHandle qhandle 
)
{
    RName *rnp = (RName *)rhandle;
    QName *qnp = (QName *)qhandle;


    if ( PR_CLIST_IS_EMPTY( &qnp->rNameList ))
        rnp = NULL;
    else if ( rnp == NULL )
        rnp = (RName *)PR_LIST_HEAD( &qnp->rNameList );
    else if ( PR_NEXT_LINK( &rnp->link ) ==  &qnp->rNameList )
        rnp = NULL;
    else
        rnp = (RName *)PR_NEXT_LINK( &rnp->link );

    PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: FindNextRname: Rhandle: %p, QHandle: %p, Returns: %p", 
        rhandle, qhandle, rnp ));

    return((PRCounterHandle)rnp);
} /*  end PR_FindNextCounterRname() */


#else  /* !(defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)) */
/*
** NSPR Counters are not defined in this case
**
**
*/
/* Some compilers don't like an empty compilation unit. */
static int dummy = 0;
#endif /* defined(DEBUG) || defined(FORCE_NSPR_COUNTERS) */