1 /*
   2  * Copyright (c) 2015, 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 package compiler.compilercontrol.jcmd;
  25 
  26 import compiler.compilercontrol.parser.HugeDirectiveUtil;
  27 import compiler.compilercontrol.share.AbstractTestBase;
  28 import compiler.compilercontrol.share.method.MethodDescriptor;
  29 import compiler.compilercontrol.share.scenario.Executor;
  30 import jdk.test.lib.OutputAnalyzer;
  31 import jdk.test.lib.TimeLimitedRunner;
  32 import jdk.test.lib.Utils;
  33 import pool.PoolHelper;
  34 
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Random;
  38 import java.util.stream.Collectors;
  39 
  40 public abstract class StressAddJcmdBase {
  41     private static final int DIRECTIVES_AMOUNT = Integer.getInteger(
  42             "compiler.compilercontrol.jcmd.StressAddJcmdBase.directivesAmount",
  43             999);
  44     private static final List<MethodDescriptor> DESCRIPTORS = new PoolHelper()
  45             .getAllMethods().stream()
  46                     .map(pair -> AbstractTestBase
  47                             .getValidMethodDescriptor(pair.first))
  48                     .collect(Collectors.toList());
  49     private static final String DIRECTIVE_FILE = "directives.json";
  50     private static final List<String> VM_OPTIONS = new ArrayList<>();
  51     private static final Random RANDOM = Utils.getRandomInstance();
  52 
  53     static {
  54         VM_OPTIONS.add("-XX:+UnlockDiagnosticVMOptions");
  55         VM_OPTIONS.add("-XX:+LogCompilation");
  56         VM_OPTIONS.add("-XX:CompilerDirectivesLimit=1000");
  57     }
  58 
  59     /**
  60      * Performs test
  61      */
  62     public void test() {
  63         HugeDirectiveUtil.createHugeFile(DESCRIPTORS, DIRECTIVE_FILE,
  64                 DIRECTIVES_AMOUNT);
  65         Executor executor = new TimeLimitedExecutor();
  66         List<OutputAnalyzer> outputAnalyzers = executor.execute();
  67         outputAnalyzers.get(0).shouldHaveExitValue(0);
  68     }
  69 
  70     /**
  71      * Makes connection to the test VM and performs a diagnostic command
  72      *
  73      * @param pid a pid of the VM under test
  74      * @return true if the test should continue invocation of this method
  75      */
  76     protected abstract boolean makeConnection(int pid);
  77 
  78     /**
  79      * Finish test executions
  80      */
  81     protected void finish() { }
  82 
  83     protected String nextCommand() {
  84         int i = RANDOM.nextInt(JcmdCommand.values().length);
  85         JcmdCommand jcmdCommand = JcmdCommand.values()[i];
  86         switch (jcmdCommand) {
  87             case ADD:
  88                 return jcmdCommand.command + " " + DIRECTIVE_FILE;
  89             case PRINT:
  90             case CLEAR:
  91             case REMOVE:
  92                 return jcmdCommand.command;
  93             default:
  94                 throw new Error("TESTBUG: incorrect command: " + jcmdCommand);
  95         }
  96     }
  97 
  98     private enum JcmdCommand {
  99         ADD("Compiler.directives_add"),
 100         PRINT("Compiler.directives_print"),
 101         CLEAR("Compiler.directives_clear"),
 102         REMOVE("Compiler.directives_remove");
 103 
 104         public final String command;
 105 
 106         JcmdCommand(String command) {
 107             this.command = command;
 108         }
 109     }
 110 
 111     private class TimeLimitedExecutor extends Executor {
 112         public TimeLimitedExecutor() {
 113             /* There are no need to check the state */
 114             super(true, VM_OPTIONS, null, null);
 115         }
 116 
 117         @Override
 118         protected OutputAnalyzer[] executeJCMD(int pid) {
 119             TimeLimitedRunner runner = new TimeLimitedRunner(
 120                     Utils.DEFAULT_TEST_TIMEOUT,
 121                     Utils.TIMEOUT_FACTOR,
 122                     () -> makeConnection(pid));
 123             try {
 124                 runner.call();
 125             } catch (Exception e) {
 126                 throw new Error("Exception during the execution: " + e, e);
 127             }
 128             finish();
 129             return new OutputAnalyzer[0];
 130         }
 131     }
 132 }