Configuring Java JDK on Ubuntu

This is an easy way to configure java on a linux box, all this information is available online.

First we need to obtain the build.

sudo wget http://192.168.201.47:8000/jdk-7u75-linux-x64.gz

Extract from tar

 sudo tar xzvf  jdk-7u75-linux-x64.gz

Create symbolic link so we can later update the version

sudo ln -s /opt/jdk1.7.0_75/ /opt/java

we edit the /etc/profile and add following two lines

export JAVA_HOME=/opt/java
export PATH=$JAVA_HOME/bin:$PATH

finally we ‘source’ the file

source /etc/profile

At this point you should be ready to go, we can verify this by executing

java -version

Starting jetty via command line an nohup

Somehow I am getting problems starting Jetty via

service jetty start

We will be using unix command called nohup
“Nohup is a unix command, used to start another program, in such a way that it does not terminate when the parent process is terminated.”

I have opted out for using this

nohup java -jar start.jar -Djetty.port=8085

while this works it shown an message

nohup: ignoring input and appending output to `nohup.out'

to fix that up we need to redirect in put and output to /dev/null

 nohup java -jar start.jar -Djetty.port=8085  /dev/null &

Taking heap dump of java process on linux and windows

Taking a heap dump from console when Java VisualVM and JMX is not available to us.
We will use following tools

    • jmap
    • jps
    • ps

Dumping heap requires two steps
1) Obtaining target process id
2) Dumping heap for given pid

First we need to obtain the target process id we would like to dump, here I will show couple ways I like to use.

ps aux | grep 'java'
-----
userx     29901  6.7 47.0 25418812 3848276 ?    Sl   Mar23  85:42 /opt/java/bin/java -Djava.util.logging.config.

Here second column indicates our process id (pid)

Second method that is quite useful to obtain pid for java processes

uxserx@WS4:/opt/java/bin# ./jps -l
4281 sun.tools.jps.Jps
29901 org.apache.catalina.startup.Bootstrap

As we see both methods returned us pid of 29901
Npw to perform the dump we issue our second command

userx@WS4:/opt/java/bin# ./jmap -dump:format=b,file=/tmp/heapdump-001.hprof 29901
Dumping heap to /tmp/heapdump-001.hprof ...

At this point we have our heap dump that is ready to be analyzed, for my analysis I use two tools. Eclipse Memory Analyzer (MAT) and Java Visual VM

Accessing data of leptonica PIX data

This is mainly as a reference

 
/**
 * Get Pixel value at given  point
 */
l_uint32 pixAtGet(PIX* pix, int_t x, int_t y)
{
    l_int32 wpl    = pixGetWpl(pix);
    l_uint32* data = pixGetData(pix);
    l_uint32* line = data + y * wpl;
    l_uint32 value = GET_DATA_BYTE(line, x);
    return value;
}

To set a pixel value we can use this

/**
 * Set Pixel value at given  point
 */
void pixAtSet(PIX* pix, int_t x, int_t y, byte_t value)
{
	l_int32 wpl     = pixGetWpl(pix);
	l_uint32* data  = pixGetData(pix);
	l_uint32* line  = data + y * wpl;
	SET_DATA_BYTE(line, x, value);
}

Tokenizing/splitting string in c++

This method uses strtok to tokeninze our string given a specific delimeter, results of that are put into supplied vector. There are few other ways we can do this but this one is straight forward.

#include <iostream>
#include <string>
#include <string.h>
 
#include <memory>
#include <stdlib.h>
#include <stdio.h>
#include <list>
#include <vector>
 
using namespace std;
 
void split(vector<string>& out, const string& in, const string& delim)
{
  char* lc = (char*) malloc(in.size());
  strcpy(lc, in.c_str());
  strtok(lc, delim.c_str());
  while (lc)
    {
      string s = lc;
      out.push_back(s);
      lc = strtok(NULL, delim.c_str());
    }
  free(lc);
}
 
int main(int argc, char* args[])
{
  string str = "apple,organge,cherry";
  vector<string> o1;
  split(o1, str, ",");
 
  for (int i = 0; i < o1.size(); ++i)
  {
     cout << "token = " << o1[i] <<endl;
  }
 
  return 0;
}

Results

Supplied string : apple,organge,cherry
Delemeter : “,”
Output

  • apple
  • organge
  • cherry

Calculating partial Hausdorff Distance

 
struct Point
{
	Point(int_t _x, int_t _y) : x(_x), y (_y)
	{
 
	}
 
	int_t x;
	int_t y;
};
 
 
typedef std::list<Point*> points_t;
 
double euclideanDistance(const Point& lhs,const Point& rhs)
{
	 double p1 = std::pow((float)(rhs.x - lhs.x), 2);
	 double p2 =  std::pow((float)(rhs.y - lhs.y), 2);
	 double vd =  std::sqrt(p1 + p2);
 
	 return vd;
}
 
 
double hausdorffPHD(points_t seta, points_t setb)
{
    double maxDistance = 0;
 
    points_t::iterator afront = seta.begin();
    points_t::iterator aback  = seta.end();
 
    std::vector<double> ranking;
 
    for(int_t i=0; afront != aback ; ++afront, ++i)
    {
    	Point* a = *afront;
        double minDistance = std::numeric_limits<double>::max();
 
        points_t::iterator bfront = setb.begin();
        points_t::iterator bback  = setb.end();
 
    	for(; bfront != bback ; ++bfront)
	    {
    		Point* b = *bfront;
    		double ed = euclideanDistance(*a, *b);
 
            if (ed < minDistance)
                minDistance = ed;
	    }
 
    	ranking.push_back(minDistance);
    }
 
    std::sort(ranking.begin(), ranking.end());
 
    double fraction = .7;
    int k = (int) (seta.size() * fraction);
    return ranking[k];
}
 
 
double hausdorff(points_t seta, points_t setb)
{
    double habPHD = hausdorffPHD( seta, setb);
    double hbaPHD = hausdorffPHD( setb, seta);
    double distancePHD = std::max(habPHD, hbaPHD);
    printf("hd = %0.4f\t %0.4f\t %0.4f\t \n", distancePHD, habPHD, hbaPHD);
    return distancePHD;
}
 
 
int_t main(int_t argc, char_t** args)
{
 
	points_t seta;
	points_t setb;
 
	seta.push_back(new Point(1,2));
	seta.push_back(new Point(2, 4));
 
	setb.push_back(new Point(2, 4));
	setb.push_back(new Point(3, 4));
 
	double val = hausdorff(seta, setb);
}

Compiling Webkit on Windows using Visual Studio 2012

Just some notes on compiling WebKit on windows with visual studio.

Issues :

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xrefwrap(431): error C2064: term does not evaluate to a function taking 1 arguments (..\..\win\WebCoreSupport\WebFrameLoaderClient.cpp)
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(239) : see reference to function template instantiation '_Ret std::_Callable_obj<_Ty>::_ApplyX<_Rx,WebCore::PolicyAction>(_V0_t &&)' being compiled
25>          with
25>          [
25>              _Ret=void,
25>              _Ty=int,
25>              _Rx=void,
25>              _V0_t=WebCore::PolicyAction
25>          ]
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(239) : see reference to function template instantiation '_Ret std::_Callable_obj<_Ty>::_ApplyX<_Rx,WebCore::PolicyAction>(_V0_t &&)' being compiled
25>          with
25>          [
25>              _Ret=void,
25>              _Ty=int,
25>              _Rx=void,
25>              _V0_t=WebCore::PolicyAction
25>          ]
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(239) : while compiling class template member function 'void std::_Func_impl<_Callable,_Alloc,_Rx,_V0_t>::_Do_call(_V0_t &&)'
25>          with
25>          [
25>              _Callable=_MyWrapper,
25>              _Alloc=std::allocator>,
25>              _Rx=void,
25>              _V0_t=WebCore::PolicyAction
25>          ]
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(515) : see reference to class template instantiation 'std::_Func_impl<_Callable,_Alloc,_Rx,_V0_t>' being compiled
25>          with
25>          [
25>              _Callable=_MyWrapper,
25>              _Alloc=std::allocator>,
25>              _Rx=void,
25>              _V0_t=WebCore::PolicyAction
25>          ]
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(515) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
25>          with
25>          [
25>              _Ret=void,
25>              _V0_t=WebCore::PolicyAction,
25>              _Ty=int,
25>              _Alloc=std::allocator>,
25>              _Fty=int
25>          ]
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(515) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
25>          with
25>          [
25>              _Ret=void,
25>              _V0_t=WebCore::PolicyAction,
25>              _Ty=int,
25>              _Alloc=std::allocator>,
25>              _Fty=int
25>          ]
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(515) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Reset_alloc<_Ty,std::allocator>>(_Fty &&,_Alloc)' being compiled
25>          with
25>          [
25>              _Ret=void,
25>              _V0_t=WebCore::PolicyAction,
25>              _Ty=int,
25>              _Fty=int,
25>              _Alloc=std::allocator>
25>          ]
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(515) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Reset_alloc<_Ty,std::allocator>>(_Fty &&,_Alloc)' being compiled
25>          with
25>          [
25>              _Ret=void,
25>              _V0_t=WebCore::PolicyAction,
25>              _Ty=int,
25>              _Fty=int,
25>              _Alloc=std::allocator>
25>          ]
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Reset<_Ty>(_Fty &&)' being compiled
25>          with
25>          [
25>              _Ret=void,
25>              _V0_t=WebCore::PolicyAction,
25>              _Ty=int,
25>              _Fty=int
25>          ]
25>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Reset<_Ty>(_Fty &&)' being compiled
25>          with
25>          [
25>              _Ret=void,
25>              _V0_t=WebCore::PolicyAction,
25>              _Ty=int,
25>              _Fty=int
25>          ]
25>          ..\..\win\WebCoreSupport\WebFrameLoaderClient.cpp(97) : see reference to function template instantiation 'std::function<_Fty>::function(_Fx &&)' being compiled
25>          with
25>          [
25>              _Fty=void (WebCore::PolicyAction),
25>              _Fx=int
25>          ]
25>          ..\..\win\WebCoreSupport\WebFrameLoaderClient.cpp(97) : see reference to function template instantiation 'std::function<_Fty>::function(_Fx &&)' being compiled
25>          with
25>          [
25>              _Fty=void (WebCore::PolicyAction),
25>              _Fx=int
25>          ]

Patch
WebFrameLoaderClient.cpp
Line 97
– : m_policyFunction(0)
+ : m_policyFunction(nullptr)

Issue:

25>WebKitGUID.lib(WebKit_i.obj) : error LNK2005: _IID_IDOMDocumentType already defined in uuid.lib(i_mshtml.obj)
25>WebKitGUID.lib(WebKit_i.obj) : error LNK2005: _IID_IDOMProcessingInstruction already defined in uuid.lib(i_mshtml.obj)
25>WebKitGUID.lib(WebKit_i.obj) : error LNK2005: _IID_IDOMEvent already defined in uuid.lib(i_mshtml.obj)
25>WebKitGUID.lib(WebKit_i.obj) : error LNK2005: _IID_IDOMUIEvent already defined in uuid.lib(i_mshtml.obj)
25>WebKitGUID.lib(WebKit_i.obj) : error LNK2005: _IID_IDOMKeyboardEvent already defined in uuid.lib(i_mshtml.obj)
25>WebKitGUID.lib(WebKit_i.obj) : error LNK2005: _IID_IDOMMouseEvent already defined in uuid.lib(i_mshtml.obj)
25>WebKitGUID.lib(WebKit_i.obj) : error LNK2005: _IID_IDOMMutationEvent already defined in uuid.lib(i_mshtml.obj)
25>WebKitGUID.lib(WebKit_i.obj) : error LNK2005: _IID_IDOMWheelEvent already defined in uuid.lib(i_mshtml.obj)

Added linker option to WebKitGUID /FORCE:MULTIPLE
Now we get warning instead of errors;

13>WebKitGUID.lib(WebKit_i.obj) : warning LNK4006: _IID_IDOMDocumentType already defined in uuid.lib(i_mshtml.obj); second definition ignored
13>WebKitGUID.lib(WebKit_i.obj) : warning LNK4006: _IID_IDOMProcessingInstruction already defined in uuid.lib(i_mshtml.obj); second definition ignored
13>WebKitGUID.lib(WebKit_i.obj) : warning LNK4006: _IID_IDOMEvent already defined in uuid.lib(i_mshtml.obj); second definition ignored
13>WebKitGUID.lib(WebKit_i.obj) : warning LNK4006: _IID_IDOMUIEvent already defined in uuid.lib(i_mshtml.obj); second definition ignored
13>WebKitGUID.lib(WebKit_i.obj) : warning LNK4006: _IID_IDOMKeyboardEvent already defined in uuid.lib(i_mshtml.obj); second definition ignored
13>WebKitGUID.lib(WebKit_i.obj) : warning LNK4006: _IID_IDOMMouseEvent already defined in uuid.lib(i_mshtml.obj); second definition ignored
13>WebKitGUID.lib(WebKit_i.obj) : warning LNK4006: _IID_IDOMMutationEvent already defined in uuid.lib(i_mshtml.obj); second definition ignored
13>WebKitGUID.lib(WebKit_i.obj) : warning LNK4006: _IID_IDOMWheelEvent already defined in uuid.lib(i_mshtml.obj); second definition ignored
13>C:\cygwin\home\gbugaj\WebKit\WebKitBuild\Debug_WinCairo\bin32\WebKit.dll : warning LNK4088: image being generated due to /FORCE option; image may not run

Building Dependencies

Cairo

Issue

gbugaj@LTRMS7GB /cygdrive/c/cygwin/home/gbugaj/cairo
$ make -f Makefile.win32  CFG=release

make[1]: Entering directory '/cygdrive/c/cygwin/home/gbugaj/cairo/src'

cairo-deflate-stream.c
e:\source\c-libraries\zlib-1.2.3-lib\include\zconf.h(289) : fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory
../build/Makefile.win32.common:55: recipe for target 'release/cairo-deflate-stream.obj' failed
make[1]: *** [release/cairo-deflate-stream.obj] Error 2
make[1]: Leaving directory '/cygdrive/c/cygwin/home/gbugaj/cairo/src'
Makefile.win32:12: recipe for target 'cairo' failed
make: *** [cairo] Error 2

Fix is to add empty ‘unistd.h’ to zlib include directory