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