Profiling a C/C++ code with Valgrind + KCachegrind

First let’s explain what is profiling. In general terms, profiling is a technique that can be used when you experience performance problems in your application. Basically, it consists in measure the spent time of each code function, to identify where is the bottleneck, i.e., the lines of the code that concentrate more the execution time.

There is a lot of tools for this proposal. In Java we have JPerformance and that are very nice. For C/C++, one that is very used is the GNU gprof, which is very simple to use (just recall to compile your code with -pg flag). Recently I knew KCachegrind, which is a cache profiler based on Valgrind, that surprised me by the simplicity of its QT interface. Valgrind, for those who does not know, is not a profiling tool, it is a memory management tool that helps you to find bugs due to memory leaks, memory conflicts. It is very recommended to use Valgrind since the beginning of coding. Let’s explain a little bit more of KCachegrind.

You can install it by apt-get repostitory:

user@host:~$ sudo apt-get install valgrind kcachegrind

For demonstration, I use a database simulator written in C (by me and others) as a target application. The simulator repeat the database operation thousand of times and compute the average of some measured metrics. The normal execution takes hours, so I run a simple experiment with valgrind (which makes the execution much slower). Before call kcachegrind is necessary to call valgrind to profile the cache data.

user@host:~$ valgrind --tool=callgrind ./simulator --param param.txt --queries 10 --seed 65270
user@host:~$ kcachegrind callgrind.out.12208

I took some screenshots to highlights some KCachegrind features:

A first feature provided by Kcachegrind, showed in the first picture, is a table with the cumulative cost of each function. It consider that main() has 100% of cost and we can follow how this cost is distributed along the functions called by main(). The same feature, explained above, can be viewed in a graphic view, as depicted in the second figure. And another way to analyse the code is through the graph view, which starts in main(), showing the cumulative cost and walk through the code graph. On this graph we can views that the higher cost of the code is concentrated on vprintf function, which is used to log the simulator execution for debug purposes.

See ya!

Copiando coleções em Java

Em java, para fazer com que uma coleção tenha o mesmo conteúdo que outra, podemos fazer o seguinte:

  List<String> a = new LinkedList<String>();
  a.add("1");
  a.add("2");
  List<String> b = new LinkedList<String>(a); //aqui b copiará a

No entanto, os elementos de b, que copiou tudo de a, continuarão referenciando os mesmos elementos de a na memória. Em alguns casos isso pode não ser o comportamento desejado.

Para forçar o Java a duplicar as instâncias da coleção copiada na coleção de destino, adicione após o construtor de b  o seguinte comando:

  Collections.copy(b,a);

Observe que se vc chamar apenas o comando copy sem ter feito a construção corretamente, provavelmente encontrará uma exceção informando que B não suporta A, ou que B não é grande o suficiente para A.

Remove if(debug) intructions from Java bytecode with a static flag

One of the several techniques (a MacGyver style) for debug in Java is insert if(DEBUG) messages for monitoring errors and events. However, according to the size of your code, the number of conditional tests performed to check if the DEBUG flag was enabled can affect the performance of your application.

In C one of the alternatives is to use #define DEBUG directive jointly with #ifdef DEBUG tests, hence, according to this flag, compiler will inserts or not the debug code in the executable.

In Java you can do that using a static flag, for instance, considers the example as follows:

class Teste{

        public static final boolean DEBUG = true;

        public static void main( String[] args ){

                if( DEBUG ){
                        int a = 10;
                        System.out.println( "a = " + a );
                }
        }
}

Compiling the code:

$ javac Teste.java

Verifying the generated bytecode to check the if(debug) instructions:

$ javap -c Teste.java

Compiled from "Teste.java"
class Teste extends java.lang.Object{
public static final boolean DEBUG;

Teste();
  Code:
   0:    aload_0
   1:    invokespecial    #1; //Method java/lang/Object."":()V
   4:    return

public static void main(java.lang.String[]);
  Code:
   0:    bipush    10
   2:    istore_1
   3:    getstatic    #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   6:    new    #3; //class java/lang/StringBuilder
   9:    dup
   10:    invokespecial    #4; //Method java/lang/StringBuilder."":()V
   13:    ldc    #5; //String a =
   15:    invokevirtual    #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   18:    iload_1
   19:    invokevirtual    #7; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
   22:    invokevirtual    #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   25:    invokevirtual    #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   28:    return

}

Changing the debug flag to false:

class Teste{

        public static final boolean DEBUG = false;

        public static void main( String[] args ){

                if( DEBUG ){
                        int a = 10;
                        System.out.println( "a = " + a );
                }
        }
}

Recompiling the code:

$ javac Teste.java

Verifying again the generated bytecode we can see that compiler take in account that since the debug flag is constant (final) its value cannot be changed during the execution so the conditional test will always returns false and can be removed.

$ javap -c Teste.java

Compiled from "Teste.java"
class Teste extends java.lang.Object{
public static final boolean DEBUG;

Teste();
  Code:
   0:    aload_0
   1:    invokespecial    #1; //Method java/lang/Object."":()V
   4:    return

public static void main(java.lang.String[]);
  Code:
   0:    return

}

See ya

Ant Compile With rJava library

This is pretty simple but for the sake of “I-will-not-forget-it-anymore” I will write it here. I copied some files of a Java project to a new machine. When I tried to execute it in the new place, “Exception in thread “main” java.lang.UnsupportedClassVersionError: Bad version number in .classa file”. Recompiling everything should make it work. Since my code requires rJava it is necessary to inform this to the ant (I’m using ant by the way).  The command which made my day is:

ant clean compile -lib ~/R/i486-pc-linux-gnu-library/2.9/rJava/jri/JRI.jar

2.9: be aware about the version you’ve installed, if you’ve compiled the packages in a version < 2.10, you will need to reinstall ’em. In my case I did reinstall nlme, mgcv, spatstat, and igraph.

Multi-dimensional array in Java

Since Java does not have pointers I was thinking if it can hold multi-dimensional arrays. Multi-dimensional array is an array where each row is another array, which can have different sizes. In C you can create an array of pointers and allocates each one to an array of different sizes. The following are two examples to allocates a multi-dimensional array in Java:

// declaration statement
int tri[][] = { {1,2,3}, {4,5}, {1} };

// dinamically statement
int[][] tri;
tri = new int[10][];
for (int r=0; r<tri.length; r++) {
	tri[r] = new int[r+1];
}
 
// print the triangle matrice
for (int r=0; r<tri.length; r++) {
	for (int c=0; c<tri[r].length; c++) {
		System.out.print(" " + tri[r][c]);
	}
	System.out.println("");
}

Read more:
http://www.go4expert.com/forums/showthread.php?t=1162

See ya