Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: online cpu can be bind by sched_getaffinity() #1883

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions auto/unix
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ ngx_feature_test="cpu_set_t mask;
sched_setaffinity(0, sizeof(cpu_set_t), &mask)"
. auto/feature

ngx_feature="sched_getaffinity()"
ngx_feature_name="NGX_HAVE_SCHED_GETAFFINITY"
lhanjian marked this conversation as resolved.
Show resolved Hide resolved
ngx_feature_run=no
ngx_feature_incs="#include <sched.h>"
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test="cpu_set_t mask;
CPU_ZERO(&mask);
sched_getaffinity(0, sizeof(cpu_set_t), &mask)"
. auto/feature

ngx_feature="SO_SETFIB"
ngx_feature_name="NGX_HAVE_SETFIB"
Expand Down Expand Up @@ -965,6 +975,15 @@ ngx_feature_libs=
ngx_feature_test="sysconf(_SC_NPROCESSORS_ONLN)"
. auto/feature

ngx_feature="sysconf(_SC_NPROCESSORS_CONF)"
ngx_feature_name="NGX_HAVE_SC_NPROCESSORS_CONF"
ngx_feature_run=no
ngx_feature_incs=
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test="sysconf(_SC_NPROCESSORS_CONF)"
. auto/feature


ngx_feature="sysconf(_SC_LEVEL1_DCACHE_LINESIZE)"
ngx_feature_name="NGX_HAVE_LEVEL1_DCACHE_LINESIZE"
Expand Down
51 changes: 51 additions & 0 deletions src/core/nginx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,26 @@ ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

n = 2;

#if (NGX_HAVE_SCHED_GETAFFINITY)
} elseif (ngx_strcmp(value[1].data, "auto_online_cpu") == 0) {
lhanjian marked this conversation as resolved.
Show resolved Hide resolved

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 = 2;

CPU_ZERO(&mask[0]);
for (i = 0; i < (ngx_uint_t) ngx_min(ngx_ncpu, CPU_SETSIZE); i++) {
CPU_SET(i, &mask[0]);
}

n = 2;
#endif

} else {
n = 1;
}
Expand All @@ -1475,6 +1495,12 @@ ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return NGX_CONF_ERROR;
}

#if (NGX_HAVE_SCHED_GETAFFINITY)
if (ccf->cpu_affinity_auto == 2) {
continue;
}
#endif

i = 0;
CPU_ZERO(&mask[n - 1]);

Expand Down Expand Up @@ -1525,6 +1551,10 @@ ngx_get_cpu_affinity(ngx_uint_t n)
ngx_cpuset_t *mask;
ngx_core_conf_t *ccf;

#if (NGX_HAVE_SCHED_GETAFFINITY)
ngx_int_t worker_i, machine_core, all_machine_cores;
#endif

static ngx_cpuset_t result;

ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
Expand All @@ -1537,6 +1567,27 @@ ngx_get_cpu_affinity(ngx_uint_t n)
if (ccf->cpu_affinity_auto) {
mask = &ccf->cpu_affinity[ccf->cpu_affinity_n - 1];

#if (NGX_HAVE_SCHED_GETAFFINITY && NGX_HAVE_SC_NPROCESSORS_ONLN)
if (ccf->cpu_affinity_auto == 2) {
all_machine_cores = sysconf(_SC_NPROCESSORS_CONF);

(void) ngx_getaffinity(mask, ngx_cycle->log);
for (machine_core = 0, worker_i = 0; worker_i < ccf->worker_processes; machine_core++) {
if (CPU_ISSET(machine_core, mask)) {
CPU_ZERO(&result);
CPU_SET(machine_core, &result);
worker_i++;
}

if (machine_core == all_machine_cores - 1) {
machine_core = 0;
}
}

return &result;
}
#endif

for (i = 0, j = n; /* void */ ; i++) {

if (CPU_ISSET(i % CPU_SETSIZE, mask) && j-- == 0) {
Expand Down
22 changes: 22 additions & 0 deletions src/os/unix/ngx_getaffinity.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

/*
* Copyright (C) Nginx, Inc.
lhanjian marked this conversation as resolved.
Show resolved Hide resolved
*/


#include <ngx_config.h>
#include <ngx_core.h>


#if (NGX_HAVE_SCHED_GETAFFINITY)

void
ngx_getaffinity(ngx_cpuset_t *cpu_affinity, ngx_log_t *log)
{
if (sched_getaffinity(0, sizeof(cpu_set_t), cpu_affinity) == -1) {
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
"sched_setaffinity() failed");
}
}

#endif
27 changes: 27 additions & 0 deletions src/os/unix/ngx_getaffinity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

/*
* Copyright (C) Nginx, Inc.
*/

#ifndef _NGX_SETAFFINITY_H_INCLUDED_
#define _NGX_SETAFFINITY_H_INCLUDED_


#if (NGX_HAVE_SCHED_SETAFFINITY)

#define NGX_HAVE_CPU_AFFINITY 1

typedef cpu_set_t ngx_cpuset_t;

void ngx_setaffinity(ngx_cpuset_t *cpu_affinity, ngx_log_t *log);

#else

#define ngx_setaffinity(cpu_affinity, log)

typedef uint64_t ngx_cpuset_t;

#endif


#endif /* _NGX_SETAFFINITY_H_INCLUDED_ */