Php and shared memory

Разделяемая (shared) память

Since there is no mention of the (lack of) need for locking here, I took a look into the shmop.c extensions code. So correct me if I’m wrong, but the shmop.c extension uses memcpy() to copy strings to and from shared memory without any form of locking, and as far as I know, memcpy() is not atomic.

If that’s true as I suspect, then these ‘easy to use’ functions are not so ‘easy to use’ any more and have to be wrapped in locks (e.g. semaphores, flocks, whatever).

I have written a script to highlight the superiority of shared memory storage.
Although it doesn’t use the shmop function, the underlying concept is similar.
‘/shm_dir/’ is a tmpfs directory, which is based on shared memory, that I have mounted on the server.

Below is the result on an Intel Pentium VI 2.8 server:

IO test on 1000 files
IO Result of Regular Directory : 0.079015016555786
IO Result of Shared Memory Directory : 0.047761917114258

IO test on 10000 files
IO Result of Regular Directory : 3.7090260982513
IO Result of Shared Memory Directory : 0.46256303787231

IO test on 40000 files
IO Result of Regular Directory : 117.35703110695 seconds
IO Result of Shared Memory Directory : 2.6221358776093 seconds

The difference is not very apparent nor convincing at 100 files.
But when we step it up a level to 10000 and 40000 files, it becomes pretty obvious that Shared Memory is a better contender.

// Your regular directory. Make sure it is write enabled
$setting [ ‘regular_dir’ ] = ‘/home/user/regular_directory/’ ;

// Your shared memory directory.
$setting [ ‘shm_dir’ ] = ‘/shm_dir/’ ;

// Number of files to read and write
$setting [ ‘files’ ] = 40000 ;

function IO_Test ( $mode )
<
$starttime = time ()+ microtime ();

for( $i = 0 ; $i < $setting [ 'files' ] ; $i ++)
<
$filename = $setting [ $mode ]. ‘test’ . $i . ‘.txt’ ;
$content = «Just a random content» ;

// Just some error detection
if (! $handle = fopen ( $filename , ‘w+’ ))
<
echo «Can’t open the file » . $filename ;
exit;
>

if ( fwrite ( $handle , $content ) === FALSE )
<
echo «Can’t write to file : » . $filename ;
exit;
>

// Read Test
file_get_contents ( $filename );

$totaltime = ( $endtime — $starttime );

echo ‘IO test on ‘ . $setting [ ‘files’ ]. ‘ files
‘ ;
echo ‘IO Result of Regular Directory : ‘ . IO_Test ( ‘regular_dir’ ) . ‘ seconds
‘ ;
echo ‘IO Result of Shared Memory Directory : ‘ . IO_Test ( ‘shm_dir’ ) . ‘ seconds
‘ ;

/* Removal of files to avoid underestimation
#
# Failure to remove files will result in inaccurate benchmark
# as it will result in the IO_Test function not re-creating the existing files
*/
foreach ( glob ( $setting [ ‘regular_dir’ ]. «*.txt» ) as $filename ) <
unlink ( $filename ); $cnt ++;
>
foreach ( glob ( $setting [ ‘shm_dir’ ]. «*.txt» ) as $filename ) <
unlink ( $filename ); $cnt ++;
>

I wrote a php memcache back in 2003 as a sort of proof of concept
it is use on a few machines for doing heavy page load caching.
it works very well.
Following are some of the core functions I made

###############################################
#### shared mem functions
/*
for debugging these
use `ipcs` to view current memory
use `ipcrm -m ` to remove
on some systems use `ipcclean` to clean up unused memory if you
don’t want to do it by hand
*/
###############################################
function get_key ( $fsize , $file ) <
if(! file_exists ( TMPDIR . TMPPRE . $file )) <
touch ( TMPDIR . TMPPRE . $file );
>
$shmkey = @ shmop_open ( ftok ( TMPDIR . TMPPRE . $file , ‘R’ ), «c» , 0644 , $fsize );
if(! $shmkey ) <
return false ;
>else <
return $shmkey ;
> //fi
>
function writemem ( $fdata , $shmkey ) <
if( MEMCOMPRESS && function_exists ( ‘gzcompress’ )) <
$fdata = @ gzcompress ( $fdata , MEMCOMPRESSLVL );
>
$fsize = strlen ( $fdata );
$shm_bytes_written = shmop_write ( $shmkey , $fdata , 0 );
updatestats ( $shm_bytes_written , «add» );
if( $shm_bytes_written != $fsize ) <
return false ;
>else <
return $shm_bytes_written ;
> //fi
>
function readmem ( $shmkey , $shm_size ) <
$my_string = @ shmop_read ( $shmkey , 0 , $shm_size );
if( MEMCOMPRESS && function_exists ( ‘gzuncompress’ )) <
$my_string = @ gzuncompress ( $my_string );
>
if(! $my_string ) <
return false ;
>else <
return $my_string ;
> //fi
>
function deletemem ( $shmkey ) <
$size = @ shmop_size ( $shmkey );
if( $size > 0 ) < updatestats ( $size , "del" ); >
if(!@ shmop_delete ( $shmkey )) <
@ shmop_close ( $shmkey );
return false ;
>else <
@ shmop_close ( $shmkey );
return true ;
>
>
function closemem ( $shmkey ) <
if(! shmop_close ( $shmkey )) <
return false ;
>else <
return true ;
>
>
function iskey ( $size , $key ) <
if( $ret = get_key ( $size , $key )) <
return $ret ;
>else <
return false ;
>
>
################################################
?>

It’s not the job of the shmop extension to provide locking, there are many locking schemes avalible, if you need some sort of atomic operations choose a locking scheme that suits you and use it.

Windows does support shared memory through memory mapped file. Check the following functions for details:

The shmop implementation as described in this help page is acttually merely a ramdisk / tmpfs that exists only within php, and even only on the linux servers. Or am I missing something?

On windows, the very same functionality can easily be achieved by creating such disk.

In fact, on my own server, I do use a tmpfs disk instead of the — as it appears to me — limited shmop feature.

Why not implement a $_SHARED or $_MUTUAL SuperGlobal in which we can create variables at will, and that is shared by all connections?

This would greatly improve performance of many PHP applications, and could save a lot of burden on server memory. Especially if those variables could be classes containing functions.

You could implement that it is up to the programmer to guard atomicity.

Such superglobal would be feasible on windows servers as well.

The idea behind SHMOP is an easy to use shared memory interface,
without any additional headers added to the shared memory segment
or requiring any special special controls to access the shared memory
segment outside of PHP. SHMOP borrows its api from C’s api to shm,
which makes it very easy to use, because it treats shared memory, like C, as
a file of sorts. This makes it very easy to use even for novices, due to this
functionality. Most importantly SHMOP uses shm segments to store raw data,
which means you don’t need to worry about matching headers, etc. when you are
using C, perl or other programming languages to open/create/read/write shm segments
that were create or are going to be used by PHP. In this it differs from
sysvshm, who’s shm interface uses a specialized header, which resides inside
the shared memory segment this adds an unnecessary level of difficulty when
you want to access php shm from external programs.
Also, from my personal tests in Linux 2.2/2.4 and FreeBSD 3.3 SHMOP is about
20% faster then sysvshm, mostly due to fact it does not need to parse the
specialized header and stores the data in raw form.

What you need to realise is that sysvshm is extremly php oriented in it’s ability, it’s quite a kludge interfacing other NON PHP utilities with it. For example have you tried using sysvshm to read an shm segment NOT created by php? It’s not possible, because sysvshm uses a proprietry format, in essense it can ONLY be used within PHP unless of course you take time to figure out this format.
So basically, the purpose of shmop is to provide a symple interface to shared memory that can be used with OTHER NON php shm creators.

Источник

Shared Memory

To save/get cache that easily just save this as cache.php or whatever you see fit:

function save_cache ( $data , $name , $timeout ) // delete cache
$id = shmop_open ( get_cache_id ( $name ), «a» , 0 , 0 );
shmop_delete ( $id );
shmop_close ( $id );

// get id for name of cache
$id = shmop_open ( get_cache_id ( $name ), «c» , 0644 , strlen ( serialize ( $data )));

// return int for data size or boolean false for fail
if ( $id ) set_timeout ( $name , $timeout );
return shmop_write ( $id , serialize ( $data ), 0 );
>
else return false ;
>

function get_cache ( $name ) if (! check_timeout ( $name )) $id = shmop_open ( get_cache_id ( $name ), «a» , 0 , 0 );

if ( $id ) $data = unserialize ( shmop_read ( $id , 0 , shmop_size ( $id )));
else return false ; // failed to load data

if ( $data ) < // array retrieved
shmop_close ();
return $data ;
>
else return false ; // failed to load data
>
else return false ; // data was expired
>

function get_cache_id ( $name ) // maintain list of caches here
$id =array( ‘test1’ => 1
‘test2’ => 2
);

function set_timeout ( $name , $int ) $timeout =new DateTime ( date ( ‘Y-m-d H:i:s’ ));
date_add ( $timeout , date_interval_create_from_date_string ( » $int seconds» ));
$timeout = date_format ( $timeout , ‘YmdHis’ );

$id = shmop_open ( 100 , «a» , 0 , 0 );
if ( $id ) $tl = unserialize ( shmop_read ( $id , 0 , shmop_size ( $id )));
else $tl =array();
shmop_delete ( $id );
shmop_close ( $id );

$tl [ $name ]= $timeout ;
$id = shmop_open ( 100 , «c» , 0644 , strlen ( serialize ( $tl )));
shmop_write ( $id , serialize ( $tl ), 0 );
>

function check_timeout ( $name ) $now =new DateTime ( date ( ‘Y-m-d H:i:s’ ));
$now = date_format ( $now , ‘YmdHis’ );

$id = shmop_open ( 100 , «a» , 0 , 0 );
if ( $id ) $tl = unserialize ( shmop_read ( $id , 0 , shmop_size ( $id )));
else return true ;
shmop_close ( $id );

$timeout = $tl [ $name ];
return ( intval ( $now )> intval ( $timeout ));
>

Источник

Introduction

These modules provide wrappers for the System V IPC family of functions. It includes semaphores, shared memory and inter-process messaging (IPC).

Semaphores may be used to provide exclusive access to resources on the current machine, or to limit the number of processes that may simultaneously use a resource.

This module provides also shared memory functions using System V shared memory. Shared memory may be used to provide access to global variables. Different httpd-daemons and even other programs (such as Perl, C, . ) are able to access this data to provide a global data-exchange. Remember, that shared memory is NOT safe against simultaneous access. Use semaphores for synchronization.

Limits of Shared Memory by the Unix OS
SHMMAX max size of shared memory, normally 131072 bytes
SHMMIN minimum size of shared memory, normally 1 byte
SHMMNI max amount of shared memory segments on a system, normally 100
SHMSEG max amount of shared memory segments per process, normally 6

The messaging functions may be used to send and receive messages to/from other processes. They provide a simple and effective means of exchanging data between processes, without the need for setting up an alternative using Unix domain sockets.

Note: Only the shared memory functions and ftok() are available on Windows. Neither semaphores nor inter-process messaging functions are supported on that platform.

User Contributed Notes

Источник

Читайте также:  Основы PHP
Оцените статью