Skip to content

Commit cde1b58

Browse files
committed
cleanup shutdown, debug arena stuff
1 parent c38a854 commit cde1b58

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed

config.m4

+12
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,23 @@ PHP_ARG_ENABLE([stat-coverage],
1212
[Enable stat coverage support])],
1313
[no], [no])
1414

15+
PHP_ARG_ENABLE([stat-arena-debug],
16+
[whether to enable stat arena debug support],
17+
[AS_HELP_STRING([--enable-stat-arena-debug],
18+
[Enable stat arena debug support])],
19+
[no], [no])
20+
1521
if test "$PHP_STAT" != "no"; then
1622
PHP_ADD_LIBRARY(pthread,, STAT_SHARED_LIBADD)
1723

1824
AC_DEFINE(HAVE_ZEND_STAT, 1, [ Have stat support ])
1925

26+
if test "$PHP_STAT_ARENA_DEBUG" != "no"; then
27+
AC_DEFINE(ZEND_STAT_ARENA_DEBUG, 1, [ Have stat arena debug support ])
28+
else
29+
AC_DEFINE(ZEND_STAT_ARENA_DEBUG, 0, [ Do not have stat arena debug support ])
30+
fi
31+
2032
PHP_NEW_EXTENSION(stat,
2133
zend_stat.c \
2234
src/zend_stat_arena.c \

src/zend_stat_arena.c

+23-14
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@
2222
#include "zend_stat.h"
2323
#include "zend_stat_arena.h"
2424

25+
#ifndef ZEND_STAT_ARENA_DEBUG
26+
# define ZEND_STAT_ARENA_DEBUG 0
27+
#endif
28+
2529
typedef struct _zend_stat_arena_block_t zend_stat_arena_block_t;
2630

2731
typedef intptr_t zend_stat_arena_ptr_t;
2832

2933
struct _zend_stat_arena_block_t {
3034
zend_long size;
31-
zend_bool used;
35+
zend_long used;
3236
zend_stat_arena_block_t *next;
3337
zend_stat_arena_ptr_t mem[1];
3438
};
@@ -70,15 +74,13 @@ static zend_always_inline zend_stat_arena_block_t* zend_stat_arena_find(zend_sta
7074
while (NULL != block) {
7175
if ((0 == block->used)) {
7276
if ((block->size >= size)) {
73-
block->used = 1;
7477
goto _zend_stat_arena_found;
7578
} else {
7679
if (NULL != block->next) {
7780
if ((0 == block->next->used) &&
7881
((block->size + block->next->size) >= size)) {
7982
block->size += block->next->size;
8083
block->next = block->next->next;
81-
block->used = 1;
8284
goto _zend_stat_arena_found;
8385
}
8486
}
@@ -91,8 +93,7 @@ static zend_always_inline zend_stat_arena_block_t* zend_stat_arena_find(zend_sta
9193
return NULL;
9294

9395
_zend_stat_arena_found:
94-
if ((NULL != block) &&
95-
((wasted = (block->size - size)) > 0)) {
96+
if (((wasted = (block->size - size)) > 0)) {
9697
if ((wasted > ZEND_STAT_ARENA_BLOCK_MIN)) {
9798
zend_stat_arena_block_t *reclaim =
9899
(zend_stat_arena_block_t*)
@@ -108,6 +109,7 @@ static zend_always_inline zend_stat_arena_block_t* zend_stat_arena_find(zend_sta
108109

109110
block->size = size;
110111
}
112+
block->used = size;
111113

112114
return block;
113115
}
@@ -175,8 +177,8 @@ void* zend_stat_arena_alloc(zend_stat_arena_t *arena, zend_long size) {
175177
goto _zend_stat_arena_alloc_oom;
176178
}
177179

178-
block->used = 1;
179-
block->size = aligned;
180+
block->used =
181+
block->size = aligned;
180182

181183
if (UNEXPECTED(NULL == arena->list.start)) {
182184
arena->list.start = block;
@@ -199,7 +201,7 @@ void* zend_stat_arena_alloc(zend_stat_arena_t *arena, zend_long size) {
199201
return NULL;
200202
}
201203

202-
#ifdef ZEND_DEBUG
204+
#if ZEND_STAT_ARENA_DEBUG
203205
static zend_always_inline zend_bool zend_stat_arena_owns(zend_stat_arena_t *arena, void *mem) {
204206
if (UNEXPECTED((((char*) mem) < ((char*) arena)) || (((char*) mem) > arena->end))) {
205207
return 0;
@@ -212,7 +214,7 @@ static zend_always_inline zend_bool zend_stat_arena_owns(zend_stat_arena_t *aren
212214
void zend_stat_arena_free(zend_stat_arena_t *arena, void *mem) {
213215
zend_stat_arena_block_t *block = zend_stat_arena_block(mem);
214216

215-
#ifdef ZEND_DEBUG
217+
#if ZEND_STAT_ARENA_DEBUG
216218
ZEND_ASSERT(zend_stat_arena_owns(arena, mem));
217219
#endif
218220

@@ -239,18 +241,25 @@ void zend_stat_arena_free(zend_stat_arena_t *arena, void *mem) {
239241
pthread_mutex_unlock(&arena->mutex);
240242
}
241243

242-
#ifdef ZEND_DEBUG
244+
#if ZEND_STAT_ARENA_DEBUG
243245
static zend_always_inline void zend_stat_arena_debug(zend_stat_arena_t *arena) {
244246
zend_stat_arena_block_t *block = arena->list.start;
247+
zend_long wasted = 0;
245248

246249
while (NULL != block) {
247-
if (block->used) {
250+
if (block->used > 0) {
251+
wasted += block->size - block->used;
252+
}
253+
254+
if (block->used > 0) {
248255
fprintf(stderr,
249-
"[STAT] %p leaked "ZEND_LONG_FMT" bytes\n",
250-
block->mem, block->size);
256+
"[STAT] %p leaked %p "ZEND_LONG_FMT" bytes\n",
257+
arena, block->mem, block->size);
251258
}
252259
block = block->next;
253260
}
261+
262+
fprintf(stderr, "[STAT] %p wasted "ZEND_LONG_FMT" bytes\n", arena, wasted);
254263
}
255264
#endif
256265

@@ -259,7 +268,7 @@ void zend_stat_arena_destroy(zend_stat_arena_t *arena) {
259268
return;
260269
}
261270

262-
#ifdef ZEND_DEBUG
271+
#if ZEND_STAT_ARENA_DEBUG
263272
zend_stat_arena_debug(arena);
264273
#endif
265274

src/zend_stat_request.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ zend_bool zend_stat_request_create(zend_stat_request_t *request) {
2929

3030
memset(request, 0, sizeof(zend_stat_request_t));
3131

32-
#ifdef ZTS
33-
request->pid = syscall(SYS_gettid);
34-
#else
35-
request->pid = getpid();
36-
#endif
37-
32+
request->pid = zend_stat_pid();
3833
request->elapsed = zend_stat_time();
3934

4035
if (EXPECTED(ri->path_translated)) {

zend_stat.c

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "zend_stat_stream.h"
4343
#include "zend_stat_strings.h"
4444

45+
static pid_t zend_stat_main = 0;
4546
static zend_stat_buffer_t* zend_stat_buffer = NULL;
4647
static zend_stat_io_t zend_stat_stream;
4748
static zend_stat_io_t zend_stat_control;
@@ -132,6 +133,7 @@ static int zend_stat_startup(zend_extension *ze) {
132133
}
133134

134135
zend_stat_started = zend_stat_time();
136+
zend_stat_main = zend_stat_pid();
135137

136138
ze->handle = 0;
137139

@@ -143,6 +145,10 @@ static void zend_stat_shutdown(zend_extension *ze) {
143145
return;
144146
}
145147

148+
if (zend_stat_pid() != zend_stat_main) {
149+
return;
150+
}
151+
146152
if (zend_stat_ini_dump > 0) {
147153
zend_stat_buffer_dump(
148154
zend_stat_buffer, zend_stat_ini_dump);

zend_stat.h

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838

3939
double zend_stat_time(void);
4040

41+
static zend_always_inline pid_t zend_stat_pid(void) {
42+
#ifdef ZTS
43+
return syscall(SYS_gettid);
44+
#else
45+
return getpid();
46+
#endif
47+
}
48+
4149
static zend_always_inline void* zend_stat_map(zend_long size) {
4250
void *mapped = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
4351

0 commit comments

Comments
 (0)