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