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 all 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
2 changes: 2 additions & 0 deletions auto/sources
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ UNIX_DEPS="$CORE_DEPS $EVENT_DEPS \
src/os/unix/ngx_shmem.h \
src/os/unix/ngx_process.h \
src/os/unix/ngx_setaffinity.h \
src/os/unix/ngx_getaffinity.h \
src/os/unix/ngx_setproctitle.h \
src/os/unix/ngx_atomic.h \
src/os/unix/ngx_gcc_atomic_x86.h \
Expand Down Expand Up @@ -192,6 +193,7 @@ UNIX_SRCS="$CORE_SRCS $EVENT_SRCS \
src/os/unix/ngx_process.c \
src/os/unix/ngx_daemon.c \
src/os/unix/ngx_setaffinity.c \
src/os/unix/ngx_getaffinity.c \
src/os/unix/ngx_setproctitle.c \
src/os/unix/ngx_posix_init.c \
src/os/unix/ngx_user.c \
Expand Down
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="T_NGX_HAVE_SCHED_GETAFFINITY"
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="T_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
54 changes: 54 additions & 0 deletions src/core/nginx.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <ngx_core.h>
#include <nginx.h>

#if (T_NGX_HAVE_SCHED_GETAFFINITY)
#include <ngx_getaffinity.h>
#endif

static void ngx_show_version_info(void);
static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
Expand Down Expand Up @@ -1462,6 +1465,26 @@ ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

n = 2;

#if (T_NGX_HAVE_SCHED_GETAFFINITY && T_NGX_HAVE_SC_NPROCESSORS_CONF)
} else if (ngx_strcmp(value[1].data, "auto_online_cpu") == 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 = 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 +1498,12 @@ ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return NGX_CONF_ERROR;
}

#if (T_NGX_HAVE_SCHED_GETAFFINITY && T_NGX_HAVE_SC_NPROCESSORS_CONF)
if (ccf->cpu_affinity_auto == 2) {
continue;
}
#endif

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

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

#if (T_NGX_HAVE_SCHED_GETAFFINITY && T_NGX_HAVE_SC_NPROCESSORS_CONF)
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 +1570,27 @@ ngx_get_cpu_affinity(ngx_uint_t n)
if (ccf->cpu_affinity_auto) {
mask = &ccf->cpu_affinity[ccf->cpu_affinity_n - 1];

#if (T_NGX_HAVE_SCHED_GETAFFINITY && T_NGX_HAVE_SC_NPROCESSORS_CONF)
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) lhanjian ([email protected])
*/


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


#if (T_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_getaffinity() failed");
}
}

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

/*
* Copyright (C) lhanjian ([email protected])
*/

#ifndef _NGX_GETAFFINITY_H_INCLUDED_
#define _NGX_GETAFFINITY_H_INCLUDED_


#if (T_NGX_HAVE_SCHED_GETAFFINITY)

typedef cpu_set_t ngx_cpuset_t;

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

#else

#define ngx_getaffinity(cpu_affinity, log)

typedef uint64_t ngx_cpuset_t;

#endif


#endif /* _NGX_GETAFFINITY_H_INCLUDED_ */