From 479f96e9d2885853fb7f983bd6f66cd7947cb87a Mon Sep 17 00:00:00 2001 From: "hanjian.lhj" Date: Fri, 3 Nov 2023 17:51:45 +0800 Subject: [PATCH 1/8] auto get cpu --- auto/unix | 19 +++++++++++++ src/core/nginx.c | 51 +++++++++++++++++++++++++++++++++++ src/os/unix/ngx_getaffinity.c | 24 +++++++++++++++++ src/os/unix/ngx_getaffinity.h | 27 +++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 src/os/unix/ngx_getaffinity.c create mode 100644 src/os/unix/ngx_getaffinity.h diff --git a/auto/unix b/auto/unix index 7c7d7bca69..e84e448598 100644 --- a/auto/unix +++ b/auto/unix @@ -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" +ngx_feature_run=no +ngx_feature_incs="#include " +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" @@ -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" diff --git a/src/core/nginx.c b/src/core/nginx.c index 05d1f8c1d3..5c21f1b9b0 100755 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -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) { + + 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; } @@ -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]); @@ -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, @@ -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) { diff --git a/src/os/unix/ngx_getaffinity.c b/src/os/unix/ngx_getaffinity.c new file mode 100644 index 0000000000..f7996593b1 --- /dev/null +++ b/src/os/unix/ngx_getaffinity.c @@ -0,0 +1,24 @@ + +/* + * Copyright (C) Nginx, Inc. + */ + + +#include +#include + + +#if (NGX_HAVE_SCHED_GETAFFINITY) + +void +ngx_getaffinity(ngx_cpuset_t *cpu_affinity, ngx_log_t *log) +{ + ngx_uint_t i; + + if (sched_getaffinity(0, sizeof(cpu_set_t), cpu_affinity) == -1) { + ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, + "sched_setaffinity() failed"); + } +} + +#endif diff --git a/src/os/unix/ngx_getaffinity.h b/src/os/unix/ngx_getaffinity.h new file mode 100644 index 0000000000..5afaa9d71b --- /dev/null +++ b/src/os/unix/ngx_getaffinity.h @@ -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_ */ From f67cff8d2a1f8f2dce60c5259b72d2c35ae6f86c Mon Sep 17 00:00:00 2001 From: "hanjian.lhj" Date: Fri, 3 Nov 2023 18:03:38 +0800 Subject: [PATCH 2/8] remove noused variable --- src/os/unix/ngx_getaffinity.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/os/unix/ngx_getaffinity.c b/src/os/unix/ngx_getaffinity.c index f7996593b1..f61ec62747 100644 --- a/src/os/unix/ngx_getaffinity.c +++ b/src/os/unix/ngx_getaffinity.c @@ -13,8 +13,6 @@ void ngx_getaffinity(ngx_cpuset_t *cpu_affinity, ngx_log_t *log) { - ngx_uint_t i; - if (sched_getaffinity(0, sizeof(cpu_set_t), cpu_affinity) == -1) { ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "sched_setaffinity() failed"); From a38a9c0aeb561276342e4ce5e73e27353f29e332 Mon Sep 17 00:00:00 2001 From: "hanjian.lhj" Date: Mon, 6 Nov 2023 19:28:17 +0800 Subject: [PATCH 3/8] update --- auto/unix | 2 +- src/core/nginx.c | 10 +++++----- src/os/unix/ngx_getaffinity.c | 4 ++-- src/os/unix/ngx_getaffinity.h | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/auto/unix b/auto/unix index e84e448598..3fb2aa8294 100644 --- a/auto/unix +++ b/auto/unix @@ -312,7 +312,7 @@ ngx_feature_test="cpu_set_t mask; . auto/feature ngx_feature="sched_getaffinity()" -ngx_feature_name="NGX_HAVE_SCHED_GETAFFINITY" +ngx_feature_name="T_NGX_HAVE_SCHED_GETAFFINITY" ngx_feature_run=no ngx_feature_incs="#include " ngx_feature_path= diff --git a/src/core/nginx.c b/src/core/nginx.c index 5c21f1b9b0..639c1cf082 100755 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -1462,8 +1462,8 @@ 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) { +#if (T_NGX_HAVE_SCHED_GETAFFINITY) + } 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, @@ -1495,7 +1495,7 @@ ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } -#if (NGX_HAVE_SCHED_GETAFFINITY) +#if (T_NGX_HAVE_SCHED_GETAFFINITY) if (ccf->cpu_affinity_auto == 2) { continue; } @@ -1551,7 +1551,7 @@ ngx_get_cpu_affinity(ngx_uint_t n) ngx_cpuset_t *mask; ngx_core_conf_t *ccf; -#if (NGX_HAVE_SCHED_GETAFFINITY) +#if (T_NGX_HAVE_SCHED_GETAFFINITY) ngx_int_t worker_i, machine_core, all_machine_cores; #endif @@ -1567,7 +1567,7 @@ 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 (T_NGX_HAVE_SCHED_GETAFFINITY && NGX_HAVE_SC_NPROCESSORS_ONLN && NGX_HAVE_SC_NPROCESSORS_CONF) if (ccf->cpu_affinity_auto == 2) { all_machine_cores = sysconf(_SC_NPROCESSORS_CONF); diff --git a/src/os/unix/ngx_getaffinity.c b/src/os/unix/ngx_getaffinity.c index f61ec62747..d2825da3be 100644 --- a/src/os/unix/ngx_getaffinity.c +++ b/src/os/unix/ngx_getaffinity.c @@ -1,6 +1,6 @@ /* - * Copyright (C) Nginx, Inc. + * Copyright (C) lhanjian (lhjay1@gmail.com) */ @@ -8,7 +8,7 @@ #include -#if (NGX_HAVE_SCHED_GETAFFINITY) +#if (T_NGX_HAVE_SCHED_GETAFFINITY) void ngx_getaffinity(ngx_cpuset_t *cpu_affinity, ngx_log_t *log) diff --git a/src/os/unix/ngx_getaffinity.h b/src/os/unix/ngx_getaffinity.h index 5afaa9d71b..e2c78dd9a5 100644 --- a/src/os/unix/ngx_getaffinity.h +++ b/src/os/unix/ngx_getaffinity.h @@ -1,6 +1,6 @@ /* - * Copyright (C) Nginx, Inc. + * Copyright (C) lhanjian (lhjay1@gmail.com) */ #ifndef _NGX_SETAFFINITY_H_INCLUDED_ From f263f105b7648e07a0d6cd4524879c4594469b96 Mon Sep 17 00:00:00 2001 From: "hanjian.lhj" Date: Mon, 6 Nov 2023 19:47:20 +0800 Subject: [PATCH 4/8] update --- src/core/nginx.c | 2 +- src/os/unix/ngx_getaffinity.h | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/core/nginx.c b/src/core/nginx.c index 639c1cf082..5921c96006 100755 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -1551,7 +1551,7 @@ ngx_get_cpu_affinity(ngx_uint_t n) ngx_cpuset_t *mask; ngx_core_conf_t *ccf; -#if (T_NGX_HAVE_SCHED_GETAFFINITY) +#if (T_NGX_HAVE_SCHED_GETAFFINITY && NGX_HAVE_SC_NPROCESSORS_ONLN && NGX_HAVE_SC_NPROCESSORS_CONF) ngx_int_t worker_i, machine_core, all_machine_cores; #endif diff --git a/src/os/unix/ngx_getaffinity.h b/src/os/unix/ngx_getaffinity.h index e2c78dd9a5..18b0417970 100644 --- a/src/os/unix/ngx_getaffinity.h +++ b/src/os/unix/ngx_getaffinity.h @@ -3,21 +3,19 @@ * Copyright (C) lhanjian (lhjay1@gmail.com) */ -#ifndef _NGX_SETAFFINITY_H_INCLUDED_ -#define _NGX_SETAFFINITY_H_INCLUDED_ +#ifndef _NGX_GETAFFINITY_H_INCLUDED_ +#define _NGX_GETAFFINITY_H_INCLUDED_ -#if (NGX_HAVE_SCHED_SETAFFINITY) - -#define NGX_HAVE_CPU_AFFINITY 1 +#if (T_NGX_HAVE_SCHED_GETAFFINITY) typedef cpu_set_t ngx_cpuset_t; -void ngx_setaffinity(ngx_cpuset_t *cpu_affinity, ngx_log_t *log); +void ngx_getaffinity(ngx_cpuset_t *cpu_affinity, ngx_log_t *log); #else -#define ngx_setaffinity(cpu_affinity, log) +#define ngx_getaffinity(cpu_affinity, log) typedef uint64_t ngx_cpuset_t; From 949161939057bf9e525a7a8826b85675c64be524 Mon Sep 17 00:00:00 2001 From: "hanjian.lhj" Date: Wed, 8 Nov 2023 11:42:56 +0800 Subject: [PATCH 5/8] update --- src/core/nginx.c | 4 ++-- src/os/unix/ngx_getaffinity.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/nginx.c b/src/core/nginx.c index 5921c96006..4d193f33a5 100755 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -1551,7 +1551,7 @@ ngx_get_cpu_affinity(ngx_uint_t n) ngx_cpuset_t *mask; ngx_core_conf_t *ccf; -#if (T_NGX_HAVE_SCHED_GETAFFINITY && NGX_HAVE_SC_NPROCESSORS_ONLN && NGX_HAVE_SC_NPROCESSORS_CONF) +#if (T_NGX_HAVE_SCHED_GETAFFINITY && NGX_HAVE_SC_NPROCESSORS_CONF) ngx_int_t worker_i, machine_core, all_machine_cores; #endif @@ -1567,7 +1567,7 @@ 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 && NGX_HAVE_SC_NPROCESSORS_ONLN && NGX_HAVE_SC_NPROCESSORS_CONF) +#if (T_NGX_HAVE_SCHED_GETAFFINITY && NGX_HAVE_SC_NPROCESSORS_CONF) if (ccf->cpu_affinity_auto == 2) { all_machine_cores = sysconf(_SC_NPROCESSORS_CONF); diff --git a/src/os/unix/ngx_getaffinity.c b/src/os/unix/ngx_getaffinity.c index d2825da3be..28899e29cd 100644 --- a/src/os/unix/ngx_getaffinity.c +++ b/src/os/unix/ngx_getaffinity.c @@ -15,7 +15,7 @@ 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"); + "sched_getaffinity() failed"); } } From 335c587ee31fcfdb3c0c58ec7bc15a7e39762d4f Mon Sep 17 00:00:00 2001 From: "hanjian.lhj" Date: Wed, 8 Nov 2023 15:04:11 +0800 Subject: [PATCH 6/8] update --- auto/sources | 2 ++ src/core/nginx.c | 3 +++ src/os/unix/ngx_getaffinity.h | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/auto/sources b/auto/sources index 7cef849cbb..9e9361bc0d 100644 --- a/auto/sources +++ b/auto/sources @@ -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 \ @@ -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 \ diff --git a/src/core/nginx.c b/src/core/nginx.c index 4d193f33a5..4c142be31d 100755 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -9,6 +9,9 @@ #include #include +#if (T_NGX_HAVE_SCHED_GETAFFINITY) +#include +#endif static void ngx_show_version_info(void); static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle); diff --git a/src/os/unix/ngx_getaffinity.h b/src/os/unix/ngx_getaffinity.h index 18b0417970..e34f1708b0 100644 --- a/src/os/unix/ngx_getaffinity.h +++ b/src/os/unix/ngx_getaffinity.h @@ -22,4 +22,4 @@ typedef uint64_t ngx_cpuset_t; #endif -#endif /* _NGX_SETAFFINITY_H_INCLUDED_ */ +#endif /* _NGX_GETAFFINITY_H_INCLUDED_ */ From 60e0b4ee4d19f7eec8410884babf4ee0d7b4c15f Mon Sep 17 00:00:00 2001 From: "hanjian.lhj" Date: Wed, 8 Nov 2023 15:28:02 +0800 Subject: [PATCH 7/8] update --- auto/unix | 2 +- src/core/nginx.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/auto/unix b/auto/unix index 3fb2aa8294..21063694c6 100644 --- a/auto/unix +++ b/auto/unix @@ -976,7 +976,7 @@ 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_name="T_NGX_HAVE_SC_NPROCESSORS_CONF" ngx_feature_run=no ngx_feature_incs= ngx_feature_path= diff --git a/src/core/nginx.c b/src/core/nginx.c index 4c142be31d..edb1251042 100755 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -1554,7 +1554,7 @@ ngx_get_cpu_affinity(ngx_uint_t n) ngx_cpuset_t *mask; ngx_core_conf_t *ccf; -#if (T_NGX_HAVE_SCHED_GETAFFINITY && NGX_HAVE_SC_NPROCESSORS_CONF) +#if (T_NGX_HAVE_SCHED_GETAFFINITY && T_NGX_HAVE_SC_NPROCESSORS_CONF) ngx_int_t worker_i, machine_core, all_machine_cores; #endif @@ -1570,7 +1570,7 @@ 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 && NGX_HAVE_SC_NPROCESSORS_CONF) +#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); From 936c3a8c955b0accdb20c7c1ffa0d603a8dae8e6 Mon Sep 17 00:00:00 2001 From: "hanjian.lhj" Date: Wed, 8 Nov 2023 15:35:01 +0800 Subject: [PATCH 8/8] update --- src/core/nginx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/nginx.c b/src/core/nginx.c index edb1251042..de963a4b5c 100755 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -1465,7 +1465,7 @@ ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) n = 2; -#if (T_NGX_HAVE_SCHED_GETAFFINITY) +#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) { @@ -1498,7 +1498,7 @@ ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } -#if (T_NGX_HAVE_SCHED_GETAFFINITY) +#if (T_NGX_HAVE_SCHED_GETAFFINITY && T_NGX_HAVE_SC_NPROCESSORS_CONF) if (ccf->cpu_affinity_auto == 2) { continue; }