~ubuntu-branches/ubuntu/saucy/nspr/saucy-updates

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-11-27 17:39:22 UTC
  • mfrom: (1.1.15) (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121127173922-1zfbtwmy1vczqwxq
Tags: 2:4.9.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - rules: Enable Thumb2 build on armel, armhf.
  - control: Change Vcs-* to XS-Debian-Vcs-*.
  - control: Add conflicts to evolution-documentation-*,
    language-support-translation-*.
  - control: Add Breaks: evolution-plugins.

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