Monday Oct 26, 2009

memsession 1.0.1 stable is now available

Install it from pecl using :
$ pecl install memsession

Caching file_get_contents output inside APC.


APC creates a mmap which is used for caching compiled php response. Besides this APC provides API to cache user data into this mmap. There are many php apps which uses file_get_contents to read the static files. If APC can cache these files in it's mmap, it will improve file_get_contents performance.

Based on the above idea, I developed a patch which stores the file_get_content output in APC's cache. In this patch, APC intercepts the calls of file_get_contents and replaces it with apc_file_get_contents function. If file is a full path and entire file is requested then APC caches the content.

To enable the caching use the following in php.ini :
apc.cache_static_contents=1
apc.user_ttl = 30

apc.user_ttl will decide how long the entry will remain inside the cache.  If apc.user_ttl is set to 0 then the entry will never be removed from cache.

Here is the performance data collected using Studio 12 on Solaris sparc for ecommerce php benchmark. (3500 simultaneous users)
Run
Excl. User CPU  sec.
Incl. User CPU sec.
Excl. Total LWP  sec.
Incl. Total LWP  sec.
Excl. Sys. CPU sec.
Incl. Sys. CPU sec.
Name
Without  Cache
0.831
28.660
1.141
540.018
0.030
344.311
zif_file_get_contents
With Cache
1.481
181.537
1.821
282.758
0.140
12.449
zif_apc_file_get_contents

User + System time before Caching = 372 seconds
User + System time after Caching = 193 seconds
Total time reduction in file_cache_contents = 48%

When static file is inside the cache then server will not do any stat calls. Which means if file changes then cache will still serve the old cached file during apc.user_ttl interval.

Monday Oct 05, 2009

memsession 1.0.0 beta is now available

Memsession 1.0.0 beta is now available, you can install it using pecl.
$ pecl install memsession-beta

Friday Sep 04, 2009

In memory session extension for multithreaded php


Php's default session handler (mod_files) saves the session data in file. To improve performance, we can save the session data in ramdisk or in /tmp/ on Solaris.  Even when we store the session data in ramdisk there is lot of CPU cycles are spent in open/read/write call. So the question is can we improve the session writing/reading in php.

Answer is yes. If we have memory available in the system then session can be stored in RAM. If php is compiled without threads (e.g fastcgi) then mod_mm extension is there in php which does the job. It saves the session in mmap. But when we run php in multithreaded mode and entire php is inside single process then session can be allocated in process memory. I wrote a php extension for multithreaded php which saves php session in memory.

Php's session GC will cleanup the old sessions. So session gc time session.gc_maxlifetime should not be very high otherwise it will consume lots of memory.

Sources can be downloaded from :
svn co http://svn.php.net/repository/pecl/memsession

Sources can be viewed from here. Look for README for more information.


Compilation is similar to any other extension:

Compiling on unix platforms (for mulithreaded php e.g NSAPI php)
$ phpize
$ CFLAGS="-m32" ./configure --with-php-config=<phpinstdir>/bin/php-config
$ gmake
$ gmake install

Compiling in Windows (tested on Win2K and Windows Vista):
 C:\\php-src-5.3.0> cscript /nologo configure.js --enable-apache2handler --enable-zts --enable-memsession=shared
C:\\php-src-5.3.0> nmake

Configuration (php.ini) :
extension="memession.so"

[Session]
session.save_handler=memsession


GCC notes : Since memsession uses gcc's atomic operation which was added in 4.1.2 so gcc version 4.1.2 is required. "-march=i586" might be required to enable gcc's atomic operations.

About

Basant Kukreja

Search

Top Tags
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today