@@ -40,6 +40,83 @@ Py_ssize_t _Py_QuickenedCount = 0;
4040#if SPECIALIZATION_STATS
4141SpecializationStats _specialization_stats [256 ] = { 0 };
4242
43+ #define ADD_STAT_TO_DICT (res , field ) \
44+ do { \
45+ PyObject *val = PyLong_FromUnsignedLongLong(stats->field); \
46+ if (val == NULL) { \
47+ Py_DECREF(res); \
48+ return NULL; \
49+ } \
50+ if (PyDict_SetItemString(res, #field, val) == -1) { \
51+ Py_DECREF(res); \
52+ Py_DECREF(val); \
53+ return NULL; \
54+ } \
55+ Py_DECREF(val); \
56+ } while(0);
57+
58+ static PyObject *
59+ stats_to_dict (SpecializationStats * stats )
60+ {
61+ PyObject * res = PyDict_New ();
62+ if (res == NULL ) {
63+ return NULL ;
64+ }
65+ ADD_STAT_TO_DICT (res , specialization_success );
66+ ADD_STAT_TO_DICT (res , specialization_failure );
67+ ADD_STAT_TO_DICT (res , hit );
68+ ADD_STAT_TO_DICT (res , deferred );
69+ ADD_STAT_TO_DICT (res , miss );
70+ ADD_STAT_TO_DICT (res , deopt );
71+ ADD_STAT_TO_DICT (res , unquickened );
72+ #if SPECIALIZATION_STATS_DETAILED
73+ if (stats -> miss_types != NULL ) {
74+ if (PyDict_SetItemString (res , "fails" , stats -> miss_types ) == -1 ) {
75+ Py_DECREF (res );
76+ return NULL ;
77+ }
78+ }
79+ #endif
80+ return res ;
81+ }
82+ #undef ADD_STAT_TO_DICT
83+
84+ static int
85+ add_stat_dict (
86+ PyObject * res ,
87+ int opcode ,
88+ const char * name ) {
89+
90+ SpecializationStats * stats = & _specialization_stats [opcode ];
91+ PyObject * d = stats_to_dict (stats );
92+ if (d == NULL ) {
93+ return -1 ;
94+ }
95+ int err = PyDict_SetItemString (res , name , d );
96+ Py_DECREF (d );
97+ return err ;
98+ }
99+
100+ #if SPECIALIZATION_STATS
101+ PyObject *
102+ _Py_GetSpecializationStats (void ) {
103+ PyObject * stats = PyDict_New ();
104+ if (stats == NULL ) {
105+ return NULL ;
106+ }
107+ int err = 0 ;
108+ err += add_stat_dict (stats , LOAD_ATTR , "load_attr" );
109+ err += add_stat_dict (stats , LOAD_GLOBAL , "load_global" );
110+ err += add_stat_dict (stats , BINARY_SUBSCR , "binary_subscr" );
111+ if (err < 0 ) {
112+ Py_DECREF (stats );
113+ return NULL ;
114+ }
115+ return stats ;
116+ }
117+ #endif
118+
119+
43120#define PRINT_STAT (name , field ) fprintf(stderr, " %s." #field " : %" PRIu64 "\n", name, stats->field);
44121
45122static void
@@ -71,6 +148,7 @@ print_stats(SpecializationStats *stats, const char *name)
71148 }
72149#endif
73150}
151+ #undef PRINT_STAT
74152
75153void
76154_Py_PrintSpecializationStats (void )
0 commit comments