Facebook HipHop for PHP

ที่ Facebook เค้าแปลง php code เป็น c++ และคอมไพล์ด้วย g++ ผลคือการใช้งาน cpu ลดลงประมาณครึ่งหนึ่ง !

http://www.facebook.com/note.php?note_id=280583813919

Today I’m excited to share the project a small team of amazing people and I have been working on for the past two years; HipHop for PHP. With HipHop we’ve reduced the CPU usage on our Web servers on average by about fifty percent, depending on the page. Less CPU means fewer servers, which means less overhead. This project has had a tremendous impact on Facebook. We feel the Web at large can benefit from HipHop, so we are releasing it as open source this evening in hope that it brings a new focus toward scaling large complex websites with PHP. While HipHop has shown us incredible results, it’s certainly not complete and you should be comfortable with beta software before trying it out.

HipHop for PHP isn’t technically a compiler itself. Rather it is a source code transformer. HipHop programmatically transforms your PHP source code into highly optimized C++ and then uses g++ to compile it. HipHop executes the source code in a semantically equivalent manner and sacrifices some rarely used features — such as eval() — in exchange for improved performance. HipHop includes a code transformer, a reimplementation of PHP’s runtime system, and a rewrite of many common PHP Extensions to take advantage of these performance optimizations.

We are proud to say that at this point, we are serving over 90% of our Web traffic using HipHop, all only six months after deployment.

อยากได้มาลองมากเลย T_T

เข้าไปลุยได้เลย OSS ครับ :slight_smile:

น่าเล่นจริงๆ ด้วยครับพี่วัฒน์
ตา Haiping Zhao อย่างกับคนจีน

just saw it in git
will try asap

กระบวนการทำงานยังไงบ้าง ช่วยอธิบายแบบละเอียดให้ฟังหน่อยครับ

ลองลงตัวใหม่ดูแล้วครับ bug เยอะพอสมควร

echo “<?php echo “test” ?>” > test.php

./src/hphp/hphp test.php --keep-tempdir=1 --log=3

running hphp…
creating temporary directory /tmp/hphp_qy7Ddy …
parsing inputs…
parsing ./test.php…
parsing inputs took 0’00" (8 ms) wall time
pre-optimizing…
pre-optimizing took 0’00" (0 ms) wall time
inferring types…
inferring types took 0’00" (0 ms) wall time
post-optimizing…
post-optimizing took 0’00" (0 ms) wall time
creating CPP files…
creating CPP files took 0’00" (59 ms) wall time
compiling and linking CPP files…
compiling and linking CPP files took 0’56" (56374 ms) wall time
running executable /tmp/hphp_qy7Ddy/program --file test.php…
testall files saved in /tmp/hphp_qy7Ddy …
running hphp took 0’57" (57056 ms) wall time

/tmp/hphp_qy7Ddy/program --file test.php

test

ls -al /tmp/hphp_qy7Ddy/

total 29060
drwx------

ผมยัง compile ไม่ผ่านเลย T_T

/home/toey/hiphop-php/src/lib/parser/hphp.y:673: error: expected `;’ before ‘}’ token

http://github.com/facebook/hiphop-php/blob/master/src/lib/parser/hphp.y

ที่พบส่วนมาก เป็น bug ใน source code อ่ะ

compile ตัว hiphop น่ะ ยังไม่ผ่านเลย T_T

lib ไม่ตรงกัน

lib ตัวไหนเหรอ

น่าลองมาก เดี๋ยวว่างๆ ลองเล่นบ้างครับ

ทางสว่าง><

วันนี้ลองโหลดตัวใหม่มา make ใหม่ แรงกว่าเก่าอีกครับ และ ลงง่ายกว่าเดิมมากครับ test on CentOS release 5.4 x86_64

ทดสอบเรียกจาก hiphop-php

simple

bookmark ๆๆๆๆๆ

thx ครับผม

ข้อมูลเพิ่มเติมครับ
http://www.phpclasses.org/blog/post/117-PHP-compiler-performance.html

HipHop for PHP

HipHop is a PHP compiler project led by Haiping Zhou. Haiping was born in China but nowadays he works for Facebook as a Senior Software Engineer in their United States office.

HipHop transforms PHP source code into C++ code. Then the resulting C++ code can be compiled into native binary executable code with a C++ compiler, like for instance GCC.

HipHop was developed to make more efficient use of the Facebook servers. Currently, over 90% of Facebook Web traffic is handled by PHP code compiled by the HipHop compiler.

HipHop compiles the code of a set of PHP scripts into an executable program that works as a multi-threaded Web server. This way, the compiled PHP code not only runs faster but also makes more efficient use of server machine memory.

A multi-threaded Web server uses less memory because it uses a single memory pool for all simultaneous requests. This is different from using multi-process Web servers, like when you use Apache in pre-fork mode. This is the mode that most Apache based PHP sites use. In this mode, each request is handled by a separate OS process. Each process has its own memory pool.

The main problem of using multi-process Web servers is that when a process uses a large chunk of memory, that memory is not returned to the OS until the process exits. Even if subsequent requests handled by the same process do not need so much memory, the unused memory space cannot be reused by other processes that may be handling other simultaneous requests.

Apache can be forced to kill pre-forked processes once in a while in order to perform memory recycling, but there is always a waste of memory until the process exit happens.

The memory usage details are very important for sites that require more than one server machine. The memory used by the Web server processes determines how many simultaneous requests each Web server machine can handle.

For instance, if you have a Web server with 1GB of RAM and each PHP request takes 10MB of RAM, in theory it can handle less than 100 of simultaneous requests. If all programs running in the same machine use more than the physically available RAM, the OS has to use virtual memory and the machine starts slowing down, as it has to swap physical memory blocks with virtual memory segments in disk.

This is why a server that takes an excessive amount of simultaneous requests can be halted, causing what is known as a DOS: Denial Of Service. With Apache, you can use the configuration directive MaxClients to limit the number of simultaneous requests. That will prevent the machine to halt, but the Web server starts queuing incoming requests. This means that the requests may be handled with a great delay or even be ignored.

This is a matter that was previously discussed in a past article about the use of multi-threaded Web servers for handling high traffic. Notice that in that article it is recommended the use of multi-threaded Web servers only for serving static files like images, CSS and Javascript.

The new aspect of HipHop PHP compiler is that Facebook developers have made an effort to convert PHP extensions in such way that they are thread-safe. This means that they can run without crashing multi-threaded Web server programs, which is what the HipHop compiler generates.