RtsFlags.c 41.1 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 72
    RtsFlags.GcFlags.initialStkSize	= 1024 / sizeof(W_);

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

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

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

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

124
    RtsFlags.ProfFlags.doHeapProfile      = rtsFalse;
125
    RtsFlags.ProfFlags.profileInterval    = 100;
126 127

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

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

147 148
    RtsFlags.MiscFlags.tickInterval	= 20;  /* In milliseconds */
    RtsFlags.ConcFlags.ctxtSwitchTime	= 20;  /* In milliseconds */
149

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

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

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

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

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

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

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

261
#ifdef TRACING
Simon Marlow's avatar
Simon Marlow committed
262
"",
263 264 265 266 267 268 269 270 271
"  -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
272 273
#endif

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

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

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

    *argc = 1;
    *rts_argc = 0;

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

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

	if (ghc_rts != NULL) {
417 418 419 420 421 422 423
            if (rtsOptsEnabled) {
                splitRtsFlags(ghc_rts, rts_argc, rts_argv);
            }
            else {
                errorBelch("Warning: Ignoring GHCRTS variable");
                // We don't actually exit, just warn
            }
424 425
	}
    }
426

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

430 431 432 433 434 435 436 437 438 439 440 441
    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])) {
442 443 444 445 446 447 448
            if (rtsOptsEnabled) {
                mode = RTS;
            }
            else {
                errorBelch("RTS options are disabled");
                stg_exit(EXIT_FAILURE);
            }
449 450 451 452 453 454 455 456 457 458 459 460 461 462
	}
	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);
	}
    }
463 464 465
    // process remaining program arguments
    for (; arg < total_arg; arg++) {
	argv[(*argc)++] = argv[arg];
466 467 468 469 470
    }
    argv[*argc] = (char *) 0;
    rts_argv[*rts_argc] = (char *) 0;

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

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

Simon Marlow's avatar
Simon Marlow committed
480 481 482 483 484
	      /* 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.
485 486 487 488 489 490
	      */

#ifdef TICKY_TICKY
# define TICKY_BUILD_ONLY(x) x
#else
# define TICKY_BUILD_ONLY(x) \
491
errorBelch("the flag %s requires the program to be built with -ticky", rts_argv[arg]); \
492 493 494 495 496 497 498
error = rtsTrue;
#endif

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

503 504
#ifdef TRACING
# define TRACING_BUILD_ONLY(x)   x
Simon Marlow's avatar
Simon Marlow committed
505
#else
506
# define TRACING_BUILD_ONLY(x) \
507
errorBelch("the flag %s requires the program to be built with -eventlog or -debug", rts_argv[arg]); \
Simon Marlow's avatar
Simon Marlow committed
508 509 510
error = rtsTrue;
#endif

511 512 513 514
#ifdef THREADED_RTS
# define THREADED_BUILD_ONLY(x)      x
#else
# define THREADED_BUILD_ONLY(x) \
515 516 517 518 519 520 521 522 523
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]); \
524
error = rtsTrue;
525 526 527 528 529 530 531
#endif

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

532 533 534 535 536 537 538 539 540 541 542 543
              /* 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
544 545 546 547
                  else if (strequal("machine-readable",
                               &rts_argv[arg][2])) {
                      RtsFlags.MiscFlags.machineReadable = rtsTrue;
                  }
548 549
                  else if (strequal("info",
                               &rts_argv[arg][2])) {
550
                      printRtsInfo();
551 552
                      exit(0);
                  }
553 554 555 556 557
                  else {
		      errorBelch("unknown RTS option: %s",rts_argv[arg]);
		      error = rtsTrue;
                  }
		  break;
558
	      case 'A':
559 560 561 562
                  RtsFlags.GcFlags.minAllocAreaSize
                      = decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_INT_MAX)
                           / BLOCK_SIZE;
                  break;
563

564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
#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;
579 580 581
		case 'e':
		  RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
		  break;
582 583 584 585 586 587 588 589
                case '+':
                  if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
                      errorBelch("maximum number of PAPI events reached");
                      stg_exit(EXIT_FAILURE);
                  }
                  RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
                  RtsFlags.PapiFlags.userEvents[RtsFlags.PapiFlags.numUserEvents++] = rts_argv[arg] + 3;
                  break;
590 591 592 593 594 595
		default:
		  bad_option( rts_argv[arg] );
		}
		break;
#endif

596 597 598 599
	      case 'B':
		RtsFlags.GcFlags.ringBell = rtsTrue;
		break;

600
	      case 'c':
601 602 603 604
		  if (rts_argv[arg][2] != '\0') {
		      RtsFlags.GcFlags.compactThreshold =
			  atof(rts_argv[arg]+2);
		  } else {
605
		      RtsFlags.GcFlags.compact = rtsTrue;
606 607
		  }
		  break;
608

609 610 611 612
              case 'w':
		RtsFlags.GcFlags.sweep = rtsTrue;
		break;

613 614 615 616 617 618 619
	      case 'F':
	        RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
	      
		if (RtsFlags.GcFlags.oldGenFactor < 0)
		  bad_option( rts_argv[arg] );
		break;
	      
620
	      case 'D':
621
              DEBUG_BUILD_ONLY(
622 623 624 625 626 627 628 629
	      { 
		  char *c;

		  for (c  = rts_argv[arg] + 2; *c != '\0'; c++) {
		      switch (*c) {
		      case 's':
			  RtsFlags.DebugFlags.scheduler = rtsTrue;
			  break;
630 631
		      case 'i':
			  RtsFlags.DebugFlags.interpreter = rtsTrue;
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
			  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;
657 658 659
		      case 'a':
			  RtsFlags.DebugFlags.apply = rtsTrue;
			  break;
660 661 662
		      case 'm':
			  RtsFlags.DebugFlags.stm = rtsTrue;
			  break;
663 664 665
		      case 'z':
			  RtsFlags.DebugFlags.squeeze = rtsTrue;
			  break;
666 667 668
		      case 'c':
			  RtsFlags.DebugFlags.hpc = rtsTrue;
			  break;
669 670 671
		      case 'r':
			  RtsFlags.DebugFlags.sparks = rtsTrue;
			  break;
672 673 674 675
		      default:
			  bad_option( rts_argv[arg] );
		      }
		  }
676 677
                  // -Dx also turns on -v.  Use -l to direct trace
                  // events to the .eventlog file instead.
678
                  RtsFlags.TraceFlags.tracing = TRACE_STDERR;
679 680
	      })
              break;
681 682

	      case 'K':
683 684 685
                  RtsFlags.GcFlags.maxStkSize =
                      decodeSize(rts_argv[arg], 2, 1, HS_WORD_MAX) / sizeof(W_);
                  break;
686 687

	      case 'k':
688 689 690
                  RtsFlags.GcFlags.initialStkSize =
                      decodeSize(rts_argv[arg], 2, 1, HS_WORD_MAX) / sizeof(W_);
                  break;
691 692

	      case 'M':
693 694 695 696
                  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;
697 698

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

701 702 703 704
                  if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
                      RtsFlags.GcFlags.pcFreeHeap > 100)
                      bad_option( rts_argv[arg] );
                  break;
705

706
	      case 'G':
707 708 709
                  RtsFlags.GcFlags.generations =
                      decodeSize(rts_argv[arg], 2, 1, HS_INT_MAX);
                  break;
710

711
	      case 'H':
712 713 714 715 716 717 718
                  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;
719

720 721 722 723 724 725
#ifdef RTS_GTK_FRONTPANEL
	      case 'f':
		  RtsFlags.GcFlags.frontpanel = rtsTrue;
		  break;
#endif

726 727 728 729 730 731
    	      case 'I':	/* idle GC delay */
		if (rts_argv[arg][2] == '\0') {
		  /* use default */
		} else {
		    I_ cst; /* tmp */

732
		    /* Convert to millisecs */
733
		    cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
734
		    RtsFlags.GcFlags.idleGCDelayTime = cst;
735 736 737
		}
		break;

738
	      case 'S':
739 740
		  RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
		  goto stats;
741

742
	      case 's':
743 744
		  RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
		  goto stats;
745 746

	      case 't':
747 748
		  RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
		  goto stats;
749

750
	    stats:
751 752 753
		{ 
		    int r;
		    r = open_stats_file(arg, *argc, argv,
754
					*rts_argc, rts_argv, NULL,
755 756 757
					&RtsFlags.GcFlags.statsFile);
		    if (r == -1) { error = rtsTrue; }
		}
Simon Marlow's avatar
Simon Marlow committed
758
                break;
759 760 761 762 763 764 765 766 767

	      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
768
		PROFILING_BUILD_ONLY(
769
		switch (rts_argv[arg][2]) {
770 771
		  case 'x':
		    RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
772
		    break;
andy's avatar
andy committed
773 774 775
		  case 'a':
		    RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
		    break;
776
		  default:
777 778 779 780 781 782 783 784
		      if (rts_argv[arg][1] == 'P') {
			  RtsFlags.CcFlags.doCostCentres =
			      COST_CENTRES_VERBOSE;
		      } else {
			  RtsFlags.CcFlags.doCostCentres =
			      COST_CENTRES_SUMMARY;
		      }
		      break;
785 786 787
		}
		) break;

788 789 790 791
	      case 'R':
		  PROFILING_BUILD_ONLY(
		      RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
  	          ) break;
ravi@bluespec.com's avatar
ravi@bluespec.com committed
792 793 794 795 796 797 798
	      case 'L':
		  PROFILING_BUILD_ONLY(
		      RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
                      if(RtsFlags.ProfFlags.ccsLength <= 0) {
			bad_option(rts_argv[arg]);
                      }
		  ) break;
799
	      case 'h': /* serial heap profile */
800
#if !defined(PROFILING)
801 802 803 804 805 806
		switch (rts_argv[arg][2]) {
		  case '\0':
		  case 'T':
		    RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
		    break;
		  default:
807
		    errorBelch("invalid heap profile option: %s",rts_argv[arg]);
808 809 810 811 812
		    error = rtsTrue;
		}
#else
		PROFILING_BUILD_ONLY(
		switch (rts_argv[arg][2]) {
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
		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], '}');
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846

			    // 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;
847 848 849
			    case 'C':
				RtsFlags.ProfFlags.ccsSelector = left;
				break;
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
			    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;
870 871 872 873
			    }
			}
			break;
		    }
874

875
		    if (RtsFlags.ProfFlags.doHeapProfile != 0) {
876
			errorBelch("multiple heap profile options");
877 878 879 880 881 882 883 884 885 886 887 888
			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':
889 890
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
			  break;
891 892
		    case 'D':
		    case 'd':
893 894
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
			  break;
895 896
		    case 'Y':
		    case 'y':
897 898
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
			  break;
899 900
		    case 'R':
		    case 'r':
901 902
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
			  break;
903 904
		    case 'B':
		    case 'b':
905 906
			  RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
			  break;
907 908
		    }
		    break;
909
		      
910
		default:
911
		    errorBelch("invalid heap profile option: %s",rts_argv[arg]);
912 913 914
		    error = rtsTrue;
		}
		) 
915
#endif /* PROFILING */
andy's avatar
andy committed
916 917 918 919 920 921 922 923 924 925
    	    	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));
926
		    RtsFlags.ProfFlags.profileInterval = cst;
andy's avatar
andy committed
927 928
		}
		break;
929 930 931 932 933 934 935 936 937 938 939 940 941 942

	      /* =========== 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;

943 944 945 946 947 948 949 950 951 952 953 954 955
              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;

956
#if !defined(NOSMP)
957
	      case 'N':
958
		THREADED_BUILD_ONLY(
959 960 961 962
		if (rts_argv[arg][2] == '\0') {
#if defined(PROFILING)
		    RtsFlags.ParFlags.nNodes = 1;
#else
963
                    RtsFlags.ParFlags.nNodes = getNumberOfProcessors();
964 965
#endif
		} else {
966
		    RtsFlags.ParFlags.nNodes
967
		      = strtol(rts_argv[arg]+2, (char **) NULL, 10);
968
		    if (RtsFlags.ParFlags.nNodes <= 0) {
969
		      errorBelch("bad value for -N");
970
		      error = rtsTrue;
971
		    }
Simon Marlow's avatar
Simon Marlow committed
972 973 974 975 976 977
#if defined(PROFILING)
                    if (RtsFlags.ParFlags.nNodes > 1) {
                        errorBelch("bad option %s: only -N1 is supported with profiling", rts_argv[arg]);
		      error = rtsTrue;
                    }
#endif
978 979 980 981 982
		}
		) break;

	      case 'g':
		THREADED_BUILD_ONLY(
983 984 985 986 987 988 989 990 991 992 993
		    switch (rts_argv[arg][2]) {
                    case '1':
                        // backwards compat only
                        RtsFlags.ParFlags.parGcEnabled = rtsFalse;
                        break;
		    default:
			errorBelch("unknown RTS option: %s",rts_argv[arg]);
			error = rtsTrue;
			break;
                    }
                    ) break;
994

Simon Marlow's avatar
Simon Marlow committed
995
	      case 'q':