200 WCS V5.1 Performance Tuning
GCStats.java
// GCStats.java
// This utility tabulates data generated from a verbose garbage collection
trace.
// To run this utility type:
// java GCStats inputfile [total_time]
//
// Gennaro (Jerry) Cuomo - IBM Corp. 03/2000
// Carmine F. Greco 3/17/00 - JDK1.2.2 compatibility
//
import java.io.*;
import java.util.*;
public class GCStats {
static int total_time=-1; // total time of run in ms
static long total_gctime=0, total_gctime1=0; // total time spent in GCs
static long total_bytes=0, total_bytes1=0; // total bytes collected
static long total_free=0, total_free1=0; // total
static int total_gc=0; // total number of GCs
static boolean verbose=false; // debug trace on/off
public static void parseLine(String line) {
// parsing a string that looks like this...
// <GC(31): freed 16407744 bytes in 107 ms, 97% free
(16417112/16777208)>
if (isGCStatsLine(line)) { // First test if line starts with "<GC..."
if (verbose) System.out.println("GOT a GC - "+line);
long temp=numberBefore(line, " bytes")/1024; // get total memory
collected
total_bytes+=temp; total_bytes1+=(temp*temp);
temp=numberBefore(line, " ms"); // get time in GC
total_gctime+=temp; total_gctime1+=(temp*temp);
temp=numberBefore(line, "% free"); // get time % free
total_free+=temp; total_free1+=(temp*temp);
if (temp!=0) {
total_gc++; // total number of GCs
}
}
}
public static int numberBefore( String line, String s) {
int ret = 0;
int idx = line.indexOf(s);
Appendix D. GCStats.java 201
int idx1= idx-1;
if (idx>0) {
// the string was found, now walk backwards until we find the blank
while (idx1!=0 && line.charAt(idx1)!=' ') idx1--;
if (idx1>0) {
String temp=line.substring(idx1+1,idx);
if (temp!=null) {
ret=Integer.parseInt(temp); // convert from string to number
}
} else {
if (verbose) System.out.println("ERROR: numberBefore() - Parse
Error looking for "+s);
}
}
return ret;
}
public static boolean isGCStatsLine(String line) {
return ( (line.indexOf("<GC") > -1) && (line.indexOf(" freed")>0) &&
(line.indexOf(" bytes")>0));
}
public static void main (String args[]) {
String filename=null;
BufferedReader foS=null;
boolean keepgoing=true;
if (args.length==0) {
System.out.println("GCStats - ");
System.out.println(" - ");
System.out.println(" - Syntax: GCStats filename
[run_duration(ms)]");
System.out.println(" - filename = file containing -verbosegc
data");
System.out.println(" - run_duration(ms) = duration of fixed work
run in which GCs took place");
return;
}
if (args.length>0) {
filename=args[0];
}
if (args.length>1) {
total_time=Integer.parseInt(args[1]);
}
if (verbose) System.out.println("Filename="+filename);
try {
foS = new BufferedReader(new FileReader(filename));
202 WCS V5.1 Performance Tuning
} catch (Throwable e) {
System.out.println("Error opening file="+filename);
return;
}
while (keepgoing) {
String nextLine;
try {
nextLine=foS.readLine();
} catch (Throwable e) {
System.out.println("Cannot read file="+filename);
return;
}
if (nextLine!=null) {
parseLine(nextLine);
} else {
keepgoing=false;
}
}
try {
foS.close();
} catch (Throwable e) {
System.out.println("Cannot close file="+filename);
return;
}
System.out.println("-------------------------------------------------");
System.out.println("- GC Statistics for file - "+filename);
System.out.println("-------------------------------------------------");
System.out.println("-**** Totals ***");
System.out.println("- "+total_gc+" Total number of GCs");
System.out.println("- "+total_gctime+" ms. Total time in GCs");
System.out.println("- "+total_bytes+" Kbytes. Total memory collected
during GCs");
System.out.println("- ");
System.out.println("-**** Averages ***");
double mean=total_gctime/total_gc,
stddev=Math.sqrt((total_gctime1-2*mean*total_gctime+total_gc*mean*mean)/total_g
c);
int imean=new Double(mean).intValue(), istddev=new
Double(stddev).intValue();
System.out.println("- "+imean+" ms. Average time per GC.
(stddev="+istddev+" ms.)");
mean=total_bytes/total_gc;
stddev=Math.sqrt((total_bytes1-2*mean*total_bytes+total_gc*mean*mean)/total_gc)
;
imean=new Double(mean).intValue(); istddev=new Double(stddev).intValue();
Appendix D. GCStats.java 203
System.out.println("- "+imean+" Kbytes. Average memory collected per GC.
(stddev="+istddev+" Kbytes)");
mean=total_free/total_gc;
stddev=Math.sqrt((total_free1-2*mean*total_free+total_gc*mean*mean)/total_gc);
imean=new Double(mean).intValue(); istddev=new Double(stddev).intValue();
System.out.println("- "+imean+"%. Free memory after each GC.
(stddev="+istddev+"%)");
if (total_time>0 && total_gctime>0) {
System.out.println("- "+((total_gctime*1.0)/(total_time*1.0))*100.0+"%
of total time ("+total_time+"ms.) spent in GC.");
}
System.out.println("___________________________ "+new Date());
System.out.println("");
}
204 WCS V5.1 Performance Tuning

Get WCS V5.1 Performance Tuning now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.