RtsFlags.c 43.2 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

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

21 22
#include <string.h>

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

/*
 * Split argument lists
 */
sof's avatar
sof committed
29
int     prog_argc = 0;    /* an "int" so as to match normal "argc" */
30
char  **prog_argv = NULL;
31 32
int     full_prog_argc = 0;    /* an "int" so as to match normal "argc" */
char  **full_prog_argv = NULL;
sof's avatar
sof committed
33
char   *prog_name = NULL; /* 'basename' of prog_argv[0] */
34
int     rts_argc = 0;  /* ditto */
35 36 37 38 39 40 41 42 43 44 45 46
char   *rts_argv[MAX_RTS_ARGS];

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

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

47
static int		/* return NULL on error */
48 49 50 51
open_stats_file (
    I_ arg,
    int argc, char *argv[],
    int rts_argc, char *rts_argv[],
52 53
    const char *FILENAME_FMT,
    FILE **file_ret);
54

55
static StgWord64 decodeSize(const char *flag, nat offset, StgWord64 min, StgWord64 max);
56
static void bad_option(const char *s);
57 58 59
#ifdef TRACING
static void read_trace_flags(char *arg);
#endif
60 61 62 63 64 65 66 67 68 69

/* -----------------------------------------------------------------------------
 * Command-line option parsing routines.
 * ---------------------------------------------------------------------------*/

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

70
    RtsFlags.GcFlags.maxStkSize		= (8 * 1024 * 1024) / sizeof(W_);
71
    RtsFlags.GcFlags.initialStkSize	= 1024 / sizeof(W_);
72 73
    RtsFlags.GcFlags.stkChunkSize       = (32 * 1024) / sizeof(W_);
    RtsFlags.GcFlags.stkChunkBufferSize = (1 * 1024) / sizeof(W_);
74

75
    RtsFlags.GcFlags.minAllocAreaSize   = (512 * 1024)        / BLOCK_SIZE;
76
    RtsFlags.GcFlags.minOldGenSize      = (1024 * 1024)       / BLOCK_SIZE;
77
    RtsFlags.GcFlags.maxHeapSize	= 0;    /* off by default */
78
    RtsFlags.GcFlags.heapSizeSuggestion	= 0;    /* none */
79
    RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsFalse;
80
    RtsFlags.GcFlags.pcFreeHeap		= 3;	/* 3% */
81
    RtsFlags.GcFlags.oldGenFactor       = 2;
82
    RtsFlags.GcFlags.generations        = 2;
83
    RtsFlags.GcFlags.squeezeUpdFrames	= rtsTrue;
84
    RtsFlags.GcFlags.compact            = rtsFalse;
85
    RtsFlags.GcFlags.compactThreshold   = 30.0;
86
    RtsFlags.GcFlags.sweep              = rtsFalse;
87 88 89
#ifdef RTS_GTK_FRONTPANEL
    RtsFlags.GcFlags.frontpanel         = rtsFalse;
#endif
90
    RtsFlags.GcFlags.idleGCDelayTime    = 300; /* millisecs */
91

92 93 94 95 96 97 98 99 100 101 102 103
#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

104 105
#ifdef DEBUG
    RtsFlags.DebugFlags.scheduler	= rtsFalse;
106
    RtsFlags.DebugFlags.interpreter	= rtsFalse;
107 108 109 110 111 112
    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;
113
    RtsFlags.DebugFlags.stm             = rtsFalse;
114
    RtsFlags.DebugFlags.prof		= rtsFalse;
115
    RtsFlags.DebugFlags.apply		= rtsFalse;
116
    RtsFlags.DebugFlags.linker		= rtsFalse;
117
    RtsFlags.DebugFlags.squeeze		= rtsFalse;
118
    RtsFlags.DebugFlags.hpc		= rtsFalse;
119
    RtsFlags.DebugFlags.sparks		= rtsFalse;
120 121
#endif

Simon Marlow's avatar
Simon Marlow committed
122
#if defined(PROFILING)
123
    RtsFlags.CcFlags.doCostCentres	= 0;
Simon Marlow's avatar
Simon Marlow committed
124
#endif /* PROFILING */
125

126
    RtsFlags.ProfFlags.doHeapProfile      = rtsFalse;
127
    RtsFlags.ProfFlags.profileInterval    = 100;
128 129

#ifdef PROFILING
130
    RtsFlags.ProfFlags.includeTSOs        = rtsFalse;
131
    RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
132
    RtsFlags.ProfFlags.maxRetainerSetSize = 8;
ravi@bluespec.com's avatar
ravi@bluespec.com committed
133
    RtsFlags.ProfFlags.ccsLength          = 25;
134 135 136 137
    RtsFlags.ProfFlags.modSelector        = NULL;
    RtsFlags.ProfFlags.descrSelector      = NULL;
    RtsFlags.ProfFlags.typeSelector       = NULL;
    RtsFlags.ProfFlags.ccSelector         = NULL;
138
    RtsFlags.ProfFlags.ccsSelector        = NULL;
139 140
    RtsFlags.ProfFlags.retainerSelector   = NULL;
    RtsFlags.ProfFlags.bioSelector        = NULL;
141 142
#endif

143
#ifdef TRACING
144
    RtsFlags.TraceFlags.tracing       = TRACE_NONE;
145 146
    RtsFlags.TraceFlags.timestamp     = rtsFalse;
    RtsFlags.TraceFlags.scheduler     = rtsFalse;
Simon Marlow's avatar
Simon Marlow committed
147 148
#endif

149 150
    RtsFlags.MiscFlags.tickInterval	= 20;  /* In milliseconds */
    RtsFlags.ConcFlags.ctxtSwitchTime	= 20;  /* In milliseconds */
151

152
    RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
Ian Lynagh's avatar
Ian Lynagh committed
153
    RtsFlags.MiscFlags.machineReadable = rtsFalse;
154
    RtsFlags.MiscFlags.linkerMemBase    = 0;
155

156
#ifdef THREADED_RTS
157
    RtsFlags.ParFlags.nNodes	        = 1;
158
    RtsFlags.ParFlags.migrate           = rtsTrue;
159
    RtsFlags.ParFlags.parGcEnabled      = 1;
160 161 162
    RtsFlags.ParFlags.parGcGen          = 0;
    RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
    RtsFlags.ParFlags.parGcLoadBalancingGen = 1;
163
    RtsFlags.ParFlags.setAffinity       = 0;
164
#endif
165

Simon Marlow's avatar
Simon Marlow committed
166
#if defined(THREADED_RTS)
167
    RtsFlags.ParFlags.maxLocalSparks	= 4096;
Simon Marlow's avatar
Simon Marlow committed
168
#endif /* THREADED_RTS */
169 170

#ifdef TICKY_TICKY
171 172
    RtsFlags.TickyFlags.showTickyStats	 = rtsFalse;
    RtsFlags.TickyFlags.tickyFile	 = NULL;
173
#endif
Simon Marlow's avatar
Simon Marlow committed
174

175
#ifdef USE_PAPI
176 177
    /* By default no special measurements taken */
    RtsFlags.PapiFlags.eventType        = 0;
178
    RtsFlags.PapiFlags.numUserEvents    = 0;
179
#endif
180 181 182 183 184 185 186 187 188 189 190 191 192 193
}

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:",
"",
194
"  -?       Prints this message and exits; the program is not executed",
195
"  --info   Print information about the RTS used by this program",
196
"",
197
"  -K<size> Sets the maximum stack size (default 8M)  Egs: -K32k   -K512k",
198 199 200
"  -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)",
201
"",
202
"  -A<size> Sets the minimum allocation area size (default 512k) Egs: -A1m -A10k",
203
"  -M<size> Sets the maximum heap size (default unlimited)  Egs: -M256k -M1G",
204
"  -H<size> Sets the minimum heap size (default 0M)   Egs: -H24m  -H1G",
205
"  -m<n>    Minimum % of heap which must be available (default 3%)",
206
"  -G<n>    Number of generations (default: 2)",
207 208 209 210 211
"  -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)",
212
"  -w       Use mark-region for the oldest generation (experimental)",
213
#if defined(THREADED_RTS)
214 215
"  -I<sec>  Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
#endif
216
"",
217 218 219
"  -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)",
220 221 222
#ifdef RTS_GTK_FRONTPANEL
"  -f       Display front panel (requires X11 & GTK+)",
#endif
223 224 225 226
"",
"",
"  -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
227
#if defined(PROFILING)
228
"",
229
"  -px      Time/allocation profile (XML)  (output file <program>.prof)",
andy's avatar
andy committed
230 231 232
"  -p       Time/allocation profile        (output file <program>.prof)",
"  -P       More detailed Time/Allocation profile",
"  -Pa      Give information about *all* cost centres",
233

234 235
# if defined(PROFILING)
"",
236 237 238 239 240 241 242 243
"  -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)",
244
"  A subset of closures may be selected thusly:",
245 246
"    -hc<cc>,...  specific cost centre(s) (top of stack only)",
"    -hC<cc>,...  specific cost centre(s) (anywhere in stack)",
247 248 249 250 251
"    -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)",
252 253
"",
"  -R<size>       Set the maximum retainer set size (default: 8)",
ravi@bluespec.com's avatar
ravi@bluespec.com committed
254 255 256
"", 
"  -L<chars>      Maximum length of a cost-centre stack in a heap profile",
"                 (default: 25)",
257
"",
258 259
"  -xt            Include threads (TSOs) in a heap profile",
"",
260
"  -xc      Show current cost centre stack on raising an exception",
261 262
# endif
#endif /* PROFILING or PAR */
Simon Marlow's avatar
Simon Marlow committed
263

264
#ifdef TRACING
Simon Marlow's avatar
Simon Marlow committed
265
"",
266 267 268 269 270 271 272 273 274
"  -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",
#  ifdef DEBUG
"                t    add time stamps (only useful with -v)",
#  endif
Simon Marlow's avatar
Simon Marlow committed
275 276
#endif

277
#if !defined(PROFILING)
278
"",
279
"  -hT      Heap residency profile (output file <program>.hp)",
280
#endif
281
"  -i<sec>  Time between heap samples (seconds, default: 0.1)",
282 283
"",
#if defined(TICKY_TICKY)
284
"  -r<file>  Produce ticky-ticky statistics (with -rstderr for stderr)",
285 286
"",
#endif
287 288 289
"  -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.",
290
"  -V<secs>  Master tick interval in seconds (0 == disable timer).",
291 292
"            This sets the resolution for -C and the profile timer -i.",
"            Default: 0.02 sec.",
293 294 295
"",
#if defined(DEBUG)
"  -Ds  DEBUG: scheduler",
296
"  -Di  DEBUG: interpreter",
297 298 299 300 301 302 303
"  -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
304
"  -De  DEBUG: event logging",
305
"  -Da  DEBUG: apply",
306
"  -Dl  DEBUG: linker",
307
"  -Dm  DEBUG: stm",
Simon Marlow's avatar
Simon Marlow committed
308
"  -Dz  DEBUG: stack squeezing",
309
"  -Dc  DEBUG: program coverage",
310 311
"  -Dr  DEBUG: sparks",
"",
312
"     NOTE: DEBUG events are sent to stderr by default; add -l to create a",
313
"     binary event log file instead.",
314
"",
315
#endif /* DEBUG */
316
#if defined(THREADED_RTS) && !defined(NOSMP)
317 318
"  -N<n>     Use <n> processors (default: 1)",
"  -N        Determine the number of processors to use automatically",
319 320 321 322 323
"  -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)",
324
"  -qm       Don't automatically migrate threads between CPUs",
325
#endif
326 327
"  --install-signal-handlers=<yes|no>",
"            Install signal handlers (default: yes)",
Simon Marlow's avatar
Simon Marlow committed
328
#if defined(THREADED_RTS)
329 330
"  -e<n>     Maximum number of outstanding local sparks (default: 4096)",
#endif
331 332 333 334
#if defined(x86_64_HOST_ARCH)
"  -xm       Base address to mmap memory in the GHCi linker",
"            (hex; must be <80000000)",
#endif
335
#if defined(USE_PAPI)
336 337
"  -aX       CPU performance counter measurements using PAPI",
"            (use with the -s<file> option).  X is one of:",
338 339 340 341 342 343
"",
/* "            y - cycles", */
"            1 - level 1 cache misses",
"            2 - level 2 cache misses",
"            b - branch mispredictions",
"            s - stalled cycles",
344
"            e - cache miss and branch misprediction events",
345 346
"            +PAPI_EVENT   - collect papi preset event PAPI_EVENT",
"            #NATIVE_EVENT - collect native event NATIVE_EVENT (in hex)",
347
#endif
348
"",
349 350
"RTS options may also be specified using the GHCRTS environment variable.",
"",
351 352 353 354 355 356
"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
357
STATIC_INLINE rtsBool
358 359 360 361 362
strequal(const char *a, const char * b)
{
    return(strcmp(a, b) == 0);
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376
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
377
	    s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
378 379 380 381 382 383 384 385 386 387 388
	    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');
}
    
389 390 391 392 393 394 395
void
setupRtsFlags(int *argc, char *argv[], int *rts_argc, char *rts_argv[])
{
    rtsBool error = rtsFalse;
    I_ mode;
    I_ arg, total_arg;

396
    setProgName (argv);
397 398 399 400 401 402
    total_arg = *argc;
    arg = 1;

    *argc = 1;
    *rts_argc = 0;

403 404 405 406 407 408 409 410 411
    // 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);
	}
    }

412
    // process arguments from the GHCRTS environment variable next
413
    // (arguments from the command line override these).
414 415 416 417
    {
	char *ghc_rts = getenv("GHCRTS");

	if (ghc_rts != NULL) {
Ian Lynagh's avatar
Ian Lynagh committed
418
            if (rtsOptsEnabled != rtsOptsNone) {
419 420 421
                splitRtsFlags(ghc_rts, rts_argc, rts_argv);
            }
            else {
422
                errorBelch("Warning: Ignoring GHCRTS variable as RTS options are disabled.\n         Link with -rtsopts to enable them.");
423 424
                // We don't actually exit, just warn
            }
425 426
	}
    }
427

428 429 430
    // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
    //   argv[0] must be PGM argument -- leave in argv

431 432 433 434 435 436 437 438 439 440 441 442
    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])) {
Ian Lynagh's avatar
Ian Lynagh committed
443
            if (rtsOptsEnabled != rtsOptsNone) {
444 445 446
                mode = RTS;
            }
            else {
447
                errorBelch("RTS options are disabled. Link with -rtsopts to enable them.");
448 449
                stg_exit(EXIT_FAILURE);
            }
450 451 452 453 454
	}
	else if (strequal("-RTS", argv[arg])) {
	    mode = PGM;
	}
	else if (mode == RTS && *rts_argc < MAX_RTS_ARGS-1) {
Ian Lynagh's avatar
Ian Lynagh committed
455 456
            rts_argv[(*rts_argc)++] = argv[arg];
        }
Simon Marlow's avatar
Simon Marlow committed
457
        else if (mode == PGM) {
458 459 460 461 462 463
	    argv[(*argc)++] = argv[arg];
	}
	else {
	  barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
	}
    }
464 465 466
    // process remaining program arguments
    for (; arg < total_arg; arg++) {
	argv[(*argc)++] = argv[arg];
467 468 469 470 471
    }
    argv[*argc] = (char *) 0;
    rts_argv[*rts_argc] = (char *) 0;

    // Process RTS (rts_argv) part: mainly to determine statsfile
472 473 474
    for (arg = 0; arg < *rts_argc; arg++) {
	if (rts_argv[arg][0] != '-') {
	    fflush(stdout);
475
	    errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
476 477 478
	    error = rtsTrue;

        } else {
Simon Marlow's avatar
Simon Marlow committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

            switch(rts_argv[arg][1]) {
            case '-':
                if (strequal("info", &rts_argv[arg][2])) {
                    printRtsInfo();
                    stg_exit(0);
                }
                break;
            default:
                break;
            }

            if (rtsOptsEnabled != rtsOptsAll)
            {
                errorBelch("Most RTS options are disabled. Link with -rtsopts to enable them.");
                stg_exit(EXIT_FAILURE);
            }

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

Simon Marlow's avatar
Simon Marlow committed
499 500 501 502 503
	      /* 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.
504 505 506 507 508 509
	      */

#ifdef TICKY_TICKY
# define TICKY_BUILD_ONLY(x) x
#else
# define TICKY_BUILD_ONLY(x) \
510
errorBelch("the flag %s requires the program to be built with -ticky", rts_argv[arg]); \
511 512 513 514 515 516 517
error = rtsTrue;
#endif

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

522 523
#ifdef TRACING
# define TRACING_BUILD_ONLY(x)   x
Simon Marlow's avatar
Simon Marlow committed
524
#else
525
# define TRACING_BUILD_ONLY(x) \
526
errorBelch("the flag %s requires the program to be built with -eventlog or -debug", rts_argv[arg]); \
Simon Marlow's avatar
Simon Marlow committed
527 528 529
error = rtsTrue;
#endif

530 531 532 533
#ifdef THREADED_RTS
# define THREADED_BUILD_ONLY(x)      x
#else
# define THREADED_BUILD_ONLY(x) \
534 535 536 537 538 539 540 541 542
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]); \
543
error = rtsTrue;
544 545 546 547 548 549 550
#endif

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

551 552 553 554 555 556 557 558 559 560 561 562
              /* 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
563 564 565 566
                  else if (strequal("machine-readable",
                               &rts_argv[arg][2])) {
                      RtsFlags.MiscFlags.machineReadable = rtsTrue;
                  }
567 568
                  else if (strequal("info",
                               &rts_argv[arg][2])) {
569
                      printRtsInfo();
570
                      stg_exit(0);
571
                  }
572 573 574 575 576
                  else {
		      errorBelch("unknown RTS option: %s",rts_argv[arg]);
		      error = rtsTrue;
                  }
		  break;
577
	      case 'A':
578 579 580 581
                  RtsFlags.GcFlags.minAllocAreaSize
                      = decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_INT_MAX)
                           / BLOCK_SIZE;
                  break;
582

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
#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;
598 599 600
		case 'e':
		  RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
		  break;
601
                case '+':
602
                case '#':
603 604 605 606
                  if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
                      errorBelch("maximum number of PAPI events reached");
                      stg_exit(EXIT_FAILURE);
                  }
607 608 609 610 611
                  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;
612
                  RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
613
                  RtsFlags.PapiFlags.userEventsKind[eventNum] = eventKind;
614
                  break;
615 616 617 618 619 620
		default:
		  bad_option( rts_argv[arg] );
		}
		break;
#endif

621 622 623 624
	      case 'B':
		RtsFlags.GcFlags.ringBell = rtsTrue;
		break;

625
	      case 'c':
626 627 628 629
		  if (rts_argv[arg][2] != '\0') {
		      RtsFlags.GcFlags.compactThreshold =
			  atof(rts_argv[arg]+2);
		  } else {
630
		      RtsFlags.GcFlags.compact = rtsTrue;
631 632
		  }
		  break;
633

634 635 636 637
              case 'w':
		RtsFlags.GcFlags.sweep = rtsTrue;
		break;

638 639 640 641 642 643 644
	      case 'F':
	        RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
	      
		if (RtsFlags.GcFlags.oldGenFactor < 0)
		  bad_option( rts_argv[arg] );
		break;
	      
645
	      case 'D':
646
              DEBUG_BUILD_ONLY(
647 648 649 650 651 652 653 654
	      { 
		  char *c;

		  for (c  = rts_argv[arg] + 2; *c != '\0'; c++) {
		      switch (*c) {
		      case 's':
			  RtsFlags.DebugFlags.scheduler = rtsTrue;
			  break;
655 656
		      case 'i':
			  RtsFlags.DebugFlags.interpreter = rtsTrue;
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
			  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;
682 683 684
		      case 'a':
			  RtsFlags.DebugFlags.apply = rtsTrue;
			  break;
685 686 687
		      case 'm':
			  RtsFlags.DebugFlags.stm = rtsTrue;
			  break;
688 689 690
		      case 'z':
			  RtsFlags.DebugFlags.squeeze = rtsTrue;
			  break;
691 692 693
		      case 'c':
			  RtsFlags.DebugFlags.hpc = rtsTrue;
			  break;
694 695 696
		      case 'r':
			  RtsFlags.DebugFlags.sparks = rtsTrue;
			  break;
697 698 699 700
		      default:
			  bad_option( rts_argv[arg] );
		      }
		  }
701 702
                  // -Dx also turns on -v.  Use -l to direct trace
                  // events to the .eventlog file instead.
703
                  RtsFlags.TraceFlags.tracing = TRACE_STDERR;
704 705
	      })
              break;
706 707

	      case 'K':
708
                  RtsFlags.GcFlags.maxStkSize =
709
                      decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
710
                  break;
711 712

	      case 'k':
713 714 715 716 717 718 719 720 721 722 723 724 725 726
		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:
727
                  RtsFlags.GcFlags.initialStkSize =
728
                      decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
729
                  break;
730 731
                }
                break;
732

733
              case 'M':
734 735 736 737
                  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;
738 739

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

742 743 744 745
                  if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
                      RtsFlags.GcFlags.pcFreeHeap > 100)
                      bad_option( rts_argv[arg] );
                  break;
746

747
	      case 'G':
748 749 750
                  RtsFlags.GcFlags.generations =
                      decodeSize(rts_argv[arg], 2, 1, HS_INT_MAX);
                  break;
751

752
	      case 'H':
753 754 755 756 757 758 759
                  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;
760

761 762 763 764 765 766
#ifdef RTS_GTK_FRONTPANEL
	      case 'f':
		  RtsFlags.GcFlags.frontpanel = rtsTrue;
		  break;
#endif

767 768 769 770 771 772
    	      case 'I':	/* idle GC delay */
		if (rts_argv[arg][2] == '\0') {
		  /* use default */
		} else {
		    I_ cst; /* tmp */

773
		    /* Convert to millisecs */
774
		    cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
775
		    RtsFlags.GcFlags.idleGCDelayTime = cst;
776 777 778
		}
		break;

779
	      case 'S':
780 781
		  RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
		  goto stats;
782

783
	      case 's':
784 785
		  RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
		  goto stats;
786 787

	      case 't':
788 789
		  RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
		  goto stats;
790

791
	    stats:
792 793 794
		{ 
		    int r;
		    r = open_stats_file(arg, *argc, argv,
795
					*rts_argc, rts_argv, NULL,
796 797 798
					&RtsFlags.GcFlags.statsFile);
		    if (r == -1) { error = rtsTrue; }
		}
Simon Marlow's avatar
Simon Marlow committed
799
                break;
800 801 802 803 804 805 806 807 808

	      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
809
		PROFILING_BUILD_ONLY(
810
		switch (rts_argv[arg][2]) {
811 812
		  case 'x':
		    RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
813
		    break;
andy's avatar
andy committed
814 815 816
		  case 'a':
		    RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
		    break;
817
		  default:
818 819 820 821 822 823 824 825
		      if (rts_argv[arg][1] == 'P') {
			  RtsFlags.CcFlags.doCostCentres =
			      COST_CENTRES_VERBOSE;
		      } else {
			  RtsFlags.CcFlags.doCostCentres =
			      COST_CENTRES_SUMMARY;
		      }
		      break;
826 827 828
		}
		) break;

829 830 831 832
	      case 'R':
		  PROFILING_BUILD_ONLY(
		      RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
  	          ) break;
ravi@bluespec.com's avatar
ravi@bluespec.com committed
833 834 835 836 837 838 839
	      case 'L':
		  PROFILING_BUILD_ONLY(
		      RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
                      if(RtsFlags.ProfFlags.ccsLength <= 0) {
			bad_option(rts_argv[arg]);
                      }
		  ) break;
840
	      case 'h': /* serial heap profile */
841
#if !defined(PROFILING)
842 843 844 845 846 847
		switch (rts_argv[arg][2]) {
		  case '\0':
		  case 'T':
		    RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
		    break;
		  default:
848
		    errorBelch("invalid heap profile option: %s",rts_argv[arg]);
849 850 851 852 853
		    error = rtsTrue;
		}
#else
		PROFILING_BUILD_ONLY(
		switch (rts_argv[arg][2]) {
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
		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], '}');
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887

			    // 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;
888 889 890
			    case 'C':
				RtsFlags.ProfFlags.ccsSelector = left;
				break;
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
			    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;
911 912 913 914
			    }
			}
			break;
		    }
915

916
		    if (RtsFlags.ProfFlags.doHeapProfile != 0) {
917
			errorBelch("multiple heap profile options");
918 919 920 921 922 923 924 925 926 927 928 929
			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':
930 931
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
			  break;
932 933
		    case 'D':
		    case 'd':
934 935
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
			  break;
936 937
		    case 'Y':
		    case 'y':
938 939
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
			  break;
940 941
		    case 'R':
		    case 'r':
942 943
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
			  break;
944 945
		    case 'B':
		    case 'b':
946 947
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
			  break;
948 949
		    }
		    break;
950
		      
951
		default:
952
		    errorBelch("invalid heap profile option: %s",rts_argv[arg]);
953 954 955
		    error = rtsTrue;
		}
		) 
956
#endif /* PROFILING */
andy's avatar
andy committed
957 958 959 960 961 962 963 964 965 966
    	    	break;

    	      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));
967
		    RtsFlags.ProfFlags.profileInterval = cst;
andy's avatar
andy committed
968 969
		}
		break;
970 971 972 973 974 975 976 977 978 979 980 981 982 983

	      /* =========== 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));
		    RtsFlags.ConcFlags.ctxtSwitchTime = cst;
		}
    	    	break;

984 985 986 987 988 989 990 991 992 993 994 995 996
              case 'V': /* master tick interval */
                if (rts_argv[arg][2] == '\0') {
                    // turns off ticks completely
                    RtsFlags.MiscFlags.tickInterval = 0;
                } else {
                    I_ cst; /* tmp */

                    /* Convert to milliseconds */
                    cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
                    RtsFlags.MiscFlags.tickInterval = cst;
                }
                break;

997
#if !defined(NOSMP)
998
	      case 'N':
999
		THREADED_BUILD_ONLY(
1000 1001 1002 1003
		if (rts_argv[arg][2] == '\0') {
#if defined(PROFILING)
		    RtsFlags.ParFlags.nNodes = 1;
#else
1004
                    RtsFlags.ParFlags.nNodes = getNumberOfProcessors();
1005 1006
#endif
		} else {
1007
		    RtsFlags.ParFlags.nNodes
1008
		      = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1009
		    if (RtsFlags.ParFlags.nNodes <= 0) {
1010
		      errorBelch("bad value for -N");
1011
		      error = rtsTrue;
1012
		    }
Simon Marlow's avatar
Simon Marlow committed
1013 1014 1015 1016 1017 1018
#if defined(PROFILING)
                    if (RtsFlags.ParFlags.nNodes > 1) {
                        errorBelch("bad option %s: only -N1 is supported with profiling", rts_argv[arg