kore/src/linux.c

309 lines
6.5 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/param.h>
#include <sys/epoll.h>
#include <sys/prctl.h>
#include <sys/sendfile.h>
#include <sched.h>
#include "kore.h"
#if defined(KORE_USE_PGSQL)
2014-07-03 22:14:46 +02:00
#include "pgsql.h"
#endif
#if defined(KORE_USE_TASKS)
2014-07-03 22:14:46 +02:00
#include "tasks.h"
#endif
static int efd = -1;
static u_int32_t event_count = 0;
static struct epoll_event *events = NULL;
void
2013-06-24 09:36:40 +02:00
kore_platform_init(void)
{
long n;
if ((n = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
kore_debug("could not get number of cpu's falling back to 1");
cpu_count = 1;
} else {
cpu_count = (u_int16_t)n;
}
}
void
2013-06-26 11:18:32 +02:00
kore_platform_worker_setcpu(struct kore_worker *kw)
{
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(kw->cpu, &cpuset);
if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset) == -1) {
kore_debug("kore_worker_setcpu(): %s", errno_s);
} else {
kore_debug("kore_worker_setcpu(): worker %d on cpu %d",
kw->id, kw->cpu);
}
}
void
2013-06-26 11:18:32 +02:00
kore_platform_event_init(void)
{
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
if (efd != -1)
close(efd);
if (events != NULL)
kore_free(events);
if ((efd = epoll_create(10000)) == -1)
fatal("epoll_create(): %s", errno_s);
event_count = worker_max_connections + nlisteners;
events = kore_calloc(event_count, sizeof(struct epoll_event));
}
void
2016-01-04 22:40:14 +01:00
kore_platform_event_cleanup(void)
{
if (efd != -1) {
close(efd);
efd = -1;
}
if (events != NULL) {
kore_free(events);
events = NULL;
}
}
int
2014-07-28 23:35:12 +02:00
kore_platform_event_wait(u_int64_t timer)
{
u_int32_t r;
struct connection *c;
struct listener *l;
u_int8_t type;
int n, i;
2014-07-28 23:35:12 +02:00
n = epoll_wait(efd, events, event_count, timer);
if (n == -1) {
if (errno == EINTR)
return (0);
fatal("epoll_wait(): %s", errno_s);
}
if (n > 0) {
kore_debug("main(): %d sockets available", n);
}
r = 0;
for (i = 0; i < n; i++) {
if (events[i].data.ptr == NULL)
fatal("events[%d].data.ptr == NULL", i);
type = *(u_int8_t *)events[i].data.ptr;
if (events[i].events & EPOLLERR ||
events[i].events & EPOLLHUP) {
switch (type) {
case KORE_TYPE_LISTENER:
fatal("failed on listener socket");
/* NOTREACHED */
#if defined(KORE_USE_PGSQL)
case KORE_TYPE_PGSQL_CONN:
kore_pgsql_handle(events[i].data.ptr, 1);
break;
#endif
#if defined(KORE_USE_TASKS)
case KORE_TYPE_TASK:
kore_task_handle(events[i].data.ptr, 1);
break;
#endif
default:
c = (struct connection *)events[i].data.ptr;
kore_connection_disconnect(c);
break;
}
continue;
}
switch (type) {
case KORE_TYPE_LISTENER:
l = (struct listener *)events[i].data.ptr;
while (worker_active_connections <
worker_max_connections) {
if (worker_accept_threshold != 0 &&
r >= worker_accept_threshold)
break;
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
if (!kore_connection_accept(l, &c))
break;
if (c == NULL)
break;
r++;
kore_platform_event_all(c->fd, c);
}
break;
case KORE_TYPE_CONNECTION:
c = (struct connection *)events[i].data.ptr;
if (events[i].events & EPOLLIN &&
!(c->flags & CONN_READ_BLOCK))
c->flags |= CONN_READ_POSSIBLE;
if (events[i].events & EPOLLOUT &&
!(c->flags & CONN_WRITE_BLOCK))
c->flags |= CONN_WRITE_POSSIBLE;
if (c->handle != NULL && !c->handle(c))
2013-06-26 11:18:32 +02:00
kore_connection_disconnect(c);
break;
#if defined(KORE_USE_PGSQL)
case KORE_TYPE_PGSQL_CONN:
kore_pgsql_handle(events[i].data.ptr, 0);
break;
#endif
#if defined(KORE_USE_TASKS)
case KORE_TYPE_TASK:
kore_task_handle(events[i].data.ptr, 0);
break;
#endif
default:
fatal("wrong type in event %d", type);
}
}
return (r);
}
void
kore_platform_event_all(int fd, void *c)
{
kore_platform_event_schedule(fd,
EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLET, 0, c);
}
void
2013-06-26 11:18:32 +02:00
kore_platform_event_schedule(int fd, int type, int flags, void *udata)
{
struct epoll_event evt;
kore_debug("kore_platform_event_schedule(%d, %d, %d, %p)",
2013-06-26 11:18:32 +02:00
fd, type, flags, udata);
evt.events = type;
evt.data.ptr = udata;
if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &evt) == -1) {
if (errno == EEXIST) {
if (epoll_ctl(efd, EPOLL_CTL_MOD, fd, &evt) == -1)
fatal("epoll_ctl() MOD: %s", errno_s);
} else {
fatal("epoll_ctl() ADD: %s", errno_s);
}
}
}
void
kore_platform_schedule_read(int fd, void *data)
{
kore_platform_event_schedule(fd, EPOLLIN | EPOLLET, 0, data);
}
2015-12-09 21:29:44 +01:00
void
kore_platform_schedule_write(int fd, void *data)
{
kore_platform_event_schedule(fd, EPOLLOUT | EPOLLET, 0, data);
2015-12-09 21:29:44 +01:00
}
void
kore_platform_disable_read(int fd)
{
if (epoll_ctl(efd, EPOLL_CTL_DEL, fd, NULL) == -1)
fatal("kore_platform_disable_read: %s", errno_s);
}
void
kore_platform_enable_accept(void)
{
struct listener *l;
kore_debug("kore_platform_enable_accept()");
LIST_FOREACH(l, &listeners, list)
kore_platform_event_schedule(l->fd, EPOLLIN, 0, l);
}
void
kore_platform_disable_accept(void)
{
struct listener *l;
kore_debug("kore_platform_disable_accept()");
LIST_FOREACH(l, &listeners, list) {
if (epoll_ctl(efd, EPOLL_CTL_DEL, l->fd, NULL) == -1)
fatal("kore_platform_disable_accept: %s", errno_s);
}
}
void
2013-06-26 11:18:32 +02:00
kore_platform_proctitle(char *title)
{
if (prctl(PR_SET_NAME, title) == -1) {
kore_debug("prctl(): %s", errno_s);
}
}
#if defined(KORE_USE_PLATFORM_SENDFILE)
int
kore_platform_sendfile(struct connection *c, struct netbuf *nb)
{
off_t smin;
ssize_t sent;
size_t len, prevoff;
prevoff = nb->fd_off;
smin = nb->fd_len - nb->fd_off;
len = MIN(SENDFILE_PAYLOAD_MAX, smin);
resend:
sent = sendfile(c->fd, nb->file_ref->fd, &nb->fd_off, len);
if (sent == -1) {
if (errno == EAGAIN) {
c->flags &= ~CONN_WRITE_POSSIBLE;
return (KORE_RESULT_OK);
}
return (KORE_RESULT_ERROR);
}
if (nb->fd_off - prevoff != (size_t)len)
goto resend;
if (sent == 0 || nb->fd_off == nb->fd_len) {
net_remove_netbuf(&(c->send_queue), nb);
c->snb = NULL;
}
return (KORE_RESULT_OK);
}
#endif