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 ***********************************************************************/
64 #include "private/pprthred.h"
76 #define printf PR_LogPrint
77 extern void SetupMacPrintfLog(char *logFile
);
80 PRIntn failed_already
=0;
83 static char* progname
;
84 static PRInt32 loops
= 1000;
85 static int tix1
, tix2
, tix3
;
86 static GCInfo
* gcInfo
;
87 static PRLock
* stderrLock
;
89 typedef struct Type1 Type1
;
90 typedef struct Type2 Type2
;
101 static void PR_CALLBACK
ScanType1(void *obj
) {
102 gcInfo
->livePointer(((Type1
*)obj
)->atwo
);
103 gcInfo
->livePointer(((Type1
*)obj
)->next
);
106 static void PR_CALLBACK
ScanType2(void *obj
) {
107 gcInfo
->livePointer(((Type2
*)obj
)->buf
);
110 static GCType type1
= {
114 static GCType type2
= {
116 /* (void (*)(void*)) ScanType2 */
119 static GCType type3
= {
123 Type1
* NewType1(void) {
124 Type1
* p
= (Type1
*) PR_AllocMemory(sizeof(Type1
), tix1
, PR_ALLOC_DOUBLE
);
125 PR_ASSERT(p
!= NULL
);
129 Type2
* NewType2(void) {
130 Type2
* p
= (Type2
*) PR_AllocMemory(sizeof(Type2
), tix2
, PR_ALLOC_DOUBLE
);
131 PR_ASSERT(p
!= NULL
);
135 void* NewBuffer(PRInt32 size
) {
136 void* p
= PR_AllocMemory(size
, tix3
, PR_ALLOC_DOUBLE
);
137 PR_ASSERT(p
!= NULL
);
141 /* Allocate alot of garbage */
142 static void PR_CALLBACK
AllocStuff(void *unused
) {
144 void* danglingRefs
[50];
145 PRIntervalTime start
, end
;
148 start
= PR_IntervalNow();
149 for (i
= 0; i
< loops
; i
++) {
152 Type1
* t1
= NewType1();
153 t1
->atwo
= NewType2();
154 t1
->next
= NewType1();
155 t1
->atwo
->buf
= NewBuffer(100);
158 Type2
* t2
= NewType2();
159 t2
->buf
= NewBuffer(i
& 16383);
163 memmove(&danglingRefs
[0], &danglingRefs
[1], 49*sizeof(void*));
164 danglingRefs
[49] = p
;
167 end
= PR_IntervalNow();
168 if (debug_mode
) PR_snprintf(msg
, sizeof(msg
), "Thread %p: %ld allocations took %ld ms",
169 PR_GetCurrentThread(), loops
,
170 PR_IntervalToMilliseconds((PRIntervalTime
) (end
- start
)));
173 fprintf(stderr
, "%s\n", msg
);
175 if (debug_mode
) printf("%s\n", msg
);
177 PR_Unlock(stderrLock
);
180 static void usage(char *progname
) {
182 fprintf(stderr
, "Usage: %s [-t threads] [-l loops]\n", progname
);
184 printf("Usage: %s [-t threads] [-l loops]\n", progname
);
189 static int realMain(int argc
, char **argv
, char *notused
) {
194 progname
= strrchr(argv
[0], '/');
195 if (progname
== 0) progname
= argv
[0];
196 for (i
= 1; i
< argc
; i
++) {
197 if (strcmp(argv
[i
], "-t") == 0) {
201 threads
= atoi(argv
[++i
]);
202 if (threads
< 0) threads
= 0;
203 if (threads
> 10000) threads
= 10000;
206 if (strcmp(argv
[i
], "-l") == 0) {
210 loops
= atoi(argv
[++i
]);
219 for (i
= 0; i
< threads
; i
++) {
223 thread
= PR_CreateThreadGCAble(PR_USER_THREAD
, /* thread type */
224 AllocStuff
, /* start function */
226 PR_PRIORITY_NORMAL
, /* priority */
227 PR_LOCAL_THREAD
, /* thread scope */
228 PR_UNJOINABLE_THREAD
, /* thread state */
232 fprintf(stderr
, "%s: no more threads (only %d were created)\n",
235 printf("%s: no more threads (only %d were created)\n",
245 static int padMain(int argc
, char **argv
) {
247 return realMain(argc
, argv
, pad
);
250 int main(int argc
, char **argv
) {
255 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
256 PR_SetThreadGCAble();
259 SetupMacPrintfLog("thrashgc.log");
263 PR_InitGC(0, 0, 0, PR_GLOBAL_THREAD
);
265 stderrLock
= PR_NewLock();
266 tix1
= PR_RegisterType(&type1
);
267 tix2
= PR_RegisterType(&type2
);
268 tix3
= PR_RegisterType(&type3
);
269 gcInfo
= PR_GetGCInfo();
270 rv
= padMain(argc
, argv
);