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