private static Set<Dependency> collectFromDependencyMonitor(
      ThreadMXBean bean, Serializable locality, Map<Long, ThreadInfo> threadInfos) {
    HashSet<Dependency> results = new HashSet<Dependency>();

    // Convert the held resources into serializable dependencies
    Set<Dependency<Serializable, Thread>> heldResources =
        DependencyMonitorManager.getHeldResources();
    for (Dependency<Serializable, Thread> dep : heldResources) {
      Thread thread = dep.getDependsOn();
      Serializable resource = dep.getDepender();
      ThreadInfo info = threadInfos.get(thread.getId());
      if (info == null) {
        info = bean.getThreadInfo(thread.getId());
      }
      if (info != null) {
        results.add(new Dependency(resource, new LocalThread(locality, info)));
      }
    }

    Set<Dependency<Thread, Serializable>> blockedThreads =
        DependencyMonitorManager.getBlockedThreads();

    // Convert the blocked threads into serializable dependencies
    for (Dependency<Thread, Serializable> dep : blockedThreads) {
      Thread thread = dep.getDepender();
      ThreadInfo info = threadInfos.get(thread.getId());
      if (info == null) {
        info = bean.getThreadInfo(thread.getId());
      }
      final Serializable resource = dep.getDependsOn();
      results.add(new Dependency(new LocalThread(locality, info), resource));
    }
    return results;
  }
  @Override
  public void execute(OperationContext context, ModelNode operation)
      throws OperationFailedException {

    validator.validate(operation);
    ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
    try {
      long id = operation.require(PlatformMBeanConstants.ID).asLong();
      ThreadInfo info = null;
      if (operation.hasDefined(PlatformMBeanConstants.MAX_DEPTH)) {
        info = mbean.getThreadInfo(id, operation.require(PlatformMBeanConstants.MAX_DEPTH).asInt());
      } else {
        info = mbean.getThreadInfo(id);
      }

      final ModelNode result = context.getResult();
      if (info != null) {
        result.set(PlatformMBeanUtil.getDetypedThreadInfo(info, mbean.isThreadCpuTimeSupported()));
      }
    } catch (SecurityException e) {
      throw new OperationFailedException(new ModelNode().set(e.toString()));
    }

    context.completeStep();
  }
Ejemplo n.º 3
0
 @RequestMapping("/dumpThead")
 @ResponseBody
 public void doDumpThread(HttpServletRequest request, HttpServletResponse response) {
   try {
     String app = request.getParameter("app");
     String threadId = request.getParameter("threadId");
     ThreadMXBean tBean = JMConnManager.getThreadMBean(app);
     JSONObject data = new JSONObject();
     if (threadId != null) {
       Long id = Long.valueOf(threadId);
       ThreadInfo threadInfo = tBean.getThreadInfo(id, Integer.MAX_VALUE);
       data.put("info", threadInfo.toString());
     } else {
       ThreadInfo[] dumpAllThreads = tBean.dumpAllThreads(false, false);
       StringBuffer info = new StringBuffer();
       for (ThreadInfo threadInfo : dumpAllThreads) {
         info.append("\n").append(threadInfo);
       }
       data.put("info", info);
     }
     writeFile(request, response, data);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
  /**
   * Update the information about completed thread that ran for runtime in milliseconds
   *
   * <p>This method updates all of the key timing and tracking information in the factory so that
   * thread can be retired. After this call the factory shouldn't have a pointer to the thread any
   * longer
   *
   * @param thread the thread whose information we are updating
   */
  @Ensures({"getTotalTime() >= old(getTotalTime())"})
  public synchronized void threadIsDone(final Thread thread) {
    nThreadsAnalyzed++;

    if (DEBUG) logger.warn("UpdateThreadInfo called");

    final long threadID = thread.getId();
    final ThreadInfo info = bean.getThreadInfo(thread.getId());
    final long totalTimeNano = bean.getThreadCpuTime(threadID);
    final long userTimeNano = bean.getThreadUserTime(threadID);
    final long systemTimeNano = totalTimeNano - userTimeNano;
    final long userTimeInMilliseconds = nanoToMilli(userTimeNano);
    final long systemTimeInMilliseconds = nanoToMilli(systemTimeNano);

    if (info != null) {
      if (DEBUG)
        logger.warn(
            "Updating thread with user runtime "
                + userTimeInMilliseconds
                + " and system runtime "
                + systemTimeInMilliseconds
                + " of which blocked "
                + info.getBlockedTime()
                + " and waiting "
                + info.getWaitedTime());
      incTimes(State.BLOCKING, info.getBlockedTime());
      incTimes(State.WAITING, info.getWaitedTime());
      incTimes(State.USER_CPU, userTimeInMilliseconds);
      incTimes(State.WAITING_FOR_IO, systemTimeInMilliseconds);
    }
  }
Ejemplo n.º 5
0
 private void doThreadUpdates() {
   ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
   long threadIds[] = threadMXBean.getAllThreadIds();
   ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadIds, 0);
   int threadsNew = 0;
   int threadsRunnable = 0;
   int threadsBlocked = 0;
   int threadsWaiting = 0;
   int threadsTimedWaiting = 0;
   int threadsTerminated = 0;
   for (ThreadInfo threadInfo : threadInfos) {
     // threadInfo is null if the thread is not alive or doesn't exist
     if (threadInfo == null) continue;
     Thread.State state = threadInfo.getThreadState();
     if (state == NEW) {
       threadsNew++;
     } else if (state == RUNNABLE) {
       threadsRunnable++;
     } else if (state == BLOCKED) {
       threadsBlocked++;
     } else if (state == WAITING) {
       threadsWaiting++;
     } else if (state == TIMED_WAITING) {
       threadsTimedWaiting++;
     } else if (state == TERMINATED) {
       threadsTerminated++;
     }
   }
   metrics.setMetric("threadsNew", threadsNew);
   metrics.setMetric("threadsRunnable", threadsRunnable);
   metrics.setMetric("threadsBlocked", threadsBlocked);
   metrics.setMetric("threadsWaiting", threadsWaiting);
   metrics.setMetric("threadsTimedWaiting", threadsTimedWaiting);
   metrics.setMetric("threadsTerminated", threadsTerminated);
 }
 public ThreadInfo[] getDeadLocks() {
   final long[] ids = _tmx.findDeadlockedThreads();
   if (ids != null) {
     return _tmx.getThreadInfo(ids, true, true);
   } else {
     return null;
   }
 }
Ejemplo n.º 7
0
 @IgnoreJRERequirement
 public static ThreadInfo[] getThreadInfos() {
   ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
   return mbean.getThreadInfo(
       mbean.getAllThreadIds(),
       mbean.isObjectMonitorUsageSupported(),
       mbean.isSynchronizerUsageSupported());
 }
Ejemplo n.º 8
0
 /**
  * {@inheritDoc}
  *
  * @see com.wily.introscope.agent.trace.ITracer#ITracer_startTrace(int,
  *     com.wily.introscope.agent.trace.InvocationData)
  */
 @Override
 public void ITracer_startTrace(int tracerIndex, InvocationData data) {
   final int currentIndex = reentrancyIndex.get().incrementAndGet();
   final ThreadInfo ti = TMX.getThreadInfo(Thread.currentThread().getId());
   waitCountBaselines.get().put(currentIndex, ti.getWaitedCount());
   blockCountBaselines.get().put(currentIndex, ti.getBlockedCount());
   if (timesEnabled) {
     waitTimeBaselines.get().put(currentIndex, ti.getWaitedTime());
     blockTimeBaselines.get().put(currentIndex, ti.getBlockedTime());
   }
 }
Ejemplo n.º 9
0
  public String getThreadDump() {
    StringBuilder report = new StringBuilder();

    report.append("DATE : ").append(dateFormat.format(new Date())).append("\n");

    final ThreadInfo[] threadInfos =
        threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    for (ThreadInfo threadInfo : threadInfos) {
      report.append(threadInfo);
    }
    return report.toString();
  }
Ejemplo n.º 10
0
  public static void main(String[] argv) throws Exception {
    mbean = newPlatformMXBeanProxy(server, THREAD_MXBEAN_NAME, ThreadMXBean.class);

    if (!mbean.isSynchronizerUsageSupported()) {
      System.out.println("Monitoring of synchronizer usage not supported");
      return;
    }

    thread.setDaemon(true);
    thread.start();

    // wait until myThread acquires mutex and lock owner is set.
    while (!(mutex.isLocked() && mutex.getLockOwner() == thread)) {
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
    }

    long[] ids = new long[] {thread.getId()};

    // validate the local access
    ThreadInfo[] infos = getThreadMXBean().getThreadInfo(ids, true, true);
    if (infos.length != 1) {
      throw new RuntimeException(
          "Returned ThreadInfo[] of length=" + infos.length + ". Expected to be 1.");
    }
    thread.checkThreadInfo(infos[0]);

    // validate the remote access
    infos = mbean.getThreadInfo(ids, true, true);
    if (infos.length != 1) {
      throw new RuntimeException(
          "Returned ThreadInfo[] of length=" + infos.length + ". Expected to be 1.");
    }
    thread.checkThreadInfo(infos[0]);

    boolean found = false;
    infos = mbean.dumpAllThreads(true, true);
    for (ThreadInfo ti : infos) {
      if (ti.getThreadId() == thread.getId()) {
        thread.checkThreadInfo(ti);
        found = true;
      }
    }

    if (!found) {
      throw new RuntimeException("No ThreadInfo found for MyThread");
    }

    System.out.println("Test passed");
  }
Ejemplo n.º 11
0
  @Override
  public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
    SimpleOrderedMap<Object> system = new SimpleOrderedMap<Object>();
    rsp.add("system", system);

    ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();

    // Thread Count
    SimpleOrderedMap<Object> nl = new SimpleOrderedMap<Object>();
    nl.add("current", tmbean.getThreadCount());
    nl.add("peak", tmbean.getPeakThreadCount());
    nl.add("daemon", tmbean.getDaemonThreadCount());
    system.add("threadCount", nl);

    // Deadlocks
    ThreadInfo[] tinfos;
    long[] tids = tmbean.findMonitorDeadlockedThreads();
    if (tids != null) {
      tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
      NamedList<SimpleOrderedMap<Object>> lst = new NamedList<SimpleOrderedMap<Object>>();
      for (ThreadInfo ti : tinfos) {
        if (ti != null) {
          lst.add("thread", getThreadInfo(ti, tmbean));
        }
      }
      system.add("deadlocks", lst);
    }

    // Now show all the threads....
    tids = tmbean.getAllThreadIds();
    tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
    NamedList<SimpleOrderedMap<Object>> lst = new NamedList<SimpleOrderedMap<Object>>();
    for (ThreadInfo ti : tinfos) {
      if (ti != null) {
        lst.add("thread", getThreadInfo(ti, tmbean));
      }
    }
    system.add("threadDump", lst);
    rsp.setHttpCaching(false);
  }
Ejemplo n.º 12
0
  /** Constructor */
  public ThreadMeasurement() {
    threadsInfo = null;

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    threadIds = threadBean.getAllThreadIds();
    threadsInfo = threadBean.getThreadInfo(threadIds, THREAD_TRACE_DEPTH);
    threadCount = threadIds.length;

    threadNames = new String[threadCount];
    for (int i = 0; i < threadCount; i++) {
      if (threadsInfo[i] != null) threadNames[i] = threadsInfo[i].getThreadName();
    }
  } // Constructor end
Ejemplo n.º 13
0
 /**
  * Check if any threads are deadlocked. If any, print the thread dump for those threads.
  *
  * @return a deadlock message and the formatted thread dump of the deadlocked threads
  */
 private static String findDeadlock() {
   ThreadInfo[] tinfos = null;
   long[] ids = threadMXBean.findDeadlockedThreads();
   if (ids != null) {
     tinfos = threadMXBean.getThreadInfo(threadMXBean.findDeadlockedThreads(), true, true);
     if (tinfos != null) {
       StringBuilder sb = new StringBuilder("Deadlock found between the following threads:");
       sb.append(CRLF);
       sb.append(getThreadDump(tinfos));
       return sb.toString();
     }
   }
   return "";
 }
Ejemplo n.º 14
0
    @DwrPermission(admin = true)
    public ProcessResult getThreadInfo() {
      synchronized (threadInfos) {
        ProcessResult result = new ProcessResult();

        // All of the last thread ids. Ids are removed from this set as they are processed. If ids
        // remain,
        // it means the thread is gone and should be removed from the map.
        Set<Long> threadIds = new HashSet<>(threadInfos.keySet());

        ThreadInfo[] threads = tmxb.getThreadInfo(tmxb.getAllThreadIds(), Integer.MAX_VALUE);
        List<ThreadInfoBean> beans = new ArrayList<>();
        for (ThreadInfo thread : threads) {
          if (thread == null) continue;

          ThreadInfoBean bean = threadInfos.get(thread.getThreadId());
          if (bean == null) {
            bean = new ThreadInfoBean();
            bean.setId(thread.getThreadId());
            bean.setName(thread.getThreadName());
            threadInfos.put(bean.getId(), bean);
          } else threadIds.remove(bean.getId());

          bean.setCpuTime(tmxb.getThreadCpuTime(bean.getId()));
          bean.setState(thread.getThreadState().name());

          if (thread.getThreadState() == State.BLOCKED)
            bean.setState(
                bean.getState()
                    + " by '"
                    + thread.getLockOwnerName()
                    + "' ("
                    + thread.getLockOwnerId()
                    + ")");

          bean.setStackTrace(thread.getStackTrace());

          beans.add(bean);
        }

        // Remove unreferenced threads
        for (Long id : threadIds) threadInfos.remove(id);

        result.addData("threads", beans);

        return result;
      }
    }
Ejemplo n.º 15
0
  /** Count the number of threads that have a stack frame containing the given string */
  private static int countThreads(String search) {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();

    int count = 0;
    ThreadInfo[] infos = threadBean.getThreadInfo(threadBean.getAllThreadIds(), 20);
    for (ThreadInfo info : infos) {
      if (info == null) continue;
      for (StackTraceElement elem : info.getStackTrace()) {
        if (elem.getClassName().contains(search)) {
          count++;
          break;
        }
      }
    }
    return count;
  }
Ejemplo n.º 16
0
 /**
  * A total hack to see if there are any active Java3D threads running
  *
  * @return any java3d threads running
  */
 private static boolean anyJava3dThreadsActive() {
   ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
   long[] ids = threadBean.getAllThreadIds();
   for (int i = 0; i < ids.length; i++) {
     ThreadInfo info = threadBean.getThreadInfo(ids[i], Integer.MAX_VALUE);
     if (info == null) {
       continue;
     }
     if (info.getThreadState() != Thread.State.RUNNABLE) {
       continue;
     }
     if (info.getThreadName().indexOf("J3D") >= 0) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 17
0
  /**
   * Update the information about completed thread that ran for runtime in milliseconds
   *
   * <p>This method updates all of the key timing and tracking information in the factory so that
   * thread can be retired. After this call the factory shouldn't have a pointer to the thread any
   * longer
   *
   * @param thread
   * @param runtimeInMilliseconds
   */
  @Ensures({
    "activeThreads.size() < old(activeThreads.size())",
    "! activeThreads.contains(thread)",
    "getTotalTime() >= old(getTotalTime())",
    "countDownLatch.getCount() < old(countDownLatch.getCount())"
  })
  private synchronized void threadIsDone(final Thread thread, final long runtimeInMilliseconds) {
    if (DEBUG)
      logger.warn(
          "  Countdown "
              + countDownLatch.getCount()
              + " in thread "
              + Thread.currentThread().getName());
    if (DEBUG) logger.warn("UpdateThreadInfo called");

    final ThreadInfo info = bean.getThreadInfo(thread.getId());
    if (info != null) {
      if (DEBUG)
        logger.warn(
            "Updating thread total runtime "
                + runtimeInMilliseconds
                + " of which blocked "
                + info.getBlockedTime()
                + " and waiting "
                + info.getWaitedTime());
      incTimes(Thread.State.BLOCKED, info.getBlockedTime());
      incTimes(Thread.State.WAITING, info.getWaitedTime());
      incTimes(
          Thread.State.RUNNABLE,
          runtimeInMilliseconds - info.getWaitedTime() - info.getBlockedTime());
    }

    // remove the thread from the list of active activeThreads
    if (!activeThreads.remove(thread))
      throw new IllegalStateException("Thread " + thread + " not in list of active activeThreads");

    // one less thread is live for those blocking on all activeThreads to be complete
    countDownLatch.countDown();
    if (DEBUG)
      logger.warn(
          "  -> Countdown "
              + countDownLatch.getCount()
              + " in thread "
              + Thread.currentThread().getName());
  }
Ejemplo n.º 18
0
 /**
  * {@inheritDoc}
  *
  * @see com.wily.introscope.agent.trace.ITracer#ITracer_finishTrace(int,
  *     com.wily.introscope.agent.trace.InvocationData)
  */
 @Override
 public void ITracer_finishTrace(int tracerIndex, InvocationData data) {
   final int currentIndex = reentrancyIndex.get().decrementAndGet();
   try {
     final int priorIndex = currentIndex + 1;
     final ThreadInfo ti = TMX.getThreadInfo(Thread.currentThread().getId());
     long newWaitCount = ti.getWaitedCount();
     long newBlockCount = ti.getBlockedCount();
     long priorWaitCount = waitCountBaselines.get().remove(priorIndex);
     long priorBlockCount = blockCountBaselines.get().remove(priorIndex);
     if (priorWaitCount != NO_ENTRY && !waitCountAcc.IDataAccumulator_isShutOff()) {
       waitCountAcc.ILongAggregatingDataAccumulator_recordDataPoint(newWaitCount - priorWaitCount);
     }
     if (priorBlockCount != NO_ENTRY && !blockCountAcc.IDataAccumulator_isShutOff()) {
       blockCountAcc.ILongAggregatingDataAccumulator_recordDataPoint(
           newBlockCount - priorBlockCount);
     }
     if (timesEnabled) {
       long newWaitTime = ti.getWaitedTime();
       long newBlockTime = ti.getBlockedTime();
       long priorWaitTime = waitTimeBaselines.get().remove(priorIndex);
       long priorBlockTime = blockTimeBaselines.get().remove(priorIndex);
       if (priorWaitTime != NO_ENTRY && !waitTimeAcc.IDataAccumulator_isShutOff()) {
         waitTimeAcc.ILongAggregatingDataAccumulator_recordDataPoint(newWaitTime - priorWaitTime);
       }
       if (priorBlockTime != NO_ENTRY && !blockTimeAcc.IDataAccumulator_isShutOff()) {
         blockTimeAcc.ILongAggregatingDataAccumulator_recordDataPoint(
             newBlockTime - priorBlockTime);
       }
     }
   } finally {
     if (currentIndex < 1) {
       waitCountBaselines.remove();
       blockCountBaselines.remove();
       if (timesEnabled) {
         waitTimeBaselines.remove();
         blockTimeBaselines.remove();
       }
     }
     if (currentIndex < 0) {
       log.warn("Reentrancy Index Underrun:", currentIndex);
       reentrancyIndex.get().set(0);
     }
   }
 }
Ejemplo n.º 19
0
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    if (args.length != 4) {
      System.err.println("Please provide process id zabbix-host zabbix-port host-guid");
      System.exit(-1);
    }
    String processPid = args[0];
    String zabbixHost = args[1];
    String zabbixPort = args[2];
    String hostGuid = args[3];

    VirtualMachine vm = VirtualMachine.attach(processPid);
    String connectorAddr =
        vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
    if (connectorAddr == null) {
      String agent =
          vm.getSystemProperties().getProperty("java.home")
              + File.separator
              + "lib"
              + File.separator
              + "management-agent.jar";
      vm.loadAgent(agent);
      connectorAddr =
          vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
    }
    JMXServiceURL serviceURL = new JMXServiceURL(connectorAddr);
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
    MBeanServerConnection mbsc = connector.getMBeanServerConnection();
    ObjectName objName = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
    Set<ObjectName> mbeans = mbsc.queryNames(objName, null);
    for (ObjectName name : mbeans) {
      ThreadMXBean threadBean;
      threadBean =
          ManagementFactory.newPlatformMXBeanProxy(mbsc, name.toString(), ThreadMXBean.class);
      long threadIds[] = threadBean.getAllThreadIds();
      for (long threadId : threadIds) {
        ThreadInfo threadInfo = threadBean.getThreadInfo(threadId);
        System.out.println(threadInfo.getThreadName() + " / " + threadInfo.getThreadState());
      }
    }
  }
Ejemplo n.º 20
0
  /**
   * Return current (raw) list of thread info objects wrapped in ThreadRankInfo type.
   *
   * @return list of threads
   */
  protected List<ThreadRankInfo> rawList() {
    // Platform MBean Server startup might be suspended (eg. for JBoss AS);
    if (threadMXBean == null) {
      if (mBeanServerRegistry.lookup("java") != null) {
        threadMXBean = ManagementFactory.getThreadMXBean();
      } else {
        return new ArrayList<ThreadRankInfo>(1);
      }
    }

    ThreadInfo[] ati = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds());
    List<ThreadRankInfo> lst = new ArrayList<ThreadRankInfo>(ati.length);

    for (ThreadInfo ti : ati) {
      long tid = ti.getThreadId();
      long cpuTime = threadMXBean.getThreadCpuTime(tid);
      lst.add(new ThreadRankInfo(tid, ti.getThreadName(), cpuTime, ti.getBlockedTime()));
    }

    return lst;
  }
Ejemplo n.º 21
0
 @Override
 public void runInternal() {
   if (transaction.isCompleted()) {
     // there is a small window between trace completion and cancellation of this
     // command,
     // plus, should a stop-the-world gc occur in this small window, even two command
     // executions can fire one right after the other in the small window (assuming the
     // first
     // didn't throw an exception which it does now), since this command is scheduled
     // using
     // ScheduledExecutorService.scheduleWithFixedDelay()
     throw new TerminateSubsequentExecutionsException();
   }
   ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
   ThreadInfo threadInfo =
       threadBean.getThreadInfo(transaction.getThreadId(), Integer.MAX_VALUE);
   transaction.captureStackTrace(
       threadInfo,
       configService.getAdvancedConfig().maxStackTraceSamplesPerTransaction(),
       configService.getAdvancedConfig().timerWrapperMethods());
 }
Ejemplo n.º 22
0
 public static String threadDump() {
   final StringBuilder dump = new StringBuilder();
   final java.lang.management.ThreadMXBean threadMXBean =
       java.lang.management.ManagementFactory.getThreadMXBean();
   final java.lang.management.ThreadInfo[] threadInfos =
       threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
   for (java.lang.management.ThreadInfo threadInfo : threadInfos) {
     dump.append('"');
     dump.append(threadInfo.getThreadName());
     dump.append("\" ");
     final Thread.State state = threadInfo.getThreadState();
     dump.append("\n   java.lang.Thread.State: ");
     dump.append(state);
     final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
     for (final StackTraceElement stackTraceElement : stackTraceElements) {
       dump.append("\n        at ");
       dump.append(stackTraceElement);
     }
     dump.append("\n\n");
   }
   return dump.toString();
 }
Ejemplo n.º 23
0
  private static void dumpThreadInfo(
      StringBuilder threadDump, boolean withLocks, ThreadMXBean threadMx) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String timestamp = dateFormat.format(new Date());
    threadDump.append(timestamp);
    threadDump.append("\nFull thread dump ");
    threadDump.append("\n");

    if (withLocks) {
      ThreadInfo[] threadInfos = threadMx.dumpAllThreads(true, true);
      for (ThreadInfo threadInfo : threadInfos) {
        printThreadInfo(threadInfo, threadDump);
        LockInfo[] syncs = threadInfo.getLockedSynchronizers();
        printLockInfo(syncs, threadDump);
      }
      threadDump.append("\n");
    } else {
      long[] threadIds = threadMx.getAllThreadIds();
      ThreadInfo[] threadInfos = threadMx.getThreadInfo(threadIds, Integer.MAX_VALUE);
      for (ThreadInfo threadInfo : threadInfos) printThreadInfo(threadInfo, threadDump);
    }
  }
Ejemplo n.º 24
0
  @Override
  public void run() {

    boolean dead = false;
    while (!dead && !interrupted()) {
      try {
        Thread.sleep(10000);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        dead = true;
      }
      ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
      long[] ids = tmx.findDeadlockedThreads();
      if (ids != null) {
        Spout.getLogger().info("Checking for deadlocks");
        ThreadInfo[] infos = tmx.getThreadInfo(ids, true, true);
        Spout.getLogger().severe("The following threads are deadlocked:");
        for (ThreadInfo ti : infos) {
          Spout.getLogger().severe(ti.toString());
        }
      }
    }
  }
Ejemplo n.º 25
0
  /**
   * @param t Thread
   * @param pw PrintWriter
   */
  public static void reportThread(Thread t, PrintWriter pw) {
    ThreadMXBean tmxb = ManagementFactory.getThreadMXBean();
    ThreadInfo info = tmxb.getThreadInfo(t.getId());
    pw.print("Java Thread State: ");
    pw.println(info.getThreadState());
    pw.print("Blocked/Waiting On: ");
    if (info.getLockOwnerId() >= 0) {
      pw.print(info.getLockName());
      pw.print(" which is owned by ");
      pw.print(info.getLockOwnerName());
      pw.print("(");
      pw.print(info.getLockOwnerId());
      pw.println(")");
    } else {
      pw.println("NONE");
    }

    StackTraceElement[] ste = t.getStackTrace();
    for (int i = 0; i < ste.length; i++) {
      pw.print("    ");
      pw.print(ste[i].toString());
      pw.println();
    }
  }
Ejemplo n.º 26
0
 @RequestMapping("/deadlockCheck")
 @ResponseBody
 public JSONObject doDeadlockCheck(HttpServletRequest request) {
   try {
     String app = request.getParameter("app");
     ThreadMXBean tBean = JMConnManager.getThreadMBean(app);
     JSONObject json = new JSONObject();
     long[] dTh = tBean.findDeadlockedThreads();
     if (dTh != null) {
       ThreadInfo[] threadInfo = tBean.getThreadInfo(dTh, Integer.MAX_VALUE);
       StringBuffer sb = new StringBuffer();
       for (ThreadInfo info : threadInfo) {
         sb.append("\n").append(info);
       }
       json.put("hasdeadlock", true);
       json.put("info", sb);
       return json;
     }
     json.put("hasdeadlock", false);
     return json;
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Ejemplo n.º 27
0
  @Override
  public void doTest() throws Throwable {
    ThreadMXBean tbean;
    tbean = ManagementFactory.getThreadMXBean();

    int nonDaemonThreadCountA = tbean.getThreadCount() - tbean.getDaemonThreadCount();
    int daemonThreadCountA = tbean.getDaemonThreadCount();
    long[] listA = tbean.getAllThreadIds();
    for (int loopNumber = 0; loopNumber < 4; loopNumber++) {
      cacheManager =
          new CacheManager(
              DaemonThreadsWriteBehindTestClient.class.getResourceAsStream("/ehcache-config.xml"));
      int daemonThreadCountB = tbean.getDaemonThreadCount();
      Assert.assertTrue(daemonThreadCountA < daemonThreadCountB);
      Cache cache = cacheManager.getCache("test");
      cache.registerCacheWriter(new WriteBehindCacheWriter(this));
      Assert.assertNotNull(cache.getWriterManager());
      Assert.assertTrue(cache.getWriterManager() instanceof WriteBehindManager);
      for (int i = 0; i < 10; i++) {
        cache.putWithWriter(new Element(i, i));
      }
      while (getWriteCount() < 10) {
        Thread.sleep(200);
      }
      resetWriteCount();
      cacheManager.shutdown();
      System.out.println("done with iteration " + loopNumber);
    }
    TimeUnit.MINUTES.sleep(1L);
    long[] listC = tbean.getAllThreadIds();
    int daemonThreadCountC = tbean.getDaemonThreadCount();
    int nonDaemonThreadCountC = tbean.getThreadCount() - tbean.getDaemonThreadCount();
    List<Long> listIntA = new ArrayList<Long>();
    for (long listAItrator : listA) {
      listIntA.add(new Long(listAItrator));
    }
    List<Long> listIntC = new ArrayList<Long>();
    for (long listAItrator : listC) {
      listIntC.add(new Long(listAItrator));
    }
    listIntC.removeAll(listIntA);
    Set<String> knownThreads = getKnownThreads();
    int skipThreadCount = 0;
    StringBuffer threadsInfo = new StringBuffer();
    System.out.println(
        "\n\n" + listIntC.size() + " Start Printing Stack Trace\n--------------------");
    for (int i = 0; i < listIntC.size(); i++) {
      ThreadInfo tinfo = tbean.getThreadInfo(listIntC.get(i));
      if (knownThreads.contains(tinfo.getThreadName().trim())) {
        ++skipThreadCount;
        continue;
      }
      String info = "Thread name: " + tinfo.getThreadName() + " | " + tinfo.getThreadId();
      threadsInfo.append(info);
      for (StackTraceElement e : tinfo.getStackTrace()) {
        threadsInfo.append(e + "\n\n");
      }
    }
    System.out.println(threadsInfo + "\n\n-----------------------\n\n");
    Assert.assertEquals(
        threadsInfo.toString(), daemonThreadCountA, daemonThreadCountC - skipThreadCount);
    Assert.assertEquals(nonDaemonThreadCountA, nonDaemonThreadCountC);
  }
Ejemplo n.º 28
0
  @Test
  public void verifyThreadNames() throws Exception {

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    Set<String> preNodeStartThreadNames = Sets.newHashSet();
    for (long l : threadBean.getAllThreadIds()) {
      ThreadInfo threadInfo = threadBean.getThreadInfo(l);
      if (threadInfo != null) {
        preNodeStartThreadNames.add(threadInfo.getThreadName());
      }
    }
    logger.info("pre node threads are {}", preNodeStartThreadNames);
    String node = internalCluster().startNode();
    logger.info("do some indexing, flushing, optimize, and searches");
    int numDocs = randomIntBetween(2, 100);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; ++i) {
      builders[i] =
          client()
              .prepareIndex("idx", "type")
              .setSource(
                  jsonBuilder()
                      .startObject()
                      .field("str_value", "s" + i)
                      .field("str_values", new String[] {"s" + (i * 2), "s" + (i * 2 + 1)})
                      .field("l_value", i)
                      .field("l_values", new int[] {i * 2, i * 2 + 1})
                      .field("d_value", i)
                      .field("d_values", new double[] {i * 2, i * 2 + 1})
                      .endObject());
    }
    indexRandom(true, builders);
    int numSearches = randomIntBetween(2, 100);
    for (int i = 0; i < numSearches; i++) {
      assertNoFailures(
          client()
              .prepareSearch("idx")
              .setQuery(QueryBuilders.termQuery("str_value", "s" + i))
              .get());
      assertNoFailures(
          client().prepareSearch("idx").setQuery(QueryBuilders.termQuery("l_value", i)).get());
    }
    Set<String> threadNames = Sets.newHashSet();
    for (long l : threadBean.getAllThreadIds()) {
      ThreadInfo threadInfo = threadBean.getThreadInfo(l);
      if (threadInfo != null) {
        threadNames.add(threadInfo.getThreadName());
      }
    }
    logger.info("post node threads are {}", threadNames);
    threadNames.removeAll(preNodeStartThreadNames);
    logger.info("post node *new* threads are {}", threadNames);
    for (String threadName : threadNames) {
      // ignore some shared threads we know that are created within the same VM, like the shared
      // discovery one
      // or the ones that are occasionally come up from ElasticsearchSingleNodeTest
      if (threadName.contains("[" + MulticastChannel.SHARED_CHANNEL_NAME + "]")
          || threadName.contains("[" + ElasticsearchSingleNodeTest.nodeName() + "]")
          || threadName.contains("Keep-Alive-Timer")) {
        continue;
      }
      String nodePrefix =
          "("
              + Pattern.quote(InternalTestCluster.TRANSPORT_CLIENT_PREFIX)
              + ")?("
              + Pattern.quote(ElasticsearchIntegrationTest.SUITE_CLUSTER_NODE_PREFIX)
              + "|"
              + Pattern.quote(ElasticsearchIntegrationTest.TEST_CLUSTER_NODE_PREFIX)
              + "|"
              + Pattern.quote(TribeTests.SECOND_CLUSTER_NODE_PREFIX)
              + ")";
      assertThat(threadName, RegexMatcher.matches("\\[" + nodePrefix + "\\d+\\]"));
    }
  }
Ejemplo n.º 29
0
 public static ThreadInfo getThreadInfo(Thread t) {
   long tid = t.getId();
   return threadBean.getThreadInfo(tid, STACK_DEPTH);
 }
 /** Get an object suitable for querying the findDependencies method for a given thread. */
 public static ThreadReference getThreadReference(String locality, Thread thread) {
   ThreadMXBean bean = ManagementFactory.getThreadMXBean();
   ThreadInfo info = bean.getThreadInfo(thread.getId(), Integer.MAX_VALUE);
   return new LocalThread(locality, info);
 }