Feb 11

Long time no blog !.

Last year I working on new type of connection probe for one of the private Torrent tracker in Thailand. So ~150000 Peers and around ~15000 clients out there. (Prove that bittorrent users typically run average of 10 torrents). It previously work using 1 process to start serveral child process for processing and die.

I just upgraded it to work as Daemon with new interprocess communication things. It work by simple unix based named socket. It work the same way that preforked daemon usally do with one master process to distribute request to clients.

But ... you may surprise -- It wrote with PHP not C, Java, Perl, Python. Dynamic languages is seems to work well for this too if it supports. This almost the C wrapper so memory leak is no probably exists. (but PHP parser itself usually have memory leaks).

Meanwhile PHP usually not good in memory conservation. My server got some errors on PHP script that decode (bencoded) .torrent files and ... Hey ... 128 MB memory was exhaust. Why, .torrent is just around 300k in size.

I figured out that PHP structure usually allocate memory in chunk. And PHP store variables that use small memory together. But sometimes it fragments so much and PHP does not good on handle memory fragment.

I use memory_get_usage(true) to check how much memory was allocated. That 300k .torrent files use as much as 136MB of memory after parsed (that memory_get_usage(true) report). But memory_get_usage(false) will give real memory usage of PHP (not alloated memory). It gives only 28MB.

So PHP not good when handle memory fragments.

Posted by SF-Alpha

Dec 3
So ... Sometimes, your need to know relative path for coding 'include()' or 'require()' in PHP but you script reside in different sub directories and different levels and it got messing.

How could you detect the Relative Path and Absolute Path on your document root ? And later use single variables or constant and specify relative path of included script relative to document root instead ...

This is the solution. But this may not work as expected if you are using URL rewrite or alias on some web server.

<?php
define(WEB_ROOTDIR, preg_replace('/\/[^\/]+/','../',dirname($SERVER['PHP_SELF']))))
require_once(WEB_ROOTDIR . '/adodb/adodb.inc.php');
require_once(WEB_ROOTDIR . '/include/myclass.php');
...
...
?>

To see how this work ... Try playing around your sub directory of your document root by put this script in various place.

<?php
header('Content-Type: text/plain');
echo $_SERVER['PHP_SELF'] . "\n";
echo dirname($_SERVER['PHP_SELF']) . "\n";
echo preg_replace('/\/[^\/]+/','../',dirname($_SERVER['PHP_SELF'])) . "\n";
echo realpath('./' . preg_replace('/\/[^\/]+/','../',dirname($_SERVER['PHP_SELF']))) . "\n";?>
?>

Now ... What if I am using url_rewrite and above code not working ? ... You need to specify your WEB_ROOTDIR as absolute path, using __FILE_ instead, for example, this file is reside in document root.

<?php
define(WEB_ROOTDIR, realpath(dirname(_FILE_)));
...
...
?>

this file is in <docroot>/include/config.inc.php

<?php
define(WEB_ROOTDIR, realpath(dirname(_FILE_) . '/..'));
...
...
?>

Posted by SF-Alpha