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