Tuesday 17 December 2013

Run a java application as a service (deamon) on linux

A simple solution is to create a script start.sh that runs Java through nohup and then stores the PID to a file:
nohup java -jar myapplication.jar > log.txt 2> errors.txt < /dev/null &
PID=$!
echo $PID > pid.txt
Then your stop script stop.sh would read the PID from the file and kill the application:
PID=$(cat pid.txt)
kill $PID
#Collected from stackoverflow

Monday 16 December 2013

Tools to profile memory usage

1. MemCheck:
Dr. Memory. It is a relatively new tool, works very well on Windows 7. My favorite feature is that it groups the same leaks' allocation stacks in the report.
I have also used UMDH( http://support.microsoft.com/kb/268343 ) and found it quiet useful and easy to setup. It works from Win2000 to Win7.
AppVerifier is a must have swissknife for windows native code developers, its "memory" checker does similar job http://msdn.microsoft.com/en-us/library/dd371695%28v=vs.85%29.aspx
2. Callgrind:
My favorite is verysleepy ( http://www.codersnotes.com/sleepy ) It is tiny but very useful and easy to use.
If you need more features, AMD CodeAnalyst™ Performance Analyzer is free:http://developer.amd.com/documentation/videos/pages/introductiontoamdcodeanalystperformanceanalyzer.aspx
Windows Performance Analysis tools is free from Microsoft, not very easy to use but can get the job done if you are willing to spend the time.http://blogs.microsoft.co.il/blogs/sasha/archive/2008/03/15/xperf-windows-performance-toolkit.aspxDownload: http://msdn.microsoft.com/en-us/performance/cc752957
3. Massif:
Similar(not quite exact match) free tools on windows are:
4. Cachegrind:
Above mentioned Windows Performance Tools has certain level of L2 cache miss profiling capability but not quite as good and easy to use as Cachegrind.
5. DRD:
Haven't found anything free and as powerful on Windows yet, the only free tool for windows I can find that is slightly close is the "lock" checker in AppVerifier: http://msdn.microsoft.com/en-us/library/dd371695%28v=vs.85%29.aspx

#reference: stackoverflow

Thursday 12 December 2013

Build sample Android NDK on Linux

1. Download Android-NDK (i'm using android-ndk-r8-crystax-1)
2. Create a simple Java wrapper file named NativeLib.java
package com.pt;

/**
 * @author khanh
 *
 */
public class NativeLib {
    static {
        System.loadLibrary("ndk_sample");
    }

    /**
     * Adds two integers, returning their sum
     */
    public native int add(int v1, int v2);

    /**
     * Returns Hello World string
     */
    public native String hello();
}
3. Create C header file by running javah -jni
javah -jni com.pt.NativeLib
mv mv com_pt_NativeLib.h ../jni/
4. Write implement C file for com_pt_NativeLib.h
 #include "com_pt_NativeLib.h"

JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
  (JNIEnv * env, jobject obj) {
        return (*env)->NewStringUTF(env, "Hello World!");
}

JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
  (JNIEnv * env, jobject obj, jint value1, jint value2) {
        return (value1 + value2);
}
5. To build library, create file Android.mk to lead compilers how to build
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := ndk_sample
LOCAL_SRC_FILES := ndk_sample.c

include $(BUILD_SHARED_LIBRARY)
6. Create folder in /apps/ndk_sample/ and create Application.mk
APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES      := ndk_sample
7. Create symbol link to the project by typing
ln -s ~/home/Android/NDKSampleProj /apps/ndk_demo/project
8. Finally, run make APP=ndk_sample that will generate libs/armeabi/libndk_sample.so
9. Enjoy the coding now with native code.

Monday 9 December 2013

Install & Configure Qt with Visual Studio

1. Download and extract Qt
2. Set environment variable: QMAKESPEC (..\Qt4\4.8.5\mkspecs\win32-msvc2005)
3. Goto extracted folder of Qt, then open Visual Studio Command Prompt, then type configure.exe with relevant options. Example: configure.exe -platform win32-msvc2005
4. Finally, type: nmake (This will take a very long time. Go out to dinner or something).
5. Reboot computer and enjoy programming with Qt

Wednesday 19 December 2012

Install SVN Server on CentOS


Install SVN


Install mod_dav_svn right away which is required to make SVN work with Apache.
$ yum install mod_dav_svn subversion

Create a repository
$ svnadmin create /path/to/

Permissions to access the repository
$ chown -R apache:apache /path/to/


Setup SVN Users

Config file for the repository to allow users access to it
$ vi /path/to//conf/svnserve.conf

Uncomment the lines like so:
auth-access = write
password-db = passwd

Create a htpasswd file which will hold users and passwords for accessing to this repository
$ htpasswd -c /path/to//conf/passwd


Configure Apache

Edit the following file:
$ vi /etc/httpd/conf.d/subversion.conf

LoadModule dav_svn_module modules/mod_dav_svn.so
LoadModule authz_svn_module modules/mod_authz_svn.so
DAV svn
SVNPath /path/to/
Authtype Basic
AuthName "My Repository"
AuthUserFile /path/to//conf/passwd
Require valid-user
 

The path /svn is where the repository will be served out of.
Finally, restart apache for new configuration to take affect:
$ service httpd restart