# Add support for setting worker processes and CPU affinity automatically

**URL:** <https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681>\
**Category:** NGINX\
**Created:** [January 12, 2012, 4:38pm UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681 "2012-01-12T16:38:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 12, 2012, 4:38pm UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/1 "2012-01-12T16:38:01Z")

</div>

Hi,

It’s a little bit inconvenient to set the CPU affinities with  
many-core-system. For example, to fully utilize a 16-core Linux box, we  
have to write something like this:  
worker\_processes 16;  
worker\_cpu\_affinity 1000000000000000 0100000000000000 0010000000000000  
0001000000000000 0000100000000000 0000010000000000 0000001000000000  
0000000100000000 0000000010000000 0000000001000000 0000000000100000  
0000000000010000 0000000000001000 0000000000000100 0000000000000010  
0000000000000001;

This patch will make life much easier:  
worker\_processes auto;  
worker\_cpu\_affinity auto;

Please see the attachment for more detail.

Regards,

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 12, 2012, 5:07pm UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/2 "2012-01-12T17:07:39Z")

</div>

Hi Joshua,

nice work indeed, thanks!

Do you have a practical example when such explicit binding  
does better work than OS scheduler?

–  
Maxim K.  
+7 (910) 4293178

> **[Advanced Load Balancer, Web Server, & Reverse Proxy - NGINX](https://www.nginx.com/)**
>
> NGINX accelerates content and application delivery, improves security, and facilitates availability and scalability for the busiest websites on the Internet.

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 12, 2012, 5:42pm UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/3 "2012-01-12T17:42:22Z")

</div>

Hi Maxim,

On Fri, Jan 13, 2012 at 12:07 AM, Maxim K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> [http://nginx.com/](http://nginx.com/)

Thanks!  
Actually we did a test against the CPU affinity feature before and found  
it  
was a little bit better than what the OS scheduler did, not too much  
though. This is as expected since there will be less CPU cache missing  
with  
this feature turned on.

Regards,

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 12, 2012, 6:54pm UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/4 "2012-01-12T18:54:49Z")

</div>

On Thursday 12 January 2012 19:37:15 Joshua Z. wrote:

> This patch will make life much easier:  
> worker\_processes auto;  
> worker\_cpu\_affinity auto;
> 
> Please see the attachment for more detail.

You use “ngx\_ncpu” global variable and for Linux you set it by  
sysconf(\_SC\_NPROCESSORS\_ONLN).

On freeBSD and MacOS systems it set by sysctls(hw.ncpu), and it is equal  
to 1 on others.

Which means that your “auto” settings are very inaccurate. Sometimes the  
“worker\_processes” directive will be equal to 2 \* cores or more,  
sometimes  
it will be equal to 1, and if you’re lucky, then it will be equal to  
real  
CPU cores.

So, I don’t think that this patch makes life easier. In fact, it creates  
a  
false sense of optimal worker\_processes setting. Even if ngx\_ncpu would  
be  
always equal to real hardware cores number, it wouldn’t be optimal for  
every  
use cases.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 13, 2012, 11:15am UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/5 "2012-01-13T11:15:43Z")

</div>

> Setting CPU affinity is available on Linux systems only. Please  
> correct me if I’m wrong.

That’s wrong.

Just some start points:

FreeBSD: cpuset\_setaffinity(2)  
Solaris: probably better to start from processor\_bind(2) man page.  
HP/UP: pset\_bind(2), psrset(1m)  
AIX: bindprocessor()

Sure other systems with more or less advances \*MP support have a  
similar functionality.

–  
Maxim K.  
+7 (910) 4293178

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 13, 2012, 11:28am UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/6 "2012-01-13T11:28:06Z")

</div>

Hi,

> > Setting CPU affinity is available on Linux systems only. Please  
> > correct me if I’m wrong.
> 
> That’s wrong.

I believe that Joshua meant that workers CPU affinity _in nginx_  
works only on Linux right now (which is true).

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 13, 2012, 11:04am UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/7 "2012-01-13T11:04:35Z")

</div>

Hi,

On Fri, Jan 13, 2012 at 1:53 AM, Valentin V. Bartenev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Thursday 12 January 2012 19:37:15 Joshua Z. wrote:

[snip]

Thank you very much for the review.

You use “ngx\_ncpu” global variable and for Linux you set it by

> sysconf(\_SC\_NPROCESSORS\_ONLN).
> 
> On freeBSD and MacOS systems it set by sysctls(hw.ncpu), and it is equal  
> to 1 on others.

Setting CPU affinity is available on Linux systems only. Please correct  
me  
if I’m wrong.

> Which means that your “auto” settings are very inaccurate. Sometimes the  
> “worker\_processes” directive will be equal to 2 \* cores or more, sometimes  
> it will be equal to 1, and if you’re lucky, then it will be equal to real  
> CPU cores.

This is mostly caused by the hyper-threading feature of some CPUs. I  
don’t  
think this is an issue if hyper-threading is turned on. Sure we need to  
find a better way to detect the number of CPUs on a system.

> So, I don’t think that this patch makes life easier. In fact, it creates a  
> false sense of optimal worker\_processes setting. Even if ngx\_ncpu would be  
> always equal to real hardware cores number, it wouldn’t be optimal for  
> every  
> use cases.

It’s not ‘optimal’, but ‘automatic’. This patch still supports setting  
worker processes and CPU affinities mannually as what the standard Nginx  
does 😉

Regards,

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 13, 2012, 11:34am UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/8 "2012-01-13T11:34:19Z")

</div>

On Friday 13 January 2012 14:04:11 Joshua Z. wrote:

> > sysconf(\_SC\_NPROCESSORS\_ONLN).
> > 
> > On freeBSD and MacOS systems it set by sysctls(hw.ncpu), and it is equal  
> > to 1 on others.
> 
> Setting CPU affinity is available on Linux systems only. Please correct me  
> if I’m wrong.

You’re right.

But I generally said about “ngx\_ncpu”, that also affects “auto” setting  
in your  
patch for the “worker\_processes” directive.

> > Which means that your “auto” settings are very inaccurate. Sometimes the  
> > “worker\_processes” directive will be equal to 2 \* cores or more,  
> > sometimes it will be equal to 1, and if you’re lucky, then it will be  
> > equal to real CPU cores.
> 
> This is mostly caused by the hyper-threading feature of some CPUs. I don’t  
> think this is an issue if hyper-threading is turned on. Sure we need to  
> find a better way to detect the number of CPUs on a system.

Yeap, we need to find a better way. And I don’t think the solution will  
be simple  
and it is worth to be implemented in a web server.

> > So, I don’t think that this patch makes life easier. In fact, it creates  
> > a false sense of optimal worker\_processes setting. Even if ngx\_ncpu  
> > would be always equal to real hardware cores number, it wouldn’t be  
> > optimal for every  
> > use cases.
> 
> It’s not ‘optimal’, but ‘automatic’. This patch still supports setting  
> worker processes and CPU affinities mannually as what the standard Nginx  
> does 😉

So, when you need some automation for configuration and deployment, the  
better  
solution would be using proper software for that task, isn’t it? Like  
Puppet,  
for example.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 13, 2012, 2:19pm UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/9 "2012-01-13T14:19:17Z")

</div>

On 1/13/12 2:27 PM, Piotr S. wrote:

> Hi,
> 
> > > Setting CPU affinity is available on Linux systems only. Please  
> > > correct me if I’m wrong.
> > 
> > That’s wrong.
> 
> I believe that Joshua meant that workers CPU affinity _in nginx_  
> works only on Linux right now (which is true).

Ah, I see, thanks.

I just wonder how useful this option in real environment.

–  
Maxim K.  
+7 (910) 4293178

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 13, 2012, 2:08pm UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/10 "2012-01-13T14:08:45Z")

</div>

Hi,

On Fri, Jan 13, 2012 at 6:33 PM, Valentin V. Bartenev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Friday 13 January 2012 14:04:11 Joshua Z. wrote:

[snip]

> So, when you need some automation for configuration and deployment, the  
> better  
> solution would be using proper software for that task, isn’t it? Like  
> Puppet,  
> for example.

OK.  
If this patch would not be accepted, could you please also review the  
first  
one?  
Thank you.

Regards,

---

<div class="post-metadata">

**Author:** ![Joshua\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ccd318/32.png) [@Joshua\_Z](https://www.ruby-forum.com/u/Joshua_Z)\
**Post date:** [January 13, 2012, 3:07pm UTC](https://www.ruby-forum.com/t/add-support-for-setting-worker-processes-and-cpu-affinity-automatically/215681/11 "2012-01-13T15:07:04Z")

</div>

Hello!

On Fri, Jan 13, 2012 at 09:08:25PM +0800, Joshua Z. wrote:

> > So, when you need some automation for configuration and deployment, the  
> > better  
> > solution would be using proper software for that task, isn’t it? Like  
> > Puppet,  
> > for example.
> 
> OK.  
> If this patch would not be accepted,

I wouldn’t say it won’t be accepted at all, I think we need  
something like this, but probably not by default.

> could you please also review the first one?

I’m currently looking into it.

Maxim D.
