本文整理汇总了Java中sun.tools.attach.HotSpotVirtualMachine类的典型用法代码示例。如果您正苦于以下问题:Java HotSpotVirtualMachine类的具体用法?Java HotSpotVirtualMachine怎么用?Java HotSpotVirtualMachine使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HotSpotVirtualMachine类属于sun.tools.attach包,在下文中一共展示了HotSpotVirtualMachine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: dump
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
private static void dump(String pid, String options) throws IOException {
// parse the options to get the dump filename
String filename = parseDumpOptions(options);
if (filename == null) {
usage(1); // invalid options or no filename
}
// get the canonical path - important to avoid just passing
// a "heap.bin" and having the dump created in the target VM
// working directory rather than the directory where jmap
// is executed.
filename = new File(filename).getCanonicalPath();
// dump live objects only or not
boolean live = isDumpLiveObjects(options);
VirtualMachine vm = attach(pid);
System.out.println("Dumping heap to " + filename + " ...");
InputStream in = ((HotSpotVirtualMachine)vm).
dumpHeap((Object)filename,
(live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION));
drain(vm, in);
}
示例2: executeCommandForPid
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
private static void executeCommandForPid(String pid, String command, Object ... args)
throws AttachNotSupportedException, IOException,
UnsupportedEncodingException {
VirtualMachine vm = VirtualMachine.attach(pid);
// Cast to HotSpotVirtualMachine as this is an
// implementation specific method.
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
try (InputStream in = hvm.executeCommand(command, args)) {
// read to EOF and just print output
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
}
vm.detach();
}
示例3: runThreadDump
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
private static String runThreadDump(String pid) throws Exception {
VirtualMachine vm = VirtualMachine.attach(pid);
// Cast to HotSpotVirtualMachine as this is implementation specific
// method.
InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump(new Object[0]);
// read to EOF and just print output
byte b[] = new byte[256];
int n;
StringBuffer text = new StringBuffer();
do {
n = in.read(b);
if (n > 0) {
text.append(new String(b, 0, n, "UTF-8"));
}
} while (n > 0);
in.close();
vm.detach();
return text.toString();
}
示例4: dump
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
private static void dump(String pid, String options) throws IOException {
// parse the options to get the dump filename
String filename = parseDumpOptions(options);
if (filename == null) {
usage(); // invalid options or no filename
}
// get the canonical path - important to avoid just passing
// a "heap.bin" and having the dump created in the target VM
// working directory rather than the directory where jmap
// is executed.
filename = new File(filename).getCanonicalPath();
// dump live objects only or not
boolean live = isDumpLiveObjects(options);
VirtualMachine vm = attach(pid);
System.out.println("Dumping heap to " + filename + " ...");
InputStream in = ((HotSpotVirtualMachine)vm).
dumpHeap((Object)filename,
(live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION));
drain(vm, in);
}
示例5: runThreadDump
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
private static void runThreadDump(String pid, String args[]) throws Exception {
VirtualMachine vm = null;
try {
vm = VirtualMachine.attach(pid);
} catch (Exception x) {
String msg = x.getMessage();
if (msg != null) {
System.err.println(pid + ": " + msg);
} else {
x.printStackTrace();
}
if ((x instanceof AttachNotSupportedException) &&
(loadSAClass() != null)) {
System.err.println("The -F option can be used when the target " +
"process is not responding");
}
System.exit(1);
}
// Cast to HotSpotVirtualMachine as this is implementation specific
// method.
InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);
// read to EOF and just print output
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
in.close();
vm.detach();
}
示例6: executeCommandForPid
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
private static void executeCommandForPid(String pid, String command)
throws AttachNotSupportedException, IOException,
UnsupportedEncodingException {
VirtualMachine vm = VirtualMachine.attach(pid);
// Cast to HotSpotVirtualMachine as this is an
// implementation specific method.
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
String lines[] = command.split("\\n");
for (String line : lines) {
if (line.trim().equals("stop")) {
break;
}
try (InputStream in = hvm.executeJCmd(line);) {
// read to EOF and just print output
byte b[] = new byte[256];
int n;
boolean messagePrinted = false;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
messagePrinted = true;
}
} while (n > 0);
if (!messagePrinted) {
System.out.println("Command executed successfully");
}
}
}
vm.detach();
}
示例7: runTest
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
public static boolean runTest() throws Exception {
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
"-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
Process target = pb.start();
Path pidFile = null;
try {
int pid = getUnixProcessId(target);
// create the stale .java_pid file. use hard-coded /tmp path as in th VM
pidFile = createJavaPidFile(pid);
if(pidFile == null) {
return false;
}
// wait for vm.paused file to be created and delete it once we find it.
waitForAndResumeVM(pid);
// unfortunately there's no reliable way to know the VM is ready to receive the
// attach request so we have to do an arbitrary sleep.
Thread.sleep(5000);
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
String line = null;
while((line = remoteDataReader.readLine()) != null);
vm.detach();
return true;
}
finally {
target.destroy();
target.waitFor();
if(pidFile != null && Files.exists(pidFile)) {
Files.delete(pidFile);
}
}
}
示例8: runTest
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
public static boolean runTest() throws Exception {
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
"-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
Process target = pb.start();
Path pidFile = null;
try {
int pid = getUnixProcessId(target);
// create the stale .java_pid file. use hard-coded /tmp path as in th VM
pidFile = createJavaPidFile(pid);
if(pidFile == null) {
return false;
}
// wait for vm.paused file to be created and delete it once we find it.
waitForAndResumeVM(pid);
waitForTargetReady(target);
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
String line = null;
while((line = remoteDataReader.readLine()) != null);
vm.detach();
return true;
}
finally {
target.destroy();
target.waitFor();
if(pidFile != null && Files.exists(pidFile)) {
Files.delete(pidFile);
}
}
}
示例9: testGetFlag
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
public static void testGetFlag(String flagName, String flagValue) throws Exception {
ProcessBuilder pb = runTarget(flagName, flagValue);
Process target = pb.start();
try {
waitForReady(target);
int pid = (int)target.pid();
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
// Test Get
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(
vm.printFlag(flagName)));
boolean foundExpectedLine = false;
String line = null;
while((line = remoteDataReader.readLine()) != null) {
System.out.println("printFlag: " + line);
if (line.equals("-XX:" + flagName + "=" + flagValue)) {
foundExpectedLine = true;
}
}
Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");
vm.detach();
}
finally {
target.destroy();
target.waitFor();
}
}
示例10: setFlagAttach
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
private boolean setFlagAttach(HotSpotVirtualMachine vm, String flagName, String flagValue) throws Exception {
boolean result;
try {
vm.setFlag(flagName, flagValue);
result = true;
} catch (AttachOperationFailedException e) {
result = false;
}
return result;
}
示例11: runThreadDump
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
private static void runThreadDump(String pid, String args[]) throws Exception {
VirtualMachine vm = null;
try {
vm = VirtualMachine.attach(pid);
} catch (Exception x) {
String msg = x.getMessage();
if (msg != null) {
System.err.println(pid + ": " + msg);
} else {
x.printStackTrace();
}
System.exit(1);
}
// Cast to HotSpotVirtualMachine as this is implementation specific
// method.
InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);
// read to EOF and just print output
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
in.close();
vm.detach();
}
示例12: getInstanceCountFromHeapHisto
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
/**
* 'vm.heapHisto("-live")' will request a full GC
*/
private static int getInstanceCountFromHeapHisto() throws AttachNotSupportedException, Exception {
int instanceCount = 0;
HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
.attach(Long.toString(ProcessTools.getProcessId()));
try {
try (InputStream heapHistoStream = vm.heapHisto("-live");
BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.contains(TARGET_CLASS)) {
instanceCount = Integer.parseInt(inputLine
.split("[ ]+")[2]);
System.out.println("instance count: " + instanceCount);
break;
}
}
}
} finally {
vm.detach();
}
assertGreaterThan(instanceCount, 0, "No instances of " + TARGET_CLASS + " are found");
return instanceCount;
}
示例13: setOptionUsingAttach
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
private static void setOptionUsingAttach(String option, String value) throws Exception {
HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine.attach(ProcessTools.getProcessId()+"");
InputStream in = vm.setFlag(option, value);
System.out.println("Result from setting '" + option + "' to '" + value + "' using attach:");
drain(vm, in);
System.out.println("-- end -- ");
}
示例14: testGetFlag
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
public static void testGetFlag(String flagName, String flagValue) throws Exception {
ProcessBuilder pb = runTarget(flagName, flagValue);
Process target = pb.start();
try {
waitForReady(target);
int pid = (int)target.getPid();
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
// Test Get
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(
vm.printFlag(flagName)));
boolean foundExpectedLine = false;
String line = null;
while((line = remoteDataReader.readLine()) != null) {
System.out.println("printFlag: " + line);
if (line.equals("-XX:" + flagName + "=" + flagValue)) {
foundExpectedLine = true;
}
}
Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");
vm.detach();
}
finally {
target.destroy();
target.waitFor();
}
}
示例15: testExecute
import sun.tools.attach.HotSpotVirtualMachine; //导入依赖的package包/类
@Test
public void testExecute() throws IOException, AttachNotSupportedException {
LocalVirtualMachineTemplate localVirtualMachineTemplate = new LocalVirtualMachineTemplate();
AttachProvider result = localVirtualMachineTemplate.execute(new HotSpotVirtualMachineCallback<AttachProvider>() {
@Override
public AttachProvider doInVirtualMachine(HotSpotVirtualMachine virtualMachine) throws IOException {
AttachProvider attachProvider = virtualMachine.provider();
return attachProvider;
}
});
Assert.assertNotNull(result);
}