mercredi 30 janvier 2008

VIM editor advanced tips n' tricks.

Vi IMproved is a powerful text based text editor in the UNIX world. Below are some of the most important tips I rely upon when using this text editor in my everyday work.
These tips are completely uncategorized and appear in the order I added them to this post.
  • Finding opening and closing curly brackets in a code block: [{ and ]} useful if we want to locate the beginning of a long if block.

  • Converting a pattern to uppercase. Suppose we want to convert the word "pattern" (without the double quotes) to uppercase. Here is how to do it in vim: :%s/\(pattern\)/\U\1/g

  • Auto-completion magic: pressing Ctrl+P after typing the first letter of a word will auto-complete the word in vim.

  • Basic indentation support for Java.
    In .vimrc file put the following:

    set autoindent

    set si

    set shiftwidth=4

    set smarttab

    inoremap { {<cr>}<left><cr><up><tab>

    inoremap ( ()<left>

    inoremap " ""<left>

  • useful links:
    http://www.vim.org/tips
    http://vim.wikia.com/wiki


dimanche 20 janvier 2008

DTrace is cool

Recently, I was faced with a peculiar application performance problem on Linux due to memory allocator inefficiencies. Part of the investigation mandated that I had to figure out what was the application's memory allocation pattern.
Using DTrace on Solaris 10 was the quickest and best choice possible. Within a few minutes, I was able to write a simple script that allowed me to monitor calls to memory allocation functions malloc, calloc, realloc and free.
Below is the script. Simple function entry and return probes along with aggregation functions and timer probes assembled in less than 50 lines of code did the trick.
Just imagine having to write this in C ... besides the code is meant to be disposed after the investigation, so it's the kind of situation where DTrace comes to the rescue.
I am impatiently waiting for SystemTAP (DTrace's equivalent on Linux) to support userland probes because it would allow me to do this kind of observations and investigations natively on Linux without having to resort to using Solaris 10/DTrace.

Till then, I consider DTrace to be the coolest technology Sun has introduced in Solaris 10.


#!/usr/sbin/dtrace -s
BEGIN
{
malloc = 0;
calloc = 0;
realloc = 0;
free = 0;
malloctime = 0;
printf("| timestamp | malloc | calloc | realloc | free | avg malloc duration |");
}
pid$target::malloc:entry
{
@["malloc"] = quantize(arg0);
malloc += 1;
last = timestamp;
}
pid$target::malloc:return
{
malloctime += (timestamp - last);
}
pid$target::calloc:entry
{
@["calloc"] = quantize(arg0*arg1);
calloc +=1;
}
pid$target::realloc:entry
{
@["realloc"] = quantize(arg1);
realloc += 1;
}
pid$target::free:entry
{
free += 1;
}
tick-60sec
{
avgmalloctime = malloctime/malloc;
printf("| %Y | %12d | %12d | %12d | %12d | %12d |", walltimestamp, malloc, calloc, realloc, free, avgmalloctime);
malloc = 0;
calloc = 0;
realloc = 0;
free = 0;
malloctime = 0;
}


samedi 5 janvier 2008

Welcome to my eclipse blog.

Hello and welcome to my eclipse blog.
In this blog, I will try to post quick notes about eclipse as I learn how to code RCP applications in this superb and rich platform.

Adding menus and coolbar actions to your Eclipse RCP application

In this quick note I show you how to add actions to the menu bar and cool bar of your Eclipse RCP application.

First you need to create your own class that extends class ActionBarAdvisor - for example:

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
...
}


Second you need to define a makeActions method in which you define your application's actions - for example the below snippet creates an exitAction which is a workbench supplied action that represents the act of closing the application:

protected void makeActions(IWorkbenchWindow window) { exitAction = ActionFactory.QUIT.create(window); register(exitAction); ... }


Third you need to create your menu bar, add menu items to it and add your newly created actions through the fillMenuBar method - for example:

protected void fillMenuBar(IMenuManager menuBar) {
MenuManager fileMenu = new MenuManager("&File", "file");
fileMenu.add(exitAction);
menuBar.add(fileMenu);
...
}


Lastly to create a cool bar you need to supply a fillCoolBar method - for example:

protected void fillCoolBar(ICoolBarManager coolBar) {
IToolBarManager toolBar = new ToolBarManager(coolBar.getStyle());
coolBar.add(toolBar);
toolBar.add(exitAction);
...
}


This is a basic skeleton but I hope you have grabed the idea.

Until next time...