[project @ 2005-05-16 14:27:07 by simonmar]
[ghc-hetmet.git] / ghc / rts / RtsFlags.c
1
2 /* -----------------------------------------------------------------------------
3  *
4  * (c) The AQUA Project, Glasgow University, 1994-1997
5  * (c) The GHC Team, 1998-1999
6  *
7  * Functions for parsing the argument list.
8  *
9  * ---------------------------------------------------------------------------*/
10
11 //@menu
12 //* Includes::                  
13 //* Constants::                 
14 //* Static function decls::     
15 //* Command-line option parsing routines::  
16 //* GranSim specific options::  
17 //* Aux fcts::                  
18 //@end menu
19 //*/
20
21 //@node Includes, Constants
22 //@subsection Includes
23
24 #include "PosixSource.h"
25 #include "Rts.h"
26 #include "RtsFlags.h"
27 #include "RtsUtils.h"
28 #include "BlockAlloc.h"
29 #include "Timer.h"              /* CS_MIN_MILLISECS */
30 #include "Profiling.h"
31
32 #ifdef HAVE_CTYPE_H
33 #include <ctype.h>
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 // Flag Structure
40 RTS_FLAGS RtsFlags;
41
42 /*
43  * Split argument lists
44  */
45 int     prog_argc = 0;    /* an "int" so as to match normal "argc" */
46 char  **prog_argv = NULL;
47 char   *prog_name = NULL; /* 'basename' of prog_argv[0] */
48 int     rts_argc = 0;  /* ditto */
49 char   *rts_argv[MAX_RTS_ARGS];
50
51 //@node Constants, Static function decls, Includes
52 //@subsection Constants
53
54 /*
55  * constants, used later 
56  */
57 #define RTS 1
58 #define PGM 0
59
60 #if defined(GRAN)
61
62 static char *gran_debug_opts_strs[] = {
63   "DEBUG (-bDe, -bD1): event_trace; printing event trace.\n",
64   "DEBUG (-bDE, -bD2): event_stats; printing event statistics.\n",
65   "DEBUG (-bDb, -bD4): bq; check blocking queues\n",
66   "DEBUG (-bDG, -bD8): pack; routines for (un-)packing graph structures.\n",
67   "DEBUG (-bDq, -bD16): checkSparkQ; check consistency of the spark queues.\n",
68   "DEBUG (-bDf, -bD32): thunkStealing; print forwarding of fetches.\n",
69   "DEBUG (-bDr, -bD64): randomSteal; stealing sparks/threads from random PEs.\n",
70   "DEBUG (-bDF, -bD128): findWork; searching spark-pools (local & remote), thread queues for work.\n",
71   "DEBUG (-bDu, -bD256): unused; currently unused flag.\n",
72   "DEBUG (-bDS, -bD512): pri; priority sparking or scheduling.\n",
73   "DEBUG (-bD:, -bD1024): checkLight; check GranSim-Light setup.\n",
74   "DEBUG (-bDo, -bD2048): sortedQ; check whether spark/thread queues are sorted.\n",
75   "DEBUG (-bDz, -bD4096): blockOnFetch; check for blocked on fetch.\n",
76   "DEBUG (-bDP, -bD8192): packBuffer; routines handling pack buffer (GranSim internal!).\n",
77   "DEBUG (-bDt, -bD16384): blockOnFetch_sanity; check for TSO asleep on fetch.\n",
78 };
79
80 /* one character codes for the available debug options */
81 static char gran_debug_opts_flags[] = {
82   'e', 'E', 'b', 'G', 'q', 'f', 'r', 'F', 'u', 'S', ':', 'o', 'z', 'P', 't'
83 };
84
85 #elif defined(PAR)
86
87 static char *par_debug_opts_strs[] = {
88   "DEBUG (-qDv, -qD1): verbose; be generally verbose with parallel related stuff.\n",
89   "DEBUG (-qDq, -qD2): bq; print blocking queues.\n",
90   "DEBUG (-qDs, -qD4): schedule; scheduling of parallel threads.\n",
91   "DEBUG (-qDe, -qD8): free; free messages.\n",
92   "DEBUG (-qDr, -qD16): resume; resume messages.\n",
93   "DEBUG (-qDw, -qD32): weight; print weights and distrib GC stuff.\n",
94   "DEBUG (-qDF, -qD64): fetch; fetch messages.\n",
95   // "DEBUG (-qDa, -qD128): ack; ack messages.\n",
96   "DEBUG (-qDf, -qD128): fish; fish messages.\n",
97   //"DEBUG (-qDo, -qD512): forward; forwarding messages to other PEs.\n",
98   "DEBUG (-qDl, -qD256): tables; print internal LAGA etc tables.\n",
99   "DEBUG (-qDo, -qD512): packet; packets and graph structures when packing.\n",
100   "DEBUG (-qDp, -qD1024): pack; packing and unpacking graphs.\n",
101   "DEBUG (-qDz, -qD2048): paranoia; ridiculously detailed output (excellent for filling a partition).\n"
102 };
103
104 /* one character codes for the available debug options */
105 static char par_debug_opts_flags[] = {
106   'v', 'q', 's', 'e', 'r', 'w', 'F', 'f', 'l', 'o', 'p', 'z'
107 };
108
109 #endif /* PAR */
110
111 //@node Static function decls, Command-line option parsing routines, Constants
112 //@subsection Static function decls
113
114 /* -----------------------------------------------------------------------------
115    Static function decls
116    -------------------------------------------------------------------------- */
117
118 static int              /* return NULL on error */
119 open_stats_file (
120     I_ arg,
121     int argc, char *argv[],
122     int rts_argc, char *rts_argv[],
123     const char *FILENAME_FMT,
124     FILE **file_ret);
125
126 static I_ decode(const char *s);
127 static void bad_option(const char *s);
128
129 #if defined(GRAN)
130 static void enable_GranSimLight(void);
131 static void process_gran_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error);
132 static void set_GranSim_debug_options(nat n);
133 static void help_GranSim_debug_options(nat n);
134 #elif defined(PAR)
135 static void process_par_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error);
136 static void set_par_debug_options(nat n);
137 static void help_par_debug_options(nat n);
138 #endif
139
140 //@node Command-line option parsing routines, GranSim specific options, Static function decls
141 //@subsection Command-line option parsing routines
142
143 /* -----------------------------------------------------------------------------
144  * Command-line option parsing routines.
145  * ---------------------------------------------------------------------------*/
146
147 void initRtsFlagsDefaults(void)
148 {
149     RtsFlags.GcFlags.statsFile          = NULL;
150     RtsFlags.GcFlags.giveStats          = NO_GC_STATS;
151
152     RtsFlags.GcFlags.maxStkSize         = (8 * 1024 * 1024) / sizeof(W_);
153     RtsFlags.GcFlags.initialStkSize     = 1024 / sizeof(W_);
154
155     RtsFlags.GcFlags.minAllocAreaSize   = (512 * 1024)        / BLOCK_SIZE;
156     RtsFlags.GcFlags.minOldGenSize      = (1024 * 1024)       / BLOCK_SIZE;
157     RtsFlags.GcFlags.maxHeapSize        = 0;    /* off by default */
158     RtsFlags.GcFlags.heapSizeSuggestion = 0;    /* none */
159     RtsFlags.GcFlags.pcFreeHeap         = 3;    /* 3% */
160     RtsFlags.GcFlags.oldGenFactor       = 2;
161 #if defined(PAR)
162     /* A hack currently needed for GUM -- HWL */
163     RtsFlags.GcFlags.generations        = 1;
164     RtsFlags.GcFlags.steps              = 2;
165     RtsFlags.GcFlags.squeezeUpdFrames   = rtsFalse;
166 #else
167     RtsFlags.GcFlags.generations        = 2;
168     RtsFlags.GcFlags.steps              = 2;
169     RtsFlags.GcFlags.squeezeUpdFrames   = rtsTrue;
170 #endif
171     RtsFlags.GcFlags.compact            = rtsFalse;
172     RtsFlags.GcFlags.compactThreshold   = 30.0;
173 #ifdef RTS_GTK_FRONTPANEL
174     RtsFlags.GcFlags.frontpanel         = rtsFalse;
175 #endif
176     RtsFlags.GcFlags.idleGCDelayTicks   = 300 / TICK_MILLISECS; /* ticks */
177
178 #ifdef DEBUG
179     RtsFlags.DebugFlags.scheduler       = rtsFalse;
180     RtsFlags.DebugFlags.interpreter     = rtsFalse;
181     RtsFlags.DebugFlags.codegen         = rtsFalse;
182     RtsFlags.DebugFlags.weak            = rtsFalse;
183     RtsFlags.DebugFlags.gccafs          = rtsFalse;
184     RtsFlags.DebugFlags.gc              = rtsFalse;
185     RtsFlags.DebugFlags.block_alloc     = rtsFalse;
186     RtsFlags.DebugFlags.sanity          = rtsFalse;
187     RtsFlags.DebugFlags.stable          = rtsFalse;
188     RtsFlags.DebugFlags.stm             = rtsFalse;
189     RtsFlags.DebugFlags.prof            = rtsFalse;
190     RtsFlags.DebugFlags.gran            = rtsFalse;
191     RtsFlags.DebugFlags.par             = rtsFalse;
192     RtsFlags.DebugFlags.linker          = rtsFalse;
193 #endif
194
195 #if defined(PROFILING) || defined(PAR)
196     RtsFlags.CcFlags.doCostCentres      = 0;
197 #endif /* PROFILING or PAR */
198
199 #ifdef PROFILING
200     RtsFlags.ProfFlags.doHeapProfile      = rtsFalse;
201     RtsFlags.ProfFlags.profileInterval    = 100;
202     RtsFlags.ProfFlags.includeTSOs        = rtsFalse;
203     RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
204     RtsFlags.ProfFlags.maxRetainerSetSize = 8;
205     RtsFlags.ProfFlags.modSelector        = NULL;
206     RtsFlags.ProfFlags.descrSelector      = NULL;
207     RtsFlags.ProfFlags.typeSelector       = NULL;
208     RtsFlags.ProfFlags.ccSelector         = NULL;
209     RtsFlags.ProfFlags.ccsSelector        = NULL;
210     RtsFlags.ProfFlags.retainerSelector   = NULL;
211     RtsFlags.ProfFlags.bioSelector        = NULL;
212
213 #elif defined(DEBUG)
214     RtsFlags.ProfFlags.doHeapProfile      = rtsFalse;
215 #endif
216
217     RtsFlags.ConcFlags.ctxtSwitchTime   = CS_MIN_MILLISECS;  /* In milliseconds */
218
219 #ifdef SMP
220     RtsFlags.ParFlags.nNodes            = 1;
221 #endif
222
223 #ifdef PAR
224     RtsFlags.ParFlags.ParStats.Full       = rtsFalse;
225     RtsFlags.ParFlags.ParStats.Suppressed = rtsFalse;
226     RtsFlags.ParFlags.ParStats.Binary     = rtsFalse;
227     RtsFlags.ParFlags.ParStats.Sparks     = rtsFalse;
228     RtsFlags.ParFlags.ParStats.Heap       = rtsFalse;
229     RtsFlags.ParFlags.ParStats.NewLogfile = rtsFalse;
230     RtsFlags.ParFlags.ParStats.Global     = rtsFalse;
231
232     RtsFlags.ParFlags.outputDisabled    = rtsFalse;
233 #ifdef DIST
234     RtsFlags.ParFlags.doFairScheduling  = rtsTrue;  /* fair sched by def */
235 #else
236     RtsFlags.ParFlags.doFairScheduling  = rtsFalse;  /* unfair sched by def */
237 #endif
238     RtsFlags.ParFlags.packBufferSize    = 1024;
239     RtsFlags.ParFlags.thunksToPack      = 1; /* 0 ... infinity; */
240     RtsFlags.ParFlags.globalising       = 1; /* 0 ... everything */
241     RtsFlags.ParFlags.maxThreads        = 1024;
242     RtsFlags.ParFlags.maxFishes        = MAX_FISHES;
243     RtsFlags.ParFlags.fishDelay         = FISH_DELAY;
244 #endif
245
246 #if defined(PAR) || defined(SMP)
247     RtsFlags.ParFlags.maxLocalSparks    = 4096;
248 #endif /* PAR || SMP */
249
250 #if defined(GRAN)
251     /* ToDo: check defaults for GranSim and GUM */
252     RtsFlags.GcFlags.maxStkSize         = (8 * 1024 * 1024) / sizeof(W_);
253     RtsFlags.GcFlags.initialStkSize     = 1024 / sizeof(W_);
254
255     RtsFlags.GranFlags.maxThreads       = 65536; // refers to mandatory threads
256     RtsFlags.GranFlags.GranSimStats.Full        = rtsFalse;
257     RtsFlags.GranFlags.GranSimStats.Suppressed  = rtsFalse;
258     RtsFlags.GranFlags.GranSimStats.Binary      = rtsFalse;
259     RtsFlags.GranFlags.GranSimStats.Sparks      = rtsFalse;
260     RtsFlags.GranFlags.GranSimStats.Heap        = rtsFalse;
261     RtsFlags.GranFlags.GranSimStats.NewLogfile  = rtsFalse;
262     RtsFlags.GranFlags.GranSimStats.Global      = rtsFalse;
263
264     RtsFlags.GranFlags.packBufferSize   = 1024;
265     RtsFlags.GranFlags.packBufferSize_internal = GRANSIM_DEFAULT_PACK_BUFFER_SIZE;
266
267     RtsFlags.GranFlags.proc         = MAX_PROC;
268     RtsFlags.GranFlags.Fishing      = rtsFalse;
269     RtsFlags.GranFlags.maxFishes   = MAX_FISHES;
270     RtsFlags.GranFlags.time_slice   = GRAN_TIME_SLICE;
271     RtsFlags.GranFlags.Light        = rtsFalse;
272
273     RtsFlags.GranFlags.Costs.latency =             LATENCY;          
274     RtsFlags.GranFlags.Costs.additional_latency =  ADDITIONAL_LATENCY; 
275     RtsFlags.GranFlags.Costs.fetchtime =           FETCHTIME; 
276     RtsFlags.GranFlags.Costs.lunblocktime =        LOCALUNBLOCKTIME; 
277     RtsFlags.GranFlags.Costs.gunblocktime =        GLOBALUNBLOCKTIME;
278     RtsFlags.GranFlags.Costs.mpacktime =           MSGPACKTIME;      
279     RtsFlags.GranFlags.Costs.munpacktime =         MSGUNPACKTIME;
280     RtsFlags.GranFlags.Costs.mtidytime =           MSGTIDYTIME;
281
282     RtsFlags.GranFlags.Costs.threadcreatetime =         THREADCREATETIME;
283     RtsFlags.GranFlags.Costs.threadqueuetime =          THREADQUEUETIME;
284     RtsFlags.GranFlags.Costs.threaddescheduletime =     THREADDESCHEDULETIME;
285     RtsFlags.GranFlags.Costs.threadscheduletime =       THREADSCHEDULETIME;
286     RtsFlags.GranFlags.Costs.threadcontextswitchtime =  THREADCONTEXTSWITCHTIME;
287
288     RtsFlags.GranFlags.Costs.arith_cost =         ARITH_COST;       
289     RtsFlags.GranFlags.Costs.branch_cost =        BRANCH_COST; 
290     RtsFlags.GranFlags.Costs.load_cost =          LOAD_COST;        
291     RtsFlags.GranFlags.Costs.store_cost =         STORE_COST; 
292     RtsFlags.GranFlags.Costs.float_cost =         FLOAT_COST;       
293
294     RtsFlags.GranFlags.Costs.heapalloc_cost =     HEAPALLOC_COST;
295
296     RtsFlags.GranFlags.Costs.pri_spark_overhead = PRI_SPARK_OVERHEAD;        
297     RtsFlags.GranFlags.Costs.pri_sched_overhead = PRI_SCHED_OVERHEAD;        
298
299     RtsFlags.GranFlags.DoFairSchedule           = rtsFalse;             
300     RtsFlags.GranFlags.DoAsyncFetch             = rtsFalse;        
301     RtsFlags.GranFlags.DoStealThreadsFirst      = rtsFalse;        
302     RtsFlags.GranFlags.DoAlwaysCreateThreads    = rtsFalse;      
303     RtsFlags.GranFlags.DoBulkFetching           = rtsFalse;             
304     RtsFlags.GranFlags.DoThreadMigration        = rtsFalse;          
305     RtsFlags.GranFlags.FetchStrategy            = 2;                     
306     RtsFlags.GranFlags.PreferSparksOfLocalNodes = rtsFalse;   
307     RtsFlags.GranFlags.DoPrioritySparking       = rtsFalse;         
308     RtsFlags.GranFlags.DoPriorityScheduling     = rtsFalse;       
309     RtsFlags.GranFlags.SparkPriority            = 0;
310     RtsFlags.GranFlags.SparkPriority2           = 0; 
311     RtsFlags.GranFlags.RandomPriorities         = rtsFalse;           
312     RtsFlags.GranFlags.InversePriorities        = rtsFalse;          
313     RtsFlags.GranFlags.IgnorePriorities         = rtsFalse;           
314     RtsFlags.GranFlags.ThunksToPack             = 0;                      
315     RtsFlags.GranFlags.RandomSteal              = rtsTrue;
316 #endif
317
318 #ifdef TICKY_TICKY
319     RtsFlags.TickyFlags.showTickyStats   = rtsFalse;
320     RtsFlags.TickyFlags.tickyFile        = NULL;
321 #endif
322 }
323
324 static const char *
325 usage_text[] = {
326 "",
327 "Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
328 "",
329 "   +RTS    Indicates run time system options follow",
330 "   -RTS    Indicates program arguments follow",
331 "  --RTS    Indicates that ALL subsequent arguments will be given to the",
332 "           program (including any of these RTS flags)",
333 "",
334 "The following run time system options are available:",
335 "",
336 "  -?       Prints this message and exits; the program is not executed",
337 "",
338 "  -K<size> Sets the maximum stack size (default 8M)  Egs: -K32k   -K512k",
339 "  -k<size> Sets the initial thread stack size (default 1k)  Egs: -k4k   -k2m",
340 "",
341 "  -A<size> Sets the minimum allocation area size (default 256k) Egs: -A1m -A10k",
342 "  -M<size> Sets the maximum heap size (default unlimited)  Egs: -M256k -M1G",
343 "  -H<size> Sets the minimum heap size (default 0M)   Egs: -H24m  -H1G",
344 "  -m<n>    Minimum % of heap which must be available (default 3%)",
345 "  -G<n>    Number of generations (default: 2)",
346 "  -T<n>    Number of steps in younger generations (default: 2)",
347 "  -c<n>    Auto-enable compaction of the oldest generation when live data is",
348 "           at least <n>% of the maximum heap size set with -M (default: 30%)",
349 "  -c       Enable compaction for all major collections",
350 #if defined(RTS_SUPPORTS_THREADS)
351 "  -I<sec>  Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
352 #endif
353 "",
354 "  -t<file> One-line GC statistics  (default file: <program>.stat)",
355 "  -s<file> Summary  GC statistics  (with -Sstderr going to stderr)",
356 "  -S<file> Detailed GC statistics",
357 #ifdef RTS_GTK_FRONTPANEL
358 "  -f       Display front panel (requires X11 & GTK+)",
359 #endif
360 "",
361 "",
362 "  -Z       Don't squeeze out update frames on stack overflow",
363 "  -B       Sound the bell at the start of each garbage collection",
364 #if defined(PROFILING) || defined(PAR)
365 "",
366 "  -px      Time/allocation profile (XML)  (output file <program>.prof)",
367 "  -p       Time/allocation profile        (output file <program>.prof)",
368 "  -P       More detailed Time/Allocation profile",
369 "  -Pa      Give information about *all* cost centres",
370
371 # if defined(PROFILING)
372 "",
373 "  -hx            Heap residency profile (XML)   (output file <program>.prof)",
374 "  -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
375 "     break-down: c = cost centre stack (default)",
376 "                 m = module",
377 "                 d = closure description",
378 "                 y = type description",
379 "                 r = retainer",
380 "                 b = biography (LAG,DRAG,VOID,USE)",
381 "  A subset of closures may be selected thusly:",
382 "    -hc<cc>,...  specific cost centre(s) (top of stack only)",
383 "    -hC<cc>,...  specific cost centre(s) (anywhere in stack)",
384 "    -hm<mod>...  all cost centres from the specified modules(s)",
385 "    -hd<des>,... closures with specified closure descriptions",
386 "    -hy<typ>...  closures with specified type descriptions",
387 "    -hr<cc>...   closures with specified retainers",
388 "    -hb<bio>...  closures with specified biographies (lag,drag,void,use)",
389 "",
390 "  -R<size>       Set the maximum retainer set size (default: 8)",
391 "",
392 "  -i<sec>        Time between heap samples (seconds, default: 0.1)",
393 "",
394 "  -xt            Include threads (TSOs) in a heap profile",
395 "",
396 "  -xc      Show current cost centre stack on raising an exception",
397 # endif
398 #endif /* PROFILING or PAR */
399 #if !defined(PROFILING) && defined(DEBUG)
400 "",
401 "  -h<break-down> Debugging Heap residency profile",
402 "                 (output file <program>.hp)",
403 "     break-down: L = closure label (default)",
404 "                 T = closure type (constructor, thunk etc.)",
405 #endif
406 "",
407 #if defined(TICKY_TICKY)
408 "  -r<file>  Produce reduction profiling statistics (with -rstderr for stderr)",
409 "",
410 #endif
411 #if defined(PAR)
412 "  -N<n>     Use <n> PVMish processors in parallel (default: 2)",
413 /* NB: the -N<n> is implemented by the driver!! */
414 #endif
415 "  -C<secs>  Context-switch interval in seconds",
416 "                (0 or no argument means switch as often as possible)",
417 "                the default is .02 sec; resolution is .02 sec",
418 "",
419 #if defined(DEBUG)
420 "  -Ds  DEBUG: scheduler",
421 "  -Di  DEBUG: interpreter",
422 "  -Dc  DEBUG: codegen",
423 "  -Dw  DEBUG: weak",
424 "  -DG  DEBUG: gccafs",
425 "  -Dg  DEBUG: gc",
426 "  -Db  DEBUG: block",
427 "  -DS  DEBUG: sanity",
428 "  -Dt  DEBUG: stable",
429 "  -Dp  DEBUG: prof",
430 "  -Dr  DEBUG: gran",
431 "  -DP  DEBUG: par",
432 "  -Dl  DEBUG: linker",
433 "  -Dm  DEBUG: stm",
434 "",
435 #endif /* DEBUG */
436 #if defined(SMP)
437 "  -N<n>     Use <n> OS threads (default: 1)",
438 #endif
439 #if defined(SMP) || defined(PAR)
440 "  -e<size>  Size of spark pools (default 100)",
441 #endif
442 #if defined(PAR)
443 "  -t<num>   Set maximum number of advisory threads per PE (default 32)",
444 "  -qP       Enable activity profile (output files in ~/<program>*.gr)",
445 "  -qQ<size> Set pack-buffer size (default: 1024)",
446 "  -qd       Turn on PVM-ish debugging",
447 "  -qO       Disable output for performance measurement",
448 #endif
449 #if defined(SMP) || defined(PAR)
450 "  -e<n>     Maximum number of outstanding local sparks (default: 4096)",
451 #endif
452 #if defined(PAR)
453 "  -d        Turn on PVM-ish debugging",
454 "  -O        Disable output for performance measurement",
455 #endif /* PAR */
456 #if defined(GRAN)  /* ToDo: fill in decent Docu here */
457 "  -b...     All GranSim options start with -b; see GranSim User's Guide for details",
458 #endif
459 "",
460 "RTS options may also be specified using the GHCRTS environment variable.",
461 "",
462 "Other RTS options may be available for programs compiled a different way.",
463 "The GHC User's Guide has full details.",
464 "",
465 0
466 };
467
468 STATIC_INLINE rtsBool
469 strequal(const char *a, const char * b)
470 {
471     return(strcmp(a, b) == 0);
472 }
473
474 static void
475 splitRtsFlags(char *s, int *rts_argc, char *rts_argv[])
476 {
477     char *c1, *c2;
478
479     c1 = s;
480     do {
481         while (isspace(*c1)) { c1++; };
482         c2 = c1;
483         while (!isspace(*c2) && *c2 != '\0') { c2++; };
484         
485         if (c1 == c2) { break; }
486         
487         if (*rts_argc < MAX_RTS_ARGS-1) {
488             s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
489             strncpy(s, c1, c2-c1);
490             s[c2-c1] = '\0';
491             rts_argv[(*rts_argc)++] = s;
492         } else {
493             barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
494         }
495         
496         c1 = c2;
497     } while (*c1 != '\0');
498 }
499     
500 void
501 setupRtsFlags(int *argc, char *argv[], int *rts_argc, char *rts_argv[])
502 {
503     rtsBool error = rtsFalse;
504     I_ mode;
505     I_ arg, total_arg;
506
507     setProgName (argv);
508     total_arg = *argc;
509     arg = 1;
510
511     *argc = 1;
512     *rts_argc = 0;
513
514     // process arguments from the ghc_rts_opts global variable first.
515     // (arguments from the GHCRTS environment variable and the command
516     // line override these).
517     {
518         if (ghc_rts_opts != NULL) {
519             splitRtsFlags(ghc_rts_opts, rts_argc, rts_argv);
520         }
521     }
522
523     // process arguments from the GHCRTS environment variable next
524     // (arguments from the command line override these).
525     {
526         char *ghc_rts = getenv("GHCRTS");
527
528         if (ghc_rts != NULL) {
529             splitRtsFlags(ghc_rts, rts_argc, rts_argv);
530         }
531     }
532
533     // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
534     //   argv[0] must be PGM argument -- leave in argv
535
536     for (mode = PGM; arg < total_arg; arg++) {
537         // The '--RTS' argument disables all future +RTS ... -RTS processing.
538         if (strequal("--RTS", argv[arg])) {
539             arg++;
540             break;
541         }
542         // The '--' argument is passed through to the program, but
543         // disables all further +RTS ... -RTS processing.
544         else if (strequal("--", argv[arg])) {
545             break;
546         }
547         else if (strequal("+RTS", argv[arg])) {
548             mode = RTS;
549         }
550         else if (strequal("-RTS", argv[arg])) {
551             mode = PGM;
552         }
553         else if (mode == RTS && *rts_argc < MAX_RTS_ARGS-1) {
554             rts_argv[(*rts_argc)++] = argv[arg];
555         }
556         else if (mode == PGM) {
557             argv[(*argc)++] = argv[arg];
558         }
559         else {
560           barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
561         }
562     }
563     // process remaining program arguments
564     for (; arg < total_arg; arg++) {
565         argv[(*argc)++] = argv[arg];
566     }
567     argv[*argc] = (char *) 0;
568     rts_argv[*rts_argc] = (char *) 0;
569
570     // Process RTS (rts_argv) part: mainly to determine statsfile
571     for (arg = 0; arg < *rts_argc; arg++) {
572         if (rts_argv[arg][0] != '-') {
573             fflush(stdout);
574             errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
575             error = rtsTrue;
576
577         } else {
578             switch(rts_argv[arg][1]) {
579
580               /* process: general args, then PROFILING-only ones,
581                  then CONCURRENT-only, PARallel-only, GRAN-only,
582                  TICKY-only (same order as defined in RtsFlags.lh);
583                  within those groups, mostly in case-insensitive
584                  alphabetical order.
585                  Final group is x*, which allows for more options.
586               */
587
588 #ifdef TICKY_TICKY
589 # define TICKY_BUILD_ONLY(x) x
590 #else
591 # define TICKY_BUILD_ONLY(x) \
592 errorBelch("not built for: ticky-ticky stats"); \
593 error = rtsTrue;
594 #endif
595
596 #if defined(PROFILING) 
597 # define COST_CENTRE_USING_BUILD_ONLY(x) x
598 #else
599 # define COST_CENTRE_USING_BUILD_ONLY(x) \
600 errorBelch("not built for: -prof or -parallel"); \
601 error = rtsTrue;
602 #endif
603
604 #ifdef PROFILING
605 # define PROFILING_BUILD_ONLY(x)   x
606 #else
607 # define PROFILING_BUILD_ONLY(x) \
608 errorBelch("not built for: -prof"); \
609 error = rtsTrue;
610 #endif
611
612 #ifdef SMP
613 # define SMP_BUILD_ONLY(x)      x
614 #else
615 # define SMP_BUILD_ONLY(x) \
616 errorBelch("not built for: -smp"); \
617 error = rtsTrue;
618 #endif
619
620 #ifdef PAR
621 # define PAR_BUILD_ONLY(x)      x
622 #else
623 # define PAR_BUILD_ONLY(x) \
624 errorBelch("not built for: -parallel"); \
625 error = rtsTrue;
626 #endif
627
628 #if defined(SMP) || defined(PAR)
629 # define PAR_OR_SMP_BUILD_ONLY(x)      x
630 #else
631 # define PAR_OR_SMP_BUILD_ONLY(x) \
632 errorBelch("not built for: -parallel or -smp"); \
633 error = rtsTrue;
634 #endif
635
636 #ifdef GRAN
637 # define GRAN_BUILD_ONLY(x)     x
638 #else
639 # define GRAN_BUILD_ONLY(x) \
640 errorBelch("not built for: -gransim"); \
641 error = rtsTrue;
642 #endif
643
644               /* =========== GENERAL ========================== */
645               case '?':
646                 error = rtsTrue;
647                 break;
648
649               case 'A':
650                 RtsFlags.GcFlags.minAllocAreaSize
651                   = decode(rts_argv[arg]+2) / BLOCK_SIZE;
652                 if (RtsFlags.GcFlags.minAllocAreaSize <= 0) {
653                   bad_option(rts_argv[arg]);
654                 }
655                 break;
656
657               case 'B':
658                 RtsFlags.GcFlags.ringBell = rtsTrue;
659                 break;
660
661               case 'c':
662                   if (rts_argv[arg][2] != '\0') {
663                       RtsFlags.GcFlags.compactThreshold =
664                           atof(rts_argv[arg]+2);
665                   } else {
666                       RtsFlags.GcFlags.compact = rtsTrue;
667                   }
668                   break;
669
670               case 'F':
671                 RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
672               
673                 if (RtsFlags.GcFlags.oldGenFactor < 0)
674                   bad_option( rts_argv[arg] );
675                 break;
676               
677 #ifdef DEBUG
678               case 'D':
679               { 
680                   char *c;
681
682                   for (c  = rts_argv[arg] + 2; *c != '\0'; c++) {
683                       switch (*c) {
684                       case 's':
685                           RtsFlags.DebugFlags.scheduler = rtsTrue;
686                           break;
687                       case 'i':
688                           RtsFlags.DebugFlags.interpreter = rtsTrue;
689                           break;
690                       case 'c':
691                           RtsFlags.DebugFlags.codegen = rtsTrue;
692                           break;
693                       case 'w':
694                           RtsFlags.DebugFlags.weak = rtsTrue;
695                           break;
696                       case 'G':
697                           RtsFlags.DebugFlags.gccafs = rtsTrue;
698                           break;
699                       case 'g':
700                           RtsFlags.DebugFlags.gc = rtsTrue;
701                           break;
702                       case 'b':
703                           RtsFlags.DebugFlags.block_alloc = rtsTrue;
704                           break;
705                       case 'S':
706                           RtsFlags.DebugFlags.sanity = rtsTrue;
707                           break;
708                       case 't':
709                           RtsFlags.DebugFlags.stable = rtsTrue;
710                           break;
711                       case 'p':
712                           RtsFlags.DebugFlags.prof = rtsTrue;
713                           break;
714                       case 'r':
715                           RtsFlags.DebugFlags.gran = rtsTrue;
716                           break;
717                       case 'P':
718                           RtsFlags.DebugFlags.par = rtsTrue;
719                           break;
720                       case 'l':
721                           RtsFlags.DebugFlags.linker = rtsTrue;
722                           break;
723                       case 'a':
724                           RtsFlags.DebugFlags.apply = rtsTrue;
725                           break;
726                       case 'm':
727                           RtsFlags.DebugFlags.stm = rtsTrue;
728                           break;
729                       default:
730                           bad_option( rts_argv[arg] );
731                       }
732                   }
733                   break;
734               }
735 #endif
736
737               case 'K':
738                 RtsFlags.GcFlags.maxStkSize = 
739                   decode(rts_argv[arg]+2) / sizeof(W_);
740
741                 if (RtsFlags.GcFlags.maxStkSize == 0) 
742                   bad_option( rts_argv[arg] );
743                 break;
744
745               case 'k':
746                 RtsFlags.GcFlags.initialStkSize = 
747                   decode(rts_argv[arg]+2) / sizeof(W_);
748
749                 if (RtsFlags.GcFlags.initialStkSize == 0) 
750                   bad_option( rts_argv[arg] );
751                 break;
752
753               case 'M':
754                 RtsFlags.GcFlags.maxHeapSize = 
755                   decode(rts_argv[arg]+2) / BLOCK_SIZE;
756                 /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
757
758                 if (RtsFlags.GcFlags.maxHeapSize <= 0) {
759                   bad_option(rts_argv[arg]);
760                 }
761                 break;
762
763               case 'm':
764                 RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
765
766                 if (RtsFlags.GcFlags.pcFreeHeap < 0 || 
767                     RtsFlags.GcFlags.pcFreeHeap > 100)
768                   bad_option( rts_argv[arg] );
769                 break;
770
771               case 'G':
772                 RtsFlags.GcFlags.generations = decode(rts_argv[arg]+2);
773                 if (RtsFlags.GcFlags.generations < 1) {
774                   bad_option(rts_argv[arg]);
775                 }
776                 break;
777
778               case 'T':
779                 RtsFlags.GcFlags.steps = decode(rts_argv[arg]+2);
780                 if (RtsFlags.GcFlags.steps < 1) {
781                   bad_option(rts_argv[arg]);
782                 }
783                 break;
784
785               case 'H':
786                 RtsFlags.GcFlags.heapSizeSuggestion = 
787                   decode(rts_argv[arg]+2) / BLOCK_SIZE;
788
789                 if (RtsFlags.GcFlags.heapSizeSuggestion <= 0) {
790                   bad_option(rts_argv[arg]);
791                 }
792                 break;
793
794 #ifdef RTS_GTK_FRONTPANEL
795               case 'f':
796                   RtsFlags.GcFlags.frontpanel = rtsTrue;
797                   break;
798 #endif
799
800               case 'I': /* idle GC delay */
801                 if (rts_argv[arg][2] == '\0') {
802                   /* use default */
803                 } else {
804                     I_ cst; /* tmp */
805
806                     /* Convert to ticks */
807                     cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
808                     if (cst > 0 && cst < TICK_MILLISECS) {
809                         cst = TICK_MILLISECS;
810                     } else {
811                         cst = cst / TICK_MILLISECS;
812                     }
813                     RtsFlags.GcFlags.idleGCDelayTicks = cst;
814                 }
815                 break;
816
817               case 'S':
818                   RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
819                   goto stats;
820
821               case 's':
822                   RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
823                   goto stats;
824
825               case 't':
826                   RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
827                   goto stats;
828
829             stats:
830 #ifdef PAR
831                 /* Opening all those files would almost certainly fail... */
832                 // RtsFlags.ParFlags.ParStats.Full = rtsTrue;
833                 RtsFlags.GcFlags.statsFile = NULL; /* temporary; ToDo: rm */
834 #else
835                 { 
836                     int r;
837                     r = open_stats_file(arg, *argc, argv,
838                                         *rts_argc, rts_argv, STAT_FILENAME_FMT,
839                                         &RtsFlags.GcFlags.statsFile);
840                     if (r == -1) { error = rtsTrue; }
841                 }
842 #endif
843                   break;
844
845               case 'Z':
846                 RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
847                 break;
848
849               /* =========== PROFILING ========================== */
850
851               case 'P': /* detailed cost centre profiling (time/alloc) */
852               case 'p': /* cost centre profiling (time/alloc) */
853                 COST_CENTRE_USING_BUILD_ONLY(
854                 switch (rts_argv[arg][2]) {
855                   case 'x':
856                     RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
857                     break;
858                   case 'a':
859                     RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
860                     break;
861                   default:
862                       if (rts_argv[arg][1] == 'P') {
863                           RtsFlags.CcFlags.doCostCentres =
864                               COST_CENTRES_VERBOSE;
865                       } else {
866                           RtsFlags.CcFlags.doCostCentres =
867                               COST_CENTRES_SUMMARY;
868                       }
869                       break;
870                 }
871                 ) break;
872
873               case 'R':
874                   PROFILING_BUILD_ONLY(
875                       RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
876                   ) break;
877
878               case 'h': /* serial heap profile */
879 #if !defined(PROFILING) && defined(DEBUG)
880                 switch (rts_argv[arg][2]) {
881                   case '\0':
882                   case 'L':
883                     RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_INFOPTR;
884                     break;
885                   case 'T':
886                     RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
887                     break;
888                   default:
889                     errorBelch("invalid heap profile option: %s",rts_argv[arg]);
890                     error = rtsTrue;
891                 }
892 #else
893                 PROFILING_BUILD_ONLY(
894                 switch (rts_argv[arg][2]) {
895                 case '\0':
896                 case 'C':
897                 case 'c':
898                 case 'M':
899                 case 'm':
900                 case 'D':
901                 case 'd':
902                 case 'Y':
903                 case 'y':
904                 case 'R':
905                 case 'r':
906                 case 'B':
907                 case 'b':
908                     if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
909                         {
910                             char *left  = strchr(rts_argv[arg], '{');
911                             char *right = strrchr(rts_argv[arg], '}');
912
913                             // curly braces are optional, for
914                             // backwards compat.
915                             if (left)
916                                 left = left+1;
917                             else
918                                 left = rts_argv[arg] + 3;
919
920                             if (!right)
921                                 right = rts_argv[arg] + strlen(rts_argv[arg]);
922
923                             *right = '\0';
924
925                             switch (rts_argv[arg][2]) {
926                             case 'c': // cost centre label select
927                                 RtsFlags.ProfFlags.ccSelector = left;
928                                 break;
929                             case 'C':
930                                 RtsFlags.ProfFlags.ccsSelector = left;
931                                 break;
932                             case 'M':
933                             case 'm': // cost centre module select
934                                 RtsFlags.ProfFlags.modSelector = left;
935                                 break;
936                             case 'D':
937                             case 'd': // closure descr select 
938                                 RtsFlags.ProfFlags.descrSelector = left;
939                                 break;
940                             case 'Y':
941                             case 'y': // closure type select
942                                 RtsFlags.ProfFlags.typeSelector = left;
943                                 break;
944                             case 'R':
945                             case 'r': // retainer select
946                                 RtsFlags.ProfFlags.retainerSelector = left;
947                                 break;
948                             case 'B':
949                             case 'b': // biography select
950                                 RtsFlags.ProfFlags.bioSelector = left;
951                                 break;
952                             }
953                         }
954                         break;
955                     }
956
957                     if (RtsFlags.ProfFlags.doHeapProfile != 0) {
958                         errorBelch("multiple heap profile options");
959                         error = rtsTrue;
960                         break;
961                     }
962
963                     switch (rts_argv[arg][2]) {
964                     case '\0':
965                     case 'C':
966                     case 'c':
967                         RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
968                         break;
969                     case 'M':
970                     case 'm':
971                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
972                           break;
973                     case 'D':
974                     case 'd':
975                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
976                           break;
977                     case 'Y':
978                     case 'y':
979                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
980                           break;
981                     case 'R':
982                     case 'r':
983                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
984                           break;
985                     case 'B':
986                     case 'b':
987                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
988                           break;
989                     }
990                     break;
991                       
992                 default:
993                     errorBelch("invalid heap profile option: %s",rts_argv[arg]);
994                     error = rtsTrue;
995                 }
996                 ) 
997 #endif /* PROFILING */
998                 break;
999
1000 #if defined(PROFILING) 
1001               case 'i': /* heap sample interval */
1002                 if (rts_argv[arg][2] == '\0') {
1003                   /* use default */
1004                 } else {
1005                     I_ cst; /* tmp */
1006
1007                     /* Convert to milliseconds */
1008                     cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1009                     cst = (cst / CS_MIN_MILLISECS) * CS_MIN_MILLISECS;
1010                     if (cst != 0 && cst < CS_MIN_MILLISECS)
1011                         cst = CS_MIN_MILLISECS;
1012
1013                     RtsFlags.ProfFlags.profileInterval = cst;
1014                 }
1015                 break;
1016 #endif
1017
1018               /* =========== CONCURRENT ========================= */
1019               case 'C': /* context switch interval */
1020                 if (rts_argv[arg][2] == '\0')
1021                     RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1022                 else {
1023                     I_ cst; /* tmp */
1024
1025                     /* Convert to milliseconds */
1026                     cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1027                     cst = (cst / CS_MIN_MILLISECS) * CS_MIN_MILLISECS;
1028                     if (cst != 0 && cst < CS_MIN_MILLISECS)
1029                         cst = CS_MIN_MILLISECS;
1030
1031                     RtsFlags.ConcFlags.ctxtSwitchTime = cst;
1032                 }
1033                 break;
1034
1035 #ifdef SMP
1036               case 'N':
1037                 SMP_BUILD_ONLY(
1038                 if (rts_argv[arg][2] != '\0') {
1039                     RtsFlags.ParFlags.nNodes
1040                       = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1041                     if (RtsFlags.ParFlags.nNodes <= 0) {
1042                       errorBelch("bad value for -N");
1043                       error = rtsTrue;
1044                     }
1045                 }
1046                 ) break;
1047 #endif
1048               /* =========== PARALLEL =========================== */
1049               case 'e':
1050                 PAR_OR_SMP_BUILD_ONLY(
1051                 if (rts_argv[arg][2] != '\0') {
1052                     RtsFlags.ParFlags.maxLocalSparks
1053                       = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1054                     if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1055                       errorBelch("bad value for -e");
1056                       error = rtsTrue;
1057                     }
1058                 }
1059                 ) break;
1060
1061               case 'q':
1062                 PAR_BUILD_ONLY(
1063                   process_par_option(arg, rts_argc, rts_argv, &error);
1064                 ) break;
1065
1066               /* =========== GRAN =============================== */
1067
1068               case 'b':
1069                 GRAN_BUILD_ONLY(
1070                   process_gran_option(arg, rts_argc, rts_argv, &error);
1071                 ) break;
1072
1073               /* =========== TICKY ============================== */
1074
1075               case 'r': /* Basic profiling stats */
1076                 TICKY_BUILD_ONLY(
1077
1078                 RtsFlags.TickyFlags.showTickyStats = rtsTrue;
1079
1080                 { 
1081                     int r;
1082                     r = open_stats_file(arg, *argc, argv,
1083                                         *rts_argc, rts_argv, TICKY_FILENAME_FMT,
1084                                         &RtsFlags.TickyFlags.tickyFile);
1085                     if (r == -1) { error = rtsTrue; }
1086                 }
1087                 ) break;
1088
1089               /* =========== EXTENDED OPTIONS =================== */
1090
1091               case 'x': /* Extend the argument space */
1092                 switch(rts_argv[arg][2]) {
1093                   case '\0':
1094                     errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1095                     error = rtsTrue;
1096                     break;
1097
1098                   case 'c': /* Debugging tool: show current cost centre on an exception */
1099                     PROFILING_BUILD_ONLY(
1100                         RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
1101                         );
1102                     break;
1103
1104                 case 't':  /* Include memory used by TSOs in a heap profile */
1105                     PROFILING_BUILD_ONLY(
1106                         RtsFlags.ProfFlags.includeTSOs = rtsTrue;
1107                         );
1108                     break;
1109
1110                   /* The option prefix '-xx' is reserved for future extension.  KSW 1999-11. */
1111
1112                   default:
1113                     errorBelch("unknown RTS option: %s",rts_argv[arg]);
1114                     error = rtsTrue;
1115                     break;
1116                 }
1117                 break;  /* defensive programming */
1118
1119               /* =========== OH DEAR ============================ */
1120               default:
1121                 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1122                 error = rtsTrue;
1123                 break;
1124             }
1125         }
1126     }
1127     if (error) {
1128         const char **p;
1129
1130         fflush(stdout);
1131         for (p = usage_text; *p; p++)
1132             errorBelch("%s", *p);
1133         stg_exit(EXIT_FAILURE);
1134     }
1135 }
1136
1137 #if defined(GRAN)
1138
1139 //@node GranSim specific options, Aux fcts, Command-line option parsing routines
1140 //@subsection GranSim specific options
1141
1142 static void
1143 enable_GranSimLight(void) {
1144
1145     debugBelch("GrAnSim Light enabled (infinite number of processors;  0 communication costs)\n");
1146     RtsFlags.GranFlags.Light=rtsTrue;
1147     RtsFlags.GranFlags.Costs.latency = 
1148         RtsFlags.GranFlags.Costs.fetchtime = 
1149         RtsFlags.GranFlags.Costs.additional_latency =
1150         RtsFlags.GranFlags.Costs.gunblocktime = 
1151         RtsFlags.GranFlags.Costs.lunblocktime =
1152         RtsFlags.GranFlags.Costs.threadcreatetime = 
1153         RtsFlags.GranFlags.Costs.threadqueuetime =
1154         RtsFlags.GranFlags.Costs.threadscheduletime = 
1155         RtsFlags.GranFlags.Costs.threaddescheduletime =
1156         RtsFlags.GranFlags.Costs.threadcontextswitchtime = 0;
1157   
1158     RtsFlags.GranFlags.Costs.mpacktime = 
1159         RtsFlags.GranFlags.Costs.munpacktime = 0;
1160
1161     RtsFlags.GranFlags.DoFairSchedule = rtsTrue;
1162     RtsFlags.GranFlags.DoAsyncFetch = rtsFalse;
1163     RtsFlags.GranFlags.DoAlwaysCreateThreads = rtsTrue;
1164     /* FetchStrategy is irrelevant in GrAnSim-Light */
1165
1166     /* GrAnSim Light often creates an abundance of parallel threads,
1167        each with its own stack etc. Therefore, it's in general a good
1168        idea to use small stack chunks (use the -o<size> option to 
1169        increase it again). 
1170     */
1171     // RtsFlags.ConcFlags.stkChunkSize = 100;
1172
1173     RtsFlags.GranFlags.proc = 1; 
1174 }
1175
1176 static void
1177 process_gran_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error)
1178 {
1179     if (rts_argv[arg][1] != 'b') /* All GranSim options start with -b */
1180       return;
1181
1182     /* or a ridiculously idealised simulator */
1183     if(strcmp((rts_argv[arg]+2),"oring")==0) {
1184       RtsFlags.GranFlags.Costs.latency = 
1185         RtsFlags.GranFlags.Costs.fetchtime = 
1186         RtsFlags.GranFlags.Costs.additional_latency =
1187         RtsFlags.GranFlags.Costs.gunblocktime = 
1188         RtsFlags.GranFlags.Costs.lunblocktime =
1189         RtsFlags.GranFlags.Costs.threadcreatetime = 
1190         RtsFlags.GranFlags.Costs.threadqueuetime =
1191         RtsFlags.GranFlags.Costs.threadscheduletime = 
1192         RtsFlags.GranFlags.Costs.threaddescheduletime =
1193         RtsFlags.GranFlags.Costs.threadcontextswitchtime = 0;
1194
1195       RtsFlags.GranFlags.Costs.mpacktime = 
1196         RtsFlags.GranFlags.Costs.munpacktime = 0;
1197
1198       RtsFlags.GranFlags.Costs.arith_cost = 
1199         RtsFlags.GranFlags.Costs.float_cost = 
1200         RtsFlags.GranFlags.Costs.load_cost =
1201         RtsFlags.GranFlags.Costs.store_cost = 
1202         RtsFlags.GranFlags.Costs.branch_cost = 0;
1203
1204       RtsFlags.GranFlags.Costs.heapalloc_cost = 1;
1205
1206       /* ++RtsFlags.GranFlags.DoFairSchedule; */
1207       RtsFlags.GranFlags.DoStealThreadsFirst = rtsTrue;        /* -bZ */
1208       RtsFlags.GranFlags.DoThreadMigration   = rtsTrue;        /* -bM */
1209       RtsFlags.GranFlags.GranSimStats.Full   = rtsTrue;        /* -bP */
1210       return;
1211     }
1212
1213       /* or a somewhat idealised simulator */
1214       if(strcmp((rts_argv[arg]+2),"onzo")==0) {
1215         RtsFlags.GranFlags.Costs.latency = 
1216         RtsFlags.GranFlags.Costs.fetchtime = 
1217         RtsFlags.GranFlags.Costs.additional_latency =
1218         RtsFlags.GranFlags.Costs.gunblocktime = 
1219         RtsFlags.GranFlags.Costs.lunblocktime =
1220         RtsFlags.GranFlags.Costs.threadcreatetime = 
1221         RtsFlags.GranFlags.Costs.threadqueuetime =
1222         RtsFlags.GranFlags.Costs.threadscheduletime = 
1223         RtsFlags.GranFlags.Costs.threaddescheduletime =
1224         RtsFlags.GranFlags.Costs.threadcontextswitchtime = 0;
1225
1226         RtsFlags.GranFlags.Costs.mpacktime = 
1227         RtsFlags.GranFlags.Costs.munpacktime = 0;
1228         
1229         RtsFlags.GranFlags.Costs.heapalloc_cost = 1;
1230
1231         /* RtsFlags.GranFlags.DoFairSchedule  = rtsTrue; */       /* -b-R */
1232         /* RtsFlags.GranFlags.DoStealThreadsFirst = rtsTrue; */   /* -b-T */
1233         RtsFlags.GranFlags.DoAsyncFetch = rtsTrue;         /* -bZ */
1234         RtsFlags.GranFlags.DoThreadMigration  = rtsTrue;          /* -bM */
1235         RtsFlags.GranFlags.GranSimStats.Full  = rtsTrue;          /* -bP */
1236 #  if defined(GRAN_CHECK) && defined(GRAN)
1237         RtsFlags.GranFlags.Debug.event_stats = rtsTrue; /* print event statistics   */
1238 #  endif
1239         return;
1240       }
1241
1242       /* Communication and task creation cost parameters */
1243       switch(rts_argv[arg][2]) {
1244         case '.':
1245           IgnoreYields = rtsTrue; // HWL HACK
1246           break;
1247
1248         case ':':
1249           enable_GranSimLight();       /* set flags for GrAnSim-Light mode */
1250           break;
1251
1252         case 'l':
1253           if (rts_argv[arg][3] != '\0')
1254             {
1255               RtsFlags.GranFlags.Costs.gunblocktime = 
1256               RtsFlags.GranFlags.Costs.latency = decode(rts_argv[arg]+3);
1257               RtsFlags.GranFlags.Costs.fetchtime = 2*RtsFlags.GranFlags.Costs.latency;
1258             }
1259           else
1260             RtsFlags.GranFlags.Costs.latency = LATENCY;
1261           break;
1262
1263         case 'a':
1264           if (rts_argv[arg][3] != '\0')
1265             RtsFlags.GranFlags.Costs.additional_latency = decode(rts_argv[arg]+3);
1266           else
1267             RtsFlags.GranFlags.Costs.additional_latency = ADDITIONAL_LATENCY;
1268           break;
1269
1270         case 'm':
1271           if (rts_argv[arg][3] != '\0')
1272             RtsFlags.GranFlags.Costs.mpacktime = decode(rts_argv[arg]+3);
1273           else
1274             RtsFlags.GranFlags.Costs.mpacktime = MSGPACKTIME;
1275           break;
1276
1277         case 'x':
1278           if (rts_argv[arg][3] != '\0')
1279             RtsFlags.GranFlags.Costs.mtidytime = decode(rts_argv[arg]+3);
1280           else
1281             RtsFlags.GranFlags.Costs.mtidytime = 0;
1282           break;
1283
1284         case 'r':
1285           if (rts_argv[arg][3] != '\0')
1286             RtsFlags.GranFlags.Costs.munpacktime = decode(rts_argv[arg]+3);
1287           else
1288             RtsFlags.GranFlags.Costs.munpacktime = MSGUNPACKTIME;
1289           break;
1290           
1291         case 'g':
1292           if (rts_argv[arg][3] != '\0')
1293             RtsFlags.GranFlags.Costs.fetchtime = decode(rts_argv[arg]+3);
1294           else
1295             RtsFlags.GranFlags.Costs.fetchtime = FETCHTIME;
1296           break;
1297           
1298         case 'n':
1299           if (rts_argv[arg][3] != '\0')
1300             RtsFlags.GranFlags.Costs.gunblocktime = decode(rts_argv[arg]+3);
1301           else
1302             RtsFlags.GranFlags.Costs.gunblocktime = GLOBALUNBLOCKTIME;
1303           break;
1304
1305         case 'u':
1306           if (rts_argv[arg][3] != '\0')
1307             RtsFlags.GranFlags.Costs.lunblocktime = decode(rts_argv[arg]+3);
1308           else
1309             RtsFlags.GranFlags.Costs.lunblocktime = LOCALUNBLOCKTIME;
1310           break;
1311
1312         /* Thread-related metrics */
1313         case 't':
1314           if (rts_argv[arg][3] != '\0')
1315             RtsFlags.GranFlags.Costs.threadcreatetime = decode(rts_argv[arg]+3);
1316           else
1317             RtsFlags.GranFlags.Costs.threadcreatetime = THREADCREATETIME;
1318           break;
1319           
1320         case 'q':
1321           if (rts_argv[arg][3] != '\0')
1322             RtsFlags.GranFlags.Costs.threadqueuetime = decode(rts_argv[arg]+3);
1323           else
1324             RtsFlags.GranFlags.Costs.threadqueuetime = THREADQUEUETIME;
1325           break;
1326           
1327         case 'c':
1328           if (rts_argv[arg][3] != '\0')
1329             RtsFlags.GranFlags.Costs.threadscheduletime = decode(rts_argv[arg]+3);
1330           else
1331             RtsFlags.GranFlags.Costs.threadscheduletime = THREADSCHEDULETIME;
1332           
1333           RtsFlags.GranFlags.Costs.threadcontextswitchtime = RtsFlags.GranFlags.Costs.threadscheduletime
1334             + RtsFlags.GranFlags.Costs.threaddescheduletime;
1335           break;
1336
1337         case 'd':
1338           if (rts_argv[arg][3] != '\0')
1339             RtsFlags.GranFlags.Costs.threaddescheduletime = decode(rts_argv[arg]+3);
1340           else
1341             RtsFlags.GranFlags.Costs.threaddescheduletime = THREADDESCHEDULETIME;
1342           
1343           RtsFlags.GranFlags.Costs.threadcontextswitchtime = RtsFlags.GranFlags.Costs.threadscheduletime
1344             + RtsFlags.GranFlags.Costs.threaddescheduletime;
1345           break;
1346
1347         /* Instruction Cost Metrics */
1348         case 'A':
1349           if (rts_argv[arg][3] != '\0')
1350             RtsFlags.GranFlags.Costs.arith_cost = decode(rts_argv[arg]+3);
1351           else
1352             RtsFlags.GranFlags.Costs.arith_cost = ARITH_COST;
1353           break;
1354
1355         case 'F':
1356           if (rts_argv[arg][3] != '\0')
1357             RtsFlags.GranFlags.Costs.float_cost = decode(rts_argv[arg]+3);
1358           else
1359             RtsFlags.GranFlags.Costs.float_cost = FLOAT_COST;
1360           break;
1361                       
1362         case 'B':
1363           if (rts_argv[arg][3] != '\0')
1364             RtsFlags.GranFlags.Costs.branch_cost = decode(rts_argv[arg]+3);
1365           else
1366             RtsFlags.GranFlags.Costs.branch_cost = BRANCH_COST;
1367           break;
1368
1369         case 'L':
1370           if (rts_argv[arg][3] != '\0')
1371             RtsFlags.GranFlags.Costs.load_cost = decode(rts_argv[arg]+3);
1372           else
1373             RtsFlags.GranFlags.Costs.load_cost = LOAD_COST;
1374           break;
1375           
1376         case 'S':
1377           if (rts_argv[arg][3] != '\0')
1378             RtsFlags.GranFlags.Costs.store_cost = decode(rts_argv[arg]+3);
1379           else
1380             RtsFlags.GranFlags.Costs.store_cost = STORE_COST;
1381           break;
1382
1383         case 'H':
1384           if (rts_argv[arg][3] != '\0')
1385             RtsFlags.GranFlags.Costs.heapalloc_cost = decode(rts_argv[arg]+3);
1386           else
1387             RtsFlags.GranFlags.Costs.heapalloc_cost = 0;
1388           break;
1389
1390         case 'y':
1391           RtsFlags.GranFlags.DoAsyncFetch = rtsTrue;
1392           if (rts_argv[arg][3] != '\0')
1393             RtsFlags.GranFlags.FetchStrategy = decode(rts_argv[arg]+3);
1394           else
1395             RtsFlags.GranFlags.FetchStrategy = 2;
1396           if (RtsFlags.GranFlags.FetchStrategy == 0)
1397             RtsFlags.GranFlags.DoAsyncFetch = rtsFalse;
1398           break;
1399           
1400         case 'K':   /* sort overhead (per elem in spark list) */
1401           if (rts_argv[arg][3] != '\0')
1402             RtsFlags.GranFlags.Costs.pri_spark_overhead = decode(rts_argv[arg]+3);
1403           else
1404             RtsFlags.GranFlags.Costs.pri_spark_overhead = PRI_SPARK_OVERHEAD;
1405           debugBelch("Overhead for pri spark: %d (per elem).\n",
1406                          RtsFlags.GranFlags.Costs.pri_spark_overhead);
1407           break;
1408
1409         case 'O':  /* sort overhead (per elem in spark list) */
1410           if (rts_argv[arg][3] != '\0')
1411             RtsFlags.GranFlags.Costs.pri_sched_overhead = decode(rts_argv[arg]+3);
1412           else
1413             RtsFlags.GranFlags.Costs.pri_sched_overhead = PRI_SCHED_OVERHEAD;
1414           debugBelch("Overhead for pri sched: %d (per elem).\n",
1415                        RtsFlags.GranFlags.Costs.pri_sched_overhead);
1416           break;
1417
1418         /* General Parameters */
1419         case 'p':
1420           if (rts_argv[arg][3] != '\0')
1421             {
1422               RtsFlags.GranFlags.proc = decode(rts_argv[arg]+3);
1423               if (RtsFlags.GranFlags.proc==0) {
1424                   enable_GranSimLight(); /* set flags for GrAnSim-Light mode */
1425               } else if (RtsFlags.GranFlags.proc > MAX_PROC || 
1426                          RtsFlags.GranFlags.proc < 1)
1427                 {
1428                   debugBelch("setupRtsFlags: no more than %u processors allowed\n",
1429                           MAX_PROC);
1430                   *error = rtsTrue;
1431                 }
1432             }
1433           else
1434             RtsFlags.GranFlags.proc = MAX_PROC;
1435           break;
1436
1437         case 'f':
1438           RtsFlags.GranFlags.Fishing = rtsTrue;
1439           if (rts_argv[arg][3] != '\0')
1440             RtsFlags.GranFlags.maxFishes = decode(rts_argv[arg]+3);
1441           else
1442             RtsFlags.GranFlags.maxFishes = MAX_FISHES;
1443           break;
1444           
1445         case 'w':
1446           if (rts_argv[arg][3] != '\0')
1447             RtsFlags.GranFlags.time_slice = decode(rts_argv[arg]+3);
1448           else
1449             RtsFlags.GranFlags.time_slice = GRAN_TIME_SLICE;
1450           break;
1451           
1452         case 'C':
1453           RtsFlags.GranFlags.DoAlwaysCreateThreads=rtsTrue;
1454           RtsFlags.GranFlags.DoThreadMigration=rtsTrue;
1455           break;
1456
1457         case 'G':
1458           debugBelch("Bulk fetching enabled.\n");
1459           RtsFlags.GranFlags.DoBulkFetching=rtsTrue;
1460           break;
1461           
1462         case 'M':
1463           debugBelch("Thread migration enabled.\n");
1464           RtsFlags.GranFlags.DoThreadMigration=rtsTrue;
1465           break;
1466
1467         case 'R':
1468           debugBelch("Fair Scheduling enabled.\n");
1469           RtsFlags.GranFlags.DoFairSchedule=rtsTrue;
1470           break;
1471           
1472         case 'I':
1473           debugBelch("Priority Scheduling enabled.\n");
1474           RtsFlags.GranFlags.DoPriorityScheduling=rtsTrue;
1475           break;
1476
1477         case 'T':
1478           RtsFlags.GranFlags.DoStealThreadsFirst=rtsTrue;
1479           RtsFlags.GranFlags.DoThreadMigration=rtsTrue;
1480           break;
1481           
1482         case 'Z':
1483           RtsFlags.GranFlags.DoAsyncFetch=rtsTrue;
1484           break;
1485           
1486 /*          case 'z': */
1487 /*        RtsFlags.GranFlags.SimplifiedFetch=rtsTrue; */
1488 /*        break; */
1489           
1490         case 'N':
1491           RtsFlags.GranFlags.PreferSparksOfLocalNodes=rtsTrue;
1492           break;
1493           
1494         case 'b':
1495           RtsFlags.GranFlags.GranSimStats.Binary=rtsTrue;
1496           break;
1497           
1498         case 'P':
1499           /* format is -bP<c> where <c> is one char describing kind of profile */
1500           RtsFlags.GranFlags.GranSimStats.Full = rtsTrue;
1501           switch(rts_argv[arg][3]) {
1502           case '\0': break; // nothing special, just an ordinary profile
1503           case '0': RtsFlags.GranFlags.GranSimStats.Suppressed = rtsTrue;
1504             break;
1505           case 'b': RtsFlags.GranFlags.GranSimStats.Binary = rtsTrue;
1506             break;
1507           case 's': RtsFlags.GranFlags.GranSimStats.Sparks = rtsTrue;
1508             break;
1509           case 'h': RtsFlags.GranFlags.GranSimStats.Heap = rtsTrue;
1510             break;
1511           case 'n': RtsFlags.GranFlags.GranSimStats.NewLogfile = rtsTrue;
1512             break;
1513           case 'g': RtsFlags.GranFlags.GranSimStats.Global = rtsTrue;
1514             break;
1515           default: barf("Unknown option -bP%c", rts_argv[arg][3]);
1516           }
1517           break;
1518
1519         case 's':
1520           RtsFlags.GranFlags.GranSimStats.Sparks=rtsTrue;
1521           break;
1522
1523         case 'h':
1524           RtsFlags.GranFlags.GranSimStats.Heap=rtsTrue;
1525           break;
1526
1527         case 'Y':   /* syntax: -bY<n>[,<n>]  n ... pos int */ 
1528           if (rts_argv[arg][3] != '\0') {
1529             char *arg0, *tmp;
1530             
1531             arg0 = rts_argv[arg]+3;
1532             if ((tmp = strstr(arg0,","))==NULL) {
1533               RtsFlags.GranFlags.SparkPriority = decode(arg0);
1534               debugBelch("SparkPriority: %u.\n",RtsFlags.GranFlags.SparkPriority);
1535             } else {
1536               *(tmp++) = '\0'; 
1537               RtsFlags.GranFlags.SparkPriority = decode(arg0);
1538               RtsFlags.GranFlags.SparkPriority2 = decode(tmp);
1539               debugBelch("SparkPriority: %u.\n",
1540                       RtsFlags.GranFlags.SparkPriority);
1541               debugBelch("SparkPriority2:%u.\n",
1542                       RtsFlags.GranFlags.SparkPriority2);
1543               if (RtsFlags.GranFlags.SparkPriority2 < 
1544                   RtsFlags.GranFlags.SparkPriority) {
1545                 debugBelch("WARNING: 2nd pri < main pri (%u<%u); 2nd pri has no effect\n",
1546                         RtsFlags.GranFlags.SparkPriority2,
1547                         RtsFlags.GranFlags.SparkPriority);
1548               }
1549             }
1550           } else {
1551             /* plain pri spark is now invoked with -bX  
1552                RtsFlags.GranFlags.DoPrioritySparking = 1;
1553                debugBelch("PrioritySparking.\n");
1554             */
1555           }
1556           break;
1557
1558         case 'Q':
1559           if (rts_argv[arg][3] != '\0') {
1560             RtsFlags.GranFlags.ThunksToPack = decode(rts_argv[arg]+3);
1561           } else {
1562             RtsFlags.GranFlags.ThunksToPack = 1;
1563           }
1564           debugBelch("Thunks To Pack in one packet: %u.\n",
1565                   RtsFlags.GranFlags.ThunksToPack);
1566           break;
1567                       
1568         case 'e':
1569           RtsFlags.GranFlags.RandomSteal = rtsFalse;
1570           debugBelch("Deterministic mode (no random stealing)\n");
1571                       break;
1572
1573           /* The following class of options contains eXperimental */
1574           /* features in connection with exploiting granularity */
1575           /* information. I.e. if -bY is chosen these options */
1576           /* tell the RTS what to do with the supplied info --HWL */
1577
1578         case 'W':
1579           if (rts_argv[arg][3] != '\0') {
1580             RtsFlags.GranFlags.packBufferSize_internal = decode(rts_argv[arg]+3);
1581           } else {
1582             RtsFlags.GranFlags.packBufferSize_internal = GRANSIM_DEFAULT_PACK_BUFFER_SIZE;
1583           }
1584           debugBelch("Size of GranSim internal pack buffer: %u.\n",
1585                   RtsFlags.GranFlags.packBufferSize_internal);
1586           break;
1587                       
1588         case 'X':
1589           switch(rts_argv[arg][3]) {
1590             
1591             case '\0':
1592               RtsFlags.GranFlags.DoPrioritySparking = 1;
1593               debugBelch("Priority Sparking with Normal Priorities.\n");
1594               RtsFlags.GranFlags.InversePriorities = rtsFalse; 
1595               RtsFlags.GranFlags.RandomPriorities = rtsFalse;
1596               RtsFlags.GranFlags.IgnorePriorities = rtsFalse;
1597               break;
1598                         
1599             case 'I':
1600               RtsFlags.GranFlags.DoPrioritySparking = 1;
1601               debugBelch("Priority Sparking with Inverse Priorities.\n");
1602               RtsFlags.GranFlags.InversePriorities++; 
1603               break;
1604               
1605             case 'R': 
1606               RtsFlags.GranFlags.DoPrioritySparking = 1;
1607               debugBelch("Priority Sparking with Random Priorities.\n");
1608               RtsFlags.GranFlags.RandomPriorities++;
1609               break;
1610               
1611             case 'N':
1612               RtsFlags.GranFlags.DoPrioritySparking = 1;
1613               debugBelch("Priority Sparking with No Priorities.\n");
1614               RtsFlags.GranFlags.IgnorePriorities++;
1615               break;
1616               
1617             default:
1618               bad_option( rts_argv[arg] );
1619               break;
1620           }
1621           break;
1622
1623         case '-':
1624           switch(rts_argv[arg][3]) {
1625             
1626             case 'C':
1627               RtsFlags.GranFlags.DoAlwaysCreateThreads=rtsFalse;
1628               RtsFlags.GranFlags.DoThreadMigration=rtsFalse;
1629               break;
1630
1631             case 'G':
1632               RtsFlags.GranFlags.DoBulkFetching=rtsFalse;
1633               break;
1634               
1635             case 'M':
1636               RtsFlags.GranFlags.DoThreadMigration=rtsFalse;
1637               break;
1638
1639             case 'R':
1640               RtsFlags.GranFlags.DoFairSchedule=rtsFalse;
1641               break;
1642
1643             case 'T':
1644               RtsFlags.GranFlags.DoStealThreadsFirst=rtsFalse;
1645               RtsFlags.GranFlags.DoThreadMigration=rtsFalse;
1646               break;
1647
1648             case 'Z':
1649               RtsFlags.GranFlags.DoAsyncFetch=rtsFalse;
1650               break;
1651               
1652             case 'N':
1653               RtsFlags.GranFlags.PreferSparksOfLocalNodes=rtsFalse;
1654                          break;
1655                          
1656             case 'P':
1657               RtsFlags.GranFlags.GranSimStats.Suppressed=rtsTrue;
1658               break;
1659
1660             case 's':
1661               RtsFlags.GranFlags.GranSimStats.Sparks=rtsFalse;
1662               break;
1663             
1664             case 'h':
1665               RtsFlags.GranFlags.GranSimStats.Heap=rtsFalse;
1666               break;
1667             
1668             case 'b':
1669               RtsFlags.GranFlags.GranSimStats.Binary=rtsFalse;
1670               break;
1671                          
1672             case 'X':
1673               RtsFlags.GranFlags.DoPrioritySparking = rtsFalse;
1674               break;
1675
1676             case 'Y':
1677               RtsFlags.GranFlags.DoPrioritySparking = rtsFalse;
1678               RtsFlags.GranFlags.SparkPriority = rtsFalse;
1679               break;
1680
1681             case 'I':
1682               RtsFlags.GranFlags.DoPriorityScheduling = rtsFalse;
1683               break;
1684
1685             case 'e':
1686               RtsFlags.GranFlags.RandomSteal = rtsFalse;
1687               break;
1688
1689             default:
1690               bad_option( rts_argv[arg] );
1691               break;
1692           }
1693           break;
1694
1695 #  if defined(GRAN_CHECK) && defined(GRAN)
1696         case 'D':
1697           switch(rts_argv[arg][3]) {
1698             case 'Q':    /* Set pack buffer size (same as 'Q' in GUM) */
1699               if (rts_argv[arg][4] != '\0') {
1700                 RtsFlags.GranFlags.packBufferSize = decode(rts_argv[arg]+4);
1701                 debugBelch("Pack buffer size: %d\n",
1702                         RtsFlags.GranFlags.packBufferSize);
1703               } else {
1704                 debugBelch("setupRtsFlags: missing size of PackBuffer (for -Q)\n");
1705                 *error = rtsTrue;
1706               }
1707               break;
1708
1709           default:
1710               if (isdigit(rts_argv[arg][3])) {/* Set all debugging options in one */
1711                 /* hack warning: interpret the flags as a binary number */
1712                 nat n = decode(rts_argv[arg]+3);
1713                 set_GranSim_debug_options(n);
1714               } else {
1715                 nat i;
1716                 for (i=0; i<=MAX_GRAN_DEBUG_OPTION; i++) 
1717                   if (rts_argv[arg][3] == gran_debug_opts_flags[i])
1718                     break;
1719                 
1720                 if (i==MAX_GRAN_DEBUG_OPTION+1) {
1721                   debugBelch("Valid GranSim debug options are:\n");
1722                   help_GranSim_debug_options(MAX_GRAN_DEBUG_MASK);
1723                   bad_option( rts_argv[arg] );
1724                 } else { // flag found; now set it
1725                   set_GranSim_debug_options(GRAN_DEBUG_MASK(i));  // 2^i
1726                 }
1727               }
1728               break;
1729               
1730 #if 0
1731             case 'e':       /* event trace; also -bD1 */
1732               debugBelch("DEBUG: event_trace; printing event trace.\n");
1733               RtsFlags.GranFlags.Debug.event_trace = rtsTrue;
1734               /* RtsFlags.GranFlags.event_trace=rtsTrue; */
1735               break;
1736               
1737             case 'E':       /* event statistics; also -bD2 */
1738               debugBelch("DEBUG: event_stats; printing event statistics.\n");
1739               RtsFlags.GranFlags.Debug.event_stats = rtsTrue;
1740               /* RtsFlags.GranFlags.Debug |= 0x20; print event statistics   */
1741               break;
1742               
1743             case 'f':       /* thunkStealing; also -bD4 */
1744               debugBelch("DEBUG: thunkStealing; printing forwarding of FETCHNODES.\n");
1745               RtsFlags.GranFlags.Debug.thunkStealing = rtsTrue;
1746               /* RtsFlags.GranFlags.Debug |= 0x2;  print fwd messages */
1747               break;
1748
1749             case 'z':       /* blockOnFetch; also -bD8 */
1750               debugBelch("DEBUG: blockOnFetch; check for blocked on fetch.\n");
1751               RtsFlags.GranFlags.Debug.blockOnFetch = rtsTrue;
1752               /* RtsFlags.GranFlags.Debug |= 0x4; debug non-reschedule-on-fetch */
1753               break;
1754               
1755             case 't':       /* blockOnFetch_sanity; also -bD16 */  
1756               debugBelch("DEBUG: blockOnFetch_sanity; check for TSO asleep on fetch.\n");
1757               RtsFlags.GranFlags.Debug.blockOnFetch_sanity = rtsTrue;
1758               /* RtsFlags.GranFlags.Debug |= 0x10; debug TSO asleep for fetch  */
1759               break;
1760
1761             case 'S':       /* priSpark; also -bD32 */
1762               debugBelch("DEBUG: priSpark; priority sparking.\n");
1763               RtsFlags.GranFlags.Debug.priSpark = rtsTrue;
1764               break;
1765
1766             case 's':       /* priSched; also -bD64 */
1767               debugBelch("DEBUG: priSched; priority scheduling.\n");
1768               RtsFlags.GranFlags.Debug.priSched = rtsTrue;
1769               break;
1770
1771             case 'F':       /* findWork; also -bD128 */
1772               debugBelch("DEBUG: findWork; searching spark-pools (local & remote), thread queues for work.\n");
1773               RtsFlags.GranFlags.Debug.findWork = rtsTrue;
1774               break;
1775               
1776             case 'g':       /* globalBlock; also -bD256 */
1777               debugBelch("DEBUG: globalBlock; blocking on remote closures (FETCHMEs etc in GUM).\n");
1778               RtsFlags.GranFlags.Debug.globalBlock = rtsTrue;
1779               break;
1780               
1781             case 'G':       /* pack; also -bD512 */
1782               debugBelch("DEBUG: pack; routines for (un-)packing graph structures.\n");
1783               RtsFlags.GranFlags.Debug.pack = rtsTrue;
1784               break;
1785               
1786             case 'P':       /* packBuffer; also -bD1024 */
1787               debugBelch("DEBUG: packBuffer; routines handling pack buffer (GranSim internal!).\n");
1788               RtsFlags.GranFlags.Debug.packBuffer = rtsTrue;
1789               break;
1790               
1791             case 'o':       /* sortedQ; also -bD2048 */
1792               debugBelch("DEBUG: sortedQ; check whether spark/thread queues are sorted.\n");
1793               RtsFlags.GranFlags.Debug.sortedQ = rtsTrue;
1794               break;
1795               
1796             case 'r':       /* randomSteal; also -bD4096 */
1797               debugBelch("DEBUG: randomSteal; stealing sparks/threads from random PEs.\n");
1798               RtsFlags.GranFlags.Debug.randomSteal = rtsTrue;
1799               break;
1800               
1801             case 'q':       /* checkSparkQ; also -bD8192 */
1802               debugBelch("DEBUG: checkSparkQ; check consistency of the spark queues.\n");
1803               RtsFlags.GranFlags.Debug.checkSparkQ = rtsTrue;
1804               break;
1805               
1806             case ':':       /* checkLight; also -bD16384 */
1807               debugBelch("DEBUG: checkLight; check GranSim-Light setup.\n");
1808               RtsFlags.GranFlags.Debug.checkLight = rtsTrue;
1809               break;
1810               
1811             case 'b':       /* bq; also -bD32768 */
1812               debugBelch("DEBUG: bq; check blocking queues\n");
1813               RtsFlags.GranFlags.Debug.bq = rtsTrue;
1814               break;
1815               
1816             case 'd':       /* all options turned on */
1817               debugBelch("DEBUG: all options turned on.\n");
1818               set_GranSim_debug_options(MAX_GRAN_DEBUG_MASK);
1819               /* RtsFlags.GranFlags.Debug |= 0x40; */
1820               break;
1821
1822 /*          case '\0': */
1823 /*            RtsFlags.GranFlags.Debug = 1; */
1824 /*            break; */
1825 #endif
1826
1827           }
1828           break;
1829 #  endif  /* GRAN_CHECK */
1830       default:
1831         bad_option( rts_argv[arg] );
1832         break;
1833       }
1834 }
1835
1836 /*
1837   Interpret n as a binary number masking GranSim debug options and set the 
1838   correxponding option. See gran_debug_opts_strs for explanations of the flags.
1839 */
1840 static void
1841 set_GranSim_debug_options(nat n) {
1842   nat i;
1843
1844   for (i=0; i<=MAX_GRAN_DEBUG_OPTION; i++) 
1845     if ((n>>i)&1) {
1846       errorBelch(gran_debug_opts_strs[i]);
1847       switch (i) {
1848         case 0: RtsFlags.GranFlags.Debug.event_trace   = rtsTrue;  break;
1849         case 1: RtsFlags.GranFlags.Debug.event_stats   = rtsTrue;  break;
1850         case 2: RtsFlags.GranFlags.Debug.bq            = rtsTrue;  break;
1851         case 3: RtsFlags.GranFlags.Debug.pack          = rtsTrue;  break;
1852         case 4: RtsFlags.GranFlags.Debug.checkSparkQ   = rtsTrue;  break;
1853         case 5: RtsFlags.GranFlags.Debug.thunkStealing = rtsTrue;  break;
1854         case 6: RtsFlags.GranFlags.Debug.randomSteal   = rtsTrue;  break;
1855         case 7: RtsFlags.GranFlags.Debug.findWork      = rtsTrue;  break;
1856         case 8: RtsFlags.GranFlags.Debug.unused        = rtsTrue;  break;
1857         case 9: RtsFlags.GranFlags.Debug.pri           = rtsTrue;  break;
1858         case 10: RtsFlags.GranFlags.Debug.checkLight   = rtsTrue;  break;
1859         case 11: RtsFlags.GranFlags.Debug.sortedQ      = rtsTrue;  break;
1860         case 12: RtsFlags.GranFlags.Debug.blockOnFetch = rtsTrue;  break;
1861         case 13: RtsFlags.GranFlags.Debug.packBuffer   = rtsTrue;  break;
1862         case 14: RtsFlags.GranFlags.Debug.blockOnFetch_sanity = rtsTrue;  break;
1863         default: barf("set_GranSim_debug_options: only %d debug options expected");
1864       } /* switch */
1865     } /* if */
1866 }
1867
1868 /*
1869   Print one line explanation for each of the GranSim debug options specified
1870   in the bitmask n.
1871 */
1872 static void
1873 help_GranSim_debug_options(nat n) {
1874   nat i;
1875
1876   for (i=0; i<=MAX_GRAN_DEBUG_OPTION; i++) 
1877     if ((n>>i)&1) 
1878       debugBelch(gran_debug_opts_strs[i]);
1879 }
1880
1881 # elif defined(PAR)
1882
1883 static void
1884 process_par_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error)
1885 {
1886
1887   if (rts_argv[arg][1] != 'q') { /* All GUM options start with -q */
1888     errorBelch("Warning: GUM option does not start with -q: %s", rts_argv[arg]);
1889     return;
1890   }
1891
1892   /* Communication and task creation cost parameters */
1893   switch(rts_argv[arg][2]) {
1894   case 'e':  /* -qe<n>  ... allow <n> local sparks */
1895     if (rts_argv[arg][3] != '\0') { /* otherwise, stick w/ the default */
1896       RtsFlags.ParFlags.maxLocalSparks
1897         = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1898       
1899       if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1900         errorBelch("setupRtsFlags: bad value for -e\n");
1901         *error = rtsTrue;
1902       }
1903     }
1904     IF_PAR_DEBUG(verbose,
1905                  errorBelch("-qe<n>: max %d local sparks", 
1906                        RtsFlags.ParFlags.maxLocalSparks));
1907     break;
1908   
1909   case 't':
1910     if (rts_argv[arg][3] != '\0') {
1911       RtsFlags.ParFlags.maxThreads
1912         = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1913     } else {
1914       errorBelch("missing size for -qt\n");
1915       *error = rtsTrue;
1916     }
1917     IF_PAR_DEBUG(verbose,
1918                  errorBelch("-qt<n>: max %d threads", 
1919                        RtsFlags.ParFlags.maxThreads));
1920     break;
1921
1922   case 'f':
1923     if (rts_argv[arg][3] != '\0')
1924       RtsFlags.ParFlags.maxFishes = decode(rts_argv[arg]+3);
1925     else
1926       RtsFlags.ParFlags.maxFishes = MAX_FISHES;
1927     break;
1928     IF_PAR_DEBUG(verbose,
1929                  errorBelch("-qf<n>: max %d fishes sent out at one time", 
1930                        RtsFlags.ParFlags.maxFishes));
1931     break;
1932   
1933   case 'F':
1934     if (rts_argv[arg][3] != '\0') {
1935       RtsFlags.ParFlags.fishDelay
1936         = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1937     } else {
1938       errorBelch("missing fish delay time for -qF\n");
1939       *error = rtsTrue;
1940     }
1941     IF_PAR_DEBUG(verbose,
1942                  errorBelch("-qF<n>: fish delay time %d us", 
1943                        RtsFlags.ParFlags.fishDelay));
1944     break;
1945
1946   case 'O':
1947     RtsFlags.ParFlags.outputDisabled = rtsTrue;
1948     IF_PAR_DEBUG(verbose,
1949                  errorBelch("-qO: output disabled"));
1950     break;
1951   
1952   case 'g': /* -qg<n> ... globalisation scheme */
1953     if (rts_argv[arg][3] != '\0') {
1954       RtsFlags.ParFlags.globalising = decode(rts_argv[arg]+3);
1955     } else {
1956       errorBelch("missing identifier for globalisation scheme (for -qg)\n");
1957       *error = rtsTrue;
1958     }
1959     IF_PAR_DEBUG(verbose,
1960                  debugBelch("-qg<n>: globalisation scheme set to  %d", 
1961                        RtsFlags.ParFlags.globalising));
1962     break;
1963
1964   case 'h': /* -qh<n> ... max number of thunks (except root) in packet */
1965     if (rts_argv[arg][3] != '\0') {
1966       RtsFlags.ParFlags.thunksToPack = decode(rts_argv[arg]+3);
1967     } else {
1968       errorBelch("missing number of thunks per packet (for -qh)\n");
1969       *error = rtsTrue;
1970     }
1971     IF_PAR_DEBUG(verbose,
1972                  debugBelch("-qh<n>: thunks per packet set to %d", 
1973                        RtsFlags.ParFlags.thunksToPack));
1974     break;
1975
1976   case 'P': /* -qP for writing a log file */
1977     //RtsFlags.ParFlags.ParStats.Full = rtsFalse;
1978     /* same encoding as in GranSim after -bP */ 
1979     switch(rts_argv[arg][3]) {
1980     case '\0': RtsFlags.ParFlags.ParStats.Full = rtsTrue;
1981       break; // nothing special, just an ordinary profile
1982     case '0': RtsFlags.ParFlags.ParStats.Suppressed = rtsTrue;
1983         RtsFlags.ParFlags.ParStats.Full = rtsFalse;
1984       break;
1985     case 'b': RtsFlags.ParFlags.ParStats.Binary = rtsTrue;
1986       break;
1987     case 's': RtsFlags.ParFlags.ParStats.Sparks = rtsTrue;
1988       break;
1989       //case 'h': RtsFlags.parFlags.ParStats.Heap = rtsTrue;
1990       //  break;
1991     case 'n': RtsFlags.ParFlags.ParStats.NewLogfile = rtsTrue;
1992       break;
1993     case 'g': 
1994 # if defined(PAR_TICKY)
1995       RtsFlags.ParFlags.ParStats.Global = rtsTrue;
1996 # else 
1997       errorBelch("-qPg is only possible for a PAR_TICKY RTS, which this is not");
1998       stg_exit(EXIT_FAILURE);
1999 # endif
2000       break;
2001     default: barf("Unknown option -qP%c", rts_argv[arg][2]);
2002     }
2003     IF_PAR_DEBUG(verbose,
2004                  debugBelch("(-qP) writing to log-file (RtsFlags.ParFlags.ParStats.Full=%s)",
2005                        (RtsFlags.ParFlags.ParStats.Full ? "rtsTrue" : "rtsFalse")));
2006     break;
2007   
2008   case 'Q': /* -qQ<n> ... set pack buffer size to <n> */
2009     if (rts_argv[arg][3] != '\0') {
2010       RtsFlags.ParFlags.packBufferSize = decode(rts_argv[arg]+3);
2011     } else {
2012       errorBelch("missing size of PackBuffer (for -qQ)\n");
2013       *error = rtsTrue;
2014     }
2015     IF_PAR_DEBUG(verbose,
2016                  debugBelch("-qQ<n>: pack buffer size set to %d", 
2017                        RtsFlags.ParFlags.packBufferSize));
2018     break;
2019
2020   case 'R':
2021     RtsFlags.ParFlags.doFairScheduling = rtsTrue;
2022     IF_PAR_DEBUG(verbose,
2023                  debugBelch("-qR: fair-ish scheduling"));
2024     break;
2025   
2026 # if defined(DEBUG)  
2027   case 'w':
2028     if (rts_argv[arg][3] != '\0') {
2029       RtsFlags.ParFlags.wait
2030         = strtol(rts_argv[arg]+3, (char **) NULL, 10);
2031     } else {
2032       RtsFlags.ParFlags.wait = 1000;
2033     }
2034     IF_PAR_DEBUG(verbose,
2035                  debugBelch("-qw<n>: length of wait loop after synchr before reduction: %d", 
2036                        RtsFlags.ParFlags.wait));
2037     break;
2038
2039   case 'D':  /* -qD ... all the debugging options */
2040     if (isdigit(rts_argv[arg][3])) {/* Set all debugging options in one */
2041       /* hack warning: interpret the flags as a binary number */
2042       nat n = decode(rts_argv[arg]+3);
2043       set_par_debug_options(n);
2044     } else {
2045       nat i;
2046       for (i=0; i<=MAX_PAR_DEBUG_OPTION; i++) 
2047         if (rts_argv[arg][3] == par_debug_opts_flags[i])
2048           break;
2049         
2050       if (i==MAX_PAR_DEBUG_OPTION+1) {
2051         errorBelch("Valid GUM debug options are:\n");
2052         help_par_debug_options(MAX_PAR_DEBUG_MASK);
2053         bad_option( rts_argv[arg] );
2054       } else { // flag found; now set it
2055         set_par_debug_options(PAR_DEBUG_MASK(i));  // 2^i
2056       }
2057     }
2058     break;
2059 # endif
2060   default:
2061     errorBelch("Unknown option -q%c (%d opts in total)", 
2062           rts_argv[arg][2], *rts_argc);
2063     break;
2064   } /* switch */
2065 }
2066
2067 /*
2068   Interpret n as a binary number masking Par debug options and set the 
2069   correxponding option. See par_debug_opts_strs for explanations of the flags.
2070 */
2071 static void
2072 set_par_debug_options(nat n) {
2073   nat i;
2074
2075   for (i=0; i<=MAX_PAR_DEBUG_OPTION; i++) 
2076     if ((n>>i)&1) {
2077       debugBelch(par_debug_opts_strs[i]);
2078       switch (i) {
2079         case 0: RtsFlags.ParFlags.Debug.verbose       = rtsTrue;  break;
2080         case 1: RtsFlags.ParFlags.Debug.bq            = rtsTrue;  break;
2081         case 2: RtsFlags.ParFlags.Debug.schedule      = rtsTrue;  break;
2082         case 3: RtsFlags.ParFlags.Debug.free          = rtsTrue;  break;
2083         case 4: RtsFlags.ParFlags.Debug.resume        = rtsTrue;  break;
2084         case 5: RtsFlags.ParFlags.Debug.weight        = rtsTrue;  break;
2085         case 6: RtsFlags.ParFlags.Debug.fetch         = rtsTrue;  break;
2086           //case 7: RtsFlags.ParFlags.Debug.ack           = rtsTrue;  break;
2087         case 7: RtsFlags.ParFlags.Debug.fish          = rtsTrue;  break;
2088         case 8: RtsFlags.ParFlags.Debug.tables        = rtsTrue;  break;
2089         case 9: RtsFlags.ParFlags.Debug.packet        = rtsTrue;  break;
2090         case 10: RtsFlags.ParFlags.Debug.pack         = rtsTrue;  break;
2091         case 11: RtsFlags.ParFlags.Debug.paranoia     = rtsTrue;  break;
2092         default: barf("set_par_debug_options: only %d debug options expected",
2093                       MAX_PAR_DEBUG_OPTION);
2094       } /* switch */
2095     } /* if */
2096 }
2097
2098 /*
2099   Print one line explanation for each of the GranSim debug options specified
2100   in the bitmask n.
2101 */
2102 static void
2103 help_par_debug_options(nat n) {
2104   nat i;
2105
2106   for (i=0; i<=MAX_PAR_DEBUG_OPTION; i++) 
2107     if ((n>>i)&1) 
2108       debugBelch(par_debug_opts_strs[i]);
2109 }
2110
2111 #endif /* PAR */
2112
2113 //@node Aux fcts,  , GranSim specific options
2114 //@subsection Aux fcts
2115
2116 static void
2117 stats_fprintf(FILE *f, char *s, ...)
2118 {
2119     va_list ap;
2120     va_start(ap,s);
2121     if (f == NULL) {
2122         vdebugBelch(s, ap);
2123     } else {
2124         vfprintf(f, s, ap);
2125     }
2126     va_end(ap);
2127 }
2128
2129 static int              /* return -1 on error */
2130 open_stats_file (
2131     I_ arg,
2132     int argc, char *argv[],
2133     int rts_argc, char *rts_argv[],
2134     const char *FILENAME_FMT,
2135     FILE **file_ret)
2136 {
2137     FILE *f = NULL;
2138
2139     if (strequal(rts_argv[arg]+2, "stderr")) { /* use debugBelch */
2140         f = NULL; /* NULL means use debugBelch */
2141     } else {
2142         if (rts_argv[arg][2] != '\0') {  /* stats file specified */
2143             f = fopen(rts_argv[arg]+2,"w");
2144         } else {
2145             char stats_filename[STATS_FILENAME_MAXLEN]; /* default <program>.<ext> */
2146             sprintf(stats_filename, FILENAME_FMT, argv[0]);
2147             f = fopen(stats_filename,"w");
2148         }
2149         if (f == NULL) {
2150             errorBelch("Can't open stats file %s\n", rts_argv[arg]+2);
2151             return -1;
2152         }
2153     }
2154     *file_ret = f;
2155
2156     {
2157         /* Write argv and rtsv into start of stats file */
2158         int count;
2159         for(count = 0; count < argc; count++) {
2160             stats_fprintf(f, "%s ", argv[count]);
2161         }
2162         stats_fprintf(f, "+RTS ");
2163         for(count = 0; count < rts_argc; count++)
2164             stats_fprintf(f, "%s ", rts_argv[count]);
2165         stats_fprintf(f, "\n");
2166     }
2167     return 0;
2168 }
2169
2170
2171
2172 static I_
2173 decode(const char *s)
2174 {
2175     I_ c;
2176     StgDouble m;
2177
2178     if (!*s)
2179         return 0;
2180
2181     m = atof(s);
2182     c = s[strlen(s)-1];
2183
2184     if (c == 'g' || c == 'G')
2185         m *= 1000*1000*1000;    /* UNchecked! */
2186     else if (c == 'm' || c == 'M')
2187         m *= 1000*1000;                 /* We do not use powers of 2 (1024) */
2188     else if (c == 'k' || c == 'K')      /* to avoid possible bad effects on */
2189         m *= 1000;                      /* a direct-mapped cache.           */ 
2190     else if (c == 'w' || c == 'W')
2191         m *= sizeof(W_);
2192
2193     return (I_)m;
2194 }
2195
2196 static void
2197 bad_option(const char *s)
2198 {
2199   errorBelch("bad RTS option: %s", s);
2200   stg_exit(EXIT_FAILURE);
2201 }
2202
2203 /* -----------------------------------------------------------------------------
2204    Getting/Setting the program's arguments.
2205
2206    These are used by System.Environment, and parts of the RTS.
2207    -------------------------------------------------------------------------- */
2208
2209 void
2210 setProgName(char *argv[])
2211 {
2212     /* Remove directory from argv[0] -- default files in current directory */
2213 #if !defined(mingw32_HOST_OS)
2214     char *last_slash;
2215     if ( (last_slash = (char *) strrchr(argv[0], '/')) != NULL ) {
2216         prog_name = last_slash+1;
2217    } else {
2218         prog_name = argv[0];
2219    }
2220 #else
2221     char* last_slash = argv[0] + (strlen(argv[0]) - 1);
2222     while ( last_slash > argv[0] ) {
2223         if ( *last_slash == '/' || *last_slash == '\\' ) {
2224             prog_name = last_slash+1;
2225             return;
2226         }
2227         last_slash--;
2228     }
2229     prog_name = argv[0];
2230 #endif
2231 }
2232
2233 void
2234 getProgArgv(int *argc, char **argv[])
2235 {
2236     if (argc) { *argc = prog_argc; }
2237     if (argv) { *argv = prog_argv; }
2238 }
2239
2240 void
2241 setProgArgv(int argc, char *argv[])
2242 {
2243    /* Usually this is done by startupHaskell, so we don't need to call this. 
2244       However, sometimes Hugs wants to change the arguments which Haskell
2245       getArgs >>= ... will be fed.  So you can do that by calling here
2246       _after_ calling startupHaskell.
2247    */
2248    prog_argc = argc;
2249    prog_argv = argv;
2250    setProgName(prog_argv);
2251 }