RtsAPI.h 19.5 KB
Newer Older
1
/* ----------------------------------------------------------------------------
2
 *
3
 * (c) The GHC Team, 1998-2004
4 5 6
 *
 * API for invoking Haskell functions via the RTS
 *
7
 * To understand the structure of the RTS headers, see the wiki:
8
 *   https://gitlab.haskell.org/ghc/ghc/wikis/commentary/source-tree/includes
9
 *
10 11
 * --------------------------------------------------------------------------*/

12
#pragma once
13

Ben Gamari's avatar
Ben Gamari committed
14
#if defined(__cplusplus)
15 16 17
extern "C" {
#endif

18
#include "HsFFI.h"
Simon Marlow's avatar
Simon Marlow committed
19
#include "rts/Time.h"
20
#include "rts/EventLogWriter.h"
21

sof's avatar
sof committed
22 23 24 25
/*
 * Running the scheduler
 */
typedef enum {
26
    NoStatus,    /* not finished yet */
27 28
    Success,     /* completed successfully */
    Killed,      /* uncaught exception */
29 30
    Interrupted, /* stopped in response to a call to interruptStgRts */
    HeapExhausted /* out of memory */
sof's avatar
sof committed
31
} SchedulerStatus;
32

Simon Marlow's avatar
Simon Marlow committed
33
typedef struct StgClosure_ *HaskellObj;
34

35 36 37 38 39 40
/*
 * An abstract type representing the token returned by rts_lock() and
 * used when allocating objects and threads in the RTS.
 */
typedef struct Capability_ Capability;

41 42 43 44 45 46 47 48 49
/*
 * The public view of a Capability: we can be sure it starts with
 * these two components (but it may have more private fields).
 */
typedef struct CapabilityPublic_ {
    StgFunTable f;
    StgRegTable r;
} CapabilityPublic;

50 51 52 53 54 55
/* ----------------------------------------------------------------------------
   RTS configuration settings, for passing to hs_init_ghc()
   ------------------------------------------------------------------------- */

typedef enum {
    RtsOptsNone,         // +RTS causes an error
56 57
    RtsOptsIgnore,       // Ignore command line arguments
    RtsOptsIgnoreAll,    // Ignore command line and Environment arguments
58 59 60 61
    RtsOptsSafeOnly,     // safe RTS options allowed; others cause an error
    RtsOptsAll           // all RTS options allowed
  } RtsOptsEnabledEnum;

Simon Marlow's avatar
Simon Marlow committed
62 63
struct GCDetails_;

64 65 66 67
// The RtsConfig struct is passed (by value) to hs_init_ghc().  The
// reason for using a struct is extensibility: we can add more
// fields to this later without breaking existing client code.
typedef struct {
68 69

    // Whether to interpret +RTS options on the command line
70
    RtsOptsEnabledEnum rts_opts_enabled;
71

72 73 74
    // Whether to give RTS flag suggestions
    HsBool rts_opts_suggestions;

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 82 83
    // Whether to retain CAFs (default: false)
    HsBool keep_cafs;

84 85 86
    // Writer a for eventlog.
    const EventLogWriter *eventlog_writer;

87 88 89 90 91 92 93 94 95 96 97 98 99 100
    // Called before processing command-line flags, so that default
    // settings for RtsFlags can be provided.
    void (* defaultsHook) (void);

    // Called just before exiting
    void (* onExitHook) (void);

    // Called on a stack overflow, before exiting
    void (* stackOverflowHook) (W_ stack_size);

    // Called on heap overflow, before exiting
    void (* outOfHeapHook) (W_ request_size, W_ heap_size);

    // Called when malloc() fails, before exiting
101
    void (* mallocFailHook) (W_ request_size /* in bytes */, const char *msg);
102 103

    // Called for every GC
Simon Marlow's avatar
Simon Marlow committed
104
    void (* gcDoneHook) (const struct GCDetails_ *stats);
Simon Marlow's avatar
Simon Marlow committed
105 106 107 108

    // Called when GC sync takes too long (+RTS --long-gc-sync=<time>)
    void (* longGCSync) (uint32_t this_cap, Time time_ns);
    void (* longGCSyncEnd) (Time time_ns);
109 110 111 112 113 114 115
} RtsConfig;

// Clients should start with defaultRtsConfig and then customise it.
// Bah, I really wanted this to be a const struct value, but it seems
// you can't do that in C (it generates code).
extern const RtsConfig defaultRtsConfig;

Simon Marlow's avatar
Simon Marlow committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129
/* -----------------------------------------------------------------------------
   Statistics
   -------------------------------------------------------------------------- */

//
// Stats about a single GC
//
typedef struct GCDetails_ {
    // The generation number of this GC
  uint32_t gen;
    // Number of threads used in this GC
  uint32_t threads;
    // Number of bytes allocated since the previous GC
  uint64_t allocated_bytes;
Ömer Sinan Ağacan's avatar
Ömer Sinan Ağacan committed
130 131 132
    // Total amount of live data in the heap (incliudes large + compact data).
    // Updated after every GC. Data in uncollected generations (in minor GCs)
    // are considered live.
Simon Marlow's avatar
Simon Marlow committed
133 134 135 136 137 138 139 140 141 142 143 144 145
  uint64_t live_bytes;
    // Total amount of live data in large objects
  uint64_t large_objects_bytes;
    // Total amount of live data in compact regions
  uint64_t compact_bytes;
    // Total amount of slop (wasted memory)
  uint64_t slop_bytes;
    // Total amount of memory in use by the RTS
  uint64_t mem_in_use_bytes;
    // Total amount of data copied during this GC
  uint64_t copied_bytes;
    // In parallel GC, the max amount of data copied by any one thread
  uint64_t par_max_copied_bytes;
146 147
  // In parallel GC, the amount of balanced data copied by all threads
  uint64_t par_balanced_copied_bytes;
Simon Marlow's avatar
Simon Marlow committed
148 149 150 151 152 153
    // The time elapsed during synchronisation before GC
  Time sync_elapsed_ns;
    // The CPU time used during GC itself
  Time cpu_ns;
    // The time elapsed during GC itself
  Time elapsed_ns;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

    //
    // Concurrent garbage collector
    //

    // The CPU time used during the post-mark pause phase of the concurrent
    // nonmoving GC.
  Time nonmoving_gc_sync_cpu_ns;
    // The time elapsed during the post-mark pause phase of the concurrent
    // nonmoving GC.
  Time nonmoving_gc_sync_elapsed_ns;
    // The CPU time used during the post-mark pause phase of the concurrent
    // nonmoving GC.
  Time nonmoving_gc_cpu_ns;
    // The time elapsed during the post-mark pause phase of the concurrent
    // nonmoving GC.
  Time nonmoving_gc_elapsed_ns;
Simon Marlow's avatar
Simon Marlow committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
} GCDetails;

//
// Stats about the RTS currently, and since the start of execution
//
typedef struct _RTSStats {

  // -----------------------------------
  // Cumulative stats about memory use

    // Total number of GCs
  uint32_t gcs;
    // Total number of major (oldest generation) GCs
  uint32_t major_gcs;
    // Total bytes allocated
  uint64_t allocated_bytes;
Ömer Sinan Ağacan's avatar
Ömer Sinan Ağacan committed
187 188
    // Maximum live data (including large objects + compact regions) in the
    // heap. Updated after a major GC.
Simon Marlow's avatar
Simon Marlow committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  uint64_t max_live_bytes;
    // Maximum live data in large objects
  uint64_t max_large_objects_bytes;
    // Maximum live data in compact regions
  uint64_t max_compact_bytes;
    // Maximum slop
  uint64_t max_slop_bytes;
    // Maximum memory in use by the RTS
  uint64_t max_mem_in_use_bytes;
    // Sum of live bytes across all major GCs.  Divided by major_gcs
    // gives the average live data over the lifetime of the program.
  uint64_t cumulative_live_bytes;
    // Sum of copied_bytes across all GCs
  uint64_t copied_bytes;
    // Sum of copied_bytes across all parallel GCs
  uint64_t par_copied_bytes;
    // Sum of par_max_copied_bytes across all parallel GCs
  uint64_t cumulative_par_max_copied_bytes;
207
    // Sum of par_balanced_copied_byes across all parallel GCs.
208
  uint64_t cumulative_par_balanced_copied_bytes;
Simon Marlow's avatar
Simon Marlow committed
209 210 211

  // -----------------------------------
  // Cumulative stats about time use
212
  // (we use signed values here because due to inaccuracies in timers
Simon Marlow's avatar
Simon Marlow committed
213 214
  // the values can occasionally go slightly negative)

215 216 217 218
    // Total CPU time used by the init phase
  Time init_cpu_ns;
    // Total elapsed time used by the init phase
  Time init_elapsed_ns;
Simon Marlow's avatar
Simon Marlow committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    // Total CPU time used by the mutator
  Time mutator_cpu_ns;
    // Total elapsed time used by the mutator
  Time mutator_elapsed_ns;
    // Total CPU time used by the GC
  Time gc_cpu_ns;
    // Total elapsed time used by the GC
  Time gc_elapsed_ns;
    // Total CPU time (at the previous GC)
  Time cpu_ns;
    // Total elapsed time (at the previous GC)
  Time elapsed_ns;

  // -----------------------------------
  // Stats about the most recent GC

  GCDetails gc;

237 238 239
  // -----------------------------------
  // Internal Counters

240 241
    // The number of times a GC thread spun on its 'gc_spin' lock.
    // Will be zero if the rts was not built with PROF_SPIN
242
  uint64_t gc_spin_spin;
243 244
    // The number of times a GC thread yielded on its 'gc_spin' lock.
    // Will be zero if the rts was not built with PROF_SPIN
245
  uint64_t gc_spin_yield;
246 247
    // The number of times a GC thread spun on its 'mut_spin' lock.
    // Will be zero if the rts was not built with PROF_SPIN
248
  uint64_t mut_spin_spin;
249 250
    // The number of times a GC thread yielded on its 'mut_spin' lock.
    // Will be zero if the rts was not built with PROF_SPIN
251
  uint64_t mut_spin_yield;
252 253
    // The number of times a GC thread has checked for work across all parallel
    // GCs
254
  uint64_t any_work;
255 256
    // The number of times a GC thread has checked for work and found none
    // across all parallel GCs
257
  uint64_t no_work;
258 259
    // The number of times a GC thread has iterated it's outer loop across all
    // parallel GCs
260
  uint64_t scav_find_work;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

  // ----------------------------------
  // Concurrent garbage collector

    // The CPU time used during the post-mark pause phase of the concurrent
    // nonmoving GC.
  Time nonmoving_gc_sync_cpu_ns;
    // The time elapsed during the post-mark pause phase of the concurrent
    // nonmoving GC.
  Time nonmoving_gc_sync_elapsed_ns;
    // The maximum time elapsed during the post-mark pause phase of the
    // concurrent nonmoving GC.
  Time nonmoving_gc_sync_max_elapsed_ns;
    // The CPU time used during the post-mark pause phase of the concurrent
    // nonmoving GC.
  Time nonmoving_gc_cpu_ns;
    // The time elapsed during the post-mark pause phase of the concurrent
    // nonmoving GC.
  Time nonmoving_gc_elapsed_ns;
    // The maximum time elapsed during the post-mark pause phase of the
    // concurrent nonmoving GC.
  Time nonmoving_gc_max_elapsed_ns;
Simon Marlow's avatar
Simon Marlow committed
283 284 285 286 287 288 289 290 291
} RTSStats;

void getRTSStats (RTSStats *s);
int getRTSStatsEnabled (void);

// Returns the total number of bytes allocated since the start of the program.
// TODO: can we remove this?
uint64_t getAllocations (void);

292 293 294
/* ----------------------------------------------------------------------------
   Starting up and shutting down the Haskell RTS.
   ------------------------------------------------------------------------- */
295 296 297

/* DEPRECATED, use hs_init() or hs_init_ghc() instead  */
extern void startupHaskell         ( int argc, char *argv[],
298
                                     void (*init_root)(void) );
299 300

/* DEPRECATED, use hs_exit() instead  */
301
extern void shutdownHaskell        ( void );
302

303 304 305 306
/* Like hs_init(), but allows rtsopts. For more complicated usage,
 * use hs_init_ghc. */
extern void hs_init_with_rtsopts (int *argc, char **argv[]);

307 308 309 310 311 312 313 314 315
/*
 * GHC-specific version of hs_init() that allows specifying whether
 * +RTS ... -RTS options are allowed or not (default: only "safe"
 * options are allowed), and allows passing an option string that is
 * to be interpreted by the RTS only, not passed to the program.
 */
extern void hs_init_ghc (int *argc, char **argv[],   // program arguments
                         RtsConfig rts_config);      // RTS configuration

316 317
extern void shutdownHaskellAndExit (int exitCode, int fastExit)
    GNUC3_ATTRIBUTE(__noreturn__);
318

Ben Gamari's avatar
Ben Gamari committed
319
#if !defined(mingw32_HOST_OS)
320 321
extern void shutdownHaskellAndSignal (int sig, int fastExit)
     GNUC3_ATTRIBUTE(__noreturn__);
322 323
#endif

324
extern void getProgArgv            ( int *argc, char **argv[] );
325
extern void setProgArgv            ( int argc, char *argv[] );
326 327
extern void getFullProgArgv        ( int *argc, char **argv[] );
extern void setFullProgArgv        ( int argc, char *argv[] );
328
extern void freeFullProgArgv       ( void ) ;
329

330 331
/* exit() override */
extern void (*exitFn)(int);
332 333 334

/* ----------------------------------------------------------------------------
   Locking.
335

336
   You have to surround all access to the RtsAPI with these calls.
337
   ------------------------------------------------------------------------- */
338

339 340 341
// acquires a token which may be used to create new objects and
// evaluate them.
Capability *rts_lock (void);
342

343 344
// releases the token acquired with rts_lock().
void rts_unlock (Capability *token);
345

346 347 348 349 350 351 352 353 354
// If you are in a context where you know you have a current capability but
// do not know what it is, then use this to get it. Basically this only
// applies to "unsafe" foreign calls (as unsafe foreign calls are made with
// the capability held).
//
// WARNING: There is *no* guarantee this returns anything sensible (eg NULL)
// when there is no current capability.
Capability *rts_unsafeGetMyCapability (void);

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
/* ----------------------------------------------------------------------------
   Which cpu should the OS thread and Haskell thread run on?

   1. Run the current thread on the given capability:
     rts_setInCallCapability(cap, 0);

   2. Run the current thread on the given capability and set the cpu affinity
      for this thread:
     rts_setInCallCapability(cap, 1);

   3. Run the current thread on the given numa node:
     rts_pinThreadToNumaNode(node);

   4. Run the current thread on the given capability and on the given numa node:
     rts_setInCallCapability(cap, 0);
     rts_pinThreadToNumaNode(cap);
   ------------------------------------------------------------------------- */

373 374 375 376 377 378 379
// Specify the Capability that the current OS thread should run on when it calls
// into Haskell.  The actual capability will be calculated as the supplied
// value modulo the number of enabled Capabilities.
//
// Note that the thread may still be migrated by the RTS scheduler, but that
// will only happen if there are multiple threads running on one Capability and
// another Capability is free.
Simon Marlow's avatar
Simon Marlow committed
380 381 382 383 384
//
// If affinity is non-zero, the current thread will be bound to
// specific CPUs according to the prevailing affinity policy for the
// specified capability, set by either +RTS -qa or +RTS --numa.
void rts_setInCallCapability (int preferred_capability, int affinity);
385

386 387 388 389 390 391
// Specify the CPU Node that the current OS thread should run on when it calls
// into Haskell. The argument can be either a node number or capability number.
// The actual node will be calculated as the supplied value modulo the number
// of numa nodes.
void rts_pinThreadToNumaNode (int node);

392 393 394
/* ----------------------------------------------------------------------------
   Building Haskell objects from C datatypes.
   ------------------------------------------------------------------------- */
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
HaskellObj   rts_mkChar       ( Capability *, HsChar   c );
HaskellObj   rts_mkInt        ( Capability *, HsInt    i );
HaskellObj   rts_mkInt8       ( Capability *, HsInt8   i );
HaskellObj   rts_mkInt16      ( Capability *, HsInt16  i );
HaskellObj   rts_mkInt32      ( Capability *, HsInt32  i );
HaskellObj   rts_mkInt64      ( Capability *, HsInt64  i );
HaskellObj   rts_mkWord       ( Capability *, HsWord   w );
HaskellObj   rts_mkWord8      ( Capability *, HsWord8  w );
HaskellObj   rts_mkWord16     ( Capability *, HsWord16 w );
HaskellObj   rts_mkWord32     ( Capability *, HsWord32 w );
HaskellObj   rts_mkWord64     ( Capability *, HsWord64 w );
HaskellObj   rts_mkPtr        ( Capability *, HsPtr    a );
HaskellObj   rts_mkFunPtr     ( Capability *, HsFunPtr a );
HaskellObj   rts_mkFloat      ( Capability *, HsFloat  f );
HaskellObj   rts_mkDouble     ( Capability *, HsDouble f );
HaskellObj   rts_mkStablePtr  ( Capability *, HsStablePtr s );
HaskellObj   rts_mkBool       ( Capability *, HsBool   b );
HaskellObj   rts_mkString     ( Capability *, char    *s );

HaskellObj   rts_apply        ( Capability *, HaskellObj, HaskellObj );
415 416 417 418

/* ----------------------------------------------------------------------------
   Deconstructing Haskell objects
   ------------------------------------------------------------------------- */
419 420
HsChar       rts_getChar      ( HaskellObj );
HsInt        rts_getInt       ( HaskellObj );
sof's avatar
sof committed
421 422
HsInt8       rts_getInt8      ( HaskellObj );
HsInt16      rts_getInt16     ( HaskellObj );
423
HsInt32      rts_getInt32     ( HaskellObj );
sof's avatar
sof committed
424
HsInt64      rts_getInt64     ( HaskellObj );
425
HsWord       rts_getWord      ( HaskellObj );
sof's avatar
sof committed
426 427
HsWord8      rts_getWord8     ( HaskellObj );
HsWord16     rts_getWord16    ( HaskellObj );
428
HsWord32     rts_getWord32    ( HaskellObj );
sof's avatar
sof committed
429
HsWord64     rts_getWord64    ( HaskellObj );
430
HsPtr        rts_getPtr       ( HaskellObj );
431
HsFunPtr     rts_getFunPtr    ( HaskellObj );
432 433 434 435
HsFloat      rts_getFloat     ( HaskellObj );
HsDouble     rts_getDouble    ( HaskellObj );
HsStablePtr  rts_getStablePtr ( HaskellObj );
HsBool       rts_getBool      ( HaskellObj );
436 437 438 439 440

/* ----------------------------------------------------------------------------
   Evaluating Haskell expressions

   The versions ending in '_' allow you to specify an initial stack size.
441 442
   Note that these calls may cause Garbage Collection, so all HaskellObj
   references are rendered invalid by these calls.
443 444 445 446 447 448 449

   All of these functions take a (Capability **) - there is a
   Capability pointer both input and output.  We use an inout
   parameter because this is less error-prone for the client than a
   return value - the client could easily forget to use the return
   value, whereas incorrectly using an inout parameter will usually
   result in a type error.
450 451
   ------------------------------------------------------------------------- */

452 453 454 455 456 457 458 459
void rts_eval (/* inout */ Capability **,
               /* in    */ HaskellObj p,
               /* out */   HaskellObj *ret);

void rts_eval_ (/* inout */ Capability **,
                /* in    */ HaskellObj p,
                /* in    */ unsigned int stack_size,
                /* out   */ HaskellObj *ret);
460

461 462 463
void rts_evalIO (/* inout */ Capability **,
                 /* in    */ HaskellObj p,
                 /* out */   HaskellObj *ret);
464

465 466 467 468
void rts_evalStableIOMain (/* inout */ Capability **,
                           /* in    */ HsStablePtr s,
                           /* out */   HsStablePtr *ret);

469 470 471
void rts_evalStableIO (/* inout */ Capability **,
                       /* in    */ HsStablePtr s,
                       /* out */   HsStablePtr *ret);
472

473 474 475
void rts_evalLazyIO (/* inout */ Capability **,
                     /* in    */ HaskellObj p,
                     /* out */   HaskellObj *ret);
476

477 478 479 480
void rts_evalLazyIO_ (/* inout */ Capability **,
                      /* in    */ HaskellObj p,
                      /* in    */ unsigned int stack_size,
                      /* out   */ HaskellObj *ret);
481

482
void rts_checkSchedStatus (char* site, Capability *);
483

484
SchedulerStatus rts_getSchedStatus (Capability *cap);
485

Simon Marlow's avatar
Simon Marlow committed
486 487 488 489 490 491 492 493 494 495 496 497 498
/*
 * The RTS allocates some thread-local data when you make a call into
 * Haskell using one of the rts_eval() functions.  This data is not
 * normally freed until hs_exit().  If you want to free it earlier
 * than this, perhaps because the thread is about to exit, then call
 * rts_done() from the thread.
 *
 * It is safe to make more rts_eval() calls after calling rts_done(),
 * but the next one will cause allocation of the thread-local memory
 * again.
 */
void rts_done (void);

499 500 501 502 503 504
/* --------------------------------------------------------------------------
   Wrapper closures

   These are used by foreign export and foreign import "wrapper" stubs.
   ----------------------------------------------------------------------- */

505 506
// When producing Windows DLLs the we need to know which symbols are in the
//      local package/DLL vs external ones.
507
//
508 509
//      Note that RtsAPI.h is also included by foreign export stubs in
//      the base package itself.
510
//
511
#if defined(COMPILING_WINDOWS_DLL) && !defined(COMPILING_BASE_PACKAGE)
512 513
__declspec(dllimport) extern StgWord base_GHCziTopHandler_runIO_closure[];
__declspec(dllimport) extern StgWord base_GHCziTopHandler_runNonIO_closure[];
514
#else
515 516
extern StgWord base_GHCziTopHandler_runIO_closure[];
extern StgWord base_GHCziTopHandler_runNonIO_closure[];
517
#endif
518

519 520
#define runIO_closure     base_GHCziTopHandler_runIO_closure
#define runNonIO_closure  base_GHCziTopHandler_runNonIO_closure
521 522

/* ------------------------------------------------------------------------ */
523

Ben Gamari's avatar
Ben Gamari committed
524
#if defined(__cplusplus)
525 526
}
#endif