[PATCH] Display human readable progress instead of many dots in jhat

Xiaoguang Sun sunxiaoguang at gmail.com
Tue Dec 10 03:24:52 PST 2013


Display human readable progress instead of many dots in jhat.

Looks like the attachment is removed as it's thought to be suspicious. I'm
attaching it again as a plain text file again. Sorry for the duplicated
emails.

Xiaoguang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/serviceability-dev/attachments/20131210/9ce08a4b/attachment-0001.html 
-------------- next part --------------
# HG changeset patch
# User Xiaoguang Sun <sunxiaoguang at gmail.com>
# Date 1386420176 -28800
#      Sat Dec 07 20:42:56 2013 +0800
# Node ID 1e178a667881f4be98f86eff7c6688c00782be23
# Parent  861e489158effbf6a841119206eea2689fcb2a83
Display human readable progress instead of many dots in jhat

diff -r 861e489158ef -r 1e178a667881 src/share/classes/com/sun/tools/hat/internal/model/Snapshot.java
--- a/src/share/classes/com/sun/tools/hat/internal/model/Snapshot.java	Thu Sep 12 17:17:40 2013 -0700
+++ b/src/share/classes/com/sun/tools/hat/internal/model/Snapshot.java	Sat Dec 07 20:42:56 2013 +0800
@@ -49,6 +49,45 @@
  */
 
 public class Snapshot {
+    private static class ProgressPrinter {
+        private StringBuilder progress = new StringBuilder();
+        private int finished;
+        private int total;
+        private int unit;
+        public ProgressPrinter(int total) {
+          this.total = total;
+          unit = total / 100;
+        }
+
+        private void printProgress()
+        {
+            for (int counter = 0, max = progress.length();
+                counter < max; ++counter) {
+                System.out.print('\b');
+            }
+            progress.setLength(0);
+            progress.append("(");
+            progress.append(finished);
+            progress.append("/");
+            progress.append(total);
+            progress.append(") ");
+            progress.append(100 * finished / total);
+            progress.append(" %");
+            System.out.print(progress);
+            System.out.flush();
+        }
+
+        public void step() {
+            if ((++finished % unit) == 0) {
+              printProgress();
+            }
+        }
+
+        public void finish() {
+            printProgress();
+            System.out.println();
+        }
+    }
 
     public static long SMALL_ID_MASK = 0x0FFFFFFFFL;
     public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
@@ -224,9 +263,6 @@
         }
     }
 
-    // To show heap parsing progress, we print a '.' after this limit
-    private static final int DOT_LIMIT = 5000;
-
     /**
      * Called after reading complete, to initialize the structure
      */
@@ -290,21 +326,19 @@
 
         if (calculateRefs) {
             calculateReferencesToObjects();
-            System.out.print("Eliminating duplicate references");
+            System.out.print("Eliminating duplicate references ");
             System.out.flush();
             // This println refers to the *next* step
         }
-        int count = 0;
+        ProgressPrinter progress = calculateRefs ? new ProgressPrinter(heapObjects.size()) : null;
         for (JavaHeapObject t : heapObjects.values()) {
             t.setupReferers();
-            ++count;
-            if (calculateRefs && count % DOT_LIMIT == 0) {
-                System.out.print(".");
-                System.out.flush();
+            if (calculateRefs) {
+                progress.step();
             }
         }
         if (calculateRefs) {
-            System.out.println("");
+            progress.finish();
         }
 
         // to ensure that Iterator.remove() on getClasses()
@@ -313,22 +347,17 @@
     }
 
     private void calculateReferencesToObjects() {
-        System.out.print("Chasing references, expect "
-                         + (heapObjects.size() / DOT_LIMIT) + " dots");
+        System.out.print("Chasing references ");
         System.out.flush();
-        int count = 0;
+        ProgressPrinter progress = new ProgressPrinter(heapObjects.size());
         MyVisitor visitor = new MyVisitor();
         for (JavaHeapObject t : heapObjects.values()) {
             visitor.t = t;
             // call addReferenceFrom(t) on all objects t references:
             t.visitReferencedObjects(visitor);
-            ++count;
-            if (count % DOT_LIMIT == 0) {
-                System.out.print(".");
-                System.out.flush();
-            }
+            progress.step();
         }
-        System.out.println();
+        progress.finish();
         for (Root r : roots) {
             r.resolve(this);
             JavaHeapObject t = findThing(r.getId());


More information about the serviceability-dev mailing list