@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;
      }
    }
  /**
   * Print all of the thread's information and stack traces.
   *
   * @param sb
   * @param info
   * @param indent
   */
  public static void appendThreadInfo(StringBuilder sb, ThreadInfo info, String indent) {
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();

    if (info == null) {
      sb.append(indent).append("Inactive (perhaps exited while monitoring was done)\n");
      return;
    }
    String taskName = getTaskName(info.getThreadId(), info.getThreadName());
    sb.append(indent).append("Thread ").append(taskName).append(":\n");

    Thread.State state = info.getThreadState();
    sb.append(indent).append("  State: ").append(state).append("\n");
    sb.append(indent).append("  Blocked count: ").append(info.getBlockedCount()).append("\n");
    sb.append(indent).append("  Waited count: ").append(info.getWaitedCount()).append("\n");
    if (contention) {
      sb.append(indent).append("  Blocked time: " + info.getBlockedTime()).append("\n");
      sb.append(indent).append("  Waited time: " + info.getWaitedTime()).append("\n");
    }
    if (state == Thread.State.WAITING) {
      sb.append(indent).append("  Waiting on ").append(info.getLockName()).append("\n");
    } else if (state == Thread.State.BLOCKED) {
      sb.append(indent).append("  Blocked on ").append(info.getLockName()).append("\n");
      sb.append(indent)
          .append("  Blocked by ")
          .append(getTaskName(info.getLockOwnerId(), info.getLockOwnerName()))
          .append("\n");
    }
    sb.append(indent).append("  Stack:").append("\n");
    for (StackTraceElement frame : info.getStackTrace()) {
      sb.append(indent).append("    ").append(frame.toString()).append("\n");
    }
  }
 private Thread findMatchingThread(ThreadInfo inf) {
   for (Thread thread : Thread.getAllStackTraces().keySet()) {
     if (thread.getId() == inf.getThreadId()) {
       return thread;
     }
   }
   throw new IllegalStateException("Deadlocked Thread not found");
 }
Exemple #4
0
  public static void main(String[] args) {
    // 获取Java线程管理MXBean
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
    for (ThreadInfo threadInfo : threadInfos) {
      System.out.println("[" + threadInfo.getThreadId() + " ] " + threadInfo.getThreadName());
    }
  }
Exemple #5
0
  /**
   * Formats the thread dump header for one thread.
   *
   * @param ti the ThreadInfo describing the thread
   * @return the formatted thread dump header
   */
  private static String getThreadDumpHeader(ThreadInfo ti) {
    StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"");
    sb.append(" Id=" + ti.getThreadId());
    sb.append(" cpu=" + threadMXBean.getThreadCpuTime(ti.getThreadId()) + " ns");
    sb.append(" usr="******" ns");
    sb.append(" blocked " + ti.getBlockedCount() + " for " + ti.getBlockedTime() + " ms");
    sb.append(" waited " + ti.getWaitedCount() + " for " + ti.getWaitedTime() + " ms");

    if (ti.isSuspended()) {
      sb.append(" (suspended)");
    }
    if (ti.isInNative()) {
      sb.append(" (running in native)");
    }
    sb.append(CRLF);
    sb.append(INDENT3 + "java.lang.Thread.State: " + ti.getThreadState());
    sb.append(CRLF);
    return sb.toString();
  }
  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");
  }
Exemple #7
0
  public static CharSequence getThreadStats(
      boolean lockedMonitors, boolean lockedSynchronizers, boolean stackTrace) {
    StringBuilder list = new StringBuilder();

    int threadCount = threadMXbean.getThreadCount();
    int daemonCount = threadMXbean.getThreadCount();
    int nonDaemonCount = threadCount - daemonCount;
    int peakCount = threadMXbean.getPeakThreadCount();
    long totalCount = threadMXbean.getTotalStartedThreadCount();

    list.append("Live: .................... ").append(threadCount).append(" threads").append("\n");
    list.append("     Non-Daemon: ......... ")
        .append(nonDaemonCount)
        .append(" threads")
        .append("\n");
    list.append("     Daemon: ............. ").append(daemonCount).append(" threads").append("\n");
    list.append("Peak: .................... ").append(peakCount).append(" threads").append("\n");
    list.append("Total started: ........... ").append(totalCount).append(" threads").append("\n");
    list.append("=================================================").append("\n");

    for (ThreadInfo info : threadMXbean.dumpAllThreads(lockedMonitors, lockedSynchronizers)) {
      list.append("Thread #")
          .append(info.getThreadId())
          .append(" (")
          .append(info.getThreadName())
          .append(")")
          .append("\n");
      list.append("=================================================\n");
      list.append("\tgetThreadState: ...... ").append(info.getThreadState()).append("\n");
      list.append("\tgetWaitedTime: ....... ").append(info.getWaitedTime()).append("\n");
      list.append("\tgetBlockedTime: ...... ").append(info.getBlockedTime()).append("\n");
      for (MonitorInfo monitorInfo : info.getLockedMonitors()) {
        list.append("\tLocked monitor: ....... ").append(monitorInfo).append("\n");
        list.append("\t\t[")
            .append(monitorInfo.getLockedStackDepth())
            .append(".]: at ")
            .append(monitorInfo.getLockedStackFrame())
            .append("\n");
      }

      for (LockInfo lockInfo : info.getLockedSynchronizers()) {
        list.append("\tLocked synchronizer: ...").append(lockInfo).append("\n");
      }
      if (stackTrace) {
        list.append("\tgetStackTace: ..........\n");
        for (StackTraceElement trace : info.getStackTrace())
          list.append("\t\tat ").append(trace).append("\n");
      }
      list.append("=================================================\n");
    }

    return list;
  }
Exemple #8
0
  @Before
  public void setUp() throws Exception {
    final StackTraceElement rLine1 = new StackTraceElement("Blah", "blee", "Blah.java", 100);

    when(runnable.getThreadName()).thenReturn("runnable");
    when(runnable.getThreadId()).thenReturn(100L);
    when(runnable.getThreadState()).thenReturn(Thread.State.RUNNABLE);
    when(runnable.getStackTrace()).thenReturn(new StackTraceElement[] {rLine1});
    when(runnable.getLockedMonitors()).thenReturn(new MonitorInfo[] {});
    when(runnable.getLockedSynchronizers()).thenReturn(new LockInfo[] {});

    when(threadMXBean.dumpAllThreads(true, true)).thenReturn(new ThreadInfo[] {runnable});
  }
Exemple #9
0
  private static void printThreadInfo(CompositeData cd) {
    ThreadInfo info = ThreadInfo.from(cd);
    if (info == null) {
      throw new RuntimeException("TEST FAILED: " + " Null ThreadInfo");
    }

    System.out.print(info.getThreadName());
    System.out.print(" id=" + info.getThreadId());
    System.out.println(" " + info.getThreadState());

    for (StackTraceElement s : info.getStackTrace()) {
      System.out.println(s);
    }
  }
Exemple #10
0
  @RequestMapping("/loadThreadInfo")
  @ResponseBody
  public JSONObject doLoadThreadInfo(HttpServletRequest request) {
    try {
      String app = request.getParameter("app");
      ThreadMXBean tBean = JMConnManager.getThreadMBean(app);
      ThreadInfo[] allThreads = tBean.dumpAllThreads(false, false);

      JSONObject root = new JSONObject();
      JSONArray detail = new JSONArray();
      HashMap<State, Integer> state = new HashMap<Thread.State, Integer>();
      for (ThreadInfo info : allThreads) {
        JSONObject th = new JSONObject();
        long threadId = info.getThreadId();
        long cpu = tBean.getThreadCpuTime(threadId);
        State tState = info.getThreadState();

        th.put("id", threadId);
        th.put("state", tState);
        th.put("name", info.getThreadName());
        th.put("cpu", TimeUnit.NANOSECONDS.toMillis(cpu));
        detail.add(th);

        Integer vl = state.get(tState);
        if (vl == null) {
          state.put(tState, 0);
        } else {
          state.put(tState, vl + 1);
        }
      }

      root.put("state", state);
      root.put("detail", detail);
      root.put("total", tBean.getThreadCount());
      root.put("time", System.currentTimeMillis());
      root.put("deamon", tBean.getDaemonThreadCount());

      return root;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  protected String generateThreadDump() {
    StringBuilder dump = new StringBuilder();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    long currentThreadId = Thread.currentThread().getId();
    for (ThreadInfo threadInfo : threadInfos) {
      long threadId = threadInfo.getThreadId();
      if (threadId == currentThreadId) {
        continue;
      }
      dump.append('"');
      dump.append(threadInfo.getThreadName());
      dump.append("\" ");

      Thread.State state = threadInfo.getThreadState();
      dump.append("\n\tjava.lang.Thread.State: ");
      dump.append(state);
      if (threadInfo.getLockName() != null) {
        dump.append(", on lock=").append(threadInfo.getLockName());
      }
      if (threadInfo.getLockOwnerName() != null) {
        dump.append(", owned by ").append(threadInfo.getLockOwnerName());
        dump.append(", id=").append(threadInfo.getLockOwnerId());
      }
      if (THREAD_CPU_TIME_INFO_AVAILABLE) {
        dump.append(", cpu=").append(threadMXBean.getThreadCpuTime(threadId)).append(" nsecs");
        dump.append(", usr="******" nsecs");
      }
      if (THREAD_CONTENTION_INFO_AVAILABLE) {
        dump.append(", blocked=").append(threadInfo.getBlockedTime()).append(" msecs");
        dump.append(", waited=").append(threadInfo.getWaitedTime()).append(" msecs");
      }
      StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
      for (StackTraceElement stackTraceElement : stackTraceElements) {
        dump.append("\n\t\tat ");
        dump.append(stackTraceElement);
      }
      dump.append("\n\n");
    }
    return dump.toString();
  }
  /**
   * Collect all of the dependencies that exist between threads in this VM, using java management
   * beans and the {@link DependencyMonitor}.
   *
   * <p>Threads may depend on locks, or on other resources that are tracked by the {@link
   * DependencyMonitor}.
   *
   * @arg locality a name tag to stick on entities to help associate them with this JVM and
   *     distinguish them from entities from other jvms
   * @return All of the dependencies between threads and locks or other resources on this VM.
   */
  public static Set<Dependency> collectAllDependencies(Serializable locality) {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] infos = bean.dumpAllThreads(true, true);

    Set<Dependency> results = new HashSet<Dependency>();

    Map<Long, ThreadInfo> threadInfos = new HashMap<Long, ThreadInfo>();
    for (ThreadInfo info : infos) {
      // This can happen if the thread died.
      if (info == null) {
        continue;
      }

      for (LockInfo monitor : info.getLockedMonitors()) {
        Dependency dependency =
            new Dependency(new LocalLockInfo(locality, monitor), new LocalThread(locality, info));
        results.add(dependency);
      }

      for (LockInfo sync : info.getLockedSynchronizers()) {
        Dependency dependency =
            new Dependency(new LocalLockInfo(locality, sync), new LocalThread(locality, info));
        results.add(dependency);
      }

      LockInfo waitingFor = info.getLockInfo();
      if (waitingFor != null) {
        Dependency dependency =
            new Dependency(
                new LocalThread(locality, info), new LocalLockInfo(locality, waitingFor));
        results.add(dependency);
      }

      threadInfos.put(info.getThreadId(), info);
    }

    Set<Dependency> monitoredDependencies =
        collectFromDependencyMonitor(bean, locality, threadInfos);
    results.addAll(monitoredDependencies);
    return results;
  }
Exemple #13
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;
  }
Exemple #14
0
  private static SimpleOrderedMap<Object> getThreadInfo(ThreadInfo ti, ThreadMXBean tmbean) {
    SimpleOrderedMap<Object> info = new SimpleOrderedMap<Object>();
    long tid = ti.getThreadId();

    info.add("id", tid);
    info.add("name", ti.getThreadName());
    info.add("state", ti.getThreadState().toString());

    if (ti.getLockName() != null) {
      info.add("lock", ti.getLockName());
    }
    if (ti.isSuspended()) {
      info.add("suspended", true);
    }
    if (ti.isInNative()) {
      info.add("native", true);
    }

    if (tmbean.isThreadCpuTimeSupported()) {
      info.add("cpuTime", formatNanos(tmbean.getThreadCpuTime(tid)));
      info.add("userTime", formatNanos(tmbean.getThreadUserTime(tid)));
    }

    if (ti.getLockOwnerName() != null) {
      SimpleOrderedMap<Object> owner = new SimpleOrderedMap<Object>();
      owner.add("name", ti.getLockOwnerName());
      owner.add("id", ti.getLockOwnerId());
    }

    // Add the stack trace
    int i = 0;
    String[] trace = new String[ti.getStackTrace().length];
    for (StackTraceElement ste : ti.getStackTrace()) {
      trace[i++] = ste.toString();
    }
    info.add("stackTrace", trace);
    return info;
  }
Exemple #15
0
  private static void printThread(ThreadInfo threadInfo, StringBuilder threadDump) {
    StringBuilder sb =
        new StringBuilder(
            "\""
                + threadInfo.getThreadName()
                + "\""
                + " nid="
                + threadInfo.getThreadId()
                + " state="
                + threadInfo.getThreadState());
    if (threadInfo.getLockName() != null && threadInfo.getThreadState() != Thread.State.BLOCKED) {
      String[] lockInfo = threadInfo.getLockName().split("@");
      sb.append("\n" + INDENT + "- waiting on <0x" + lockInfo[1] + "> (a " + lockInfo[0] + ")");
      sb.append("\n" + INDENT + "- locked <0x" + lockInfo[1] + "> (a " + lockInfo[0] + ")");
    } else if (threadInfo.getLockName() != null
        && threadInfo.getThreadState() == Thread.State.BLOCKED) {
      String[] lockInfo = threadInfo.getLockName().split("@");
      sb.append(
          "\n" + INDENT + "- waiting to lock <0x" + lockInfo[1] + "> (a " + lockInfo[0] + ")");
    }
    if (threadInfo.isSuspended()) sb.append(" (suspended)");

    if (threadInfo.isInNative()) sb.append(" (running in native)");

    threadDump.append(sb.toString());
    threadDump.append("\n");
    if (threadInfo.getLockOwnerName() != null) {
      threadDump.append(
          INDENT
              + " owned by "
              + threadInfo.getLockOwnerName()
              + " id="
              + threadInfo.getLockOwnerId());
      threadDump.append("\n");
    }
  }
Exemple #16
0
 /**
  * スレッドの状態をStringBufferにして返します。<br>
  *
  * @param info {@link ThreadInfo}オブジェクト
  * @return スレッドの状態をStringBufferにして返したもの。
  */
 private static StringBuilder getThreadInfoBuffer(ThreadInfo info) {
   StringBuilder sb =
       new StringBuilder(
           "\""
               + info.getThreadName()
               + "\""
               + " Id="
               + info.getThreadId()
               + " "
               + info.getThreadState());
   if (info.getLockName() != null) {
     sb.append(" on " + info.getLockName());
   }
   if (info.getLockOwnerName() != null) {
     sb.append(" owned by \"" + info.getLockOwnerName() + "\" Id=" + info.getLockOwnerId());
   }
   if (info.isSuspended()) {
     sb.append(" (suspended)");
   }
   if (info.isInNative()) {
     sb.append(" (in native)");
   }
   return sb;
 }
Exemple #17
0
  // ThreadInfo.toString() truncates the stack trace by first 8, so needed my own version
  @IgnoreJRERequirement
  public static String dumpThreadInfo(ThreadInfo ti) {
    StringBuilder sb =
        new StringBuilder(
            "\""
                + ti.getThreadName()
                + "\""
                + " Id="
                + ti.getThreadId()
                + " "
                + ti.getThreadState());
    if (ti.getLockName() != null) {
      sb.append(" on " + ti.getLockName());
    }
    if (ti.getLockOwnerName() != null) {
      sb.append(" owned by \"" + ti.getLockOwnerName() + "\" Id=" + ti.getLockOwnerId());
    }
    if (ti.isSuspended()) {
      sb.append(" (suspended)");
    }
    if (ti.isInNative()) {
      sb.append(" (in native)");
    }
    sb.append('\n');
    StackTraceElement[] stackTrace = ti.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
      StackTraceElement ste = stackTrace[i];
      sb.append("\tat " + ste.toString());
      sb.append('\n');
      if (i == 0 && ti.getLockInfo() != null) {
        Thread.State ts = ti.getThreadState();
        switch (ts) {
          case BLOCKED:
            sb.append("\t-  blocked on " + ti.getLockInfo());
            sb.append('\n');
            break;
          case WAITING:
            sb.append("\t-  waiting on " + ti.getLockInfo());
            sb.append('\n');
            break;
          case TIMED_WAITING:
            sb.append("\t-  waiting on " + ti.getLockInfo());
            sb.append('\n');
            break;
          default:
        }
      }

      for (MonitorInfo mi : ti.getLockedMonitors()) {
        if (mi.getLockedStackDepth() == i) {
          sb.append("\t-  locked " + mi);
          sb.append('\n');
        }
      }
    }

    LockInfo[] locks = ti.getLockedSynchronizers();
    if (locks.length > 0) {
      sb.append("\n\tNumber of locked synchronizers = " + locks.length);
      sb.append('\n');
      for (LockInfo li : locks) {
        sb.append("\t- " + li);
        sb.append('\n');
      }
    }
    sb.append('\n');
    return sb.toString();
  }
  private void printThread() {
    long now = System.currentTimeMillis();

    long diffLastThreadDump = now - lastThreadDump;
    logger.info("diffLastThreadDump:" + diffLastThreadDump);
    if (diffLastThreadDump > 60000) {
      logger.info("had not sent all threads for a while.. will do so now");
      lastThreadDump = now;

      try {
        ThreadMXBean t = ManagementFactory.getThreadMXBean();
        long threads[] = t.getAllThreadIds();
        ThreadInfo[] tinfo = t.getThreadInfo(threads, 40);

        StringBuilder sb = new StringBuilder("All Threads");

        for (int i = 0; i < tinfo.length; i++) {
          ThreadInfo e = tinfo[i];

          try {
            StackTraceElement[] el = e.getStackTrace();
            sb.append(
                "\n\n"
                    + e.getThreadName()
                    + "\n"
                    + " "
                    + " Thread id = "
                    + e.getThreadId()
                    + " "
                    + e.getThreadState());
            if (e.getThreadState().equals(State.BLOCKED)) {
              sb.append(
                  "\n\nBlocked info: "
                      + e.getBlockedCount()
                      + ":"
                      + e.getBlockedTime()
                      + ":"
                      + e.getLockName()
                      + ":"
                      + e.getLockOwnerId()
                      + ":"
                      + e.getLockOwnerName()
                      + "\n"
                      + " "
                      + " Thread id = "
                      + e.getThreadId()
                      + " "
                      + e.getThreadState());

              ThreadInfo eBlockedThread = t.getThreadInfo(e.getLockOwnerId(), 40);
              StackTraceElement[] elBlockedThread = eBlockedThread.getStackTrace();
              sb.append(
                  "\n\n    "
                      + e.getThreadName()
                      + "\n"
                      + " "
                      + " Thread id = "
                      + eBlockedThread.getThreadId()
                      + " "
                      + eBlockedThread.getThreadState());

              if (elBlockedThread == null || elBlockedThread.length == 0) {
                sb.append("        no stack trace available");
              } else {
                for (int n = 0; n < elBlockedThread.length; n++) {
                  if (n != 0) sb.append("\n");

                  StackTraceElement frame = elBlockedThread[n];

                  if (frame == null) {
                    sb.append("        null stack frame");
                    continue;
                  }

                  sb.append("        ");
                  sb.append(frame.toString());
                }
              }
            }

            if (el == null || el.length == 0) {
              sb.append("    no stack trace available");
              continue;
            }

            for (int n = 0; n < el.length; n++) {
              if (n != 0) sb.append("\n");

              StackTraceElement frame = el[n];

              if (frame == null) {
                sb.append("    null stack frame");
                continue;
              }

              sb.append("    ");
              sb.append(frame.toString());
            }
          } catch (Exception e2) {
          }
        }

        String warningEmailReceiver = CmsPropertyHandler.getWarningEmailReceiver();
        if (warningEmailReceiver != null
            && !warningEmailReceiver.equals("")
            && warningEmailReceiver.indexOf("@warningEmailReceiver@") == -1) {
          try {
            logger.info("Mailing..");
            MailServiceFactory.getService()
                .sendEmail(
                    CmsPropertyHandler.getMailContentType(),
                    warningEmailReceiver,
                    warningEmailReceiver,
                    null,
                    null,
                    null,
                    null,
                    message,
                    sb.toString().replaceAll("\n", "<br/>"),
                    "utf-8");
          } catch (Exception e) {
            logger.error("Could not send mail:" + e.getMessage(), e);
          }
        }
      } catch (Throwable e) {
        logger.error("Error generating message:" + e.getMessage(), e);
      }
    }

    // Only sends if the last stack was sent more than 3 seconds ago.
    if ((now - lastSentTimer) > 10000) {
      lastSentTimer = System.currentTimeMillis();

      StackTraceElement[] el = targetThread.getStackTrace();

      ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
      long stopTimeInNs = threadMXBean.getThreadUserTime(targetThread.getId());
      long diff = (stopTimeInNs - startTimeInNs) / 1000000000;

      StringBuffer stackString = new StringBuffer("\n\n" + message + "\n\n");
      stackString.append("ServerName: " + getServerName() + "\n");
      stackString.append("Maximum memory (MB): " + (maxMemory / 1024 / 1024) + "\n");
      stackString.append("Used memory (MB): " + ((totalMemory - freeMemory) / 1024 / 1024) + "\n");
      stackString.append("Free memory (MB): " + (freeMemory / 1024 / 1024) + "\n");
      stackString.append("Total memory (MB): " + (totalMemory / 1024 / 1024) + "\n");
      stackString.append("Number of current requests: " + numberOfCurrentRequests + "\n");
      stackString.append("Number of active requests: " + numberOfActiveRequests + "\n");
      stackString.append("Number of long requests: " + longThreadMonitorsSize + "\n");
      stackString.append("Current thread time: " + diff + " seconds\n");
      stackString.append(
          "Average time: " + RequestAnalyser.getRequestAnalyser().getAverageElapsedTime() + "\n");
      stackString.append(
          "Longest time: " + RequestAnalyser.getRequestAnalyser().getMaxElapsedTime() + "\n");
      stackString.append("Original url: " + getOriginalFullURL() + "\n");
      stackString.append("UserInfo: " + getUserInfo() + "\n");
      stackString.append("--------------------------------------------\n\n");
      stackString.append("Thread with id [" + targetThread.getId() + "] at report time:\n");

      if (el != null && el.length != 0) {
        for (int j = 0; j < el.length; j++) {
          StackTraceElement frame = el[j];
          if (frame == null) stackString.append("    null stack frame" + "\n");
          else stackString.append("    ").append(frame.toString()).append("\n");

          // if((stackString.indexOf("infoglue") > -1 && j > 20) || j > 35)
          //	break;
        }
      }

      if (targetThread.getState().equals(State.BLOCKED)) {
        ThreadMXBean t = ManagementFactory.getThreadMXBean();

        ThreadInfo e = t.getThreadInfo(targetThread.getId(), 40);
        stackString.append(
            "\n\nBlocked info: "
                + e.getBlockedCount()
                + ":"
                + e.getBlockedTime()
                + ":"
                + e.getLockName()
                + ":"
                + e.getLockOwnerId()
                + ":"
                + e.getLockOwnerName()
                + "\n"
                + " "
                + " Thread id = "
                + e.getThreadId()
                + " "
                + e.getThreadState());

        ThreadInfo eBlockedThread = t.getThreadInfo(e.getLockOwnerId(), 40);
        StackTraceElement[] elBlockedThread = eBlockedThread.getStackTrace();
        stackString.append(
            "\n\nBlocked thread: "
                + e.getThreadName()
                + "\n"
                + " "
                + " Thread id = "
                + eBlockedThread.getThreadId()
                + " "
                + eBlockedThread.getThreadState());

        if (elBlockedThread == null || elBlockedThread.length == 0) {
          stackString.append("        no stack trace available");
        } else {
          for (int n = 0; n < elBlockedThread.length; n++) {
            if (n != 0) stackString.append("\n");

            StackTraceElement frame = elBlockedThread[n];

            if (frame == null) {
              stackString.append("        null stack frame");
              continue;
            }

            stackString.append("        ");
            stackString.append(frame.toString());
          }
        }
      }

      stackString.append(
          "\n\n**********************************\nConcurrent long threads (Only an excerpt of all)\n**********************************");

      ThreadMXBean t = ManagementFactory.getThreadMXBean();

      List threadMonitors = RequestAnalyser.getLongThreadMonitors();
      Iterator threadMonitorsIterator = threadMonitors.iterator();
      int threadCount = 0;
      while (threadMonitorsIterator.hasNext() && threadCount < 5) {
        SimpleThreadMonitor tm = (SimpleThreadMonitor) threadMonitorsIterator.next();

        if (targetThread.getId() == tm.getThreadId()) continue;

        long threads[] = {tm.getThreadId()};
        ThreadInfo[] tinfo = t.getThreadInfo(threads, 40);

        stackString
            .append("\n\n---------------------------------\nConcurrent long thread [")
            .append(tm.getThreadId())
            .append("]:\n");
        stackString
            .append("Elapsed time:")
            .append(tm.getElapsedTime())
            .append("\n Thread id: ")
            .append(tm.getThreadId())
            .append("\n Original url: ")
            .append(tm.getOriginalFullURL())
            .append(")");

        for (int i = 0; i < tinfo.length; i++) {
          ThreadInfo e = tinfo[i];

          el = e.getStackTrace();

          if (el != null && el.length != 0) {
            for (int n = 0; n < el.length; n++) {
              StackTraceElement frame = el[n];
              if (frame == null) stackString.append("    null stack frame\n");
              else stackString.append("    null stack frame").append(frame.toString()).append("\n");
            }
          }
        }

        threadCount++;
      }

      logger.warn(stackString);
    } else {
      logger.warn(
          "A thread took to long but the system seems to be really clogged so we don't send this one.");
    }
  }
Exemple #19
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);
  }