RtsFlags.c 67 KB
Newer Older
1

2
/* -----------------------------------------------------------------------------
3
4
5
 *
 * (c) The AQUA Project, Glasgow University, 1994-1997
 * (c) The GHC Team, 1998-1999
6
7
8
9
10
 *
 * Functions for parsing the argument list.
 *
 * ---------------------------------------------------------------------------*/

11
12
13
14
15
16
17
18
19
20
21
22
23
//@menu
//* Includes::			
//* Constants::			
//* Static function decls::	
//* Command-line option parsing routines::  
//* GranSim specific options::	
//* Aux fcts::			
//@end menu
//*/

//@node Includes, Constants
//@subsection Includes

24
#include "PosixSource.h"
25
26
27
28
#include "Rts.h"
#include "RtsFlags.h"
#include "RtsUtils.h"
#include "BlockAlloc.h"
sof's avatar
sof committed
29
#include "Timer.h"		/* CS_MIN_MILLISECS */
30
#include "Profiling.h"
31

32
33
#ifdef HAVE_CTYPE_H
#include <ctype.h>
34
35
#endif

36
37
38
#include <stdlib.h>
#include <string.h>

39
40
// Flag Structure
RTS_FLAGS RtsFlags;
41
42
43
44

/*
 * Split argument lists
 */
sof's avatar
sof committed
45
int     prog_argc = 0;    /* an "int" so as to match normal "argc" */
46
char  **prog_argv = NULL;
sof's avatar
sof committed
47
char   *prog_name = NULL; /* 'basename' of prog_argv[0] */
48
int     rts_argc = 0;  /* ditto */
49
50
char   *rts_argv[MAX_RTS_ARGS];

51
52
53
//@node Constants, Static function decls, Includes
//@subsection Constants

54
55
56
57
58
59
/*
 * constants, used later 
 */
#define RTS 1
#define PGM 0

60
61
#if defined(GRAN)

sof's avatar
sof committed
62
static char *gran_debug_opts_strs[] = {
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  "DEBUG (-bDe, -bD1): event_trace; printing event trace.\n",
  "DEBUG (-bDE, -bD2): event_stats; printing event statistics.\n",
  "DEBUG (-bDb, -bD4): bq; check blocking queues\n",
  "DEBUG (-bDG, -bD8): pack; routines for (un-)packing graph structures.\n",
  "DEBUG (-bDq, -bD16): checkSparkQ; check consistency of the spark queues.\n",
  "DEBUG (-bDf, -bD32): thunkStealing; print forwarding of fetches.\n",
  "DEBUG (-bDr, -bD64): randomSteal; stealing sparks/threads from random PEs.\n",
  "DEBUG (-bDF, -bD128): findWork; searching spark-pools (local & remote), thread queues for work.\n",
  "DEBUG (-bDu, -bD256): unused; currently unused flag.\n",
  "DEBUG (-bDS, -bD512): pri; priority sparking or scheduling.\n",
  "DEBUG (-bD:, -bD1024): checkLight; check GranSim-Light setup.\n",
  "DEBUG (-bDo, -bD2048): sortedQ; check whether spark/thread queues are sorted.\n",
  "DEBUG (-bDz, -bD4096): blockOnFetch; check for blocked on fetch.\n",
  "DEBUG (-bDP, -bD8192): packBuffer; routines handling pack buffer (GranSim internal!).\n",
  "DEBUG (-bDt, -bD16384): blockOnFetch_sanity; check for TSO asleep on fetch.\n",
};

/* one character codes for the available debug options */
sof's avatar
sof committed
81
static char gran_debug_opts_flags[] = {
82
83
84
85
86
  'e', 'E', 'b', 'G', 'q', 'f', 'r', 'F', 'u', 'S', ':', 'o', 'z', 'P', 't'
};

#elif defined(PAR)

sof's avatar
sof committed
87
static char *par_debug_opts_strs[] = {
88
  "DEBUG (-qDv, -qD1): verbose; be generally verbose with parallel related stuff.\n",
89
  "DEBUG (-qDq, -qD2): bq; print blocking queues.\n",
90
91
92
  "DEBUG (-qDs, -qD4): schedule; scheduling of parallel threads.\n",
  "DEBUG (-qDe, -qD8): free; free messages.\n",
  "DEBUG (-qDr, -qD16): resume; resume messages.\n",
93
  "DEBUG (-qDw, -qD32): weight; print weights and distrib GC stuff.\n",
94
  "DEBUG (-qDF, -qD64): fetch; fetch messages.\n",
95
96
97
98
99
  // "DEBUG (-qDa, -qD128): ack; ack messages.\n",
  "DEBUG (-qDf, -qD128): fish; fish messages.\n",
  //"DEBUG (-qDo, -qD512): forward; forwarding messages to other PEs.\n",
  "DEBUG (-qDl, -qD256): tables; print internal LAGA etc tables.\n",
  "DEBUG (-qDo, -qD512): packet; packets and graph structures when packing.\n",
100
101
  "DEBUG (-qDp, -qD1024): pack; packing and unpacking graphs.\n",
  "DEBUG (-qDz, -qD2048): paranoia; ridiculously detailed output (excellent for filling a partition).\n"
102
103
104
};

/* one character codes for the available debug options */
sof's avatar
sof committed
105
static char par_debug_opts_flags[] = {
106
  'v', 'q', 's', 'e', 'r', 'w', 'F', 'f', 'l', 'o', 'p', 'z'
107
108
109
110
111
112
113
};

#endif /* PAR */

//@node Static function decls, Command-line option parsing routines, Constants
//@subsection Static function decls

114
115
116
117
/* -----------------------------------------------------------------------------
   Static function decls
   -------------------------------------------------------------------------- */

118
static int		/* return NULL on error */
119
120
121
122
open_stats_file (
    I_ arg,
    int argc, char *argv[],
    int rts_argc, char *rts_argv[],
123
124
    const char *FILENAME_FMT,
    FILE **file_ret);
125
126
127
128

static I_ decode(const char *s);
static void bad_option(const char *s);

129
130
131
132
133
134
135
136
137
138
139
140
141
142
#if defined(GRAN)
static void enable_GranSimLight(void);
static void process_gran_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error);
static void set_GranSim_debug_options(nat n);
static void help_GranSim_debug_options(nat n);
#elif defined(PAR)
static void process_par_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error);
static void set_par_debug_options(nat n);
static void help_par_debug_options(nat n);
#endif

//@node Command-line option parsing routines, GranSim specific options, Static function decls
//@subsection Command-line option parsing routines

143
144
145
146
147
148
149
150
151
/* -----------------------------------------------------------------------------
 * Command-line option parsing routines.
 * ---------------------------------------------------------------------------*/

void initRtsFlagsDefaults(void)
{
    RtsFlags.GcFlags.statsFile		= NULL;
    RtsFlags.GcFlags.giveStats		= NO_GC_STATS;

152
    RtsFlags.GcFlags.maxStkSize		= (8 * 1024 * 1024) / sizeof(W_);
153
154
    RtsFlags.GcFlags.initialStkSize	= 1024 / sizeof(W_);

155
    RtsFlags.GcFlags.minAllocAreaSize   = (512 * 1024)        / BLOCK_SIZE;
156
    RtsFlags.GcFlags.minOldGenSize      = (1024 * 1024)       / BLOCK_SIZE;
157
    RtsFlags.GcFlags.maxHeapSize	= 0;    /* off by default */
158
    RtsFlags.GcFlags.heapSizeSuggestion	= 0;    /* none */
159
    RtsFlags.GcFlags.pcFreeHeap		= 3;	/* 3% */
160
    RtsFlags.GcFlags.oldGenFactor       = 2;
161
162
163
164
165
166
#if defined(PAR)
    /* A hack currently needed for GUM -- HWL */
    RtsFlags.GcFlags.generations        = 1;
    RtsFlags.GcFlags.steps              = 2;
    RtsFlags.GcFlags.squeezeUpdFrames	= rtsFalse;
#else
167
    RtsFlags.GcFlags.generations        = 2;
168
    RtsFlags.GcFlags.steps              = 2;
169
    RtsFlags.GcFlags.squeezeUpdFrames	= rtsTrue;
170
#endif
171
    RtsFlags.GcFlags.compact            = rtsFalse;
172
    RtsFlags.GcFlags.compactThreshold   = 30.0;
173
174
175
#ifdef RTS_GTK_FRONTPANEL
    RtsFlags.GcFlags.frontpanel         = rtsFalse;
#endif
176

177
178
#ifdef DEBUG
    RtsFlags.DebugFlags.scheduler	= rtsFalse;
179
    RtsFlags.DebugFlags.interpreter	= rtsFalse;
180
181
182
183
184
185
186
    RtsFlags.DebugFlags.codegen		= rtsFalse;
    RtsFlags.DebugFlags.weak		= rtsFalse;
    RtsFlags.DebugFlags.gccafs		= rtsFalse;
    RtsFlags.DebugFlags.gc		= rtsFalse;
    RtsFlags.DebugFlags.block_alloc	= rtsFalse;
    RtsFlags.DebugFlags.sanity		= rtsFalse;
    RtsFlags.DebugFlags.stable		= rtsFalse;
187
    RtsFlags.DebugFlags.stm             = rtsFalse;
188
189
190
191
192
193
    RtsFlags.DebugFlags.prof		= rtsFalse;
    RtsFlags.DebugFlags.gran		= rtsFalse;
    RtsFlags.DebugFlags.par		= rtsFalse;
    RtsFlags.DebugFlags.linker		= rtsFalse;
#endif

194
195
196
197
198
#if defined(PROFILING) || defined(PAR)
    RtsFlags.CcFlags.doCostCentres	= 0;
#endif /* PROFILING or PAR */

#ifdef PROFILING
199
    RtsFlags.ProfFlags.doHeapProfile      = rtsFalse;
200
    RtsFlags.ProfFlags.profileInterval    = 100;
201
    RtsFlags.ProfFlags.includeTSOs        = rtsFalse;
202
    RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
203
    RtsFlags.ProfFlags.maxRetainerSetSize = 8;
204
205
206
207
    RtsFlags.ProfFlags.modSelector        = NULL;
    RtsFlags.ProfFlags.descrSelector      = NULL;
    RtsFlags.ProfFlags.typeSelector       = NULL;
    RtsFlags.ProfFlags.ccSelector         = NULL;
208
    RtsFlags.ProfFlags.ccsSelector        = NULL;
209
210
    RtsFlags.ProfFlags.retainerSelector   = NULL;
    RtsFlags.ProfFlags.bioSelector        = NULL;
211

212
#elif defined(DEBUG)
213
    RtsFlags.ProfFlags.doHeapProfile      = rtsFalse;
214
215
216
#endif

    RtsFlags.ConcFlags.ctxtSwitchTime	= CS_MIN_MILLISECS;  /* In milliseconds */
217

218
#ifdef SMP
219
    RtsFlags.ParFlags.nNodes	        = 1;
220
#endif
221

222
#ifdef PAR
223
    RtsFlags.ParFlags.ParStats.Full   	  = rtsFalse;
224
    RtsFlags.ParFlags.ParStats.Suppressed = rtsFalse;
225
226
227
228
229
230
    RtsFlags.ParFlags.ParStats.Binary 	  = rtsFalse;
    RtsFlags.ParFlags.ParStats.Sparks 	  = rtsFalse;
    RtsFlags.ParFlags.ParStats.Heap   	  = rtsFalse;
    RtsFlags.ParFlags.ParStats.NewLogfile = rtsFalse;
    RtsFlags.ParFlags.ParStats.Global     = rtsFalse;

231
    RtsFlags.ParFlags.outputDisabled	= rtsFalse;
232
233
234
235
236
#ifdef DIST
    RtsFlags.ParFlags.doFairScheduling  = rtsTrue;  /* fair sched by def */
#else
    RtsFlags.ParFlags.doFairScheduling  = rtsFalse;  /* unfair sched by def */
#endif
237
    RtsFlags.ParFlags.packBufferSize	= 1024;
238
239
    RtsFlags.ParFlags.thunksToPack      = 1; /* 0 ... infinity; */
    RtsFlags.ParFlags.globalising       = 1; /* 0 ... everything */
240
241
242
    RtsFlags.ParFlags.maxThreads        = 1024;
    RtsFlags.ParFlags.maxFishes        = MAX_FISHES;
    RtsFlags.ParFlags.fishDelay         = FISH_DELAY;
243
244
245
#endif

#if defined(PAR) || defined(SMP)
246
    RtsFlags.ParFlags.maxLocalSparks	= 4096;
247
248
249
250
#endif /* PAR || SMP */

#if defined(GRAN)
    /* ToDo: check defaults for GranSim and GUM */
251
    RtsFlags.GcFlags.maxStkSize		= (8 * 1024 * 1024) / sizeof(W_);
252
253
    RtsFlags.GcFlags.initialStkSize	= 1024 / sizeof(W_);

254
    RtsFlags.GranFlags.maxThreads	= 65536; // refers to mandatory threads
255
256
257
258
259
260
261
    RtsFlags.GranFlags.GranSimStats.Full	= rtsFalse;
    RtsFlags.GranFlags.GranSimStats.Suppressed	= rtsFalse;
    RtsFlags.GranFlags.GranSimStats.Binary      = rtsFalse;
    RtsFlags.GranFlags.GranSimStats.Sparks      = rtsFalse;
    RtsFlags.GranFlags.GranSimStats.Heap        = rtsFalse;
    RtsFlags.GranFlags.GranSimStats.NewLogfile  = rtsFalse;
    RtsFlags.GranFlags.GranSimStats.Global      = rtsFalse;
262
263
264
265

    RtsFlags.GranFlags.packBufferSize	= 1024;
    RtsFlags.GranFlags.packBufferSize_internal = GRANSIM_DEFAULT_PACK_BUFFER_SIZE;

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
    RtsFlags.GranFlags.proc         = MAX_PROC;
    RtsFlags.GranFlags.Fishing      = rtsFalse;
    RtsFlags.GranFlags.maxFishes   = MAX_FISHES;
    RtsFlags.GranFlags.time_slice   = GRAN_TIME_SLICE;
    RtsFlags.GranFlags.Light        = rtsFalse;

    RtsFlags.GranFlags.Costs.latency =             LATENCY;          
    RtsFlags.GranFlags.Costs.additional_latency =  ADDITIONAL_LATENCY; 
    RtsFlags.GranFlags.Costs.fetchtime =           FETCHTIME; 
    RtsFlags.GranFlags.Costs.lunblocktime =        LOCALUNBLOCKTIME; 
    RtsFlags.GranFlags.Costs.gunblocktime =        GLOBALUNBLOCKTIME;
    RtsFlags.GranFlags.Costs.mpacktime =           MSGPACKTIME;      
    RtsFlags.GranFlags.Costs.munpacktime =         MSGUNPACKTIME;
    RtsFlags.GranFlags.Costs.mtidytime =           MSGTIDYTIME;

    RtsFlags.GranFlags.Costs.threadcreatetime =         THREADCREATETIME;
    RtsFlags.GranFlags.Costs.threadqueuetime =          THREADQUEUETIME;
    RtsFlags.GranFlags.Costs.threaddescheduletime =     THREADDESCHEDULETIME;
    RtsFlags.GranFlags.Costs.threadscheduletime =       THREADSCHEDULETIME;
    RtsFlags.GranFlags.Costs.threadcontextswitchtime =  THREADCONTEXTSWITCHTIME;

    RtsFlags.GranFlags.Costs.arith_cost =         ARITH_COST;       
    RtsFlags.GranFlags.Costs.branch_cost =        BRANCH_COST; 
    RtsFlags.GranFlags.Costs.load_cost =          LOAD_COST;        
    RtsFlags.GranFlags.Costs.store_cost =         STORE_COST; 
    RtsFlags.GranFlags.Costs.float_cost =         FLOAT_COST;       

    RtsFlags.GranFlags.Costs.heapalloc_cost =     HEAPALLOC_COST;

    RtsFlags.GranFlags.Costs.pri_spark_overhead = PRI_SPARK_OVERHEAD;        
    RtsFlags.GranFlags.Costs.pri_sched_overhead = PRI_SCHED_OVERHEAD;        

    RtsFlags.GranFlags.DoFairSchedule           = rtsFalse;             
    RtsFlags.GranFlags.DoAsyncFetch             = rtsFalse;        
    RtsFlags.GranFlags.DoStealThreadsFirst      = rtsFalse;        
    RtsFlags.GranFlags.DoAlwaysCreateThreads    = rtsFalse;      
    RtsFlags.GranFlags.DoBulkFetching           = rtsFalse;             
    RtsFlags.GranFlags.DoThreadMigration        = rtsFalse;          
    RtsFlags.GranFlags.FetchStrategy            = 2;                     
305
    RtsFlags.GranFlags.PreferSparksOfLocalNodes = rtsFalse;   
306
307
308
309
310
311
312
313
314
    RtsFlags.GranFlags.DoPrioritySparking       = rtsFalse;         
    RtsFlags.GranFlags.DoPriorityScheduling     = rtsFalse;       
    RtsFlags.GranFlags.SparkPriority            = 0;
    RtsFlags.GranFlags.SparkPriority2           = 0; 
    RtsFlags.GranFlags.RandomPriorities         = rtsFalse;           
    RtsFlags.GranFlags.InversePriorities        = rtsFalse;          
    RtsFlags.GranFlags.IgnorePriorities         = rtsFalse;           
    RtsFlags.GranFlags.ThunksToPack             = 0;                      
    RtsFlags.GranFlags.RandomSteal              = rtsTrue;
315
316
317
#endif

#ifdef TICKY_TICKY
318
319
    RtsFlags.TickyFlags.showTickyStats	 = rtsFalse;
    RtsFlags.TickyFlags.tickyFile	 = NULL;
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#endif
}

static const char *
usage_text[] = {
"",
"Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
"",
"   +RTS    Indicates run time system options follow",
"   -RTS    Indicates program arguments follow",
"  --RTS    Indicates that ALL subsequent arguments will be given to the",
"           program (including any of these RTS flags)",
"",
"The following run time system options are available:",
"",
335
"  -?       Prints this message and exits; the program is not executed",
336
"",
337
"  -K<size> Sets the maximum stack size (default 8M)  Egs: -K32k   -K512k",
sof's avatar
sof committed
338
"  -k<size> Sets the initial thread stack size (default 1k)  Egs: -k4k   -k2m",
339
340
"",
"  -A<size> Sets the minimum allocation area size (default 256k) Egs: -A1m -A10k",
341
"  -M<size> Sets the maximum heap size (default unlimited)  Egs: -M256k -M1G",
342
"  -H<size> Sets the minimum heap size (default 0M)   Egs: -H24m  -H1G",
343
"  -m<n>    Minimum % of heap which must be available (default 3%)",
344
"  -G<n>    Number of generations (default: 2)",
345
"  -T<n>    Number of steps in younger generations (default: 2)",
346
347
"  -c<n>    Auto-enable compaction of the oldest generation when live data is",
"           at least <n>% of the maximum heap size set with -M (default: 30%)",
348
"  -c       Enable compaction for all major collections",
349
350
351
352
"",
"  -t<file> One-line GC statistics  (default file: <program>.stat)",
"  -s<file> Summary  GC statistics  (with -Sstderr going to stderr)",
"  -S<file> Detailed GC statistics",
353
354
355
#ifdef RTS_GTK_FRONTPANEL
"  -f       Display front panel (requires X11 & GTK+)",
#endif
356
357
358
359
360
361
"",
"",
"  -Z       Don't squeeze out update frames on stack overflow",
"  -B       Sound the bell at the start of each garbage collection",
#if defined(PROFILING) || defined(PAR)
"",
362
"  -px      Time/allocation profile (XML)  (output file <program>.prof)",
andy's avatar
andy committed
363
364
365
"  -p       Time/allocation profile        (output file <program>.prof)",
"  -P       More detailed Time/Allocation profile",
"  -Pa      Give information about *all* cost centres",
366

367
368
# if defined(PROFILING)
"",
369
370
371
372
373
374
375
376
"  -hx            Heap residency profile (XML)   (output file <program>.prof)",
"  -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
"     break-down: c = cost centre stack (default)",
"                 m = module",
"                 d = closure description",
"                 y = type description",
"                 r = retainer",
"                 b = biography (LAG,DRAG,VOID,USE)",
377
"  A subset of closures may be selected thusly:",
378
379
"    -hc<cc>,...  specific cost centre(s) (top of stack only)",
"    -hC<cc>,...  specific cost centre(s) (anywhere in stack)",
380
381
382
383
384
"    -hm<mod>...  all cost centres from the specified modules(s)",
"    -hd<des>,... closures with specified closure descriptions",
"    -hy<typ>...  closures with specified type descriptions",
"    -hr<cc>...   closures with specified retainers",
"    -hb<bio>...  closures with specified biographies (lag,drag,void,use)",
385
386
"",
"  -R<size>       Set the maximum retainer set size (default: 8)",
387
"",
388
"  -i<sec>        Time between heap samples (seconds, default: 0.1)",
andy's avatar
andy committed
389
"",
390
391
"  -xt            Include threads (TSOs) in a heap profile",
"",
392
"  -xc      Show current cost centre stack on raising an exception",
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# endif
#endif /* PROFILING or PAR */
#if !defined(PROFILING) && defined(DEBUG)
"",
"  -h<break-down> Debugging Heap residency profile",
"                 (output file <program>.hp)",
"     break-down: L = closure label (default)",
"                 T = closure type (constructor, thunk etc.)",
#endif
"",
#if defined(TICKY_TICKY)
"  -r<file>  Produce reduction profiling statistics (with -rstderr for stderr)",
"",
#endif
407
#if defined(PAR)
408
409
"  -N<n>     Use <n> PVMish processors in parallel (default: 2)",
/* NB: the -N<n> is implemented by the driver!! */
410
#endif
411
412
"  -C<secs>  Context-switch interval in seconds",
"                (0 or no argument means switch as often as possible)",
413
"                the default is .02 sec; resolution is .02 sec",
414
415
416
"",
#if defined(DEBUG)
"  -Ds  DEBUG: scheduler",
417
"  -Di  DEBUG: interpreter",
418
419
420
421
422
423
424
425
426
427
428
"  -Dc  DEBUG: codegen",
"  -Dw  DEBUG: weak",
"  -DG  DEBUG: gccafs",
"  -Dg  DEBUG: gc",
"  -Db  DEBUG: block",
"  -DS  DEBUG: sanity",
"  -Dt  DEBUG: stable",
"  -Dp  DEBUG: prof",
"  -Dr  DEBUG: gran",
"  -DP  DEBUG: par",
"  -Dl  DEBUG: linker",
429
"  -Dm  DEBUG: stm",
430
"",
431
#endif /* DEBUG */
432
#if defined(SMP)
433
"  -N<n>     Use <n> OS threads (default: 1)",
434
435
436
437
438
#endif
#if defined(SMP) || defined(PAR)
"  -e<size>  Size of spark pools (default 100)",
#endif
#if defined(PAR)
439
440
441
442
443
"  -t<num>   Set maximum number of advisory threads per PE (default 32)",
"  -qP       Enable activity profile (output files in ~/<program>*.gr)",
"  -qQ<size> Set pack-buffer size (default: 1024)",
"  -qd       Turn on PVM-ish debugging",
"  -qO       Disable output for performance measurement",
444
445
#endif
#if defined(SMP) || defined(PAR)
446
447
"  -e<n>     Maximum number of outstanding local sparks (default: 4096)",
#endif
448
#if defined(PAR)
449
450
"  -d        Turn on PVM-ish debugging",
"  -O        Disable output for performance measurement",
451
452
#endif /* PAR */
#if defined(GRAN)  /* ToDo: fill in decent Docu here */
453
"  -b...     All GranSim options start with -b; see GranSim User's Guide for details",
454
#endif
455
"",
456
457
"RTS options may also be specified using the GHCRTS environment variable.",
"",
458
459
460
461
462
463
"Other RTS options may be available for programs compiled a different way.",
"The GHC User's Guide has full details.",
"",
0
};

sof's avatar
sof committed
464
STATIC_INLINE rtsBool
465
466
467
468
469
strequal(const char *a, const char * b)
{
    return(strcmp(a, b) == 0);
}

470
471
472
473
474
475
476
477
478
479
480
481
482
483
static void
splitRtsFlags(char *s, int *rts_argc, char *rts_argv[])
{
    char *c1, *c2;

    c1 = s;
    do {
	while (isspace(*c1)) { c1++; };
	c2 = c1;
	while (!isspace(*c2) && *c2 != '\0') { c2++; };
	
	if (c1 == c2) { break; }
	
	if (*rts_argc < MAX_RTS_ARGS-1) {
sof's avatar
sof committed
484
	    s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
485
486
487
488
489
490
491
492
493
494
495
	    strncpy(s, c1, c2-c1);
	    s[c2-c1] = '\0';
	    rts_argv[(*rts_argc)++] = s;
	} else {
	    barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
	}
	
	c1 = c2;
    } while (*c1 != '\0');
}
    
496
497
498
499
500
501
502
void
setupRtsFlags(int *argc, char *argv[], int *rts_argc, char *rts_argv[])
{
    rtsBool error = rtsFalse;
    I_ mode;
    I_ arg, total_arg;

503
    setProgName (argv);
504
505
506
507
508
509
    total_arg = *argc;
    arg = 1;

    *argc = 1;
    *rts_argc = 0;

510
511
512
513
514
515
516
517
518
    // process arguments from the ghc_rts_opts global variable first.
    // (arguments from the GHCRTS environment variable and the command
    // line override these).
    {
	if (ghc_rts_opts != NULL) {
	    splitRtsFlags(ghc_rts_opts, rts_argc, rts_argv);
	}
    }

519
    // process arguments from the GHCRTS environment variable next
520
    // (arguments from the command line override these).
521
522
523
524
    {
	char *ghc_rts = getenv("GHCRTS");

	if (ghc_rts != NULL) {
525
	    splitRtsFlags(ghc_rts, rts_argc, rts_argv);
526
527
	}
    }
528

529
530
531
    // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
    //   argv[0] must be PGM argument -- leave in argv

532
533
534
535
536
537
538
539
540
541
542
543
    for (mode = PGM; arg < total_arg; arg++) {
	// The '--RTS' argument disables all future +RTS ... -RTS processing.
	if (strequal("--RTS", argv[arg])) {
	    arg++;
	    break;
	}
	// The '--' argument is passed through to the program, but
	// disables all further +RTS ... -RTS processing.
	else if (strequal("--", argv[arg])) {
	    break;
	}
	else if (strequal("+RTS", argv[arg])) {
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
	    mode = RTS;
	}
	else if (strequal("-RTS", argv[arg])) {
	    mode = PGM;
	}
	else if (mode == RTS && *rts_argc < MAX_RTS_ARGS-1) {
	    rts_argv[(*rts_argc)++] = argv[arg];
	}
	else if (mode == PGM) {
	    argv[(*argc)++] = argv[arg];
	}
	else {
	  barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
	}
    }
559
560
561
    // process remaining program arguments
    for (; arg < total_arg; arg++) {
	argv[(*argc)++] = argv[arg];
562
563
564
565
566
    }
    argv[*argc] = (char *) 0;
    rts_argv[*rts_argc] = (char *) 0;

    // Process RTS (rts_argv) part: mainly to determine statsfile
567
568
569
    for (arg = 0; arg < *rts_argc; arg++) {
	if (rts_argv[arg][0] != '-') {
	    fflush(stdout);
570
	    errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
571
572
573
574
575
576
577
578
579
580
	    error = rtsTrue;

        } else {
	    switch(rts_argv[arg][1]) {

	      /* process: general args, then PROFILING-only ones,
		 then CONCURRENT-only, PARallel-only, GRAN-only,
		 TICKY-only (same order as defined in RtsFlags.lh);
		 within those groups, mostly in case-insensitive
		 alphabetical order.
581
                 Final group is x*, which allows for more options.
582
583
584
585
586
587
	      */

#ifdef TICKY_TICKY
# define TICKY_BUILD_ONLY(x) x
#else
# define TICKY_BUILD_ONLY(x) \
588
errorBelch("not built for: ticky-ticky stats"); \
589
590
591
592
593
594
595
error = rtsTrue;
#endif

#if defined(PROFILING) 
# define COST_CENTRE_USING_BUILD_ONLY(x) x
#else
# define COST_CENTRE_USING_BUILD_ONLY(x) \
596
errorBelch("not built for: -prof or -parallel"); \
597
598
599
600
601
602
603
error = rtsTrue;
#endif

#ifdef PROFILING
# define PROFILING_BUILD_ONLY(x)   x
#else
# define PROFILING_BUILD_ONLY(x) \
604
errorBelch("not built for: -prof"); \
605
606
607
608
609
610
611
error = rtsTrue;
#endif

#ifdef SMP
# define SMP_BUILD_ONLY(x)      x
#else
# define SMP_BUILD_ONLY(x) \
612
errorBelch("not built for: -smp"); \
613
614
615
616
617
618
619
error = rtsTrue;
#endif

#ifdef PAR
# define PAR_BUILD_ONLY(x)      x
#else
# define PAR_BUILD_ONLY(x) \
620
errorBelch("not built for: -parallel"); \
621
622
623
624
625
626
627
error = rtsTrue;
#endif

#if defined(SMP) || defined(PAR)
# define PAR_OR_SMP_BUILD_ONLY(x)      x
#else
# define PAR_OR_SMP_BUILD_ONLY(x) \
628
errorBelch("not built for: -parallel or -smp"); \
629
630
631
632
633
634
635
error = rtsTrue;
#endif

#ifdef GRAN
# define GRAN_BUILD_ONLY(x)     x
#else
# define GRAN_BUILD_ONLY(x) \
636
errorBelch("not built for: -gransim"); \
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
error = rtsTrue;
#endif

	      /* =========== GENERAL ========================== */
	      case '?':
		error = rtsTrue;
		break;

	      case 'A':
		RtsFlags.GcFlags.minAllocAreaSize
		  = decode(rts_argv[arg]+2) / BLOCK_SIZE;
		if (RtsFlags.GcFlags.minAllocAreaSize <= 0) {
		  bad_option(rts_argv[arg]);
		}
		break;

	      case 'B':
		RtsFlags.GcFlags.ringBell = rtsTrue;
		break;

657
	      case 'c':
658
659
660
661
		  if (rts_argv[arg][2] != '\0') {
		      RtsFlags.GcFlags.compactThreshold =
			  atof(rts_argv[arg]+2);
		  } else {
662
		      RtsFlags.GcFlags.compact = rtsTrue;
663
664
		  }
		  break;
665

666
667
668
669
670
671
672
	      case 'F':
	        RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
	      
		if (RtsFlags.GcFlags.oldGenFactor < 0)
		  bad_option( rts_argv[arg] );
		break;
	      
673
674
#ifdef DEBUG
	      case 'D':
675
676
677
678
679
680
681
682
	      { 
		  char *c;

		  for (c  = rts_argv[arg] + 2; *c != '\0'; c++) {
		      switch (*c) {
		      case 's':
			  RtsFlags.DebugFlags.scheduler = rtsTrue;
			  break;
683
684
		      case 'i':
			  RtsFlags.DebugFlags.interpreter = rtsTrue;
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
			  break;
		      case 'c':
			  RtsFlags.DebugFlags.codegen = rtsTrue;
			  break;
		      case 'w':
			  RtsFlags.DebugFlags.weak = rtsTrue;
			  break;
		      case 'G':
			  RtsFlags.DebugFlags.gccafs = rtsTrue;
			  break;
		      case 'g':
			  RtsFlags.DebugFlags.gc = rtsTrue;
			  break;
		      case 'b':
			  RtsFlags.DebugFlags.block_alloc = rtsTrue;
			  break;
		      case 'S':
			  RtsFlags.DebugFlags.sanity = rtsTrue;
			  break;
		      case 't':
			  RtsFlags.DebugFlags.stable = rtsTrue;
			  break;
		      case 'p':
			  RtsFlags.DebugFlags.prof = rtsTrue;
			  break;
		      case 'r':
			  RtsFlags.DebugFlags.gran = rtsTrue;
			  break;
		      case 'P':
			  RtsFlags.DebugFlags.par = rtsTrue;
			  break;
		      case 'l':
			  RtsFlags.DebugFlags.linker = rtsTrue;
			  break;
719
720
721
		      case 'a':
			  RtsFlags.DebugFlags.apply = rtsTrue;
			  break;
722
723
724
		      case 'm':
			  RtsFlags.DebugFlags.stm = rtsTrue;
			  break;
725
726
727
728
729
730
		      default:
			  bad_option( rts_argv[arg] );
		      }
		  }
		  break;
	      }
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#endif

	      case 'K':
		RtsFlags.GcFlags.maxStkSize = 
		  decode(rts_argv[arg]+2) / sizeof(W_);

		if (RtsFlags.GcFlags.maxStkSize == 0) 
		  bad_option( rts_argv[arg] );
		break;

	      case 'k':
		RtsFlags.GcFlags.initialStkSize = 
		  decode(rts_argv[arg]+2) / sizeof(W_);

		if (RtsFlags.GcFlags.initialStkSize == 0) 
		  bad_option( rts_argv[arg] );
		break;

	      case 'M':
		RtsFlags.GcFlags.maxHeapSize = 
		  decode(rts_argv[arg]+2) / BLOCK_SIZE;
		/* user give size in *bytes* but "maxHeapSize" is in *blocks* */

		if (RtsFlags.GcFlags.maxHeapSize <= 0) {
		  bad_option(rts_argv[arg]);
		}
		break;

	      case 'm':
		RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);

		if (RtsFlags.GcFlags.pcFreeHeap < 0 || 
		    RtsFlags.GcFlags.pcFreeHeap > 100)
		  bad_option( rts_argv[arg] );
		break;

767
768
	      case 'G':
		RtsFlags.GcFlags.generations = decode(rts_argv[arg]+2);
769
		if (RtsFlags.GcFlags.generations < 1) {
770
771
772
773
		  bad_option(rts_argv[arg]);
		}
		break;

774
775
776
777
778
779
780
	      case 'T':
		RtsFlags.GcFlags.steps = decode(rts_argv[arg]+2);
		if (RtsFlags.GcFlags.steps < 1) {
		  bad_option(rts_argv[arg]);
		}
		break;

781
	      case 'H':
782
783
784
785
786
787
		RtsFlags.GcFlags.heapSizeSuggestion = 
		  decode(rts_argv[arg]+2) / BLOCK_SIZE;

		if (RtsFlags.GcFlags.heapSizeSuggestion <= 0) {
		  bad_option(rts_argv[arg]);
		}
788
789
		break;

790
791
792
793
794
795
#ifdef RTS_GTK_FRONTPANEL
	      case 'f':
		  RtsFlags.GcFlags.frontpanel = rtsTrue;
		  break;
#endif

796
	      case 'S':
797
798
		  RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
		  goto stats;
799

800
	      case 's':
801
802
		  RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
		  goto stats;
803
804

	      case 't':
805
806
		  RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
		  goto stats;
807

808
	    stats:
809
#ifdef PAR
810
811
		/* Opening all those files would almost certainly fail... */
		// RtsFlags.ParFlags.ParStats.Full = rtsTrue;
812
		RtsFlags.GcFlags.statsFile = NULL; /* temporary; ToDo: rm */
813
#else
814
815
816
817
818
819
820
		{ 
		    int r;
		    r = open_stats_file(arg, *argc, argv,
					*rts_argc, rts_argv, STAT_FILENAME_FMT,
					&RtsFlags.GcFlags.statsFile);
		    if (r == -1) { error = rtsTrue; }
		}
821
#endif
822
		  break;
823
824
825
826
827
828
829
830
831
832
833

	      case 'Z':
		RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
		break;

	      /* =========== PROFILING ========================== */

	      case 'P': /* detailed cost centre profiling (time/alloc) */
	      case 'p': /* cost centre profiling (time/alloc) */
		COST_CENTRE_USING_BUILD_ONLY(
		switch (rts_argv[arg][2]) {
834
835
		  case 'x':
		    RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
836
		    break;
andy's avatar
andy committed
837
838
839
		  case 'a':
		    RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
		    break;
840
		  default:
841
842
843
844
845
846
847
848
		      if (rts_argv[arg][1] == 'P') {
			  RtsFlags.CcFlags.doCostCentres =
			      COST_CENTRES_VERBOSE;
		      } else {
			  RtsFlags.CcFlags.doCostCentres =
			      COST_CENTRES_SUMMARY;
		      }
		      break;
849
850
851
		}
		) break;

852
853
854
855
856
	      case 'R':
		  PROFILING_BUILD_ONLY(
		      RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
  	          ) break;

857
858
859
860
861
862
863
864
865
866
867
	      case 'h': /* serial heap profile */
#if !defined(PROFILING) && defined(DEBUG)
		switch (rts_argv[arg][2]) {
		  case '\0':
		  case 'L':
		    RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_INFOPTR;
		    break;
		  case 'T':
		    RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
		    break;
		  default:
868
		    errorBelch("invalid heap profile option: %s",rts_argv[arg]);
869
870
871
872
873
		    error = rtsTrue;
		}
#else
		PROFILING_BUILD_ONLY(
		switch (rts_argv[arg][2]) {
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
		case '\0':
		case 'C':
		case 'c':
		case 'M':
		case 'm':
		case 'D':
		case 'd':
		case 'Y':
		case 'y':
		case 'R':
		case 'r':
		case 'B':
		case 'b':
		    if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
			{
			    char *left  = strchr(rts_argv[arg], '{');
			    char *right = strrchr(rts_argv[arg], '}');
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907

			    // curly braces are optional, for
			    // backwards compat.
			    if (left)
				left = left+1;
			    else
				left = rts_argv[arg] + 3;

			    if (!right)
				right = rts_argv[arg] + strlen(rts_argv[arg]);

			    *right = '\0';

			    switch (rts_argv[arg][2]) {
			    case 'c': // cost centre label select
				RtsFlags.ProfFlags.ccSelector = left;
				break;
908
909
910
			    case 'C':
				RtsFlags.ProfFlags.ccsSelector = left;
				break;
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
			    case 'M':
			    case 'm': // cost centre module select
				RtsFlags.ProfFlags.modSelector = left;
				break;
			    case 'D':
			    case 'd': // closure descr select 
				RtsFlags.ProfFlags.descrSelector = left;
				break;
			    case 'Y':
			    case 'y': // closure type select
				RtsFlags.ProfFlags.typeSelector = left;
				break;
			    case 'R':
			    case 'r': // retainer select
				RtsFlags.ProfFlags.retainerSelector = left;
				break;
			    case 'B':
			    case 'b': // biography select
				RtsFlags.ProfFlags.bioSelector = left;
				break;
931
932
933
934
			    }
			}
			break;
		    }
935

936
		    if (RtsFlags.ProfFlags.doHeapProfile != 0) {
937
			errorBelch("multiple heap profile options");
938
939
940
941
942
943
944
945
946
947
948
949
			error = rtsTrue;
			break;
		    }

		    switch (rts_argv[arg][2]) {
		    case '\0':
		    case 'C':
		    case 'c':
			RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
			break;
		    case 'M':
		    case 'm':
950
951
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
			  break;
952
953
		    case 'D':
		    case 'd':
954
955
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
			  break;
956
957
		    case 'Y':
		    case 'y':
958
959
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
			  break;
960
961
		    case 'R':
		    case 'r':
962
963
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
			  break;
964
965
		    case 'B':
		    case 'b':
966
967
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
			  break;
968
969
		    }
		    break;
970
		      
971
		default:
972
		    errorBelch("invalid heap profile option: %s",rts_argv[arg]);
973
974
975
		    error = rtsTrue;
		}
		) 
976
#endif /* PROFILING */
andy's avatar
andy committed
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
    	    	break;

#if defined(PROFILING) 
    	      case 'i':	/* heap sample interval */
		if (rts_argv[arg][2] == '\0') {
		  /* use default */
		} else {
		    I_ cst; /* tmp */

		    /* Convert to milliseconds */
		    cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
		    cst = (cst / CS_MIN_MILLISECS) * CS_MIN_MILLISECS;
		    if (cst != 0 && cst < CS_MIN_MILLISECS)
			cst = CS_MIN_MILLISECS;

992
		    RtsFlags.ProfFlags.profileInterval = cst;
andy's avatar
andy committed
993
994
995
		}
		break;
#endif
996
997
998
999
1000
1001
1002
1003
1004
1005
1006

	      /* =========== CONCURRENT ========================= */
    	      case 'C':	/* context switch interval */
		if (rts_argv[arg][2] == '\0')
    	    	    RtsFlags.ConcFlags.ctxtSwitchTime = 0;
		else {
		    I_ cst; /* tmp */

		    /* Convert to milliseconds */
		    cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
		    cst = (cst / CS_MIN_MILLISECS) * CS_MIN_MILLISECS;
1007
		    if (cst != 0 && cst < CS_MIN_MILLISECS)
1008
1009
1010
1011
1012
1013
			cst = CS_MIN_MILLISECS;

		    RtsFlags.ConcFlags.ctxtSwitchTime = cst;
		}
    	    	break;

1014
1015
#ifdef SMP
	      case 'N':
1016
		SMP_BUILD_ONLY(
1017
		if (rts_argv[arg][2] != '\0') {
1018
		    RtsFlags.ParFlags.nNodes
1019
		      = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1020
		    if (RtsFlags.ParFlags.nNodes <= 0) {
1021
		      errorBelch("bad value for -N");
1022
		      error = rtsTrue;
1023
1024
		    }
		}
1025
		) break;
1026
#endif
1027
1028
	      /* =========== PARALLEL =========================== */
	      case 'e':
1029
1030
		PAR_OR_SMP_BUILD_ONLY(
		if (rts_argv[arg][2] != '\0') {
1031
1032
1033
		    RtsFlags.ParFlags.maxLocalSparks
		      = strtol(rts_argv[arg]+2, (char **) NULL, 10);
		    if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1034
		      errorBelch("bad value for -e");
1035
		      error = rtsTrue;
1036
1037
1038
1039
		    }
		}
		) break;

1040
    	      case 'q':
1041
		PAR_BUILD_ONLY(
1042
		  process_par_option(arg, rts_argc, rts_argv, &error);
1043
1044
1045
1046
1047
1048
		) break;

	      /* =========== GRAN =============================== */

    	      case 'b':
		GRAN_BUILD_ONLY(
1049
		  process_gran_option(arg, rts_argc, rts_argv, &error);
1050
1051
1052
1053
1054
1055
1056
1057
1058
		) break;

	      /* =========== TICKY ============================== */

	      case 'r': /* Basic profiling stats */
		TICKY_BUILD_ONLY(

		RtsFlags.TickyFlags.showTickyStats = rtsTrue;

1059
1060
1061
1062
1063
1064
1065
		{ 
		    int r;
		    r = open_stats_file(arg, *argc, argv,
					*rts_argc, rts_argv, TICKY_FILENAME_FMT,
					&RtsFlags.TickyFlags.tickyFile);
		    if (r == -1) { error = rtsTrue; }
		}
1066
1067
	        ) break;

1068
1069
1070
1071
1072
	      /* =========== EXTENDED OPTIONS =================== */

              case 'x': /* Extend the argument space */
                switch(rts_argv[arg][2]) {
                  case '\0':
1073
		    errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1074
1075
1076
1077
1078
		    error = rtsTrue;
		    break;

                  case 'c': /* Debugging tool: show current cost centre on an exception */
                    PROFILING_BUILD_ONLY(
1079
1080
1081
1082
1083
1084
1085
1086
1087
			RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
			);
		    break;

		case 't':  /* Include memory used by TSOs in a heap profile */
		    PROFILING_BUILD_ONLY(
			RtsFlags.ProfFlags.includeTSOs = rtsTrue;
			);
		    break;
1088
1089
1090
1091

                  /* The option prefix '-xx' is reserved for future extension.  KSW 1999-11. */

	          default:
1092
		    errorBelch("unknown RTS option: %s",rts_argv[arg]);
1093
1094
1095
1096
1097
		    error = rtsTrue;
		    break;
                }
                break;  /* defensive programming */

1098
1099
	      /* =========== OH DEAR ============================ */
	      default:
1100
		errorBelch("unknown RTS option: %s",rts_argv[arg]);
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
		error = rtsTrue;
		break;
	    }
	}
    }
    if (error) {
	const char **p;

        fflush(stdout);
	for (p = usage_text; *p; p++)
1111
	    errorBelch("%s", *p);
1112
1113
1114
1115
	stg_exit(EXIT_FAILURE);
    }
}

1116
1117
1118
1119
1120
1121
1122
1123
#if defined(GRAN)

//@node GranSim specific options, Aux fcts, Command-line option parsing routines
//@subsection GranSim specific options

static void
enable_GranSimLight(void) {

1124
    debugBelch("GrAnSim Light enabled (infinite number of processors;  0 communication costs)\n");
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
    RtsFlags.GranFlags.Light=rtsTrue;
    RtsFlags.GranFlags.Costs.latency = 
	RtsFlags.GranFlags.Costs.fetchtime = 
	RtsFlags.GranFlags.Costs.additional_latency =
	RtsFlags.GranFlags.Costs.gunblocktime = 
	RtsFlags.GranFlags.Costs.lunblocktime =
	RtsFlags.GranFlags.Costs.threadcreatetime = 
	RtsFlags.GranFlags.Costs.threadqueuetime =
	RtsFlags.GranFlags.Costs.threadscheduletime = 
	RtsFlags.GranFlags.Costs.threaddescheduletime =
	RtsFlags.GranFlags.Costs.threadcontextswitchtime = 0;
  
    RtsFlags.GranFlags.Costs.mpacktime = 
	RtsFlags.GranFlags.Costs.munpacktime = 0;

    RtsFlags.GranFlags.DoFairSchedule = rtsTrue;
    RtsFlags.GranFlags.DoAsyncFetch = rtsFalse;
    RtsFlags.GranFlags.DoAlwaysCreateThreads = rtsTrue;
    /* FetchStrategy is irrelevant in GrAnSim-Light */

    /* GrAnSim Light often creates an abundance of parallel threads,
       each with its own stack etc. Therefore, it's in general a good
       idea to use small stack chunks (use the -o<size> option to 
       increase it again). 
    */
    // RtsFlags.ConcFlags.stkChunkSize = 100;

    RtsFlags.GranFlags.proc = 1; 
}

static void
process_gran_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error)
{
    if (rts_argv[arg][1] != 'b') /* All GranSim options start with -b */
      return;

    /* or a ridiculously idealised simulator */
    if(strcmp((rts_argv[arg]+2),"oring")==0) {
      RtsFlags.GranFlags.Costs.latency = 
	RtsFlags.GranFlags.Costs.fetchtime = 
	RtsFlags.GranFlags.Costs.additional_latency =
	RtsFlags.GranFlags.Costs.gunblocktime = 
	RtsFlags.GranFlags.Costs.lunblocktime =
	RtsFlags.GranFlags.Costs.threadcreatetime = 
	RtsFlags.GranFlags.Costs.threadqueuetime =
	RtsFlags.GranFlags.Costs.threadscheduletime = 
	RtsFlags.GranFlags.Costs.threaddescheduletime =
	RtsFlags.GranFlags.Costs.threadcontextswitchtime = 0;

      RtsFlags.GranFlags.Costs.mpacktime = 
	RtsFlags.GranFlags.Costs.munpacktime = 0;

      RtsFlags.GranFlags.Costs.arith_cost = 
	RtsFlags.GranFlags.Costs.float_cost = 
	RtsFlags.GranFlags.Costs.load_cost =
	RtsFlags.GranFlags.Costs.store_cost = 
	RtsFlags.GranFlags.Costs.branch_cost = 0;

      RtsFlags.GranFlags.Costs.heapalloc_cost = 1;

      /* ++RtsFlags.GranFlags.DoFairSchedule; */
      RtsFlags.GranFlags.DoStealThreadsFirst = rtsTrue;        /* -bZ */
      RtsFlags.GranFlags.DoThreadMigration   = rtsTrue;        /* -bM */
      RtsFlags.GranFlags.GranSimStats.Full   = rtsTrue;        /* -bP */
      return;
    }

      /* or a somewhat idealised simulator */
      if(strcmp((rts_argv[arg]+2),"onzo")==0) {
	RtsFlags.GranFlags.Costs.latency = 
	RtsFlags.GranFlags.Costs.fetchtime = 
	RtsFlags.GranFlags.Costs.additional_latency =
	RtsFlags.GranFlags.Costs.gunblocktime = 
	RtsFlags.GranFlags.Costs.lunblocktime =
	RtsFlags.GranFlags.Costs.threadcreatetime = 
	RtsFlags.GranFlags.Costs.threadqueuetime =
	RtsFlags.GranFlags.Costs.threadscheduletime = 
	RtsFlags.GranFlags.Costs.threaddescheduletime =
	RtsFlags.GranFlags.Costs.threadcontextswitchtime = 0;

	RtsFlags.GranFlags.Costs.mpacktime = 
	RtsFlags.GranFlags.Costs.munpacktime = 0;
	
	RtsFlags.GranFlags.Costs.heapalloc_cost = 1;

	/* RtsFlags.GranFlags.DoFairSchedule  = rtsTrue; */       /* -b-R */
	/* RtsFlags.GranFlags.DoStealThreadsFirst = rtsTrue; */   /* -b-T */
	RtsFlags.GranFlags.DoAsyncFetch = rtsTrue;         /* -bZ */
	RtsFlags.GranFlags.DoThreadMigration  = rtsTrue;          /* -bM */
	RtsFlags.GranFlags.GranSimStats.Full  = rtsTrue;          /* -bP */
#  if defined(GRAN_CHECK) && defined(GRAN)
	RtsFlags.GranFlags.Debug.event_stats = rtsTrue; /* print event statistics   */
#  endif
	return;
      }

      /* Communication and task creation cost parameters */
      switch(rts_argv[arg][2]) {
        case '.':
	  IgnoreYields = rtsTrue; // HWL HACK
	  break;

        case ':':
	  enable_GranSimLight();       /* set flags for GrAnSim-Light mode */
	  break;

        case 'l':
	  if (rts_argv[arg][3] != '\0')
	    {
	      RtsFlags.GranFlags.Costs.gunblocktime = 
	      RtsFlags.GranFlags.Costs.latency = decode(rts_argv[arg]+3);
	      RtsFlags.GranFlags.Costs.fetchtime = 2*RtsFlags.GranFlags.Costs.latency;
	    }
	  else
	    RtsFlags.GranFlags.Costs.latency = LATENCY;
	  break;

        case 'a':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.additional_latency = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.additional_latency = ADDITIONAL_LATENCY;
	  break;

        case 'm':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.mpacktime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.mpacktime = MSGPACKTIME;
	  break;

        case 'x':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.mtidytime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.mtidytime = 0;
	  break;

        case 'r':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.munpacktime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.munpacktime = MSGUNPACKTIME;
	  break;
	  
        case 'g':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.fetchtime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.fetchtime = FETCHTIME;
	  break;
	  
        case 'n':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.gunblocktime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.gunblocktime = GLOBALUNBLOCKTIME;
	  break;

        case 'u':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.lunblocktime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.lunblocktime = LOCALUNBLOCKTIME;
	  break;

	/* Thread-related metrics */
        case 't':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.threadcreatetime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.threadcreatetime = THREADCREATETIME;
	  break;
	  
        case 'q':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.threadqueuetime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.threadqueuetime = THREADQUEUETIME;
	  break;
	  
        case 'c':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.threadscheduletime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.threadscheduletime = THREADSCHEDULETIME;
	  
	  RtsFlags.GranFlags.Costs.threadcontextswitchtime = RtsFlags.GranFlags.Costs.threadscheduletime
	    + RtsFlags.GranFlags.Costs.threaddescheduletime;
	  break;

        case 'd':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.threaddescheduletime = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.threaddescheduletime = THREADDESCHEDULETIME;
	  
	  RtsFlags.GranFlags.Costs.threadcontextswitchtime = RtsFlags.GranFlags.Costs.threadscheduletime
	    + RtsFlags.GranFlags.Costs.threaddescheduletime;
	  break;

	/* Instruction Cost Metrics */
        case 'A':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.arith_cost = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.arith_cost = ARITH_COST;
	  break;

        case 'F':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.float_cost = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.float_cost = FLOAT_COST;
	  break;
		      
        case 'B':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.branch_cost = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.branch_cost = BRANCH_COST;
	  break;

        case 'L':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.load_cost = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.load_cost = LOAD_COST;
	  break;
	  
        case 'S':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.store_cost = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.store_cost = STORE_COST;
	  break;

        case 'H':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.heapalloc_cost = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.heapalloc_cost = 0;
	  break;

        case 'y':
	  RtsFlags.GranFlags.DoAsyncFetch = rtsTrue;
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.FetchStrategy = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.FetchStrategy = 2;
	  if (RtsFlags.GranFlags.FetchStrategy == 0)
	    RtsFlags.GranFlags.DoAsyncFetch = rtsFalse;
	  break;
	  
        case 'K':   /* sort overhead (per elem in spark list) */
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.pri_spark_overhead = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.pri_spark_overhead = PRI_SPARK_OVERHEAD;
1384
	  debugBelch("Overhead for pri spark: %d (per elem).\n",
1385
1386
1387
1388
1389
1390
1391
1392
		         RtsFlags.GranFlags.Costs.pri_spark_overhead);
	  break;

        case 'O':  /* sort overhead (per elem in spark list) */
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.Costs.pri_sched_overhead = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.Costs.pri_sched_overhead = PRI_SCHED_OVERHEAD;
1393
	  debugBelch("Overhead for pri sched: %d (per elem).\n",
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
		       RtsFlags.GranFlags.Costs.pri_sched_overhead);
	  break;

        /* General Parameters */
        case 'p':
	  if (rts_argv[arg][3] != '\0')
	    {
	      RtsFlags.GranFlags.proc = decode(rts_argv[arg]+3);
	      if (RtsFlags.GranFlags.proc==0) {
		  enable_GranSimLight(); /* set flags for GrAnSim-Light mode */
	      } else if (RtsFlags.GranFlags.proc > MAX_PROC || 
			 RtsFlags.GranFlags.proc < 1)
		{
1407
		  debugBelch("setupRtsFlags: no more than %u processors allowed\n",
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
			  MAX_PROC);
		  *error = rtsTrue;
		}
	    }
	  else
	    RtsFlags.GranFlags.proc = MAX_PROC;
	  break;

        case 'f':
	  RtsFlags.GranFlags.Fishing = rtsTrue;
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.maxFishes = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.maxFishes = MAX_FISHES;
	  break;
	  
        case 'w':
	  if (rts_argv[arg][3] != '\0')
	    RtsFlags.GranFlags.time_slice = decode(rts_argv[arg]+3);
	  else
	    RtsFlags.GranFlags.time_slice = GRAN_TIME_SLICE;
	  break;
	  
        case 'C':
	  RtsFlags.GranFlags.DoAlwaysCreateThreads=rtsTrue;
	  RtsFlags.GranFlags.DoThreadMigration=rtsTrue;
	  break;

        case 'G':
1437
	  debugBelch("Bulk fetching enabled.\n");
1438
1439
1440
1441
	  RtsFlags.GranFlags.DoBulkFetching=rtsTrue;
	  break;
	  
        case 'M':
1442
	  debugBelch("Thread migration enabled.\n");
1443
1444
1445
1446
	  RtsFlags.GranFlags.DoThreadMigration=rtsTrue;
	  break;

        case 'R':
1447
	  debugBelch("Fair Scheduling enabled.\n");
1448
1449
1450
1451
	  RtsFlags.GranFlags.DoFairSchedule=rtsTrue;
	  break;
	  
        case 'I':
1452
	  debugBelch("Priority Scheduling enabled.\n");
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
	  RtsFlags.GranFlags.DoPriorityScheduling=rtsTrue;
	  break;

        case 'T':
	  RtsFlags.GranFlags.DoStealThreadsFirst=rtsTrue;
	  RtsFlags.GranFlags.DoThreadMigration=rtsTrue;
	  break;
	  
        case 'Z':
	  RtsFlags.GranFlags.DoAsyncFetch=rtsTrue;
	  break;
	  
/*          case 'z': */
/*  	  RtsFlags.GranFlags.SimplifiedFetch=rtsTrue; */
/*  	  break; */
	  
        case 'N':
	  RtsFlags.GranFlags.PreferSparksOfLocalNodes=rtsTrue;
	  break;
	  
        case 'b':
	  RtsFlags.GranFlags.GranSimStats.Binary=rtsTrue;
	  break;
	  
        case 'P':
	  /* format is -bP<c> where <c> is one char describing kind of profile */
	  RtsFlags.GranFlags.GranSimStats.Full = rtsTrue;
	  switch(rts_argv[arg][3]) {
	  case '\0': break; // nothing special, just an ordinary profile
	  case '0': RtsFlags.GranFlags.GranSimStats.Suppressed = rtsTrue;
	    break;
	  case 'b': RtsFlags.GranFlags.GranSimStats.Binary = rtsTrue;
	    break;
	  case 's': RtsFlags.GranFlags.GranSimStats.Sparks = rtsTrue;
	    break;
	  case 'h': RtsFlags.GranFlags.GranSimStats.Heap = rtsTrue;
	    break;
	  case 'n': RtsFlags.GranFlags.GranSimStats.NewLogfile = rtsTrue;
	    break;
	  case 'g': RtsFlags.GranFlags.GranSimStats.Global = rtsTrue;
	    break;
	  default: barf("Unknown option -bP%c", rts_argv[arg][3]);
	  }
	  break;

        case 's':
	  RtsFlags.GranFlags.GranSimStats.Sparks=rtsTrue;
	  break;

        case 'h':
	  RtsFlags.GranFlags.GranSimStats.Heap=rtsTrue;
	  break;

        case 'Y':   /* syntax: -bY<n>[,<n>]  n ... pos int */ 
	  if (rts_argv[arg][3] != '\0') {
	    char *arg0, *tmp;
	    
	    arg0 = rts_argv[arg]+3;
	    if ((tmp = strstr(arg0,","))==NULL) {
	      RtsFlags.GranFlags.SparkPriority = decode(arg0);
1513
	      debugBelch("SparkPriority: %u.\n",RtsFlags.GranFlags.SparkPriority);
1514
1515
1516
1517
	    } else {
	      *(tmp++) = '\0'; 
	      RtsFlags.GranFlags.SparkPriority = decode(arg0);
	      RtsFlags.GranFlags.SparkPriority2 = decode(tmp);