diff --git a/core/socket.c b/core/socket.c index 5dfe66e1..b9eb5b44 100644 --- a/core/socket.c +++ b/core/socket.c @@ -1517,3 +1517,66 @@ void uwsgi_setup_shared_sockets() { } + +void uwsgi_map_sockets() { + struct uwsgi_socket *uwsgi_sock = uwsgi.sockets; + while (uwsgi_sock) { + struct uwsgi_string_list *usl = uwsgi.map_socket; + int enabled = 1; + while (usl) { + + char *colon = strchr(usl->value, ':'); + if ((int) uwsgi_str_num(usl->value, colon - usl->value) == uwsgi_get_socket_num(uwsgi_sock)) { + enabled = 0; + char *p = strtok(colon + 1, ","); + while (p != NULL) { + int w = atoi(p); + if (w < 1 || w > uwsgi.numproc) { + uwsgi_log("invalid worker num: %d\n", w); + exit(1); + } + if (w == uwsgi.mywid) { + enabled = 1; + uwsgi_log("mapped socket %d (%s) to worker %d\n", uwsgi_get_socket_num(uwsgi_sock), uwsgi_sock->name, uwsgi.mywid); + break; + } + p = strtok(NULL, ","); + } + } + + usl = usl->next; + } + + if (!enabled) { + close(uwsgi_sock->fd); + int fd = open("/dev/null", O_RDONLY); + if (fd < 0) { + uwsgi_error_open("/dev/null"); + exit(1); + } + if (fd != uwsgi_sock->fd) { + if (dup2(fd, uwsgi_sock->fd)) { + uwsgi_error("dup2()"); + exit(1); + } + close(fd); + } + uwsgi_sock->disabled = 1; + } + + + uwsgi_sock = uwsgi_sock->next; + + } + + uwsgi_sock = uwsgi.sockets; + while (uwsgi_sock) { + if (uwsgi_sock->disabled) { + uwsgi_sock = uwsgi_del_socket(uwsgi_sock); + } + else { + uwsgi_sock = uwsgi_sock->next; + } + } + +} diff --git a/core/utils.c b/core/utils.c index 6869b446..dd338bfa 100644 --- a/core/utils.c +++ b/core/utils.c @@ -4717,3 +4717,31 @@ char *uwsgi_sanitize_cert_filename(char *base, char *key, uint16_t keylen) { } #endif + +void uwsgi_set_cpu_affinity() { + int i; + 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 + } + +} diff --git a/core/uwsgi.c b/core/uwsgi.c index 4120130d..03e503a4 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -2772,6 +2772,7 @@ nextsock: uwsgi_notify_ready(); uwsgi.current_time = uwsgi_now(); + // here we spawn the workers... if (!uwsgi.cheap) { if (uwsgi.cheaper && uwsgi.cheaper_count) { int nproc = uwsgi.cheaper_initial; @@ -2796,6 +2797,9 @@ nextsock: } } + // END OF INITIALIZATION + + // !!! from now on, we could be in the master or in a worker !!! if (getpid() == masterpid && uwsgi.master_process == 1) { @@ -2810,91 +2814,11 @@ nextsock: //from now on the process is a real worker } + // eventually maps (or disable) sockets for the worker + uwsgi_map_sockets(); - uwsgi_sock = uwsgi.sockets; - while (uwsgi_sock) { - struct uwsgi_string_list *usl = uwsgi.map_socket; - int enabled = 1; - while (usl) { - - char *colon = strchr(usl->value, ':'); - if ((int) uwsgi_str_num(usl->value, colon - usl->value) == uwsgi_get_socket_num(uwsgi_sock)) { - enabled = 0; - char *p = strtok(colon + 1, ","); - while (p != NULL) { - int w = atoi(p); - if (w < 1 || w > uwsgi.numproc) { - uwsgi_log("invalid worker num: %d\n", w); - exit(1); - } - if (w == uwsgi.mywid) { - enabled = 1; - uwsgi_log("mapped socket %d (%s) to worker %d\n", uwsgi_get_socket_num(uwsgi_sock), uwsgi_sock->name, uwsgi.mywid); - break; - } - p = strtok(NULL, ","); - } - } - - usl = usl->next; - } - - if (!enabled) { - close(uwsgi_sock->fd); - int fd = open("/dev/null", O_RDONLY); - if (fd < 0) { - uwsgi_error_open("/dev/null"); - exit(1); - } - if (fd != uwsgi_sock->fd) { - if (dup2(fd, uwsgi_sock->fd)) { - uwsgi_error("dup2()"); - exit(1); - } - close(fd); - } - uwsgi_sock->disabled = 1; - } - - - uwsgi_sock = uwsgi_sock->next; - - } - - uwsgi_sock = uwsgi.sockets; - while (uwsgi_sock) { - if (uwsgi_sock->disabled) { - uwsgi_sock = uwsgi_del_socket(uwsgi_sock); - } - else { - uwsgi_sock = uwsgi_sock->next; - } - } - - 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 - } - + // eventually set cpu affinity poilicies (OS-dependent) + uwsgi_set_cpu_affinity(); if (uwsgi.worker_exec) { char *w_argv[2]; diff --git a/uwsgi.h b/uwsgi.h index 30061de6..ab43fd1a 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -3187,7 +3187,10 @@ void uwsgi_setup_shared_sockets(void); void uwsgi_setup_mules_and_farms(void); -void uwsgi_setup_workers(); +void uwsgi_setup_workers(void); +void uwsgi_map_sockets(void); + +void uwsgi_set_cpu_affinity(void); #ifdef UWSGI_AS_SHARED_LIBRARY int uwsgi_init(int, char **, char **);