diff --git a/Include/cpython/pystats.h b/Include/cpython/pystats.h index 1fff02dea860c9..daa4face311726 100644 --- a/Include/cpython/pystats.h +++ b/Include/cpython/pystats.h @@ -176,10 +176,11 @@ typedef struct _stats { // Export for shared extensions like 'math' PyAPI_DATA(PyStats*) _Py_stats; -PyStats *get_pystats(); -void OBJECT_STAT_INCREMENT(const char *tag); -void OBJECT_STAT_FREELIST_INCREMENT(const char *tag); -void _guard_stats_table(); +PyAPI_FUNC(PyStats *) get_pystats(void); +PyAPI_FUNC(void) OBJECT_STAT_INCREMENT(const char *tag); +PyAPI_FUNC(void) OBJECT_STAT_FREELIST_INCREMENT(const char *tag); +PyAPI_FUNC(void) OBJECT_STAT_ALLOC_INCREMENT(const char *tag); +PyAPI_FUNC(void) _guard_stats_table(void); #ifdef _PY_INTERPRETER # define _Py_INCREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.interpreter_increfs++; } while (0) diff --git a/Include/ht.h b/Include/ht.h index 7c03fc3514e8c7..cbc4699d2644a9 100644 --- a/Include/ht.h +++ b/Include/ht.h @@ -61,7 +61,7 @@ inline void ht_destroy(ht* table) { // Return 64-bit FNV-1a hash for key (NUL-terminated). See description: // https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function -static uint64_t hash_key(const char* key) { +inline uint64_t hash_key(const char* key) { uint64_t hash = FNV_OFFSET; for (const char* p = key; *p; p++) { hash ^= (uint64_t)(unsigned char)(*p); @@ -115,7 +115,7 @@ inline const char* ht_set_entry(ht_entry* entries, size_t capacity, // Didn't find key, allocate+copy if needed, then insert it. if (plength != NULL) { - key = _strdup(key); + key = strdup(key); if (key == NULL) { return NULL; } @@ -211,16 +211,15 @@ inline int ht_next(hti* it) { return 0; } -inline void ht_exit_nomem(void) { +static inline void ht_exit_nomem(void) { fprintf(stderr, "hash_table: out of memory or other failure\n"); exit(1); } -inline void show_hash_table_int(ht *table, FILE *out) { +static inline void show_hash_table_int(ht *table, FILE *out) { assert (table); // Calculate average probe length. hti it = ht_iterator(table); - size_t total_probes = 0; while (ht_next(&it)) { if (out==0) { printf("%s: %d\n", it.key, *(int*) it.value); @@ -231,18 +230,16 @@ inline void show_hash_table_int(ht *table, FILE *out) { } } -inline void show_hash_table_float(ht *table) { +static inline void show_hash_table_float(ht *table) { assert (table); hti it = ht_iterator(table); - size_t total_probes = 0; while (ht_next(&it)) { printf("%s: %f\n", it.key, *(float *)(it.value)); } } -inline void hash_table_inc(ht *table, const char *key) +static inline void hash_table_inc(ht *table, const char *key) { - void *value = ht_get(table, key); if (value != NULL) { // Already exists, increment int that value points to. @@ -263,4 +260,4 @@ inline void hash_table_inc(ht *table, const char *key) } -#endif \ No newline at end of file +#endif diff --git a/Objects/longobject.c b/Objects/longobject.c index 675587950ae1a5..5e1f36384cc6f9 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -170,7 +170,7 @@ _PyLong_New(Py_ssize_t size) sizeof() instead of the offsetof, but this risks being incorrect in the presence of padding between the header and the digits. */ - OBJECT_STAT_INCREMENT("_PyLong_New"); + OBJECT_STAT_ALLOC_INCREMENT("_PyLong_New"); result = PyObject_Malloc(offsetof(PyLongObject, long_value.ob_digit) + ndigits*sizeof(digit)); if (!result) { diff --git a/Objects/object.c b/Objects/object.c index 0bbb0d3bc8a351..3bb542af71d5c9 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -549,11 +549,10 @@ void _guard_stats_table() } - void OBJECT_STAT_ALLOCATION_TYPE(PyTypeObject *tp) { #ifdef Py_STATS - OBJECT_STAT_INCREMENT(tp->tp_name); + OBJECT_STAT_ALLOC_INCREMENT(tp->tp_name); #endif } diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index a21984faf601e8..2d94dc388a9ad7 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -46,13 +46,24 @@ void OBJECT_STAT_FREELIST_INCREMENT(const char *tag) { #ifdef Py_STATS if (_Py_stats) { - char freelist_tag[200] = "freelist_"; + char freelist_tag[200] = "Freelist allocate type #"; strncat(freelist_tag, tag, 200-9-1); OBJECT_STAT_INCREMENT(freelist_tag); } #endif } +void OBJECT_STAT_ALLOC_INCREMENT(const char *tag) +{ +#ifdef Py_STATS + if (_Py_stats) { + char sub_tag[200] = "Allocate type #"; + strncat(sub_tag, tag, 200-6-1); + OBJECT_STAT_INCREMENT(sub_tag); + } +#endif +} + /* Defined in tracemalloc.c */ extern void _PyMem_DumpTraceback(int fd, const void *ptr); diff --git a/Python/specialize.c b/Python/specialize.c index 12739bc7bc59fa..7d9d79d9b675f5 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -346,10 +346,13 @@ print_stats(FILE *out, PyStats *stats) #endif print_rare_event_stats(out, &stats->rare_event_stats); + printf("print_stats!\n"); #ifdef Py_STATS PyStats *_Py_stats = get_pystats(); + printf("print_stats! 2\n"); if (_Py_stats) { + printf("print_stats! 4\n"); _guard_stats_table(); show_hash_table_int(_Py_stats->object_stats.allocation_table, out); }