@@ -359,6 +359,14 @@ static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
359
359
/* }}} */
360
360
361
361
#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
+
362
370
static long fpm_cpumax (void )
363
371
{
364
372
static long cpuid = LONG_MIN ;
@@ -369,53 +377,82 @@ static long fpm_cpumax(void)
369
377
return cpuid ;
370
378
}
371
379
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
397
385
}
398
386
399
- static int fpm_setcpuaffinity ( int min , int max )
387
+ static void fpm_cpuaffinity_add ( struct fpm_cpuaffinity_conf * c )
400
388
{
401
389
#if defined(HAVE_SCHED_SETAFFINITY ) || defined(HAVE_CPUSET_SETAFFINITY )
402
- cpu_set_t cset ;
403
390
int i ;
404
391
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 );
409
395
}
410
396
}
397
+ #endif
398
+ }
411
399
400
+ static int fpm_cpuaffinity_set (struct fpm_cpuaffinity_conf * c )
401
+ {
412
402
#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 );
417
406
#endif
418
407
}
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
+ }
419
456
#endif
420
457
421
458
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) /* {{{ */
443
480
}
444
481
}
445
482
#if HAVE_FPM_CPUAFFINITY
446
- if (is_root && wp -> config -> process_cpumask ) {
447
- int fcpu , lcpu ;
483
+ if (wp -> config -> process_cpumask ) {
448
484
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 )) {
454
486
zlog (ZLOG_SYSERROR , "[pool %s] failed to fpm_setcpuaffinity(%s)" , wp -> config -> name , wp -> config -> process_cpumask );
455
487
return -1 ;
456
488
}
@@ -704,19 +736,9 @@ int fpm_unix_init_main(void)
704
736
705
737
#if HAVE_FPM_CPUAFFINITY
706
738
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 ;
720
742
}
721
743
}
722
744
#endif
0 commit comments