Thursday, November 15, 2007

Apache - Multi-Processing Modules (MPMs)

MPMs are Responsible for binding to network ports on the machine, accepting requests, and dispatching children to handle the requests. For Linux, two types: worker (threaded MPM) and prefork (non-threaded MPM)

worker MPM
prefork MPM

How it works

Uses multiple child processes. It's multi-threaded within each child, and each thread handles a single connection.

Uses multiple child processes, each child handles one connection at a time.


fast and highly scalable

speed is comparable to that of worker


Memory footprint is comparatively low

Memory usage is high, and more traffic leads to greater memory usage


less tolerant of faulty modules, and a faulty thread can affect all the threads in a child process.

highly tolerant of faulty modules and crashing children

Suited for

multiple processors

single or double CPU systems

Tuesday, July 03, 2007

Better Code Through Destruction

Perl's garbage collector counts references. When the count reaches zero (which means that no one has a reference), Perl reclaims the entity. The approach is simple and effective. However, circular references (when object A has a reference to object B, and object B has a reference to object A) present a problem.

A common example is a tree-like data structure. To navigate both directions--from root to leaves and vice versa--a parent node has a list of children and a child node has a reference to its parent. Many CPAN modules implement their data models this way, including HTML::Tree, XML::DOM, and Text::PDF::File. All these modules provide a method to release the memory. The client application must call the method when it no longer needs an object. However, the requirement of an explicit call is not very appealing and can result in unsafe code.


Instead of explicitly calling the method to delete the object, create a special guard object (of another class) whose sole responsibility is to release the resource. When the guard object gets destroyed, its destructor deletes the tree.
    use HTML::TreeBuilder;

foreach my $filename (@ARGV) {
my $tree = HTML::TreeBuilder->new;

my $sentry = Sentry->new($tree);

next unless $tree->look_down('_tag', 'img');
## next, last or return are safe here.
## Tree will be deleted automatically.

package Sentry;
sub new {
my $class = shift;
my $tree = shift;
return bless {tree => $tree}, $class;
my $self = shift;

Note that now there is no need to call $tree->delete explicitly at the end of the loop. The magic is simple. The code of DESTROY method of the Sentry package calls, in turn, the method delete of the $tree object.

Finally, there is no need to code your own Sentry class. Use Object::Destroyer, originally written by Adam Kennedy.


Thursday, June 21, 2007

Switching between HTTP and HTTPS


Following statements are there in my Apache2 virtual hosts section

Redirect /host/directory/folder/login.php https://servername/host/directory/folder/login.php
Redirect /host/directory/folder/register.php https://servername/host/directory/folder/register.php

How to return to http (not https) when the above 2 pages are not involved?


RewriteRule /.../login.php https://.../login.php [R]
RewriteRule /.../register.php https://.../register.php [R]

RewriteRule /.../login.php - [S=2]
RewriteRule /.../register.php - [S=1]
RewriteRule ^/(.*) http://servername/$1 [R]

In the HTTPS VH, if you get login.php or register.php you don't rewrite it (the "-"), then you skip the next 2 or 1 RewriteRules ( [S=2], [S=1]) - that is, you skip over the general purpose rewrite back to HTTP. So login.php or register.php get served from HTTPS and everything else goes back to HTTP.

Thursday, June 07, 2007

Perl Code Profiling - Apache::DProf

  1. In
       # The Apache::DProf requires this
    use Apache::Registry;

  2. In conf:
       PerlModule Apache::DProf

  3. Make sure the logs directory has write rights

  4. Restart Apache & do whatever you want in the browser page

  5. Stop Apache (only then the tmon.out will get complete output)

  6. Go to the directory where tmon is (logs/dprof//) and run dprofpp

Refer: Code Profiling Techniques

Monday, June 04, 2007

Apache and strace

To display a trace of system calls. Useful for debugging.

root@sri# apachectl stop (First stop Apache and then restart Apache with strace)
root@sri# strace -f -o trace.txt /etc/rc.d/init.d/httpd start

-f: Traces child processes as they are created by currently traced processes.

-o: Outputs to a text file