@Override
  public Map<String, Metric> getMetrics() {
    final Map<String, Metric> gauges = new HashMap<String, Metric>();

    for (final Thread.State state : Thread.State.values()) {
      gauges.put(
          name("systats.threads.bystate." + state.toString().toLowerCase(), "count"),
          new Gauge<Long>() {
            public Long getValue() {
              ThreadInfo[] currentThreads =
                  threads.getThreadInfo(threads.getAllThreadIds(), STACK_TRACE_DEPTH);

              Long value = 0L;
              for (ThreadInfo info : currentThreads) {
                if (info != null && info.getThreadState() == state) {
                  value++;
                }
              }

              return value;
            }
          });
    }

    gauges.put(
        "systats.threads.standardthreads.count",
        new Gauge<Integer>() {
          public Integer getValue() {
            return threads.getThreadCount();
          }
        });

    gauges.put(
        "systats.threads.demonthreads.count",
        new Gauge<Integer>() {
          public Integer getValue() {
            return threads.getDaemonThreadCount();
          }
        });

    return Collections.unmodifiableMap(gauges);
  }
예제 #2
0
 /** Ferma definitivamente il thread. */
 public void ferma() {
   loop = false;
   vivo = false;
   while (true) {
     if (getState().equals(Thread.State.valueOf("TERMINATED"))) {
       try {
         Thread.sleep(100);
         break;
       } catch (Exception e) {
       }
     }
   }
 }
  /**
   * Create a new factory generating threads whose runtime and contention behavior is tracked in
   * this factory.
   *
   * @param nThreadsToCreate the number of threads we will create in the factory before it's
   *     considered complete // TODO -- remove argument when we figure out how to implement this
   *     capability
   */
  public StateMonitoringThreadFactory(final int nThreadsToCreate) {
    if (nThreadsToCreate <= 0)
      throw new IllegalArgumentException("nThreadsToCreate <= 0: " + nThreadsToCreate);

    this.nThreadsToCreate = nThreadsToCreate;
    activeThreads = new ArrayList<Thread>(nThreadsToCreate);

    // initialize times to 0
    for (final Thread.State state : Thread.State.values()) times.put(state, 0l);

    // get the bean, and start tracking
    bean = ManagementFactory.getThreadMXBean();
    if (bean.isThreadContentionMonitoringSupported())
      bean.setThreadContentionMonitoringEnabled(true);
    else
      logger.warn(
          "Thread contention monitoring not supported, we cannot track GATK multi-threaded efficiency");
    // bean.setThreadCpuTimeEnabled(true);

    countDownLatch = new CountDownLatch(nThreadsToCreate);
  }
예제 #4
0
  private static synchronized void initThreadStateMap() {
    if (threadStateMap != null) {
      return;
    }

    final Thread.State[] ts = Thread.State.values();

    final int[][] vmThreadStateValues = new int[ts.length][];
    final String[][] vmThreadStateNames = new String[ts.length][];
    getThreadStateValues(vmThreadStateValues, vmThreadStateNames);

    threadStateMap = new HashMap<Integer, Thread.State>();
    threadStateNames = new HashMap<Integer, String>();
    for (int i = 0; i < ts.length; i++) {
      String state = ts[i].name();
      int[] values = null;
      String[] names = null;
      for (int j = 0; j < ts.length; j++) {
        if (vmThreadStateNames[j][0].startsWith(state)) {
          values = vmThreadStateValues[j];
          names = vmThreadStateNames[j];
        }
      }
      if (values == null) {
        throw new InternalError("No VM thread state mapped to " + state);
      }
      if (values.length != names.length) {
        throw new InternalError(
            "VM thread state values and names " + " mapped to " + state + ": length not matched");
      }
      for (int k = 0; k < values.length; k++) {
        threadStateMap.put(values[k], ts[i]);
        threadStateNames.put(values[k], names[k]);
      }
    }
  }