MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Driver.java
1 /* -*- mode: java; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=4:tabstop=4:smarttab:
3  *
4  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 package com.mysql.cluster.benchmark.tws;
21 
22 import java.io.PrintWriter;
23 import java.io.FileInputStream;
24 import java.io.InputStream;
25 import java.io.IOException;
26 import java.io.FileWriter;
27 
28 import java.util.Properties;
29 import java.util.List;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.text.SimpleDateFormat;
33 
34 
35 public abstract class Driver {
36 
37  // console
38  static protected final PrintWriter out = new PrintWriter(System.out, true);
39  static protected final PrintWriter err = new PrintWriter(System.err, true);
40 
41  // shortcuts
42  static protected final String endl = System.getProperty("line.separator");
43  static protected final Runtime rt = Runtime.getRuntime();
44 
45  // driver command-line arguments
46  static private final List<String> propFileNames = new ArrayList<String>();
47  static private String logFileName;
48 
49  // driver settings
50  protected final Properties props = new Properties();
51  protected boolean logRealTime;
52  protected boolean logMemUsage;
53  protected boolean includeFullGC;
54  protected int warmupRuns;
55 
56  // driver resources
57  protected PrintWriter log;
58  protected boolean logHeader;
59  protected StringBuilder header;
60  protected StringBuilder rtimes;
61  protected StringBuilder musage;
62  protected long t0 = 0, t1 = 0, ta = 0;
63  protected long m0 = 0, m1 = 0, ma = 0;
64 
65  // ----------------------------------------------------------------------
66  // driver usage
67  // ----------------------------------------------------------------------
68 
72  static protected void exitUsage() {
73  out.println("usage: [options]");
74  out.println(" [-p <file name>]... a properties file name");
75  out.println(" [-l <file name>] log file name for data output");
76  out.println(" [-h|--help] print usage message and exit");
77  out.println();
78  System.exit(1); // return an error code
79  }
80 
84  static public void parseArguments(String[] args) {
85  for (int i = 0; i < args.length; i++) {
86  final String arg = args[i];
87  if (arg.equals("-p")) {
88  if (i >= args.length) {
89  exitUsage();
90  }
91  propFileNames.add(args[++i]);
92  } else if (arg.equals("-l")) {
93  if (i >= args.length) {
94  exitUsage();
95  }
96  logFileName = args[++i];
97  } else if (arg.equals("-h") || arg.equals("--help")) {
98  exitUsage();
99  } else {
100  out.println("unknown option: " + arg);
101  exitUsage();
102  }
103  }
104 
105  if (propFileNames.size() == 0) {
106  propFileNames.add("run.properties");
107  }
108 
109  if (logFileName == null) {
110  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHMMss");
111  logFileName = ("log_" + sdf.format(new Date()) + ".txt");
112  }
113  }
114 
118  public Driver() {}
119 
123  public void run() {
124  try {
125  init();
126 
127  if (warmupRuns > 0) {
128  out.println();
129  out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
130  out.println("warmup runs ...");
131  out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
132 
133  for (int i = 0; i < warmupRuns; i++) {
134  runTests();
135  }
136 
137  // truncate log file, reset log buffers
138  closeLogFile();
139  openLogFile();
140  clearLogBuffers();
141  }
142 
143  out.println();
144  out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
145  out.println("hot runs ...");
146  out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
147  runTests();
148 
149  out.println();
150  out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
151  close();
152  } catch (Exception ex) {
153  // end the program regardless of threads
154  out.println("caught " + ex);
155  ex.printStackTrace();
156  System.exit(2); // return an error code
157  }
158  }
159 
160  // ----------------------------------------------------------------------
161  // driver intializers/finalizers
162  // ----------------------------------------------------------------------
163 
164  // loads a dynamically linked system library and reports any failures
165  static protected void loadSystemLibrary(String name) {
166  out.print("loading libary ...");
167  out.flush();
168  try {
169  System.loadLibrary(name);
170  } catch (UnsatisfiedLinkError e) {
171  String path;
172  try {
173  path = System.getProperty("java.library.path");
174  } catch (Exception ex) {
175  path = "<exception caught: " + ex.getMessage() + ">";
176  }
177  err.println("NdbBase: failed loading library '"
178  + name + "'; java.library.path='" + path + "'");
179  throw e;
180  } catch (SecurityException e) {
181  err.println("NdbBase: failed loading library '"
182  + name + "'; caught exception: " + e);
183  throw e;
184  }
185  out.println(" [" + name + "]");
186  }
187 
188  // attempts to run the JVM's Garbage Collector
189  static private void gc() {
190  // empirically determined limit after which no further
191  // reduction in memory usage has been observed
192  //final int nFullGCs = 5;
193  final int nFullGCs = 10;
194  for (int i = 0; i < nFullGCs; i++) {
195  //out.print("gc: ");
196  long oldfree;
197  long newfree = rt.freeMemory();
198  do {
199  oldfree = newfree;
200  rt.runFinalization();
201  rt.gc();
202  newfree = rt.freeMemory();
203  //out.print('.');
204  } while (newfree > oldfree);
205  //out.println();
206  }
207  }
208 
209  // initializes the driver's resources.
210  protected void init() throws Exception {
211  loadProperties();
212  initProperties();
213  printProperties();
214  openLogFile();
215  clearLogBuffers();
216  }
217 
218  // releases the driver's resources.
219  protected void close() throws Exception {
220  out.println();
221 
222  // release log buffers
223  logHeader = false;
224  header = null;
225  rtimes = null;
226  musage = null;
227 
228  closeLogFile();
229  props.clear();
230  }
231 
232  // loads the benchmark's properties from properties files
233  private void loadProperties() throws IOException {
234  out.println();
235  for (String fn : propFileNames) {
236  out.println("reading properties file: " + fn);
237  InputStream is = null;
238  try {
239  is = new FileInputStream(fn);
240  props.load(is);
241  } finally {
242  if (is != null)
243  is.close();
244  }
245  }
246  }
247 
248  // retrieves a property's value and parses it as a boolean
249  protected boolean parseBoolean(String k, boolean vdefault) {
250  final String v = props.getProperty(k);
251  return (v == null ? vdefault : Boolean.parseBoolean(v));
252  }
253 
254  // retrieves a property's value and parses it as a signed decimal integer
255  protected int parseInt(String k, int vdefault) {
256  final String v = props.getProperty(k);
257  try {
258  return (v == null ? vdefault : Integer.parseInt(v));
259  } catch (NumberFormatException e) {
260  final NumberFormatException nfe = new NumberFormatException(
261  "invalid value of benchmark property ('" + k + "', '"
262  + v + "').");
263  nfe.initCause(e);
264  throw nfe;
265  }
266  }
267 
268  // initializes the benchmark properties
269  protected void initProperties() {
270  //props.list(out);
271  out.print("setting driver properties ...");
272  out.flush();
273 
274  final StringBuilder msg = new StringBuilder();
275  final String eol = System.getProperty("line.separator");
276 
277  logRealTime = parseBoolean("logRealTime", true);
278  logMemUsage = parseBoolean("logMemUsage", false);
279  includeFullGC = parseBoolean("includeFullGC", false);
280 
281  warmupRuns = parseInt("warmupRuns", 0);
282  if (warmupRuns < 0) {
283  msg.append("[ignored] warmupRuns: " + warmupRuns + eol);
284  warmupRuns = 0;
285  }
286 
287  if (msg.length() == 0) {
288  out.println(" [ok]");
289  } else {
290  out.println();
291  out.print(msg.toString());
292  }
293  }
294 
295  // prints the benchmark's properties
296  protected void printProperties() {
297  out.println();
298  out.println("driver settings ...");
299  out.println("logRealTime: " + logRealTime);
300  out.println("logMemUsage: " + logMemUsage);
301  out.println("includeFullGC: " + includeFullGC);
302  out.println("warmupRuns: " + warmupRuns);
303  }
304 
305  // opens the benchmark's data log file
306  private void openLogFile() throws IOException {
307  out.println();
308  out.println("writing results to file: " + logFileName);
309  log = new PrintWriter(new FileWriter(logFileName, false));
310  }
311 
312  // closes the benchmark's data log file
313  private void closeLogFile() throws IOException {
314  out.print("closing files ...");
315  out.flush();
316  if (log != null) {
317  log.close();
318  log = null;
319  }
320  out.println(" [ok]");
321  }
322 
323  // ----------------------------------------------------------------------
324  // benchmark operations
325  // ----------------------------------------------------------------------
326 
327  abstract protected void runTests() throws Exception;
328 
329  protected void clearLogBuffers() {
330  logHeader = true;
331  header = new StringBuilder();
332  if (logRealTime) {
333  rtimes = new StringBuilder();
334  }
335  if (logMemUsage) {
336  musage = new StringBuilder();
337  }
338  }
339 
340  protected void writeLogBuffers(String descr) {
341  if (logRealTime) {
342  log.println(descr + ", rtime[ms]"
343  + header.toString() + endl
344  + rtimes.toString() + endl);
345  }
346  if (logMemUsage) {
347  log.println(descr + ", net musage[KiB]"
348  + header.toString() + endl
349  + musage.toString() + endl);
350  }
351  }
352 
353  protected void begin(String name) {
354  out.println();
355  out.println(name);
356 
357  // attempt max GC, before tx
358  gc();
359 
360  if (logMemUsage) {
361  m0 = rt.totalMemory() - rt.freeMemory();
362  }
363 
364  if (logRealTime) {
365  //t0 = System.currentTimeMillis();
366  t0 = System.nanoTime() / 1000000;
367  }
368  }
369 
370  protected void finish(String name) {
371  // attempt one full GC, before timing tx end
372  if (includeFullGC) {
373  rt.gc();
374  }
375 
376  if (logRealTime) {
377  //t1 = System.currentTimeMillis();
378  t1 = System.nanoTime() / 1000000;
379  final long t = t1 - t0;
380  out.println("tx real time " + t
381  + "\tms");
382  //rtimes.append("\t" + (Math.round(t / 100.0) / 10.0));
383  rtimes.append("\t" + t);
384  ta += t;
385  }
386 
387  if (logMemUsage) {
388  // attempt max GC, after tx
389  gc();
390  m1 = rt.totalMemory() - rt.freeMemory();
391  final long m0K = (m0 / 1024);
392  final long m1K = (m1 / 1024);
393  final long mK = m1K - m0K;
394  out.println("net mem usage "
395  + (mK >= 0 ? "+" : "") + mK
396  + "\tKiB [" + m0K + "K->" + m1K + "K]");
397  musage.append("\t" + mK);
398  ma += mK;
399  }
400 
401  if (logHeader)
402  header.append("\t" + name);
403  }
404 }