2013/07/03

[nginx] worker_processesとworker_cpu_affinityについて

nginxにはworker_processesというパラメータがあります。 NginxのWikiCoreモジュールのページには、次のような説明があります。


A worker process is a single-threaded process.
If Nginx is doing CPU-intensive work such as SSL or gzipping and you have 2 or more CPUs/cores, then you may set worker_processes to be equal to the number of CPUs or cores.
If you are serving a lot of static files and the total size of the files is bigger than the available memory, then you may increase worker_processes to fully utilize disk bandwidth.
Your OS may schedule all workers on single CPU/core this can be avoided using worker_cpu_affinity.
Nginx has the ability to use more than one worker process for several reasons:

  1. to use SMP
  2. to decrease latency when workers blockend on disk I/O
  3. to limit number of connections per process when select()/poll() is used
The worker_processes and worker_connections from the event sections allows you to calculate maxclients value:
max_clients = worker_processes * worker_connections


何が書いてあるかといえば、

worker prcessは、シングルスレッドなプロセスだよ。
nginxがSSLやgzip圧縮みたいにCPUを専有するような処理をするって場合に、2つ以上のCPUコアがあるなら、worker_processesにはCPUコア数と同じを設定したらいいよ。
大量の静的ファイルをホスティングしていて、ファイルサイズが空きメモリサイズよりも大きくなっちゃうような場合も、worker_processesの値を増やして、ディスク帯域を十分に使えるようにしたらいいよ。
君のOSだと全部のworker_processを1つのCPUコアに割りつけてしまうかもしれないけど、そんな時はworker_cpu_affinityを設定すれば回避できるよ。
nginxは、

  1. SMPを使うため。
  2. ディスクI/Oによる応答遅延を減らすため。
  3. select()/poll()環境下でプロセスごとの接続数を制限するため。
といったいくつかの理由から複数のworker processが使えるようになっているよ。

worker_processesとeventセクションに設定するworker_connectionsから、最大クライアント数を計算できるよ。つまり、
最大クライアント数 = worker_processes * worker_connections
になるよ。

といった内容でしょうか。
つまり、worker_processes を CPUコア数にして、worker_cpu_affinityをきちんと設定しなさいよ、ということで、
僕のサーバは2コアなので、

  worker_processes 2;
  worker_cpu_affinity 0101 1010;
を設定しました。

0 件のコメント: