forked from krakjoe/stat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zend_stat_buffer.c
196 lines (155 loc) · 6.1 KB
/
zend_stat_buffer.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
/*
+----------------------------------------------------------------------+
| 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_BUFFER
# define ZEND_STAT_BUFFER
#include "zend_stat.h"
#include "zend_stat_buffer.h"
#include "zend_stat_io.h"
struct _zend_stat_buffer_t {
struct {
zend_ulong interval;
zend_bool arginfo;
} options;
zend_stat_sample_t *samples;
zend_stat_sample_t *position;
zend_stat_sample_t *it;
zend_stat_sample_t *end;
zend_ulong max;
zend_ulong used;
};
static size_t zend_always_inline zend_stat_buffer_size(zend_long samples) {
return sizeof(zend_stat_buffer_t) +
(samples * sizeof(zend_stat_sample_t));
}
zend_stat_buffer_t* zend_stat_buffer_startup(zend_long samples, zend_long interval, zend_bool arginfo) {
size_t size = zend_stat_buffer_size(samples);
zend_stat_buffer_t *buffer = zend_stat_map(size);
if (!buffer) {
zend_error(E_WARNING,
"[STAT] Failed to allocate shared memory for buffer");
return NULL;
}
memset(buffer, 0, size);
buffer->samples =
buffer->it =
buffer->position =
(zend_stat_sample_t*) (((char*) buffer) + sizeof(zend_stat_buffer_t));
buffer->max = samples;
buffer->used = 0;
buffer->end = buffer->position + buffer->max;
buffer->options.interval = interval * 1000;
buffer->options.arginfo = arginfo;
memset(buffer->samples, 0, sizeof(zend_stat_sample_t) * buffer->max);
return buffer;
}
zend_bool zend_stat_buffer_empty(zend_stat_buffer_t *buffer) {
return 0 == __atomic_load_n(&buffer->used, __ATOMIC_SEQ_CST);
}
void zend_stat_buffer_insert(zend_stat_buffer_t *buffer, zend_stat_sample_t *input) {
zend_stat_sample_t *sample;
zend_bool _unused = 0,
_used = 1;
do {
zend_bool _unbusy = 0,
_busy = 1;
sample = __atomic_fetch_add(
&buffer->position, sizeof(zend_stat_sample_t), __ATOMIC_SEQ_CST);
if (UNEXPECTED(sample >= buffer->end)) {
__atomic_store_n(
&buffer->position,
buffer->samples, __ATOMIC_SEQ_CST);
continue;
}
if (UNEXPECTED(!__atomic_compare_exchange(
&sample->state.busy,
&_unbusy, &_busy,
0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))) {
continue;
}
break;
} while(1);
memcpy(
ZEND_STAT_SAMPLE_DATA(sample),
ZEND_STAT_SAMPLE_DATA(input),
ZEND_STAT_SAMPLE_DATA_SIZE);
if (__atomic_compare_exchange(&sample->state.used,
&_unused, &_used, 0,
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
__atomic_fetch_add(&buffer->used, 1, __ATOMIC_SEQ_CST);
}
__atomic_store_n(&sample->state.busy, 0, __ATOMIC_SEQ_CST);
}
zend_bool zend_stat_buffer_dump(zend_stat_buffer_t *buffer, int fd) {
zend_stat_sample_t *sample;
zend_ulong tried = 0;
if (zend_stat_buffer_empty(buffer)) {
return 1;
}
while (tried++ < buffer->max) {
zend_stat_sample_t sampled = zend_stat_sample_empty;
zend_bool _unbusy = 0,
_busy = 1,
_unused = 0,
_used = 1;
sample = __atomic_fetch_add(
&buffer->it, sizeof(zend_stat_sample_t), __ATOMIC_SEQ_CST);
if (UNEXPECTED(sample >= buffer->end)) {
__atomic_store_n(
&buffer->it,
buffer->samples, __ATOMIC_SEQ_CST);
continue;
}
if (UNEXPECTED(!__atomic_compare_exchange(
&sample->state.busy,
&_unbusy, &_busy,
0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))) {
continue;
}
if (EXPECTED(__atomic_compare_exchange(
&sample->state.used,
&_used, &_unused,
0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))) {
__atomic_sub_fetch(&buffer->used, 1, __ATOMIC_SEQ_CST);
memcpy(&sampled, sample, sizeof(zend_stat_sample_t));
}
__atomic_store_n(&sample->state.busy, 0, __ATOMIC_SEQ_CST);
if (UNEXPECTED(ZEND_STAT_SAMPLE_UNUSED == sampled.type)) {
continue;
}
if (!zend_stat_sample_write(&sampled, fd)) {
return 0;
}
}
return 1;
}
void zend_stat_buffer_interval_set(zend_stat_buffer_t *buffer, zend_long interval) {
__atomic_store_n(&buffer->options.interval, interval * 1000, __ATOMIC_SEQ_CST);
}
zend_long zend_stat_buffer_interval_get(zend_stat_buffer_t *buffer) {
return __atomic_load_n(&buffer->options.interval, __ATOMIC_SEQ_CST);
}
zend_bool zend_stat_buffer_arginfo_get(zend_stat_buffer_t *buffer) {
return __atomic_load_n(&buffer->options.arginfo, __ATOMIC_SEQ_CST);
}
void zend_stat_buffer_arginfo_set(zend_stat_buffer_t *buffer, zend_bool arginfo) {
__atomic_store_n(&buffer->options.arginfo, arginfo, __ATOMIC_SEQ_CST);
}
void zend_stat_buffer_shutdown(zend_stat_buffer_t *buffer) {
zend_stat_unmap(buffer, zend_stat_buffer_size(buffer->max));
}
#endif /* ZEND_STAT_BUFFER */