forked from krakjoe/stat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzend_stat.c
190 lines (155 loc) · 5.39 KB
/
zend_stat.c
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
/*
+----------------------------------------------------------------------+
| stat |
+----------------------------------------------------------------------+
| Copyright (c) Joe Watkins 2019 |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: krakjoe |
+----------------------------------------------------------------------+
*/
#ifndef ZEND_STAT
# define ZEND_STAT
#define ZEND_STAT_EXTNAME "Stat"
#define ZEND_STAT_VERSION "0.0.1-dev"
#define ZEND_STAT_AUTHOR "krakjoe"
#define ZEND_STAT_URL "https://github.com/krakjoe/stat"
#define ZEND_STAT_COPYRIGHT "Copyright (c) 2019"
#if defined(__GNUC__) && __GNUC__ >= 4
# define ZEND_STAT_EXTENSION_API __attribute__ ((visibility("default")))
#else
# define ZEND_STAT_EXTENSION_API
#endif
#include "zend_stat.h"
#include "zend_stat_buffer.h"
#include "zend_stat_control.h"
#include "zend_stat_ini.h"
#include "zend_stat_io.h"
#include "zend_stat_sampler.h"
#include "zend_stat_stream.h"
#include "zend_stat_strings.h"
static zend_stat_buffer_t* zend_stat_buffer = NULL;
static zend_stat_io_t zend_stat_stream;
static zend_stat_io_t zend_stat_control;
static double zend_stat_started = 0;
ZEND_TLS zend_stat_sampler_t zend_stat_sampler;
static int zend_stat_startup(zend_extension*);
static void zend_stat_shutdown(zend_extension *);
static void zend_stat_activate(void);
static void zend_stat_deactivate(void);
ZEND_STAT_EXTENSION_API zend_extension_version_info extension_version_info = {
ZEND_EXTENSION_API_NO,
ZEND_EXTENSION_BUILD_ID
};
ZEND_STAT_EXTENSION_API zend_extension zend_extension_entry = {
ZEND_STAT_EXTNAME,
ZEND_STAT_VERSION,
ZEND_STAT_AUTHOR,
ZEND_STAT_URL,
ZEND_STAT_COPYRIGHT,
zend_stat_startup,
zend_stat_shutdown,
zend_stat_activate,
zend_stat_deactivate,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
STANDARD_ZEND_EXTENSION_PROPERTIES
};
static int zend_stat_startup(zend_extension *ze) {
zend_stat_ini_startup();
if (!zend_stat_ini_stream && !zend_stat_ini_dump) {
zend_error(E_WARNING,
"[STAT] stream and dump are both disabled by configuration, "
"may be misconfigured");
zend_stat_ini_shutdown();
return SUCCESS;
}
if (!zend_stat_strings_startup(zend_stat_ini_strings)) {
zend_stat_ini_shutdown();
return SUCCESS;
}
if (!(zend_stat_buffer = zend_stat_buffer_startup(
zend_stat_ini_samples,
zend_stat_ini_interval,
zend_stat_ini_arginfo))) {
zend_stat_strings_shutdown();
zend_stat_ini_shutdown();
return SUCCESS;
}
if (!zend_stat_control_startup(
&zend_stat_control,
zend_stat_buffer,
zend_stat_ini_control)) {
zend_stat_buffer_shutdown(zend_stat_buffer);
zend_stat_strings_shutdown();
zend_stat_ini_shutdown();
return SUCCESS;
}
if (!zend_stat_stream_startup(
&zend_stat_stream,
zend_stat_buffer,
zend_stat_ini_stream)) {
zend_stat_control_shutdown(&zend_stat_control);
zend_stat_buffer_shutdown(zend_stat_buffer);
zend_stat_strings_shutdown();
zend_stat_ini_shutdown();
return SUCCESS;
}
zend_stat_started = zend_stat_time();
ze->handle = 0;
return SUCCESS;
}
static void zend_stat_shutdown(zend_extension *ze) {
if (0 == zend_stat_started) {
return;
}
if (zend_stat_ini_dump > 0) {
zend_stat_buffer_dump(
zend_stat_buffer, zend_stat_ini_dump);
}
zend_stat_control_shutdown(&zend_stat_control);
zend_stat_stream_shutdown(&zend_stat_stream);
zend_stat_buffer_shutdown(zend_stat_buffer);
zend_stat_strings_shutdown();
zend_stat_ini_shutdown();
zend_stat_started = 0;
}
static void zend_stat_activate(void) {
#if defined(ZTS) && defined(COMPILE_DL_STAT)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
if (0 == zend_stat_started) {
return;
}
#if defined(ZTS)
zend_stat_sampler_activate(&zend_stat_sampler, syscall(SYS_gettid), zend_stat_buffer);
#else
zend_stat_sampler_activate(&zend_stat_sampler, getpid(), zend_stat_buffer);
#endif
}
static void zend_stat_deactivate(void) {
zend_stat_sampler_deactivate(&zend_stat_sampler);
}
double zend_stat_time(void) {
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != SUCCESS) {
return (double) -1;
}
return ((double) ts.tv_sec + ts.tv_nsec / 1000000000.00) - zend_stat_started;
}
#if defined(ZTS) && defined(COMPILE_DL_STAT)
ZEND_TSRMLS_CACHE_DEFINE();
#endif
#endif /* ZEND_STAT */