Skip to content

Commit

Permalink
Move observability-relevant structure fields to top
Browse files Browse the repository at this point in the history
Some structures have fields that are used by out-of-process tools, like
Austin. Having these fields defined after some more complex structures
makes it hard to maintain these tools. With this change, we move the
declaration of the most useful fields to the top of the structure
definition. This reduces the amount of irrelevant information that the
mentioned tools have to replicate to retrieve the actually useful data
  • Loading branch information
P403n1x87 committed Jun 3, 2023
1 parent 94a1eea commit 945c762
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
14 changes: 9 additions & 5 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ enum _frameowner {
typedef struct _PyInterpreterFrame {
PyCodeObject *f_code; /* Strong reference */
struct _PyInterpreterFrame *previous;
PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */
PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */
PyObject *f_builtins; /* Borrowed reference. Only valid if not on C stack */
PyObject *f_locals; /* Strong reference, may be NULL. Only valid if not on C stack */
PyFrameObject *frame_obj; /* Strong reference, may be NULL. Only valid if not on C stack */
// NOTE: This is not necessarily the last instruction started in the given
// frame. Rather, it is the code unit *prior to* the *next* instruction. For
// example, it may be an inline CACHE entry, an instruction we just jumped
// over, or (in the case of a newly-created frame) a totally invalid value:
_Py_CODEUNIT *prev_instr;

/* The fields above this line are declared as early as possible to
facilitate out-of-process observability tools. */

PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */
PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */
PyObject *f_builtins; /* Borrowed reference. Only valid if not on C stack */
PyObject *f_locals; /* Strong reference, may be NULL. Only valid if not on C stack */
PyFrameObject *frame_obj; /* Strong reference, may be NULL. Only valid if not on C stack */
int stacktop; /* Offset of TOS from localsplus */
/* The return_offset determines where a `RETURN` should go in the caller,
* relative to `prev_instr`.
Expand Down
40 changes: 22 additions & 18 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ struct _Py_long_state {
The PyInterpreterState typedef is in Include/pytypedefs.h.
*/
struct _is {

struct _ceval_state ceval;
PyInterpreterState *next;

uint64_t monitoring_version;
Expand All @@ -72,6 +70,28 @@ struct _is {
Get runtime from tstate: tstate->interp->runtime. */
struct pyruntimestate *runtime;

struct _gc_runtime_state gc;

/* The following fields are here to avoid allocation during init.
The data is exposed through PyInterpreterState pointer fields.
These fields should not be accessed directly outside of init.
All other PyInterpreterState pointer fields are populated when
needed and default to NULL.
For now there are some exceptions to that rule, which require
allocation during init. These will be addressed on a case-by-case
basis. Also see _PyRuntimeState regarding the various mutex fields.
*/

/* The per-interpreter GIL, which might not be used. */
struct _gil_runtime_state _gil;

/* The fields above this line are declared as early as possible to
facilitate out-of-process observability tools. */

struct _ceval_state ceval;

int64_t id;
int64_t id_refcount;
int requires_idref;
Expand All @@ -93,7 +113,6 @@ struct _is {

struct _obmalloc_state obmalloc;

struct _gc_runtime_state gc;

struct _import_state imports;

Expand Down Expand Up @@ -176,21 +195,6 @@ struct _is {
struct _Py_interp_cached_objects cached_objects;
struct _Py_interp_static_objects static_objects;

/* The following fields are here to avoid allocation during init.
The data is exposed through PyInterpreterState pointer fields.
These fields should not be accessed directly outside of init.
All other PyInterpreterState pointer fields are populated when
needed and default to NULL.
For now there are some exceptions to that rule, which require
allocation during init. These will be addressed on a case-by-case
basis. Also see _PyRuntimeState regarding the various mutex fields.
*/

/* The per-interpreter GIL, which might not be used. */
struct _gil_runtime_state _gil;

/* the initial PyInterpreterState.threads.head */
PyThreadState _initial_thread;
};
Expand Down
19 changes: 11 additions & 8 deletions Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ typedef struct pyruntimestate {
to access it, don't access it directly. */
_Py_atomic_address _finalizing;

struct _pymem_allocators allocators;
struct _obmalloc_global_state obmalloc;
struct pyhash_runtime_state pyhash_state;
struct _time_runtime_state time;
struct _pythread_runtime_state threads;
struct _signals_runtime_state signals;

struct pyinterpreters {
PyThread_type_lock mutex;
/* The linked list of interpreters, newest first. */
Expand All @@ -110,12 +103,22 @@ typedef struct pyruntimestate {
int64_t next_id;
} interpreters;
// XXX Remove this field once we have a tp_* slot.
unsigned long main_thread;

/* The fields above this line are declared as early as possible to
facilitate out-of-process observability tools. */

struct _xidregistry {
PyThread_type_lock mutex;
struct _xidregitem *head;
} xidregistry;

unsigned long main_thread;
struct _pymem_allocators allocators;
struct _obmalloc_global_state obmalloc;
struct pyhash_runtime_state pyhash_state;
struct _time_runtime_state time;
struct _pythread_runtime_state threads;
struct _signals_runtime_state signals;

/* Used for the thread state bound to the current thread. */
Py_tss_t autoTSSkey;
Expand Down

0 comments on commit 945c762

Please sign in to comment.