Fixed ridiculous CPU usage
This commit is contained in:
@@ -32,36 +32,28 @@
|
|||||||
#include <responses.h>
|
#include <responses.h>
|
||||||
#include <connections.h>
|
#include <connections.h>
|
||||||
|
|
||||||
Connection *newConnection(int fd) {
|
int newConnection(int fd, Connection *ret) {
|
||||||
Connection *ret = malloc(sizeof(Connection));
|
|
||||||
if (ret == NULL)
|
|
||||||
return NULL;
|
|
||||||
ret->fd = fd;
|
ret->fd = fd;
|
||||||
ret->progress = RECEIVE_REQUEST;
|
ret->progress = RECEIVE_REQUEST;
|
||||||
|
|
||||||
ret->currLineAlloc = 30;
|
ret->currLineAlloc = 30;
|
||||||
ret->currLineLen = 0;
|
ret->currLineLen = 0;
|
||||||
ret->currLine = malloc(ret->currLineAlloc);
|
ret->currLine = malloc(ret->currLineAlloc);
|
||||||
if (ret->currLine == NULL) {
|
if (ret->currLine == NULL)
|
||||||
free(ret);
|
return 1;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret->allocatedFields = 10;
|
ret->allocatedFields = 10;
|
||||||
ret->fields = malloc(sizeof(Field) * ret->allocatedFields);
|
ret->fields = malloc(sizeof(Field) * ret->allocatedFields);
|
||||||
if (ret->fields == NULL) {
|
if (ret->fields == NULL) {
|
||||||
free(ret->currLine);
|
free(ret->currLine);
|
||||||
free(ret);
|
return 1;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret->path = NULL;
|
ret->path = NULL;
|
||||||
ret->body = NULL;
|
ret->body = NULL;
|
||||||
//pointers to things that are allocated within functions should be
|
//pointers to things that are allocated within functions should be
|
||||||
//initialized to NULL so that free() doens't fail.
|
//initialized to NULL so that free() doens't fail.
|
||||||
|
return 0;
|
||||||
ret->next = NULL;
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetConnection(Connection *conn) {
|
void resetConnection(Connection *conn) {
|
||||||
@@ -79,7 +71,6 @@ void freeConnection(Connection *conn) {
|
|||||||
free(conn->path);
|
free(conn->path);
|
||||||
free(conn->fields);
|
free(conn->fields);
|
||||||
free(conn->body);
|
free(conn->body);
|
||||||
free(conn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int processRequest(Connection *conn) {
|
static int processRequest(Connection *conn) {
|
||||||
|
|||||||
@@ -50,8 +50,6 @@ typedef struct Connection {
|
|||||||
size_t bodylen;
|
size_t bodylen;
|
||||||
size_t receivedBody;
|
size_t receivedBody;
|
||||||
|
|
||||||
struct Connection *next;
|
|
||||||
|
|
||||||
char *currLine;
|
char *currLine;
|
||||||
//persistent
|
//persistent
|
||||||
size_t currLineAlloc;
|
size_t currLineAlloc;
|
||||||
@@ -62,7 +60,7 @@ typedef struct Connection {
|
|||||||
//Ephemeral fields: Things which are freed and reallocated after each new
|
//Ephemeral fields: Things which are freed and reallocated after each new
|
||||||
//request, path, body
|
//request, path, body
|
||||||
|
|
||||||
Connection *newConnection(int fd);
|
int newConnection(int fd, Connection *ret);
|
||||||
//returns non-zero on error. creates a new connection bound to fd
|
//returns non-zero on error. creates a new connection bound to fd
|
||||||
void resetConnection(Connection *conn);
|
void resetConnection(Connection *conn);
|
||||||
void freeConnection(Connection *conn);
|
void freeConnection(Connection *conn);
|
||||||
|
|||||||
@@ -27,11 +27,10 @@ typedef struct {
|
|||||||
int *pending;
|
int *pending;
|
||||||
//pending[thread id] = the number of connections being handled by that
|
//pending[thread id] = the number of connections being handled by that
|
||||||
// thread
|
// thread
|
||||||
int *schedule;
|
int notify;
|
||||||
/*
|
/*
|
||||||
* schedule[0] = the thread that should take the next connection (-1 if
|
* When this runner should accept a connection, notify will contain an
|
||||||
* there is no connection).
|
* int ready to be read.
|
||||||
* schedule[1] = the fd of the connection to be accepted
|
|
||||||
*/
|
*/
|
||||||
int id;
|
int id;
|
||||||
} RunnerArgs;
|
} RunnerArgs;
|
||||||
|
|||||||
42
src/main.c
42
src/main.c
@@ -103,21 +103,20 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int *pending = calloc(processes - 1, sizeof(int));
|
int *pending = calloc(processes - 1, sizeof(int));
|
||||||
int *schedule = malloc(2 * sizeof(int));
|
int (*notify)[2] = malloc(sizeof(int[2]) * (processes - 1));
|
||||||
if (schedule == NULL)
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
schedule[0] = -1;
|
|
||||||
pthread_t *threads = malloc(sizeof(pthread_t) * (processes - 1));
|
pthread_t *threads = malloc(sizeof(pthread_t) * (processes - 1));
|
||||||
if (threads == NULL)
|
if (threads == NULL)
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|
||||||
for (int i = 0; i < processes - 1; i++) {
|
for (int i = 0; i < processes - 1; i++) {
|
||||||
|
if (pipe(notify[i]))
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
RunnerArgs *args = malloc(sizeof(RunnerArgs));
|
RunnerArgs *args = malloc(sizeof(RunnerArgs));
|
||||||
if (args == NULL)
|
if (args == NULL)
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
args->site = site;
|
args->site = site;
|
||||||
args->pending = pending;
|
args->pending = pending;
|
||||||
args->schedule = schedule;
|
args->notify = notify[i][0];
|
||||||
args->id = i;
|
args->id = i;
|
||||||
pthread_create(threads + i, NULL,
|
pthread_create(threads + i, NULL,
|
||||||
(void*(*)(void*)) runServer, args);
|
(void*(*)(void*)) runServer, args);
|
||||||
@@ -126,24 +125,23 @@ int main(int argc, char **argv) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
fsync(fd);
|
fsync(fd);
|
||||||
//TODO: Find out why this works
|
//TODO: Find out why this works
|
||||||
if (schedule[0] == -1) {
|
int newfd = accept(fd, (struct sockaddr *) &addr,
|
||||||
int newfd = accept(fd, (struct sockaddr *) &addr,
|
&addrlen);
|
||||||
&addrlen);
|
if (newfd < 0)
|
||||||
if (newfd < 0)
|
exit(EXIT_FAILURE);
|
||||||
exit(EXIT_FAILURE);
|
int flags = fcntl(newfd, F_GETFL);
|
||||||
int flags = fcntl(newfd, F_GETFL);
|
if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK))
|
||||||
if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK))
|
exit(EXIT_FAILURE);
|
||||||
exit(EXIT_FAILURE);
|
int lowestThread = 0;
|
||||||
int lowestThread = 0;
|
int lowestCount = pending[0];
|
||||||
int lowestCount = pending[0];
|
for (int i = 1; i < processes - 1; i++) {
|
||||||
for (int i = 1; i < processes - 1; i++) {
|
if (pending[i] < lowestCount) {
|
||||||
if (pending[i] < lowestCount) {
|
lowestThread = i;
|
||||||
lowestThread = i;
|
lowestCount = pending[i];
|
||||||
lowestCount = pending[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
schedule[1] = newfd;
|
|
||||||
schedule[0] = lowestThread;
|
|
||||||
}
|
}
|
||||||
|
if (write(notify[lowestThread][1], &newfd, sizeof(newfd))
|
||||||
|
< sizeof(newfd))
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
80
src/runner.c
80
src/runner.c
@@ -22,6 +22,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <poll.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@@ -33,49 +34,56 @@
|
|||||||
void *runServer(RunnerArgs *args) {
|
void *runServer(RunnerArgs *args) {
|
||||||
Sitefile *site = args->site;
|
Sitefile *site = args->site;
|
||||||
int *pending = args->pending;
|
int *pending = args->pending;
|
||||||
int *schedule = args->schedule;
|
int notify = args->notify;
|
||||||
int id = args->id;
|
int id = args->id;
|
||||||
|
|
||||||
Connection *connections = NULL;
|
int allocConns = 100;
|
||||||
Connection *last = NULL;
|
struct pollfd *fds = malloc(sizeof(struct pollfd) * allocConns);
|
||||||
//Connections are processed in a queue, which is really just a linked
|
Connection *connections = malloc(sizeof(Connection) * allocConns);
|
||||||
//list where we add to the end and read from the beginning.
|
assert(fds != NULL);
|
||||||
|
assert(connections != NULL);
|
||||||
|
fds[0].fd = notify;
|
||||||
|
fds[0].events = POLLIN;
|
||||||
|
int connCount = 1;
|
||||||
|
//connections are 1 indexed, this is because fds[0] is the notify fd.
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (schedule[0] == id) {
|
poll(fds, connCount, -1);
|
||||||
Connection *newconn = newConnection(schedule[1]);
|
|
||||||
assert(newconn != NULL);
|
|
||||||
|
|
||||||
if (last == NULL)
|
for (int i = 1; i < connCount; i++) {
|
||||||
connections = newconn;
|
if (updateConnection(connections + i, site)) {
|
||||||
else
|
connCount--;
|
||||||
last->next = newconn;
|
memcpy(fds + i, fds + connCount,
|
||||||
last = newconn;
|
sizeof(struct pollfd));
|
||||||
pending[id]++;
|
|
||||||
schedule[0] = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Connection *prev = NULL;
|
|
||||||
Connection *iter = connections;
|
|
||||||
//I know of the Linus Thorvalds good taste code thing, it just
|
|
||||||
//gets very confusing very fast to think about pointers to
|
|
||||||
//pointers which have pointers.
|
|
||||||
while (iter != NULL) {
|
|
||||||
if (updateConnection(iter, site)) {
|
|
||||||
if (iter == last)
|
|
||||||
last = prev;
|
|
||||||
Connection *old = iter;
|
|
||||||
iter = iter->next;
|
|
||||||
freeConnection(old);
|
|
||||||
if (prev == NULL)
|
|
||||||
connections = iter;
|
|
||||||
else
|
|
||||||
prev->next = iter;
|
|
||||||
pending[id]--;
|
pending[id]--;
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
prev = iter;
|
|
||||||
iter = iter->next;
|
if (fds[0].revents == POLLIN) {
|
||||||
|
if (connCount >= allocConns) {
|
||||||
|
allocConns *= 2;
|
||||||
|
struct pollfd *newfds = realloc(fds,
|
||||||
|
sizeof(struct pollfd) * allocConns);
|
||||||
|
if (newfds == NULL)
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
fds = newfds;
|
||||||
|
|
||||||
|
Connection *newconns = realloc(connections,
|
||||||
|
sizeof(Connection) * allocConns);
|
||||||
|
if (newconns == NULL)
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
connections = newconns;
|
||||||
}
|
}
|
||||||
|
int newfd;
|
||||||
|
if (read(notify, &newfd, sizeof(newfd)) < sizeof(newfd))
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
fds[connCount].fd = newfd;
|
||||||
|
fds[connCount].events = POLLIN;
|
||||||
|
|
||||||
|
if (newConnection(newfd, connections + connCount))
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
connCount++;
|
||||||
|
pending[id]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user