-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathatom.c
435 lines (336 loc) · 8.96 KB
/
atom.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_atom.h"
#include "spinlock.h"
#include "shm.h"
#ifdef __x86_64__
typedef unsigned long u64_t;
#else
typedef unsigned long long u64_t;
#endif
struct context {
atomic_t lock; /* Lock context */
long sequence;
u64_t last_ts;
/* Various once initialized variables */
u64_t datacenter_id;
u64_t worker_id;
u64_t twepoch;
unsigned char worker_id_bits;
unsigned char datacenter_id_bits;
unsigned char sequence_bits;
int worker_id_shift;
int datacenter_id_shift;
int timestamp_left_shift;
int sequence_mask;
};
/* If you declare any globals in php_atom.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(atom)
*/
/* True global resources - no need for thread safety here */
static int le_atom;
static struct shm shmem;
static struct context *context;
static pid_t current_pid = 0;
static u64_t datacenter_id;
static u64_t worker_id;
static u64_t twepoch;
ZEND_INI_MH(atom_ini_set_datacenter_id)
{
#if ZEND_MODULE_API_NO >= 20151012 /* PHP7 */
if (ZSTR_LEN(new_value) == 0) {
return FAILURE;
}
datacenter_id = (u64_t)atoi(ZSTR_VAL(new_value));
if (datacenter_id < 0 || datacenter_id > 31) {
return FAILURE;
}
return SUCCESS;
#else
if (new_value_length == 0) {
return FAILURE;
}
datacenter_id = (u64_t)atoi(new_value);
if (datacenter_id < 0 || datacenter_id > 31) {
return FAILURE;
}
return SUCCESS;
#endif
}
ZEND_INI_MH(atom_ini_set_worker)
{
#if ZEND_MODULE_API_NO >= 20151012
if (ZSTR_LEN(new_value) == 0) {
return FAILURE;
}
worker_id = (u64_t)atoi(ZSTR_VAL(new_value));
if (worker_id < 0 || worker_id > 31) {
return FAILURE;
}
return SUCCESS;
#else
if (new_value_length == 0) {
return FAILURE;
}
worker_id = (u64_t)atoi(new_value);
if (worker_id < 0 || worker_id > 31) {
return FAILURE;
}
return SUCCESS;
#endif
}
ZEND_INI_MH(atom_ini_set_twepoch)
{
#if ZEND_MODULE_API_NO >= 20151012
if (ZSTR_LEN(new_value) == 0) {
return FAILURE;
}
sscanf(ZSTR_VAL(new_value), "%llu", &twepoch);
if (twepoch <= 0ULL) {
return FAILURE;
}
return SUCCESS;
#else
if (new_value_length == 0) {
return FAILURE;
}
sscanf(new_value, "%llu", &twepoch);
if (twepoch <= 0ULL) {
return FAILURE;
}
return SUCCESS;
#endif
}
PHP_INI_BEGIN()
PHP_INI_ENTRY("atom.datacenter", "0", PHP_INI_ALL,
atom_ini_set_datacenter_id)
PHP_INI_ENTRY("atom.worker", "0", PHP_INI_ALL,
atom_ini_set_worker)
PHP_INI_ENTRY("atom.twepoch", "1451606400000", PHP_INI_ALL, /* 2016-01-01 */
atom_ini_set_twepoch)
PHP_INI_END()
static u64_t realtime()
{
struct timeval tv;
u64_t retval;
if (gettimeofday(&tv, NULL) == -1) {
return 0ULL;
}
retval = (u64_t)tv.tv_sec * 1000ULL +
(u64_t)tv.tv_usec / 1000ULL;
return retval;
}
static u64_t
skip_next_millis(u64_t last_ts)
{
u64_t current;
for (;;) {
current = realtime();
if (current > last_ts) {
break;
}
}
return current;
}
PHP_FUNCTION(atom_next_id)
{
u64_t current = realtime();
u64_t retval;
int len;
char buf[128];
if (current == 0ULL) {
RETURN_FALSE;
}
/* Make sure one process get the lock at the same time */
spin_lock(&context->lock, (int)current_pid);
if (context->last_ts == current) {
context->sequence = (context->sequence + 1) & context->sequence_mask;
if (context->sequence == 0) {
current = skip_next_millis(context->last_ts);
}
} else {
context->sequence = 0;
}
context->last_ts = current;
retval = ((current - context->twepoch) << context->timestamp_left_shift)
| (context->datacenter_id << context->datacenter_id_shift)
| (context->worker_id << context->worker_id_shift)
| context->sequence;
spin_unlock(&context->lock, (int)current_pid);
len = sprintf(buf, "%llu", retval);
#if ZEND_MODULE_API_NO >= 20151012
RETURN_STRINGL(buf, len);
#else
RETURN_STRINGL(buf, len, 1);
#endif
}
PHP_FUNCTION(atom_explain)
{
u64_t id;
char *key;
int len;
int ts, wk, dc;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key,
&len TSRMLS_CC) == FAILURE)
{
RETURN_FALSE;
}
if (sscanf(key, "%llu", &id) == 0) {
RETURN_FALSE;
}
/**
* Don't need locked here,
* because the fields are unchanging
*/
ts = ((id >> context->timestamp_left_shift) + context->twepoch) / 1000ULL;
dc = (id >> context->datacenter_id_shift) & 0x1FULL;
wk = (id >> context->worker_id_shift) & 0x1FULL;
array_init(return_value);
add_assoc_long(return_value, "timestamp", ts);
add_assoc_long(return_value, "datacenter", dc);
add_assoc_long(return_value, "worker", wk);
}
static int module_inited = 0;
int startup_atom_module()
{
shmem.size = sizeof(struct context);
if (shm_alloc(&shmem) == -1) {
return -1;
}
context = (struct context *)shmem.addr;
context->lock = 0;
/* would changing */
context->sequence = 0;
context->last_ts = 0ULL;
/* would not changing */
context->datacenter_id = datacenter_id;
context->worker_id = worker_id;
context->twepoch = twepoch;
context->worker_id_bits = 5;
context->datacenter_id_bits = 5;
context->sequence_bits = 12;
context->worker_id_shift = context->sequence_bits;
context->datacenter_id_shift = context->sequence_bits
+ context->worker_id_bits;
context->timestamp_left_shift = context->sequence_bits
+ context->worker_id_bits
+ context->datacenter_id_bits;
context->sequence_mask = -1 ^ (-1 << context->sequence_bits);
module_inited = 1;
spin_init();
return 0;
}
void shutdown_atom_module()
{
if (module_inited) {
shm_free(&shmem);
module_inited = 0;
}
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(atom)
{
REGISTER_INI_ENTRIES();
if (startup_atom_module() == -1) {
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(atom)
{
UNREGISTER_INI_ENTRIES();
shutdown_atom_module();
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(atom)
{
if (current_pid == 0) {
current_pid = getpid();
}
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(atom)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(atom)
{
php_info_print_table_start();
php_info_print_table_header(2, "atom support", "enabled");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ atom_functions[]
*
* Every user visible function must have an entry in atom_functions[].
*/
const zend_function_entry atom_functions[] = {
PHP_FE(atom_next_id, NULL)
PHP_FE(atom_explain, NULL)
PHP_FE_END /* Must be the last line in atom_functions[] */
};
/* }}} */
/* {{{ atom_module_entry
*/
zend_module_entry atom_module_entry = {
STANDARD_MODULE_HEADER,
"atom",
atom_functions,
PHP_MINIT(atom),
PHP_MSHUTDOWN(atom),
PHP_RINIT(atom),
PHP_RSHUTDOWN(atom),
PHP_MINFO(atom),
PHP_ATOM_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_ATOM
ZEND_GET_MODULE(atom)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/