1 /* 2 * Copyright (c) 2004, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.tools.jstatd; 27 28 import java.util.*; 29 import java.nio.*; 30 import java.io.*; 31 import java.net.*; 32 import java.rmi.*; 33 import java.rmi.server.*; 34 import sun.jvmstat.monitor.*; 35 import sun.jvmstat.monitor.event.*; 36 import sun.jvmstat.monitor.remote.*; 37 38 /** 39 * Concrete implementation of the RemoteHost interface for the HotSpot 40 * PerfData <em>rmi:</em> protocol. 41 * <p> 42 * This class provides remote access to the instrumentation exported 43 * by HotSpot Java Virtual Machines through the PerfData shared memory 44 * interface. 45 * 46 * @author Brian Doherty 47 * @since 1.5 48 */ 49 public class RemoteHostImpl implements RemoteHost, HostListener { 50 51 private MonitoredHost monitoredHost; 52 private Set<Integer> activeVms; 53 private static RemoteVm rvm; 54 private final int rmiPort; 55 RemoteHostImpl()56 public RemoteHostImpl() throws MonitorException { 57 this(0); 58 } 59 RemoteHostImpl(int rmiPort)60 public RemoteHostImpl(int rmiPort) throws MonitorException { 61 this.rmiPort = rmiPort; 62 try { 63 monitoredHost = MonitoredHost.getMonitoredHost("localhost"); 64 } catch (URISyntaxException e) { } 65 66 activeVms = monitoredHost.activeVms(); 67 monitoredHost.addHostListener(this); 68 } 69 attachVm(int lvmid, String mode)70 public RemoteVm attachVm(int lvmid, String mode) 71 throws RemoteException, MonitorException { 72 Integer v = lvmid; 73 RemoteVm stub = null; 74 StringBuilder sb = new StringBuilder(); 75 76 sb.append("local://").append(lvmid).append("@localhost"); 77 if (mode != null) { 78 sb.append("?mode=").append(mode); 79 } 80 81 String vmidStr = sb.toString(); 82 83 try { 84 VmIdentifier vmid = new VmIdentifier(vmidStr); 85 MonitoredVm mvm = monitoredHost.getMonitoredVm(vmid); 86 rvm = new RemoteVmImpl((BufferedMonitoredVm)mvm); 87 stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, rmiPort); 88 } 89 catch (URISyntaxException e) { 90 throw new RuntimeException("Malformed VmIdentifier URI: " 91 + vmidStr, e); 92 } 93 return stub; 94 } 95 detachVm(RemoteVm rvm)96 public void detachVm(RemoteVm rvm) throws RemoteException { 97 rvm.detach(); 98 } 99 activeVms()100 public int[] activeVms() throws MonitorException { 101 Object[] vms = null; 102 int[] vmids = null; 103 104 vms = monitoredHost.activeVms().toArray(); 105 vmids = new int[vms.length]; 106 107 for (int i = 0; i < vmids.length; i++) { 108 vmids[i] = ((Integer)vms[i]).intValue(); 109 } 110 return vmids; 111 } 112 vmStatusChanged(VmStatusChangeEvent ev)113 public void vmStatusChanged(VmStatusChangeEvent ev) { 114 synchronized(this.activeVms) { 115 activeVms.retainAll(ev.getActive()); 116 } 117 } 118 disconnected(HostEvent ev)119 public void disconnected(HostEvent ev) { 120 // we only monitor the local host, so this event shouldn't occur. 121 } 122 } 123