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