Skip to content

Commit

Permalink
Move jl_task_t to julia_threads.h (#57117)
Browse files Browse the repository at this point in the history
This is a subset of PR #56477 . I hope that it will be easier to get
this merged first, as it just moves things around, afterwards we can
update #56477 and it will be considerably smaller, and will thus
hopefully break less often (and in less bad ways) over time. (I can
perform the required update of that PR, too).

See also PR #57116 which contains another independent part of that PR.
  • Loading branch information
fingolfin authored Jan 22, 2025
1 parent 7f99e95 commit 61e8f1d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 77 deletions.
85 changes: 8 additions & 77 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,20 @@ typedef struct _jl_tls_states_t *jl_ptls_t;
#endif
#include "gc-interface.h"
#include "julia_atomics.h"
#include "julia_threads.h"
#include "julia_assert.h"

// the common fields are hidden before the pointer, but the following macro is
// used to indicate which types below are subtypes of jl_value_t
#define JL_DATA_TYPE
typedef struct _jl_value_t jl_value_t;
#include "julia_threads.h"

#ifdef __cplusplus
extern "C" {
#endif

// core data types ------------------------------------------------------------

// the common fields are hidden before the pointer, but the following macro is
// used to indicate which types below are subtypes of jl_value_t
#define JL_DATA_TYPE

typedef struct _jl_value_t jl_value_t;

struct _jl_taggedvalue_bits {
uintptr_t gc:2;
uintptr_t in_image:1;
Expand Down Expand Up @@ -484,9 +483,6 @@ typedef struct _jl_abi_override_t {
jl_method_instance_t *def;
} jl_abi_override_t;

// all values are callable as Functions
typedef jl_value_t jl_function_t;

typedef struct {
JL_DATA_TYPE
jl_sym_t *name;
Expand Down Expand Up @@ -2262,12 +2258,8 @@ JL_DLLEXPORT void jl_sigatomic_end(void);

// tasks and exceptions -------------------------------------------------------

typedef struct _jl_timing_block_t jl_timing_block_t;
typedef struct _jl_timing_event_t jl_timing_event_t;
typedef struct _jl_excstack_t jl_excstack_t;

// info describing an exception handler
typedef struct _jl_handler_t {
struct _jl_handler_t {
jl_jmp_buf eh_ctx;
jl_gcframe_t *gcstack;
jl_value_t *scope;
Expand All @@ -2277,68 +2269,7 @@ typedef struct _jl_handler_t {
sig_atomic_t defer_signal;
jl_timing_block_t *timing_stack;
size_t world_age;
} jl_handler_t;

#define JL_RNG_SIZE 5 // xoshiro 4 + splitmix 1

typedef struct _jl_task_t {
JL_DATA_TYPE
jl_value_t *next; // invasive linked list for scheduler
jl_value_t *queue; // invasive linked list for scheduler
jl_value_t *tls;
jl_value_t *donenotify;
jl_value_t *result;
jl_value_t *scope;
jl_function_t *start;
_Atomic(uint8_t) _state;
uint8_t sticky; // record whether this Task can be migrated to a new thread
uint16_t priority;
_Atomic(uint8_t) _isexception; // set if `result` is an exception to throw or that we exited with
uint8_t pad0[3];
// === 64 bytes (cache line)
uint64_t rngState[JL_RNG_SIZE];
// flag indicating whether or not to record timing metrics for this task
uint8_t metrics_enabled;
uint8_t pad1[3];
// timestamp this task first entered the run queue
_Atomic(uint64_t) first_enqueued_at;
// timestamp this task was most recently scheduled to run
_Atomic(uint64_t) last_started_running_at;
// time this task has spent running; updated when it yields or finishes.
_Atomic(uint64_t) running_time_ns;
// === 64 bytes (cache line)
// timestamp this task finished (i.e. entered state DONE or FAILED).
_Atomic(uint64_t) finished_at;

// hidden state:

// id of owning thread - does not need to be defined until the task runs
_Atomic(int16_t) tid;
// threadpool id
int8_t threadpoolid;
// Reentrancy bits
// Bit 0: 1 if we are currently running inference/codegen
// Bit 1-2: 0-3 counter of how many times we've reentered inference
// Bit 3: 1 if we are writing the image and inference is illegal
uint8_t reentrant_timing;
// 2 bytes of padding on 32-bit, 6 bytes on 64-bit
// uint16_t padding2_32;
// uint48_t padding2_64;
// saved gc stack top for context switches
jl_gcframe_t *gcstack;
size_t world_age;
// quick lookup for current ptls
jl_ptls_t ptls; // == jl_all_tls_states[tid]
#ifdef USE_TRACY
const char *name;
#endif
// saved exception stack
jl_excstack_t *excstack;
// current exception handler
jl_handler_t *eh;
// saved thread state
jl_ucontext_t ctx; // pointer into stkbuf, if suspended
} jl_task_t;
};

#define JL_TASK_STATE_RUNNABLE 0
#define JL_TASK_STATE_DONE 1
Expand Down
70 changes: 70 additions & 0 deletions src/julia_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,76 @@ typedef struct _jl_tls_states_t {
#endif
} jl_tls_states_t;

#define JL_RNG_SIZE 5 // xoshiro 4 + splitmix 1

// all values are callable as Functions
typedef jl_value_t jl_function_t;

typedef struct _jl_timing_block_t jl_timing_block_t;
typedef struct _jl_timing_event_t jl_timing_event_t;
typedef struct _jl_excstack_t jl_excstack_t;

typedef struct _jl_handler_t jl_handler_t;

typedef struct _jl_task_t {
JL_DATA_TYPE
jl_value_t *next; // invasive linked list for scheduler
jl_value_t *queue; // invasive linked list for scheduler
jl_value_t *tls;
jl_value_t *donenotify;
jl_value_t *result;
jl_value_t *scope;
jl_function_t *start;
_Atomic(uint8_t) _state;
uint8_t sticky; // record whether this Task can be migrated to a new thread
uint16_t priority;
_Atomic(uint8_t) _isexception; // set if `result` is an exception to throw or that we exited with
uint8_t pad0[3];
// === 64 bytes (cache line)
uint64_t rngState[JL_RNG_SIZE];
// flag indicating whether or not to record timing metrics for this task
uint8_t metrics_enabled;
uint8_t pad1[3];
// timestamp this task first entered the run queue
_Atomic(uint64_t) first_enqueued_at;
// timestamp this task was most recently scheduled to run
_Atomic(uint64_t) last_started_running_at;
// time this task has spent running; updated when it yields or finishes.
_Atomic(uint64_t) running_time_ns;
// === 64 bytes (cache line)
// timestamp this task finished (i.e. entered state DONE or FAILED).
_Atomic(uint64_t) finished_at;

// hidden state:

// id of owning thread - does not need to be defined until the task runs
_Atomic(int16_t) tid;
// threadpool id
int8_t threadpoolid;
// Reentrancy bits
// Bit 0: 1 if we are currently running inference/codegen
// Bit 1-2: 0-3 counter of how many times we've reentered inference
// Bit 3: 1 if we are writing the image and inference is illegal
uint8_t reentrant_timing;
// 2 bytes of padding on 32-bit, 6 bytes on 64-bit
// uint16_t padding2_32;
// uint48_t padding2_64;
// saved gc stack top for context switches
jl_gcframe_t *gcstack;
size_t world_age;
// quick lookup for current ptls
jl_ptls_t ptls; // == jl_all_tls_states[tid]
#ifdef USE_TRACY
const char *name;
#endif
// saved exception stack
jl_excstack_t *excstack;
// current exception handler
jl_handler_t *eh;
// saved thread state
jl_ucontext_t ctx; // pointer into stkbuf, if suspended
} jl_task_t;

JL_DLLEXPORT void *jl_get_ptls_states(void);

// Update codegen version in `ccall.cpp` after changing either `pause` or `wake`
Expand Down

0 comments on commit 61e8f1d

Please sign in to comment.