Ruby Enterprise Editionインストールで性能改善

結論から言いますと、49.3% もメモリ使用率を削減できました!!
以下、技術的な情報になりますので興味がある方だけ読んでください。

VPS環境にRuby Enterprise Edition(version 1.8.7-2009.10)をインストールしました。
VPS上では複数のRailsアプリケーションを動作させています。
最大メモリは十分にある環境なのですが、必要時に割り当てられるメモリなので、一瞬でも枯渇する機会を少なくするために導入しました。

まずは下記サイトよりダウンロード。
http://www.rubyenterpriseedition.com/

# wget http://rubyforge.org/frs/download.php/66162/ruby-enterprise-1.8.7-2009.10.tar.gz

次に展開してインストールです。

# tar zxvf ruby-enterprise-1.8.7-2009.10.tar.gz
# cd ruby-enterprise-1.8.7-2009.10
# ./installer
./platform_info.rb:224: warning: Insecure world writable dir /home/kembo in PATH, mode 040777
Welcome to the Ruby Enterprise Edition installer
This installer will help you install Ruby Enterprise Edition 1.8.7-2009.10.
Don't worry, none of your system files will be touched if you don't want them
to, so there is no risk that things will screw up.

You can expect this from the installation process:

  1. Ruby Enterprise Edition will be compiled and optimized for speed for this
     system.
  2. Ruby on Rails will be installed for Ruby Enterprise Edition.
  3. You will learn how to tell Phusion Passenger to use Ruby Enterprise
     Edition instead of regular Ruby.

Press Enter to continue, or Ctrl-C to abort.

Checking for required software...

 * C compiler... found at /usr/bin/gcc
 * C++ compiler... found at /usr/bin/g++
 * The 'make' tool... found at /usr/bin/make
 * Zlib development headers... found
 * OpenSSL development headers... found
 * GNU Readline development headers... found
                                                                                      • -
Target directory Where would you like to install Ruby Enterprise Edition to? (All Ruby Enterprise Edition files will be put inside that directory.) [/opt/ruby-enterprise-1.8.7-2009.10] :
                                                                                      • -
Compiling and optimizing the memory allocator for Ruby Enterprise Edition In the mean time, feel free to grab a cup of coffee. ./configure --prefix=/opt/ruby-enterprise-1.8.7-2009.10 --disable-dependency-tracking checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes 省略

Cコンパイラなどのインストールチェック後、インストール先ディレクトリを入力(上記例ではデフォルトのまま)。
インストールが開始されます。
途中次の画面で止まります。

                                                                                      • -
Warning: some libraries could not be installed The following gems could not be installed, probably because of an Internet connection error: * sqlite3-ruby * pg These gems are not required, i.e. Ruby Enterprise Edition will work fine without them. But most people use Ruby Enterprise Edition in combination with Phusion Passenger and Ruby on Rails, which do require one or more of the aforementioned gems, so you may want to install them later. To install the aforementioned gems, please use the following commands: * /opt/ruby-enterprise-1.8.7-2009.10/bin/ruby /opt/ruby-enterprise-1.8.7-2009.10/bin/gem install sqlite3-ruby * /opt/ruby-enterprise-1.8.7-2009.10/bin/ruby /opt/ruby-enterprise-1.8.7-2009.10/bin/gem install pg Press ENTER to show the next screen.

SQL LightやPostgre SQLを使う場合は指示に従ってインストールしましょう的なメッセージです。
インストールコマンドまで表示されて丁寧ですね。
passengerのインストールも同様ですが、とてもユーザビリティの高いインストーラで関心します。

インストールが成功すると下記画面が表示されます。

                                                                                      • -
Ruby Enterprise Edition is successfully installed! If want to use Phusion Passenger (http://www.modrails.com) in combination with Ruby Enterprise Edition, then you must reinstall Phusion Passenger against Ruby Enterprise Edition, as follows: /opt/ruby-enterprise-1.8.7-2009.10/bin/passenger-install-apache2-module Make sure you don't forget to paste the Apache configuration directives that the installer gives you. If you ever want to uninstall Ruby Enterprise Edition, simply remove this directory: /opt/ruby-enterprise-1.8.7-2009.10 If you have any questions, feel free to visit our website: http://www.rubyenterpriseedition.com Enjoy Ruby Enterprise Edition, a product of Phusion (www.phusion.nl) :-)

既存のRubyには一切影響を与えないという点と、アンインストール方法(ディレクトリを削除するだけ)が表示されています。

次はpassengerのインストールです。
この作業はインストールしたEnterprise Rubyを使用して行います。

/opt/ruby-enterprise-1.8.7-2009.10/bin/passenger- install-apache2-module

インストール自体は通常のpassengerと同じです。
私の環境の場合はここでエラーとなりました。

省略
virtual memory exhausted: Cannot allocate memory
rake aborted!
Command failed with status (1): [g++ -Iext -Iext/common -fPIC -DLINUX=2 -D_...]
/opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/passenger-2.2.8/Rakefile:172:in `define_common_library_task'
(See full trace by running task with --trace)

                                                                                      • -
省略

virtual memory exhausted: Cannot allocate memory と怒られています。
topコマンド+Shift M(Memory使用量でソート)でメモリの使用状況を確認するとapacheが700MBほど使っていました。
夜間だったこともあり、一時的にapacheを停止して再度チャレンジ。

                                                                                      • -
The Apache 2 module was successfully installed. Please edit your Apache configuration file, and add these lines: LoadModule passenger_module /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so PassengerRoot /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/passenger-2.2.8 PassengerRuby /opt/ruby-enterprise-1.8.7-2009.10/bin/ruby After you restart Apache, you are ready to deploy any number of Ruby on Rails applications on Apache, without any further Ruby on Rails-specific configuration! Press ENTER to continue.
                                                                                      • -
Deploying a Ruby on Rails application: an example Suppose you have a Rails application in /somewhere. Add a virtual host to your Apache configuration file and set its DocumentRoot to /somewhere/public: ServerName www.yourhost.com DocumentRoot /somewhere/public # <-- be sure to point to 'public'! AllowOverride all # <-- relax Apache security settings Options -MultiViews # <-- MultiViews must be turned off And that's it! You may also want to check the Users Guide for security and optimization tips, troubleshooting and other useful information: /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/passenger-2.2.8/doc/Users guide Apache.html Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-) http://www.modrails.com/ Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.

やりました。成功です。

次は既存のRuby環境をEnterprise Ruby上にインストールします。
Enterprise RubyとEnterprise Rubyのgemを使います。

# /opt/ruby-enterprise-1.8.7-2009.10/bin/ruby /opt/rruby-enterprise-1.8.7-2009.10/bin/gem install rails -v=2.3.2 --no-ri --no-rdoc
Successfully installed activesupport-2.2.2
Successfully installed activerecord-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activeresource-2.2.2
Successfully installed rails-2.2.2
6 gems installed

全てのgemをインストールする必要があります。
次にapacheの設定です。
次の3行を旧passengerの設定と置き換えます。

#   LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
#   PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.8
#   PassengerRuby /usr/local/bin/ruby

   LoadModule passenger_module /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
   PassengerRoot /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/passenger-2.2.8
   PassengerRuby /opt/ruby-enterprise-1.8.7-2009.10/bin/ruby

ここまででインストール完了です。

いよいよ性能測定です。
apacheの設定ファイルを再読み込みする前に性能を取得しておきましょう。
性能データの取得にはpassengerのpassenger-memory-statsコマンドを使います。
ちなみに通常のruby版もEnterpirse ruby版もコマンドに違いは無いようです。
私は同じ負荷をかけながら性能データを取得しました。


通常のRuby

3330  16.6 MB   0.4 MB   /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/ApplicationPoolServerExecutable 0 /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.8/bin/passenger-spawn-server  /usr/local/bin/ruby  /tmp/passenger.31799
3331  44.5 MB   12.6 MB  Passenger spawn server
3368  186.2 MB  22.2 MB  Passenger ApplicationSpawner: /var/www/location/html/current
3397  186.3 MB  22.3 MB  Rails: /var/www/location/html/current
3400  186.2 MB  22.8 MB  Rails: /var/www/location/html/current
3404  187.5 MB  74.7 MB  Passenger ApplicationSpawner: /var/www/lovechic/html/current
3431  199.8 MB  86.6 MB  Rails: /var/www/lovechic/html/current
3433  199.8 MB  86.6 MB  Rails: /var/www/lovechic/html/current
3534  189.8 MB  74.9 MB  Rails: /var/www/lovechic/html/current
3554  189.8 MB  74.9 MB  Rails: /var/www/lovechic/html/current
### Processes: 10
### Total private dirty RSS: 477.94 MB


Enterprise Ruby

3832  16.6 MB   0.4 MB   /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/ApplicationPoolServerExecutable 0 /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/passenger-2.2.8/bin/passenger-spawn-server  /opt/ruby-enterprise-1.8.7-2009.10/bin/ruby  /tmp/passenger.31799
3833  107.7 MB  15.5 MB  Passenger spawn server
3889  215.9 MB  26.4 MB  Passenger ApplicationSpawner: /var/www/location/html/current
3899  217.9 MB  27.9 MB  Rails: /var/www/location/html/current
3902  217.9 MB  27.8 MB  Rails: /var/www/location/html/current
3912  196.5 MB  22.9 MB  Passenger ApplicationSpawner: /var/www/lovechic/html/current
3934  207.7 MB  52.2 MB  Rails: /var/www/lovechic/html/current
5133  198.5 MB  23.3 MB  Rails: /var/www/lovechic/html/current
5140  198.5 MB  22.9 MB  Rails: /var/www/lovechic/html/current
5146  198.5 MB  23.0 MB  Rails: /var/www/lovechic/html/current
### Processes: 10
### Total private dirty RSS: 242.32 MB

通常のRuby:477.94MB
Enterprise Ruby:242.32MB

なんと、49.3%もメモリ使用率が減りました!ものすごい改善率です。
これで処理能力も上がっているというのですから、使わない手はありません。
Enterprise Ruby Editionはruby1.8.7しか出ていませんので、その点はご注意を。