diff --git a/uwsgi.c b/uwsgi.c index b89f4b37..206fb30e 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -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}, @@ -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= 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; @@ -2244,6 +2268,9 @@ end: 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; diff --git a/uwsgi.h b/uwsgi.h index b8f0093c..dfca0a99 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -98,6 +98,8 @@ #ifdef __linux +#define __USE_GNU +#include #include #include #include @@ -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 @@ -718,6 +721,8 @@ struct uwsgi_server { time_t master_mercy; + int cpu_affinity; + int reload_mercy; int option_index; struct option *long_options;