mirror of
https://github.com/nginx/nginx.git
synced 2025-06-10 11:38:36 +08:00
Core: worker_cpu_affinity auto.
If enabled, workers are bound to available CPUs, each worker to once CPU in order. If there are more workers than available CPUs, remaining are bound in a loop, starting again from the first available CPU. The optional mask parameter defines which CPUs are available for automatic binding. In collaboration with Vladimir Homutov.
This commit is contained in:
parent
af647a3da2
commit
d0bf684ab6
@ -961,6 +961,7 @@ ngx_core_module_create_conf(ngx_cycle_t *cycle)
|
|||||||
* ccf->pid = NULL;
|
* ccf->pid = NULL;
|
||||||
* ccf->oldpid = NULL;
|
* ccf->oldpid = NULL;
|
||||||
* ccf->priority = 0;
|
* ccf->priority = 0;
|
||||||
|
* ccf->cpu_affinity_auto = 0;
|
||||||
* ccf->cpu_affinity_n = 0;
|
* ccf->cpu_affinity_n = 0;
|
||||||
* ccf->cpu_affinity = NULL;
|
* ccf->cpu_affinity = NULL;
|
||||||
*/
|
*/
|
||||||
@ -1002,7 +1003,8 @@ ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
|
|||||||
|
|
||||||
#if (NGX_HAVE_CPU_AFFINITY)
|
#if (NGX_HAVE_CPU_AFFINITY)
|
||||||
|
|
||||||
if (ccf->cpu_affinity_n
|
if (!ccf->cpu_affinity_auto
|
||||||
|
&& ccf->cpu_affinity_n
|
||||||
&& ccf->cpu_affinity_n != 1
|
&& ccf->cpu_affinity_n != 1
|
||||||
&& ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes)
|
&& ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes)
|
||||||
{
|
{
|
||||||
@ -1273,7 +1275,24 @@ ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
|
|
||||||
value = cf->args->elts;
|
value = cf->args->elts;
|
||||||
|
|
||||||
for (n = 1; n < cf->args->nelts; n++) {
|
if (ngx_strcmp(value[1].data, "auto") == 0) {
|
||||||
|
|
||||||
|
if (cf->args->nelts > 3) {
|
||||||
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
||||||
|
"invalid number of arguments in "
|
||||||
|
"\"worker_cpu_affinity\" directive");
|
||||||
|
return NGX_CONF_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
ccf->cpu_affinity_auto = 1;
|
||||||
|
mask[0] = (uint64_t) -1 >> (64 - ngx_min(64, ngx_ncpu));
|
||||||
|
n = 2;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
n = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( /* void */ ; n < cf->args->nelts; n++) {
|
||||||
|
|
||||||
if (value[n].len > 64) {
|
if (value[n].len > 64) {
|
||||||
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
||||||
@ -1323,6 +1342,8 @@ ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
uint64_t
|
uint64_t
|
||||||
ngx_get_cpu_affinity(ngx_uint_t n)
|
ngx_get_cpu_affinity(ngx_uint_t n)
|
||||||
{
|
{
|
||||||
|
uint64_t mask;
|
||||||
|
ngx_uint_t i;
|
||||||
ngx_core_conf_t *ccf;
|
ngx_core_conf_t *ccf;
|
||||||
|
|
||||||
ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
|
ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
|
||||||
@ -1332,6 +1353,24 @@ ngx_get_cpu_affinity(ngx_uint_t n)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ccf->cpu_affinity_auto) {
|
||||||
|
mask = ccf->cpu_affinity[ccf->cpu_affinity_n - 1];
|
||||||
|
|
||||||
|
if (mask == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; /* void */ ; i++) {
|
||||||
|
if ((mask & ((uint64_t) 1 << (i % 64))) && n-- == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* void */
|
||||||
|
}
|
||||||
|
|
||||||
|
return (uint64_t) 1 << (i % 64);
|
||||||
|
}
|
||||||
|
|
||||||
if (ccf->cpu_affinity_n > n) {
|
if (ccf->cpu_affinity_n > n) {
|
||||||
return ccf->cpu_affinity[n];
|
return ccf->cpu_affinity[n];
|
||||||
}
|
}
|
||||||
|
@ -88,6 +88,7 @@ typedef struct {
|
|||||||
|
|
||||||
int priority;
|
int priority;
|
||||||
|
|
||||||
|
ngx_uint_t cpu_affinity_auto;
|
||||||
ngx_uint_t cpu_affinity_n;
|
ngx_uint_t cpu_affinity_n;
|
||||||
uint64_t *cpu_affinity;
|
uint64_t *cpu_affinity;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user