forked from krakjoe/stat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzend_stat_strings.c
234 lines (169 loc) · 6.5 KB
/
zend_stat_strings.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
+----------------------------------------------------------------------+
| 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_STRINGS
# define ZEND_STAT_STRINGS
#include "zend_stat.h"
#include "zend_stat_strings.h"
typedef struct {
zend_long size;
zend_long used;
zend_long slots;
struct {
void *memory;
zend_long size;
zend_long used;
} buffer;
zend_stat_string_t *strings;
} zend_stat_strings_t;
static zend_stat_strings_t* zend_stat_strings;
static zend_stat_string_t* zend_stat_strings_opcodes[256];
#define ZTSG(v) zend_stat_strings->v
#define ZTSB(v) ZTSG(buffer).v
static zend_always_inline zend_stat_string_t* zend_stat_string_init(const char *value, size_t length) {
zend_stat_string_t *string;
zend_ulong slot;
zend_ulong offset;
zend_ulong hash = zend_inline_hash_func(value, length);
if (UNEXPECTED(++ZTSG(used) >= ZTSG(slots))) {
--ZTSG(used);
return NULL;
}
slot = hash % ZTSG(slots);
_zend_stat_string_init_load:
string = &ZTSG(strings)[slot];
if (UNEXPECTED(string->hash == hash)) {
return string;
} else {
if (UNEXPECTED(string->length)) {
slot++;
slot %= ZTSG(slots);
goto _zend_stat_string_init_load;
}
}
offset = ZTSB(used);
if (UNEXPECTED((offset + length) >= ZTSB(size))) {
/* panic OOM */
return NULL;
}
string->value = (char*) (((char*) ZTSB(memory)) + offset);
memcpy(string->value, value, length);
string->value[length] = 0;
string->hash = hash;
string->length = length;
ZTSB(used) += length;
return string;
}
static zend_always_inline zend_stat_string_t* zend_stat_string_copy(zend_string *string) {
zend_stat_string_t *copy;
zend_ulong slot;
zend_ulong offset;
if (UNEXPECTED(__atomic_add_fetch(
&ZTSG(used), 1, __ATOMIC_ACQ_REL) >= ZTSG(slots))) {
__atomic_sub_fetch(&ZTSG(used), 1, __ATOMIC_ACQ_REL);
/* panic OOM */
return NULL;
}
slot = ZSTR_HASH(string) % ZTSG(slots);
_zend_stat_string_copy_load:
copy = &ZTSG(strings)[slot];
if (EXPECTED(__atomic_load_n(©->length, __ATOMIC_SEQ_CST))) {
_zend_stat_string_copy_check:
if (UNEXPECTED(copy->hash != ZSTR_HASH(string))) {
++slot;
slot %= ZTSG(slots);
goto _zend_stat_string_copy_load;
}
__atomic_sub_fetch(
&ZTSG(used), 1, __ATOMIC_ACQ_REL);
free(string);
return copy;
}
while (__atomic_exchange_n(©->locked, 1, __ATOMIC_RELAXED));
__atomic_thread_fence(__ATOMIC_ACQUIRE);
offset = __atomic_fetch_add(&ZTSB(used), ZSTR_LEN(string), __ATOMIC_ACQ_REL);
if (UNEXPECTED((offset + ZSTR_LEN(string)) >= ZTSB(size))) {
__atomic_sub_fetch(&ZTSB(used), ZSTR_LEN(string), __ATOMIC_ACQ_REL);
__atomic_thread_fence(__ATOMIC_RELEASE);
__atomic_exchange_n(©->locked, 0, __ATOMIC_RELAXED);
/* panic OOM */
return NULL;
}
if (UNEXPECTED(__atomic_load_n(©->length, __ATOMIC_SEQ_CST))) {
__atomic_thread_fence(__ATOMIC_RELEASE);
__atomic_exchange_n(©->locked, 0, __ATOMIC_RELAXED);
goto _zend_stat_string_copy_check;
}
copy->value = (char*) (((char*) ZTSB(memory)) + offset);
memcpy(copy->value,
ZSTR_VAL(string),
ZSTR_LEN(string));
copy->value[ZSTR_LEN(string)] = 0;
copy->hash = ZSTR_HASH(string);
__atomic_store_n(©->length, ZSTR_LEN(string), __ATOMIC_SEQ_CST);
__atomic_thread_fence(__ATOMIC_RELEASE);
__atomic_exchange_n(©->locked, 0, __ATOMIC_RELAXED);
free(string);
return copy;
}
zend_bool zend_stat_strings_startup(zend_long strings) {
size_t zend_stat_strings_size = floor((strings / 5) * 1),
zend_stat_strings_buffer_size = floor((strings / 5) * 4);
zend_stat_strings = zend_stat_map(strings + sizeof(zend_stat_strings_t));
if (!zend_stat_strings) {
zend_error(E_WARNING,
"[STAT] Failed to allocate shared memory for strings");
return 0;
}
memset(zend_stat_strings, 0, sizeof(zend_stat_strings_t));
ZTSG(strings) = (void*)
(((char*) zend_stat_strings) + sizeof(zend_stat_strings_t));
ZTSG(size) = zend_stat_strings_size;
ZTSG(slots) = ZTSG(size) / sizeof(zend_stat_string_t);
ZTSG(used) = 0;
memset(ZTSG(strings), 0, zend_stat_strings_size);
ZTSB(memory) = (void*)
(((char*) ZTSG(strings)) + zend_stat_strings_size);
ZTSB(size) = zend_stat_strings_buffer_size;
ZTSB(used) = 0;
memset(ZTSB(memory), 0, zend_stat_strings_buffer_size);
{
int it = 0, end = 256;
memset(zend_stat_strings_opcodes, 0, sizeof(zend_stat_strings_opcodes));
while (it < end) {
const char *name = zend_get_opcode_name(it);
if (name) {
zend_stat_strings_opcodes[it] =
zend_stat_string_init(
(char*) name + (sizeof("ZEND_")-1),
strlen(name) - (sizeof("ZEND_")-1));
}
it++;
}
}
return 1;
}
zend_stat_string_t *zend_stat_string_opcode(zend_uchar opcode) {
return zend_stat_strings_opcodes[opcode];
}
zend_stat_string_t *zend_stat_string(zend_string *string) {
return zend_stat_string_copy(string);
}
void zend_stat_strings_shutdown(void) {
zend_stat_unmap(zend_stat_strings, ZTSG(size));
}
#endif /* ZEND_STAT_STRINGS */