Arrow
Software Bookmark Icon

Software >> libprofiler

 

This is a simple library that will profile thread contention points in a multi-threaded Java program. Unlike most general-purpose Java profilers, this is very light weight and imposes very little overhead on the application to be profiled. It uses the JVMPI, however, which seems to be pretty unstable (on Linux, at least, at this time (2001)).

In fact, as of right now (2001), JDK 1.3.1b24 seems to be the only build of the VM where the JVMPI is implemented correctly on Linux. You can download it here, since that particular build of the JDK is probably hard to find. [Updated to add: This should work fine now on modern JDKs (2009)]

For a Java program like:
public class Contention implements Runnable {
  public Contention() {}

  // Runnable Implementation
  public void run() {
    for (;;) {
      for (int i=0;i<5;i++) doWork();
      for (int i=0;i<5;i++) doMoreWork();
      for (int i=0;i<5;i++) doEvenMoreWork();
    }
  }

  private synchronized void doWork() {
    sleep(500);
  }

  private synchronized void doMoreWork() {
    sleep(500);
  }

  private synchronized void doEvenMoreWork() {
    sleep(5000);
  }

  private void sleep(int millis) {
    try {
      Thread.sleep(millis);
    } catch (InterruptedException ie) {}
  }

  private static void printUsage() {
    System.err.println("Usage: Contention <ThreadCount>");
  }
  
  public static void main(String argv[]) {
    if (argv.length != 1) {
      printUsage();
      return;
    }

    Contention c = new Contention();

    int threadCount = Integer.parseInt(argv[0]);
    for (int i=0;i<threadCount;i++) {
      Thread thread = new Thread(c, "CounterThread-" + i);
      thread.setDaemon(true);
      thread.start();
    }

    try {
      Thread.sleep(60000);
    } catch (InterruptedException ie) {}
  }
}

Running the profiler would look like:

mimir:~/dev$ java -Xruncontentionprofiler Contention 20
Initializing Profiler...
Profiler Running...

Contention Points:
98 threads spent 868316 millis in Contention on line 26.  Average: 8860, Min: 0, Max: 9820.
19 threads spent 184111 millis in Contention on line 30.  Average: 9690, Min: 0, Max: 9691.