Kaydet (Commit) 40f6b121 authored tarafından Antoine Pitrou's avatar Antoine Pitrou

Issue #21555: simplify code in gcmodule.c by using the pytime.h functions…

Issue #21555: simplify code in gcmodule.c by using the pytime.h functions instead of trying to call time.time() via the C API.
Patch by Geoffrey Spear.
üst b19e75d0
...@@ -1250,6 +1250,7 @@ Paul Sokolovsky ...@@ -1250,6 +1250,7 @@ Paul Sokolovsky
Evgeny Sologubov Evgeny Sologubov
Cody Somerville Cody Somerville
Edoardo Spadolini Edoardo Spadolini
Geoffrey Spear
Clay Spence Clay Spence
Stefan Sperling Stefan Sperling
Nicholas Spies Nicholas Spies
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "Python.h" #include "Python.h"
#include "frameobject.h" /* for PyFrame_ClearFreeList */ #include "frameobject.h" /* for PyFrame_ClearFreeList */
#include "pytime.h" /* for _PyTime_gettimeofday, _PyTime_INTERVAL */
/* Get an object's GC head */ /* Get an object's GC head */
#define AS_GC(o) ((PyGC_Head *)(o)-1) #define AS_GC(o) ((PyGC_Head *)(o)-1)
...@@ -166,7 +167,6 @@ static Py_ssize_t long_lived_pending = 0; ...@@ -166,7 +167,6 @@ static Py_ssize_t long_lived_pending = 0;
DEBUG_UNCOLLECTABLE | \ DEBUG_UNCOLLECTABLE | \
DEBUG_SAVEALL DEBUG_SAVEALL
static int debug; static int debug;
static PyObject *tmod = NULL;
/* Running stats per generation */ /* Running stats per generation */
struct gc_generation_stats { struct gc_generation_stats {
...@@ -894,26 +894,6 @@ clear_freelists(void) ...@@ -894,26 +894,6 @@ clear_freelists(void)
(void)PySet_ClearFreeList(); (void)PySet_ClearFreeList();
} }
static double
get_time(void)
{
double result = 0;
if (tmod != NULL) {
_Py_IDENTIFIER(time);
PyObject *f = _PyObject_CallMethodId(tmod, &PyId_time, NULL);
if (f == NULL) {
PyErr_Clear();
}
else {
if (PyFloat_Check(f))
result = PyFloat_AsDouble(f);
Py_DECREF(f);
}
}
return result;
}
/* This is the main function. Read this to understand how the /* This is the main function. Read this to understand how the
* collection process works. */ * collection process works. */
static Py_ssize_t static Py_ssize_t
...@@ -928,7 +908,8 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, ...@@ -928,7 +908,8 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable,
PyGC_Head unreachable; /* non-problematic unreachable trash */ PyGC_Head unreachable; /* non-problematic unreachable trash */
PyGC_Head finalizers; /* objects with, & reachable from, __del__ */ PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
PyGC_Head *gc; PyGC_Head *gc;
double t1 = 0.0; _PyTime_timeval t1;
struct gc_generation_stats *stats = &generation_stats[generation]; struct gc_generation_stats *stats = &generation_stats[generation];
if (debug & DEBUG_STATS) { if (debug & DEBUG_STATS) {
...@@ -938,7 +919,8 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, ...@@ -938,7 +919,8 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable,
for (i = 0; i < NUM_GENERATIONS; i++) for (i = 0; i < NUM_GENERATIONS; i++)
PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d", PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d",
gc_list_size(GEN_HEAD(i))); gc_list_size(GEN_HEAD(i)));
t1 = get_time(); _PyTime_gettimeofday(&t1);
PySys_WriteStderr("\n"); PySys_WriteStderr("\n");
} }
...@@ -1042,7 +1024,9 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, ...@@ -1042,7 +1024,9 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable,
debug_cycle("uncollectable", FROM_GC(gc)); debug_cycle("uncollectable", FROM_GC(gc));
} }
if (debug & DEBUG_STATS) { if (debug & DEBUG_STATS) {
double t2 = get_time(); _PyTime_timeval t2;
_PyTime_gettimeofday(&t2);
if (m == 0 && n == 0) if (m == 0 && n == 0)
PySys_WriteStderr("gc: done"); PySys_WriteStderr("gc: done");
else else
...@@ -1051,10 +1035,7 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, ...@@ -1051,10 +1035,7 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable,
"%" PY_FORMAT_SIZE_T "d unreachable, " "%" PY_FORMAT_SIZE_T "d unreachable, "
"%" PY_FORMAT_SIZE_T "d uncollectable", "%" PY_FORMAT_SIZE_T "d uncollectable",
n+m, n); n+m, n);
if (t1 && t2) { PySys_WriteStderr(", %.4fs elapsed\n", _PyTime_INTERVAL(t1, t2));
PySys_WriteStderr(", %.4fs elapsed", t2-t1);
}
PySys_WriteStderr(".\n");
} }
/* Append instances in the uncollectable set to a Python /* Append instances in the uncollectable set to a Python
...@@ -1581,18 +1562,6 @@ PyInit_gc(void) ...@@ -1581,18 +1562,6 @@ PyInit_gc(void)
if (PyModule_AddObject(m, "callbacks", callbacks) < 0) if (PyModule_AddObject(m, "callbacks", callbacks) < 0)
return NULL; return NULL;
/* Importing can't be done in collect() because collect()
* can be called via PyGC_Collect() in Py_Finalize().
* This wouldn't be a problem, except that <initialized> is
* reset to 0 before calling collect which trips up
* the import and triggers an assertion.
*/
if (tmod == NULL) {
tmod = PyImport_ImportModuleNoBlock("time");
if (tmod == NULL)
PyErr_Clear();
}
#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return NULL #define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return NULL
ADD_INT(DEBUG_STATS); ADD_INT(DEBUG_STATS);
ADD_INT(DEBUG_COLLECTABLE); ADD_INT(DEBUG_COLLECTABLE);
...@@ -1681,7 +1650,6 @@ void ...@@ -1681,7 +1650,6 @@ void
_PyGC_Fini(void) _PyGC_Fini(void)
{ {
Py_CLEAR(callbacks); Py_CLEAR(callbacks);
Py_CLEAR(tmod);
} }
/* for debugging */ /* for debugging */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment