01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.test.server.util;
06:
07: import com.tc.util.runtime.Os;
08:
09: import java.io.File;
10: import java.io.FileOutputStream;
11: import java.io.IOException;
12:
13: public class VmStat {
14:
15: private static Process process;
16: private static File scriptFile;
17: private static final String scriptName = "capture-vmstat.sh";
18: private static final String scriptContents = "#!/bin/sh\nexec vmstat 1 > vmstat.txt\n";
19:
20: public static synchronized void start(File workingDir)
21: throws IOException {
22: if (process != null) {
23: stop();
24: throw new IllegalStateException(
25: "VmStat is already running. Stopping VmStat...");
26: }
27: // win* and mac not supported
28: if (Os.isWindows() || Os.isMac())
29: return;
30:
31: String[] commandLine = new String[2];
32: commandLine[0] = "/bin/bash";
33: commandLine[1] = createScriptFile(workingDir).toString();
34:
35: Runtime runtime = Runtime.getRuntime();
36: process = runtime.exec(commandLine, null, workingDir);
37:
38: String msg = "\n";
39: msg += "*****************************\n";
40: msg += "* Running vmstat in [" + workingDir + "]\n";
41: msg += "*****************************\n";
42: System.out.println(msg);
43: }
44:
45: public static synchronized void stop() {
46: if (process != null) {
47: process.destroy();
48: process = null;
49: deleteScriptFile();
50: }
51: }
52:
53: private static synchronized File createScriptFile(File baseDir)
54: throws IOException {
55: if (scriptFile != null)
56: return scriptFile;
57: File script = new File(baseDir + File.separator + scriptName);
58: script.createNewFile();
59: FileOutputStream out = new FileOutputStream(script);
60: out.write(scriptContents.getBytes());
61: out.flush();
62: out.close();
63: return scriptFile = script;
64: }
65:
66: private static synchronized void deleteScriptFile() {
67: if (scriptFile != null) {
68: scriptFile.delete();
69: scriptFile = null;
70: }
71: }
72: }
|