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