jdk/src/jdk.jstatd/share/classes/sun/jvmstat/perfdata/monitor/protocol/rmi/RemoteMonitoredVm.java
author mchung
Wed, 27 Apr 2016 12:06:51 -0700
changeset 37675 a9be5f4baa63
parent 34351 jdk/src/jdk.jvmstat.rmi/share/classes/sun/jvmstat/perfdata/monitor/protocol/rmi/RemoteMonitoredVm.java@f30070a16a4b
permissions -rw-r--r--
8154905: Rename jdk.jvmstat.rmi to jdk.jstatd Reviewed-by: alanb, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.jvmstat.perfdata.monitor.protocol.rmi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.jvmstat.monitor.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.jvmstat.monitor.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.jvmstat.perfdata.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Concrete implementation of the AbstractMonitoredVm class for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <em>rmi:</em> protocol for the HotSpot PerfData monitoring implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class provides the ability to acquire to the instrumentation buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * of a live, remote target Java Virtual Machine through an RMI server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class RemoteMonitoredVm extends AbstractMonitoredVm {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private ArrayList<VmListener> listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private NotifierTask notifierTask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private SamplerTask samplerTask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private RemoteVm rvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private ByteBuffer updateBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Create a RemoteMonitoredVm instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param rvm the proxy to the remote MonitoredVm instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param vmid the vm identifier specifying the remot target JVM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param timer the timer used to run polling tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param interval the sampling interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public RemoteMonitoredVm(RemoteVm rvm, VmIdentifier vmid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                             Timer timer, int interval)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
           throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        super(vmid, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.rvm = rvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        pdb = new PerfDataBuffer(rvm, vmid.getLocalVmId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.listeners = new ArrayList<VmListener>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.timer = timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Method to attach to the remote MonitoredVm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void attach() throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        updateBuffer = pdb.getByteBuffer().duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // if continuous sampling is requested, register with the sampler thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (interval > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            samplerTask = new SamplerTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            timer.schedule(samplerTask, 0, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void detach() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (interval > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (samplerTask != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    samplerTask.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    samplerTask = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (notifierTask != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    notifierTask.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    notifierTask = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                sample();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            // XXX: - use logging api? throw an exception instead?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            System.err.println("Could not read data for remote JVM " + vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            super.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Get a copy of the remote instrumentation buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The data in the remote instrumentation buffer is copied into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * a local byte buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws RemoteException Thrown on any communications errors with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *                         the remote system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void sample() throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        assert updateBuffer != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        ((PerfDataBuffer)pdb).sample(updateBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Get the proxy to the remote MonitoredVm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return RemoteVm - the proxy to the remote MonitoredVm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public RemoteVm getRemoteVm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return rvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void addVmListener(VmListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            listeners.add(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (notifierTask == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                notifierTask = new NotifierTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                timer.schedule(notifierTask, 0, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public void removeVmListener(VmListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            listeners.remove(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (listeners.isEmpty() && (notifierTask != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                notifierTask.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                notifierTask = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void setInterval(int newInterval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (newInterval == interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            int oldInterval = interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            super.setInterval(newInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (samplerTask != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                samplerTask.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                SamplerTask oldSamplerTask = samplerTask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                samplerTask = new SamplerTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                CountedTimerTaskUtils.reschedule(timer, oldSamplerTask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                                 samplerTask, oldInterval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                                 newInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (notifierTask != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                notifierTask.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                NotifierTask oldNotifierTask = notifierTask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                notifierTask = new NotifierTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                CountedTimerTaskUtils.reschedule(timer, oldNotifierTask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                                 notifierTask, oldInterval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                                 newInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Fire MonitoredVmStructureChanged events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param inserted List of Monitor objects inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param removed List of Monitor objects removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   201
    @SuppressWarnings("unchecked") // Cast of result of clone
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   202
    void fireMonitorStatusChangedEvents(List<Monitor> inserted, List<Monitor> removed) {
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   203
        ArrayList<VmListener> registered = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        MonitorStatusChangeEvent ev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            registered = (ArrayList)listeners.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   210
        for (Iterator<VmListener> i = registered.iterator(); i.hasNext(); /* empty */) {
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   211
            VmListener l = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (ev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                ev = new MonitorStatusChangeEvent(this, inserted, removed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            l.monitorStatusChanged(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Fire MonitoredVmStructureChanged events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   222
    @SuppressWarnings("unchecked") // Cast of result of clone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    void fireMonitorsUpdatedEvents() {
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   224
        ArrayList<VmListener> registered = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        VmEvent ev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        synchronized(listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            registered = (ArrayList)listeners.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   231
        for (Iterator<VmListener> i = registered.iterator(); i.hasNext(); /* empty */) {
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   232
            VmListener l = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (ev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                ev = new VmEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            l.monitorsUpdated(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Timer Tasks. There are two separate timer tasks here. The SamplerTask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * is active whenever we are attached to the remote JVM with a periodic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * sampling interval > 0. The NotifierTask is only active if a VmListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * has registered with this RemoteMonitoredVm instance. Also, in the future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * we may want to run these tasks at different intervals. Currently,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * they run at the same interval and some significant work may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * need to be done to complete the separation of these two intervals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Class to periodically check the state of the defined monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * for the remote MonitoredVm instance and to notify listeners of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * any detected changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private class NotifierTask extends CountedTimerTask {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            super.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                MonitorStatus status = getMonitorStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
25401
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   261
                List<Monitor> inserted = status.getInserted();
2e2b5a66a787 8049794: Fix raw and unchecked warnings in jvmstat
darcy
parents: 5506
diff changeset
   262
                List<Monitor> removed = status.getRemoved();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if (!inserted.isEmpty() || !removed.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    fireMonitorStatusChangedEvents(inserted, removed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } catch (MonitorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                // XXX: use logging api? fire disconnect events? mark errored?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                // fireDisconnectedEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                System.err.println("Exception updating monitors for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                   + getVmIdentifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                // XXX: should we cancle the notifierTask here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                // this.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Class to periodically sample the remote instrumentation byte buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * and refresh the local copy. Registered listeners are notified of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * the completion of a sampling event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private class SamplerTask extends CountedTimerTask {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            super.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                sample();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                fireMonitorsUpdatedEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                // XXX: use logging api, mark vm as errored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                System.err.println("Exception taking sample for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                   + getVmIdentifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                this.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}