~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/lib/msgc/tests/gc1.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* 
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are 
 
17
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
22
 * Alternatively, the contents of this file may be used under the
 
23
 * terms of the GNU General Public License Version 2 or later (the
 
24
 * "GPL"), in which case the provisions of the GPL are applicable 
 
25
 * instead of those above.  If you wish to allow use of your 
 
26
 * version of this file only under the terms of the GPL and not to
 
27
 * allow others to use your version of this file under the MPL,
 
28
 * indicate your decision by deleting the provisions above and
 
29
 * replace them with the notice and other provisions required by
 
30
 * the GPL.  If you do not delete the provisions above, a recipient
 
31
 * may use your version of this file under either the MPL or the
 
32
 * GPL.
 
33
 */
 
34
 
 
35
/***********************************************************************
 
36
** Includes
 
37
***********************************************************************/
 
38
/* Used to get the command line option */
 
39
#include "plgetopt.h"
 
40
 
 
41
#include "prgc.h"
 
42
#include "prinit.h"
 
43
#include "prmon.h"
 
44
#include "prinrval.h"
 
45
#ifndef XP_MAC
 
46
#include "private/pprthred.h"
 
47
#else
 
48
#include "pprthred.h"
 
49
#endif
 
50
 
 
51
#include <stdio.h>
 
52
#include <stdlib.h>
 
53
 
 
54
#ifdef XP_MAC
 
55
#include "prlog.h"
 
56
#define printf PR_LogPrint
 
57
extern void SetupMacPrintfLog(char *logFile);
 
58
#endif
 
59
 
 
60
static PRMonitor *mon;
 
61
static PRInt32 threads, waiting, iterations;
 
62
static PRInt32 scanCount, finalizeCount, freeCount;
 
63
 
 
64
PRIntn failed_already=0;
 
65
PRIntn debug_mode;
 
66
 
 
67
 
 
68
typedef struct Array {
 
69
    PRUintn size;
 
70
    void *body[1];
 
71
} Array;
 
72
 
 
73
int arrayTypeIndex;
 
74
 
 
75
static void PR_CALLBACK ScanArray(void *a)
 
76
{
 
77
/*      printf ("In ScanArray a = %X size = %d \n", a, a->size); */
 
78
        scanCount++;
 
79
}
 
80
 
 
81
static void PR_CALLBACK FinalizeArray(void *a)
 
82
{
 
83
/*      printf ("In FinalizeArray a = %X size = %d \n", a, a->size); */ 
 
84
        finalizeCount++;
 
85
}
 
86
 
 
87
static void PR_CALLBACK FreeArray(void *a)
 
88
{
 
89
/*      printf ("In FreeArray\n");      */
 
90
        freeCount++;
 
91
}
 
92
 
 
93
static Array *NewArray(PRUintn size)
 
94
{
 
95
    Array *a;
 
96
 
 
97
    a = (Array *)PR_AllocMemory(sizeof(Array) + size*sizeof(void*) - 1*sizeof(void*),
 
98
                       arrayTypeIndex, PR_ALLOC_CLEAN);
 
99
 
 
100
/*      printf ("In NewArray a = %X \n", a); */ 
 
101
 
 
102
    if (a)
 
103
        a->size = size;
 
104
    return a;
 
105
}
 
106
 
 
107
GCType arrayType = {
 
108
    ScanArray,
 
109
    FinalizeArray,
 
110
    0,
 
111
    0,
 
112
    FreeArray,
 
113
    0
 
114
};
 
115
 
 
116
static void Initialize(void)
 
117
{
 
118
    PR_InitGC(0, 0, 0, PR_GLOBAL_THREAD);
 
119
    arrayTypeIndex = PR_RegisterType(&arrayType);
 
120
}
 
121
 
 
122
static void PR_CALLBACK AllocateLikeMad(void *arg)
 
123
{
 
124
    Array *prev;
 
125
    PRInt32 i;
 
126
        PRInt32 count;
 
127
 
 
128
        count = (PRInt32)arg;
 
129
    prev = 0;
 
130
    for (i = 0; i < count; i++) {
 
131
        Array *leak = NewArray(i & 511);
 
132
        if ((i & 1023) == 0) {
 
133
            prev = 0;                   /* forget */
 
134
        } else {
 
135
            if (i & 1) {
 
136
                prev = leak;            /* remember */
 
137
            }
 
138
        }
 
139
    }
 
140
    PR_EnterMonitor(mon);
 
141
    waiting++;
 
142
    PR_Notify(mon);
 
143
    PR_ExitMonitor(mon);
 
144
}
 
145
 
 
146
int main(int argc, char **argv)
 
147
{
 
148
    PRIntervalTime start, stop, usec;
 
149
    double d;
 
150
    PRIntn i, totalIterations;
 
151
        /* The command line argument: -d is used to determine if the test is being run
 
152
        in debug mode. The regress tool requires only one line output:PASS or FAIL.
 
153
        All of the printfs associated with this test has been handled with a if (debug_mode)
 
154
        test.
 
155
        Usage: test_name -d
 
156
        */
 
157
        PLOptStatus os;
 
158
        PLOptState *opt = PL_CreateOptState(argc, argv, "dt:c:");
 
159
 
 
160
        threads = 10;
 
161
        iterations = 100;
 
162
 
 
163
        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
 
164
    {
 
165
        if (PL_OPT_BAD == os) {
 
166
            fprintf(stderr, "Invalid command-line option\n");
 
167
            exit(1);
 
168
        }
 
169
        switch (opt->option)
 
170
        {
 
171
        case 'd':  /* debug mode */
 
172
                        debug_mode = 1;
 
173
            break;
 
174
        case 't':  /* number of threads */
 
175
            threads = atoi(opt->value);
 
176
            break;
 
177
        case 'c':  /* iteration count */
 
178
            iterations = atoi(opt->value);
 
179
            break;
 
180
        default:
 
181
            break;
 
182
        }
 
183
    }
 
184
        PL_DestroyOptState(opt);
 
185
 
 
186
    fprintf(stderr, "t is %ld, i is %ld\n", (long) threads, (long) iterations);
 
187
        /* main test */
 
188
 
 
189
    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 5);
 
190
    PR_STDIO_INIT();
 
191
    Initialize();
 
192
 
 
193
#ifdef XP_MAC
 
194
        SetupMacPrintfLog("gc1.log");
 
195
        debug_mode = 1;
 
196
#endif
 
197
 
 
198
    /* Spin all of the allocator threads and then wait for them to exit */
 
199
    start = PR_IntervalNow();
 
200
    mon = PR_NewMonitor();
 
201
    PR_EnterMonitor(mon);
 
202
    waiting = 0;
 
203
    for (i = 0; i < threads; i++) {
 
204
        (void) PR_CreateThreadGCAble(PR_USER_THREAD,
 
205
                               AllocateLikeMad, (void*)iterations,
 
206
                                                      PR_PRIORITY_NORMAL,
 
207
                                                      PR_LOCAL_THREAD,
 
208
                                                  PR_UNJOINABLE_THREAD,
 
209
                                                      0);
 
210
    }
 
211
    while (waiting != threads) {
 
212
        PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
 
213
    }
 
214
    PR_ExitMonitor(mon);
 
215
 
 
216
        PR_GC();
 
217
        PR_ForceFinalize();     
 
218
 
 
219
        totalIterations = iterations * threads;
 
220
/*
 
221
        if (scanCount != totalIterations)
 
222
                printf ("scanCount discrepancy scanCount = %d totalIterations = %d \n", 
 
223
                        scanCount, totalIterations);
 
224
        if (freeCount != totalIterations)
 
225
                printf ("freeCount discrepancy freeCount = %d totalIterations = %d \n", 
 
226
                        freeCount, totalIterations);
 
227
        if ((finalizeCount != totalIterations) && (finalizeCount != (totalIterations-1)))
 
228
                printf ("finalizeCount discrepancy finalizeCount = %d totalIterations = %d \n", 
 
229
                        finalizeCount,totalIterations);
 
230
*/
 
231
 
 
232
    stop = PR_IntervalNow();
 
233
    
 
234
    usec = stop = stop - start;
 
235
    d = (double)usec;
 
236
 
 
237
    if (debug_mode) printf("%40s: %6.2f usec\n", "GC allocation", d / (iterations * threads));
 
238
        else {
 
239
                if (d == 0.0) failed_already = PR_TRUE;
 
240
 
 
241
        }
 
242
 
 
243
    PR_Cleanup();
 
244
        if(failed_already)      
 
245
        {
 
246
            printf("FAIL\n");
 
247
                return 1;
 
248
        }
 
249
        else
 
250
        {
 
251
            printf("PASS\n");
 
252
                return 0;
 
253
        }
 
254
}