-
Notifications
You must be signed in to change notification settings - Fork 1
/
php_bprof.h
executable file
·201 lines (147 loc) · 5.86 KB
/
php_bprof.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef PHP_BPROF_H
#define PHP_BPROF_H
extern zend_module_entry bprof_module_entry;
#define phpext_bprof_ptr &bprof_module_entry
#ifdef PHP_WIN32
#define PHP_BPROF_API __declspec(dllexport)
#else
#define PHP_BPROF_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
#include "zend_observer.h"
#define BPROF_VERSION "1.4"
#define BPROF_FUNC_HASH_COUNTERS_SIZE 1024
#define ROOT_SYMBOL "main()"
#define SCRATCH_BUF_LEN 512
#define BPROF_FLAGS_NO_BUILTINS 0x0001
#define BPROF_FLAGS_MEMORY 0x0004
#if !defined(uint32)
typedef unsigned int uint32;
#endif
#if !defined(uint8)
typedef unsigned char uint8;
#endif
#define register_trace_callback(function_name, cb) zend_hash_str_update_mem(BPROF_G(trace_callbacks), function_name, sizeof(function_name) - 1, &cb, sizeof(bp_trace_callback));
/* bprof maintains a stack of entries being profiled. The memory for the entry
* is passed by the layer that invokes BEGIN_PROFILING(), e.g. the bp_execute()
* function. Often, this is just C-stack memory.
*
* This structure is a convenient place to track start time of a particular
* profile operation, recursion depth, and the name of the function being
* profiled. */
typedef struct bp_entry_t {
struct bp_entry_t *prev_bprof; // ptr to prev entry being profiled
zend_string *name_bprof; // function name
int rlvl_bprof; // recursion level for function
long int mu_start_bprof; // memory usage
long int pmu_start_bprof; // peak memory usage
zend_ulong tsc_start; // start value for TSC counter
zend_ulong hash_code; // hash_code for the function name
int is_trace;
} bp_entry_t;
typedef zend_string *(*bp_trace_callback)(zend_string *symbol, zend_execute_data *data);
/* Various types for BPROF callbacks */
typedef void (*bp_init_cb)();
typedef void (*bp_exit_cb)();
typedef void (*bp_begin_function_cb)(bp_entry_t **entries, bp_entry_t *current);
typedef void (*bp_end_function_cb)(bp_entry_t **entries);
/**
* ***********************
* GLOBAL STATIC VARIABLES
* ***********************
*/
/* Pointer to the origianl execute_internal function */
static void (*_zend_execute_internal)(zend_execute_data *data, zval *return_value);
ZEND_DLEXPORT void bp_execute_internal(zend_execute_data *execute_data, zval *return_value);
static zend_op_array * (*_zend_compile_file) (zend_file_handle *file_handle, int type);
ZEND_DLEXPORT zend_op_array* bp_compile_file(zend_file_handle *file_handle, int type);
#if PHP_VERSION_ID >= 80200
/* Pointer to the original compile string function (used by eval) */
static zend_op_array * (*_zend_compile_string) (zend_string *source_string, const char *filename, zend_compile_position position);
ZEND_DLEXPORT zend_op_array* bp_compile_string(zend_string *source_string, const char *filename, zend_compile_position position);
static zend_observer_fcall_handlers tracer_observer(zend_execute_data *execute_data);
static void tracer_observer_begin(zend_execute_data *ex);
static void tracer_observer_end(zend_execute_data *ex, zval *return_value);
#else
/* Pointer to the original compile string function (used by eval) */
static zend_op_array * (*_zend_compile_string) (zend_string *source_string, const char *filename);
ZEND_DLEXPORT zend_op_array* bp_compile_string(zend_string *source_string, const char *filename);
static zend_observer_fcall_handlers tracer_observer(zend_execute_data *execute_data);
static void tracer_observer_begin(zend_execute_data *ex);
static void tracer_observer_end(zend_execute_data *ex, zval *return_value);
#endif
/**
* ****************************
* STATIC FUNCTION DECLARATIONS
* ****************************
*/
static void bp_register_constants(INIT_FUNC_ARGS);
static void bp_begin(zend_long bprof_flags);
static void bp_stop();
static void bp_end();
static inline zend_ulong cycle_timer();
static void bp_free_the_free_list();
static void bp_fast_free_bprof_entry(bp_entry_t *p);
void bp_init_trace_callbacks();
double get_timebase_conversion();
/* Struct to hold the various callbacks for a single bprof mode */
typedef struct bp_mode_cb {
bp_init_cb init_cb;
bp_exit_cb exit_cb;
bp_begin_function_cb begin_fn_cb;
bp_end_function_cb end_fn_cb;
} bp_mode_cb;
/* Bprof's global state.
*
* This structure is instantiated once. Initialize defaults for attributes in
* bp_init_profiler_state() Cleanup/free attributes in
* bp_clean_profiler_state() */
ZEND_BEGIN_MODULE_GLOBALS(bprof)
/* ---------- Global attributes: ----------- */
/* Indicates if bprof is currently enabled */
int enabled;
/* Indicates if bprof was ever enabled during this request */
int ever_enabled;
/* Holds all the bprof statistics */
zval stats_count;
/* Indicates the current bprof mode or level */
int profiler_level;
/* Top of the profile stack */
bp_entry_t *entries;
/* freelist of bp_entry_t chunks for reuse... */
bp_entry_t *entry_free_list;
/* Callbacks for various bprof modes */
bp_mode_cb mode_cb;
/* ---------- Mode specific attributes: ----------- */
/* Global to track the time of the last sample in time and ticks */
struct timeval last_sample_time;
zend_ulong last_sample_tsc;
/* BPROF_SAMPLING_INTERVAL in ticks */
zend_long sampling_interval;
zend_ulong sampling_interval_tsc;
zend_long sampling_depth;
/* bprof flags */
uint32 bprof_flags;
zend_string *root;
/* counter table indexed by hash value of function names. */
zend_ulong func_hash_counters[BPROF_FUNC_HASH_COUNTERS_SIZE];
HashTable *trace_callbacks;
double timebase_conversion;
zend_bool collect_additional_info;
ZEND_END_MODULE_GLOBALS(bprof)
PHP_MINIT_FUNCTION(bprof);
PHP_MSHUTDOWN_FUNCTION(bprof);
PHP_RINIT_FUNCTION(bprof);
PHP_RSHUTDOWN_FUNCTION(bprof);
PHP_MINFO_FUNCTION(bprof);
PHP_FUNCTION(bprof_enable);
PHP_FUNCTION(bprof_disable);
#ifdef ZTS
#define BPROF_G(v) TSRMG(bprof_globals_id, zend_bprof_globals *, v)
#else
#define BPROF_G(v) (bprof_globals.v)
#endif
extern ZEND_DECLARE_MODULE_GLOBALS(bprof);
#endif /* PHP_BPROF_H */