1 /*
  2  * Copyright (c) 2007, 2020, 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 nsk.share.runner;
 25 
 26 import nsk.share.log.Log;
 27 import nsk.share.test.StressOptions;
 28 import java.io.PrintStream;
 29 import jdk.test.lib.Utils;
 30 
 31 public class RunParams {
 32         private StressOptions stressOptions;
 33         private long sleepTime = 500;
 34         private long iterations = 0;
 35         private int numberOfThreads;
 36         private boolean runGCThread = false;
 37         private boolean runFinThread = false;
 38         private boolean runMemDiagThread = false;
 39         private boolean runFinDiagThread = false;
 40         private boolean runAllDiagThread = false;
 41         private boolean runForever = false;
 42         private long threadBlockSize = 64 * 1024 * 1024;
 43         private boolean interruptThreads = false;
 44 
 45         public RunParams() {
 46                 this(new StressOptions());
 47         }
 48 
 49         public RunParams(StressOptions stressOptions) {
 50                 this.stressOptions = stressOptions;
 51                 numberOfThreads = getMediumLoadThreadsCount();
 52         }
 53 
 54         public RunParams(String[] args) {
 55                 this();
 56                 parseCommandLine(args);
 57         }
 58 
 59         /**
 60          *  Get an approximate memory which test should fill.
 61          *
 62          *  This can be used to adjust the parameters of allocated objects
 63          *  to test run environment. Currently it is 3/5 of
 64          *  Runtime.getRuntime().maxMemory().
 65          */
 66         public long getTestMemory() {
 67                 return 3 * Runtime.getRuntime().maxMemory() / 5;
 68         }
 69 
 70         /**
 71          * Return memory to use for allocation of threads.
 72          *
 73          * This is currently 3/4 of getTestMemory();
 74          */
 75         public long getThreadsMemory() {
 76                 return 3 * getTestMemory() / 4;
 77         }
 78 
 79         public final long getSleepTime() {
 80                 return sleepTime;
 81         }
 82 
 83         public final void setSleepTime(long sleepTime) {
 84                 this.sleepTime = sleepTime;
 85         }
 86 
 87         public final long getIterations() {
 88                 return iterations;
 89         }
 90 
 91         public final void setIterations(long iterations) {
 92                 if (this.iterations != iterations) {
 93                         this.iterations = iterations;
 94                         System.out.println("Iterations: " + iterations);
 95                 }
 96         }
 97 
 98         public int getBasicLoadThreadsCount() {
 99                 int cnt = (int) Math.min(
100                         Integer.MAX_VALUE,
101                         Math.min(
102                                 Runtime.getRuntime().availableProcessors(),
103                                 Math.round((double) Runtime.getRuntime().maxMemory() / threadBlockSize)
104                         )
105                 );
106 
107                 // cnt could be equal to 0 in case maxMemory is less than threadBlockSize then
108                 // so, need to check this
109                 return (cnt > 0 ? cnt : 1);
110         }
111 
112         public int getMediumLoadThreadsCount() {
113                 return 2 * getBasicLoadThreadsCount();
114         }
115 
116         public int getHighLoadThreadsCount() {
117                 return 100 * getBasicLoadThreadsCount();
118         }
119 
120         public final int getNumberOfThreads() {
121                 return numberOfThreads * stressOptions.getThreadsFactor();
122         }
123 
124         public final void setNumberOfThreads(int numberOfThreads) {
125                 this.numberOfThreads = numberOfThreads;
126         }
127 
128         public final long getSeed() {
129                 // ensure that seed got printed out
130                 Utils.getRandomInstance();
131                 return Utils.SEED;
132         }
133 
134         public final boolean isRunGCThread() {
135                 return runGCThread;
136         }
137 
138         public final void setRunGCThread(boolean runGCThread) {
139                 this.runGCThread = runGCThread;
140         }
141 
142         public final boolean isRunFinThread() {
143                 return runFinThread;
144         }
145 
146         public final void setRunFinThread(boolean runFinThread) {
147                 this.runFinThread = runFinThread;
148         }
149 
150         public final boolean isRunMemDiagThread() {
151                 return runMemDiagThread;
152         }
153 
154         public final void setRunMemDiagThread(boolean runMemDiagThread) {
155                 this.runMemDiagThread = runMemDiagThread;
156         }
157 
158         public final boolean isRunFinDiagThread() {
159                 return runFinDiagThread;
160         }
161 
162         public final void setRunFinDiagThread(boolean runFinDiagThread) {
163                 this.runFinDiagThread = runFinDiagThread;
164         }
165 
166         public final boolean isRunAllDiagThread() {
167                 return runAllDiagThread;
168         }
169 
170         public final void setRunAllDiagThread(boolean runAllDiagThread) {
171                 this.runAllDiagThread = runAllDiagThread;
172         }
173 
174         public final boolean isRunForever() {
175                 return runForever;
176         }
177 
178         public final void setRunForever(boolean runForever) {
179                 this.runForever = runForever;
180         }
181 
182         public final boolean isInterruptThreads() {
183                 return interruptThreads;
184         }
185 
186         public final void setInterruptThreads(boolean interruptThreads) {
187                 this.interruptThreads = interruptThreads;
188         }
189 
190         public final StressOptions getStressOptions() {
191                 return stressOptions;
192         }
193 
194         public void parseCommandLine(String[] args) {
195                 if (args == null)
196                         return;
197                 stressOptions.parseCommandLine(args);
198                 for (int i = 0; i < args.length; ++i) {
199                         if (args[i].equals("-f"))
200                                 runForever = true;
201                         else if (args[i].equals("-tg"))
202                                 runGCThread = true;
203                         else if (args[i].equals("-tf"))
204                                 runFinThread = true;
205                         else if (args[i].equals("-Dm"))
206                                 runMemDiagThread = true;
207                         else if (args[i].equals("-Dm-"))
208                                 runMemDiagThread = false;
209                         else if (args[i].equals("-Df1"))
210                                 runFinDiagThread = true;
211                         else if (args[i].equals("-Df"))
212                                 runFinDiagThread = true;
213                         else if (args[i].equals("-t"))
214                                 numberOfThreads = Integer.parseInt(args[++i]);
215                         else if (args[i].equals("-it"))
216                                 interruptThreads = true;
217                         else if (args[i].equals("-iterations"))
218                                 iterations = Integer.parseInt(args[++i]);
219                 }
220                 printConfig(System.out);
221         }
222 
223         public void prinUsage() {
224         }
225 
226         public void printConfig(PrintStream out) {
227                 stressOptions.printInfo(out);
228                 out.println("Max memory: " + Runtime.getRuntime().maxMemory());
229                 out.println("Sleep time: " + sleepTime);
230                 out.println("Iterations: " + iterations);
231                 out.println("Number of threads: " + numberOfThreads);
232                 out.println("Run GC thread: " + runGCThread);
233                 out.println("Run mem diag thread: " + runMemDiagThread);
234                 out.println("Run forever: " + runForever);
235         }
236 
237         public void logConfig(Log log) {
238                 log.debug("Max memory: " + Runtime.getRuntime().maxMemory());
239                 log.debug("Sleep time: " + sleepTime);
240                 log.debug("Iterations: " + iterations);
241                 log.debug("Number of threads: " + numberOfThreads);
242                 log.debug("Run GC thread: " + runGCThread);
243                 log.debug("Run mem diag thread: " + runMemDiagThread);
244                 log.debug("Run forever: " + runForever);
245         }
246 
247         private static RunParams instance;
248 
249         public static RunParams getInstance() {
250                 synchronized (RunParams.class) {
251                         if (instance == null)
252                                 instance = new RunParams();
253                         return instance;
254                 }
255         }
256 
257         public static void setInstance(RunParams runParams) {
258                 synchronized (RunParams.class) {
259                         instance = runParams;
260                 }
261         }
262 }