1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8195100
  27  * @summary a short lived Symbol should be cleaned up
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          java.management
  31  * @requires (vm.debug == true)
  32  */
  33 
  34 import jdk.test.lib.Platform;
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import jdk.test.lib.process.ProcessTools;
  37 import java.util.Scanner;
  38 
  39 public class ShortLivedSymbolCleanup {
  40           
  41   static int getSymbolTableSize(ProcessBuilder pb) throws Exception {
  42     int size = 0;
  43     
  44     OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
  45     String output = analyzer.getStdout();
  46     analyzer.shouldHaveExitValue(0);
  47 
  48     // Split string into lines using platform independent end of line marker.
  49     String[] lines = output.split("\\R");
  50     for (String line : lines) {
  51       if (line.contains("Start size")) {
  52         // ex. "[0.023s][trace][symboltable] Start size: 32768 (15)"
  53         Scanner scanner = new Scanner(line);
  54         scanner.next(); // skip "[0.023s][trace][symboltable]"
  55         scanner.next(); // skip "Start"
  56         scanner.next(); // skip "size:"
  57         size = Integer.parseInt(scanner.next()); // process "32768"
  58         scanner.close();
  59       }
  60     }
  61     
  62     return size;
  63   }
  64   
  65   static void analyzeOutputOn(int size, ProcessBuilder pb) throws Exception {
  66     OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
  67     String output = analyzer.getStdout();
  68     analyzer.shouldHaveExitValue(0);
  69 
  70     // Split string into lines using platform independent end of line marker.
  71     String[] lines = output.split("\\R");
  72     for (String line : lines) {
  73       if (line.startsWith("  Total removed")) {
  74         // ex. "Total removed              13309"
  75         Scanner scanner = new Scanner(line);
  76         scanner.next(); // skip "Total"
  77         scanner.next(); // skip "removed"
  78         int removed = Integer.parseInt(scanner.next()); // process "13309"
  79         scanner.close();
  80 
  81         if (removed < (size/2)) {
  82           System.out.println(output);
  83           // We should have removed at least half of the temporary Symbols
  84           throw new RuntimeException("Did not clean dead temporary Symbols [removed:"+removed+", size:"+size+"]");
  85         }
  86       }
  87     }
  88   }
  89         
  90   public static void main(String[] args) throws Exception {      
  91       
  92       if (Platform.isDebugBuild()) {
  93         {
  94           ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:symboltable=trace", 
  95                                                                     "-version");
  96           int size = getSymbolTableSize(pb);
  97           
  98           pb = ProcessTools.createJavaProcessBuilder("-XX:+PrintSymbolTableSizeHistogram",
  99                                                                     LotsOfTempSymbols.class.getName(),
 100                                                                     Integer.toString(size));
 101           analyzeOutputOn(size, pb);
 102         }
 103     }
 104   }
 105         
 106   static class LotsOfTempSymbols {
 107     public static void main(String [] args) {
 108       int size = 2*Integer.parseInt(args[0]);
 109       // Create enough temporary Symbols, that we are
 110       // guranteed to   insert into every bucket twice,
 111       // and therefore have the table check for dead entries
 112       for (int i=0; i<size; i++) {
 113         try {
 114           Class.forName(String.format("%05d", i), false, null);
 115         } catch (java.lang.ClassNotFoundException e) {}
 116       }
 117     }
 118   }
 119 }
 120