2
0
Fork 0
lighttpd2/src/main/stat_cache.c

388 lines
10 KiB
C
Raw Normal View History

2009-03-01 20:16:58 +00:00
#include <lighttpd/base.h>
2009-03-26 22:05:17 +00:00
#include <sys/stat.h>
#include <fcntl.h>
2009-03-01 20:16:58 +00:00
static void stat_cache_job_cb(struct ev_loop *loop, ev_async *w, int revents);
static void stat_cache_delete_cb(struct ev_loop *loop, ev_timer *w, int revents);
static gpointer stat_cache_thread(gpointer data);
static void stat_cache_entry_free(liStatCacheEntry *sce);
void li_stat_cache_new(liWorker *wrk, gdouble ttl) {
liStatCache *sc;
2009-03-01 20:16:58 +00:00
GError *err;
if (ttl < 0) {
/* fall back to default if not sane */
ttl = 10.0;
} else if (ttl == 0) {
/* ttl means disabled stat cache */
return;
}
sc = g_slice_new0(liStatCache);
2009-03-01 20:16:58 +00:00
sc->ttl = ttl;
sc->entries = g_hash_table_new_full((GHashFunc)g_string_hash, (GEqualFunc)g_string_equal, NULL, NULL);
2009-03-26 22:05:17 +00:00
sc->dirlists = g_hash_table_new_full((GHashFunc)g_string_hash, (GEqualFunc)g_string_equal, NULL, NULL);
2009-03-01 20:16:58 +00:00
sc->job_queue_in = g_async_queue_new();
sc->job_queue_out = g_async_queue_new();
2009-07-09 20:17:24 +00:00
li_waitqueue_init(&sc->delete_queue, wrk->loop, stat_cache_delete_cb, ttl, sc);
2009-03-01 20:16:58 +00:00
ev_init(&sc->job_watcher, stat_cache_job_cb);
sc->job_watcher.data = wrk;
ev_async_start(wrk->loop, &sc->job_watcher);
ev_unref(wrk->loop); /* this watcher shouldn't keep the loop alive */
wrk->stat_cache = sc;
sc->thread = g_thread_create(stat_cache_thread, sc, TRUE, &err);
if (!sc->thread) {
/* failed to create thread */
assert(0);
}
}
void li_stat_cache_free(liStatCache *sc) {
2009-10-05 17:41:48 +00:00
liStatCacheEntry *sce;
liWaitQueueElem *wqe;
2009-03-01 20:16:58 +00:00
/* check if stat cache was enabled */
if (!sc)
return;
2009-03-01 20:16:58 +00:00
/* wake up thread */
2009-10-05 17:41:48 +00:00
sce = g_slice_new0(liStatCacheEntry);
g_async_queue_push(sc->job_queue_out, sce);
2009-03-01 20:16:58 +00:00
g_thread_join(sc->thread);
2009-10-05 17:41:48 +00:00
g_slice_free(liStatCacheEntry, sce);
2009-03-01 20:16:58 +00:00
2009-10-05 17:41:48 +00:00
li_waitqueue_stop(&sc->delete_queue);
while (NULL != (wqe = li_waitqueue_pop_force(&sc->delete_queue))) {
sce = wqe->data;
if (sce->cached) {
if (sce->type == STAT_CACHE_ENTRY_SINGLE)
g_hash_table_remove(sc->entries, sce->data.path);
else
g_hash_table_remove(sc->dirlists, sce->data.path);
sce->cached = FALSE;
}
stat_cache_entry_free(sce);
2009-03-01 20:16:58 +00:00
}
2009-10-05 17:41:48 +00:00
li_ev_safe_ref_and_stop(ev_async_stop, sc->delete_queue.loop, &sc->job_watcher);
2009-03-01 20:16:58 +00:00
g_async_queue_unref(sc->job_queue_in);
g_async_queue_unref(sc->job_queue_out);
g_hash_table_destroy(sc->entries);
2009-03-26 22:05:17 +00:00
g_hash_table_destroy(sc->dirlists);
g_slice_free(liStatCache, sc);
2009-03-01 20:16:58 +00:00
}
static void stat_cache_delete_cb(struct ev_loop *loop, ev_timer *w, int revents) {
liStatCache *sc = (liStatCache*) w->data;
liStatCacheEntry *sce;
liWaitQueueElem *wqe;
2009-03-01 20:16:58 +00:00
UNUSED(loop);
UNUSED(revents);
2009-07-09 20:17:24 +00:00
while ((wqe = li_waitqueue_pop(&sc->delete_queue)) != NULL) {
2009-03-01 20:16:58 +00:00
/* stat cache entry TTL over */
sce = wqe->data;
2009-03-26 22:05:17 +00:00
if (sce->cached) {
if (sce->type == STAT_CACHE_ENTRY_SINGLE)
g_hash_table_remove(sc->entries, sce->data.path);
else
g_hash_table_remove(sc->dirlists, sce->data.path);
sce->cached = FALSE;
}
2009-03-01 20:16:58 +00:00
if (sce->refcount) {
/* if there are still vrequests using this entry just requeue it */
2009-07-09 20:17:24 +00:00
li_waitqueue_push(&sc->delete_queue, wqe);
2009-03-01 20:16:58 +00:00
} else {
/* no more vrequests using this entry, finally free it */
stat_cache_entry_free(sce);
}
}
2009-07-09 20:17:24 +00:00
li_waitqueue_update(&sc->delete_queue);
2009-03-01 20:16:58 +00:00
}
2009-03-26 22:05:17 +00:00
/* called whenever an async stat job has finished */
static void stat_cache_job_cb(struct ev_loop *loop, ev_async *w, int revents) {
2009-03-01 20:16:58 +00:00
guint i;
liStatCacheEntry *sce;
liStatCache *sc = ((liWorker*)w->data)->stat_cache;
liVRequest *vr;
2009-03-01 20:16:58 +00:00
UNUSED(loop);
UNUSED(revents);
while ((sce = g_async_queue_try_pop(sc->job_queue_in)) != NULL) {
if (sce->data.failed)
2009-03-01 20:16:58 +00:00
sc->errors++;
2009-03-26 22:05:17 +00:00
/* queue pending vrequests */
2009-03-01 20:16:58 +00:00
for (i = 0; i < sce->vrequests->len; i++) {
vr = g_ptr_array_index(sce->vrequests, i);
2009-07-09 20:17:24 +00:00
li_vrequest_joblist_append(vr);
2009-03-01 20:16:58 +00:00
}
g_ptr_array_set_size(sce->vrequests, 0);
2009-03-26 22:05:17 +00:00
sce->refcount--;
2009-03-01 20:16:58 +00:00
}
}
static void stat_cache_entry_free(liStatCacheEntry *sce) {
guint i;
2009-03-01 20:16:58 +00:00
assert(sce->vrequests->len == 0);
assert(sce->refcount == 0);
g_string_free(sce->data.path, TRUE);
2009-03-01 20:16:58 +00:00
g_ptr_array_free(sce->vrequests, TRUE);
if (NULL != sce->dirlist) {
for (i = 0; i < sce->dirlist->len; i++) {
g_string_free(g_array_index(sce->dirlist, liStatCacheEntryData, i).path, TRUE);
}
g_array_free(sce->dirlist, TRUE);
}
g_slice_free(liStatCacheEntry, sce);
2009-03-01 20:16:58 +00:00
}
static gpointer stat_cache_thread(gpointer data) {
liStatCache *sc = data;
liStatCacheEntry *sce;
while (TRUE) {
sce = g_async_queue_pop(sc->job_queue_out);
/* stat cache entry with path == NULL indicates server stop */
if (!sce->data.path)
break;
2009-03-17 10:45:44 +00:00
if (stat(sce->data.path->str, &sce->data.st) == -1) {
sce->data.failed = TRUE;
sce->data.err = errno;
} else {
2009-03-17 10:45:44 +00:00
sce->data.failed = FALSE;
}
if (!sce->data.failed && sce->type == STAT_CACHE_ENTRY_DIR) {
/* dirlisting */
DIR *dirp;
gsize size;
struct dirent *entry;
struct dirent *result;
gint error;
liStatCacheEntryData sced;
2009-03-06 16:22:14 +00:00
GString *str;
dirp = opendir(sce->data.path->str);
if (dirp == NULL) {
sce->data.failed = TRUE;
sce->data.err = errno;
} else {
2009-07-09 20:17:24 +00:00
size = li_dirent_buf_size(dirp);
assert(size != (gsize)-1);
entry = g_slice_alloc(size);
sce->dirlist = g_array_sized_new(FALSE, FALSE, sizeof(liStatCacheEntryData), 32);
str = g_string_sized_new(sce->data.path->len + 64);
2009-03-06 16:22:14 +00:00
g_string_append_len(str, GSTR_LEN(sce->data.path));
2009-03-06 16:22:14 +00:00
while ((error = readdir_r(dirp, entry, &result)) == 0 && result != NULL) {
/* hide "." and ".." */
if (result->d_name[0] == '.' && (result->d_name[1] == '\0' ||
(result->d_name[1] == '.' && result->d_name[2] == '\0'))) {
continue;
}
sced.path = g_string_sized_new(63);
g_string_assign(sced.path, result->d_name);
2009-03-06 16:22:14 +00:00
g_string_truncate(str, sce->data.path->len);
/* make sure the path ends with / (or whatever) */
2009-03-26 22:05:17 +00:00
if (!sce->data.path->len || sce->data.path->str[sce->data.path->len-1] != G_DIR_SEPARATOR)
2009-03-06 16:22:14 +00:00
g_string_append_c(str, G_DIR_SEPARATOR);
g_string_append_len(str, GSTR_LEN(sced.path));
if (stat(str->str, &sced.st) == -1) {
sced.failed = TRUE;
sced.err = errno;
} else {
sced.failed = FALSE;
}
2009-03-06 16:22:14 +00:00
g_array_append_val(sce->dirlist, sced);
}
if (error) {
sce->data.failed = TRUE;
sce->data.err = error;
}
2009-03-06 16:22:14 +00:00
g_string_free(str, TRUE);
g_slice_free1(size, entry);
closedir(dirp);
}
}
g_atomic_int_set(&sce->state, STAT_CACHE_ENTRY_FINISHED);
g_async_queue_push(sc->job_queue_in, sce);
ev_async_send(sc->delete_queue.loop, &sc->job_watcher);
}
return NULL;
}
static liStatCacheEntry *stat_cache_entry_new(GString *path) {
liStatCacheEntry *sce;
2009-03-26 22:05:17 +00:00
sce = g_slice_new0(liStatCacheEntry);
2009-03-26 22:05:17 +00:00
sce->data.path = g_string_new_len(GSTR_LEN(path));
sce->vrequests = g_ptr_array_sized_new(8);
sce->state = STAT_CACHE_ENTRY_WAITING;
sce->queue_elem.data = sce;
sce->refcount = 1;
sce->cached = TRUE;
return sce;
}
2009-07-09 20:17:24 +00:00
liHandlerResult li_stat_cache_get_dirlist(liVRequest *vr, GString *path, liStatCacheEntry **result) {
liStatCache *sc;
liStatCacheEntry *sce;
2009-03-26 22:05:17 +00:00
guint i;
2009-03-01 20:16:58 +00:00
sc = vr->wrk->stat_cache;
2009-03-26 22:05:17 +00:00
sce = g_hash_table_lookup(sc->dirlists, path);
2009-03-01 20:16:58 +00:00
if (sce) {
/* cache hit, check state */
2009-03-26 22:05:17 +00:00
if (g_atomic_int_get(&sce->state) == STAT_CACHE_ENTRY_WAITING) {
/* already waiting for it? */
for (i = 0; i < vr->stat_cache_entries->len; i++) {
if (g_ptr_array_index(vr->stat_cache_entries, i) == sce)
return LI_HANDLER_WAIT_FOR_EVENT;
2009-03-01 20:16:58 +00:00
}
2009-07-09 20:17:24 +00:00
li_stat_cache_entry_acquire(vr, sce);
return LI_HANDLER_WAIT_FOR_EVENT;
2009-03-01 20:16:58 +00:00
}
2009-03-26 22:05:17 +00:00
sc->hits++;
*result = sce;
2009-10-05 17:41:48 +00:00
for (i = 0; i < vr->stat_cache_entries->len; i++) {
if (g_ptr_array_index(vr->stat_cache_entries, i) == sce)
return LI_HANDLER_GO_ON;
}
li_stat_cache_entry_acquire(vr, sce);
return LI_HANDLER_GO_ON;
2009-03-01 20:16:58 +00:00
} else {
/* cache miss, allocate new entry */
2009-03-26 22:05:17 +00:00
sce = stat_cache_entry_new(path);
sce->type = STAT_CACHE_ENTRY_DIR;
2009-07-09 20:17:24 +00:00
li_stat_cache_entry_acquire(vr, sce);
li_waitqueue_push(&sc->delete_queue, &sce->queue_elem);
2009-03-26 22:05:17 +00:00
g_hash_table_insert(sc->dirlists, sce->data.path, sce);
g_async_queue_push(sc->job_queue_out, sce);
2009-03-01 20:16:58 +00:00
sc->misses++;
return LI_HANDLER_WAIT_FOR_EVENT;
2009-03-26 22:05:17 +00:00
}
}
static liHandlerResult stat_cache_get(liVRequest *vr, GString *path, struct stat *st, int *err, int *fd, gboolean async) {
liStatCache *sc;
liStatCacheEntry *sce;
2009-03-26 22:05:17 +00:00
guint i;
/* force blocking call if we are not in a vrequest context or stat cache is disabled */
if (!vr || !(sc = vr->wrk->stat_cache))
async = FALSE;
2009-03-26 22:05:17 +00:00
if (async) {
sce = g_hash_table_lookup(sc->entries, path);
if (sce) {
/* cache hit, check state */
if (g_atomic_int_get(&sce->state) == STAT_CACHE_ENTRY_WAITING) {
/* already waiting for it? */
for (i = 0; i < vr->stat_cache_entries->len; i++) {
if (g_ptr_array_index(vr->stat_cache_entries, i) == sce) {
return LI_HANDLER_WAIT_FOR_EVENT;
}
}
li_stat_cache_entry_acquire(vr, sce);
return LI_HANDLER_WAIT_FOR_EVENT;
2009-03-26 22:05:17 +00:00
}
sc->hits++;
} else {
/* cache miss, allocate new entry */
sce = stat_cache_entry_new(path);
sce->type = STAT_CACHE_ENTRY_SINGLE;
2009-07-09 20:17:24 +00:00
li_stat_cache_entry_acquire(vr, sce);
li_waitqueue_push(&sc->delete_queue, &sce->queue_elem);
g_hash_table_insert(sc->entries, sce->data.path, sce);
g_async_queue_push(sc->job_queue_out, sce);
sc->misses++;
return LI_HANDLER_WAIT_FOR_EVENT;
}
2009-03-26 22:05:17 +00:00
}
2009-03-06 23:08:07 +00:00
2009-03-26 22:05:17 +00:00
if (fd) {
/* open + fstat */
while (-1 == (*fd = open(path->str, O_RDONLY))) {
if (errno == EINTR)
continue;
2009-03-26 22:05:17 +00:00
*err = errno;
return LI_HANDLER_ERROR;
2009-03-26 22:05:17 +00:00
}
if (-1 == fstat(*fd, st)) {
*err = errno;
2009-11-15 20:48:00 +00:00
close(*fd);
*fd = -1;
return LI_HANDLER_ERROR;
2009-03-26 22:05:17 +00:00
}
} else {
/* stat */
if (-1 == stat(path->str, st)) {
*err = errno;
return LI_HANDLER_ERROR;
2009-03-26 22:05:17 +00:00
}
2009-03-01 20:16:58 +00:00
}
return LI_HANDLER_GO_ON;
2009-03-01 20:16:58 +00:00
}
liHandlerResult li_stat_cache_get(liVRequest *vr, GString *path, struct stat *st, int *err, int *fd) {
2010-03-03 10:51:49 +00:00
return stat_cache_get(vr, path, st, err, fd, TRUE);
}
/* doesn't return HANDLER_WAIT_FOR_EVENT, blocks instead of async lookup */
liHandlerResult li_stat_cache_get_sync(liVRequest *vr, GString *path, struct stat *st, int *err, int *fd) {
2010-03-03 10:51:49 +00:00
return stat_cache_get(vr, path, st, err, fd, FALSE);
}
2009-07-09 20:17:24 +00:00
void li_stat_cache_entry_acquire(liVRequest *vr, liStatCacheEntry *sce) {
2009-03-26 22:05:17 +00:00
sce->refcount++;
g_ptr_array_add(vr->stat_cache_entries, sce);
g_ptr_array_add(sce->vrequests, vr);
}
2009-03-01 20:16:58 +00:00
2009-07-09 20:17:24 +00:00
void li_stat_cache_entry_release(liVRequest *vr, liStatCacheEntry *sce) {
2009-03-26 22:05:17 +00:00
sce->refcount--;
g_ptr_array_remove_fast(sce->vrequests, vr);
g_ptr_array_remove_fast(vr->stat_cache_entries, sce);
2009-03-17 10:45:44 +00:00
}