Mini Porting guide of Linux Applications to Windows + NailGun (for faster startup of Java Apps) Windows Port Available + Review of NailGun

By Angsuman Chakraborty, Gaea News Network
Tuesday, November 23, 2004

Contents

  • NailGun Introduction
  • Rationale for Porting
  • Compiler choice: MinGW versus CygWin
  • Mini Porting guide from Linux to Windows (networking focussed)
  • Testing
  • Code & Executable Download
  • Suggestions for improving NailGun


NailGun Introduction

I was intrigued by the concept of nailgun which provides a way to run small Java apps significantly faster than the current procedure (load the jvm, run the app, upload the jvm). The main issue with using the Sun JVM (java.exe) is the startup time of the JVM. For long running applications it is a non-issue, however for short running ones…
Note that Sun too has recently made some improvements by caching the jars of an application to reduce the startup time for bigger applications. That still doesn’t address the startup time issue. So I think the solutions are complementary. It would be interesting if someone can do a benchmark on Windows.

The key concept of NailGun is that a Java server keeps running in the background and a small executable client ( written in C for speed ) is used to send the application details to the server for execution, details like application class name, command line arguments etc.

As you can see from the above web page it can significantly speed up the startup time. Also repeated running will conceivably further improve performance as the classes are already loaded in the JVM.

This is ideally suited for small command line applications like grep, wc, sort etc. , more so when you want to do some processing in a loop.

Rationale for porting & Why me?
I think it can prove to be an useful tool in your toolbox specially for windows programmers as windows lack several useful command line utilities which can be trivially implemented (and has been implemented in many cases) in Java.

I lead a massive porting effort 8 years back of porting over a million lines of C code (running on Solaris, AIX & Mac) to windows nt and windows 3.1. I had to deal with socket issues among a host of other. However it was on MSVC & Borland compilers.
So I thought yesterday to try my hand at porting some code on MinGW. My incentive was to re-hash the experience I had gained and hopefully learn some more.

Why MinGW?

I avoided Cygwin because of its dependence on cygwin dll and licensing issues. Nailgun is on Apache license. Cygwin is on GPL. This will force the executable to have more restrictive license than the original intention which is not desired.

Mini Porting guide from Linux to Windows (networking focussed)
Several networking related Linux headers files are not available in Windows. Instead winsock.h or winsock2.h is a good replacement for them. You can use the following template:

#ifdef WIN32
#include <winsock.h>
#else
#include <netdb .h>
#include <arpa /inet.h>
#include <netinet /in.h>
#include <sys /socket.h>
#include <sys /types.h>
#endif

The file separator is different as you all know:

#ifdef WIN32
#define FILE_SEPARATOR ''
#define MSG_WAITALL 0
#else
#define FILE_SEPARATOR '/'
#endif

Oh yes, MSG_WAITALL is not defined in Windows. The value 0 serves the purpose.

Windows implements Berkeley Socket in a half-hearted way. It requires invoking WSAStartup before making any socket calls. This is obviously not a requirement in Unix/Linux systems. A template code can be as follows:

#ifdef WIN32
    WSADATA wsaData;
    WORD wVersionRequested;
    int err;

    wVersionRequested = MAKEWORD( 2, 0 ); // 2.0 and above version of WinSock
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        fprintf(stderr, "Couldn't not find a usable WinSock DLL.n");
        exit(NAILGUN_CONNECT_FAILED); // Replace with your own exit code
    }
#endif

Winsock version 2.0 and above is required for select functionality.

And don’t forget to cleanup when done:

#ifdef WIN32
	WSACleanup();
#endif

select doesn’t work on files under windows. This is a major difference from Unix/Linux systems where files are treated as sockets. Hence implemneting non-blocking calls on Windows becomes a hassle. For I/O from console kbhit() can be used to find there was a key press (except certain control keys) and then getch() (no echo) or getche() can be used to fetch the characters. However the same technique doesn’t work when another program is using a pipe to send data to this program. Then overlapped I/O (Windows name for non-blocking I/O) can be used (ReadFile() or ReadFileEx()). ReadFileEx() is used for overlapped I/O only, whereas ReadFile() is used for both.

I found a good article which is better than Microsoft documentation on the subject.

This is one part where the port is incomplete. It handles keyboard input well. However it doesn’t handle piped inputs. I leave it as a challenge for anyone who wants to gather experience on async file I/O in Windows platform.

perror() doesn’t work for Socket calls. WSAGetLastError() must be used to get the error, if any, in the last socket call.
Note that for File I/O etc. GetLastError() returns the error code.

For other details you can refer to Microsoft porting guide guide.

Interestingly the guide is silent on the above mentioned critical issues of porting.

Final Notes:
1. Always remember to check and possibly handle the error codes.
2. Remember to close the sockets before exiting

Testing

The code has been tested only on Windows 2000 Professional.

However I think it should work well on Windows NT and XP versions. About Windows 98 (Is it an Operating System or just a game playing console?) and Windows ME I am not too sure.

Where do I get it?
I have made available the source code of the C port of ng.c (client) and the resulting executable for invoking nailgun server. The rest should be downloaded from Source Forge. The executable has been checked with AVG Virus Scanner with latest update installed. However I insist that you too check it with your virus scanners.

Note:
The orginal code was developed by Martian Software. I just ported it to Windows.

Disclaimer:
The code and executable software is supplied “as is”. The publisher disclaims all warranties, expressed or implied, including, without limitation, the warranties of merchantability and of fitness for any purpose. The publisher assumes no liability for damages, direct or consequential, which may result from the use of the provided code or executable or any other software program downloaded from the Website. The entire risk as to the quality and performance of the software is with you, and you assume the entire cost of any necessary servicing, repair or correction.

Please check the instructions.

Suggestions for improving NailGun
1. Allow recognizing -cp (and other Java options) and load the classes dynamically
2. Is it using a Thread Pool? If not it should be used
3. Ability to reload classes
4. Ability to modify classes to disable System.exit and make the server more robust
5. Use a Java client and compile using MinGW on Windows

Discussion
November 24, 2004: 12:11 am

Few Blog Entries of Note + Look at current ORM’s
Please see my posts on my other weblog :

Another look at Internet Explorer (IE) security
Several items like Linux to Windows Porting, Introduction, Review of NailGun, Download for Windows port of NailGun and misc. items
Interesting arti

November 23, 2004: 10:29 pm

How many backslash does WordPress require? I had to use 8 backslashes in the above post to get WordPress to generate two backslashes in the output (for specifying the file separator) !
Wonder how many levels of processing the document endures before being published…
And then I realized that if I edit the document, all the backslashes vanishes and only two remains!

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :