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
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/
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
15
* The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36
* ***** END LICENSE BLOCK ***** */
38
/***********************************************************************
41
** Description: test garbace collection functions.
43
** Modification History:
44
** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
45
** The debug mode will print all of the printfs associated with this test.
46
** The regress mode will be the default mode. Since the regress tool limits
47
** the output to a one line status:PASS or FAIL,all of the printf statements
48
** have been handled with an if (debug_mode) statement.
49
** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
50
** recognize the return code from tha main program.
51
***********************************************************************/
52
/***********************************************************************
54
***********************************************************************/
63
#include "private/pprthred.h"
70
PRIntn failed_already=0;
73
static char* progname;
74
static PRInt32 loops = 1000;
75
static int tix1, tix2, tix3;
76
static GCInfo* gcInfo;
77
static PRLock* stderrLock;
79
typedef struct Type1 Type1;
80
typedef struct Type2 Type2;
91
static void PR_CALLBACK ScanType1(void *obj) {
92
gcInfo->livePointer(((Type1 *)obj)->atwo);
93
gcInfo->livePointer(((Type1 *)obj)->next);
96
static void PR_CALLBACK ScanType2(void *obj) {
97
gcInfo->livePointer(((Type2 *)obj)->buf);
100
static GCType type1 = {
104
static GCType type2 = {
106
/* (void (*)(void*)) ScanType2 */
109
static GCType type3 = {
113
Type1* NewType1(void) {
114
Type1* p = (Type1*) PR_AllocMemory(sizeof(Type1), tix1, PR_ALLOC_DOUBLE);
115
PR_ASSERT(p != NULL);
119
Type2* NewType2(void) {
120
Type2* p = (Type2*) PR_AllocMemory(sizeof(Type2), tix2, PR_ALLOC_DOUBLE);
121
PR_ASSERT(p != NULL);
125
void* NewBuffer(PRInt32 size) {
126
void* p = PR_AllocMemory(size, tix3, PR_ALLOC_DOUBLE);
127
PR_ASSERT(p != NULL);
131
/* Allocate alot of garbage */
132
static void PR_CALLBACK AllocStuff(void *unused) {
134
void* danglingRefs[50];
135
PRIntervalTime start, end;
138
start = PR_IntervalNow();
139
for (i = 0; i < loops; i++) {
142
Type1* t1 = NewType1();
143
t1->atwo = NewType2();
144
t1->next = NewType1();
145
t1->atwo->buf = NewBuffer(100);
148
Type2* t2 = NewType2();
149
t2->buf = NewBuffer(i & 16383);
153
memmove(&danglingRefs[0], &danglingRefs[1], 49*sizeof(void*));
154
danglingRefs[49] = p;
157
end = PR_IntervalNow();
158
if (debug_mode) PR_snprintf(msg, sizeof(msg), "Thread %p: %ld allocations took %ld ms",
159
PR_GetCurrentThread(), loops,
160
PR_IntervalToMilliseconds((PRIntervalTime) (end - start)));
162
fprintf(stderr, "%s\n", msg);
163
PR_Unlock(stderrLock);
166
static void usage(char *progname) {
167
fprintf(stderr, "Usage: %s [-t threads] [-l loops]\n", progname);
171
static int realMain(int argc, char **argv, char *notused) {
175
progname = strrchr(argv[0], '/');
176
if (progname == 0) progname = argv[0];
177
for (i = 1; i < argc; i++) {
178
if (strcmp(argv[i], "-t") == 0) {
182
threads = atoi(argv[++i]);
183
if (threads < 0) threads = 0;
184
if (threads > 10000) threads = 10000;
187
if (strcmp(argv[i], "-l") == 0) {
191
loops = atoi(argv[++i]);
197
for (i = 0; i < threads; i++) {
201
thread = PR_CreateThreadGCAble(PR_USER_THREAD, /* thread type */
202
AllocStuff, /* start function */
204
PR_PRIORITY_NORMAL, /* priority */
205
PR_LOCAL_THREAD, /* thread scope */
206
PR_UNJOINABLE_THREAD, /* thread state */
209
fprintf(stderr, "%s: no more threads (only %d were created)\n",
218
static int padMain(int argc, char **argv) {
220
return realMain(argc, argv, pad);
223
int main(int argc, char **argv) {
228
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
229
PR_SetThreadGCAble();
231
PR_InitGC(0, 0, 0, PR_GLOBAL_THREAD);
233
stderrLock = PR_NewLock();
234
tix1 = PR_RegisterType(&type1);
235
tix2 = PR_RegisterType(&type2);
236
tix3 = PR_RegisterType(&type3);
237
gcInfo = PR_GetGCInfo();
238
rv = padMain(argc, argv);