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