-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsnowflake.c
310 lines (274 loc) · 7.69 KB
/
snowflake.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* snowflake extension for PHP */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_snowflake.h"
#include "snowflake_arginfo.h"
#include <sys/time.h>
#include <sched.h>
#include <sys/ipc.h>
#include <sys/shm.h>
static uint8_t ncpu = 1U;
static uint32_t pid = 0U;
static uint32_t lock = 0U;
static int le_snowflake;
static snowflake snowf;
static sf_shmtx_t *mtx;
static int shm_id;
zend_class_entry snowflake_ce;
ZEND_DECLARE_MODULE_GLOBALS(snowflake)
static int le_snowflake;
static void php_snowflake_init_globals(zend_snowflake_globals *snowflake_globals)
{
snowflake_globals->region_id = 2;
}
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
static int trylock(sf_shmtx_t *mtx)
{
return (mtx->lock == 0 && __sync_bool_compare_and_swap(&(mtx->lock), 0, pid));
}
static void spinlock(sf_shmtx_t *mtx)
{
uint32_t i, n;
for (;;)
{
if (trylock(mtx))
{
return;
}
if (ncpu > 1)
{ // ncpu CPU个数
for (n = 1; n < 2048; n <<= 1)
{
for (i = 0; i < n; i++)
{
__asm__ __volatile__("pause");
}
if (trylock(mtx))
{
return;
}
}
}
sched_yield(); // 只旋一个周期,没有获取到锁,退出CPU控制权
}
}
static void spinlock_unlock(sf_shmtx_t *mtx)
{
__sync_bool_compare_and_swap(&(mtx->lock), pid, 0);
}
static int sf_shmtx_shutdown()
{
if (mtx->lock != 0)
{
spinlock_unlock(mtx);
}
shmdt(mtx);
shmctl(shm_id, IPC_RMID, 0);
return SUCCESS;
}
static int sf_shmtx_create()
{
shm_id = shmget(IPCKEY, sizeof(sf_shmtx_t), 0600);
if (shm_id != -1)
{
mtx = (sf_shmtx_t *)shmat(shm_id, NULL, 0);
sf_shmtx_shutdown();
return SUCCESS;
}
shm_id = shmget(IPCKEY, sizeof(sf_shmtx_t), IPC_CREAT | IPC_EXCL | 0600);
if (shm_id == -1)
{
php_error_docref(NULL, E_WARNING, "Init the shared memory[%lu] failed [%d:%s]!", sizeof(sf_shmtx_t), errno, strerror(errno));
return FAILURE;
}
mtx = (sf_shmtx_t *)shmat(shm_id, NULL, 0);
mtx->lock = 0U;
mtx->last_time = 0ULL;
mtx->seq = 0U;
return SUCCESS;
}
static zend_ulong snowflake_init(snowflake *sf)
{
zend_ulong region_id = SF_G(region_id);
zend_ulong max_region_id = (-1L << SF_G(region_bits)) ^ -1L;
if (region_id < 0 || region_id > max_region_id)
{
php_error_docref(NULL, E_WARNING, "Region ID must be in the range : 0-%llu", max_region_id);
return FAILURE;
}
zend_ulong worker_id = SF_G(worker_id);
zend_ulong max_worker_id = (-1L << SF_G(worker_bits)) ^ -1L;
if (worker_id < 0 || worker_id > max_worker_id)
{
php_error_docref(NULL, E_WARNING, "Worker ID must be in the range: 0-%llu", max_worker_id);
return FAILURE;
}
sf->time_shift = SF_G(region_bits) + SF_G(worker_bits) + SF_G(sequence_bits);
sf->region_shift = SF_G(worker_bits) + SF_G(sequence_bits);
sf->worker_shift = SF_G(sequence_bits);
sf->seq_mask = (-1L << SF_G(sequence_bits)) ^ -1L;
return region_id;
}
static inline uint64_t timestamp_gen()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return ((uint64_t)tv.tv_sec) * (uint64_t)1e3 + ((uint64_t)tv.tv_usec) / (uint64_t)1e3;
}
static uint64_t wait_next_ms(uint64_t lastStamp)
{
uint64_t cur = 0;
do {
cur = timestamp_gen();
} while (cur <= lastStamp);
return cur;
}
static uint64_t snowflake_id(snowflake *sf)
{
uint64_t now = timestamp_gen();
if (now > mtx->last_time)
{
mtx->seq = 0;
}
else if (now == mtx->last_time)
{
mtx->seq = (mtx->seq + 1) & sf->seq_mask;
if (mtx->seq == 0)
{
now = wait_next_ms(now);
}
} else if (mtx->last_time - now <= 5) { //5ms内的时差
mtx->seq = 0;
now = wait_next_ms(now);
}
else
{
php_error_docref(NULL, E_WARNING, "last_time in the range of 0, %llu, last_time:%llu", now, mtx->last_time);
}
mtx->last_time = now;
return ((now - SF_G(epoch)) << sf->time_shift) | (SF_G(region_id) << sf->region_shift) | (SF_G(worker_id) << sf->worker_shift) | (mtx->seq);
return now;
}
PHP_METHOD(snowflake, getId)
{
if (zend_parse_parameters_none() == FAILURE)
{
return;
}
spinlock(mtx);
uint64_t id = snowflake_id(&snowf);
spinlock_unlock(mtx);
if (id)
{
RETURN_LONG(id);
}
else
{
RETURN_BOOL(0);
}
}
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("snowflake.worker_id", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, worker_id, zend_snowflake_globals, snowflake_globals)
STD_PHP_INI_ENTRY("snowflake.region_id", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, region_id, zend_snowflake_globals, snowflake_globals)
//1576080000000 2019-12-12
STD_PHP_INI_ENTRY("snowflake.epoch", "1576080000000", PHP_INI_SYSTEM, OnUpdateLongGEZero, epoch, zend_snowflake_globals, snowflake_globals)
STD_PHP_INI_ENTRY("snowflake.time_bits", "41", PHP_INI_SYSTEM, OnUpdateLongGEZero, time_bits, zend_snowflake_globals, snowflake_globals)
STD_PHP_INI_ENTRY("snowflake.region_bits", "5", PHP_INI_SYSTEM, OnUpdateLongGEZero, region_bits, zend_snowflake_globals, snowflake_globals)
STD_PHP_INI_ENTRY("snowflake.worker_bits", "5", PHP_INI_SYSTEM, OnUpdateLongGEZero, worker_bits, zend_snowflake_globals, snowflake_globals)
STD_PHP_INI_ENTRY("snowflake.sequence_bits", "12", PHP_INI_SYSTEM, OnUpdateLongGEZero, sequence_bits, zend_snowflake_globals, snowflake_globals)
PHP_INI_END()
/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(snowflake)
{
#if defined(ZTS) && defined(COMPILE_DL_TEST)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
if (pid == 0)
{
pid = (uint32_t)getpid();
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(snowflake)
{
#if defined(ZTS) && defined(COMPILE_DL_TEST)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(snowflake)
{
php_info_print_table_start();
php_info_print_table_header(2, "Snowflake support", "enabled");
php_info_print_table_row(2, "Snowflake Version", PHP_SNOWFLAKE_VERSION);
php_info_print_table_row(2, "Supports", SF_SUPPORT_URL);
php_info_print_table_row(2, "worker_id", "1");
php_info_print_table_row(2, "region_id", "1");
php_info_print_table_row(2, "epoch", "1576080000000");
php_info_print_table_row(2, "time_bits", "41");
php_info_print_table_row(2, "region_bits", "5");
php_info_print_table_row(2, "worker_bits", "5");
php_info_print_table_row(2, "sequence_bits", "12");
php_info_print_table_end();
}
/* }}} */
PHP_MINIT_FUNCTION(snowflake)
{
ZEND_INIT_MODULE_GLOBALS(snowflake, php_snowflake_init_globals, NULL);
REGISTER_INI_ENTRIES();
INIT_CLASS_ENTRY(snowflake_ce, "snowflake", snowflake_methods); //注册类及类方法
zend_register_internal_class(&snowflake_ce);
if (sf_shmtx_create() == FAILURE)
{
return FAILURE;
}
if (snowflake_init(&snowf) == FAILURE)
{
return FAILURE;
}
ncpu = sysconf(_SC_NPROCESSORS_ONLN);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(snowflake)
{
ZEND_INIT_MODULE_GLOBALS(snowflake, php_snowflake_init_globals, NULL);
UNREGISTER_INI_ENTRIES();
sf_shmtx_shutdown();
return SUCCESS;
}
/* {{{ snowflake_method[]
*
* Every user visible function must have an entry in snowflake_functions[].
*/
/* }}} */
/* {{{ snowflake_module_entry */
zend_module_entry snowflake_module_entry = {
STANDARD_MODULE_HEADER,
"snowflake", /* Extension name */
ext_functions, /* zend_function_entry */
PHP_MINIT(snowflake), /* PHP_MINIT - Module initialization */
PHP_MSHUTDOWN(snowflake), /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(snowflake), /* PHP_RINIT - Request initialization */
PHP_RSHUTDOWN(snowflake), /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(snowflake), /* PHP_MINFO - Module info */
PHP_SNOWFLAKE_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES};
/* }}} */
#ifdef COMPILE_DL_SNOWFLAKE
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(snowflake)
#endif