Monitoring Apache memory allocation with DTrace

The Apache web server uses an abstraction concept called a pool to simplify memory management in the Apache web server. Memory can be allocated and assigned to a pool (there are several pools, with different lifetimes depending on how long the memory needs to stick around) through numerous routines, including the apr_palloc() routine. This routine contains the following prototype:

void *apr_palloc(apr_pool_t *pool, apr_size_t size)

apr_palloc() and the underlying allocator routines try to allocated memory in chunks, which ensures that brk() and company aren’t called excessively. To get a better idea of how often apr_palloc() requested memory from chunks already allocated (the underlying allocator uses malloc() ), I created the following D script:

$ dtrace -q -p 4551 -n ‘
pid$target::malloc:entry
{
@malloc[probefunc] = count();
}

pid$target::apr_palloc:entry
{
@palloc[probefunc] = count();
}’

malloc                                                           22
apr_palloc                                                      671

Wow — apr_palloc() was called 671 times for 3 GET requests, and malloc() was called 22 times to meet the demand for memory (this is another reason why it’s a good idea to comment out unused modules). To see the size of each request I used the following D script:

$ dtrace -q -p 4551 -n ‘
pid$target::malloc:entry
{
@malloc[probefunc] = quantize(arg0);
}

pid$target::apr_palloc:entry
{
@palloc[probefunc] = quantize(arg1);
}’

  malloc                                            
           value  ------------- Distribution ------------- count    
              32 |                                         0        
              64 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@     20       
             128 |                                         0        
             256 |                                         0        
             512 |                                         0        
            1024 |                                         0        
            2048 |                                         0        
            4096 |                                         0        
            8192 |@@@@                                     2        
           16384 |                                         0        

  apr_palloc                                        
           value  ------------- Distribution ------------- count    
               0 |                                         0        
               1 |                                         1        
               2 |@@@@                                     68       
               4 |@@                                       26       
               8 |@@@@@@@@@                                142      
              16 |@@@@@@@@@@@@@@                           219      
              32 |@@@@@                                    72       
              64 |@@                                       33       
             128 |@                                        10       
             256 |@@@                                      54       
             512 |                                         3        
            1024 |                                         0        
            2048 |                                         1        
            4096 |                                         1        
            8192 |                                         0    

One item to note — the malloc() calls may have come from a module directly, which could have thrown off the analysis performed here. I tried to issue several requests prior to capturing the data, and may need to spend some time correlating the malloc() calls with the code that actually issued them. Apache and DTrace rock!

Leave a Reply

Your email address will not be published. Required fields are marked *