Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eendebakpt committed Dec 22, 2024
1 parent c463dce commit 8f79778
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
9 changes: 5 additions & 4 deletions Include/cpython/pystats.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions Include/ht.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand All @@ -263,4 +260,4 @@ inline void hash_table_inc(ht *table, const char *key)
}


#endif
#endif
2 changes: 1 addition & 1 deletion Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 12 additions & 1 deletion Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 8f79778

Please sign in to comment.