Letsgetdugg

Random tech jargon

Browsing the tag threads

OpenSolaris uses a single-threaded malloc by default for all applications. The JDK that is compiled for Solaris fails to be linked against mtmalloc or the newer umem malloc implementation that is multithread optimized. In a multithreaded application using a single threaded malloc can degrade performance. As memory is being allocated concurrently in multiple threads, all the threads must wait in a queue while malloc() handles one request at a time, this is called heap contention. To get around this contention point you can force the JDK to use the umem malloc. 

LD_PRELOAD=/usr/lib/libumem.so /opt/jdk1.7.0/bin/java start.jar or LD_PRELOAD=/usr/lib/libmtmalloc.so /opt/jdk1.7.0/bin/java start.jar

This simple fix has really improved performance on our web service fabulously40. The application went from serving 120req/sec uncached to 170req/sec. Not bad no? 

This also works wonders for mysql and varnish, two applications that really put those threads to use.  We have dropped 100ms in response time with varnish by just using umem for the malloc implementation.

Try this fun perl benchmark, to test your dual core, SMP or hyperthreaded system.

Before running, make sure you have perl 5.8 with threading support compiled in.

Perl has native ithreads as of perl 5.8.

#!/usr/bin/perl -w
use threads;
use strict;

my $y1=Bench->new();
print "Bencmarking multi-threadedn";
$y1->benchmark();

print "Benchmarking single-threadedn";
$y1->ncpu(1);
$y1->benchmark();

package Bench;

sub new ()
{
        my $self = {result => 0,ncpu=>0};
       
        my $cpus =`sysctl hw.ncpu`;
        $cpus =~ /: (.*)/g;
        $self->{ncpu}=$1;
       
        my $class = shift;
        bless ($self,$class);
        return $self;
}

sub ncpu {
        my ($self,$num) = @_;
        if(defined $num) { $self->{ncpu}=$num; } else { return $self->{ncpu}; }
}

sub benchmark ()
{
        my ($self)=@_;
        my @thr;
        for(my $i=0;$i < $self->{ncpu};$i++)
        {
                print "Starting thread $in";
                push @thr, threads->create(‘benchmark_thread’);
        }
        my $total=0;
        foreach my $t (@thr)
        {
                $total=$total+$t->join();
        }
        print "Total number of insane floating point divisions in 10 seconds is ". $total . "n";       
}

sub benchmark_thread()
{
        my ($y,$x)=0;
        my $time1 = time();
        my ($self)=@_;
        while(1){
          #$time2 = time();
          if((time()  - $time1)>= 10){last;}
          else {
            $x=1.00/24000000000.001;
            $y++;
          }
        }      
        return $y;
}

Tagged with , ,