Tuesday, April 15, 2008

I'm back with NetBeans Profiler

I had been playing around with NetBeans profiler for past few days. All I can tell is that NetBeans Profiler is one of its kind and the most gorgeous tool in NetBeans.

Getting Started
Prior to using Profiler with any project we need to set the profiler to get accurate results later and to integrate the tool with your project. To achieve accurate results profiler must be calibrated in the beginning. To do this go to Profiler-->Advanced Commands -->Run Profiler Calibration. A dialog box appears where you need to select your JDK.

The calibration needs to be run only once. However if any significant changes are made to your machines, you need to run it again. A dialog box appears after the calibration where you can check the info about calibration and it also saves the calibration information in .nbprofile in your home directory. The profiler tool profiles the main project by default, However you can select the project you want by right clicking on the required project and selecting Profile option in the pop-up menu.

The profiler tool window appears wherein you can select the required action.

To integrate with the project, the IDE modifies the build script of your project to import an additional build script for the required profiling libraries. Before modifying the build scipt, the IDE creates a backup of the original build script. You can restore the original build script by choosing Profiler > Advanced Commands > Unintegrate from the main menu.

IDE provides you different profiling tasks which you can select according to your needs. The following table describes the profiling tasks and the profiling results obtained from running the task.
Profiling Task Results
Memory Application
Choose this to obtain high-level information about properties of the target JVM, including thread activity and memory allocations.
Analyze CPU Performance
Choose this to obtain detailed data on application performance, including the time to execute methods and the number of times the method is invoked.
Analyze Memory Usage Choose this to obtain detailed data on object allocation and garbage collection.

Monitoring an Application
The target application is starts soon after you click on the above option. Note that the overhead is low in this case.

To get the Telmetric view, under the view option (in the profiler window) select VM Telemetry, It must be something like this,

The Profiler window contains the controls that enable you to do the following:
  • Control the profiling task
  • See the status of the current profiling task
  • Display profiling results
  • Manage profiling results snapshots
  • See basic telemetry statistics
Analyzing CPU Performance
This part is quite interesting! CPU task allows you to profile the entire application or a particular code fragment (Of course you can select this!). It allows method by method execution.
You can select one of the options in the window,
  • Entire Application : The IDE instruments the entire code that you have written. It highlights the thread entry and exit points as well as calculates the time spent in each method.
  • Part of type Application : In this mode you can profile a particular code snippet of your interest. This significantly reduces the profiling overhead. You need to select the root methods in order to give profiler an entry point. Profiling data is collected when one of the application's threads enters and leaves the instrumentation root. No profiling data is collected until one of the application's threads enters the root method.

After defining the required root methods the same thread window pops up showing the thread execution status as well as the time taken by the entire method to run. You can view the Signal Dispatcher (it shows the status of selected threads) in the thread window by selecting the appropriate threads in the main window of the threads. Screen Shot of my app is shown below:

Analyzing Memory Usage
This mode gives you information regarding allocation to memory to objects, type if objects etc etc.
You can select one among these :
  • Record object creation only : When this option is selected, all classes currently loaded by the target JVM (and each new class as it is loaded) are instrumented to produce information about object allocations.
  • Record both object creation and garbage collection : When selected, profiling the application gives you information on object liveness such as about how many objects of each type are still alive, their size and average age, as well as allocation data.

You can also view the live results by selecting live results under the profiler menu in profiler window.

Taking SnapShots
You can obtain the memory snapshots of your application and also see the allocation stack trace for any object you want.
To do this,
  • Make sure that the profiler is still running, otherwise go back to your project, right click on it and open profiler tool again as mentioned before.
  • Open up the Live Results from the Profiler window, right click on an object whose stack trace you want and click on take snapshot.

The IDE takes the memory snapshot and opens the snapshot to the Allocation Stack Traces tab where you can explore the reverse call tree for methods that instantiated this object.

Sunday, April 6, 2008

New Language Support in NetBeans 6x

Few days ago I happened to come across Project Schliemann , I already had interest for providing VHDL Language support in NetBeans. Prior to Project Schliemann I was struggling to set up syntax highlighting support, code Folding & parser. Again I had to go thru lots of Netbeans APIs like Nodes APIs, File System APIs etc. It was a heck for me atleast! Days went on and I kept wondering when I'll kickstart my work. Then suddenly as though god had answered my prayers Project Schliemann came to my rescue. Thanks to the team of Project Schliemann.

I think I needn't explain what project Schliemann is about.
Schliemann project implements Generic Language Framework for NetBeans IDE. Schliemann engine allows one to describe some programming language and define how to integrate it to the NetBeans. Each programming language is defined in one nbs (NetBeans Schliemann) file. New declarative language has been created for that purpose.

As normal, In the first step you should describe the language - lexical part (define tokens using regular expressions) and syntax (grammar rules). In the second step you can define how to visualize this language in NetBeans. You can define colors for tokens (syntax coloring) or folding (based on grammar rules). You can define what parts of this language should be displayed in the navigator, and how to indent this language plus many other features.

Here I go with my findings,
- NetBeans is bundled with GLF support and shipped. It must not be difficult for users to extend the IDE to support their favorite language. Start with a new Netbeans module project:

Define the code name base and Display name on the next page:

New project is created when the finish button is pressed.

Now a new language support is to be added so right click on the project root node, then choose Language support (or Other-->Module Development-->Language Support)

Select the mime type as shown below:

After this you should see a language.nbs file in the editor window. It creates syntax definitons, grammars, color highlighting etc etc for an artificial language. It can be modified according to the
need of language description. This module can be directly built and installed now.

However if one wishes to see the changes made to his nbs file in the output file ( Example.vhd in my case) immediately, the following steps must be followed.

1. Open Advanced Options Dialog first (Tools--> Options action and Advanced Options button).

2. Select IDE Configuration/System / Object Types / Generic Languages Framework Objects node in Advanced Options Dialog.

3. Open "GLF Files" property for editing & press "..." button. A dialog box appears where the corresponding mime type can be selected for editing.

4. Select the mimeType ("text/vhd" in my case) there and press "Edit" button.

Now onwards, all the changes made in the nbs file will be reflected on the actual file type opened!
Lexical Structure of the Language can be defined in the way one wants! Here are few regular expression constructs-
'a' character a
"abc" string abc - syntax is the same like in Java (\t, \n, ...)
"ab"i case-insensitive string, i.e. ab, Ab, aB or AB
['a' 'b' 'c'] charater a, b, or c (simple class)
[^'a' 'b' 'c'] any character except a, b, or c (negation)
['a'-'z' 'A'-'Z'] a through z or A through Z, inclusive (range)
. any character
'a'? character a once or not at all
'a'+ character a one or more time
'a'* character a zero or more time
XY X followed by Y
X/Y Either X or Y
(X) X, as a capturing group

TOKEN:identifier: ( ['a'-'z' 'A'-'Z'] ['a'-'z' 'A'-'Z' '0'-'9']*)
Here is the list of Tokens( To be modified later) I declared:

A statement can be skipped by using the SKIP keyword as shown,
SKIP: comment

Note that the comment has to be declared as a token prior to the usage of it with this keyword.

Grammar has to be defined for the language, here is a pic of my grammar definition

Coloring can be done in an easy fashion:
Supported properties:

  • color_name: Name of color. elementName is used for name of color if its not specified.
  • default_coloring: Defines parent coloring (operator, keyword, identifier, whitespace, number, char, string, comment).
  • foreground_color: Foreground color (for example "white", "FF00FF").
  • background_color: Background color.
  • underline_color: Underlined color.
  • wave_underline_color: Wave underlined color.
  • strike_through_color: Strike through color.
  • font_name: Name of font.
  • font_type: Font type (like "bold" or "italics-bold").

Code Folding is also made easy. Any grammar rule can be code folded.


foldDefinition = "FOLD" ":" identifier [ ":" parameters ( "\"" text "\"" ) | methodCall ]

Where identifier is name of some grammar rule.


FOLD:additiveExpression:"$multiplicativeExpression$ + $additiveExpression$"

If some part of code is folded, there is some text written in place of it. There are three ways how to specify this text.

  1. If you do not specify the text, default text ("...") is used.
  2. You can specify text directly. And you can use some expressions inside this text.
  3. Text can be obtained from some method call.
Here is my code which is an example for it.

Here is a screen shot of sample vhdl file in Netbeans. It looks very cool for me!!

I'm exploring more and more. There are many more things included in this brilliant project. I wish to come up with it in future. It has made life so easy for coders like me :)

Saturday, April 5, 2008

My First Post

Hi folks,
Finally I'm set to blog! I must say that this is the brilliant way of communicating with the world. Thanks to blogger.com. Well it's a fun IMO to blog. Its a kinda brain draining. Whateva it is, I will try to put up everything in a decent manner.

Thursday, April 3, 2008

NetBeans Blog Contest

Hi Folks,
My blog will be mainly Netbeans centric! I'm set to submit this blog for the NetBeans Blogging contest. Its a wonderful opportunity to discover plethora of things in NetBeans the only IDE you need. I'm set for it now. Wish me all the best frenz!