Writing better JNI code

Well, it seems Ron beat me to the punch to blog about this – but allow me to promote the recently published developerWorks article that I had a hand in bringing to life:  Best practices for using the Java Native Interface

Here is the summary:

The Java™ Native Interface (JNI) is a standard Java API that enables Java code to integrate with code written in other programming languages. JNI can be a key element in your toolkit if you want to leverage existing code assets — for example, in a service-oriented architecture (SOA) or a cloud-based system. But when used without due care, JNI can quickly lead to poorly performing and unstable applications. This article identifies the top 10 JNI programming pitfalls, provides best practices for avoiding them, and introduces the tools available for implementing these practices.

Many kudos to Michael Dawson for his wordsmithing and persistence to make this article happen, he really deserves the lion’s share of the credit.

Ron calls out his favorite pitfall as using the wrong JNIEnv.  I’ll pick the one that makes me laugh because it is sad but true:  Choosing the wrong boundary between native and Java code – believe it or not there was a real world example that motivated the sample code.  The customer had decided to go for a “pure java” solution for their embedded application, so the minimal amount of native code was written – two functions that could read or write individual bits (not bytes).  While the example is outlandish, many times the integration of legacy code via JNI is done without thinking of the cost of the boundary.

Let’s discuss the picking the right side of the JNI boundary for a minute.  The NIO implementation is a good example of trying to fix file performance by keeping the data on the “right” side of the JNI boundary.  While NIO provides other functions allowing for tighter OS integration, one key performance win is due to the file data is kept in native buffers.   If you’re trying to get the data into java objects to be manipulated, you are likely defeating the performance win of NIO by forcing all that file data across the JNI boundary.  Where NIO will excel is needing to pull a bunch of data from a file and put it in another file – if you don’t need to hoist that data up into the java object space, you’ll get close to pure native performance.

Please check out the article, or at least bookmark it and send it to people who write JNI code.   Maybe we’ll see fewer false JVM bugs that turn out to be JNI related problems.

JavaOne

JavaOne will be held next week (June 2-5th) at the Moscone Center in San Francisco, the same location where Google I/O was held this past week – and the week after JavaOne has Apple’s WWDC in the same location.

I won’t be attending this year, but my friend and colleague Trent Gray-Donald will be there presenting a session titled “Under the Hood: Inside a High-Performance JVM™ Machine” on Friday June 5th, 10:50am – 11:50am.  While you won’t find him on the Rock Star Wall of Fame, he was did earn that title back in 2007 – his sessions are usually well attended so if you want to see him in action go early for a good seat.  This year his presentation discloses more details about the IBM JVM technology than have been previously made available, expect to see assembly code snippets on screen during the presentation along with lots of technical detail.  His presentation should be a reasonably good primer in what it takes to be a “state of the art” JVM in today’s competitive market – and it will be presented by someone who has hands on experience creating the technology.

If you can’t make it in person, JavaOne has a pretty good track record of making past sessions available online. Of course you can also follow along on Twitter, and there appear to be two hashtags (#JavaOne and #JavaOneConf) being used to help track tweets.

As for what to expect from JavaOne this year, I’ll shy away from trying to make any firm predictions – but it has been announced there will be more details on the Java App Store.  The Oracle/Sun deal will certainly be one of the topics that can’t be avoided.  Java7 should get some sort of announcement along with some benchmarking fun I’m sure.  One camp to keep an eye on is the Microsoft keynote – the 1st time they’ve done one for JavaOne.  Last, but not least – IBM is a general session cosponsor thus Craig Hayman will be giving a talk as well (Thursday, June 4th, 5:30 – 6:15 p.m), one I’d recommend people attend if they can.

When DNS Fails

Tonight I needed to do a bit of work as we’re coming up to a deadline and as usual things are running a bit behind. Normally I just VPN into work and use remote desktop to drive my desktop. Unfortunately when I went to do this tonight I wasn’t able to connect to my workstation via remote desktop.

A quick NSLookup query turned up the problem – it seems the DNS server at work had been reset and the DHCP lease for my workstation had not expired yet.  This means that the DNS server had no idea what IP my machine is using, but my workstation is under the assumption it has the rights to that IP address still.

Jim happened to be one of the few online, so I chatted with him a bit to see if he had any ideas how to resolve the problem.  He mentioned that this had happened to him a couple of times and his solution ended up being to drive back into work, I figured that would be my backup plan.

Jim then suggested using arp on a machine I had recently ssh‘d into from my workstation.  While I hadn’t actually used ssh to connect to any of our lab machines, it  got me thinking about our CVS server – as I was running Eclipse on my workstation and did have in theory an active connection.  From my laptop I was able to ssh into the CVS server, then using netstat I got a full list of the active connections.  Looking through the list turned up a few which were numeric (clearly where DNS had failed to provide a reverse name mapping).  From there it was a simple matter to remote desktop to the “right” numeric address to reach my workstation – I got lucky on my 1st try.