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 final int DEFAULT_SYMBOL_TABLE_SIZE_POWER_2 = 14;
  42   static final int SIZE = (int)Math.pow(2, DEFAULT_SYMBOL_TABLE_SIZE_POWER_2+1);
  43 
  44   static void analyzeOutputOn(ProcessBuilder pb) throws Exception {
  45     OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
  46     String output = analyzer.getStdout();
  47     analyzer.shouldHaveExitValue(0);
  48 
  49     // Split string into lines using platform independent end of line marker.
  50     String[] lines = output.split("\\R");
  51     for (String line : lines) {
  52       if (line.startsWith("Total removed")) {
  53         // ex. "Total removed              13309"
  54         Scanner scanner = new Scanner(line);
  55         scanner.next(); // skip "Total"
  56         scanner.next(); // skip "removed"
  57         int removed = Integer.parseInt(scanner.next()); // process "13309"
  58         scanner.close();
  59 
  60         if (removed < (SIZE/2)) {
  61           System.out.println(output);
  62           // We should have removed at least half of the temporary Symbols
  63           throw new RuntimeException("Did not clean dead temporary Symbols");
  64         }
  65       }
  66     }
  67   }
  68         
  69   public static void main(String[] args) throws Exception {
  70       if (Platform.isDebugBuild()) {
  71       ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+PrintSymbolTableSizeHistogram", 
  72                                                                 LotsOfTempSymbols.class.getName());
  73       analyzeOutputOn(pb);
  74     }
  75   }
  76         
  77   static class LotsOfTempSymbols {
  78     public static void main(String [] args) {
  79       // Create enough temporary Symbols, that we are
  80       // guranteed to   insert into every bucket twice,
  81       // and therefore have the table check for dead entries
  82       for (int i=0; i<SIZE; i++) {
  83         try {
  84           Class.forName(String.format("%05d", i), false, null);
  85         } catch (java.lang.ClassNotFoundException e) {}
  86       }
  87     }
  88   }
  89 }
  90