Add rtsopts ignore and ignoreAll.
[ghc.git] / includes / RtsAPI.h
1 /* ----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2004
4 *
5 * API for invoking Haskell functions via the RTS
6 *
7 * To understand the structure of the RTS headers, see the wiki:
8 * http://ghc.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
9 *
10 * --------------------------------------------------------------------------*/
11
12 #pragma once
13
14 #if defined(__cplusplus)
15 extern "C" {
16 #endif
17
18 #include "HsFFI.h"
19 #include "rts/Time.h"
20 #include "rts/EventLogWriter.h"
21
22 /*
23 * Running the scheduler
24 */
25 typedef enum {
26 NoStatus, /* not finished yet */
27 Success, /* completed successfully */
28 Killed, /* uncaught exception */
29 Interrupted, /* stopped in response to a call to interruptStgRts */
30 HeapExhausted /* out of memory */
31 } SchedulerStatus;
32
33 typedef struct StgClosure_ *HaskellObj;
34
35 /*
36 * An abstract type representing the token returned by rts_lock() and
37 * used when allocating objects and threads in the RTS.
38 */
39 typedef struct Capability_ Capability;
40
41 /*
42 * The public view of a Capability: we can be sure it starts with
43 * these two components (but it may have more private fields).
44 */
45 typedef struct CapabilityPublic_ {
46 StgFunTable f;
47 StgRegTable r;
48 } CapabilityPublic;
49
50 /* ----------------------------------------------------------------------------
51 RTS configuration settings, for passing to hs_init_ghc()
52 ------------------------------------------------------------------------- */
53
54 typedef enum {
55 RtsOptsNone, // +RTS causes an error
56 RtsOptsIgnore, // Ignore command line arguments
57 RtsOptsIgnoreAll, // Ignore command line and Environment arguments
58 RtsOptsSafeOnly, // safe RTS options allowed; others cause an error
59 RtsOptsAll // all RTS options allowed
60 } RtsOptsEnabledEnum;
61
62 struct GCDetails_;
63
64 // The RtsConfig struct is passed (by value) to hs_init_ghc(). The
65 // reason for using a struct is extensibility: we can add more
66 // fields to this later without breaking existing client code.
67 typedef struct {
68
69 // Whether to interpret +RTS options on the command line
70 RtsOptsEnabledEnum rts_opts_enabled;
71
72 // Whether to give RTS flag suggestions
73 HsBool rts_opts_suggestions;
74
75 // additional RTS options
76 const char *rts_opts;
77
78 // True if GHC was not passed -no-hs-main
79 HsBool rts_hs_main;
80
81 // Whether to retain CAFs (default: false)
82 HsBool keep_cafs;
83
84 // Writer a for eventlog.
85 const EventLogWriter *eventlog_writer;
86
87 // Called before processing command-line flags, so that default
88 // settings for RtsFlags can be provided.
89 void (* defaultsHook) (void);
90
91 // Called just before exiting
92 void (* onExitHook) (void);
93
94 // Called on a stack overflow, before exiting
95 void (* stackOverflowHook) (W_ stack_size);
96
97 // Called on heap overflow, before exiting
98 void (* outOfHeapHook) (W_ request_size, W_ heap_size);
99
100 // Called when malloc() fails, before exiting
101 void (* mallocFailHook) (W_ request_size /* in bytes */, const char *msg);
102
103 // Called for every GC
104 void (* gcDoneHook) (const struct GCDetails_ *stats);
105 } RtsConfig;
106
107 // Clients should start with defaultRtsConfig and then customise it.
108 // Bah, I really wanted this to be a const struct value, but it seems
109 // you can't do that in C (it generates code).
110 extern const RtsConfig defaultRtsConfig;
111
112 /* -----------------------------------------------------------------------------
113 Statistics
114 -------------------------------------------------------------------------- */
115
116 //
117 // Stats about a single GC
118 //
119 typedef struct GCDetails_ {
120 // The generation number of this GC
121 uint32_t gen;
122 // Number of threads used in this GC
123 uint32_t threads;
124 // Number of bytes allocated since the previous GC
125 uint64_t allocated_bytes;
126 // Total amount of live data in the heap (incliudes large + compact data)
127 uint64_t live_bytes;
128 // Total amount of live data in large objects
129 uint64_t large_objects_bytes;
130 // Total amount of live data in compact regions
131 uint64_t compact_bytes;
132 // Total amount of slop (wasted memory)
133 uint64_t slop_bytes;
134 // Total amount of memory in use by the RTS
135 uint64_t mem_in_use_bytes;
136 // Total amount of data copied during this GC
137 uint64_t copied_bytes;
138 // In parallel GC, the max amount of data copied by any one thread
139 uint64_t par_max_copied_bytes;
140 // In parallel GC, the amount of balanced data copied by all threads
141 uint64_t par_balanced_copied_bytes;
142 // The time elapsed during synchronisation before GC
143 Time sync_elapsed_ns;
144 // The CPU time used during GC itself
145 Time cpu_ns;
146 // The time elapsed during GC itself
147 Time elapsed_ns;
148 } GCDetails;
149
150 //
151 // Stats about the RTS currently, and since the start of execution
152 //
153 typedef struct _RTSStats {
154
155 // -----------------------------------
156 // Cumulative stats about memory use
157
158 // Total number of GCs
159 uint32_t gcs;
160 // Total number of major (oldest generation) GCs
161 uint32_t major_gcs;
162 // Total bytes allocated
163 uint64_t allocated_bytes;
164 // Maximum live data (including large objects + compact regions)
165 uint64_t max_live_bytes;
166 // Maximum live data in large objects
167 uint64_t max_large_objects_bytes;
168 // Maximum live data in compact regions
169 uint64_t max_compact_bytes;
170 // Maximum slop
171 uint64_t max_slop_bytes;
172 // Maximum memory in use by the RTS
173 uint64_t max_mem_in_use_bytes;
174 // Sum of live bytes across all major GCs. Divided by major_gcs
175 // gives the average live data over the lifetime of the program.
176 uint64_t cumulative_live_bytes;
177 // Sum of copied_bytes across all GCs
178 uint64_t copied_bytes;
179 // Sum of copied_bytes across all parallel GCs
180 uint64_t par_copied_bytes;
181 // Sum of par_max_copied_bytes across all parallel GCs
182 uint64_t cumulative_par_max_copied_bytes;
183 // Sum of par_balanced_copied_byes across all parallel GCs.
184 uint64_t cumulative_par_balanced_copied_bytes;
185
186 // -----------------------------------
187 // Cumulative stats about time use
188 // (we use signed values here because due to inaccuracies in timers
189 // the values can occasionally go slightly negative)
190
191 // Total CPU time used by the mutator
192 Time mutator_cpu_ns;
193 // Total elapsed time used by the mutator
194 Time mutator_elapsed_ns;
195 // Total CPU time used by the GC
196 Time gc_cpu_ns;
197 // Total elapsed time used by the GC
198 Time gc_elapsed_ns;
199 // Total CPU time (at the previous GC)
200 Time cpu_ns;
201 // Total elapsed time (at the previous GC)
202 Time elapsed_ns;
203
204 // -----------------------------------
205 // Stats about the most recent GC
206
207 GCDetails gc;
208
209 } RTSStats;
210
211 void getRTSStats (RTSStats *s);
212 int getRTSStatsEnabled (void);
213
214 // Returns the total number of bytes allocated since the start of the program.
215 // TODO: can we remove this?
216 uint64_t getAllocations (void);
217
218 /* ----------------------------------------------------------------------------
219 Starting up and shutting down the Haskell RTS.
220 ------------------------------------------------------------------------- */
221
222 /* DEPRECATED, use hs_init() or hs_init_ghc() instead */
223 extern void startupHaskell ( int argc, char *argv[],
224 void (*init_root)(void) );
225
226 /* DEPRECATED, use hs_exit() instead */
227 extern void shutdownHaskell ( void );
228
229 /* Like hs_init(), but allows rtsopts. For more complicated usage,
230 * use hs_init_ghc. */
231 extern void hs_init_with_rtsopts (int *argc, char **argv[]);
232
233 /*
234 * GHC-specific version of hs_init() that allows specifying whether
235 * +RTS ... -RTS options are allowed or not (default: only "safe"
236 * options are allowed), and allows passing an option string that is
237 * to be interpreted by the RTS only, not passed to the program.
238 */
239 extern void hs_init_ghc (int *argc, char **argv[], // program arguments
240 RtsConfig rts_config); // RTS configuration
241
242 extern void shutdownHaskellAndExit (int exitCode, int fastExit)
243 GNUC3_ATTRIBUTE(__noreturn__);
244
245 #if !defined(mingw32_HOST_OS)
246 extern void shutdownHaskellAndSignal (int sig, int fastExit)
247 GNUC3_ATTRIBUTE(__noreturn__);
248 #endif
249
250 extern void getProgArgv ( int *argc, char **argv[] );
251 extern void setProgArgv ( int argc, char *argv[] );
252 extern void getFullProgArgv ( int *argc, char **argv[] );
253 extern void setFullProgArgv ( int argc, char *argv[] );
254 extern void freeFullProgArgv ( void ) ;
255
256 /* exit() override */
257 extern void (*exitFn)(int);
258
259 /* ----------------------------------------------------------------------------
260 Locking.
261
262 You have to surround all access to the RtsAPI with these calls.
263 ------------------------------------------------------------------------- */
264
265 // acquires a token which may be used to create new objects and
266 // evaluate them.
267 Capability *rts_lock (void);
268
269 // releases the token acquired with rts_lock().
270 void rts_unlock (Capability *token);
271
272 // If you are in a context where you know you have a current capability but
273 // do not know what it is, then use this to get it. Basically this only
274 // applies to "unsafe" foreign calls (as unsafe foreign calls are made with
275 // the capability held).
276 //
277 // WARNING: There is *no* guarantee this returns anything sensible (eg NULL)
278 // when there is no current capability.
279 Capability *rts_unsafeGetMyCapability (void);
280
281 /* ----------------------------------------------------------------------------
282 Which cpu should the OS thread and Haskell thread run on?
283
284 1. Run the current thread on the given capability:
285 rts_setInCallCapability(cap, 0);
286
287 2. Run the current thread on the given capability and set the cpu affinity
288 for this thread:
289 rts_setInCallCapability(cap, 1);
290
291 3. Run the current thread on the given numa node:
292 rts_pinThreadToNumaNode(node);
293
294 4. Run the current thread on the given capability and on the given numa node:
295 rts_setInCallCapability(cap, 0);
296 rts_pinThreadToNumaNode(cap);
297 ------------------------------------------------------------------------- */
298
299 // Specify the Capability that the current OS thread should run on when it calls
300 // into Haskell. The actual capability will be calculated as the supplied
301 // value modulo the number of enabled Capabilities.
302 //
303 // Note that the thread may still be migrated by the RTS scheduler, but that
304 // will only happen if there are multiple threads running on one Capability and
305 // another Capability is free.
306 //
307 // If affinity is non-zero, the current thread will be bound to
308 // specific CPUs according to the prevailing affinity policy for the
309 // specified capability, set by either +RTS -qa or +RTS --numa.
310 void rts_setInCallCapability (int preferred_capability, int affinity);
311
312 // Specify the CPU Node that the current OS thread should run on when it calls
313 // into Haskell. The argument can be either a node number or capability number.
314 // The actual node will be calculated as the supplied value modulo the number
315 // of numa nodes.
316 void rts_pinThreadToNumaNode (int node);
317
318 /* ----------------------------------------------------------------------------
319 Building Haskell objects from C datatypes.
320 ------------------------------------------------------------------------- */
321 HaskellObj rts_mkChar ( Capability *, HsChar c );
322 HaskellObj rts_mkInt ( Capability *, HsInt i );
323 HaskellObj rts_mkInt8 ( Capability *, HsInt8 i );
324 HaskellObj rts_mkInt16 ( Capability *, HsInt16 i );
325 HaskellObj rts_mkInt32 ( Capability *, HsInt32 i );
326 HaskellObj rts_mkInt64 ( Capability *, HsInt64 i );
327 HaskellObj rts_mkWord ( Capability *, HsWord w );
328 HaskellObj rts_mkWord8 ( Capability *, HsWord8 w );
329 HaskellObj rts_mkWord16 ( Capability *, HsWord16 w );
330 HaskellObj rts_mkWord32 ( Capability *, HsWord32 w );
331 HaskellObj rts_mkWord64 ( Capability *, HsWord64 w );
332 HaskellObj rts_mkPtr ( Capability *, HsPtr a );
333 HaskellObj rts_mkFunPtr ( Capability *, HsFunPtr a );
334 HaskellObj rts_mkFloat ( Capability *, HsFloat f );
335 HaskellObj rts_mkDouble ( Capability *, HsDouble f );
336 HaskellObj rts_mkStablePtr ( Capability *, HsStablePtr s );
337 HaskellObj rts_mkBool ( Capability *, HsBool b );
338 HaskellObj rts_mkString ( Capability *, char *s );
339
340 HaskellObj rts_apply ( Capability *, HaskellObj, HaskellObj );
341
342 /* ----------------------------------------------------------------------------
343 Deconstructing Haskell objects
344 ------------------------------------------------------------------------- */
345 HsChar rts_getChar ( HaskellObj );
346 HsInt rts_getInt ( HaskellObj );
347 HsInt8 rts_getInt8 ( HaskellObj );
348 HsInt16 rts_getInt16 ( HaskellObj );
349 HsInt32 rts_getInt32 ( HaskellObj );
350 HsInt64 rts_getInt64 ( HaskellObj );
351 HsWord rts_getWord ( HaskellObj );
352 HsWord8 rts_getWord8 ( HaskellObj );
353 HsWord16 rts_getWord16 ( HaskellObj );
354 HsWord32 rts_getWord32 ( HaskellObj );
355 HsWord64 rts_getWord64 ( HaskellObj );
356 HsPtr rts_getPtr ( HaskellObj );
357 HsFunPtr rts_getFunPtr ( HaskellObj );
358 HsFloat rts_getFloat ( HaskellObj );
359 HsDouble rts_getDouble ( HaskellObj );
360 HsStablePtr rts_getStablePtr ( HaskellObj );
361 HsBool rts_getBool ( HaskellObj );
362
363 /* ----------------------------------------------------------------------------
364 Evaluating Haskell expressions
365
366 The versions ending in '_' allow you to specify an initial stack size.
367 Note that these calls may cause Garbage Collection, so all HaskellObj
368 references are rendered invalid by these calls.
369
370 All of these functions take a (Capability **) - there is a
371 Capability pointer both input and output. We use an inout
372 parameter because this is less error-prone for the client than a
373 return value - the client could easily forget to use the return
374 value, whereas incorrectly using an inout parameter will usually
375 result in a type error.
376 ------------------------------------------------------------------------- */
377
378 void rts_eval (/* inout */ Capability **,
379 /* in */ HaskellObj p,
380 /* out */ HaskellObj *ret);
381
382 void rts_eval_ (/* inout */ Capability **,
383 /* in */ HaskellObj p,
384 /* in */ unsigned int stack_size,
385 /* out */ HaskellObj *ret);
386
387 void rts_evalIO (/* inout */ Capability **,
388 /* in */ HaskellObj p,
389 /* out */ HaskellObj *ret);
390
391 void rts_evalStableIOMain (/* inout */ Capability **,
392 /* in */ HsStablePtr s,
393 /* out */ HsStablePtr *ret);
394
395 void rts_evalStableIO (/* inout */ Capability **,
396 /* in */ HsStablePtr s,
397 /* out */ HsStablePtr *ret);
398
399 void rts_evalLazyIO (/* inout */ Capability **,
400 /* in */ HaskellObj p,
401 /* out */ HaskellObj *ret);
402
403 void rts_evalLazyIO_ (/* inout */ Capability **,
404 /* in */ HaskellObj p,
405 /* in */ unsigned int stack_size,
406 /* out */ HaskellObj *ret);
407
408 void rts_checkSchedStatus (char* site, Capability *);
409
410 SchedulerStatus rts_getSchedStatus (Capability *cap);
411
412 /*
413 * The RTS allocates some thread-local data when you make a call into
414 * Haskell using one of the rts_eval() functions. This data is not
415 * normally freed until hs_exit(). If you want to free it earlier
416 * than this, perhaps because the thread is about to exit, then call
417 * rts_done() from the thread.
418 *
419 * It is safe to make more rts_eval() calls after calling rts_done(),
420 * but the next one will cause allocation of the thread-local memory
421 * again.
422 */
423 void rts_done (void);
424
425 /* --------------------------------------------------------------------------
426 Wrapper closures
427
428 These are used by foreign export and foreign import "wrapper" stubs.
429 ----------------------------------------------------------------------- */
430
431 // When producing Windows DLLs the we need to know which symbols are in the
432 // local package/DLL vs external ones.
433 //
434 // Note that RtsAPI.h is also included by foreign export stubs in
435 // the base package itself.
436 //
437 #if defined(COMPILING_WINDOWS_DLL) && !defined(COMPILING_BASE_PACKAGE)
438 __declspec(dllimport) extern StgWord base_GHCziTopHandler_runIO_closure[];
439 __declspec(dllimport) extern StgWord base_GHCziTopHandler_runNonIO_closure[];
440 #else
441 extern StgWord base_GHCziTopHandler_runIO_closure[];
442 extern StgWord base_GHCziTopHandler_runNonIO_closure[];
443 #endif
444
445 #define runIO_closure base_GHCziTopHandler_runIO_closure
446 #define runNonIO_closure base_GHCziTopHandler_runNonIO_closure
447
448 /* ------------------------------------------------------------------------ */
449
450 #if defined(__cplusplus)
451 }
452 #endif