RtsFlags.c 50.6 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2 3
 *
 * (c) The AQUA Project, Glasgow University, 1994-1997
Simon Marlow's avatar
Simon Marlow committed
4
 * (c) The GHC Team, 1998-2006
5 6 7 8 9
 *
 * Functions for parsing the argument list.
 *
 * ---------------------------------------------------------------------------*/

10
#include "PosixSource.h"
11
#include "Rts.h"
Simon Marlow's avatar
Simon Marlow committed
12

13
#include "RtsOpts.h"
14
#include "RtsUtils.h"
15
#include "Profiling.h"
16
#include "RtsFlags.h"
17

18 19
#ifdef HAVE_CTYPE_H
#include <ctype.h>
20 21
#endif

22 23
#include <string.h>

24 25
// Flag Structure
RTS_FLAGS RtsFlags;
26 27 28 29

/*
 * Split argument lists
 */
sof's avatar
sof committed
30
int     prog_argc = 0;    /* an "int" so as to match normal "argc" */
31
char  **prog_argv = NULL;
32 33
int     full_prog_argc = 0;    /* an "int" so as to match normal "argc" */
char  **full_prog_argv = NULL;
sof's avatar
sof committed
34
char   *prog_name = NULL; /* 'basename' of prog_argv[0] */
35
int     rts_argc = 0;  /* ditto */
36
char  **rts_argv = NULL;
37 38 39 40 41 42 43 44
#if defined(mingw32_HOST_OS)
// On Windows, we want to use GetCommandLineW rather than argc/argv,
// but we need to mutate the command line arguments for withProgName and
// friends. The System.Environment module achieves that using this bit of
// shared state:
int       win32_prog_argc = 0;
wchar_t **win32_prog_argv = NULL;
#endif
45 46 47 48 49 50 51 52 53 54 55

/*
 * constants, used later 
 */
#define RTS 1
#define PGM 0

/* -----------------------------------------------------------------------------
   Static function decls
   -------------------------------------------------------------------------- */

56 57 58 59 60 61 62 63 64 65 66 67 68
static void procRtsOpts      (int rts_argc0, RtsOptsEnabledEnum enabled);

static void normaliseRtsOpts (void);

static void initStatsFile    (FILE *f);

static int  openStatsFile    (char *filename, const char *FILENAME_FMT,
                              FILE **file_ret);

static StgWord64 decodeSize  (const char *flag, nat offset,
                              StgWord64 min, StgWord64 max);

static void bad_option       (const char *s);
69

70 71 72
#ifdef TRACING
static void read_trace_flags(char *arg);
#endif
73

74 75
static void errorUsage      (void) GNU_ATTRIBUTE(__noreturn__);

76 77 78 79
static char *  copyArg  (char *arg);
static char ** copyArgv (int argc, char *argv[]);
static void    freeArgv (int argc, char *argv[]);

80 81 82 83 84 85 86 87 88
/* -----------------------------------------------------------------------------
 * Command-line option parsing routines.
 * ---------------------------------------------------------------------------*/

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

89
    RtsFlags.GcFlags.maxStkSize		= (8 * 1024 * 1024) / sizeof(W_);
90
    RtsFlags.GcFlags.initialStkSize	= 1024 / sizeof(W_);
91 92
    RtsFlags.GcFlags.stkChunkSize       = (32 * 1024) / sizeof(W_);
    RtsFlags.GcFlags.stkChunkBufferSize = (1 * 1024) / sizeof(W_);
93

94
    RtsFlags.GcFlags.minAllocAreaSize   = (512 * 1024)        / BLOCK_SIZE;
95
    RtsFlags.GcFlags.minOldGenSize      = (1024 * 1024)       / BLOCK_SIZE;
96
    RtsFlags.GcFlags.maxHeapSize	= 0;    /* off by default */
97
    RtsFlags.GcFlags.heapSizeSuggestion	= 0;    /* none */
98
    RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsFalse;
99
    RtsFlags.GcFlags.pcFreeHeap		= 3;	/* 3% */
100
    RtsFlags.GcFlags.oldGenFactor       = 2;
101
    RtsFlags.GcFlags.generations        = 2;
102
    RtsFlags.GcFlags.squeezeUpdFrames	= rtsTrue;
103
    RtsFlags.GcFlags.compact            = rtsFalse;
104
    RtsFlags.GcFlags.compactThreshold   = 30.0;
105
    RtsFlags.GcFlags.sweep              = rtsFalse;
106 107 108
#ifdef RTS_GTK_FRONTPANEL
    RtsFlags.GcFlags.frontpanel         = rtsFalse;
#endif
109
    RtsFlags.GcFlags.idleGCDelayTime    = 300; /* millisecs */
110

111 112 113 114 115 116 117 118 119 120 121 122
#if osf3_HOST_OS
/* ToDo: Perhaps by adjusting this value we can make linking without
 * -static work (i.e., not generate a core-dumping executable)? */
# if SIZEOF_VOID_P == 8
    RtsFlags.GcFlags.heapBase           = 0x180000000L;
# else
#  error I have no idea where to begin the heap on a non-64-bit osf3 machine.
# endif
#else
    RtsFlags.GcFlags.heapBase           = 0;   /* means don't care */
#endif

123 124
#ifdef DEBUG
    RtsFlags.DebugFlags.scheduler	= rtsFalse;
125
    RtsFlags.DebugFlags.interpreter	= rtsFalse;
126 127 128 129 130 131
    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;
132
    RtsFlags.DebugFlags.stm             = rtsFalse;
133
    RtsFlags.DebugFlags.prof		= rtsFalse;
134
    RtsFlags.DebugFlags.apply		= rtsFalse;
135
    RtsFlags.DebugFlags.linker		= rtsFalse;
136
    RtsFlags.DebugFlags.squeeze		= rtsFalse;
137
    RtsFlags.DebugFlags.hpc		= rtsFalse;
138
    RtsFlags.DebugFlags.sparks		= rtsFalse;
139 140
#endif

Simon Marlow's avatar
Simon Marlow committed
141
#if defined(PROFILING)
142
    RtsFlags.CcFlags.doCostCentres	= 0;
Simon Marlow's avatar
Simon Marlow committed
143
#endif /* PROFILING */
144

145
    RtsFlags.ProfFlags.doHeapProfile      = rtsFalse;
146
    RtsFlags.ProfFlags.profileInterval    = 100;
147 148

#ifdef PROFILING
149
    RtsFlags.ProfFlags.includeTSOs        = rtsFalse;
150
    RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
151
    RtsFlags.ProfFlags.maxRetainerSetSize = 8;
ravi@bluespec.com's avatar
ravi@bluespec.com committed
152
    RtsFlags.ProfFlags.ccsLength          = 25;
153 154 155 156
    RtsFlags.ProfFlags.modSelector        = NULL;
    RtsFlags.ProfFlags.descrSelector      = NULL;
    RtsFlags.ProfFlags.typeSelector       = NULL;
    RtsFlags.ProfFlags.ccSelector         = NULL;
157
    RtsFlags.ProfFlags.ccsSelector        = NULL;
158 159
    RtsFlags.ProfFlags.retainerSelector   = NULL;
    RtsFlags.ProfFlags.bioSelector        = NULL;
160 161
#endif

162
#ifdef TRACING
163
    RtsFlags.TraceFlags.tracing       = TRACE_NONE;
164 165
    RtsFlags.TraceFlags.timestamp     = rtsFalse;
    RtsFlags.TraceFlags.scheduler     = rtsFalse;
166
    RtsFlags.TraceFlags.gc            = rtsFalse;
167 168
    RtsFlags.TraceFlags.sparks_sampled= rtsFalse;
    RtsFlags.TraceFlags.sparks_full   = rtsFalse;
Simon Marlow's avatar
Simon Marlow committed
169 170
#endif

171 172
    RtsFlags.MiscFlags.tickInterval	= 20;  /* In milliseconds */
    RtsFlags.ConcFlags.ctxtSwitchTime	= 20;  /* In milliseconds */
173

174
    RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
Ian Lynagh's avatar
Ian Lynagh committed
175
    RtsFlags.MiscFlags.machineReadable = rtsFalse;
176
    RtsFlags.MiscFlags.linkerMemBase    = 0;
177

178
#ifdef THREADED_RTS
179
    RtsFlags.ParFlags.nNodes	        = 1;
180
    RtsFlags.ParFlags.migrate           = rtsTrue;
181
    RtsFlags.ParFlags.parGcEnabled      = 1;
182 183 184
    RtsFlags.ParFlags.parGcGen          = 0;
    RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
    RtsFlags.ParFlags.parGcLoadBalancingGen = 1;
185
    RtsFlags.ParFlags.setAffinity       = 0;
186
#endif
187

Simon Marlow's avatar
Simon Marlow committed
188
#if defined(THREADED_RTS)
189
    RtsFlags.ParFlags.maxLocalSparks	= 4096;
Simon Marlow's avatar
Simon Marlow committed
190
#endif /* THREADED_RTS */
191 192

#ifdef TICKY_TICKY
193 194
    RtsFlags.TickyFlags.showTickyStats	 = rtsFalse;
    RtsFlags.TickyFlags.tickyFile	 = NULL;
195
#endif
Simon Marlow's avatar
Simon Marlow committed
196

197
#ifdef USE_PAPI
198 199
    /* By default no special measurements taken */
    RtsFlags.PapiFlags.eventType        = 0;
200
    RtsFlags.PapiFlags.numUserEvents    = 0;
201
#endif
202 203 204 205 206 207 208 209 210 211 212 213 214 215
}

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:",
"",
216
"  -?       Prints this message and exits; the program is not executed",
217
"  --info   Print information about the RTS used by this program",
218
"",
219
"  -K<size> Sets the maximum stack size (default 8M)  Egs: -K32k   -K512k",
220 221 222
"  -ki<size> Sets the initial thread stack size (default 1k)  Egs: -ki4k -ki2m",
"  -kc<size> Sets the stack chunk size (default 32k)",
"  -kb<size> Sets the stack chunk buffer size (default 1k)",
223
"",
224
"  -A<size> Sets the minimum allocation area size (default 512k) Egs: -A1m -A10k",
225
"  -M<size> Sets the maximum heap size (default unlimited)  Egs: -M256k -M1G",
226
"  -H<size> Sets the minimum heap size (default 0M)   Egs: -H24m  -H1G",
227
"  -m<n>    Minimum % of heap which must be available (default 3%)",
228
"  -G<n>    Number of generations (default: 2)",
229 230 231 232 233
"  -c<n>    Use in-place compaction instead of copying in the oldest generation",
"           when live data is at least <n>% of the maximum heap size set with",
"           -M (default: 30%)",
"  -c       Use in-place compaction for all oldest generation collections",
"           (the default is to use copying)",
234
"  -w       Use mark-region for the oldest generation (experimental)",
235
#if defined(THREADED_RTS)
236 237
"  -I<sec>  Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
#endif
238
"",
239 240 241
"  -t[<file>] One-line GC statistics (if <file> omitted, uses stderr)",
"  -s[<file>] Summary  GC statistics (if <file> omitted, uses stderr)",
"  -S[<file>] Detailed GC statistics (if <file> omitted, uses stderr)",
242 243 244
#ifdef RTS_GTK_FRONTPANEL
"  -f       Display front panel (requires X11 & GTK+)",
#endif
245 246 247 248
"",
"",
"  -Z       Don't squeeze out update frames on stack overflow",
"  -B       Sound the bell at the start of each garbage collection",
Simon Marlow's avatar
Simon Marlow committed
249
#if defined(PROFILING)
250
"",
251
"  -px      Time/allocation profile (XML)  (output file <program>.prof)",
andy's avatar
andy committed
252 253 254
"  -p       Time/allocation profile        (output file <program>.prof)",
"  -P       More detailed Time/Allocation profile",
"  -Pa      Give information about *all* cost centres",
255

256 257
# if defined(PROFILING)
"",
258 259 260 261 262 263 264 265
"  -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)",
266
"  A subset of closures may be selected thusly:",
267 268
"    -hc<cc>,...  specific cost centre(s) (top of stack only)",
"    -hC<cc>,...  specific cost centre(s) (anywhere in stack)",
269 270 271 272 273
"    -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)",
274 275
"",
"  -R<size>       Set the maximum retainer set size (default: 8)",
ravi@bluespec.com's avatar
ravi@bluespec.com committed
276 277 278
"", 
"  -L<chars>      Maximum length of a cost-centre stack in a heap profile",
"                 (default: 25)",
279
"",
280 281
"  -xt            Include threads (TSOs) in a heap profile",
"",
282
"  -xc      Show current cost centre stack on raising an exception",
283 284
# endif
#endif /* PROFILING or PAR */
Simon Marlow's avatar
Simon Marlow committed
285

286
#ifdef TRACING
Simon Marlow's avatar
Simon Marlow committed
287
"",
288 289 290 291 292 293
"  -l[flags]  Log events in binary format to the file <program>.eventlog",
#  ifdef DEBUG
"  -v[flags]  Log events to stderr",
#  endif
"             where [flags] can contain:",
"                s    scheduler events",
294
"                g    GC events",
295 296
"                p    par spark events (sampled)",
"                f    par spark events (full detail)",
297 298 299
#  ifdef DEBUG
"                t    add time stamps (only useful with -v)",
#  endif
Simon Marlow's avatar
Simon Marlow committed
300 301
#endif

302
#if !defined(PROFILING)
303
"",
304
"  -hT      Heap residency profile (output file <program>.hp)",
305
#endif
306
"  -i<sec>  Time between heap samples (seconds, default: 0.1)",
307 308
"",
#if defined(TICKY_TICKY)
309
"  -r<file>  Produce ticky-ticky statistics (with -rstderr for stderr)",
310 311
"",
#endif
312 313 314
"  -C<secs>  Context-switch interval in seconds.",
"            0 or no argument means switch as often as possible.",
"            Default: 0.02 sec; resolution is set by -V below.",
315
"  -V<secs>  Master tick interval in seconds (0 == disable timer).",
316 317
"            This sets the resolution for -C and the profile timer -i.",
"            Default: 0.02 sec.",
318 319 320
"",
#if defined(DEBUG)
"  -Ds  DEBUG: scheduler",
321
"  -Di  DEBUG: interpreter",
322 323 324 325 326 327 328
"  -Dw  DEBUG: weak",
"  -DG  DEBUG: gccafs",
"  -Dg  DEBUG: gc",
"  -Db  DEBUG: block",
"  -DS  DEBUG: sanity",
"  -Dt  DEBUG: stable",
"  -Dp  DEBUG: prof",
Simon Marlow's avatar
Simon Marlow committed
329
"  -De  DEBUG: event logging",
330
"  -Da  DEBUG: apply",
331
"  -Dl  DEBUG: linker",
332
"  -Dm  DEBUG: stm",
Simon Marlow's avatar
Simon Marlow committed
333
"  -Dz  DEBUG: stack squeezing",
334
"  -Dc  DEBUG: program coverage",
335 336
"  -Dr  DEBUG: sparks",
"",
337
"     NOTE: DEBUG events are sent to stderr by default; add -l to create a",
338
"     binary event log file instead.",
339
"",
340
#endif /* DEBUG */
341
#if defined(THREADED_RTS) && !defined(NOSMP)
342 343
"  -N<n>     Use <n> processors (default: 1)",
"  -N        Determine the number of processors to use automatically",
344 345 346 347 348
"  -qg[<n>]  Use parallel GC only for generations >= <n>",
"            (default: 0, -qg alone turns off parallel GC)",
"  -qb[<n>]  Use load-balancing in the parallel GC only for generations >= <n>",
"            (default: 1, -qb alone turns off load-balancing)",
"  -qa       Use the OS to set thread affinity (experimental)",
349
"  -qm       Don't automatically migrate threads between CPUs",
350
#endif
351 352
"  --install-signal-handlers=<yes|no>",
"            Install signal handlers (default: yes)",
Simon Marlow's avatar
Simon Marlow committed
353
#if defined(THREADED_RTS)
354 355
"  -e<n>     Maximum number of outstanding local sparks (default: 4096)",
#endif
356 357 358 359
#if defined(x86_64_HOST_ARCH)
"  -xm       Base address to mmap memory in the GHCi linker",
"            (hex; must be <80000000)",
#endif
360
#if defined(USE_PAPI)
361 362
"  -aX       CPU performance counter measurements using PAPI",
"            (use with the -s<file> option).  X is one of:",
363 364 365 366 367 368
"",
/* "            y - cycles", */
"            1 - level 1 cache misses",
"            2 - level 2 cache misses",
"            b - branch mispredictions",
"            s - stalled cycles",
369
"            e - cache miss and branch misprediction events",
370 371
"            +PAPI_EVENT   - collect papi preset event PAPI_EVENT",
"            #NATIVE_EVENT - collect native event NATIVE_EVENT (in hex)",
372
#endif
373
"",
374 375
"RTS options may also be specified using the GHCRTS environment variable.",
"",
376 377 378 379 380 381
"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
382
STATIC_INLINE rtsBool
383 384 385 386 387
strequal(const char *a, const char * b)
{
    return(strcmp(a, b) == 0);
}

388
static void splitRtsFlags(char *s)
389 390 391 392 393 394 395 396 397 398 399
{
    char *c1, *c2;

    c1 = s;
    do {
	while (isspace(*c1)) { c1++; };
	c2 = c1;
	while (!isspace(*c2) && *c2 != '\0') { c2++; };
	
	if (c1 == c2) { break; }
	
400 401 402 403 404
        s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
        strncpy(s, c1, c2-c1);
        s[c2-c1] = '\0';
        rts_argv[rts_argc++] = s;

405 406 407 408
	c1 = c2;
    } while (*c1 != '\0');
}
    
409 410 411 412 413 414 415
/* -----------------------------------------------------------------------------
   Parse the command line arguments, collecting options for the RTS.

   On return:
     - argv[] is *modified*, any RTS options have been stripped out
     - *argc  contains the new count of arguments in argv[]

416
     - rts_argv[]  (global) contains a copy of the collected RTS args
417 418
     - rts_argc    (global) contains the count of args in rts_argv

419
     - prog_argv[] (global) contains a copy of the non-RTS args (== argv)
420 421
     - prog_argc   (global) contains the count of args in prog_argv

422
     - prog_name   (global) contains the basename of prog_argv[0]
423 424 425 426

  -------------------------------------------------------------------------- */

void setupRtsFlags (int *argc, char *argv[])
427
{
428 429 430
    nat mode;
    nat total_arg;
    nat arg, rts_argc0;
431

432
    setProgName (argv);
433 434 435 436
    total_arg = *argc;
    arg = 1;

    *argc = 1;
437 438
    rts_argc = 0;

439 440
    rts_argv = stgCallocBytes(total_arg + 1, sizeof (char *), "setupRtsFlags");

441
    rts_argc0 = rts_argc;
442

443 444 445 446 447
    // 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) {
448 449 450 451 452
            splitRtsFlags(ghc_rts_opts);
            // opts from ghc_rts_opts are always enabled:
            procRtsOpts(rts_argc0, RtsOptsAll);
            rts_argc0 = rts_argc;
        }
453 454
    }

455
    // process arguments from the GHCRTS environment variable next
456
    // (arguments from the command line override these).
457 458 459 460
    {
	char *ghc_rts = getenv("GHCRTS");

	if (ghc_rts != NULL) {
461
            if (rtsOptsEnabled == RtsOptsNone) {
462
                errorBelch("Warning: Ignoring GHCRTS variable as RTS options are disabled.\n         Link with -rtsopts to enable them.");
463
                // We don't actually exit, just warn
464 465 466 467
            } else {
                splitRtsFlags(ghc_rts);
                procRtsOpts(rts_argc0, rtsOptsEnabled);
                rts_argc0 = rts_argc;
468
            }
469
        }
470
    }
471

472 473 474
    // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
    //   argv[0] must be PGM argument -- leave in argv

475 476 477 478 479 480 481 482 483 484 485 486
    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])) {
487 488
            mode = RTS;
        }
489 490 491
	else if (strequal("-RTS", argv[arg])) {
	    mode = PGM;
	}
492 493
        else if (mode == RTS) {
            rts_argv[rts_argc++] = copyArg(argv[arg]);
Ian Lynagh's avatar
Ian Lynagh committed
494
        }
495 496
        else {
            argv[(*argc)++] = argv[arg];
497 498
	}
    }
499 500 501
    // process remaining program arguments
    for (; arg < total_arg; arg++) {
	argv[(*argc)++] = argv[arg];
502 503
    }
    argv[*argc] = (char *) 0;
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    rts_argv[rts_argc] = (char *) 0;

    procRtsOpts(rts_argc0, rtsOptsEnabled);

    normaliseRtsOpts();

    setProgArgv(*argc, argv);

    if (RtsFlags.GcFlags.statsFile != NULL) {
        initStatsFile (RtsFlags.GcFlags.statsFile);
    }
    if (RtsFlags.TickyFlags.tickyFile != NULL) {
        initStatsFile (RtsFlags.GcFlags.statsFile);
    }
}

/* -----------------------------------------------------------------------------
 * procRtsOpts: Process rts_argv between rts_argc0 and rts_argc.
 * -------------------------------------------------------------------------- */

static void procRtsOpts (int rts_argc0, RtsOptsEnabledEnum enabled)
{
    rtsBool error = rtsFalse;
    int arg;
528 529

    // Process RTS (rts_argv) part: mainly to determine statsfile
530 531
    for (arg = rts_argc0; arg < rts_argc; arg++) {
        if (rts_argv[arg][0] != '-') {
532
	    fflush(stdout);
533
	    errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
534 535 536
	    error = rtsTrue;

        } else {
Simon Marlow's avatar
Simon Marlow committed
537

538 539 540 541 542
            if (enabled == RtsOptsNone) {
                errorBelch("RTS options are disabled. Link with -rtsopts to enable them.");
                stg_exit(EXIT_FAILURE);
            }

Simon Marlow's avatar
Simon Marlow committed
543 544 545 546 547 548 549 550 551 552 553
            switch(rts_argv[arg][1]) {
            case '-':
                if (strequal("info", &rts_argv[arg][2])) {
                    printRtsInfo();
                    stg_exit(0);
                }
                break;
            default:
                break;
            }

554
            if (enabled == RtsOptsSafeOnly) {
Simon Marlow's avatar
Simon Marlow committed
555 556 557 558 559
                errorBelch("Most RTS options are disabled. Link with -rtsopts to enable them.");
                stg_exit(EXIT_FAILURE);
            }

            switch(rts_argv[arg][1]) {
560

Simon Marlow's avatar
Simon Marlow committed
561 562 563 564 565
	      /* process: general args, then PROFILING-only ones, then
		 CONCURRENT-only, TICKY-only (same order as defined in
		 RtsFlags.lh); within those groups, mostly in
		 case-insensitive alphabetical order.  Final group is
		 x*, which allows for more options.
566 567 568 569 570 571
	      */

#ifdef TICKY_TICKY
# define TICKY_BUILD_ONLY(x) x
#else
# define TICKY_BUILD_ONLY(x) \
572
errorBelch("the flag %s requires the program to be built with -ticky", rts_argv[arg]); \
573 574 575 576 577 578 579
error = rtsTrue;
#endif

#ifdef PROFILING
# define PROFILING_BUILD_ONLY(x)   x
#else
# define PROFILING_BUILD_ONLY(x) \
580
errorBelch("the flag %s requires the program to be built with -prof", rts_argv[arg]); \
581 582 583
error = rtsTrue;
#endif

584 585
#ifdef TRACING
# define TRACING_BUILD_ONLY(x)   x
Simon Marlow's avatar
Simon Marlow committed
586
#else
587
# define TRACING_BUILD_ONLY(x) \
588
errorBelch("the flag %s requires the program to be built with -eventlog or -debug", rts_argv[arg]); \
Simon Marlow's avatar
Simon Marlow committed
589 590 591
error = rtsTrue;
#endif

592 593 594 595
#ifdef THREADED_RTS
# define THREADED_BUILD_ONLY(x)      x
#else
# define THREADED_BUILD_ONLY(x) \
596 597 598 599 600 601 602 603 604
errorBelch("the flag %s requires the program to be built with -threaded", rts_argv[arg]); \
error = rtsTrue;
#endif

#ifdef DEBUG
# define DEBUG_BUILD_ONLY(x) x
#else
# define DEBUG_BUILD_ONLY(x) \
errorBelch("the flag %s requires the program to be built with -debug", rts_argv[arg]); \
605
error = rtsTrue;
606 607 608 609 610 611 612
#endif

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

613 614 615 616 617 618 619 620 621 622 623 624
              /* This isn't going to allow us to keep related options
                 together as we add more --* flags. We really need a
                 proper options parser. */
	      case '-':
                  if (strequal("install-signal-handlers=yes",
                               &rts_argv[arg][2])) {
                      RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
                  }
                  else if (strequal("install-signal-handlers=no",
                               &rts_argv[arg][2])) {
                      RtsFlags.MiscFlags.install_signal_handlers = rtsFalse;
                  }
Ian Lynagh's avatar
Ian Lynagh committed
625 626 627 628
                  else if (strequal("machine-readable",
                               &rts_argv[arg][2])) {
                      RtsFlags.MiscFlags.machineReadable = rtsTrue;
                  }
629 630
                  else if (strequal("info",
                               &rts_argv[arg][2])) {
631
                      printRtsInfo();
632
                      stg_exit(0);
633
                  }
634 635 636 637 638
                  else {
		      errorBelch("unknown RTS option: %s",rts_argv[arg]);
		      error = rtsTrue;
                  }
		  break;
639
	      case 'A':
640 641 642 643
                  RtsFlags.GcFlags.minAllocAreaSize
                      = decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_INT_MAX)
                           / BLOCK_SIZE;
                  break;
644

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
#ifdef USE_PAPI
	      case 'a':
		switch(rts_argv[arg][2]) {
		case '1':
		  RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L1;
		  break;
		case '2':
		  RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L2;
		  break;
		case 'b':
		  RtsFlags.PapiFlags.eventType = PAPI_FLAG_BRANCH;
		  break;
		case 's':
		  RtsFlags.PapiFlags.eventType = PAPI_FLAG_STALLS;
		  break;
660 661 662
		case 'e':
		  RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
		  break;
663
                case '+':
664
                case '#':
665 666 667 668
                  if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
                      errorBelch("maximum number of PAPI events reached");
                      stg_exit(EXIT_FAILURE);
                  }
669 670 671 672 673
                  nat eventNum  = RtsFlags.PapiFlags.numUserEvents++;
                  char kind     = rts_argv[arg][2];
                  nat eventKind = kind == '+' ? PAPI_PRESET_EVENT_KIND : PAPI_NATIVE_EVENT_KIND;

                  RtsFlags.PapiFlags.userEvents[eventNum] = rts_argv[arg] + 3;
674
                  RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
675
                  RtsFlags.PapiFlags.userEventsKind[eventNum] = eventKind;
676
                  break;
677 678 679 680 681 682
		default:
		  bad_option( rts_argv[arg] );
		}
		break;
#endif

683 684 685 686
	      case 'B':
		RtsFlags.GcFlags.ringBell = rtsTrue;
		break;

687
	      case 'c':
688 689 690 691
		  if (rts_argv[arg][2] != '\0') {
		      RtsFlags.GcFlags.compactThreshold =
			  atof(rts_argv[arg]+2);
		  } else {
692
		      RtsFlags.GcFlags.compact = rtsTrue;
693 694
		  }
		  break;
695

696 697 698 699
              case 'w':
		RtsFlags.GcFlags.sweep = rtsTrue;
		break;

700 701 702 703 704 705 706
	      case 'F':
	        RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
	      
		if (RtsFlags.GcFlags.oldGenFactor < 0)
		  bad_option( rts_argv[arg] );
		break;
	      
707
	      case 'D':
708
              DEBUG_BUILD_ONLY(
709 710 711 712 713 714 715 716
	      { 
		  char *c;

		  for (c  = rts_argv[arg] + 2; *c != '\0'; c++) {
		      switch (*c) {
		      case 's':
			  RtsFlags.DebugFlags.scheduler = rtsTrue;
			  break;
717 718
		      case 'i':
			  RtsFlags.DebugFlags.interpreter = rtsTrue;
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
			  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 'l':
			  RtsFlags.DebugFlags.linker = rtsTrue;
			  break;
744 745 746
		      case 'a':
			  RtsFlags.DebugFlags.apply = rtsTrue;
			  break;
747 748 749
		      case 'm':
			  RtsFlags.DebugFlags.stm = rtsTrue;
			  break;
750 751 752
		      case 'z':
			  RtsFlags.DebugFlags.squeeze = rtsTrue;
			  break;
753 754 755
		      case 'c':
			  RtsFlags.DebugFlags.hpc = rtsTrue;
			  break;
756 757 758
		      case 'r':
			  RtsFlags.DebugFlags.sparks = rtsTrue;
			  break;
759 760 761 762
		      default:
			  bad_option( rts_argv[arg] );
		      }
		  }
763 764
                  // -Dx also turns on -v.  Use -l to direct trace
                  // events to the .eventlog file instead.
765
                  RtsFlags.TraceFlags.tracing = TRACE_STDERR;
766 767
	      })
              break;
768 769

	      case 'K':
770
                  RtsFlags.GcFlags.maxStkSize =
771
                      decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
772
                  break;
773 774

	      case 'k':
775 776 777 778 779 780 781 782 783 784 785 786 787 788
		switch(rts_argv[arg][2]) {
                case 'c':
                  RtsFlags.GcFlags.stkChunkSize =
                      decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
                  break;
                case 'b':
                  RtsFlags.GcFlags.stkChunkBufferSize =
                      decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
                  break;
                case 'i':
                  RtsFlags.GcFlags.initialStkSize =
                      decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
                  break;
                default:
789
                  RtsFlags.GcFlags.initialStkSize =
790
                      decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
791
                  break;
792 793
                }
                break;
794

795
              case 'M':
796 797 798 799
                  RtsFlags.GcFlags.maxHeapSize =
                      decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE;
                  /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
                  break;
800 801

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

804 805 806 807
                  if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
                      RtsFlags.GcFlags.pcFreeHeap > 100)
                      bad_option( rts_argv[arg] );
                  break;
808

809
	      case 'G':
810 811 812
                  RtsFlags.GcFlags.generations =
                      decodeSize(rts_argv[arg], 2, 1, HS_INT_MAX);
                  break;
813

814
	      case 'H':
815 816 817 818 819 820 821
                  if (rts_argv[arg][2] == '\0') {
                      RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsTrue;
                  } else {
                      RtsFlags.GcFlags.heapSizeSuggestion =
                          (nat)(decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE);
                  }
                  break;
822

823 824 825 826 827 828
#ifdef RTS_GTK_FRONTPANEL
	      case 'f':
		  RtsFlags.GcFlags.frontpanel = rtsTrue;
		  break;
#endif

829 830 831 832 833 834
    	      case 'I':	/* idle GC delay */
		if (rts_argv[arg][2] == '\0') {
		  /* use default */
		} else {
		    I_ cst; /* tmp */

835
		    /* Convert to millisecs */
836
		    cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
837
		    RtsFlags.GcFlags.idleGCDelayTime = cst;
838 839 840
		}
		break;

841
	      case 'S':
842 843
		  RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
		  goto stats;
844

845
	      case 's':
846 847
		  RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
		  goto stats;
848 849

	      case 't':
850 851
		  RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
		  goto stats;
852

853
	    stats:
854 855
		{ 
		    int r;
856 857
                    r = openStatsFile(rts_argv[arg]+2, NULL,
                                      &RtsFlags.GcFlags.statsFile);
858 859
		    if (r == -1) { error = rtsTrue; }
		}
Simon Marlow's avatar
Simon Marlow committed
860
                break;
861 862 863 864 865 866 867 868 869

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

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

	      case 'P': /* detailed cost centre profiling (time/alloc) */
	      case 'p': /* cost centre profiling (time/alloc) */
Simon Marlow's avatar
Simon Marlow committed
870
		PROFILING_BUILD_ONLY(
871
		switch (rts_argv[arg][2]) {
872 873
		  case 'x':
		    RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
874
		    break;
andy's avatar
andy committed
875 876 877
		  case 'a':
		    RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
		    break;
878
		  default:
879 880 881 882 883 884 885 886
		      if (rts_argv[arg][1] == 'P') {
			  RtsFlags.CcFlags.doCostCentres =
			      COST_CENTRES_VERBOSE;
		      } else {
			  RtsFlags.CcFlags.doCostCentres =
			      COST_CENTRES_SUMMARY;
		      }
		      break;
887 888 889
		}
		) break;

890 891 892 893
	      case 'R':
		  PROFILING_BUILD_ONLY(
		      RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
  	          ) break;
ravi@bluespec.com's avatar
ravi@bluespec.com committed
894 895 896 897 898 899 900
	      case 'L':
		  PROFILING_BUILD_ONLY(
		      RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
                      if(RtsFlags.ProfFlags.ccsLength <= 0) {
			bad_option(rts_argv[arg]);
                      }
		  ) break;
901
	      case 'h': /* serial heap profile */
902
#if !defined(PROFILING)
903 904 905 906 907 908
		switch (rts_argv[arg][2]) {
		  case '\0':
		  case 'T':
		    RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
		    break;
		  default:
909
		    errorBelch("invalid heap profile option: %s",rts_argv[arg]);
910 911 912 913 914
		    error = rtsTrue;
		}
#else
		PROFILING_BUILD_ONLY(
		switch (rts_argv[arg][2]) {
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
		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], '}');
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948

			    // 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;
949 950 951
			    case 'C':
				RtsFlags.ProfFlags.ccsSelector = left;
				break;
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
			    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;
972 973 974 975
			    }
			}
			break;
		    }
976

977
		    if (RtsFlags.ProfFlags.doHeapProfile != 0) {
978
			errorBelch("multiple heap profile options");
979 980 981 982 983 984 985 986 987 988 989 990
			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':
991 992
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
			  break;
993 994
		    case 'D':
		    case 'd':
995 996
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
			  break;
997 998
		    case 'Y':
		    case 'y':
999 1000
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
			  break;
1001 1002
		    case 'R':
		    case 'r':
1003 1004
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
			  break;
simonmar's avatar