Browse Source

Set cpu affinity

personal/stbuehler/wip
Stefan Bühler 14 years ago
parent
commit
14cb73e80b
  1. 2
      src/server.c
  2. 19
      src/worker.c
  3. 1
      src/worker.h

2
src/server.c

@ -188,6 +188,7 @@ gboolean server_loop_init(server *srv) {
if (srv->worker_count < 1) srv->worker_count = 1;
g_array_set_size(srv->workers, srv->worker_count);
srv->main_worker = g_array_index(srv->workers, worker*, 0) = worker_new(srv, loop);
srv->main_worker->ndx = 0;
for (i = 1; i < srv->worker_count; i++) {
GError *error = NULL;
worker *wrk;
@ -196,6 +197,7 @@ gboolean server_loop_init(server *srv) {
return FALSE;
}
wrk = g_array_index(srv->workers, worker*, i) = worker_new(srv, loop);
wrk->ndx = i;
if (NULL == (wrk->thread = g_thread_create(server_worker_cb, wrk, TRUE, &error))) {
g_error ( "g_thread_create failed: %s", error->message );
return FALSE;

19
src/worker.c

@ -1,4 +1,6 @@
#include <sched.h>
#include "base.h"
void con_put(connection *con);
@ -200,6 +202,23 @@ void worker_free(worker *wrk) {
}
void worker_run(worker *wrk) {
cpu_set_t mask;
if (0 != sched_getaffinity(0, sizeof(mask), &mask)) {
ERROR(wrk->srv, "couldn't get cpu affinity mask: %s", g_strerror(errno));
} else {
guint cpus = 0;
while (CPU_ISSET(cpus, &mask)) cpus++;
if (cpus) {
CPU_ZERO(&mask);
CPU_SET(wrk->ndx % cpus, &mask);
if (0 != sched_setaffinity(0, sizeof(mask), &mask)) {
ERROR(wrk->srv, "couldn't set cpu affinity mask: %s", g_strerror(errno));
}
} else {
ERROR(wrk->srv, "%s", "cpu 0 not enabled, no affinity set");
}
}
ev_loop(wrk->loop, 0);
}

1
src/worker.h

@ -20,6 +20,7 @@ struct worker {
struct server *srv;
GThread *thread; /* managed by server.c */
guint ndx; /* worker index */
struct ev_loop *loop;
ev_prepare loop_prepare;

Loading…
Cancel
Save