Skip to content

Commit 3347b41

Browse files
committed
multi-ranges support.
1 parent caebe17 commit 3347b41

File tree

3 files changed

+83
-57
lines changed

3 files changed

+83
-57
lines changed

sapi/fpm/fpm/fpm_unix.c

Lines changed: 77 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
359359
/* }}} */
360360

361361
#if HAVE_FPM_CPUAFFINITY
362+
struct fpm_cpuaffinity_conf {
363+
#if defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_CPUSET_SETAFFINITY)
364+
cpu_set_t cset;
365+
#endif
366+
long min;
367+
long max;
368+
};
369+
362370
static long fpm_cpumax(void)
363371
{
364372
static long cpuid = LONG_MIN;
@@ -369,53 +377,82 @@ static long fpm_cpumax(void)
369377
return cpuid;
370378
}
371379

372-
static int fpm_cpusrange(char *cpumask, int *min, int *max) {
373-
char *cpumasksep;
374-
long cpumax;
375-
cpumax = fpm_cpumax();
376-
if (0 >= cpumax) {
377-
return -1;
378-
}
379-
*min = strtol(cpumask, &cpumasksep, 0);
380-
if (errno || *min < 0 || *min >= cpumax) {
381-
return -1;
382-
}
383-
384-
*max = *min;
385-
if (*cpumasksep == '-') {
386-
if (strlen(cpumasksep) > 1) {
387-
char *err;
388-
*max = strtol(cpumasksep + 1, &err, 0);
389-
if (errno || *err != '\0' || *max <= *min || *max >= cpumax) {
390-
return -1;
391-
}
392-
} else {
393-
return -1;
394-
}
395-
}
396-
return 0;
380+
static void fpm_cpuaffinity_init(struct fpm_cpuaffinity_conf *c)
381+
{
382+
#if defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_CPUSET_SETAFFINITY)
383+
CPU_ZERO(&c->cset);
384+
#endif
397385
}
398386

399-
static int fpm_setcpuaffinity(int min, int max)
387+
static void fpm_cpuaffinity_add(struct fpm_cpuaffinity_conf *c)
400388
{
401389
#if defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_CPUSET_SETAFFINITY)
402-
cpu_set_t cset;
403390
int i;
404391

405-
CPU_ZERO(&cset);
406-
for (i = min; i <= max; i ++) {
407-
if (!CPU_ISSET(i, &cset)) {
408-
CPU_SET(i, &cset);
392+
for (i = c->min; i <= c->max; i ++) {
393+
if (!CPU_ISSET(i, &c->cset)) {
394+
CPU_SET(i, &c->cset);
409395
}
410396
}
397+
#endif
398+
}
411399

400+
static int fpm_cpuaffinity_set(struct fpm_cpuaffinity_conf *c)
401+
{
412402
#if defined(HAVE_SCHED_SETAFFINITY)
413-
return sched_setaffinity(0, sizeof(cset), &cset);
414-
#else
415-
return cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(cset), &cset);
416-
#endif
403+
return sched_setaffinity(0, sizeof(c->cset), &c->cset);
404+
#elif defined(HAVE_CPUSET_SETAFFINITY)
405+
return cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(c->cset), &c->cset);
417406
#endif
418407
}
408+
409+
static void fpm_cpuaffinity_destroy(struct fpm_cpuaffinity_conf *c)
410+
{
411+
// some platform/apis requires to allocates data on the heap
412+
}
413+
414+
static int fpm_setcpuaffinity(char *cpumask)
415+
{
416+
char *token, *buf;
417+
struct fpm_cpuaffinity_conf fconf;
418+
int r, cpumax;
419+
420+
r = -1;
421+
cpumax = fpm_cpumax();
422+
423+
fpm_cpuaffinity_init(&fconf);
424+
token = php_strtok_r(cpumask, ";", &buf);
425+
426+
while (token) {
427+
char *cpumasksep;
428+
429+
fconf.min = strtol(token, &cpumasksep, 0);
430+
if (errno || fconf.min > cpumax) {
431+
goto fail;
432+
}
433+
fconf.max = fconf.min;
434+
if (*cpumasksep == '-') {
435+
if (strlen(cpumasksep) > 1) {
436+
char *err;
437+
fconf.max = strtol(cpumasksep + 1, &err, 0);
438+
if (errno || *err != '\0' || fconf.max < fconf.min || fconf.max > cpumax) {
439+
return -1;
440+
}
441+
} else {
442+
return -1;
443+
}
444+
}
445+
446+
fpm_cpuaffinity_add(&fconf);
447+
448+
token = php_strtok_r(NULL, ";", &buf);
449+
}
450+
451+
fail:
452+
r = fpm_cpuaffinity_set(&fconf);
453+
fpm_cpuaffinity_destroy(&fconf);
454+
return r;
455+
}
419456
#endif
420457

421458
int fpm_unix_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
@@ -443,14 +480,9 @@ int fpm_unix_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
443480
}
444481
}
445482
#if HAVE_FPM_CPUAFFINITY
446-
if (is_root && wp->config->process_cpumask) {
447-
int fcpu, lcpu;
483+
if (wp->config->process_cpumask) {
448484

449-
if (0 > fpm_cpusrange(wp->config->process_cpumask, &fcpu, &lcpu)) {
450-
zlog(ZLOG_SYSERROR, "[pool %s] failed to fpm_cpusrange(%s)", wp->config->name, wp->config->process_cpumask);
451-
return -1;
452-
}
453-
if (0 > fpm_setcpuaffinity(fcpu, lcpu)) {
485+
if (0 > fpm_setcpuaffinity(wp->config->process_cpumask)) {
454486
zlog(ZLOG_SYSERROR, "[pool %s] failed to fpm_setcpuaffinity(%s)", wp->config->name, wp->config->process_cpumask);
455487
return -1;
456488
}
@@ -704,19 +736,9 @@ int fpm_unix_init_main(void)
704736

705737
#if HAVE_FPM_CPUAFFINITY
706738
if (fpm_global_config.process_cpumask) {
707-
if (is_root) {
708-
int fcpu, lcpu;
709-
710-
if (0 > fpm_cpusrange(fpm_global_config.process_cpumask, &fcpu, &lcpu)) {
711-
zlog(ZLOG_SYSERROR, "failed to fpm_cpusrange(%s)", fpm_global_config.process_cpumask);
712-
return -1;
713-
}
714-
if (0 > fpm_setcpuaffinity(fcpu, lcpu)) {
715-
zlog(ZLOG_SYSERROR, "failed to fpm_setcpuaffinity(%s)", fpm_global_config.process_cpumask);
716-
return -1;
717-
}
718-
} else {
719-
zlog(ZLOG_NOTICE, "'process.cpumask' directive is ignored when FPM is not running as root");
739+
if (0 > fpm_setcpuaffinity(fpm_global_config.process_cpumask)) {
740+
zlog(ZLOG_SYSERROR, "failed to fpm_setcpuaffinity(%s)", fpm_global_config.process_cpumask);
741+
return -1;
720742
}
721743
}
722744
#endif

sapi/fpm/php-fpm.conf.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@
129129
; The value can be one cpu id or a range.
130130
;
131131
; Default Value: not set
132-
; process.cpumask = 0-3
132+
; process.cpumask = "4"
133+
; process.cpumask = "0-3"
134+
; process.cpumask = "1-4;8-16;32-48"
133135

134136
;;;;;;;;;;;;;;;;;;;;
135137
; Pool Definitions ;

sapi/fpm/www.conf.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ listen = 127.0.0.1:9000
7878
; The value can be one cpu id or a range.
7979
;
8080
; Default Value: inherits master's cpu affinity
81-
; process.cpumask = 4-7
81+
; process.cpumask = "8"
82+
; process.cpumask = "0-3"
83+
; process.cpumask = "4-7;10-12"
8284

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

0 commit comments

Comments
 (0)