From f201c207001c62b2ab6e1d5af2e126407da9c983 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 10 Nov 2022 17:52:27 -0700 Subject: [PATCH 1/2] Add _PyRuntimeState.imports.lock. --- Include/internal/pycore_import.h | 6 ++++++ Include/internal/pycore_runtime_init.h | 7 +++++++ Python/import.c | 6 +++--- Tools/c-analyzer/cpython/globals-to-fix.tsv | 3 --- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 7e8d73be5b9ac1..80c4c5260a3832 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -21,6 +21,12 @@ struct _import_runtime_state { This is initialized lazily in _PyImport_FixupExtensionObject(). Modules are added there and looked up in _imp.find_extension(). */ PyObject *extensions; + /* The global import lock. */ + struct { + PyThread_type_lock mutex; + unsigned long thread; + int level; + } lock; }; diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index 9a2aad24b56970..01cfb67a2bc927 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -39,6 +39,13 @@ extern "C" { .types = { \ .next_version_tag = 1, \ }, \ + .imports = { \ + .lock = { \ + .mutex = NULL, \ + .thread = PYTHREAD_INVALID_THREAD_ID, \ + .level = 0, \ + }, \ + }, \ .global_objects = { \ .singletons = { \ .small_ints = _Py_small_ints_INIT, \ diff --git a/Python/import.c b/Python/import.c index daee16ec4ddddf..fc21844bb97819 100644 --- a/Python/import.c +++ b/Python/import.c @@ -94,9 +94,9 @@ _PyImportZip_Init(PyThreadState *tstate) in different threads to return with a partially loaded module. These calls are serialized by the global interpreter lock. */ -static PyThread_type_lock import_lock = NULL; -static unsigned long import_lock_thread = PYTHREAD_INVALID_THREAD_ID; -static int import_lock_level = 0; +#define import_lock _PyRuntime.imports.lock.mutex +#define import_lock_thread _PyRuntime.imports.lock.thread +#define import_lock_level _PyRuntime.imports.lock.level void _PyImport_AcquireLock(void) diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index aaae1e851480eb..cbe20cbaac90d1 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -368,7 +368,6 @@ Python/dtoa.c - p5s - Python/fileutils.c - _Py_open_cloexec_works - Python/fileutils.c - force_ascii - Python/fileutils.c set_inheritable ioctl_works - -Python/import.c - import_lock - Python/import.c import_find_and_load header - #----------------------- @@ -431,8 +430,6 @@ Python/bootstrap_hash.c - urandom_cache - Python/ceval_gil.c make_pending_calls busy - Python/ceval.c _PyEval_SetProfile reentrant - Python/ceval.c _PyEval_SetTrace reentrant - -Python/import.c - import_lock_level - -Python/import.c - import_lock_thread - Python/import.c import_find_and_load accumulated - Python/import.c import_find_and_load import_level - Python/modsupport.c - _Py_PackageContext - From 7472eb440ae1a66b13e9fc8b5ebf90967dbf10fe Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 10 Nov 2022 18:02:51 -0700 Subject: [PATCH 2/2] Add _PyRuntime.imports.find_and_load. --- Include/internal/pycore_import.h | 5 +++++ Include/internal/pycore_runtime_init.h | 3 +++ Python/import.c | 9 ++++++--- Tools/c-analyzer/cpython/globals-to-fix.tsv | 3 --- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h index 80c4c5260a3832..8ba9666cdcf9a9 100644 --- a/Include/internal/pycore_import.h +++ b/Include/internal/pycore_import.h @@ -27,6 +27,11 @@ struct _import_runtime_state { unsigned long thread; int level; } lock; + struct { + int import_level; + _PyTime_t accumulated; + int header; + } find_and_load; }; diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index 01cfb67a2bc927..ea98c3784ff300 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -45,6 +45,9 @@ extern "C" { .thread = PYTHREAD_INVALID_THREAD_ID, \ .level = 0, \ }, \ + .find_and_load = { \ + .header = 1, \ + }, \ }, \ .global_objects = { \ .singletons = { \ diff --git a/Python/import.c b/Python/import.c index fc21844bb97819..c12b3e07b5d437 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1759,8 +1759,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name) PyObject *mod = NULL; PyInterpreterState *interp = tstate->interp; int import_time = _PyInterpreterState_GetConfig(interp)->import_time; - static int import_level; - static _PyTime_t accumulated; +#define import_level _PyRuntime.imports.find_and_load.import_level +#define accumulated _PyRuntime.imports.find_and_load.accumulated _PyTime_t t1 = 0, accumulated_copy = accumulated; @@ -1781,12 +1781,13 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name) * _PyDict_GetItemIdWithError(). */ if (import_time) { - static int header = 1; +#define header _PyRuntime.imports.find_and_load.header if (header) { fputs("import time: self [us] | cumulative | imported package\n", stderr); header = 0; } +#undef header import_level++; t1 = _PyTime_GetPerfCounter(); @@ -1816,6 +1817,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name) } return mod; +#undef import_level +#undef accumulated } PyObject * diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index cbe20cbaac90d1..b60f16db9a28ac 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -368,7 +368,6 @@ Python/dtoa.c - p5s - Python/fileutils.c - _Py_open_cloexec_works - Python/fileutils.c - force_ascii - Python/fileutils.c set_inheritable ioctl_works - -Python/import.c import_find_and_load header - #----------------------- # unlikely to change after init (or main thread) @@ -430,8 +429,6 @@ Python/bootstrap_hash.c - urandom_cache - Python/ceval_gil.c make_pending_calls busy - Python/ceval.c _PyEval_SetProfile reentrant - Python/ceval.c _PyEval_SetTrace reentrant - -Python/import.c import_find_and_load accumulated - -Python/import.c import_find_and_load import_level - Python/modsupport.c - _Py_PackageContext - Python/thread_pthread_stubs.h - py_tls_entries - Python/pyfpe.c - PyFPE_counter -