/**
   * Method to provide a gross level of synchronization with the target monitored jvm.
   *
   * <p>gross synchronization works by polling for the hotspot.rt.hrt.ticks counter, which is the
   * last counter created by the StatSampler initialization code. The counter is updated when the
   * watcher thread starts scheduling tasks, which is the last thing done in vm initialization.
   */
  protected void synchWithTarget(Map<String, Monitor> map) throws MonitorException {
    /*
     * synch must happen with syncWaitMs from now. Default is 5 seconds,
     * which is reasonabally generous and should provide for extreme
     * situations like startup delays due to allocation of large ISM heaps.
     */
    long timeLimit = System.currentTimeMillis() + syncWaitMs;

    String name = "hotspot.rt.hrt.ticks";
    LongMonitor ticks = (LongMonitor) pollFor(map, name, timeLimit);

    /*
     * loop waiting for the ticks counter to be non zero. This is
     * an indication that the jvm is initialized.
     */
    log("synchWithTarget: " + lvmid + " ");
    while (ticks.longValue() == 0) {
      log(".");

      try {
        Thread.sleep(20);
      } catch (InterruptedException e) {
      }

      if (System.currentTimeMillis() > timeLimit) {
        lognl("failed: " + lvmid);
        throw new MonitorException("Could Not Synchronize with target");
      }
    }
    lognl("success: " + lvmid);
  }
  /**
   * Method to poll the instrumentation memory for a counter with the given name. The polling period
   * is bounded by the timeLimit argument.
   */
  protected Monitor pollFor(Map<String, Monitor> map, String name, long timeLimit)
      throws MonitorException {
    Monitor monitor = null;

    log("polling for: " + lvmid + "," + name + " ");

    pollForEntry = nextEntry;
    while ((monitor = map.get(name)) == null) {
      log(".");

      try {
        Thread.sleep(20);
      } catch (InterruptedException e) {
      }

      long t = System.currentTimeMillis();
      if ((t > timeLimit) || (overflow.intValue() > 0)) {
        lognl("failed: " + lvmid + "," + name);
        dumpAll(map, lvmid);
        throw new MonitorException("Could not find expected counter");
      }

      getNewMonitors(map);
    }
    lognl("success: " + lvmid + "," + name);
    return monitor;
  }