Skip to content

Commit 64b8306

Browse files
committed
changes from feedback, removing "further plans" parts.
using _SC_NPROCESSORS_ONLN code path only as backup now, checking actual max cpu affinity first.
1 parent 78e6ebc commit 64b8306

File tree

9 files changed

+53
-40
lines changed

9 files changed

+53
-40
lines changed

configure.ac

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,8 @@ pty.h \
403403
pwd.h \
404404
resolv.h \
405405
strings.h \
406-
sched.h \
407406
syslog.h \
408407
sysexits.h \
409-
sys/cpuset.h \
410408
sys/ioctl.h \
411409
sys/file.h \
412410
sys/mman.h \
@@ -572,7 +570,6 @@ AC_CHECK_FUNCS(
572570
alphasort \
573571
asctime_r \
574572
chroot \
575-
cpuset_setaffinity \
576573
ctime_r \
577574
explicit_memset \
578575
fdatasync \
@@ -610,7 +607,6 @@ poll \
610607
pthread_jit_write_protect_np \
611608
putenv \
612609
scandir \
613-
sched_setaffinity \
614610
setitimer \
615611
setenv \
616612
shutdown \

sapi/fpm/config.m4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ PHP_ARG_ENABLE([fpm],,
77
dnl Configure checks.
88
AC_DEFUN([AC_FPM_STDLIBS],
99
[
10-
AC_CHECK_FUNCS(clearenv setproctitle setproctitle_fast)
10+
AC_CHECK_FUNCS(clearenv setproctitle setproctitle_fast cpuset_setaffinity sched_setaffinity)
11+
AC_CHECK_HEADERS([sys/cpuset.h sched.h])
1112
1213
AC_SEARCH_LIBS(socket, socket)
1314
AC_SEARCH_LIBS(inet_addr, nsl)

sapi/fpm/fpm/fpm_conf.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static struct ini_value_parser_s ini_fpm_global_options[] = {
100100
{ "process.max", &fpm_conf_set_integer, GO(process_max) },
101101
{ "process.priority", &fpm_conf_set_integer, GO(process_priority) },
102102
#if HAVE_FPM_CPUAFFINITY
103-
{ "process.cpumask", &fpm_conf_set_string, GO(process_cpumask) },
103+
{ "process.cpu_list", &fpm_conf_set_string, GO(process_cpu_list) },
104104
#endif
105105
{ "daemonize", &fpm_conf_set_boolean, GO(daemonize) },
106106
{ "rlimit_files", &fpm_conf_set_integer, GO(rlimit_files) },
@@ -135,7 +135,7 @@ static struct ini_value_parser_s ini_fpm_pool_options[] = {
135135
{ "process.priority", &fpm_conf_set_integer, WPO(process_priority) },
136136
{ "process.dumpable", &fpm_conf_set_boolean, WPO(process_dumpable) },
137137
#if HAVE_FPM_CPUAFFINITY
138-
{ "process.cpumask", &fpm_conf_set_string, WPO(process_cpumask) },
138+
{ "process.cpu_list", &fpm_conf_set_string, WPO(process_cpu_list) },
139139
#endif
140140
{ "pm", &fpm_conf_set_pm, WPO(pm) },
141141
{ "pm.max_children", &fpm_conf_set_integer, WPO(pm_max_children) },
@@ -629,7 +629,7 @@ static void *fpm_worker_pool_config_alloc(void)
629629
wp->config->process_priority = 64; /* 64 means unset */
630630
wp->config->process_dumpable = 0;
631631
#if HAVE_FPM_CPUAFFINITY
632-
wp->config->process_cpumask = NULL;
632+
wp->config->process_cpu_list = NULL;
633633
#endif
634634
wp->config->clear_env = 1;
635635
wp->config->decorate_workers_output = 1;
@@ -1742,6 +1742,9 @@ static void fpm_conf_dump(void)
17421742
} else {
17431743
zlog(ZLOG_NOTICE, "\tprocess.priority = %d", fpm_global_config.process_priority);
17441744
}
1745+
#if HAVE_FPM_CPUAFFINITY
1746+
zlog(ZLOG_NOTICE, "\tprocess.cpu_list = %s", STR2STR(fpm_global_config.process_cpu_list));
1747+
#endif
17451748
zlog(ZLOG_NOTICE, "\tdaemonize = %s", BOOL2STR(fpm_global_config.daemonize));
17461749
zlog(ZLOG_NOTICE, "\trlimit_files = %d", fpm_global_config.rlimit_files);
17471750
zlog(ZLOG_NOTICE, "\trlimit_core = %d", fpm_global_config.rlimit_core);
@@ -1781,6 +1784,9 @@ static void fpm_conf_dump(void)
17811784
zlog(ZLOG_NOTICE, "\tprocess.priority = %d", wp->config->process_priority);
17821785
}
17831786
zlog(ZLOG_NOTICE, "\tprocess.dumpable = %s", BOOL2STR(wp->config->process_dumpable));
1787+
#if HAVE_FPM_CPUAFFINITY
1788+
zlog(ZLOG_NOTICE, "\tprocess.cpu_list = %s", STR2STR(wp->config->process_cpu_list));
1789+
#endif
17841790
zlog(ZLOG_NOTICE, "\tpm = %s", PM2STR(wp->config->pm));
17851791
zlog(ZLOG_NOTICE, "\tpm.max_children = %d", wp->config->pm_max_children);
17861792
zlog(ZLOG_NOTICE, "\tpm.start_servers = %d", wp->config->pm_start_servers);

sapi/fpm/fpm/fpm_conf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct fpm_global_config_s {
4545
int systemd_interval;
4646
#endif
4747
#if HAVE_FPM_CPUAFFINITY
48-
char *process_cpumask;
48+
char *process_cpu_list;
4949
#endif
5050
};
5151

@@ -111,7 +111,7 @@ struct fpm_worker_pool_config_s {
111111
int listen_setfib;
112112
#endif
113113
#if HAVE_FPM_CPUAFFINITY
114-
char *process_cpumask;
114+
char *process_cpu_list;
115115
#endif
116116
};
117117

sapi/fpm/fpm/fpm_unix.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <stdlib.h>
99
#include <unistd.h>
1010
#include <sys/types.h>
11+
#include <errno.h>
1112
#include <pwd.h>
1213
#include <grp.h>
1314

@@ -414,22 +415,31 @@ static long fpm_cpumax(void)
414415
{
415416
static long cpuid = LONG_MIN;
416417
if (cpuid == LONG_MIN) {
417-
cpuid = sysconf(_SC_NPROCESSORS_ONLN);
418+
cpu_set_t cset;
419+
#if defined(HAVE_SCHED_SETAFFINITY)
420+
if (sched_getaffinity(0, sizeof(cset), &cset) == 0) {
421+
#elif defined(HAVE_CPUSET_SETAFFINITY)
422+
if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cset), &cset) == 0) {
423+
#endif
424+
cpuid = CPU_COUNT(&cset);
425+
} else {
426+
cpuid = -1;
427+
}
418428
}
419429

420430
return cpuid;
421431
}
422432

423433
static void fpm_cpuaffinity_init(struct fpm_cpuaffinity_conf *c)
424434
{
425-
#if defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_CPUSET_SETAFFINITY)
435+
#if defined(HAVE_FPM_CPUAFFINITY)
426436
CPU_ZERO(&c->cset);
427437
#endif
428438
}
429439

430440
static void fpm_cpuaffinity_add(struct fpm_cpuaffinity_conf *c)
431441
{
432-
#if defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_CPUSET_SETAFFINITY)
442+
#if defined(HAVE_FPM_CPUAFFINITY)
433443
int i;
434444

435445
for (i = c->min; i <= c->max; i ++) {
@@ -449,12 +459,7 @@ static int fpm_cpuaffinity_set(struct fpm_cpuaffinity_conf *c)
449459
#endif
450460
}
451461

452-
static void fpm_cpuaffinity_destroy(struct fpm_cpuaffinity_conf *c)
453-
{
454-
// some platform/apis require to allocates data on the heap
455-
}
456-
457-
static int fpm_setcpuaffinity(char *cpumask)
462+
static int fpm_setcpuaffinity(char *cpu_list)
458463
{
459464
char *token, *buf;
460465
struct fpm_cpuaffinity_conf fconf;
@@ -464,20 +469,20 @@ static int fpm_setcpuaffinity(char *cpumask)
464469
cpumax = fpm_cpumax();
465470

466471
fpm_cpuaffinity_init(&fconf);
467-
token = php_strtok_r(cpumask, ";", &buf);
472+
token = php_strtok_r(cpu_list, ",", &buf);
468473

469474
while (token) {
470-
char *cpumasksep;
475+
char *cpu_listsep;
471476

472-
fconf.min = strtol(token, &cpumasksep, 0);
477+
fconf.min = strtol(token, &cpu_listsep, 0);
473478
if (errno || fconf.min < 0 || fconf.min > cpumax) {
474479
return -1;
475480
}
476481
fconf.max = fconf.min;
477-
if (*cpumasksep == '-') {
478-
if (strlen(cpumasksep) > 1) {
482+
if (*cpu_listsep == '-') {
483+
if (strlen(cpu_listsep) > 1) {
479484
char *err;
480-
fconf.max = strtol(cpumasksep + 1, &err, 0);
485+
fconf.max = strtol(cpu_listsep + 1, &err, 0);
481486
if (errno || *err != '\0' || fconf.max < fconf.min || fconf.max > cpumax) {
482487
return -1;
483488
}
@@ -492,7 +497,6 @@ static int fpm_setcpuaffinity(char *cpumask)
492497
}
493498

494499
r = fpm_cpuaffinity_set(&fconf);
495-
fpm_cpuaffinity_destroy(&fconf);
496500
return r;
497501
}
498502
#endif
@@ -522,10 +526,10 @@ int fpm_unix_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
522526
}
523527
}
524528
#if HAVE_FPM_CPUAFFINITY
525-
if (wp->config->process_cpumask) {
529+
if (wp->config->process_cpu_list) {
526530

527-
if (0 > fpm_setcpuaffinity(wp->config->process_cpumask)) {
528-
zlog(ZLOG_SYSERROR, "[pool %s] failed to fpm_setcpuaffinity(%s)", wp->config->name, wp->config->process_cpumask);
531+
if (0 > fpm_setcpuaffinity(wp->config->process_cpu_list)) {
532+
zlog(ZLOG_SYSERROR, "[pool %s] failed to fpm_setcpuaffinity(%s)", wp->config->name, wp->config->process_cpu_list);
529533
return -1;
530534
}
531535
}
@@ -777,9 +781,9 @@ int fpm_unix_init_main(void)
777781
}
778782

779783
#if HAVE_FPM_CPUAFFINITY
780-
if (fpm_global_config.process_cpumask) {
781-
if (0 > fpm_setcpuaffinity(fpm_global_config.process_cpumask)) {
782-
zlog(ZLOG_SYSERROR, "failed to fpm_setcpuaffinity(%s)", fpm_global_config.process_cpumask);
784+
if (fpm_global_config.process_cpu_list) {
785+
if (0 > fpm_setcpuaffinity(fpm_global_config.process_cpu_list)) {
786+
zlog(ZLOG_SYSERROR, "failed to fpm_setcpuaffinity(%s)", fpm_global_config.process_cpu_list);
783787
return -1;
784788
}
785789
}

sapi/fpm/php-fpm.conf.in

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,12 @@
129129
; The value can be one cpu id, a range or a list thereof.
130130
;
131131
; Default Value: not set
132-
; process.cpumask = "4"
133-
; process.cpumask = "0-3"
134-
; process.cpumask = "1-4;8-16;32-48"
132+
; the master process could be bound to a single core
133+
; process.cpu_list = "4"
134+
; a range from the minimum cpuid to the max required
135+
; process.cpu_list = "0-3"
136+
; or multiple ranges separated by a comma
137+
; process.cpu_list = "1-4,8-16,32-48"
135138

136139
;;;;;;;;;;;;;;;;;;;;
137140
; Pool Definitions ;

sapi/fpm/tests/cpuaffinity-fail.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require_once "tester.inc";
1414

1515
$cfg = <<<EOT
1616
[global]
17-
process.cpumask = 1000024
17+
process.cpu_list = 1000024
1818
error_log = {{FILE:LOG}}
1919
pid = {{FILE:PID}}
2020
[unconfined]

sapi/fpm/tests/cpuaffinity.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pm.max_children = 1
2727
pm.start_servers = 1
2828
pm.min_spare_servers = 1
2929
pm.max_spare_servers = 1
30-
process.cpumask = 1
30+
process.cpu_list = 1
3131
EOT;
3232

3333
$tester = new FPM\Tester($cfg);

sapi/fpm/www.conf.in

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ listen = 127.0.0.1:9000
8484
; The value can be one cpu id, a range or a list thereof.
8585
;
8686
; Default Value: inherits master's cpu affinity
87-
; process.cpumask = "8"
88-
; process.cpumask = "0-3"
89-
; process.cpumask = "4-7;10-12"
87+
; the pool process could be bound to a single core
88+
; process.cpu_list = "8"
89+
; a range from the minimum cpuid to the max required
90+
; process.cpu_list = "0-3"
91+
; or multiple ranges separated by a comma
92+
; process.cpu_list = "4-7,10-12"
9093

9194
; Set the process dumpable flag (PR_SET_DUMPABLE prctl for Linux or
9295
; PROC_TRACE_CTL procctl for FreeBSD) even if the process user

0 commit comments

Comments
 (0)