kore/src/pool.c

202 lines
4.6 KiB
C
Raw Normal View History

/*
2018-01-20 22:51:06 +01:00
* Copyright (c) 2013-2018 Joris Vink <joris@coders.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/mman.h>
#include <sys/queue.h>
#include <stdint.h>
#include "kore.h"
#define POOL_ELEMENT_BUSY 0
#define POOL_ELEMENT_FREE 1
#if defined(KORE_USE_TASKS)
static void pool_lock(struct kore_pool *);
static void pool_unlock(struct kore_pool *);
#endif
2016-07-12 14:01:02 +02:00
static void pool_region_create(struct kore_pool *, size_t);
static void pool_region_destroy(struct kore_pool *);
void
2014-08-04 12:40:21 +02:00
kore_pool_init(struct kore_pool *pool, const char *name,
2016-07-12 14:01:02 +02:00
size_t len, size_t elm)
{
2016-07-12 14:01:02 +02:00
kore_debug("kore_pool_init(%p, %s, %zu, %zu)", pool, name, len, elm);
if ((pool->name = strdup(name)) == NULL)
fatal("kore_pool_init: strdup %s", errno_s);
pool->lock = 0;
pool->elms = 0;
pool->inuse = 0;
pool->elen = len;
pool->growth = elm * 0.25f;
pool->slen = pool->elen + sizeof(struct kore_pool_entry);
LIST_INIT(&(pool->regions));
LIST_INIT(&(pool->freelist));
pool_region_create(pool, elm);
}
void
2016-01-04 22:40:14 +01:00
kore_pool_cleanup(struct kore_pool *pool)
{
pool->lock = 0;
pool->elms = 0;
pool->inuse = 0;
pool->elen = 0;
pool->slen = 0;
Rework HTTP and worker processes. The HTTP layer used to make a copy of each incoming header and its value for a request. Stop doing that and make HTTP headers zero-copy all across the board. This change comes with some api function changes, notably the http_request_header() function which now takes a const char ** rather than a char ** out pointer. This commit also constifies several members of http_request, beware. Additional rework how the worker processes deal with the accept lock. Before: if a worker held the accept lock and it accepted a new connection it would release the lock for others and back off for 500ms before attempting to grab the lock again. This approach worked but under high load this starts becoming obvious. Now: - workers not holding the accept lock and not having any connections will wait less long before returning from kore_platform_event_wait(). - workers not holding the accept lock will no longer blindly wait an arbitrary amount in kore_platform_event_wait() but will look at how long until the next lock grab is and base their timeout on that. - if a worker its next_lock timeout is up and failed to grab the lock it will try again in half the time again. - the worker process holding the lock will when releasing the lock double check if it still has space for newer connections, if it does it will keep the lock until it is full. This prevents the lock from bouncing between several non busy worker processes all the time. Additional fixes: - Reduce the number of times we check the timeout list, only do it twice per second rather then every event tick. - Fix solo worker count for TLS (we actually hold two processes, not one). - Make sure we don't accidentally miscalculate the idle time causing new connections under heavy load to instantly drop. - Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
free(pool->name);
pool->name = NULL;
pool_region_destroy(pool);
}
void *
kore_pool_get(struct kore_pool *pool)
{
u_int8_t *ptr;
struct kore_pool_entry *entry;
#if defined(KORE_USE_TASKS)
pool_lock(pool);
#endif
if (LIST_EMPTY(&(pool->freelist)))
pool_region_create(pool, pool->growth);
entry = LIST_FIRST(&(pool->freelist));
if (entry->state != POOL_ELEMENT_FREE)
2014-03-06 08:21:58 +01:00
fatal("%s: element %p was not free", pool->name, entry);
LIST_REMOVE(entry, list);
entry->state = POOL_ELEMENT_BUSY;
ptr = (u_int8_t *)entry + sizeof(struct kore_pool_entry);
pool->inuse++;
#if defined(KORE_USE_TASKS)
pool_unlock(pool);
#endif
return (ptr);
}
void
kore_pool_put(struct kore_pool *pool, void *ptr)
{
struct kore_pool_entry *entry;
#if defined(KORE_USE_TASKS)
pool_lock(pool);
#endif
entry = (struct kore_pool_entry *)
((u_int8_t *)ptr - sizeof(struct kore_pool_entry));
if (entry->state != POOL_ELEMENT_BUSY)
fatal("%s: element %p was not busy", pool->name, ptr);
entry->state = POOL_ELEMENT_FREE;
LIST_INSERT_HEAD(&(pool->freelist), entry, list);
pool->inuse--;
#if defined(KORE_USE_TASKS)
pool_unlock(pool);
#endif
}
static void
2016-07-12 14:01:02 +02:00
pool_region_create(struct kore_pool *pool, size_t elms)
{
2016-07-12 14:01:02 +02:00
size_t i;
u_int8_t *p;
struct kore_pool_region *reg;
struct kore_pool_entry *entry;
kore_debug("pool_region_create(%p, %zu)", pool, elms);
if ((reg = calloc(1, sizeof(struct kore_pool_region))) == NULL)
fatal("pool_region_create: calloc: %s", errno_s);
LIST_INSERT_HEAD(&(pool->regions), reg, list);
if (SIZE_MAX / elms < pool->slen)
fatal("pool_region_create: overflow");
reg->length = elms * pool->slen;
reg->start = mmap(NULL, reg->length, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
2018-04-04 09:34:18 +02:00
if (reg->start == MAP_FAILED)
fatal("mmap: %s", errno_s);
p = (u_int8_t *)reg->start;
for (i = 0; i < elms; i++) {
entry = (struct kore_pool_entry *)p;
entry->region = reg;
entry->state = POOL_ELEMENT_FREE;
LIST_INSERT_HEAD(&(pool->freelist), entry, list);
p = p + pool->slen;
}
pool->elms += elms;
}
static void
pool_region_destroy(struct kore_pool *pool)
{
struct kore_pool_region *reg;
kore_debug("pool_region_destroy(%p)", pool);
/* Take care iterating when modifying list contents */
while (!LIST_EMPTY(&pool->regions)) {
reg = LIST_FIRST(&pool->regions);
LIST_REMOVE(reg, list);
(void)munmap(reg->start, reg->length);
free(reg);
}
/* Freelist references into the regions memory allocations */
LIST_INIT(&pool->freelist);
pool->elms = 0;
}
#if defined(KORE_USE_TASKS)
static void
pool_lock(struct kore_pool *pool)
{
for (;;) {
if (__sync_bool_compare_and_swap(&pool->lock, 0, 1))
break;
}
}
static void
pool_unlock(struct kore_pool *pool)
{
if (!__sync_bool_compare_and_swap(&pool->lock, 1, 0))
fatal("pool_unlock: failed to release %s", pool->name);
}
#endif