Monday, September 10, 2007

Porting fron VC6 to VC7 compiler errors

------------------------------------------------------------------------

Code which compiles in VC6

typedef priority_queue*,//value type
deque*>,//container type
Command::Compare > REQUESTQUEUE; //comapritor

typedef map::iterator REQUESTQUEUEITR;


std::map RequestQueue;
std::map::iterator CurrentItr;


Changes to be done to compile in VC7
------------------------------------------------------------------------

typedef priority_queue*,//value type
deque*>,//container type
typename Command::Compare > REQUESTQUEUE; //comapritor

typedef typename map::iterator REQUESTQUEUEITR;


//todo make this a priority queue - priority_queue

std::map RequestQueue;
typename std::map::iterator CurrentItr;

------------------------------------------------------------------------

tags

vc7 nested class template cannot access private class declared in class

vc7 dependent name is not a type

Tuesday, May 22, 2007

A little Perl goes a long way

I have found Perl as very powerful language , a must in the toolkit for any programmer

Here are some Perl code snippets.

It is always better to start your perl code with

use strict;
use warnings;

With this you have to declare variable before you use them

like

my $SearchStep=0;
my $line;

Also if you want to use special Perl modules like for example working with ini files , you
include it

use IniFiles;

Everything is pretty simple in Perl, only thing is that the syntax can become unreadable; so
put in a lot of comments

# This is a comment

Okay let us jump in -- here is the code to open a file

my $FilePath = "system1.txt";
my $opened =open (FH, $FilePath) ;
if( !$opened)
{
print "Cannot open file\n";
die;
}

and to read line by line from the file

my $line;
while ( $line =)
{
chomp ($line);
:

Now to test if a line matches a perl pattern

if($line=~m/$SearchPattern/i)

/i stands for case insensitive search.

Okay a few other matche examples ( basically the better you are at regular expression the more powerfully you can drive perl)

my $regex = join '|', qw(cat,cow,hen);
if($line=~m/\b(?:$regex)\b/i)

(qw- means quote word, basically puts "" )

matching a pettern

if($line=~m/\b(\d\.\d\.\d)\b/i)

Okay now there is another operator ( the substitute operator s// )

Here are some examples for the same

my $the = "The";
$left=~s/^$the\s+//i; #replace "The" with nothing match no case

In case you are creating an XML file these substitutions will be helpful, otherwise
the less than "<" and greater than ">"symbols interfere

$left=~s/\&/and/g; #replace & with 'and' , g - continue till end
#( note special charectes like & is preceeded by the escape charecter \ - \& )
$token=~ s/\s+//g; #replace space with nothing, g - continue till end

$line2=~s/\</& lt;/g; #replace < with <
$line2=~s/\>/& gt;/g; #replace > with >

(note there should be no spaces between '& gt'-- html rendering pblms )


Okay here is another way to take care of all the non word charectes that occur inthe
string and causes wrong string concatination etc.

#the \Q=quote non-word characters till \E
# if( ! ($g_Accumilated=~m/\Q$line2\E/) )

Here is another tool to split up a line on either side of a token

@list =split(/$token/i,$line2);

$left=$'; # the left part of the matched string is in the system variable $'
#right part is see http://www.perl.com/doc/manual/html/pod/perlvar.html

The @ symbols precceds an array variable defenition

To traverse through the array

foreach( (@list) )
{
print ("Item=",$_, "\n");
}

The system variable $_ contains the current variable

What if you want to write a function , you define it as

sub GetToken
{



Okay what if it has to take parameters

sub GetToken
{

my ($line2,$token) = @_; #the parmaters are in the default array variable @_

:

}

and how to call it -

GetToken($line,"Split token");

A few more tokenizing examples

#tokenize the line based on spaces
@tokens = split(/ +/, $line);

Adding one string to another using the . operator

$g_Accumilated = $g_Accumilated . $line2 . "\n";

Okay now on to another powerful container - Perl hash maps

Declaring a hash map

my %pbNameValue = ();

Populating it

$pbNameValue{$tempKey} =$tempVal;


Sorting the hash map

#sort based on the nueric value of the key ie 1 2 10 and not like 1 10 2
my $key;
my %tmpNameValueHash = ();

foreach $key (sort {$a <=> $b } keys %pbNameValue)
{

Iterating through the hash map

foreach $key (keys %tmpNameValueHash )
{










Thursday, April 26, 2007

Java JVM high memory usage problems

Though I have some three four years experience in C++ ,I did not have
that much oppurtunity to work in Java. Currenly I was doing some
analysis for a very simple CLI and was surprised to come with a memory
restriction. I found that if I use Java for developing the CLI
application I will be exhausting the memory of our Application Server
(AS). Just to mention the architecture ,users (telecom operators) use
a metaframe server client ( =something like remote desktop) to login
to the AS and then open a GUI to work on it. With Java I can service
only about 15 clients with the available memory . I just checked the
reason for this resource crunch and found that already many Java based
GUI's are served by the AS and each is taking some 25 MB or more. The
first thing I thought is that adding more RAM will solve this ( though
this is not an easy option) . Then I understood that a 32 bit system
can have just about 3 gb ram for applciations and our AS had already 4
GB with 1.5 GB VM also configured. Then I thought there might be ways
to make the JVM shareable. But no. Also tried to use the flags to fine
tune JVM. No go there also. Irrespective of how much you limit the
actual limit is in the heap size allocated to the application and not
to the private bytes of JVM.

-----------------------------------------
Data from Jconsole for the Java application

Memory
Current Heap Size 4.3 MB
Max heap size 12. 2 MB ( set by flag)
Committed memory 5 MB
Operating System
Committed VM - 26.7 MB

Data from Perfmon (for java.exe)

Private Bytes - 27.7 MB
----------------------------------------

I am part of a large team that developes and maintains a telecom
network managment software system. Also this software is used all
around the globe almost in around 190 countries usually by telecom
service providers. So this is a very real problem that I am speaking
about. I am getting to love the simplicty of Java and of the great IDE
Eclipse as much as I love the power of STL; but I am comming face to
face with the main constraint of Java, its memory footprint and I
guess thousands of others like me must have faced similar problems.

If so the next question is what is SUN doing about this. I could not
find it in their top 25 RFE's or top 25 bugs. (http://bugs.sun.com/
bugdatabase/top25_bugs.do http://bugs.sun.com/bugdatabase/
top25_rfes.do )

1) Is this really not a problem in the outside world then ?

2) Or is it that with the introduction of 64 bit HW and serveres this
will be of no significance ?

Anyway I doubt if companies like ours will adopt 64 bit HW and servers
because of the costs involved in it ( note this is just a logical
guess, I have no experience in such decisions)

Anyway I am now forced to use C++ for the client and use Java on the
server side. Fortunately most of the Java in the server side is done
up as EJB's all conatined in the JBoss server thus consuming only one
JVM there)
-------
I posted this in a popular Java group and did not get any meaningful replies. So I guess this is one limitation that people are living with ; or that J2EE frameworks amalgamates everything to one large chunk of VM.


Monday, April 23, 2007

Windebug and Adplus for debugging process hang

1. Set the symbol, especillay windows symbols
.......26_Merged\UNCRelease;\\blrm2fsp\SCSD-Common\Sym\WinSym2K3SP1

2. Set the souce and image

3. Load the extension dll ( for !locks command ) if not already loaded

.load C:\Program Files\Debugging Tools for Windows\winxp\kdexts.dll

4. Use the !ntsdexts.locks ( or !locks) command to see a list of critical section

:004> !ntsdexts.locks

CritSec ntdll!LdrpLoaderLock+0 at 7C889D94
LockCount -6902
RecursionCount 1
OwningThread 2b3c
EntryCount 0
ContentionCount 28f3
*** Locked

CritSec +12f9b4 at 0012F9B4
LockCount 0
RecursionCount 0
OwningThread 0
*** Locked

CritSec +12f70c at 0012F70C
LockCount 0
RecursionCount 0
OwningThread 0
*** Locked

CritSec +128202c at 0128202C
LockCount -2
RecursionCount 1
OwningThread 3d9c
EntryCount 0
ContentionCount 0
*** Locked
-----------

A 'LockCount' other than 0 means that many threads are waitiing on this and it is the suspect

6. Do a ~* kb ( print stack trace for all threads)

and search for the critsection address (7C889D94) got from step 5, in the stack print.

Usually you will find many thread waiting for the same ( in this case thread id 163)

163 Id: 4d8.3c78 Suspend: 0 Teb: 7fe8e000 Unfrozen
ChildEBP RetAddr Args to Child
225dfbc8 7c822124 7c83970f 000001e4 00000000 ntdll!KiFastSystemCallRet
225dfbcc 7c83970f 000001e4 00000000 00000000 ntdll!NtWaitForSingleObject+0xc
225dfc08 7c839620 00000000 00000004 00000000 ntdll!RtlpWaitOnCriticalSection+0x19c
225dfc28 7c81a86c 7c889d94 00000000 7ffde000 ntdll!RtlEnterCriticalSection+0xa8
225dfcb8 7c81b22d 225dfd28 225dfd28 00000000 ntdll!LdrpInitializeThread+0x68
225dfd14 7c82ec2d 225dfd28 7c800000 00000000 ntdll!_LdrpInitialize+0x16f
00000000 00000000 00000000 00000000 00000000 ntdll!KiUserApcDispatcher+0x25

( the third parameter has the critical section address)


5. !critsec 7c889d94

CritSec ntdll!LdrpLoaderLock+0 at 7C889D94
LockCount -6902
RecursionCount 1
OwningThread 2b3c
EntryCount 0
ContentionCount 28f3
*** Locked

6.. Use ~ to get thread list; Check if you can find the thread in the dump.

If not ,then you have two things to do - Google about this locked critsection assuming it is a windows one,which gives you a hint about the suspect code parts. ( in this case TerminateThread calls from somewhere in your process)

Also you can attach Adplus ( part of windows debug package) to see if there was any exception that had occurred.
The Adplus gives you a dump of the process at the time when the exception is thrown .

Analysing each first and second chance exception is
important as this can also lead to memory corruption, which can manifest later on in many ways

7. Adplus setting F:\\Tools\ADPlus>adplus -pn proceesname.exe -crash -FullonFirst -quiet .

This will dump when ever an exception happens ( even if you are 'handling' it in the code using cach(…) )

Do a !analyze -v command on the dumps ( after setting the symbols correct) and this will give you the stack trace of the crashing code.

Correct these in source and run with adplus again.

Sooner or later you will find the problem area. And of course the solution.

Wednesday, February 21, 2007

windbg command

Wednesday, 25 August 2004

To list all modules loaded by the process -LM
(shows the loaded module address also)



: WinDebug thread commands

To see all threads ~
To freeze all threads ~*F
to unfreeze thread 1 ~1U
to set the thread ~S

copying to stl list
If destination is an empty list


lstOfInt.push_back(1);
lstOfInt.push_back(2);
lstOfInt.push_back(3);

copy(lstOfInt.begin(),lstOfInt.end(),lstOfIntDest.begin() ); -->WRONG
copy(lstOfInt.begin(),lstOfInt.end(),back_inserter(lstOfIntDest) ); -->CORRECT

Tuesday, February 20, 2007

CORBA - C++ Client (ACE TAO) Java Server (JacORB) A simple example

Step 1
I downloaded the binary relaease of JacOrb
Extracted the zip file to
E:\Program Files\JacORB-2.2.4\
Step 2
Then also downloaded Ant
Also jdk was downloaded and installed
Step 3
Then created the batch file (to set the path)
Set ANT_HOME=E:\Program Files\Java_Ant\apache-ant-1.6.5
Set JAVA_HOME=E:\Program Files\Java\jdk1.5.0_03
Set JACORB_HOME=E:\Program Files\JacORB-2.2.4
Set Path=%ANT_HOME%\bin;%JAVA_HOME%\bin;%JACORB_HOME%\bin;%JACORB_HOME%;
Set CLASSPATH=%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib;%JAVA_HOME%;%JACORB_HOME%\lib;
----now to configure JacORB -
In the command promt
E:\Program Files\Java_Ant\apache-ant-1.6.5>ant jaco
E:\Program Files\Java_Ant\apache-ant-1.6.5>ant idlcmd
okay that’s done
Now write the idl

--- test1.idl------
module Quoter1
{
//a test interface
interface Stock
{
double price(); //to get the quote of the stock
readonly attribute string symbol;
readonly attribute string fullname;
};
//interface to get the stock object
exception InvalidStockSymbol {};
//to get the desired stock object by looking up the symbol name
interface Stock_Factory
{
Stock get_stock(in string stock_symbol)
raises (InvalidStockSymbol);
};
};
----------

You can do the steps in the programming guide
E:\Program Files\JacORB-2.2.4\doc\ProgrammingGuide\ ProgrammingGuide.pdf


Generating the Java server from the idl

1. First to generate the necessary .java files from the idl .
For this first run antconfig.bat (in zip) fie and then from the command promt type
E:\Coding\corba\Javaserver>idl -d ./generated2 test1.idl

This will generate the necessar tao classses

2. Second import all these files o Eclipse
Create an empty java project and use File -> Import o import the file system

3. Now youy can see that based on this idl there are two *POA classes defined
StockPOA and Stock_FactoryPOA

We have to extend from the POA class and then implement the methods which were not
implemented.

A) For this create a class name 'StockImpl' and give the base class as StockPOA.
You have to fill in the inplementation of the methods that eclipse has created
public class StockImpl extends StockPOA

B) Similary create a new class Stock_FactoryImpl with the base class as Stock_FactoryPOA
and fill in the parts

--------------
public class Stock_FactoryImpl extends Stock_FactoryPOA
{
public Stock get_stock(String stock_symbol) throws InvalidStockSymbol {
// TODO Auto-generated method stub
return null;
}
}
-----------
//Check out the StockPOA.java and Stock_FactoryPOA.java file in the source zip for implementaion

Then compile the jave parts and
Run the java server
first run the following batch file to set the path

antconfig.bat
-------------------
Set ANT_HOME=E:\Program Files\Java_Ant\apache-ant-1.6.5Set JAVA_HOME=E:\Program Files\Java\jdk1.5.0_03Set JACORB_HOME=E:\Program Files\JacORB-2.2.4
Set Path=%ANT_HOME%\bin;%JAVA_HOME%\bin;%JACORB_HOME%\bin;%JACORB_HOME%;Set CLASSPATH=%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib;%JAVA_HOME%;%JACORB_HOME%\lib;
------------------------------

E:\Coding\corba\Javaserver\generated\Quoter1\CorbaServer>java Quoter1.Server "E:\Temp\j_testior3.ior"


Creating the C++ client form the idl

First download the TAO_1.4.1.1_2.
Installation is just extracting this to a directory of your choise.

We use the test.idl specified above (obviously)

1.Generate the files needes using TAO idl

You can see the tutorial that comes with TAO for this
E:\Program Files\TAO_1.4.1.1_2\index.html
(\docs\release\TAO\tutorials\Simple\Client\index.html)

A. Set the TAO paths, use the batch files in taoconfig.bat
-----------------------------
taoconfig.bat
set ACE_ROOT="E:\Program Files\TAO_1.4.1.1_2"set TAO_ROOT="E:\Program Files\TAO_1.4.1.1_2"set TAO_IDL="E:\Program Files\TAO_1.4.1.1_2\bin\tao_idl.exe"set Path=%Path%;E:\Program Files\TAO_1.4.1.1_2\bin;E:\Program Files\TAO_1.4.1.1_2\lib;
-------------------------

B. In the command prompt type
%TAO_IDL% test1.idl
voila this will create the necessary files

2. Create a empty console project and fill in
See the source attached

Note - Add the generated test1C.cpp to the project ( generated from test.idl)

------main source----------
CORBA::ORB_var orb = CORBA::ORB_init(argc,argv,"");
printf("Initialised the ORB\n");
//do the work
//get the remote object
CORBA::Object_var factory_object = orb->string_to_object(argv[1]);
printf("Object string_to_object\n");
//now test if this object is actually the type that we need
Quoter1::Stock_Factory_var factory
= Quoter1::Stock_Factory::_narrow(factory_object.in() );
printf("Object Narrow over\n");
printf("Calling Quoter1::Stock get_stock \n");
CORBA::Object_var stockobj = factory->get_stock ("Infy");
printf("got the object. Calling narrow\n");
Quoter1::Stock_var stock = Quoter1::Stock::_narrow(stockobj);
printf("Exit::Stock get_stock \n");
int price =stock->price();
printf("Infy pice =%d\n",price);


//destroy the orb
orb->destroy();
-------------------------------------------------------
3.Run the C++ client
fisrst run the following batch file (taoconfig.bat) to set the path

(we are not using naming service or anything, just IOR)

E:\Coding\corba\client\Release>client.exe file://E:\Temp\j_testior3.ior
4. That’s all folks !!

Also chek out this bug
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4780678

Install/Upgrade NVIDI Driver in Ubuntu for CUDA SDK

Most linux distribution comes with the Nouveau https://nouveau.freedesktop.org/wiki/ display driver configured. If you need to use NVIDIA...