mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-11-21 10:56:10 +00:00
first commit of the new caching infrastructure
This commit is contained in:
+242
-160
@@ -2,33 +2,54 @@
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
// block bitmap manager
|
||||
/*
|
||||
static uint64_t cache_get_block_by_bitmap(struct uwsgi_cache *uc, uint64_t len) {
|
||||
// first of all, how many blocks i need ?
|
||||
uint64_t blocks = len/uc->blocksize;
|
||||
if (len%uc->blocksize > 0) blocks++;
|
||||
uwsgi_log("searching for %d free blocks\n");
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
static void cache_unmark_blocks(struct uwsgi_cache *uc, uint64_t index, uint64_t len) {
|
||||
/*
|
||||
uint64_t base = index/8;
|
||||
uint64_t bit = index%8;
|
||||
uint64_t blocks = len/uc->blocksize;
|
||||
if (len%uc->blocksize > 0) blocks++;
|
||||
*/
|
||||
}
|
||||
|
||||
static void cache_send_udp_command(char *, uint16_t, char *, uint16_t, uint64_t, uint8_t);
|
||||
|
||||
static void cache_sync_hook(char *k, uint16_t kl, char *v, uint16_t vl, void *data) {
|
||||
struct uwsgi_cache *uc = (struct uwsgi_cache *) data;
|
||||
if (!uwsgi_strncmp(k, kl, "items", 5)) {
|
||||
size_t num = uwsgi_str_num(v, vl);
|
||||
if (num != uwsgi.cache_max_items) {
|
||||
uwsgi_log("[cache-sync] invalid cache size, expected %llu received %llu\n", (unsigned long long) uwsgi.cache_max_items, (unsigned long long) num);
|
||||
if (num != uc->max_items) {
|
||||
uwsgi_log("[cache-sync] invalid cache size, expected %llu received %llu\n", (unsigned long long) uc->max_items, (unsigned long long) num);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (!uwsgi_strncmp(k, kl, "blocksize", 9)) {
|
||||
size_t num = uwsgi_str_num(v, vl);
|
||||
if (num != uwsgi.cache_blocksize) {
|
||||
uwsgi_log("[cache-sync] invalid cache block size, expected %llu received %llu\n", (unsigned long long) uwsgi.cache_blocksize, (unsigned long long) num);
|
||||
if (num != uc->blocksize) {
|
||||
uwsgi_log("[cache-sync] invalid cache block size, expected %llu received %llu\n", (unsigned long long) uc->blocksize, (unsigned long long) num);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void uwsgi_cache_load_files() {
|
||||
static void uwsgi_cache_load_files(struct uwsgi_cache *uc) {
|
||||
|
||||
struct uwsgi_string_list *usl = uwsgi.load_file_in_cache;
|
||||
while(usl) {
|
||||
size_t len = 0;
|
||||
char *value = uwsgi_open_and_read(usl->value, &len, 0, NULL);
|
||||
if (value) {
|
||||
if (!uwsgi_cache_set(usl->value, usl->len, value, len, 0, 0)) {
|
||||
if (!uwsgi_cache_set2(uc,usl->value, usl->len, value, len, 0, 0)) {
|
||||
uwsgi_log("[cache] stored %s\n", usl->value);
|
||||
}
|
||||
}
|
||||
@@ -36,82 +57,81 @@ static void uwsgi_cache_load_files() {
|
||||
}
|
||||
}
|
||||
|
||||
void uwsgi_init_cache() {
|
||||
|
||||
uwsgi.cache_hashtable = (uint64_t *) mmap(NULL, sizeof(uint64_t) * UMAX16, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
|
||||
if (!uwsgi.cache_hashtable) {
|
||||
uwsgi_error("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memset(uwsgi.cache_hashtable, 0, sizeof(uint64_t) * UMAX16);
|
||||
|
||||
uwsgi.cache_unused_stack = (uint64_t *) mmap(NULL, sizeof(uint64_t) * uwsgi.cache_max_items, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
|
||||
if (!uwsgi.cache_unused_stack) {
|
||||
uwsgi_error("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memset(uwsgi.cache_unused_stack, 0, sizeof(uint64_t) * uwsgi.cache_max_items);
|
||||
void uwsgi_cache_init(struct uwsgi_cache *uc) {
|
||||
|
||||
uc->hashtable = uwsgi_calloc_shared(sizeof(uint64_t) * uc->hashsize);
|
||||
uc->unused_blocks_stack = uwsgi_calloc_shared(sizeof(uint64_t) * uc->blocks);
|
||||
// the first cache item is always zero
|
||||
uwsgi.shared->cache_first_available_item = 1;
|
||||
uwsgi.shared->cache_unused_stack_ptr = 0;
|
||||
uwsgi.cache_filesize = (sizeof(struct uwsgi_cache_item) * uwsgi.cache_max_items) + (uwsgi.cache_blocksize * uwsgi.cache_max_items);
|
||||
uc->first_available_block = 1;
|
||||
uc->unused_blocks_stack_ptr = 0;
|
||||
uc->filesize = ( (sizeof(struct uwsgi_cache_item)+uc->keysize) * uc->max_items) + (uc->blocksize * uc->blocks);
|
||||
|
||||
if (uc->use_blocks_bitmap) {
|
||||
uc->blocks_bitmap = uwsgi_calloc_shared(uc->blocks/8);
|
||||
}
|
||||
|
||||
//uwsgi.cache_items = (struct uwsgi_cache_item *) mmap(NULL, sizeof(struct uwsgi_cache_item) * uwsgi.cache_max_items, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
|
||||
if (uwsgi.cache_store) {
|
||||
if (uc->store) {
|
||||
int cache_fd;
|
||||
struct stat cst;
|
||||
|
||||
if (stat(uwsgi.cache_store, &cst)) {
|
||||
uwsgi_log("creating a new cache store file: %s\n", uwsgi.cache_store);
|
||||
cache_fd = open(uwsgi.cache_store, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
if (stat(uc->store, &cst)) {
|
||||
uwsgi_log("creating a new cache store file: %s\n", uc->store);
|
||||
cache_fd = open(uc->store, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
if (cache_fd >= 0) {
|
||||
// fill the caching store
|
||||
if (ftruncate(cache_fd, uwsgi.cache_filesize)) {
|
||||
if (ftruncate(cache_fd, uc->filesize)) {
|
||||
uwsgi_log("ftruncate()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((size_t) cst.st_size != uwsgi.cache_filesize || !S_ISREG(cst.st_mode)) {
|
||||
if ((size_t) cst.st_size != uc->filesize || !S_ISREG(cst.st_mode)) {
|
||||
uwsgi_log("invalid cache store file. Please remove it or fix cache blocksize/items to match its size\n");
|
||||
exit(1);
|
||||
}
|
||||
cache_fd = open(uwsgi.cache_store, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
uwsgi_log("recovered cache from backing store file: %s\n", uwsgi.cache_store);
|
||||
cache_fd = open(uc->store, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
uwsgi_log("recovered cache from backing store file: %s\n", uc->store);
|
||||
}
|
||||
|
||||
if (cache_fd < 0) {
|
||||
uwsgi_error_open(uwsgi.cache_store);
|
||||
uwsgi_error_open(uc->store);
|
||||
exit(1);
|
||||
}
|
||||
uwsgi.cache_items = (struct uwsgi_cache_item *) mmap(NULL, uwsgi.cache_filesize, PROT_READ | PROT_WRITE, MAP_SHARED, cache_fd, 0);
|
||||
uwsgi_cache_fix();
|
||||
uc->items = (struct uwsgi_cache_item *) mmap(NULL, uc->filesize, PROT_READ | PROT_WRITE, MAP_SHARED, cache_fd, 0);
|
||||
if (!uc->items) {
|
||||
uwsgi_error("uwsgi_cache_init()/mmap() [with store]");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uwsgi_cache_fix(uc);
|
||||
|
||||
}
|
||||
else {
|
||||
uwsgi.cache_items = (struct uwsgi_cache_item *) mmap(NULL, uwsgi.cache_filesize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
|
||||
int i;
|
||||
for (i = 0; i < (int) uwsgi.cache_max_items; i++) {
|
||||
memset(&uwsgi.cache_items[i], 0, sizeof(struct uwsgi_cache_item));
|
||||
uc->items = (struct uwsgi_cache_item *) mmap(NULL, uc->filesize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
|
||||
if (!uc->items) {
|
||||
uwsgi_error("uwsgi_cache_init()/mmap()");
|
||||
exit(1);
|
||||
}
|
||||
uint64_t i;
|
||||
for (i = 0; i < uc->max_items; i++) {
|
||||
// here we only need to clear the item header
|
||||
memset(&uc->items[i], 0, sizeof(struct uwsgi_cache_item));
|
||||
}
|
||||
}
|
||||
|
||||
if (!uwsgi.cache_items) {
|
||||
uwsgi_error("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
uc->data = uc->items + ((sizeof(struct uwsgi_cache_item)+uc->keysize) * uc->max_items);
|
||||
|
||||
uwsgi.cache = ((void *) uwsgi.cache_items) + (sizeof(struct uwsgi_cache_item) * uwsgi.cache_max_items);
|
||||
uc->lock = uwsgi_rwlock_init("cache");
|
||||
|
||||
uwsgi.cache_lock = uwsgi_rwlock_init("cache");
|
||||
uwsgi_log("*** Cache subsystem initialized: %lluMB (key: %llu bytes, keys: %llu bytes, data: %llu bytes) preallocated ***\n", (unsigned long long) uc->filesize / (1024 * 1024),
|
||||
(unsigned long long) sizeof(struct uwsgi_cache_item)+uc->keysize,
|
||||
(unsigned long long) ((sizeof(struct uwsgi_cache_item)+uc->keysize) * uc->max_items), (unsigned long long) (uc->blocksize * uc->max_items));
|
||||
|
||||
uwsgi_log("*** Cache subsystem initialized: %lluMB (key: %llu bytes, keys: %llu bytes, data: %llu bytes) preallocated ***\n", (unsigned long long) ((uwsgi.cache_blocksize * uwsgi.cache_max_items) + (sizeof(struct uwsgi_cache_item) * uwsgi.cache_max_items)) / (1024 * 1024), (unsigned long long) sizeof(struct uwsgi_cache_item), (unsigned long long) (sizeof(struct uwsgi_cache_item) * uwsgi.cache_max_items), (unsigned long long) (uwsgi.cache_blocksize * uwsgi.cache_max_items));
|
||||
|
||||
uwsgi_cache_load_files();
|
||||
uwsgi_cache_load_files(uc);
|
||||
|
||||
struct uwsgi_string_list *usl = uwsgi.cache_udp_node;
|
||||
while(usl) {
|
||||
@@ -171,50 +191,37 @@ void uwsgi_init_cache() {
|
||||
|
||||
uwsgi_hooked_parse(dump_buf, cuh.pktsize, cache_sync_hook, NULL);
|
||||
|
||||
ret = uwsgi_read_nb(fd, (char *) uwsgi.cache_items, uwsgi.cache_filesize, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
ret = uwsgi_read_nb(fd, (char *) uc->items, uc->filesize, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
if (ret) {
|
||||
uwsgi_log("[cache-sync] unable to read from the cache server\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// reset the hashtable
|
||||
memset(uwsgi.cache_hashtable, 0, sizeof(uint64_t) * UMAX16);
|
||||
memset(uc->hashtable, 0, sizeof(uint64_t) * UMAX16);
|
||||
// re-fill the hashtable
|
||||
uwsgi_cache_fix();
|
||||
uwsgi_cache_fix(uc);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t djb33x_hash(char *key, int keylen) {
|
||||
static inline uint64_t uwsgi_cache_get_index(struct uwsgi_cache *uc, char *key, uint16_t keylen) {
|
||||
|
||||
register uint32_t hash = 5381;
|
||||
int i;
|
||||
uint32_t hash = uc->hash->func(key, keylen);
|
||||
|
||||
for (i = 0; i < keylen; i++) {
|
||||
hash = ((hash << 5) + hash) ^ key[i];
|
||||
}
|
||||
int hash_key = hash % uc->hashsize;
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
static inline uint64_t uwsgi_cache_get_index(char *key, uint16_t keylen) {
|
||||
|
||||
uint32_t hash = djb33x_hash(key, keylen);
|
||||
|
||||
int hash_key = hash % 0xffff;
|
||||
|
||||
uint64_t slot = uwsgi.cache_hashtable[hash_key];
|
||||
uint64_t slot = uc->hashtable[hash_key];
|
||||
|
||||
struct uwsgi_cache_item *uci;
|
||||
uint64_t rounds = 0;
|
||||
|
||||
//uwsgi_log("found slot %d for key %d\n", slot, hash_key);
|
||||
|
||||
uci = &uwsgi.cache_items[slot];
|
||||
uci = &uc->items[slot];
|
||||
|
||||
// first round
|
||||
if (uci->djbhash != hash)
|
||||
if (uci->hash != hash)
|
||||
return 0;
|
||||
if (uci->keysize != keylen)
|
||||
goto cycle;
|
||||
@@ -226,9 +233,9 @@ static inline uint64_t uwsgi_cache_get_index(char *key, uint16_t keylen) {
|
||||
cycle:
|
||||
while (uci->next) {
|
||||
slot = uci->next;
|
||||
uci = &uwsgi.cache_items[slot];
|
||||
uci = &uc->items[slot];
|
||||
rounds++;
|
||||
if (rounds > uwsgi.cache_max_items) {
|
||||
if (rounds > uc->max_items) {
|
||||
uwsgi_log("ALARM !!! cache-loop (and potential deadlock) detected slot = %lu prev = %lu next = %lu\n", slot, uci->prev, uci->next);
|
||||
// terrible case: the whole uWSGI stack can deadlock, leaving only the master alive
|
||||
// if the master is avalable, trigger a brutal reload
|
||||
@@ -240,7 +247,7 @@ cycle:
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (uci->djbhash != hash)
|
||||
if (uci->hash != hash)
|
||||
return 0;
|
||||
if (uci->keysize != keylen)
|
||||
continue;
|
||||
@@ -251,73 +258,72 @@ cycle:
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t uwsgi_cache_exists(char *key, uint16_t keylen) {
|
||||
uint32_t uwsgi_cache_exists2(struct uwsgi_cache *uc, char *key, uint16_t keylen) {
|
||||
|
||||
return uwsgi_cache_get_index(key, keylen);
|
||||
return uwsgi_cache_get_index(uc, key, keylen);
|
||||
}
|
||||
|
||||
char *uwsgi_cache_get(char *key, uint16_t keylen, uint64_t * valsize) {
|
||||
char *uwsgi_cache_get2(struct uwsgi_cache *uc, char *key, uint16_t keylen, uint64_t * valsize) {
|
||||
|
||||
uint64_t index = uwsgi_cache_get_index(key, keylen);
|
||||
uint64_t index = uwsgi_cache_get_index(uc, key, keylen);
|
||||
|
||||
if (index) {
|
||||
if (uwsgi.cache_items[index].flags & UWSGI_CACHE_FLAG_UNGETTABLE)
|
||||
if (uc->items[index].flags & UWSGI_CACHE_FLAG_UNGETTABLE)
|
||||
return NULL;
|
||||
*valsize = uwsgi.cache_items[index].valsize;
|
||||
uwsgi.cache_items[index].hits++;
|
||||
ushared->cache_hits++;
|
||||
return uwsgi.cache + (index * uwsgi.cache_blocksize);
|
||||
*valsize = uc->items[index].valsize;
|
||||
uc->items[index].hits++;
|
||||
uc->hits++;
|
||||
return uc->data + (index * uc->blocksize);
|
||||
}
|
||||
|
||||
ushared->cache_miss++;
|
||||
uc->miss++;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int uwsgi_cache_del(char *key, uint16_t keylen, uint64_t index, uint16_t flags) {
|
||||
int uwsgi_cache_del2(struct uwsgi_cache *uc, char *key, uint16_t keylen, uint64_t index, uint16_t flags) {
|
||||
|
||||
struct uwsgi_cache_item *uci;
|
||||
int ret = -1;
|
||||
|
||||
if (!index)
|
||||
index = uwsgi_cache_get_index(key, keylen);
|
||||
index = uwsgi_cache_get_index(uc, key, keylen);
|
||||
|
||||
if (index) {
|
||||
uci = &uwsgi.cache_items[index];
|
||||
uci = &uc->items[index];
|
||||
uci->keysize = 0;
|
||||
cache_unmark_blocks(uc, index, uci->valsize);
|
||||
uci->valsize = 0;
|
||||
uwsgi.shared->cache_unused_stack_ptr++;
|
||||
uwsgi.cache_unused_stack[uwsgi.shared->cache_unused_stack_ptr] = index;
|
||||
// try to return to initial condition...
|
||||
if (index == uwsgi.shared->cache_first_available_item - 1) {
|
||||
uwsgi.shared->cache_first_available_item--;
|
||||
//uwsgi_log("FACI: %llu STACK PTR: %llu\n", (unsigned long long) uwsgi.shared->cache_first_available_item, (unsigned long long) uwsgi.shared->cache_unused_stack_ptr);
|
||||
if (index == uc->first_available_block - 1) {
|
||||
uc->first_available_block--;
|
||||
//uwsgi_log("FACI: %llu STACK PTR: %llu\n", (unsigned long long) uwsgi.shared->cache_first_available_block, (unsigned long long) uwsgi.shared->cache_unused_blocks_stack_ptr);
|
||||
}
|
||||
ret = 0;
|
||||
// relink collisioned entry
|
||||
if (uci->prev) {
|
||||
uwsgi.cache_items[uci->prev].next = uci->next;
|
||||
uc->items[uci->prev].next = uci->next;
|
||||
}
|
||||
else {
|
||||
// set next as the new entry point (could be 0)
|
||||
uwsgi.cache_hashtable[uci->djbhash % 0xffff] = uci->next;
|
||||
uc->hashtable[uci->hash % uc->hashsize] = uci->next;
|
||||
}
|
||||
|
||||
if (uci->next) {
|
||||
uwsgi.cache_items[uci->next].prev = uci->prev;
|
||||
uc->items[uci->next].prev = uci->prev;
|
||||
}
|
||||
|
||||
if (!uci->prev && !uci->next) {
|
||||
// reset hashtable entry
|
||||
//uwsgi_log("!!! resetted hashtable entry !!!\n");
|
||||
uwsgi.cache_hashtable[uci->djbhash % 0xffff] = 0;
|
||||
uc->hashtable[uci->hash % uc->hashsize] = 0;
|
||||
}
|
||||
uci->djbhash = 0;
|
||||
uci->hash = 0;
|
||||
uci->prev = 0;
|
||||
uci->next = 0;
|
||||
uci->expires = 0;
|
||||
|
||||
ushared->cache_items--;
|
||||
uc->n_items--;
|
||||
}
|
||||
|
||||
if (uwsgi.cache_udp_node && ret == 0 && !(flags & UWSGI_CACHE_FLAG_LOCAL)) {
|
||||
@@ -327,32 +333,32 @@ int uwsgi_cache_del(char *key, uint16_t keylen, uint64_t index, uint16_t flags)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void uwsgi_cache_fix() {
|
||||
void uwsgi_cache_fix(struct uwsgi_cache *uc) {
|
||||
|
||||
uint64_t i;
|
||||
unsigned long long restored = 0;
|
||||
|
||||
for (i = 0; i < uwsgi.cache_max_items; i++) {
|
||||
for (i = 0; i < uc->max_items; i++) {
|
||||
// valid record ?
|
||||
if (uwsgi.cache_items[i].keysize) {
|
||||
if (!uwsgi.cache_items[i].prev) {
|
||||
if (uc->items[i].keysize) {
|
||||
if (!uc->items[i].prev) {
|
||||
// put value in hash_table
|
||||
uwsgi.cache_hashtable[uwsgi.cache_items[i].djbhash % 0xffff] = i;
|
||||
uc->hashtable[uc->items[i].hash % uc->hashsize] = i;
|
||||
restored++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// put this record in unused stack
|
||||
uwsgi.shared->cache_first_available_item = i;
|
||||
uwsgi.shared->cache_unused_stack_ptr++;
|
||||
uwsgi.cache_unused_stack[uwsgi.shared->cache_unused_stack_ptr] = i;
|
||||
uc->first_available_block = i;
|
||||
uc->unused_blocks_stack_ptr++;
|
||||
uc->unused_blocks_stack[uc->unused_blocks_stack_ptr] = i;
|
||||
}
|
||||
}
|
||||
|
||||
uwsgi_log("[uwsgi-cache] restored %llu items\n", restored);
|
||||
}
|
||||
|
||||
int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint64_t vallen, uint64_t expires, uint16_t flags) {
|
||||
int uwsgi_cache_set2(struct uwsgi_cache *uc, char *key, uint16_t keylen, char *val, uint64_t vallen, uint64_t expires, uint64_t flags) {
|
||||
|
||||
uint64_t index = 0, last_index = 0;
|
||||
|
||||
@@ -363,38 +369,38 @@ int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint64_t vallen, uint
|
||||
if (!keylen || !vallen)
|
||||
return -1;
|
||||
|
||||
if (keylen > UWSGI_CACHE_MAX_KEY_SIZE)
|
||||
if (keylen > uc->keysize)
|
||||
return -1;
|
||||
|
||||
if (uwsgi.shared->cache_first_available_item >= uwsgi.cache_max_items && !uwsgi.shared->cache_unused_stack_ptr) {
|
||||
if (uc->first_available_block >= uc->max_items && !uc->unused_blocks_stack_ptr) {
|
||||
uwsgi_log("*** DANGER cache is FULL !!! ***\n");
|
||||
ushared->cache_full++;
|
||||
uc->full++;
|
||||
goto end;
|
||||
}
|
||||
|
||||
//uwsgi_log("putting cache data in key %.*s %d\n", keylen, key, vallen);
|
||||
index = uwsgi_cache_get_index(key, keylen);
|
||||
index = uwsgi_cache_get_index(uc, key, keylen);
|
||||
if (!index) {
|
||||
if (uwsgi.shared->cache_unused_stack_ptr) {
|
||||
//uwsgi_log("!!! REUSING CACHE SLOT !!! (faci: %llu)\n", (unsigned long long) uwsgi.shared->cache_first_available_item);
|
||||
index = uwsgi.cache_unused_stack[uwsgi.shared->cache_unused_stack_ptr];
|
||||
uwsgi.shared->cache_unused_stack_ptr--;
|
||||
if (uc->unused_blocks_stack_ptr) {
|
||||
//uwsgi_log("!!! REUSING CACHE SLOT !!! (faci: %llu)\n", (unsigned long long) uwsgi.shared->cache_first_available_block);
|
||||
index = uc->unused_blocks_stack[uc->unused_blocks_stack_ptr];
|
||||
uc->unused_blocks_stack_ptr--;
|
||||
}
|
||||
else {
|
||||
index = uwsgi.shared->cache_first_available_item;
|
||||
if (uwsgi.shared->cache_first_available_item < uwsgi.cache_max_items) {
|
||||
uwsgi.shared->cache_first_available_item++;
|
||||
index = uc->first_available_block;
|
||||
if (uc->first_available_block < uc->max_items) {
|
||||
uc->first_available_block++;
|
||||
}
|
||||
}
|
||||
uci = &uwsgi.cache_items[index];
|
||||
uci = &uc->items[index];
|
||||
if (expires && !(flags & UWSGI_CACHE_FLAG_ABSEXPIRE))
|
||||
expires += uwsgi_now();
|
||||
uci->expires = expires;
|
||||
uci->djbhash = djb33x_hash(key, keylen);
|
||||
uci->hash = uc->hash->func(key, keylen);
|
||||
uci->hits = 0;
|
||||
uci->flags = flags;
|
||||
memcpy(uci->key, key, keylen);
|
||||
memcpy(uwsgi.cache + (index * uwsgi.cache_blocksize), val, vallen);
|
||||
memcpy(uc->data + (index * uc->blocksize), val, vallen);
|
||||
|
||||
// set this as late as possibile (to reduce races risk)
|
||||
|
||||
@@ -402,36 +408,36 @@ int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint64_t vallen, uint
|
||||
uci->keysize = keylen;
|
||||
ret = 0;
|
||||
// now put the value in the 16bit hashtable
|
||||
int slot = uci->djbhash % 0xffff;
|
||||
int slot = uci->hash % uc->hashsize;
|
||||
// reset values
|
||||
uci->prev = 0;
|
||||
uci->next = 0;
|
||||
|
||||
if (uwsgi.cache_hashtable[slot] == 0) {
|
||||
uwsgi.cache_hashtable[slot] = index;
|
||||
if (uc->hashtable[slot] == 0) {
|
||||
uc->hashtable[slot] = index;
|
||||
}
|
||||
else {
|
||||
//uwsgi_log("HASH COLLISION !!!!\n");
|
||||
// append to first available next
|
||||
last_index = uwsgi.cache_hashtable[slot];
|
||||
ucii = &uwsgi.cache_items[last_index];
|
||||
last_index = uc->hashtable[slot];
|
||||
ucii = &uc->items[last_index];
|
||||
while (ucii->next) {
|
||||
last_index = ucii->next;
|
||||
ucii = &uwsgi.cache_items[last_index];
|
||||
ucii = &uc->items[last_index];
|
||||
}
|
||||
ucii->next = index;
|
||||
uci->prev = last_index;
|
||||
}
|
||||
|
||||
ushared->cache_items++ ;
|
||||
uc->n_items++ ;
|
||||
}
|
||||
else if (flags & UWSGI_CACHE_FLAG_UPDATE) {
|
||||
uci = &uwsgi.cache_items[index];
|
||||
uci = &uc->items[index];
|
||||
if (expires && !(flags & UWSGI_CACHE_FLAG_ABSEXPIRE)) {
|
||||
expires += uwsgi_now();
|
||||
uci->expires = expires;
|
||||
}
|
||||
memcpy(uwsgi.cache + (index * uwsgi.cache_blocksize), val, vallen);
|
||||
memcpy(uc->data + (index * uc->blocksize), val, vallen);
|
||||
uci->valsize = vallen;
|
||||
ret = 0;
|
||||
}
|
||||
@@ -521,10 +527,10 @@ static void cache_send_udp_command(char *key, uint16_t keylen, char *val, uint16
|
||||
|
||||
/* THIS PART IS HEAVILY OPTIMIZED: PERFORMANCE NOT ELEGANCE !!! */
|
||||
|
||||
void *cache_thread_loop(void *fd_ptr) {
|
||||
void *cache_thread_loop(void *ucache) {
|
||||
|
||||
int *fd_tmp = (int *) fd_ptr;
|
||||
int fd = *fd_tmp;
|
||||
struct uwsgi_cache *uc = (struct uwsgi_cache *) ucache;
|
||||
int fd = uc->thread_server_fd;
|
||||
int i;
|
||||
ssize_t len;
|
||||
char uwsgi_packet[UMAX16 + 4];
|
||||
@@ -602,14 +608,14 @@ void *cache_thread_loop(void *fd_ptr) {
|
||||
if (key + keylen > watermark)
|
||||
goto clear;
|
||||
|
||||
uwsgi_rlock(uwsgi.cache_lock);
|
||||
val = uwsgi_cache_get(key, keylen, &vallen);
|
||||
uwsgi_rlock(uc->lock);
|
||||
val = uwsgi_cache_get2(uc, key, keylen, &vallen);
|
||||
if (val && vallen > 0) {
|
||||
if (write(ctl_poll.fd, val, vallen) != (int64_t) vallen) {
|
||||
uwsgi_error("cache write()");
|
||||
}
|
||||
}
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uc->lock);
|
||||
|
||||
clear:
|
||||
close(ctl_poll.fd);
|
||||
@@ -646,30 +652,26 @@ int uwsgi_cache_server(char *socket, int threads) {
|
||||
return *fd;
|
||||
}
|
||||
|
||||
int uwsgi_cache_enabled() {
|
||||
if (uwsgi.cache_max_items > 0)
|
||||
return 1;
|
||||
return 0;
|
||||
void uwsgi_cache_wlock(struct uwsgi_cache *uc) {
|
||||
uwsgi.lock_ops.wlock(uc->lock);
|
||||
}
|
||||
|
||||
void uwsgi_cache_wlock() {
|
||||
uwsgi.lock_ops.wlock(uwsgi.cache_lock);
|
||||
void uwsgi_cache_rlock(struct uwsgi_cache *uc) {
|
||||
uwsgi.lock_ops.rlock(uc->lock);
|
||||
}
|
||||
|
||||
void uwsgi_cache_rlock() {
|
||||
uwsgi.lock_ops.rlock(uwsgi.cache_lock);
|
||||
void uwsgi_cache_rwunlock(struct uwsgi_cache *uc) {
|
||||
uwsgi.lock_ops.rwunlock(uc->lock);
|
||||
}
|
||||
|
||||
void uwsgi_cache_rwunlock() {
|
||||
uwsgi.lock_ops.rwunlock(uwsgi.cache_lock);
|
||||
}
|
||||
|
||||
void *cache_udp_server_loop(void *noarg) {
|
||||
void *cache_udp_server_loop(void *ucache) {
|
||||
// block all signals
|
||||
sigset_t smask;
|
||||
sigfillset(&smask);
|
||||
pthread_sigmask(SIG_BLOCK, &smask, NULL);
|
||||
|
||||
struct uwsgi_cache *uc = (struct uwsgi_cache *) ucache;
|
||||
|
||||
int queue = event_queue_init();
|
||||
struct uwsgi_string_list *usl = uwsgi.cache_udp_server;
|
||||
while(usl) {
|
||||
@@ -721,33 +723,35 @@ void *cache_udp_server_loop(void *noarg) {
|
||||
if (6+keylen+vallen+ss > pktsize) continue;
|
||||
expires = uwsgi_str_num(buf + 10 + keylen+vallen, ss);
|
||||
}
|
||||
uwsgi_wlock(uwsgi.cache_lock);
|
||||
uwsgi_wlock(uc->lock);
|
||||
if (uwsgi_cache_set(key, keylen, val, vallen, expires, UWSGI_CACHE_FLAG_UPDATE|UWSGI_CACHE_FLAG_LOCAL|UWSGI_CACHE_FLAG_ABSEXPIRE)) {
|
||||
uwsgi_log("[cache-udp-server] unable to update cache\n");
|
||||
}
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uc->lock);
|
||||
}
|
||||
// cache del
|
||||
else if (buf[3] == 11) {
|
||||
uwsgi_wlock(uwsgi.cache_lock);
|
||||
uwsgi_wlock(uc->lock);
|
||||
if (uwsgi_cache_del(key, keylen, 0, UWSGI_CACHE_FLAG_LOCAL)) {
|
||||
uwsgi_log("[cache-udp-server] unable to update cache\n");
|
||||
}
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uc->lock);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *cache_sweeper_loop(void *noarg) {
|
||||
void *cache_sweeper_loop(void *ucache) {
|
||||
|
||||
int i;
|
||||
uint64_t i;
|
||||
// block all signals
|
||||
sigset_t smask;
|
||||
sigfillset(&smask);
|
||||
pthread_sigmask(SIG_BLOCK, &smask, NULL);
|
||||
|
||||
struct uwsgi_cache *uc = (struct uwsgi_cache *) ucache;
|
||||
|
||||
if (!uwsgi.cache_expire_freq)
|
||||
uwsgi.cache_expire_freq = 3;
|
||||
|
||||
@@ -756,15 +760,15 @@ void *cache_sweeper_loop(void *noarg) {
|
||||
sleep(uwsgi.cache_expire_freq);
|
||||
uint64_t freed_items = 0;
|
||||
// skip the first slot
|
||||
for (i = 1; i < (int) uwsgi.cache_max_items; i++) {
|
||||
uwsgi_wlock(uwsgi.cache_lock);
|
||||
if (uwsgi.cache_items[i].expires) {
|
||||
if (uwsgi.cache_items[i].expires < (uint64_t) uwsgi.current_time) {
|
||||
uwsgi_cache_del(NULL, 0, i, UWSGI_CACHE_FLAG_LOCAL);
|
||||
for (i = 1; i < uc->max_items; i++) {
|
||||
uwsgi_wlock(uc->lock);
|
||||
if (uc->items[i].expires) {
|
||||
if (uc->items[i].expires < (uint64_t) uwsgi.current_time) {
|
||||
uwsgi_cache_del2(uc, NULL, 0, i, UWSGI_CACHE_FLAG_LOCAL);
|
||||
freed_items++;
|
||||
}
|
||||
}
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uc->lock);
|
||||
}
|
||||
if (uwsgi.cache_report_freed_items && freed_items > 0) {
|
||||
uwsgi_log("freed %llu cache items\n", (unsigned long long) freed_items);
|
||||
@@ -774,3 +778,81 @@ void *cache_sweeper_loop(void *noarg) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void uwsgi_cache_sync_all() {
|
||||
/*
|
||||
if (uwsgi.cache_store && uwsgi.cache_filesize) {
|
||||
if (msync(uwsgi.cache_items, uwsgi.cache_filesize, MS_ASYNC)) {
|
||||
uwsgi_error("msync()");
|
||||
}
|
||||
}
|
||||
|
||||
if (uwsgi.cache_store && uwsgi.cache_filesize && uwsgi.cache_store_sync && ((uwsgi.master_cycles % uwsgi.cache_store_sync) == 0)) {
|
||||
if (msync(uwsgi.cache_items, uwsgi.cache_filesize, MS_ASYNC)) {
|
||||
uwsgi_error("msync()");
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void uwsgi_cache_start_sweepers() {
|
||||
/*
|
||||
if (uwsgi.caches > 0 && !uwsgi.cache_no_expire) {
|
||||
if (pthread_create(&cache_sweeper, NULL, cache_sweeper_loop, NULL)) {
|
||||
uwsgi_error("pthread_create()");
|
||||
uwsgi_log("unable to run the cache sweeper !!!\n");
|
||||
}
|
||||
else {
|
||||
uwsgi_log("cache sweeper thread enabled\n");
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_cache_start_sync_servers() {
|
||||
|
||||
/*
|
||||
|
||||
if (uwsgi.cache_max_items > 0 && uwsgi.cache_udp_server) {
|
||||
if (pthread_create(&cache_udp_server, NULL, cache_udp_server_loop, NULL)) {
|
||||
uwsgi_error("pthread_create()");
|
||||
uwsgi_log("unable to run the cache udp server !!!\n");
|
||||
}
|
||||
else {
|
||||
uwsgi_log("cache udp server thread enabled\n");
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
void uwsgi_cache_create(char *arg) {
|
||||
struct uwsgi_cache *old_uc = NULL, *uc = uwsgi.caches;
|
||||
while(uc) {
|
||||
old_uc = uc;
|
||||
uc = uc->next;
|
||||
}
|
||||
|
||||
uc = uwsgi_calloc_shared(sizeof(struct uwsgi_cache));
|
||||
if (old_uc) {
|
||||
old_uc->next = uc;
|
||||
}
|
||||
else {
|
||||
uwsgi.caches = uc;
|
||||
}
|
||||
|
||||
// default (old-stye) cache ?
|
||||
if (!arg) {
|
||||
uc->blocksize = uwsgi.cache_blocksize;
|
||||
if (!uc->blocksize) uc->blocksize = UMAX16;
|
||||
uc->max_items = uwsgi.cache_max_items;
|
||||
uc->blocks = uwsgi.cache_max_items;
|
||||
uc->keysize = 2048;
|
||||
uc->hashsize = UMAX16;
|
||||
uc->hash = uwsgi_hash_algo_get("djb33x");
|
||||
}
|
||||
else {
|
||||
}
|
||||
|
||||
uwsgi_cache_init(uc);
|
||||
}
|
||||
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
#include <uwsgi.h>
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
// Bernstein classic hash (this is not static as it is used by other areas)
|
||||
uint32_t djb33x_hash(char *key, uint64_t keylen) {
|
||||
|
||||
register uint32_t hash = 5381;
|
||||
uint64_t i;
|
||||
|
||||
for (i = 0; i < keylen; i++) {
|
||||
hash = ((hash << 5) + hash) ^ key[i];
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
// Murmur2 hash Copyright (C) Austin Appleby
|
||||
// adapted from nginx
|
||||
uint32_t murmur2_hash(char *key, uint64_t keylen) {
|
||||
|
||||
uint32_t h, k;
|
||||
uint8_t *ukey = (uint8_t *) key;
|
||||
h = 0 ^ keylen;
|
||||
while (keylen >= 4) {
|
||||
k = ukey[0];
|
||||
k |= ukey[1] << 8;
|
||||
k |= ukey[2] << 16;
|
||||
k |= ukey[3] << 24;
|
||||
|
||||
k *= 0x5bd1e995;
|
||||
k ^= k >> 24;
|
||||
k *= 0x5bd1e995;
|
||||
|
||||
h *= 0x5bd1e995;
|
||||
h ^= k;
|
||||
|
||||
ukey += 4;
|
||||
keylen -= 4;
|
||||
}
|
||||
|
||||
switch (keylen) {
|
||||
case 3:
|
||||
h ^= key[2] << 16;
|
||||
case 2:
|
||||
h ^= key[1] << 8;
|
||||
case 1:
|
||||
h ^= key[0];
|
||||
h *= 0x5bd1e995;
|
||||
}
|
||||
|
||||
h ^= h >> 13;
|
||||
h *= 0x5bd1e995;
|
||||
h ^= h >> 15;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
struct uwsgi_hash_algo *uwsgi_hash_algo_get(char *name) {
|
||||
struct uwsgi_hash_algo *uha = uwsgi.hash_algos;
|
||||
while(uha) {
|
||||
if (!strcmp(name, uha->name)) {
|
||||
return uha;
|
||||
}
|
||||
uha = uha->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
void uwsgi_hash_algo_register(char *name, uint32_t (*func)(char *, uint64_t)) {
|
||||
|
||||
struct uwsgi_hash_algo *old_uha = NULL, *uha = uwsgi.hash_algos;
|
||||
while(uha) {
|
||||
if (!strcmp(uha->name, name)) return;
|
||||
old_uha = uha;
|
||||
uha = uha->next;
|
||||
}
|
||||
|
||||
uha = uwsgi_calloc(sizeof(struct uwsgi_hash_algo));
|
||||
uha->name = name;
|
||||
uha->func = func;
|
||||
if (old_uha) {
|
||||
old_uha->next = uha;
|
||||
}
|
||||
else {
|
||||
uwsgi.hash_algos = uha;
|
||||
}
|
||||
}
|
||||
|
||||
void uwsgi_hash_algo_register_all() {
|
||||
uwsgi_hash_algo_register("djb33x", djb33x_hash);
|
||||
uwsgi_hash_algo_register("murmur2", murmur2_hash);
|
||||
}
|
||||
+1
-2
@@ -69,7 +69,6 @@ void uwsgi_init_default() {
|
||||
uwsgi.signal_socket = -1;
|
||||
uwsgi.my_signal_socket = -1;
|
||||
uwsgi.cache_server_fd = -1;
|
||||
uwsgi.cache_blocksize = UMAX16;
|
||||
uwsgi.stats_fd = -1;
|
||||
|
||||
uwsgi.stats_pusher_default_freq = 3;
|
||||
@@ -414,7 +413,7 @@ void sanitize_args() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (uwsgi.static_cache_paths > 0 && !uwsgi.cache_max_items) {
|
||||
if (uwsgi.static_cache_paths > 0 && !uwsgi.caches) {
|
||||
uwsgi_log("caching of static paths requires uWSGI caching !!!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
+4
-31
@@ -444,8 +444,6 @@ int master_loop(char **argv, char **environ) {
|
||||
uint64_t last_request_count = 0;
|
||||
|
||||
pthread_t logger_thread;
|
||||
pthread_t cache_sweeper;
|
||||
pthread_t cache_udp_server;
|
||||
pthread_t channels_loop;
|
||||
|
||||
#ifdef UWSGI_UDP
|
||||
@@ -546,25 +544,8 @@ int master_loop(char **argv, char **environ) {
|
||||
uwsgi_start_legions();
|
||||
#endif
|
||||
|
||||
if (uwsgi.cache_max_items > 0 && !uwsgi.cache_no_expire) {
|
||||
if (pthread_create(&cache_sweeper, NULL, cache_sweeper_loop, NULL)) {
|
||||
uwsgi_error("pthread_create()");
|
||||
uwsgi_log("unable to run the cache sweeper !!!\n");
|
||||
}
|
||||
else {
|
||||
uwsgi_log("cache sweeper thread enabled\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (uwsgi.cache_max_items > 0 && uwsgi.cache_udp_server) {
|
||||
if (pthread_create(&cache_udp_server, NULL, cache_udp_server_loop, NULL)) {
|
||||
uwsgi_error("pthread_create()");
|
||||
uwsgi_log("unable to run the cache udp server !!!\n");
|
||||
}
|
||||
else {
|
||||
uwsgi_log("cache udp server thread enabled\n");
|
||||
}
|
||||
}
|
||||
uwsgi_cache_start_sweepers();
|
||||
uwsgi_cache_start_sync_servers();
|
||||
|
||||
if (uwsgi.channels) {
|
||||
if (pthread_create(&channels_loop, NULL, uwsgi_channels_loop, NULL)) {
|
||||
@@ -669,11 +650,7 @@ int master_loop(char **argv, char **environ) {
|
||||
uwsgi_subscribe_all(0, 1);
|
||||
|
||||
// sync the cache store if needed
|
||||
if (uwsgi.cache_store && uwsgi.cache_filesize) {
|
||||
if (msync(uwsgi.cache_items, uwsgi.cache_filesize, MS_ASYNC)) {
|
||||
uwsgi_error("msync()");
|
||||
}
|
||||
}
|
||||
uwsgi_cache_sync_all();
|
||||
|
||||
if (uwsgi.queue_store && uwsgi.queue_filesize) {
|
||||
if (msync(uwsgi.queue_header, uwsgi.queue_filesize, MS_ASYNC)) {
|
||||
@@ -1267,11 +1244,7 @@ health_cycle:
|
||||
|
||||
#endif
|
||||
|
||||
if (uwsgi.cache_store && uwsgi.cache_filesize && uwsgi.cache_store_sync && ((uwsgi.master_cycles % uwsgi.cache_store_sync) == 0)) {
|
||||
if (msync(uwsgi.cache_items, uwsgi.cache_filesize, MS_ASYNC)) {
|
||||
uwsgi_error("msync()");
|
||||
}
|
||||
}
|
||||
uwsgi_cache_sync_all();
|
||||
|
||||
if (uwsgi.queue_store && uwsgi.queue_filesize && uwsgi.queue_store_sync && ((uwsgi.master_cycles % uwsgi.queue_store_sync) == 0)) {
|
||||
if (msync(uwsgi.queue_header, uwsgi.queue_filesize, MS_ASYNC)) {
|
||||
|
||||
+27
-19
@@ -870,35 +870,43 @@ struct uwsgi_stats *uwsgi_master_generate_stats() {
|
||||
if (uwsgi_stats_comma(us))
|
||||
goto end;
|
||||
|
||||
if (uwsgi.cache_max_items > 0) {
|
||||
if (uwsgi_stats_key(us, "cache"))
|
||||
if (uwsgi.caches) {
|
||||
|
||||
|
||||
if (uwsgi_stats_key(us, "caches"))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_object_open(us))
|
||||
goto end;
|
||||
if (uwsgi_stats_list_open(us)) goto end;
|
||||
|
||||
if (uwsgi_stats_keylong_comma(us, "max_items", (unsigned long long) uwsgi.cache_max_items))
|
||||
goto end;
|
||||
struct uwsgi_cache *uc = uwsgi.caches;
|
||||
while(uc) {
|
||||
if (uwsgi_stats_object_open(us))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_keylong_comma(us, "blocksize", (unsigned long long) uwsgi.cache_blocksize))
|
||||
goto end;
|
||||
if (uwsgi_stats_keylong_comma(us, "max_items", (unsigned long long) uc->max_items))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_keylong_comma(us, "items", (unsigned long long) ushared->cache_items))
|
||||
goto end;
|
||||
if (uwsgi_stats_keylong_comma(us, "blocksize", (unsigned long long) uc->blocksize))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_keylong_comma(us, "hits", (unsigned long long) ushared->cache_hits))
|
||||
goto end;
|
||||
if (uwsgi_stats_keylong_comma(us, "items", (unsigned long long) uc->n_items))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_keylong_comma(us, "miss", (unsigned long long) ushared->cache_miss))
|
||||
goto end;
|
||||
if (uwsgi_stats_keylong_comma(us, "hits", (unsigned long long) uc->hits))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_keylong(us, "full", (unsigned long long) ushared->cache_full))
|
||||
goto end;
|
||||
if (uwsgi_stats_keylong_comma(us, "miss", (unsigned long long) uc->miss))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_object_close(us))
|
||||
goto end;
|
||||
if (uwsgi_stats_keylong(us, "full", (unsigned long long) uc->full))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_comma(us))
|
||||
if (uwsgi_stats_object_close(us))
|
||||
goto end;
|
||||
uc = uc->next;
|
||||
}
|
||||
|
||||
if (uwsgi_stats_comma(us))
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -657,7 +657,7 @@ static int uwsgi_proto_check_15(struct wsgi_request *wsgi_req, char *key, char *
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (uwsgi.cache_max_items > 0 && !uwsgi_proto_key("UWSGI_CACHE_GET", 15)) {
|
||||
if (uwsgi.caches > 0 && !uwsgi_proto_key("UWSGI_CACHE_GET", 15)) {
|
||||
wsgi_req->cache_get = buf;
|
||||
wsgi_req->cache_get_len = len;
|
||||
return 0;
|
||||
|
||||
+12
-12
@@ -45,13 +45,13 @@ int uwsgi_ssl_session_new_cb(SSL *ssl, SSL_SESSION *sess) {
|
||||
i2d_SSL_SESSION(sess, &p);
|
||||
|
||||
// ok let's write the value to the cache
|
||||
uwsgi_wlock(uwsgi.cache_lock);
|
||||
if (uwsgi_cache_set((char *) sess->session_id, sess->session_id_length, session_blob, len, uwsgi.ssl_sessions_timeout, 0)) {
|
||||
uwsgi_wlock(uwsgi.ssl_sessions_cache->lock);
|
||||
if (uwsgi_cache_set2(uwsgi.ssl_sessions_cache, (char *) sess->session_id, sess->session_id_length, session_blob, len, uwsgi.ssl_sessions_timeout, 0)) {
|
||||
if (uwsgi.ssl_verbose) {
|
||||
uwsgi_log("[uwsgi-ssl] unable to store session of size %d in the cache\n", len);
|
||||
}
|
||||
}
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uwsgi.ssl_sessions_cache->lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -60,28 +60,28 @@ SSL_SESSION *uwsgi_ssl_session_get_cb(SSL *ssl, unsigned char *key, int keylen,
|
||||
uint64_t valsize = 0;
|
||||
|
||||
*copy = 0;
|
||||
uwsgi_rlock(uwsgi.cache_lock);
|
||||
char *value = uwsgi_cache_get((char *)key, keylen, &valsize);
|
||||
uwsgi_rlock(uwsgi.ssl_sessions_cache->lock);
|
||||
char *value = uwsgi_cache_get2(uwsgi.ssl_sessions_cache, (char *)key, keylen, &valsize);
|
||||
if (!value) {
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uwsgi.ssl_sessions_cache->lock);
|
||||
if (uwsgi.ssl_verbose) {
|
||||
uwsgi_log("[uwsgi-ssl] cache miss\n");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
SSL_SESSION *sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&value, valsize);
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uwsgi.ssl_sessions_cache->lock);
|
||||
return sess;
|
||||
}
|
||||
|
||||
void uwsgi_ssl_session_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) {
|
||||
uwsgi_wlock(uwsgi.cache_lock);
|
||||
if (uwsgi_cache_del((char *) sess->session_id, sess->session_id_length, 0, 0)) {
|
||||
uwsgi_wlock(uwsgi.ssl_sessions_cache->lock);
|
||||
if (uwsgi_cache_del2(uwsgi.ssl_sessions_cache, (char *) sess->session_id, sess->session_id_length, 0, 0)) {
|
||||
if (uwsgi.ssl_verbose) {
|
||||
uwsgi_log("[uwsgi-ssl] error removing cache item\n");
|
||||
}
|
||||
}
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uwsgi.ssl_sessions_cache->lock);
|
||||
}
|
||||
|
||||
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
|
||||
@@ -221,12 +221,12 @@ SSL_CTX *uwsgi_ssl_new_server_context(char *name, char *crt, char *key, char *ci
|
||||
|
||||
if (uwsgi.ssl_sessions_use_cache) {
|
||||
|
||||
if (!uwsgi.cache_max_items) {
|
||||
if (!uwsgi.ssl_sessions_cache->max_items) {
|
||||
uwsgi_log("you have to enable uWSGI cache to use it as SSL session store !!!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (uwsgi.cache_blocksize < 4096) {
|
||||
if (uwsgi.ssl_sessions_cache->blocksize < 4096) {
|
||||
uwsgi_log("cache blocksize for SSL session store must be at least 4096 bytes\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
+7
-7
@@ -481,14 +481,14 @@ int uwsgi_file_serve(struct wsgi_request *wsgi_req, char *document_root, uint16_
|
||||
#endif
|
||||
|
||||
if (uwsgi.static_cache_paths) {
|
||||
uwsgi_rlock(uwsgi.cache_lock);
|
||||
char *item = uwsgi_cache_get(filename, filename_len, &real_filename_len);
|
||||
uwsgi_rlock(uwsgi.static_cache_paths->lock);
|
||||
char *item = uwsgi_cache_get2(uwsgi.static_cache_paths, filename, filename_len, &real_filename_len);
|
||||
if (item && real_filename_len > 0 && real_filename_len <= PATH_MAX) {
|
||||
memcpy(real_filename, item, real_filename_len);
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uwsgi.static_cache_paths->lock);
|
||||
goto found;
|
||||
}
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_rwunlock(uwsgi.static_cache_paths->lock);
|
||||
}
|
||||
|
||||
if (!realpath(filename, real_filename)) {
|
||||
@@ -501,9 +501,9 @@ int uwsgi_file_serve(struct wsgi_request *wsgi_req, char *document_root, uint16_
|
||||
real_filename_len = strlen(real_filename);
|
||||
|
||||
if (uwsgi.static_cache_paths) {
|
||||
uwsgi_wlock(uwsgi.cache_lock);
|
||||
uwsgi_cache_set(filename, filename_len, real_filename, real_filename_len, uwsgi.static_cache_paths, UWSGI_CACHE_FLAG_UPDATE);
|
||||
uwsgi_rwunlock(uwsgi.cache_lock);
|
||||
uwsgi_wlock(uwsgi.static_cache_paths->lock);
|
||||
uwsgi_cache_set2(uwsgi.static_cache_paths, filename, filename_len, real_filename, real_filename_len, uwsgi.use_static_cache_paths, UWSGI_CACHE_FLAG_UPDATE);
|
||||
uwsgi_rwunlock(uwsgi.static_cache_paths->lock);
|
||||
}
|
||||
|
||||
found:
|
||||
|
||||
+5
-2
@@ -503,6 +503,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"static-index", required_argument, 0, "search for specified file if a directory is requested", uwsgi_opt_add_string_list, &uwsgi.static_index, UWSGI_OPT_MIME},
|
||||
{"static-safe", required_argument, 0, "skip security checks if the file is under the specified path", uwsgi_opt_add_string_list, &uwsgi.static_safe, UWSGI_OPT_MIME},
|
||||
{"static-cache-paths", required_argument, 0, "put resolved paths in the uWSGI cache for the specified amount of seconds", uwsgi_opt_set_int, &uwsgi.static_cache_paths, UWSGI_OPT_MIME|UWSGI_OPT_MASTER},
|
||||
{"static-cache-name", required_argument, 0, "use the specified cache for static paths", uwsgi_opt_set_str, &uwsgi.static_cache_paths_name, UWSGI_OPT_MIME|UWSGI_OPT_MASTER},
|
||||
{"mimefile", required_argument, 0, "set mime types file path (default /etc/mime.types)", uwsgi_opt_add_string_list, &uwsgi.mime_file, UWSGI_OPT_MIME},
|
||||
{"mime-file", required_argument, 0, "set mime types file path (default /etc/mime.types)", uwsgi_opt_add_string_list, &uwsgi.mime_file, UWSGI_OPT_MIME},
|
||||
|
||||
@@ -2286,11 +2287,13 @@ int uwsgi_start(void *v_argv) {
|
||||
uwsgi_init_queue();
|
||||
}
|
||||
|
||||
// setup cache
|
||||
// setup default cache
|
||||
if (uwsgi.cache_max_items > 0) {
|
||||
uwsgi_init_cache();
|
||||
uwsgi_cache_create(NULL);
|
||||
}
|
||||
|
||||
// setup new generation caches
|
||||
|
||||
// create the cache server
|
||||
if (uwsgi.master_process && uwsgi.cache_server) {
|
||||
uwsgi.cache_server_fd = uwsgi_cache_server(uwsgi.cache_server, uwsgi.cache_server_threads);
|
||||
|
||||
@@ -293,11 +293,10 @@ extern int pivot_root(const char *new_root, const char *put_old);
|
||||
#include <zmq.h>
|
||||
#endif
|
||||
|
||||
#define UWSGI_CACHE_MAX_KEY_SIZE 2048
|
||||
#define UWSGI_CACHE_FLAG_UNGETTABLE 0x0001
|
||||
#define UWSGI_CACHE_FLAG_UPDATE 0x0002
|
||||
#define UWSGI_CACHE_FLAG_LOCAL 0x0004
|
||||
#define UWSGI_CACHE_FLAG_ABSEXPIRE 0x0008
|
||||
#define UWSGI_CACHE_FLAG_UNGETTABLE 0x01
|
||||
#define UWSGI_CACHE_FLAG_UPDATE 0x02
|
||||
#define UWSGI_CACHE_FLAG_LOCAL 0x04
|
||||
#define UWSGI_CACHE_FLAG_ABSEXPIRE 0x08
|
||||
|
||||
#define uwsgi_cache_update_start(x, y, z) uwsgi_cache_set(x, y, "", 0, CACHE_FLAG_UNGETTABLE)
|
||||
|
||||
@@ -591,14 +590,23 @@ struct uwsgi_queue_item {
|
||||
time_t ts;
|
||||
};
|
||||
|
||||
struct uwsgi_hash_algo {
|
||||
char *name;
|
||||
uint32_t (*func)(char *, uint64_t);
|
||||
struct uwsgi_hash_algo *next;
|
||||
};
|
||||
|
||||
struct uwsgi_hash_algo *uwsgi_hash_algo_get(char *);
|
||||
void uwsgi_hash_algo_register(char *, uint32_t (*)(char *, uint64_t));
|
||||
|
||||
// maintain alignment here !!!
|
||||
struct uwsgi_cache_item {
|
||||
// unused
|
||||
uint16_t flags;
|
||||
// item specific flags
|
||||
uint64_t flags;
|
||||
// size of the key
|
||||
uint16_t keysize;
|
||||
// djb hash of the key
|
||||
uint32_t djbhash;
|
||||
uint64_t keysize;
|
||||
// hash of the key
|
||||
uint64_t hash;
|
||||
// size of the value (64bit)
|
||||
uint64_t valsize;
|
||||
// 64bit expiration (0 for immortal)
|
||||
@@ -609,10 +617,49 @@ struct uwsgi_cache_item {
|
||||
uint64_t prev;
|
||||
// next same-hash item
|
||||
uint64_t next;
|
||||
// key chracters follows...
|
||||
char key[UWSGI_CACHE_MAX_KEY_SIZE];
|
||||
// key characters follows...
|
||||
char key[];
|
||||
} __attribute__ ((__packed__));
|
||||
|
||||
struct uwsgi_cache {
|
||||
char *name;
|
||||
uint16_t name_len;
|
||||
|
||||
uint64_t keysize;
|
||||
uint64_t blocks;
|
||||
uint64_t blocksize;
|
||||
|
||||
struct uwsgi_hash_algo *hash;
|
||||
uint64_t *hashtable;
|
||||
uint32_t hashsize;
|
||||
|
||||
uint64_t first_available_block;
|
||||
uint64_t *unused_blocks_stack;
|
||||
uint64_t unused_blocks_stack_ptr;
|
||||
uint8_t use_blocks_bitmap;
|
||||
uint8_t *blocks_bitmap;
|
||||
|
||||
uint64_t max_items;
|
||||
uint64_t n_items;
|
||||
struct uwsgi_cache_item *items;
|
||||
|
||||
void *data;
|
||||
|
||||
uint8_t no_expire;
|
||||
uint64_t full;
|
||||
uint64_t hits;
|
||||
uint64_t miss;
|
||||
|
||||
char *store;
|
||||
uint64_t filesize;
|
||||
|
||||
int thread_server_fd;
|
||||
|
||||
struct uwsgi_lock_item *lock;
|
||||
|
||||
struct uwsgi_cache *next;
|
||||
};
|
||||
|
||||
struct uwsgi_option {
|
||||
char *name;
|
||||
int type;
|
||||
@@ -1650,7 +1697,17 @@ struct uwsgi_server {
|
||||
struct uwsgi_string_list *static_index;
|
||||
struct uwsgi_string_list *static_safe;
|
||||
|
||||
int static_cache_paths;
|
||||
struct uwsgi_hash_algo *hash_algos;
|
||||
int use_static_cache_paths;
|
||||
char *static_cache_paths_name;
|
||||
struct uwsgi_cache *static_cache_paths;
|
||||
int cache_expire_freq;
|
||||
int cache_report_freed_items;
|
||||
int cache_no_expire;
|
||||
int cache_max_items;
|
||||
int cache_blocksize;
|
||||
char *cache_store;
|
||||
int cache_store_sync;
|
||||
|
||||
struct uwsgi_dyn_dict *static_expires_type;
|
||||
struct uwsgi_dyn_dict *static_expires_type_mtime;
|
||||
@@ -1962,19 +2019,7 @@ struct uwsgi_server {
|
||||
|
||||
struct uwsgi_string_list *load_file_in_cache;
|
||||
int check_cache;
|
||||
|
||||
uint32_t cache_max_items;
|
||||
uint64_t *cache_hashtable;
|
||||
uint64_t *cache_unused_stack;
|
||||
uint64_t cache_blocksize;
|
||||
struct uwsgi_cache_item *cache_items;
|
||||
void *cache;
|
||||
char *cache_store;
|
||||
size_t cache_filesize;
|
||||
int cache_store_sync;
|
||||
int cache_no_expire;
|
||||
int cache_expire_freq;
|
||||
int cache_report_freed_items;
|
||||
struct uwsgi_cache *caches;
|
||||
|
||||
struct uwsgi_string_list *cache_udp_server;
|
||||
struct uwsgi_string_list *cache_udp_node;
|
||||
@@ -2008,7 +2053,6 @@ struct uwsgi_server {
|
||||
|
||||
int locks;
|
||||
|
||||
struct uwsgi_lock_item *cache_lock;
|
||||
struct uwsgi_lock_item *queue_lock;
|
||||
struct uwsgi_lock_item **user_lock;
|
||||
struct uwsgi_lock_item *signal_table_lock;
|
||||
@@ -2037,6 +2081,7 @@ struct uwsgi_server {
|
||||
int ssl_verbose;
|
||||
int ssl_sessions_use_cache;
|
||||
int ssl_sessions_timeout;
|
||||
struct uwsgi_cache *ssl_sessions_cache;
|
||||
#ifdef UWSGI_PCRE
|
||||
struct uwsgi_regexp_list *sni_regexp;
|
||||
#endif
|
||||
@@ -2186,14 +2231,6 @@ struct uwsgi_shared {
|
||||
|
||||
#endif
|
||||
|
||||
uint64_t cache_first_available_item;
|
||||
uint64_t cache_unused_stack_ptr;
|
||||
uint64_t cache_hits;
|
||||
uint64_t cache_miss;
|
||||
uint64_t cache_items;
|
||||
uint64_t cache_full;
|
||||
|
||||
|
||||
int worker_signal_pipe[2];
|
||||
#ifdef UWSGI_SPOOLER
|
||||
int spooler_frequency;
|
||||
@@ -2587,10 +2624,20 @@ ssize_t uwsgi_send_message(int, uint8_t, uint8_t, char *, uint16_t, int, ssize_t
|
||||
|
||||
char *uwsgi_cluster_best_node(void);
|
||||
|
||||
int uwsgi_cache_set(char *, uint16_t, char *, uint64_t, uint64_t, uint16_t);
|
||||
int uwsgi_cache_del(char *, uint16_t, uint64_t, uint16_t);
|
||||
char *uwsgi_cache_get(char *, uint16_t, uint64_t *);
|
||||
uint32_t uwsgi_cache_exists(char *, uint16_t);
|
||||
int uwsgi_cache_set2(struct uwsgi_cache *, char *, uint16_t, char *, uint64_t, uint64_t, uint64_t);
|
||||
int uwsgi_cache_del2(struct uwsgi_cache *, char *, uint16_t, uint64_t, uint16_t);
|
||||
char *uwsgi_cache_get2(struct uwsgi_cache *, char *, uint16_t, uint64_t *);
|
||||
uint32_t uwsgi_cache_exists2(struct uwsgi_cache *, char *, uint16_t);
|
||||
void uwsgi_cache_create(char *);
|
||||
|
||||
#define uwsgi_cache_set(x1, x2, x3, x4, x5, x6) uwsgi_cache_set2(uwsgi.caches, x1, x2, x3, x4, x5, x6)
|
||||
#define uwsgi_cache_del(x1, x2, x3, x4) uwsgi_cache_del2(uwsgi.caches, x1, x2, x3, x4)
|
||||
#define uwsgi_cache_get(x1, x2, x3) uwsgi_cache_get2(uwsgi.caches, x1, x2, x3)
|
||||
#define uwsgi_cache_exists(x1, x2) uwsgi_cache_exists2(uwsgi.caches, x1, x2)
|
||||
|
||||
void uwsgi_cache_sync_all(void);
|
||||
void uwsgi_cache_start_sweepers(void);
|
||||
void uwsgi_cache_start_sync_servers(void);
|
||||
|
||||
|
||||
void *uwsgi_malloc(size_t);
|
||||
@@ -2693,7 +2740,6 @@ int is_a_number(char *);
|
||||
char *uwsgi_resolve_ip(char *);
|
||||
|
||||
void uwsgi_init_queue(void);
|
||||
void uwsgi_init_cache(void);
|
||||
char *uwsgi_queue_get(uint64_t, uint64_t *);
|
||||
char *uwsgi_queue_pull(uint64_t *);
|
||||
int uwsgi_queue_push(char *, uint64_t);
|
||||
@@ -2847,7 +2893,7 @@ int uwsgi_list_has_num(char *, int);
|
||||
|
||||
int uwsgi_list_has_str(char *, char *);
|
||||
|
||||
void uwsgi_cache_fix(void);
|
||||
void uwsgi_cache_fix(struct uwsgi_cache *);
|
||||
|
||||
struct uwsgi_async_request {
|
||||
|
||||
@@ -3098,6 +3144,7 @@ struct uwsgi_subscribe_slot {
|
||||
|
||||
void mule_send_msg(int, char *, size_t);
|
||||
|
||||
uint32_t djb33x_hash(char *, uint64_t);
|
||||
void create_signal_pipe(int *);
|
||||
struct uwsgi_subscribe_slot *uwsgi_get_subscribe_slot(struct uwsgi_subscribe_slot **, char *, uint16_t);
|
||||
struct uwsgi_subscribe_node *uwsgi_get_subscribe_node_by_name(struct uwsgi_subscribe_slot **, char *, uint16_t, char *, uint16_t);
|
||||
@@ -3427,7 +3474,6 @@ char *uwsgi_sha1_2n(char *, size_t, char *, size_t, char *);
|
||||
|
||||
void uwsgi_opt_ssa(char *, char *, void *);
|
||||
|
||||
uint32_t djb33x_hash(char *, int);
|
||||
int uwsgi_no_subscriptions(struct uwsgi_subscribe_slot **);
|
||||
void uwsgi_deadlock_check(pid_t);
|
||||
|
||||
@@ -3677,10 +3723,9 @@ int uwsgi_send_http_stats(int);
|
||||
ssize_t uwsgi_simple_request_read(struct wsgi_request *, char *, size_t);
|
||||
int uwsgi_plugin_modifier1(char *);
|
||||
|
||||
int uwsgi_cache_enabled(void);
|
||||
void uwsgi_cache_wlock(void);
|
||||
void uwsgi_cache_rlock(void);
|
||||
void uwsgi_cache_rwunlock(void);
|
||||
void uwsgi_cache_wlock(struct uwsgi_cache *);
|
||||
void uwsgi_cache_rlock(struct uwsgi_cache *);
|
||||
void uwsgi_cache_rwunlock(struct uwsgi_cache *);
|
||||
|
||||
void *cache_sweeper_loop(void *);
|
||||
void *cache_udp_server_loop(void *);
|
||||
|
||||
+1
-1
@@ -456,7 +456,7 @@ class uConf(object):
|
||||
'core/notify', 'core/mule', 'core/subscription', 'core/stats', 'core/sendfile',
|
||||
'core/offload', 'core/io', 'core/static', 'core/websockets', 'core/channels',
|
||||
'core/setup_utils', 'core/clock', 'core/init', 'core/buffer', 'core/writer',
|
||||
'core/plugins', 'core/lock', 'core/cache', 'core/daemons', 'core/errors',
|
||||
'core/plugins', 'core/lock', 'core/cache', 'core/daemons', 'core/errors', 'core/hash',
|
||||
'core/queue', 'core/event', 'core/signal', 'core/cluster', 'core/strings',
|
||||
'core/rpc', 'core/gateway', 'core/loop', 'lib/rbtree', 'core/rb_timers', 'core/uwsgi']
|
||||
# add protocols
|
||||
|
||||
Reference in New Issue
Block a user