Skip to content

Commit

Permalink
preliminary cpu-affinity support
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto@natty32 committed Mar 11, 2011
1 parent 64249ab commit df4f610
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
27 changes: 27 additions & 0 deletions uwsgi.c
Expand Up @@ -95,6 +95,7 @@ static struct option long_base_options[] = {
{"limit-post", required_argument, 0, LONG_ARGS_LIMIT_POST},
{"no-orphans", no_argument, &uwsgi.no_orphans, 1},
{"prio", required_argument, 0, LONG_ARGS_PRIO},
{"cpu-affinity", required_argument, 0, LONG_ARGS_CPU_AFFINITY},
{"post-buffering", required_argument, 0, LONG_ARGS_POST_BUFFERING},
{"post-buffering-bufsize", required_argument, 0, LONG_ARGS_POST_BUFFERING_SIZE},
{"upload-progress", required_argument, 0, LONG_ARGS_UPLOAD_PROGRESS},
Expand Down Expand Up @@ -1865,6 +1866,29 @@ uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping; //100
//from now on the process is a real worker
}

if (uwsgi.cpu_affinity) {
#ifdef __linux__
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
int ncpu = sysconf(_SC_NPROCESSORS_ONLN);
int base_cpu = (uwsgi.mywid-1)*uwsgi.cpu_affinity;
if (base_cpu >= ncpu) {
base_cpu = base_cpu % ncpu;
}
uwsgi_log("set cpu affinity for worker %d to", uwsgi.mywid);
for(i=0;i<uwsgi.cpu_affinity;i++) {
if (base_cpu >= ncpu) base_cpu = 0 ;
CPU_SET(base_cpu, &cpuset);
uwsgi_log(" %d", base_cpu);
base_cpu++;
}
if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset)) {
uwsgi_error("sched_setaffinity()");
}
uwsgi_log("\n");
#endif
}

if (uwsgi.worker_exec) {
char *w_argv[2];
w_argv[0] = uwsgi.worker_exec;
Expand Down Expand Up @@ -2244,6 +2268,9 @@ uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping; //100
case LONG_ARGS_PRIO:
uwsgi.prio = (int) strtol(optarg, NULL, 10);
return 1;
case LONG_ARGS_CPU_AFFINITY:
uwsgi.cpu_affinity = (int) strtol(optarg, NULL, 10);
return 1;
case LONG_ARGS_POST_BUFFERING:
uwsgi.post_buffering = atoi(optarg);
return 1;
Expand Down
5 changes: 5 additions & 0 deletions uwsgi.h
Expand Up @@ -98,6 +98,8 @@


#ifdef __linux
#define __USE_GNU
#include <sched.h>
#include <sys/prctl.h>
#include <linux/limits.h>
#include <sys/mount.h>
Expand Down Expand Up @@ -359,6 +361,7 @@ struct uwsgi_opt {
#define LONG_ARGS_RELOAD_MERCY 17080
#define LONG_ARGS_ALLOWED_MODIFIERS 17081
#define LONG_ARGS_LINUX_NS_NET 17082
#define LONG_ARGS_CPU_AFFINITY 17083


#define UWSGI_OK 0
Expand Down Expand Up @@ -718,6 +721,8 @@ struct uwsgi_server {

time_t master_mercy;

int cpu_affinity;

int reload_mercy;
int option_index;
struct option *long_options;
Expand Down

0 comments on commit df4f610

Please sign in to comment.