1 /*
2  * Copyright (c) 2007, 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 package nsk.share.runner;
25 
26 import nsk.share.log.Log;
27 import nsk.share.test.StressOptions;
28 import java.io.PrintStream;
29 
30 public class RunParams {
31         private StressOptions stressOptions;
32         private long sleepTime = 500;
33         private long iterations = 0;
34         private int numberOfThreads;
35         private long seed = System.currentTimeMillis();
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 
RunParams()45         public RunParams() {
46                 this(new StressOptions());
47         }
48 
RunParams(StressOptions stressOptions)49         public RunParams(StressOptions stressOptions) {
50                 this.stressOptions = stressOptions;
51                 numberOfThreads = getMediumLoadThreadsCount();
52         }
53 
RunParams(String[] args)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          */
getTestMemory()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          */
getThreadsMemory()75         public long getThreadsMemory() {
76                 return 3 * getTestMemory() / 4;
77         }
78 
getSleepTime()79         public final long getSleepTime() {
80                 return sleepTime;
81         }
82 
setSleepTime(long sleepTime)83         public final void setSleepTime(long sleepTime) {
84                 this.sleepTime = sleepTime;
85         }
86 
getIterations()87         public final long getIterations() {
88                 return iterations;
89         }
90 
setIterations(long iterations)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 
getBasicLoadThreadsCount()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 
getMediumLoadThreadsCount()112         public int getMediumLoadThreadsCount() {
113                 return 2 * getBasicLoadThreadsCount();
114         }
115 
getHighLoadThreadsCount()116         public int getHighLoadThreadsCount() {
117                 return 100 * getBasicLoadThreadsCount();
118         }
119 
getNumberOfThreads()120         public final int getNumberOfThreads() {
121                 return numberOfThreads * stressOptions.getThreadsFactor();
122         }
123 
setNumberOfThreads(int numberOfThreads)124         public final void setNumberOfThreads(int numberOfThreads) {
125                 this.numberOfThreads = numberOfThreads;
126         }
127 
getSeed()128         public final long getSeed() {
129                 return seed;
130         }
131 
setSeed(long seed)132         public final void setSeed(long seed) {
133                 this.seed = seed;
134         }
135 
isRunGCThread()136         public final boolean isRunGCThread() {
137                 return runGCThread;
138         }
139 
setRunGCThread(boolean runGCThread)140         public final void setRunGCThread(boolean runGCThread) {
141                 this.runGCThread = runGCThread;
142         }
143 
isRunFinThread()144         public final boolean isRunFinThread() {
145                 return runFinThread;
146         }
147 
setRunFinThread(boolean runFinThread)148         public final void setRunFinThread(boolean runFinThread) {
149                 this.runFinThread = runFinThread;
150         }
151 
isRunMemDiagThread()152         public final boolean isRunMemDiagThread() {
153                 return runMemDiagThread;
154         }
155 
setRunMemDiagThread(boolean runMemDiagThread)156         public final void setRunMemDiagThread(boolean runMemDiagThread) {
157                 this.runMemDiagThread = runMemDiagThread;
158         }
159 
isRunFinDiagThread()160         public final boolean isRunFinDiagThread() {
161                 return runFinDiagThread;
162         }
163 
setRunFinDiagThread(boolean runFinDiagThread)164         public final void setRunFinDiagThread(boolean runFinDiagThread) {
165                 this.runFinDiagThread = runFinDiagThread;
166         }
167 
isRunAllDiagThread()168         public final boolean isRunAllDiagThread() {
169                 return runAllDiagThread;
170         }
171 
setRunAllDiagThread(boolean runAllDiagThread)172         public final void setRunAllDiagThread(boolean runAllDiagThread) {
173                 this.runAllDiagThread = runAllDiagThread;
174         }
175 
isRunForever()176         public final boolean isRunForever() {
177                 return runForever;
178         }
179 
setRunForever(boolean runForever)180         public final void setRunForever(boolean runForever) {
181                 this.runForever = runForever;
182         }
183 
isInterruptThreads()184         public final boolean isInterruptThreads() {
185                 return interruptThreads;
186         }
187 
setInterruptThreads(boolean interruptThreads)188         public final void setInterruptThreads(boolean interruptThreads) {
189                 this.interruptThreads = interruptThreads;
190         }
191 
getStressOptions()192         public final StressOptions getStressOptions() {
193                 return stressOptions;
194         }
195 
parseCommandLine(String[] args)196         public void parseCommandLine(String[] args) {
197                 if (args == null)
198                         return;
199                 stressOptions.parseCommandLine(args);
200                 for (int i = 0; i < args.length; ++i) {
201                         if (args[i].equals("-f"))
202                                 runForever = true;
203                         else if (args[i].equals("-tg"))
204                                 runGCThread = true;
205                         else if (args[i].equals("-tf"))
206                                 runFinThread = true;
207                         else if (args[i].equals("-Dm"))
208                                 runMemDiagThread = true;
209                         else if (args[i].equals("-Dm-"))
210                                 runMemDiagThread = false;
211                         else if (args[i].equals("-Df1"))
212                                 runFinDiagThread = true;
213                         else if (args[i].equals("-Df"))
214                                 runFinDiagThread = true;
215                         else if (args[i].equals("-s"))
216                                 seed = Long.parseLong(args[++i]);
217                         else if (args[i].equals("-t"))
218                                 numberOfThreads = Integer.parseInt(args[++i]);
219                         else if (args[i].equals("-it"))
220                                 interruptThreads = true;
221                         else if (args[i].equals("-iterations"))
222                                 iterations = Integer.parseInt(args[++i]);
223                 }
224                 printConfig(System.out);
225         }
226 
prinUsage()227         public void prinUsage() {
228         }
229 
printConfig(PrintStream out)230         public void printConfig(PrintStream out) {
231                 stressOptions.printInfo(out);
232                 out.println("Max memory: " + Runtime.getRuntime().maxMemory());
233                 out.println("Sleep time: " + sleepTime);
234                 out.println("Iterations: " + iterations);
235                 out.println("Number of threads: " + numberOfThreads);
236                 out.println("Seed: " + seed);
237                 out.println("Run GC thread: " + runGCThread);
238                 out.println("Run mem diag thread: " + runMemDiagThread);
239                 out.println("Run forever: " + runForever);
240         }
241 
logConfig(Log log)242         public void logConfig(Log log) {
243                 log.debug("Max memory: " + Runtime.getRuntime().maxMemory());
244                 log.debug("Sleep time: " + sleepTime);
245                 log.debug("Iterations: " + iterations);
246                 log.debug("Number of threads: " + numberOfThreads);
247                 log.debug("Seed: " + seed);
248                 log.debug("Run GC thread: " + runGCThread);
249                 log.debug("Run mem diag thread: " + runMemDiagThread);
250                 log.debug("Run forever: " + runForever);
251         }
252 
253         private static RunParams instance;
254 
getInstance()255         public static RunParams getInstance() {
256                 synchronized (RunParams.class) {
257                         if (instance == null)
258                                 instance = new RunParams();
259                         return instance;
260                 }
261         }
262 
setInstance(RunParams runParams)263         public static void setInstance(RunParams runParams) {
264                 synchronized (RunParams.class) {
265                         instance = runParams;
266                 }
267         }
268 }
269