Translate: 
EnglishFrenchGermanItalianPolishPortugueseRussianSpanish

Drupal 7 and HipHop for PHP: compilation

Shortly after the release of HipHop for PHP, many articles about its possibilities and functions it can offer to existing PHP scripts appeared on the internet, but the most of them described problems which disrupted the compilation process of two popular CMS systems: Drupal and Joomla. They were caused by the poor quality of the PHP code in which these systems were written.

These articles are outdated since the release of new versions: Drupal 7 and Joomla 1.6.

Has the quality of PHP code improved? Is it now possible to compile the Drupal system without any problems?
This series of articles will answer these questions.

Background

I tested the Drupal 7.0 system. I used MySQL 5.1.47 to store data. The database and PHP scripts were installed on a test server controlled by Fedora 12.

I used the newest (as on 29.04.2011) HipHop for PHP (the installation process on CentOS 5.x is described in this article).

Drupal’s file list

Before I could conduct any tests, I had to generate a list of files to be compiled. I’ve found the necessary data in htaccess file included with Drupal. It has all extensions of files which contain PHP code.

I used the following command to generate the list:

cd drupal
find . \( -name "*.php" -o -name "*.engine" -o -name "*.inc" -o -name "*.info" -o -name "*.install" -o -name "*.module" -o -name "*.profile" -o -name "*.test" -o -name "*.theme" -o -name "*.tpl" \) > files.list

I worked on the Drupal system few years ago and saw how the files are structured, but I didn’t expect so many different extensions, in which the PHP code can be contained. For some people it is a matter of habit, for others it can be a difficulty in maintaining the system.

PHP code compilation

I used the following command to speed up the compilation process:

export MAKEOPTS=' -j4 '

Setting the $MAKEOPTS variable to “-j4″ forces the HipHop for PHP to compile PHP scripts in four parallel cc1plus processes. When you choose the optimal value, you should consider available RAM and the number of CPUs on your server (the recommended configuration for the value specified in the command above is 2 GB RAM and 4 CPUs).

You can also speed up the compilation process by using the distcc application, which specializes in distributing compilations through the network (because of hardware limitations I’ll describe this mechanism in another article).

In order to compile the Drupal I used the following command in Drupal’s home directory:

date && ~/hiphop/hiphop-php/src/hphp/hphp\
 --keep-tempdir=1 \
 --log=3 \
 --input-list=files.list \
 --include-path="." \
 --force=1 \
 --cluster-count=16 \
 -v "AllDynamic=true" \
 -v "AllVolatile=true"

Because the compilation process on the test server takes about 13 minutes, I used date command to estimate how long it will take for the operation to finish.

Description of used switches:

  1. -keep-tempdir=1
    It forces the system to save the temporal directory in which the PHP script was compiled. It allows you to see source codes in C++ generated by HipHop for PHP.
  2. -log=3
    Increases the level of reporting of actions which are performed by HipHop for PHP during the compilation process.
  3. -input-list=files.list
    Loads the list of PHP scripts from files.list which are going to be compiled.
  4. -force=1
    It forces the compilation of code regardless of any errors found in it. Without it HipHop for PHP stops the process whenever it finds an illegal function (i.e. eval()) or other errors in code (even if these aren’t going to be ran later). Later you can read these foundings in CodeError.js, which will be saved in the directory with the executable file.
  5. -include-path=”.”
    It sets the current directory as a directory, from which pathes to included by PHP files begin.
  6. -v “AllDynamic=true”
    It enables support for dynamic functions calls (i.e. with call_user_func()). This switch slows down the application’s performance.
  7. -v “AllVolatile=true”
    It allows for dynamic declaration of classes and functions (used in conditions: if(!class_exists(„Test”)) { }). It is advised to use this switch only if the PHP code needs it, because it slows down the compiled application’s performance.
  8. -cluster-count=16
    Without this switch, there will be one *.cpp file corresponding to each PHP file. If objects contain a very large number of files, the compiler’s performance will significantly drop. The value of “16″ means that PHP scripts will be grouped in up to 16 *.cpp files. The optimal value speeds up the parralel compilation (local or even with distcc utility).

Running CMS system

Drupal system must work as a HTTP server. Fortunately, it’s not a problem for HipHop for PHP translator. Depending on input arguments, compiled PHP scripts can be ran in two different modes: as console applications (default) or HTTP servers (switch -m “server”):

/tmp/hphp_AJSp1P/program -m server -p 8080 \
 -v "Server.SourceRoot=`pwd`" \ 
 -v "Server.DefaultDocument=index.php" \
 -v "Log.Level=Verbose" \
 -v "Log.NoSilencer=true" \
 -v "Log.AlwaysLogUnhandledExceptions=true" \
 -c $HPHP_HOME/bin/mime.hdf &

Path /tmp/hphp_AJSp1P/program is temporal and indicates where the program generated by HipHop for PHP is. The directory’s path changes after every compilation, so you should be aware of this when you try to launch the application.

Description of switches I used:

  1. -p 8080
    It launches a HTTP Server on port 8080.
  2. -v „Server.SourceRoot=`pwd`”
    It allows you to trick the compiled PHP scripts, so that when realpath() function is used, they’re informed that they were started in a directory with an uncompiled CMS system (despite the fact that the executables are located elsewhere).
  3. -v „Server.DefaultDocument=index.php”
    It sets index.php to be a default file launched when serving short URL addresses (i.e.: htttp://localhost/).
  4. -v „Log.Level=Verbose”
    It turns on the highest level of logging PHP events in the compiled script. It makes debugging poorly written applications easier.
  5. -v „Log.NoSilencer=true”
    It forces reporting of errors and warnings even if they were ignored in the code by the “@” operator. It’s a very useful feature when you expect problems with the compiled application.
  6. -v „Log.AlwaysLogUnhandledExceptions=true”
    It turns on logging of all unserved exceptions. This mechanism is a great addition when the PHP application did not serve the exception but had its own error handling ignoring this event)
  7. -c $HPHP_HOME/bin/mime.hdf
    It loads on the HTTP server a list of MIME types for files which are going to be sent to the user. Without this command, graphics and CSS styles won’t load correctly in a WWW browser.

As you can see, I turned on the most of the available debugging flags before I launched the application. This is due to a serious concern that launching this CMS system will be problematic.

Test of the Drupal system

After the compilation of scripts and the launch of the HTTP server, it’s time for tests!
The following command is enough for a fast check:

curl http://127.0.0.1:8080/

Unfortunately, a blank page will appear and the application’s log will show critical errors. It turns out, that Drupal’s source code is still not compatible with HipHop for PHP compiler. A modification of its original source code and a recompilation of the project are neccessary to successfuly display the page.

In following articles I’m going to describe encountered problems with the PHP compatibility that did not allow to compile the system and how to perform all neccessary modifications of the code to make the application run.

*** Thanks to Piotr Graniszewski for English translation ***


Other articles about HipHop for PHP


Tags: , , ,

One Response to “Drupal 7 and HipHop for PHP: compilation”

  1. Deanna Bjornstrom says:

    #Drupal has not been great for designers so far, and it shows in many projects.

Leave a Reply