001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.management.diagnostics;
020
021import java.io.Serializable;
022import java.lang.management.*;
023import java.util.*;
024
025/**
026 * This class encapsulates a JVM thread dump, including dealocks information when available.
027 * @author Laurent Cohen
028 */
029public class ThreadDump implements Serializable {
030  /**
031   * Explicit serialVersionUID.
032   */
033  private static final long serialVersionUID = 1L;
034  /**
035   * Information on the threads.
036   */
037  private final Map<Long, ThreadInformation> threads;
038  /**
039   * The ids of the deadlocked threads.
040   */
041  private final long[] deadlockedThreads;
042
043  /**
044   * Create this thread dump from the specified thread mxbean.
045   * @param threadMXBean the thread MXBean to get the information from.
046   */
047  public ThreadDump(final ThreadMXBean threadMXBean) {
048    final ThreadInfo[] tis = threadMXBean.dumpAllThreads(threadMXBean.isObjectMonitorUsageSupported(), threadMXBean.isSynchronizerUsageSupported());
049    if ((tis == null) || (tis.length <= 0)) threads = null;
050    else {
051      threads = new TreeMap<>();
052      for (final ThreadInfo ti: tis) threads.put(ti.getThreadId(), new ThreadInformation(ti));
053    }
054    final long[] ids = threadMXBean.isSynchronizerUsageSupported() ? threadMXBean.findDeadlockedThreads() : null;
055    this.deadlockedThreads = (ids == null) || (ids.length <= 0) ? null : ids;
056  }
057
058  /**
059   * Get information on the threads.
060   * @return a mapping of {@link ThreadInformation} objects to their thread id, or <code>null</code> if no thread information is available.
061   */
062  public Map<Long, ThreadInformation> getThreads() {
063    return threads;
064  }
065
066  /**
067   * Get the ids of the deadlock threads, if any.
068   * @return the ids as an array of <code>long</code> values, or <code>null</code> if none exists.
069   */
070  public long[] getDeadlockedThreads() {
071    return deadlockedThreads;
072  }
073
074  @Override
075  public String toString() {
076    return TextThreadDumpWriter.printToString(this, "ThreadDump");
077  }
078
079  /**
080   * Print this thread dump to a plain text formatted string.
081   * @param title the title given to the thread dump.
082   * @return the thread dump printed to a plain test formatted string, or null if it could not be printed.
083   */
084  public String toPlainTextString(final String title) {
085    return TextThreadDumpWriter.printToString(this, title);
086  }
087
088  /**
089   * Print this thread dump to an HTML formatted string.
090   * @param title the title given to the thread dump.
091   * @param includeBody whether to add the &lt;html&gt; and &lt;body&gt; tags.
092   * @param fontSize the size of the font used to write the thread dump.
093   * @return the thread dump printed to an HTML string, or null if it could not be printed.
094   */
095  public String toHTMLString(final String title, final boolean includeBody, final int fontSize) {
096    return HTMLThreadDumpWriter.printToString(this, title, includeBody, fontSize);
097  }
098}