Back to index

lightning-sunbird  0.9+nobinonly
thrashgc.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /***********************************************************************
00039 ** Name: thrashgc
00040 **
00041 ** Description: test garbace collection functions.
00042 **
00043 ** Modification History:
00044 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00045 **              The debug mode will print all of the printfs associated with this test.
00046 **                    The regress mode will be the default mode. Since the regress tool limits
00047 **           the output to a one line status:PASS or FAIL,all of the printf statements
00048 **                    have been handled with an if (debug_mode) statement.
00049 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
00050 **                   recognize the return code from tha main program.
00051 ***********************************************************************/
00052 /***********************************************************************
00053 ** Includes
00054 ***********************************************************************/
00055 #include "prthread.h"
00056 #include "prgc.h"
00057 #include "prprf.h"
00058 #include "prinrval.h"
00059 #include "prlock.h"
00060 #include "prinit.h"
00061 #include "prcvar.h"
00062 
00063 #ifndef XP_MAC
00064 #include "private/pprthred.h"
00065 #else
00066 #include "pprthred.h"
00067 #endif
00068 
00069 #include <stdio.h>
00070 #include <memory.h>
00071 #include <string.h>
00072 
00073 
00074 #ifdef XP_MAC
00075 #include "prlog.h"
00076 #define printf PR_LogPrint
00077 extern void SetupMacPrintfLog(char *logFile);
00078 #endif
00079 
00080 PRIntn failed_already=0;
00081 PRIntn debug_mode;
00082 
00083 static char* progname;
00084 static PRInt32 loops = 1000;
00085 static int tix1, tix2, tix3;
00086 static GCInfo* gcInfo;
00087 static PRLock* stderrLock;
00088 
00089 typedef struct Type1 Type1;
00090 typedef struct Type2 Type2;
00091 
00092 struct Type1 {
00093   Type2* atwo;
00094   Type1* next;
00095 };
00096 
00097 struct Type2 {
00098   void* buf;
00099 };
00100 
00101 static void PR_CALLBACK ScanType1(void *obj) {
00102   gcInfo->livePointer(((Type1 *)obj)->atwo);
00103   gcInfo->livePointer(((Type1 *)obj)->next);
00104 }
00105 
00106 static void PR_CALLBACK ScanType2(void *obj) {
00107   gcInfo->livePointer(((Type2 *)obj)->buf);
00108 }
00109 
00110 static GCType type1 = {
00111     ScanType1
00112 };
00113 
00114 static GCType type2 = {
00115     ScanType2
00116 /*  (void (*)(void*)) ScanType2 */
00117 };
00118 
00119 static GCType type3 = {
00120   0
00121 };
00122 
00123 Type1* NewType1(void) {
00124     Type1* p = (Type1*) PR_AllocMemory(sizeof(Type1), tix1, PR_ALLOC_DOUBLE);
00125     PR_ASSERT(p != NULL);
00126     return p;
00127 }
00128 
00129 Type2* NewType2(void) {
00130     Type2* p = (Type2*) PR_AllocMemory(sizeof(Type2), tix2, PR_ALLOC_DOUBLE);
00131     PR_ASSERT(p != NULL);
00132     return p;
00133 }
00134 
00135 void* NewBuffer(PRInt32 size) {
00136     void* p = PR_AllocMemory(size, tix3, PR_ALLOC_DOUBLE);
00137     PR_ASSERT(p != NULL);
00138     return p;
00139 }
00140 
00141 /* Allocate alot of garbage */
00142 static void PR_CALLBACK AllocStuff(void *unused) {
00143   PRInt32 i;
00144   void* danglingRefs[50];
00145   PRIntervalTime start, end;
00146   char msg[100];
00147 
00148   start = PR_IntervalNow();
00149   for (i = 0; i < loops; i++) {
00150     void* p;
00151     if (i & 1) {
00152       Type1* t1 = NewType1();
00153       t1->atwo = NewType2();
00154       t1->next = NewType1();
00155       t1->atwo->buf = NewBuffer(100);
00156       p = t1;
00157     } else {
00158       Type2* t2 = NewType2();
00159       t2->buf = NewBuffer(i & 16383);
00160       p = t2;
00161     }
00162     if ((i % 10) == 0) {
00163       memmove(&danglingRefs[0], &danglingRefs[1], 49*sizeof(void*));
00164       danglingRefs[49] = p;
00165     }
00166   }
00167   end = PR_IntervalNow();
00168   if (debug_mode) PR_snprintf(msg, sizeof(msg), "Thread %p: %ld allocations took %ld ms",
00169              PR_GetCurrentThread(), loops,
00170              PR_IntervalToMilliseconds((PRIntervalTime) (end - start)));
00171   PR_Lock(stderrLock);
00172 #ifndef XP_MAC
00173   fprintf(stderr, "%s\n", msg);
00174 #else
00175   if (debug_mode) printf("%s\n", msg);
00176 #endif
00177   PR_Unlock(stderrLock);
00178   }
00179 
00180 static void usage(char *progname) {
00181 #ifndef XP_MAC
00182   fprintf(stderr, "Usage: %s [-t threads] [-l loops]\n", progname);
00183 #else
00184   printf("Usage: %s [-t threads] [-l loops]\n", progname);
00185 #endif
00186   exit(-1);
00187 }
00188 
00189 static int realMain(int argc, char **argv, char *notused) {
00190   int i;
00191   int threads = 0;
00192 
00193 #ifndef XP_MAC
00194   progname = strrchr(argv[0], '/');
00195   if (progname == 0) progname = argv[0];
00196   for (i = 1; i < argc; i++) {
00197     if (strcmp(argv[i], "-t") == 0) {
00198       if (i == argc - 1) {
00199        usage(progname);
00200       }
00201       threads = atoi(argv[++i]);
00202       if (threads < 0) threads = 0;
00203       if (threads > 10000) threads = 10000;
00204       continue;
00205     }
00206     if (strcmp(argv[i], "-l") == 0) {
00207       if (i == argc - 1) {
00208        usage(progname);
00209       }
00210       loops = atoi(argv[++i]);
00211       continue;
00212     }
00213     usage(progname);
00214   }
00215 #else
00216        threads = 50;
00217 #endif
00218 
00219   for (i = 0; i < threads; i++) {
00220     PRThread* thread;
00221 
00222     /* XXXXX */
00223     thread = PR_CreateThreadGCAble(PR_USER_THREAD,  /* thread type */
00224                           AllocStuff,  /* start function */
00225                           NULL,  /* arg */
00226                           PR_PRIORITY_NORMAL,  /* priority */
00227                           PR_LOCAL_THREAD,  /* thread scope */
00228                           PR_UNJOINABLE_THREAD,  /* thread state */
00229                           0);   /* stack size */
00230     if (thread == 0) {
00231 #ifndef XP_MAC
00232       fprintf(stderr, "%s: no more threads (only %d were created)\n",
00233              progname, i);
00234 #else
00235       printf("%s: no more threads (only %d were created)\n",
00236              progname, i);
00237 #endif
00238       break;
00239     }
00240   }
00241   AllocStuff(NULL);
00242   return 0;
00243 }
00244 
00245 static int padMain(int argc, char **argv) {
00246   char pad[512];
00247   return realMain(argc, argv, pad);
00248 }
00249 
00250 int main(int argc, char **argv) {
00251   int rv;
00252 
00253   debug_mode = 1;
00254   
00255   PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00256   PR_SetThreadGCAble();
00257 
00258 #ifdef XP_MAC
00259   SetupMacPrintfLog("thrashgc.log");
00260   debug_mode = 1;
00261 #endif
00262 
00263   PR_InitGC(0, 0, 0, PR_GLOBAL_THREAD);
00264   PR_STDIO_INIT();
00265   stderrLock = PR_NewLock();
00266   tix1 = PR_RegisterType(&type1);
00267   tix2 = PR_RegisterType(&type2);
00268   tix3 = PR_RegisterType(&type3);
00269   gcInfo = PR_GetGCInfo();
00270   rv = padMain(argc, argv);
00271   printf("PASS\n");
00272   PR_Cleanup();
00273   return rv;
00274 }