Facebook開源項目Hiphop-php使用介紹(原創)
作者:餘超 EMail:yuchao86@gmail.com
Facebook開源的Hiphop-php能將php編譯成C++程式,提高網站的運行效率,在github上有最新的hiphop-php的原始碼。
如下是Hiphop-php的作者Haiping Zhao(趙海平)對Hiphop的定義:
HipHop是一個原始碼轉換器。它將PHP代碼轉換為高度最佳化的C++代碼,然後再使用g++編譯器編譯。它可以保持語義等效地執行原始碼,但犧牲了一些很少會使用到的功能,比如 eval()。為了進一步的提升效能,HipHop包含一個code transformer,一個重新實現的PHP runtime系統,並利用這些效能的最佳化的優勢,對許多共同的PHP擴充進行了重寫。
編譯的時候碰到的很多問題,中間還有很多包需要裝,但基本上是按照wiki的步驟進行的:
=====================================================================================
1. 安裝關聯庫,在編譯過程中還發現有其它包需要安裝請使用命令sudo yum -y install **安裝:
[yuchao@yuchao-Latitude-E5410 hiphop-php]$ls
bin CMake CMakeCache.txt CMakeFiles CMakeLists.txt configure doc LICENSE.PHP LICENSE.ZEND local phpt README.md src
[yuchao@yuchao-Latitude-E5410 hiphop-php]$pwd
/home/yuchao/dev/hiphop-php
[yuchao@yuchao-Latitude-E5410 hiphop-php]$sudo yum -y install git cmake boost pcre-devel libicu-devel\
libmcrypt-devel oniguruma-devel mysql-devel gd-devel boost-devel\
libxml2-devel libcap-devel binutils-devel flex bison\
expat-devel
=====================================================================================
2. 下載Hiphop-php安裝包,以及依賴庫libevent, curl,從二進位可執行檔能夠運行請求都依賴於這兩個庫。
1) 擷取Hiphop-php原始碼.
[yuchao@yuchao-Latitude-E5410 hiphop-php]$git clone git://github.com/facebook/hiphop-php.git
2) 擷取編譯所需要的各種庫,re2c,tbb,curl和libevent等等。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$wget "http://downloads.sourceforge.net/project/re2c/re2c/0.13.5/re2c-0.13.5.tar.gz?use_mirror=cdnetworks-us-2"
[yuchao@yuchao-Latitude-E5410 hiphop-php]$wget "http://www.threadingbuildingblocks.org/uploads/77/142/2.2/tbb22_20090809oss_src.tgz"
[yuchao@yuchao-Latitude-E5410 hiphop-php]$wget "http://curl.haxx.se/download/curl-7.20.0.tar.bz2"
[yuchao@yuchao-Latitude-E5410 hiphop-php]$wget "http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz"
3. 解壓縮以上下載的各種依賴包,gz包用xvzf而bz2包用xvjf
[yuchao@yuchao-Latitude-E5410 hiphop-php]$tar xvjf curl-7.20.0.tar.bz2
[yuchao@yuchao-Latitude-E5410 hiphop-php]$tar xvzf libevent-1.4.13-stable.tar.gz
[yuchao@yuchao-Latitude-E5410 hiphop-php]$tar xvzf re2c-0.13.5.tar.gz
[yuchao@yuchao-Latitude-E5410 hiphop-php]$tar xvzf tbb22_20090809oss_src.tgz
4. 安裝解壓出來的各種依賴包,編譯過程中如果出現錯誤,請自己根據提示資訊拍錯,那樣你學到的東西更多。
1) 安裝Intel並行線程庫
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd tbb22_20090809oss
[yuchao@yuchao-Latitude-E5410 hiphop-php]$gmake
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cp ./build/*_release/*.so.* /usr/lib/
[yuchao@yuchao-Latitude-E5410 hiphop-php]$ldconfig
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd ..
2) 安裝比lex更有效率的re2c嵌入式的詞法解析工具.
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd re2c-0.13.5
[yuchao@yuchao-Latitude-E5410 hiphop-php]$mkdir -p /usr/local/re2c
[yuchao@yuchao-Latitude-E5410 hiphop-php]$./configure –prefix=/usr/local/re2c
[yuchao@yuchao-Latitude-E5410 hiphop-php]$make && make install
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd ..
3) 安裝用於處理網路IO,timer,signal的事件觸發機制處理庫libevent,
可以參考我的另一篇文章《libevent源碼分析》http://blog.csdn.net/cyberexp2008/article/details/5674693
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd libevent-1.4.13-stable
必須用hiphop-php/src/third_party/目錄下對應libevent版本號碼打好補丁。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cp ../hiphop-php/src/third_party/libevent-1.4.13.fb-changes.diff ./
[yuchao@yuchao-Latitude-E5410 hiphop-php]$patch -p1 < libevent-1.4.13.fb-changes.diff
[yuchao@yuchao-Latitude-E5410 hiphop-php]$./configure --prefix=/usr/local/libevent/
[yuchao@yuchao-Latitude-E5410 hiphop-php]$make && make install
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd ..
4) 安裝用於處理URL文法在命令列方式下工作的檔案傳輸工具CURL,如需瞭解更多請自己Google之。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd curl-7.20.0
同樣必須用hiphop-php/src/third_party/目錄下對應libcurl版本號碼打好補丁。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cp ../hiphop-php/src/third_party/libcurl.fb-changes.diff ./
[yuchao@yuchao-Latitude-E5410 hiphop-php]$patch -p1 < libcurl.fb-changes.diff
[yuchao@yuchao-Latitude-E5410 hiphop-php]$./configure --prefix=/usr/local/curl
[yuchao@yuchao-Latitude-E5410 hiphop-php]$make && make install
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd ..
==========================================================================================
5. 接下來就開始安裝hiphop-php,在正式編譯之前,需要設定CMAKE_PREFIX_PATH,HPHP_HOME和HPHP_LIB環境變數
1) 設定環境變數,cmake尋找libevent與curl時的路徑,如果設定不正確,我在編譯的過程中提示CMake Error: The following variables are used in this project, but they are set to NOTFOUND.Please set them or make sure they are set and tested correctly in the CMake files:
CCLIENT_INCLUDE_PATH (ADVANCED)的錯誤資訊,請自己排錯。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$export CMAKE_PREFIX_PATH=/usr/local/libevent/
[yuchao@yuchao-Latitude-E5410 hiphop-php]$export CMAKE_PREFIX_PATH=$CMAKE_PREFIX_PATH:/usr/local/curl/
2) 安裝hiphop-php
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd hiphop-php
[yuchao@yuchao-Latitude-E5410 hiphop-php]$git submodule init
[yuchao@yuchao-Latitude-E5410 hiphop-php]$git submodule update
[yuchao@yuchao-Latitude-E5410 hiphop-php]$export HPHP_HOME=`pwd`
[yuchao@yuchao-Latitude-E5410 hiphop-php]$export HPHP_LIB=`pwd`/bin
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cmake .
[yuchao@yuchao-Latitude-E5410 hiphop-php]$make
===============================================================================
安裝完成之後,我們就可以開始使用hphp命令了,下面來簡單的測試一個php檔案,代碼如下:
運行php編譯檔案參考了文章如下:
https://github.com/facebook/hiphop-php/wiki/Running-HipHop
<?php
$i = 0;
for($j = 0; $j < 1000000; $j++)
$i += $j;
echo $i, "n";
?>
用hphp進行編譯:
[yuchao@yuchao-Latitude-E5410 hiphop-php]$hphp test.php --keep-tempdir=1 --log=3
提示產生新的可執行檔
[yuchao@yuchao-Latitude-E5410 hiphop-php]$/tmp/hphp_c9sbnG/program
做一下已耗用時間對比:
[yuchao@yuchao-Latitude-E5410 hiphop-php]$time php test.php
499999500000
real 0m0.307s
user 0m0.299s
sys 0m0.007s
[yuchao@yuchao-Latitude-E5410 hiphop-php]$time /tmp/hphp_c9sbnG/program
499999500000
real 0m0.140s
user 0m0.076s
sys 0m0.006s
可以看到,經hiphop編譯後的php,執行時間幾乎快了一倍。特此聲明
參考文章:
[1]. http://www.ioncannon.net/programming/918/building-hiphop-php-for-fedora-12-on-64-bit-and-32-bit-systems/
[2]. https://github.com/facebook/hiphop-php/wiki/building-and-installing
[3]. https://github.com/facebook/hiphop-php/wiki/Installing-or-Building-HipHop-PHP-via-RPM-on-CentOS-5