summaryrefslogtreecommitdiff
path: root/nsprpub/pr/tests/alarm.c
blob: e3dedb67fcae5b33d4ff9bcb1348abedf96ac7c5 (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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

/***********************************************************************
**  1996 - Netscape Communications Corporation
**
** Name: alarmtst.c
**
** Description: Test alarms
**
** Modification History:
** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
**	         The debug mode will print all of the printfs associated with this test.
**			 The regress mode will be the default mode. Since the regress tool limits
**           the output to a one line status:PASS or FAIL,all of the printf statements
**			 have been handled with an if (debug_mode) statement.
** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
**			recognize the return code from tha main program.
***********************************************************************/

/***********************************************************************
** Includes
***********************************************************************/

#include "prlog.h"
#include "prinit.h"
#include "obsolete/pralarm.h"
#include "prlock.h"
#include "prlong.h"
#include "prcvar.h"
#include "prinrval.h"
#include "prtime.h"

/* Used to get the command line option */
#include "plgetopt.h"
#include <stdio.h>
#include <stdlib.h>

#if defined(XP_UNIX)
#include <sys/time.h>
#endif

static PRIntn debug_mode;
static PRIntn failed_already=0;
static PRThreadScope thread_scope = PR_LOCAL_THREAD;

typedef struct notifyData {
    PRLock *ml;
    PRCondVar *child;
    PRCondVar *parent;
    PRBool pending;
    PRUint32 counter;
} NotifyData;

static void Notifier(void *arg)
{
    NotifyData *notifyData = (NotifyData*)arg;
    PR_Lock(notifyData->ml);
    while (notifyData->counter > 0)
    {
        while (!notifyData->pending)
            PR_WaitCondVar(notifyData->child, PR_INTERVAL_NO_TIMEOUT);
        notifyData->counter -= 1;
        notifyData->pending = PR_FALSE;
        PR_NotifyCondVar(notifyData->parent);
    }
    PR_Unlock(notifyData->ml);
}  /* Notifier */
/***********************************************************************
** PRIVATE FUNCTION:    ConditionNotify
** DESCRIPTION:
** 
** INPUTS:      loops
** OUTPUTS:     None
** RETURN:      overhead
** SIDE EFFECTS:
**      
** RESTRICTIONS:
**      None
** MEMORY:      NA
** ALGORITHM:
**      
***********************************************************************/


static PRIntervalTime ConditionNotify(PRUint32 loops)
{
    PRThread *thread;
    NotifyData notifyData;
    PRIntervalTime timein, overhead;
    
    timein = PR_IntervalNow();

    notifyData.counter = loops;
    notifyData.ml = PR_NewLock();
    notifyData.child = PR_NewCondVar(notifyData.ml);
    notifyData.parent = PR_NewCondVar(notifyData.ml);
    thread = PR_CreateThread(
        PR_USER_THREAD, Notifier, &notifyData,
        PR_GetThreadPriority(PR_GetCurrentThread()),
        thread_scope, PR_JOINABLE_THREAD, 0);

    overhead = PR_IntervalNow() - timein;  /* elapsed so far */

    PR_Lock(notifyData.ml);
    while (notifyData.counter > 0)
    {
        notifyData.pending = PR_TRUE;
        PR_NotifyCondVar(notifyData.child);
        while (notifyData.pending)
            PR_WaitCondVar(notifyData.parent, PR_INTERVAL_NO_TIMEOUT);
    }
    PR_Unlock(notifyData.ml);

    timein = PR_IntervalNow();

    (void)PR_JoinThread(thread);
    PR_DestroyCondVar(notifyData.child);
    PR_DestroyCondVar(notifyData.parent);
    PR_DestroyLock(notifyData.ml);
    
    overhead += (PR_IntervalNow() - timein);  /* more overhead */

    return overhead;
}  /* ConditionNotify */

static PRIntervalTime ConditionTimeout(PRUint32 loops)
{
    PRUintn count;
    PRIntervalTime overhead, timein = PR_IntervalNow();

    PRLock *ml = PR_NewLock();
    PRCondVar *cv = PR_NewCondVar(ml);
    PRIntervalTime interval = PR_MillisecondsToInterval(50);

    overhead = PR_IntervalNow() - timein;

    PR_Lock(ml);
    for (count = 0; count < loops; ++count)
    {
        overhead += interval;
        PR_ASSERT(PR_WaitCondVar(cv, interval) == PR_SUCCESS);
    }
    PR_Unlock(ml);

    timein = PR_IntervalNow();
    PR_DestroyCondVar(cv);
    PR_DestroyLock(ml);
    overhead += (PR_IntervalNow() - timein);

    return overhead;
}  /* ConditionTimeout */

typedef struct AlarmData {
    PRLock *ml;
    PRCondVar *cv;
    PRUint32 rate, late, times;
    PRIntervalTime duration, timein, period;
} AlarmData;

static PRBool AlarmFn1(PRAlarmID *id, void *clientData, PRUint32 late)
{
    PRStatus rv = PR_SUCCESS;
    PRBool keepGoing, resetAlarm;
    PRIntervalTime interval, now = PR_IntervalNow();
    AlarmData *ad = (AlarmData*)clientData;

    PR_Lock(ad->ml);
    ad->late += late;
    ad->times += 1;
    keepGoing = ((PRIntervalTime)(now - ad->timein) < ad->duration) ?
        PR_TRUE : PR_FALSE;
    if (!keepGoing)
        rv = PR_NotifyCondVar(ad->cv);
    resetAlarm = ((ad->times % 31) == 0) ? PR_TRUE : PR_FALSE;
                                         
    interval = (ad->period + ad->rate - 1) / ad->rate;
    if (!late && (interval > 10))
    {
        interval &= (now & 0x03) + 1;
        PR_WaitCondVar(ad->cv, interval);
    }
          
    PR_Unlock(ad->ml);

    if (rv != PR_SUCCESS)
    {
		if (!debug_mode) failed_already=1;
		else
		 printf("AlarmFn: notify status: FAIL\n");
		
	}

    if (resetAlarm)
    {   
        ad->rate += 3;
        ad->late = ad->times = 0;
        if (PR_ResetAlarm(id, ad->period, ad->rate) != PR_SUCCESS)
        {
			if (!debug_mode)
				failed_already=1;
			else		
				printf("AlarmFn: Resetting alarm status: FAIL\n");

            keepGoing = PR_FALSE;
        }

    }

    return keepGoing;
}  /* AlarmFn1 */

static PRIntervalTime Alarms1(PRUint32 loops)
{
    PRAlarm *alarm;
    AlarmData ad;
    PRIntervalTime overhead, timein = PR_IntervalNow();
    PRIntervalTime duration = PR_SecondsToInterval(3);

    PRLock *ml = PR_NewLock();
    PRCondVar *cv = PR_NewCondVar(ml);

    ad.ml = ml;
    ad.cv = cv;
    ad.rate = 1;
    ad.times = loops;
    ad.late = ad.times = 0;
    ad.duration = duration;
    ad.timein = PR_IntervalNow();
    ad.period = PR_SecondsToInterval(1);

    alarm = PR_CreateAlarm();

    (void)PR_SetAlarm(
        alarm, ad.period, ad.rate, AlarmFn1, &ad);
        
    overhead = PR_IntervalNow() - timein;

    PR_Lock(ml);
    while ((PRIntervalTime)(PR_IntervalNow() - ad.timein) < duration)
        PR_WaitCondVar(cv, PR_INTERVAL_NO_TIMEOUT);
    PR_Unlock(ml);

    timein = PR_IntervalNow();
    (void)PR_DestroyAlarm(alarm);
    PR_DestroyCondVar(cv);
    PR_DestroyLock(ml);
    overhead += (PR_IntervalNow() - timein);
    
    return duration + overhead;
}  /* Alarms1 */

static PRBool AlarmFn2(PRAlarmID *id, void *clientData, PRUint32 late)
{
    PRBool keepGoing;
    PRStatus rv = PR_SUCCESS;
    AlarmData *ad = (AlarmData*)clientData;
    PRIntervalTime interval, now = PR_IntervalNow();

    PR_Lock(ad->ml);
    ad->times += 1;
    keepGoing = ((PRIntervalTime)(now - ad->timein) < ad->duration) ?
        PR_TRUE : PR_FALSE;
    interval = (ad->period + ad->rate - 1) / ad->rate;

    if (!late && (interval > 10))
    {
        interval &= (now & 0x03) + 1;
        PR_WaitCondVar(ad->cv, interval);
    }

    if (!keepGoing) rv = PR_NotifyCondVar(ad->cv);

    PR_Unlock(ad->ml);


    if (rv != PR_SUCCESS)
		failed_already=1;;

    return keepGoing;
}  /* AlarmFn2 */

static PRIntervalTime Alarms2(PRUint32 loops)
{
    PRStatus rv;
    PRAlarm *alarm;
    PRIntervalTime overhead, timein = PR_IntervalNow();
    AlarmData ad;
    PRIntervalTime duration = PR_SecondsToInterval(30);

    PRLock *ml = PR_NewLock();
    PRCondVar *cv = PR_NewCondVar(ml);

    ad.ml = ml;
    ad.cv = cv;
    ad.rate = 1;
    ad.times = loops;
    ad.late = ad.times = 0;
    ad.duration = duration;
    ad.timein = PR_IntervalNow();
    ad.period = PR_SecondsToInterval(1);

    alarm = PR_CreateAlarm();

    (void)PR_SetAlarm(
        alarm, ad.period, ad.rate, AlarmFn2, &ad);
        
    overhead = PR_IntervalNow() - timein;

    PR_Lock(ml);
    while ((PRIntervalTime)(PR_IntervalNow() - ad.timein) < duration)
        PR_WaitCondVar(cv, PR_INTERVAL_NO_TIMEOUT);
    PR_Unlock(ml);
    
    timein = PR_IntervalNow();

    rv = PR_DestroyAlarm(alarm);
    if (rv != PR_SUCCESS)
    {
		if (!debug_mode)
			failed_already=1;
		else	
			printf("***Destroying alarm status: FAIL\n");
    }
		

    PR_DestroyCondVar(cv);
    PR_DestroyLock(ml);
    
    overhead += (PR_IntervalNow() - timein);
    
    return duration + overhead;
}  /* Alarms2 */

static PRIntervalTime Alarms3(PRUint32 loops)
{
    PRIntn i;
    PRStatus rv;
    PRAlarm *alarm;
    AlarmData ad[3];
    PRIntervalTime duration = PR_SecondsToInterval(30);
    PRIntervalTime overhead, timein = PR_IntervalNow();

    PRLock *ml = PR_NewLock();
    PRCondVar *cv = PR_NewCondVar(ml);

    for (i = 0; i < 3; ++i)
    {
        ad[i].ml = ml;
        ad[i].cv = cv;
        ad[i].rate = 1;
        ad[i].times = loops;
        ad[i].duration = duration;
        ad[i].late = ad[i].times = 0;
        ad[i].timein = PR_IntervalNow();
        ad[i].period = PR_SecondsToInterval(1);

        /* more loops, faster rate => same elapsed time */
        ad[i].times = (i + 1) * loops;
        ad[i].rate = (i + 1) * 10;
    }

    alarm = PR_CreateAlarm();

    for (i = 0; i < 3; ++i)
    {
        (void)PR_SetAlarm(
            alarm, ad[i].period, ad[i].rate,
            AlarmFn2, &ad[i]);
    }
        
    overhead = PR_IntervalNow() - timein;

    PR_Lock(ml);
    for (i = 0; i < 3; ++i)
    {
        while ((PRIntervalTime)(PR_IntervalNow() - ad[i].timein) < duration)
            PR_WaitCondVar(cv, PR_INTERVAL_NO_TIMEOUT);
    }
    PR_Unlock(ml);

    timein = PR_IntervalNow();

	if (debug_mode)
	printf
        ("Alarms3 finished at %u, %u, %u\n",
        ad[0].timein, ad[1].timein, ad[2].timein);
    
    rv = PR_DestroyAlarm(alarm);
    if (rv != PR_SUCCESS)
    {
		if (!debug_mode)		
			failed_already=1;
		else	
		   printf("***Destroying alarm status: FAIL\n");
	}
    PR_DestroyCondVar(cv);
    PR_DestroyLock(ml);
    
    overhead += (duration / 3);
    overhead += (PR_IntervalNow() - timein);

    return overhead;
}  /* Alarms3 */

static PRUint32 TimeThis(
    const char *msg, PRUint32 (*func)(PRUint32 loops), PRUint32 loops)
{
    PRUint32 overhead, usecs;
    PRIntervalTime predicted, timein, timeout, ticks;

    if (debug_mode)
      printf("Testing %s ...", msg);

    timein = PR_IntervalNow();
    predicted = func(loops);
    timeout = PR_IntervalNow();

    if (debug_mode)
      printf(" done\n");

    ticks = timeout - timein;
    usecs = PR_IntervalToMicroseconds(ticks);
    overhead = PR_IntervalToMicroseconds(predicted);

    if(ticks < predicted)
    {
		if (debug_mode) {
        printf("\tFinished in negative time\n");
        printf("\tpredicted overhead was %d usecs\n", overhead);
        printf("\ttest completed in %d usecs\n\n", usecs);
		}
    }
    else
    {
	if (debug_mode)		
        printf(
            "\ttotal: %d usecs\n\toverhead: %d usecs\n\tcost: %6.3f usecs\n\n",
            usecs, overhead, ((double)(usecs - overhead) / (double)loops));
    }

    return overhead;
}  /* TimeThis */

int prmain(int argc, char** argv)
{
    PRUint32 cpu, cpus = 0, loops = 0;

	/* The command line argument: -d is used to determine if the test is being run
	in debug mode. The regress tool requires only one line output:PASS or FAIL.
	All of the printfs associated with this test has been handled with a if (debug_mode)
	test.
	Usage: test_name [-d]
	*/
	PLOptStatus os;
	PLOptState *opt = PL_CreateOptState(argc, argv, "Gdl:c:");
	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    {
		if (PL_OPT_BAD == os) continue;
        switch (opt->option)
        {
        case 'G':  /* GLOBAL threads */
			thread_scope = PR_GLOBAL_THREAD;
            break;
        case 'd':  /* debug mode */
			debug_mode = 1;
            break;
        case 'l':  /* loop count */
			loops = atoi(opt->value);
            break;
        case 'c':  /* concurrency limit */
			cpus = atoi(opt->value);
            break;
         default:
            break;
        }
    }
	PL_DestroyOptState(opt);


    if (cpus == 0) cpus = 1;
    if (loops == 0) loops = 4;

	if (debug_mode)
		printf("Alarm: Using %d loops\n", loops);

	if (debug_mode)		
        printf("Alarm: Using %d cpu(s)\n", cpus);

    for (cpu = 1; cpu <= cpus; ++cpu)
    {
      if (debug_mode)
        printf("\nAlarm: Using %d CPU(s)\n", cpu);

      PR_SetConcurrency(cpu);

      /* some basic time test */
      (void)TimeThis("ConditionNotify", ConditionNotify, loops);
      (void)TimeThis("ConditionTimeout", ConditionTimeout, loops);
      (void)TimeThis("Alarms1", Alarms1, loops);
      (void)TimeThis("Alarms2", Alarms2, loops);
      (void)TimeThis("Alarms3", Alarms3, loops);
    }
    return 0;
}

int main(int argc, char** argv)
{
     PR_Initialize(prmain, argc, argv, 0);
     PR_STDIO_INIT();
	 if (failed_already) return 1;
	 else return 0;

}  /* main */


/* alarmtst.c */