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