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
|
#include "uwsgi.h"
extern struct uwsgi_server uwsgi;
/*
This is an high-performance memory area shared by all workers/cores/threads
Contrary to the caching subsystem it is 1-copy (caching for non-c apps is 2-copy)
Languages not allowing that kind of access should emulate it calling uwsgi_malloc and then copying it back to
the language object.
The memory areas could be monitored for changes (read: cores can be suspended while waiting for values)
You can configure multiple areas specifying multiple --sharedarea options
This is a very low-level api, try to use it to build higher-level primitives or rely on the caching subsystem
*/
struct uwsgi_sharedarea *uwsgi_sharedarea_get_by_id(int id, uint64_t pos) {
if (id > uwsgi.sharedareas_cnt-1) return NULL;
struct uwsgi_sharedarea *sa = uwsgi.sharedareas[id];
if (pos > sa->max_pos) return NULL;
return sa;
}
int uwsgi_sharedarea_update(int id) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, 0);
if (!sa) return -1;
sa->updates++;
return 0;
}
int uwsgi_sharedarea_rlock(int id) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, 0);
if (!sa) return -1;
uwsgi_rlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_wlock(int id) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, 0);
if (!sa) return -1;
uwsgi_wlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_unlock(int id) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, 0);
if (!sa) return -1;
uwsgi_rwunlock(sa->lock);
return 0;
}
int64_t uwsgi_sharedarea_read(int id, uint64_t pos, char *blob, uint64_t len) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + len > sa->max_pos + 1) return -1;
if (len == 0) len = (sa->max_pos + 1) - pos;
if (sa->honour_used && sa->used-pos < len) len = sa->used-pos ;
uwsgi_rlock(sa->lock);
memcpy(blob, sa->area + pos, len);
sa->hits++;
uwsgi_rwunlock(sa->lock);
return len;
}
int uwsgi_sharedarea_write(int id, uint64_t pos, char *blob, uint64_t len) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + len > sa->max_pos + 1) return -1;
uwsgi_wlock(sa->lock);
memcpy(sa->area + pos, blob, len);
sa->updates++;
uwsgi_rwunlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_read64(int id, uint64_t pos, int64_t *value) {
int64_t rlen = uwsgi_sharedarea_read(id, pos, (char *) value, 8);
return rlen == 8 ? 0 : -1;
}
int uwsgi_sharedarea_write64(int id, uint64_t pos, int64_t *value) {
return uwsgi_sharedarea_write(id, pos, (char *) value, 8);
}
int uwsgi_sharedarea_read8(int id, uint64_t pos, int8_t *value) {
int64_t rlen = uwsgi_sharedarea_read(id, pos, (char *) value, 1);
return rlen == 1 ? 0 : -1;
}
int uwsgi_sharedarea_write8(int id, uint64_t pos, int8_t *value) {
return uwsgi_sharedarea_write(id, pos, (char *) value, 1);
}
int uwsgi_sharedarea_read16(int id, uint64_t pos, int16_t *value) {
int64_t rlen = uwsgi_sharedarea_read(id, pos, (char *) value, 2);
return rlen == 2 ? 0 : -1;
}
int uwsgi_sharedarea_write16(int id, uint64_t pos, int16_t *value) {
return uwsgi_sharedarea_write(id, pos, (char *) value, 2);
}
int uwsgi_sharedarea_read32(int id, uint64_t pos, int32_t *value) {
int64_t rlen = uwsgi_sharedarea_read(id, pos, (char *) value, 4);
return rlen == 4 ? 0 : -1;
}
int uwsgi_sharedarea_write32(int id, uint64_t pos, int32_t *value) {
return uwsgi_sharedarea_write(id, pos, (char *) value, 4);
}
int uwsgi_sharedarea_inc8(int id, uint64_t pos, int8_t amount) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + 1 > sa->max_pos + 1) return -1;
uwsgi_wlock(sa->lock);
int8_t *n_ptr = (int8_t *) (sa->area + pos);
*n_ptr+=amount;
sa->updates++;
uwsgi_rwunlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_inc16(int id, uint64_t pos, int16_t amount) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + 2 > sa->max_pos + 1) return -1;
uwsgi_wlock(sa->lock);
int16_t *n_ptr = (int16_t *) (sa->area + pos);
*n_ptr+=amount;
sa->updates++;
uwsgi_rwunlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_inc32(int id, uint64_t pos, int32_t amount) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + 4 > sa->max_pos + 1) return -1;
uwsgi_wlock(sa->lock);
int32_t *n_ptr = (int32_t *) (sa->area + pos);
*n_ptr+=amount;
sa->updates++;
uwsgi_rwunlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_inc64(int id, uint64_t pos, int64_t amount) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + 4 > sa->max_pos + 1) return -1;
uwsgi_wlock(sa->lock);
int64_t *n_ptr = (int64_t *) (sa->area + pos);
*n_ptr+=amount;
sa->updates++;
uwsgi_rwunlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_dec8(int id, uint64_t pos, int8_t amount) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + 1 > sa->max_pos + 1) return -1;
uwsgi_wlock(sa->lock);
int8_t *n_ptr = (int8_t *) (sa->area + pos);
*n_ptr-=amount;
sa->updates++;
uwsgi_rwunlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_dec16(int id, uint64_t pos, int16_t amount) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + 2 > sa->max_pos + 1) return -1;
uwsgi_wlock(sa->lock);
int16_t *n_ptr = (int16_t *) (sa->area + pos);
*n_ptr-=amount;
sa->updates++;
uwsgi_rwunlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_dec32(int id, uint64_t pos, int32_t amount) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + 4 > sa->max_pos + 1) return -1;
uwsgi_wlock(sa->lock);
int32_t *n_ptr = (int32_t *) (sa->area + pos);
*n_ptr-=amount;
sa->updates++;
uwsgi_rwunlock(sa->lock);
return 0;
}
int uwsgi_sharedarea_dec64(int id, uint64_t pos, int64_t amount) {
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, pos);
if (!sa) return -1;
if (pos + 4 > sa->max_pos + 1) return -1;
uwsgi_wlock(sa->lock);
int64_t *n_ptr = (int64_t *) (sa->area + pos);
*n_ptr-=amount;
sa->updates++;
uwsgi_rwunlock(sa->lock);
return 0;
}
/*
returns:
0 -> on updates
-1 -> on error
-2 -> on timeout
*/
int uwsgi_sharedarea_wait(int id, int freq, int timeout) {
int waiting = 0;
struct uwsgi_sharedarea *sa = uwsgi_sharedarea_get_by_id(id, 0);
if (!sa) return -1;
if (!freq) freq = 100;
uwsgi_rlock(sa->lock);
uint64_t updates = sa->updates;
uwsgi_rwunlock(sa->lock);
while(timeout == 0 || waiting == 0 || (timeout > 0 && waiting > 0 && (waiting/1000) < timeout)) {
if (uwsgi.wait_milliseconds_hook(freq)) {
uwsgi_rwunlock(sa->lock);
return -1;
}
waiting += freq;
// lock sa
uwsgi_rlock(sa->lock);
if (sa->updates != updates) {
uwsgi_rwunlock(sa->lock);
return 0;
}
// unlock sa
uwsgi_rwunlock(sa->lock);
}
return -2;
}
int uwsgi_sharedarea_new_id() {
int id = uwsgi.sharedareas_cnt;
uwsgi.sharedareas_cnt++;
if (!uwsgi.sharedareas) {
uwsgi.sharedareas = uwsgi_malloc(sizeof(struct uwsgi_sharedarea *));
}
else {
struct uwsgi_sharedarea **usa = realloc(uwsgi.sharedareas, ((sizeof(struct uwsgi_sharedarea *)) * uwsgi.sharedareas_cnt));
if (!usa) {
uwsgi_error("uwsgi_sharedarea_init()/realloc()");
exit(1);
}
uwsgi.sharedareas = usa;
}
return id;
}
static struct uwsgi_sharedarea *announce_sa(struct uwsgi_sharedarea *sa) {
uwsgi_log("sharedarea %d created at %p (%d pages, area at %p)\n", sa->id, sa, sa->pages, sa->area);
return sa;
}
struct uwsgi_sharedarea *uwsgi_sharedarea_init_fd(int fd, uint64_t len, off_t offset) {
int id = uwsgi_sharedarea_new_id();
uwsgi.sharedareas[id] = uwsgi_calloc_shared(sizeof(struct uwsgi_sharedarea));
uwsgi.sharedareas[id]->area = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset);
if (uwsgi.sharedareas[id]->area == MAP_FAILED) {
uwsgi_error("uwsgi_sharedarea_init_fd()/mmap()");
exit(1);
}
uwsgi.sharedareas[id]->id = id;
uwsgi.sharedareas[id]->fd = fd;
uwsgi.sharedareas[id]->pages = len / (size_t) uwsgi.page_size;
if (len % (size_t) uwsgi.page_size != 0) uwsgi.sharedareas[id]->pages++;
uwsgi.sharedareas[id]->max_pos = len-1;
char *id_str = uwsgi_num2str(id);
uwsgi.sharedareas[id]->lock = uwsgi_rwlock_init(uwsgi_concat2("sharedarea", id_str));
free(id_str);
return announce_sa(uwsgi.sharedareas[id]);
}
struct uwsgi_sharedarea *uwsgi_sharedarea_init(int pages) {
int id = uwsgi_sharedarea_new_id();
uwsgi.sharedareas[id] = uwsgi_calloc_shared((size_t)uwsgi.page_size * (size_t)(pages + 1));
uwsgi.sharedareas[id]->area = ((char *) uwsgi.sharedareas[id]) + (size_t) uwsgi.page_size;
uwsgi.sharedareas[id]->id = id;
uwsgi.sharedareas[id]->fd = -1;
uwsgi.sharedareas[id]->pages = pages;
uwsgi.sharedareas[id]->max_pos = ((size_t)uwsgi.page_size * (size_t)pages) -1;
char *id_str = uwsgi_num2str(id);
uwsgi.sharedareas[id]->lock = uwsgi_rwlock_init(uwsgi_concat2("sharedarea", id_str));
free(id_str);
return announce_sa(uwsgi.sharedareas[id]);
}
struct uwsgi_sharedarea *uwsgi_sharedarea_init_ptr(char *area, uint64_t len) {
int id = uwsgi_sharedarea_new_id();
uwsgi.sharedareas[id] = uwsgi_calloc_shared(sizeof(struct uwsgi_sharedarea));
uwsgi.sharedareas[id]->area = area;
uwsgi.sharedareas[id]->id = id;
uwsgi.sharedareas[id]->fd = -1;
uwsgi.sharedareas[id]->pages = len / (size_t) uwsgi.page_size;
if (len % (size_t) uwsgi.page_size != 0) uwsgi.sharedareas[id]->pages++;
uwsgi.sharedareas[id]->max_pos = len-1;
char *id_str = uwsgi_num2str(id);
uwsgi.sharedareas[id]->lock = uwsgi_rwlock_init(uwsgi_concat2("sharedarea", id_str));
free(id_str);
return announce_sa(uwsgi.sharedareas[id]);
}
struct uwsgi_sharedarea *uwsgi_sharedarea_init_keyval(char *arg) {
char *s_pages = NULL;
char *s_file = NULL;
char *s_fd = NULL;
char *s_ptr = NULL;
char *s_size = NULL;
char *s_offset = NULL;
if (uwsgi_kvlist_parse(arg, strlen(arg), ',', '=',
"pages", &s_pages,
"file", &s_file,
"fd", &s_fd,
"ptr", &s_ptr,
"size", &s_size,
"offset", &s_offset,
NULL)) {
uwsgi_log("invalid sharedarea keyval syntax\n");
exit(1);
}
uint64_t len = 0;
off_t offset = 0;
int pages = 0;
if (s_size) {
if (strlen(s_size) > 2 && s_size[0] == '0' && s_size[1] == 'x') {
len = strtoul(s_size+2, NULL, 16);
}
else {
len = uwsgi_n64(s_size);
}
pages = len / (size_t) uwsgi.page_size;
if (len % (size_t) uwsgi.page_size != 0) pages++;
}
if (s_offset) {
if (strlen(s_offset) > 2 && s_offset[0] == '0' && s_offset[1] == 'x') {
offset = strtoul(s_offset+2, NULL, 16);
}
else {
offset = uwsgi_n64(s_offset);
}
}
if (s_pages) {
pages = atoi(s_pages);
}
char *area = NULL;
struct uwsgi_sharedarea *sa = NULL;
int fd = -1;
if (s_file) {
fd = open(s_file, O_RDWR|O_SYNC);
if (fd < 0) {
uwsgi_error_open(s_file);
exit(1);
}
}
else if (s_fd) {
fd = atoi(s_fd);
}
else if (s_ptr) {
}
if (pages) {
if (fd > -1) {
sa = uwsgi_sharedarea_init_fd(fd, len, offset);
}
else if (area) {
sa = uwsgi_sharedarea_init_ptr(area, len);
}
else {
sa = uwsgi_sharedarea_init(pages);
}
}
else {
uwsgi_log("you need to set a size for a sharedarea !!! [%s]\n", arg);
exit(1);
}
if (s_pages) free(s_pages);
if (s_file) free(s_file);
if (s_fd) free(s_fd);
if (s_ptr) free(s_ptr);
if (s_size) free(s_size);
if (s_offset) free(s_offset);
return sa;
}
void uwsgi_sharedareas_init() {
struct uwsgi_string_list *usl = NULL;
uwsgi_foreach(usl, uwsgi.sharedareas_list) {
char *is_keyval = strchr(usl->value, '=');
if (!is_keyval) {
uwsgi_sharedarea_init(atoi(usl->value));
}
else {
uwsgi_sharedarea_init_keyval(usl->value);
}
}
}
|