Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56820c2

Browse files
committed
Add some stats collection in debugging mode.
No good way to extract output yet.
1 parent dfc2404 commit 56820c2

1 file changed

Lines changed: 41 additions & 3 deletions

File tree

Python/pyarena.c

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#include "Python.h"
22
#include "pyarena.h"
33

4-
/* A simple arena block structure */
5-
/* TODO(jhylton): Measurement to justify block size. */
4+
/* A simple arena block structure
5+
6+
Measurements with standard library modules suggest the average
7+
allocation is about 20 bytes and that most compiles use a single
8+
block.
9+
*/
610

711
#define DEFAULT_BLOCK_SIZE 8192
812
typedef struct _block {
@@ -21,6 +25,14 @@ struct _arena {
2125
block *a_head;
2226
block *a_cur;
2327
PyObject *a_objects;
28+
#if defined(Py_DEBUG)
29+
/* Debug output */
30+
size_t total_allocs;
31+
size_t total_size;
32+
size_t total_blocks;
33+
size_t total_block_size;
34+
size_t total_big_blocks;
35+
#endif
2436
};
2537

2638
static block *
@@ -86,19 +98,35 @@ PyArena_New()
8698
free((void *)arena);
8799
return NULL;
88100
}
89-
arena->a_objects = PyList_New(16);
101+
arena->a_objects = PyList_New(0);
90102
if (!arena->a_objects) {
91103
block_free(arena->a_head);
92104
free((void *)arena);
93105
return NULL;
94106
}
107+
#if defined(Py_DEBUG)
108+
arena->total_allocs = 0;
109+
arena->total_size = 0;
110+
arena->total_blocks = 1;
111+
arena->total_block_size = DEFAULT_BLOCK_SIZE;
112+
arena->total_big_blocks = 0;
113+
#endif
95114
return arena;
96115
}
97116

98117
void
99118
PyArena_Free(PyArena *arena)
100119
{
101120
assert(arena);
121+
#if defined(Py_DEBUG)
122+
/*
123+
fprintf(stderr,
124+
"alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n",
125+
arena->total_allocs, arena->total_size, arena->total_blocks,
126+
arena->total_block_size, arena->total_big_blocks,
127+
PyList_Size(arena->a_objects));
128+
*/
129+
#endif
102130
block_free(arena->a_head);
103131
assert(arena->a_objects->ob_refcnt == 1);
104132
Py_DECREF(arena->a_objects);
@@ -111,9 +139,19 @@ PyArena_Malloc(PyArena *arena, size_t size)
111139
void *p = block_alloc(arena->a_cur, size);
112140
if (!p)
113141
return NULL;
142+
#if defined(Py_DEBUG)
143+
arena->total_allocs++;
144+
arena->total_size += size;
145+
#endif
114146
/* Reset cur if we allocated a new block. */
115147
if (arena->a_cur->ab_next) {
116148
arena->a_cur = arena->a_cur->ab_next;
149+
#if defined(Py_DEBUG)
150+
arena->total_blocks++;
151+
arena->total_block_size += arena->a_cur->ab_size;
152+
if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE)
153+
arena->total_big_blocks++;
154+
#endif
117155
}
118156
return p;
119157
}

0 commit comments

Comments
 (0)