/** {@inheritDoc} */
  protected void getNewMonitors(Map<String, Monitor> map) throws MonitorException {
    assert Thread.holdsLock(this);

    int used = prologue.getUsed();
    long modificationTime = prologue.getModificationTimeStamp();

    if ((used > lastUsed) || (lastModificationTime > modificationTime)) {

      lastUsed = used;
      lastModificationTime = modificationTime;

      Monitor monitor = getNextMonitorEntry();
      while (monitor != null) {
        String name = monitor.getName();

        // guard against duplicate entries
        if (!map.containsKey(name)) {
          map.put(name, monitor);

          /*
           * insertedMonitors is null when called from pollFor()
           * via buildMonitorMap(). Since we update insertedMonitors
           * at the end of buildMonitorMap(), it's ok to skip the
           * add here.
           */
          if (insertedMonitors != null) {
            insertedMonitors.add(monitor);
          }
        }
        monitor = getNextMonitorEntry();
      }
    }
  }
Beispiel #2
0
    /** set a new channel whose limits we wish to monitor */
    public void setChannel(final Channel channel) {
      synchronized (this) {
        if (_lowerChannel != null) {
          _lowerChannel.removeConnectionListener(this);
          if (_lowerMonitor != null) {
            _lowerMonitor.clear();
            _lowerMonitor = null;
          }
        }

        final String lowerLimitPV = channel.channelName() + ".LOPR";
        _lowerChannel = ChannelFactory.defaultFactory().getChannel(lowerLimitPV);
        _lowerChannel.addConnectionListener(this);
        _lowerChannel.requestConnection();

        if (_upperChannel != null) {
          _upperChannel.removeConnectionListener(this);
          if (_upperMonitor != null) {
            _upperMonitor.clear();
            _upperMonitor = null;
          }
        }

        final String upperLimitPV = channel.channelName() + ".HOPR";
        _upperChannel = ChannelFactory.defaultFactory().getChannel(upperLimitPV);
        _upperChannel.addConnectionListener(this);
        _upperChannel.requestConnection();
      }
    }
Beispiel #3
0
  public void destroy() {
    try {
      server_mon.enter();

      Iterator it = rendezvous_bindings.values().iterator();

      while (it.hasNext()) {

        Object[] entry = (Object[]) it.next();

        final DHTTransportUDPContact contact = (DHTTransportUDPContact) entry[0];

        new AEThread2("DHTNATPuncher:destroy", true) {
          public void run() {
            sendClose(contact);
          }
        }.start();
      }
    } catch (Throwable e) {

      log(e);

    } finally {

      server_mon.exit();
    }
  }
  /** Read all PeMS data in the given time range at the given VDS. */
  public PeMSProfile read(Interval interval, Long vdsId) throws DatabaseException {
    PeMSProfile profile;
    String pemsIdStr = "pems.{vds_id=" + vdsId + ", interval=" + interval + "}";

    long timeBegin = System.nanoTime();

    try {
      dbr.transactionBegin();
      Monitor.debug("PeMS reader transaction beginning on " + pemsIdStr);

      profile = readProfile(interval, vdsId);

      dbr.transactionCommit();
      Monitor.debug("PeMS reader transaction committing on " + pemsIdStr);
    } catch (DatabaseException dbExc) {
      Monitor.err(dbExc);
      throw dbExc;
    } finally {
      try {
        dbr.transactionRollback();
        Monitor.debug("PeMS reader transaction rollback on " + pemsIdStr);
      } catch (Exception Exc) {
        // Do nothing.
      }
    }

    long timeCommit = System.nanoTime();
    if (profile != null) {
      Monitor.duration("Read " + pemsIdStr, timeCommit - timeBegin);
    }

    return profile;
  }
 @Override
 public void reset() {
   super.reset();
   m_phi.reset();
   m_mons = new LinkedList<ValuePair>();
   m_variableResolver = new HashMap<String, String>();
 }
Beispiel #6
0
  protected void receiveTunnelInbound(DHTTransportUDPContact originator, Map data) {
    log("Received tunnel inbound message from " + originator.getString());

    try {
      punch_mon.enter();

      for (int i = 0; i < oustanding_punches.size(); i++) {

        Object[] wait_data = (Object[]) oustanding_punches.get(i);

        DHTTransportContact wait_contact = (DHTTransportContact) wait_data[0];

        if (originator.getAddress().getAddress().equals(wait_contact.getAddress().getAddress())) {

          wait_data[2] = new Integer(originator.getTransportAddress().getPort());

          ((AESemaphore) wait_data[1]).release();
        }
      }

    } finally {

      punch_mon.exit();
    }
  }
Beispiel #7
0
  protected void rendezvousFailed(DHTTransportContact current_target, boolean tidy) {
    log("Rendezvous " + (tidy ? "closed" : "failed") + ": " + current_target.getString());

    try {
      pub_mon.enter();

      failed_rendezvous.put(current_target.getAddress(), "");

    } finally {

      pub_mon.exit();
    }

    publish(true);
  }
  /** Method to dump debugging information */
  private void dumpAll(Map map, int lvmid) {
    if (DEBUG) {
      Set keys = map.keySet();

      System.err.println("Dump for " + lvmid);
      int j = 0;
      for (Iterator i = keys.iterator(); i.hasNext(); j++) {
        Monitor monitor = (Monitor) map.get(i.next());
        System.err.println(j + "\t" + monitor.getName() + "=" + monitor.getValue());
      }
      System.err.println("nextEntry = " + nextEntry + " pollForEntry = " + pollForEntry);
      System.err.println("Buffer info:");
      System.err.println("buffer = " + buffer);
    }
  }
  /**
   * Read all PeMS aggregate data in the given time range, having a VDS ID in the given list, and at
   * the given aggregation level. List is sorted by time.
   */
  public List<PeMSStationAggregate> read(
      Interval interval, List<Long> vdsIds, PeMSAggregate.AggregationLevel level)
      throws DatabaseException {

    List<PeMSStationAggregate> list;

    String pemsIdStr =
        "pems.{vds_id=["
            + vdsIds.get(0)
            + ","
            + vdsIds.get(1)
            + ",...], interval="
            + interval
            + ", level="
            + level
            + "}";

    long timeBegin = System.nanoTime();

    try {
      dbr.transactionBegin();
      Monitor.debug("PeMS aggregate reader transaction beginning on " + pemsIdStr);

      list = readList(interval, vdsIds, level);

      dbr.transactionCommit();
      Monitor.debug("PeMS aggregate reader transaction committing on " + pemsIdStr);
    } catch (DatabaseException dbExc) {
      Monitor.err(dbExc);
      throw dbExc;
    } finally {
      try {
        dbr.transactionRollback();
        Monitor.debug("PeMS aggregate reader transaction rollback on " + pemsIdStr);
      } catch (Exception Exc) {
        // Do nothing.
      }
    }

    long timeCommit = System.nanoTime();
    if (list != null) {
      Monitor.duration("Read " + pemsIdStr, timeCommit - timeBegin);
    }

    return list;
  }
Beispiel #10
0
  protected void receivePunch(DHTTransportUDPContact originator, Map request, Map response) {
    trace("received punch request");

    boolean ok = false;

    String target_str = new String((byte[]) request.get("target"));

    Object[] entry;

    try {
      server_mon.enter();

      entry = (Object[]) rendezvous_bindings.get(target_str);

    } finally {

      server_mon.exit();
    }

    if (entry != null) {

      DHTTransportUDPContact target = (DHTTransportUDPContact) entry[0];

      Map target_client_data = sendConnect(target, originator, (Map) request.get("client_data"));

      if (target_client_data != null) {

        response.put("client_data", target_client_data);

        response.put("port", new Long(target.getTransportAddress().getPort()));

        ok = true;
      }
    }

    log(
        "Rendezvous punch request from "
            + originator.getString()
            + " to "
            + target_str
            + " "
            + (ok ? "initiated" : "failed"));

    response.put("ok", new Long(ok ? 1 : 0));
  }
Beispiel #11
0
  protected void receiveBind(DHTTransportUDPContact originator, Map request, Map response) {
    trace("received bind request");

    boolean ok = true;
    boolean log = true;

    try {
      server_mon.enter();

      Object[] entry = (Object[]) rendezvous_bindings.get(originator.getAddress().toString());

      if (entry == null) {

        if (rendezvous_bindings.size() == RENDEZVOUS_SERVER_MAX) {

          ok = false;
        }
      } else {

        // already present, no need to log again

        log = false;
      }

      if (ok) {

        long now = plugin_interface.getUtilities().getCurrentSystemTime();

        rendezvous_bindings.put(
            originator.getAddress().toString(), new Object[] {originator, new Long(now)});

        response.put("port", new Long(originator.getAddress().getPort()));
      }
    } finally {

      server_mon.exit();
    }

    if (log) {

      log("Rendezvous request from " + originator.getString() + " " + (ok ? "accepted" : "denied"));
    }

    response.put("ok", new Long(ok ? 1 : 0));
  }
Beispiel #12
0
  /*
   * evaluate the given expression.
   */
  public Object evaluate(Expression e) {
    if (e == null) {
      return null;
    }

    if (debug) {
      System.out.println("Evaluating expression: " + e);
    }

    if (e instanceof Literal) {
      return ((Literal) e).getValue();
    }

    if (e instanceof Identifier) {
      Identifier id = (Identifier) e;
      if (map.containsKey(id.getName())) {
        return map.get(id.getName());
      } else {
        // cache the data values for coherency of the values over
        // the life of this expression executer.
        Monitor m = (Monitor) id.getValue();
        Object v = m.getValue();
        map.put(id.getName(), v);
        return v;
      }
    }

    Expression l = e.getLeft();
    Expression r = e.getRight();

    Operator op = e.getOperator();

    if (op == null) {
      return evaluate(l);
    } else {
      Double lval = new Double(((Number) evaluate(l)).doubleValue());
      Double rval = new Double(((Number) evaluate(r)).doubleValue());
      double result = op.eval(lval.doubleValue(), rval.doubleValue());
      if (debug) {
        System.out.println("Performed Operation: " + lval + op + rval + " = " + result);
      }
      return new Double(result);
    }
  }
  /** {@inheritDoc} */
  protected void buildMonitorMap(Map<String, Monitor> map) throws MonitorException {
    assert Thread.holdsLock(this);

    // start at the beginning of the buffer
    buffer.rewind();

    // create pseudo monitors
    buildPseudoMonitors(map);

    // position buffer to start of the data section
    buffer.position(prologue.getSize());
    nextEntry = buffer.position();
    perfDataItem = 0;

    int used = prologue.getUsed();
    long modificationTime = prologue.getModificationTimeStamp();

    Monitor m = getNextMonitorEntry();
    while (m != null) {
      map.put(m.getName(), m);
      m = getNextMonitorEntry();
    }

    /*
     * set the last modification data. These are set to the values
     * recorded before parsing the data structure. This allows the
     * the data structure to be modified while the Map is being built.
     * The Map may contain more entries than indicated based on the
     * time stamp, but this is handled by ignoring duplicate entries
     * when the Map is updated in getNewMonitors().
     */
    lastUsed = used;
    lastModificationTime = modificationTime;

    // synchronize with the target jvm
    synchWithTarget(map);

    // work around 1.4.2 counter inititization bugs
    kludge(map);

    insertedMonitors = new ArrayList<Monitor>(map.values());
  }
  /** Read all PeMS data in the given time range and having a VDS ID in the given list. */
  public PeMSSet read(Interval interval, List<Long> vdsIds) throws DatabaseException {
    PeMSSet set = null;

    String pemsIdStr =
        "pems.{vds_id=["
            + vdsIds.get(0)
            + ","
            + vdsIds.get(1)
            + ",...], interval="
            + interval
            + "}";

    long timeBegin = System.nanoTime();

    try {
      dbr.transactionBegin();
      Monitor.debug("PeMS reader transaction beginning on " + pemsIdStr);

      set = readSet(interval, vdsIds);

      dbr.transactionCommit();
      Monitor.debug("PeMS reader transaction committing on " + pemsIdStr);
    } catch (DatabaseException dbExc) {
      Monitor.err(dbExc);
      throw dbExc;
    } finally {
      try {
        dbr.transactionRollback();
        Monitor.debug("PeMS reader transaction rollback on " + pemsIdStr);
      } catch (Exception Exc) {
        // Do nothing.
      }
    }

    long timeCommit = System.nanoTime();
    if (set != null) {
      Monitor.duration("Read " + pemsIdStr, timeCommit - timeBegin);
    }

    return set;
  }
Beispiel #15
0
  protected void runRendezvous() {
    try {
      pub_mon.enter();

      if (!rendezvous_thread_running) {

        rendezvous_thread_running = true;

        plugin_interface
            .getUtilities()
            .createThread(
                "DHTNatPuncher:rendevzous",
                new Runnable() {
                  public void run() {
                    runRendezvousSupport();
                  }
                });
      }
    } finally {

      pub_mon.exit();
    }
  }
Beispiel #16
0
  protected Map sendPunch(
      DHTTransportContact rendezvous,
      final DHTTransportUDPContact target,
      Map originator_client_data,
      boolean no_tunnel) {
    AESemaphore wait_sem = new AESemaphore("DHTNatPuncher::sendPunch");
    Object[] wait_data = new Object[] {target, wait_sem, new Integer(0)};

    try {

      try {
        punch_mon.enter();

        oustanding_punches.add(wait_data);

      } finally {

        punch_mon.exit();
      }

      Map request = new HashMap();

      request.put("type", new Long(RT_PUNCH_REQUEST));

      request.put("target", target.getAddress().toString().getBytes());

      if (originator_client_data != null) {

        if (no_tunnel) {

          originator_client_data.put("_notunnel", new Long(1));
        }

        request.put("client_data", originator_client_data);
      }

      // for a message payload (i.e. no_tunnel) we double the initiator timeout to give
      // more chance for reasonable size messages to get through as they have to go through
      // 2 xfer processes

      Map response =
          sendRequest(rendezvous, request, no_tunnel ? TRANSFER_TIMEOUT * 2 : TRANSFER_TIMEOUT);

      if (response == null) {

        return (null);
      }

      if (((Long) response.get("type")).intValue() == RT_PUNCH_REPLY) {

        int result = ((Long) response.get("ok")).intValue();

        trace(
            "received "
                + (no_tunnel ? "message" : "punch")
                + " reply: "
                + (result == 0 ? "failed" : "ok"));

        if (result == 1) {

          // pick up port changes from the rendezvous

          Long indirect_port = (Long) response.get("port");

          if (indirect_port != null) {

            int transport_port = indirect_port.intValue();

            if (transport_port != 0) {

              InetSocketAddress existing_address = target.getTransportAddress();

              if (transport_port != existing_address.getPort()) {

                target.setTransportAddress(
                    new InetSocketAddress(existing_address.getAddress(), transport_port));
              }
            }
          }

          if (!no_tunnel) {

            // ping the target a few times to try and establish a tunnel

            UTTimerEvent event =
                timer.addPeriodicEvent(
                    3000,
                    new UTTimerEventPerformer() {
                      private int pings = 1;

                      public void perform(UTTimerEvent event) {
                        if (pings > 3) {

                          event.cancel();

                          return;
                        }

                        pings++;

                        if (sendTunnelOutbound(target)) {

                          event.cancel();
                        }
                      }
                    });

            if (sendTunnelOutbound(target)) {

              event.cancel();
            }

            // give the other end a few seconds to kick off some tunnel events to us

            if (wait_sem.reserve(10000)) {

              event.cancel();
            }
          }

          // routers often fiddle with the port when not mapped so we need to grab the right one to
          // use
          // for direct communication

          // first priority goes to direct tunnel messages received

          int transport_port = 0;

          try {
            punch_mon.enter();

            transport_port = ((Integer) wait_data[2]).intValue();

          } finally {

            punch_mon.exit();
          }

          if (transport_port != 0) {

            InetSocketAddress existing_address = target.getTransportAddress();

            if (transport_port != existing_address.getPort()) {

              target.setTransportAddress(
                  new InetSocketAddress(existing_address.getAddress(), transport_port));
            }
          }

          Map target_client_data = (Map) response.get("client_data");

          if (target_client_data == null) {

            target_client_data = new HashMap();
          }

          return (target_client_data);
        }
      }

      return (null);

    } catch (Throwable e) {

      log(e);

      return (null);

    } finally {

      try {
        punch_mon.enter();

        oustanding_punches.remove(wait_data);

      } finally {

        punch_mon.exit();
      }
    }
  }
Beispiel #17
0
  protected void publishSupport() {
    DHTTransport transport = dht.getTransport();

    if (TESTING || !transport.isReachable()) {

      DHTTransportContact local_contact = transport.getLocalContact();

      // see if the rendezvous has failed and therefore we are required to find a new one

      boolean force =
          rendezvous_target != null
              && failed_rendezvous.containsKey(rendezvous_target.getAddress());

      if (rendezvous_local_contact != null && !force) {

        if (local_contact.getAddress().equals(rendezvous_local_contact.getAddress())) {

          // already running for the current local contact

          return;
        }
      }

      DHTTransportContact explicit =
          (DHTTransportContact) explicit_rendezvous_map.get(local_contact.getAddress());

      if (explicit != null) {

        try {
          pub_mon.enter();

          rendezvous_local_contact = local_contact;
          rendezvous_target = explicit;

          runRendezvous();

        } finally {

          pub_mon.exit();
        }
      } else {

        final DHTTransportContact[] new_rendezvous_target = {null};

        DHTTransportContact[] reachables = dht.getTransport().getReachableContacts();

        int reachables_tried = 0;
        int reachables_skipped = 0;

        final Semaphore sem = plugin_interface.getUtilities().getSemaphore();

        for (int i = 0; i < reachables.length; i++) {

          DHTTransportContact contact = reachables[i];

          try {
            pub_mon.enter();

            // see if we've found a good one yet

            if (new_rendezvous_target[0] != null) {

              break;
            }

            // skip any known bad ones

            if (failed_rendezvous.containsKey(contact.getAddress())) {

              reachables_skipped++;

              sem.release();

              continue;
            }
          } finally {

            pub_mon.exit();
          }

          if (i > 0) {

            try {
              Thread.sleep(1000);

            } catch (Throwable e) {

            }
          }

          reachables_tried++;

          contact.sendPing(
              new DHTTransportReplyHandlerAdapter() {
                public void pingReply(DHTTransportContact ok_contact) {
                  trace("Punch:" + ok_contact.getString() + " OK");

                  try {
                    pub_mon.enter();

                    if (new_rendezvous_target[0] == null) {

                      new_rendezvous_target[0] = ok_contact;
                    }
                  } finally {

                    pub_mon.exit();

                    sem.release();
                  }
                }

                public void failed(DHTTransportContact failed_contact, Throwable e) {
                  try {
                    trace("Punch:" + failed_contact.getString() + " Failed");

                  } finally {

                    sem.release();
                  }
                }
              });
        }

        for (int i = 0; i < reachables.length; i++) {

          sem.reserve();

          try {
            pub_mon.enter();

            if (new_rendezvous_target[0] != null) {

              rendezvous_target = new_rendezvous_target[0];
              rendezvous_local_contact = local_contact;

              log(
                  "Rendezvous found: "
                      + rendezvous_local_contact.getString()
                      + " -> "
                      + rendezvous_target.getString());

              runRendezvous();

              break;
            }
          } finally {

            pub_mon.exit();
          }
        }

        if (new_rendezvous_target[0] == null) {

          log(
              "No rendezvous found: candidates="
                  + reachables.length
                  + ",tried="
                  + reachables_tried
                  + ",skipped="
                  + reachables_skipped);

          try {
            pub_mon.enter();

            rendezvous_local_contact = null;
            rendezvous_target = null;

          } finally {

            pub_mon.exit();
          }
        }
      }
    } else {

      try {
        pub_mon.enter();

        rendezvous_local_contact = null;
        rendezvous_target = null;

      } finally {

        pub_mon.exit();
      }
    }
  }
 @Override
 public void setValue(Atom a, Atom v) {
   m_phi.setValue(a, v);
   m_variableResolver.put(a.toString(), v.toString());
 }
Beispiel #19
0
  public static void main(String[] args) {

    Monitor timer = MonitorFactory.start("totaltime");
    for (int i = 1; i <= 10; i++) {
      System.out.println(MonitorFactory.add("NIC.bytes.sent", "bytes", i * 1000));
      System.out.println(MonitorFactory.add("negativetest", "neg", -1000.0 * i));
    }

    System.out.println("");
    Monitor m = null;
    m = MonitorFactory.start("purchasesTimeTestNoRange");

    for (int i = 1; i <= 1000000; i++) MonitorFactory.add("purchasesNoRange", "dollars", 1000.0);

    System.out.println("purchasesTimeTestNoRange=" + m.stop().getTotal());

    m = MonitorFactory.start("testTimerTimeTest");
    for (int i = 1; i <= 1000000; i++) MonitorFactory.start("testTimer").stop();

    System.out.println("testTimerTimeTest=" + m.stop().getTotal());
    for (int i = -5; i <= 20; i++) {
      MonitorFactory.add("purchases", "dollars", i * 50);
    }

    System.out.println("");

    System.out.println(MonitorFactory.add("NIC.bytes.received", "bytes", 250.0));
    System.out.println(MonitorFactory.add("NIC.bytes.received", "bytes", 250.0));

    timer.stop();

    for (int i = -5; i < 25; i++) MonitorFactory.add("timetest", "ms.", i * 5);

    System.out.println(MonitorFactory.getMonitor("purchases", "dollars").getHits());
    System.out.println(MonitorFactory.getTimeMonitor("testTimer").getHits());
    // case sensitive so won't print
    System.out.println(MonitorFactory.getTimeMonitor("testtimer").getHits());
    System.out.println("Total time=" + timer.getTotal());

    MonitorFactory.reset();
    MonitorFactory.setRangeDefault("dollars", getTestHolder());
    m = MonitorFactory.start("purchasesTimeTestRange");
    for (int i = 1; i <= 1000000; i++) MonitorFactory.add("purchasesRange", "dollars", 1000.0);

    System.out.println("purchasesTimeTestRange=" + m.stop().getTotal());
    Object[][] data = null;
    MonitorFactory.setRangeDefault("bytes", getTestHolder());
    MonitorFactory.setRangeDefault("cents", getTestHolder());
    MonitorFactory.setRangeDefault("minutes", getTestHolder());
    MonitorFactory.setRangeDefault("MB", getTestHolder());
    MonitorFactory.setRangeDefault("KB", getTestHolder());
    MonitorFactory.setRangeDefault("points", getTestHolder());

    String[] header = MonitorFactory.getComposite("ms.").getHeader();

    data = MonitorFactory.getComposite("ms.").getData();
    header = MonitorFactory.getComposite("ms.").getBasicHeader();

    data = MonitorFactory.getComposite("ms.").getBasicData();
    header = MonitorFactory.getComposite("ms.").getDisplayHeader();

    data = MonitorFactory.getComposite("ms.").getDisplayData();
    MonitorFactory.getComposite("ms.").disable();

    header = MonitorFactory.getComposite("ms.").getHeader();
    data = MonitorFactory.getComposite("ms.").getData();

    System.out.println("header length=" + header.length + ", data length=" + data.length);
    System.out.println("JAMon Version=" + MonitorFactory.getVersion());
  }
Beispiel #20
0
 public int compare(Monitor o1, Monitor o2) {
   String name1 = o1.getName();
   String name2 = o2.getName();
   return name2.compareTo(name1);
 }