Skip to content

Commit 0abc26c

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 0abc26c

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.cpumask = %s", STR2STR(fpm_global_config.process_cpumask));
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.cpumask = %s", STR2STR(wp->config->process_cpumask));
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_unix.c

Lines changed: 14 additions & 10 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,11 +459,6 @@ 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-
457462
static int fpm_setcpuaffinity(char *cpumask)
458463
{
459464
char *token, *buf;
@@ -464,7 +469,7 @@ 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(cpumask, ",", &buf);
468473

469474
while (token) {
470475
char *cpumasksep;
@@ -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

sapi/fpm/php-fpm.conf.in

Lines changed: 4 additions & 1 deletion
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+
; the master process could be bound to a single core
132133
; process.cpumask = "4"
134+
; a range from the minimum cpuid to the max required
133135
; process.cpumask = "0-3"
134-
; process.cpumask = "1-4;8-16;32-48"
136+
; or multiple ranges separated by a comma
137+
; process.cpumask = "1-4,8-16,32-48"
135138

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

sapi/fpm/www.conf.in

Lines changed: 4 additions & 1 deletion
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+
; the pool process could be bound to a single core
8788
; process.cpumask = "8"
89+
; a range from the minimum cpuid to the max required
8890
; process.cpumask = "0-3"
89-
; process.cpumask = "4-7;10-12"
91+
; or multiple ranges separated by a comma
92+
; process.cpumask = "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)