Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:42

0001 // Statistics on Python performance.
0002 //
0003 // API:
0004 //
0005 // - _Py_INCREF_STAT_INC() and _Py_DECREF_STAT_INC() used by Py_INCREF()
0006 //   and Py_DECREF().
0007 // - _Py_stats variable
0008 //
0009 // Functions of the sys module:
0010 //
0011 // - sys._stats_on()
0012 // - sys._stats_off()
0013 // - sys._stats_clear()
0014 // - sys._stats_dump()
0015 //
0016 // Python must be built with ./configure --enable-pystats to define the
0017 // Py_STATS macro.
0018 //
0019 // Define _PY_INTERPRETER macro to increment interpreter_increfs and
0020 // interpreter_decrefs. Otherwise, increment increfs and decrefs.
0021 //
0022 // The number of incref operations counted by `incref` and
0023 // `interpreter_incref` is the number of increment operations, which is
0024 // not equal to the total of all reference counts. A single increment
0025 // operation may increase the reference count of an object by more than
0026 // one. For example, see `_Py_RefcntAdd`.
0027 
0028 #ifndef Py_CPYTHON_PYSTATS_H
0029 #  error "this header file must not be included directly"
0030 #endif
0031 
0032 #define PYSTATS_MAX_UOP_ID 512
0033 
0034 #define SPECIALIZATION_FAILURE_KINDS 36
0035 
0036 /* Stats for determining who is calling PyEval_EvalFrame */
0037 #define EVAL_CALL_TOTAL 0
0038 #define EVAL_CALL_VECTOR 1
0039 #define EVAL_CALL_GENERATOR 2
0040 #define EVAL_CALL_LEGACY 3
0041 #define EVAL_CALL_FUNCTION_VECTORCALL 4
0042 #define EVAL_CALL_BUILD_CLASS 5
0043 #define EVAL_CALL_SLOT 6
0044 #define EVAL_CALL_FUNCTION_EX 7
0045 #define EVAL_CALL_API 8
0046 #define EVAL_CALL_METHOD 9
0047 
0048 #define EVAL_CALL_KINDS 10
0049 
0050 typedef struct _specialization_stats {
0051     uint64_t success;
0052     uint64_t failure;
0053     uint64_t hit;
0054     uint64_t deferred;
0055     uint64_t miss;
0056     uint64_t deopt;
0057     uint64_t failure_kinds[SPECIALIZATION_FAILURE_KINDS];
0058 } SpecializationStats;
0059 
0060 typedef struct _opcode_stats {
0061     SpecializationStats specialization;
0062     uint64_t execution_count;
0063     uint64_t pair_count[256];
0064 } OpcodeStats;
0065 
0066 typedef struct _call_stats {
0067     uint64_t inlined_py_calls;
0068     uint64_t pyeval_calls;
0069     uint64_t frames_pushed;
0070     uint64_t frame_objects_created;
0071     uint64_t eval_calls[EVAL_CALL_KINDS];
0072 } CallStats;
0073 
0074 typedef struct _object_stats {
0075     uint64_t increfs;
0076     uint64_t decrefs;
0077     uint64_t interpreter_increfs;
0078     uint64_t interpreter_decrefs;
0079     uint64_t allocations;
0080     uint64_t allocations512;
0081     uint64_t allocations4k;
0082     uint64_t allocations_big;
0083     uint64_t frees;
0084     uint64_t to_freelist;
0085     uint64_t from_freelist;
0086     uint64_t inline_values;
0087     uint64_t dict_materialized_on_request;
0088     uint64_t dict_materialized_new_key;
0089     uint64_t dict_materialized_too_big;
0090     uint64_t dict_materialized_str_subclass;
0091     uint64_t type_cache_hits;
0092     uint64_t type_cache_misses;
0093     uint64_t type_cache_dunder_hits;
0094     uint64_t type_cache_dunder_misses;
0095     uint64_t type_cache_collisions;
0096     /* Temporary value used during GC */
0097     uint64_t object_visits;
0098 } ObjectStats;
0099 
0100 typedef struct _gc_stats {
0101     uint64_t collections;
0102     uint64_t object_visits;
0103     uint64_t objects_collected;
0104 } GCStats;
0105 
0106 typedef struct _uop_stats {
0107     uint64_t execution_count;
0108     uint64_t miss;
0109     uint64_t pair_count[PYSTATS_MAX_UOP_ID + 1];
0110 } UOpStats;
0111 
0112 #define _Py_UOP_HIST_SIZE 32
0113 
0114 typedef struct _optimization_stats {
0115     uint64_t attempts;
0116     uint64_t traces_created;
0117     uint64_t traces_executed;
0118     uint64_t uops_executed;
0119     uint64_t trace_stack_overflow;
0120     uint64_t trace_stack_underflow;
0121     uint64_t trace_too_long;
0122     uint64_t trace_too_short;
0123     uint64_t inner_loop;
0124     uint64_t recursive_call;
0125     uint64_t low_confidence;
0126     uint64_t executors_invalidated;
0127     UOpStats opcode[PYSTATS_MAX_UOP_ID + 1];
0128     uint64_t unsupported_opcode[256];
0129     uint64_t trace_length_hist[_Py_UOP_HIST_SIZE];
0130     uint64_t trace_run_length_hist[_Py_UOP_HIST_SIZE];
0131     uint64_t optimized_trace_length_hist[_Py_UOP_HIST_SIZE];
0132     uint64_t optimizer_attempts;
0133     uint64_t optimizer_successes;
0134     uint64_t optimizer_failure_reason_no_memory;
0135     uint64_t remove_globals_builtins_changed;
0136     uint64_t remove_globals_incorrect_keys;
0137     uint64_t error_in_opcode[PYSTATS_MAX_UOP_ID + 1];
0138 } OptimizationStats;
0139 
0140 typedef struct _rare_event_stats {
0141     /* Setting an object's class, obj.__class__ = ... */
0142     uint64_t set_class;
0143     /* Setting the bases of a class, cls.__bases__ = ... */
0144     uint64_t set_bases;
0145     /* Setting the PEP 523 frame eval function, _PyInterpreterState_SetFrameEvalFunc() */
0146     uint64_t set_eval_frame_func;
0147     /* Modifying the builtins,  __builtins__.__dict__[var] = ... */
0148     uint64_t builtin_dict;
0149     /* Modifying a function, e.g. func.__defaults__ = ..., etc. */
0150     uint64_t func_modification;
0151     /* Modifying a dict that is being watched */
0152     uint64_t watched_dict_modification;
0153     uint64_t watched_globals_modification;
0154 } RareEventStats;
0155 
0156 typedef struct _stats {
0157     OpcodeStats opcode_stats[256];
0158     CallStats call_stats;
0159     ObjectStats object_stats;
0160     OptimizationStats optimization_stats;
0161     RareEventStats rare_event_stats;
0162     GCStats *gc_stats;
0163 } PyStats;
0164 
0165 
0166 // Export for shared extensions like 'math'
0167 PyAPI_DATA(PyStats*) _Py_stats;
0168 
0169 #ifdef _PY_INTERPRETER
0170 #  define _Py_INCREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.interpreter_increfs++; } while (0)
0171 #  define _Py_DECREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.interpreter_decrefs++; } while (0)
0172 #else
0173 #  define _Py_INCREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.increfs++; } while (0)
0174 #  define _Py_DECREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.decrefs++; } while (0)
0175 #endif