Performance impact of __attribute__((packed))

__attribute__((packed)) is a GNU C++ extension tell the compiler not to not to align the data structure to the machine word. Using this attribute make it easy serialize the content of the structure in a platform independent way.

For example,

struct Aligned {
  u_int64_t a;
  char b;
};

On 64-bit machine (word length 8), sizeof(Aligned) is 8 (assuming sizeof(int) is 4)). Here structure is padded with 3 bytes to align it to the word size.

struct NonAligned {
  int a;
  char b;
};

On 64-bit machine (word length 8), sizeof(Aligned) is 5 (assuming sizeof(int) is 4)). Here there is not padding added.

For the non-aligned structure, compiler generate extra code to adjust the memcpy operation. I tried the following code.

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>

struct NonAligned {
  int a;
  char b;
} __attribute__ ((packed));

struct Aligned {
  int a;
  char b;
};

int main() {
  NonAligned a;
  Aligned b;

  char buf[10];
  size_t c = 1000000000;
  {
    struct timeval start, end;
    gettimeofday(&start, NULL);
    for (size_t i = 0; i < c; ++i) {
      memcpy(&buf, &a, sizeof(a));
    }
    gettimeofday(&end, NULL);
    suseconds_t t = ( (end.tv_usec + end.tv_sec * 1000000) - (start.tv_usec + start.tv_sec * 1000000) );
    printf( "total time: %u\n", t);
  }

  {
    struct timeval start, end;
    gettimeofday(&start, NULL);
    for (size_t i = 0; i < c; ++i) {
      memcpy(&buf, &b, sizeof(b));
    }
    gettimeofday(&end, NULL);
    suseconds_t t = ( (end.tv_usec + end.tv_sec * 1000000) - (start.tv_usec + start.tv_sec * 1000000) );
    printf( "total time: %u\n", t);
  }

}

The above code produced the following output on my Mac air.

total time: 4487688
total time: 2565614

Copying non-aligned structure is 42% slower than the aligned structure.

To Like or not to Like?

Today Yahoo! posted a story on facebook about a death in Yosemite National Park. On Facebook fan page, there were around 138 Likes for this link.

I think this is where the meaning of the Like is twisted. When you say I Like the story, does that mean you enjoyed reading that news? How can one enjoy the death of another human being?

Should there be more buttons like Unlike, Sad, Happy etc.?

Synchronizing time in Ubuntu on Virtual Box

Recently I started noticing ntp crashing in ubuntu virtual machine, installed on virtual box Virtual machine. In turns out that virtual box can synchronize the time in guest OS with host OS. For Ubuntu following need to be done.

  • Install some pre-requisites
  • aptitude install build-essential linux-headers-$(uname -r) -y
    
  • Install Guest Additions with Devices -> Install Guest Additions…
  • Mount virtual CDROM
  • sudo mkdir -p /media/cdrom
    sudo mount /dev/cdrom /media/cdrom
    
  • Run the installer script. This will take several minutes to build and install.
  • cd /media/cdrom
    sudo ./VBoxLinuxAdditions.run
    
  • Unmount the cdrom
  • cd
    sudo umount /media/cdrom
    

Chances of India winning the ICC World Cup Final – 2011

India won the world cup in 1983. Now it is 2011 and India is again into World Cup Finals. As an Indian, I am supporting India to win the World Cup 2011. It is not just me, but the calendar also think that this year India will win the ICC World Cup 2011. Look at the Calendar of 1983 and 2011 for the Month of April. Check out the calendar for other months of 1983 and 2011

April 1983
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

April 2011
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Solr/Tomcat on Unbuntu

Here is the script I wrote to setup the solr in the tomcat container on Ubuntu Linux distribution

#!/bin/bash

cd /tmp
echo "Installing tomcat..."
if [ ! -d /usr/local/tomcat6 ]; then
  if [ ! -e "apache-tomcat-6.0.29.tar.gz" ]; then
    wget "http://apache.cs.utah.edu//tomcat/tomcat-6/v6.0.29/bin/apache-tomcat-6.0.29.tar.gz";
  fi
  tar zxpvf apache-tomcat-6.0.29.tar.gz
  mv  apache-tomcat-6.0.29 /usr/local/tomcat6
fi
pushd /usr/local/tomcat6/bin
./startup.sh
popd
if [ $? -ne 0 ]; then
  echo "Failed to start tomcat";
  exit 1;
else
  echo "done"
  sleep 5
  pushd /usr/local/tomcat6/bin
  ./shutdown.sh
  popd
fi
if [ ! -e "apache-solr-1.4.1.tgz" ]; then
  echo "Downloading solr"
  wget "http://mirror.atlanticmetro.net/apache//lucene/solr/1.4.1/apache-solr-1.4.1.tgz";
fi
tar zxpvf apache-solr-1.4.1.tgz
pushd /usr/local/tomcat6
mkdir -p conf/Catalina conf/Catalina/localhost 
popd
SOLR_HOME=/usr/local/solr
mkdir -p $SOLR_HOME
cp -R apache-solr-1.4.1/example/solr/* $SOLR_HOME

cp apache-solr-1.4.1/dist/apache-solr-1.4.1.war $SOLR_HOME

sed -e "s!\(<dataDir>\${solr.data.dir:\)\.!\1${SOLR_HOME}!g" $SOLR_HOME/conf/solrconfig.xml > solrconfig.xml.temp
mv solrconfig.xml.temp $SOLR_HOME/conf/solrconfig.xml

pushd /usr/local/tomcat6/conf/Catalina/localhost

cat <<EOF > solrdev.xml
<?xml version="1.0" encoding="utf-8"?>
<Context docBase="$SOLR_HOME/apache-solr-1.4.1.war" debug="0" crossContext="true">
  <Environment name="solr/home" type="java.lang.String" value="$SOLR_HOME" override="true"/>
</Context>
EOF

#cat <<EOF > solrprod.conf
#<Context docBase="/usr/local/tomcat6/data/solr/apache-solr-1.4.1.war" debug="0" crossContext="true">
#  <Environment name="solr/home" type="java.lang.String" value="/usr/local/tomcat6/data/solr/prod" override="true"/>
#</Context>
#EOF

popd

pushd /usr/local/tomcat6/bin
sudo ./startup.sh
popd

Download the source here

Java Generics Gone Wrong!

I have an array list say

ArrayList<String> list = ArrayList<String>();

Now I want to get the String array from this list. Simple huh?

String[] arr = list.toArray();

Wrong!!!!

toArray method returns an array of Object, not the template type (in this case String). Instead you have to call

String[] arr = list.toArray( new String[list.size()] );

What were they thinking?