/// <summary> /// Creates threads dump and try to mimic JVM stack trace as much as possible to allow existing analytics tools to be used /// </summary> /// <param name="threadMxBean"> bean to use for thread dump </param> /// <param name="systemProperties"> dumped vm system properties </param> /// <returns> string that contains thread dump </returns> public static string ThreadDump(ThreadMXBean threadMxBean, Properties systemProperties) { ThreadInfo[] threadInfos = threadMxBean.dumpAllThreads(true, true); // Reproduce JVM stack trace as far as possible to allow existing analytics tools to be used string vmName = systemProperties.getProperty("java.vm.name"); string vmVersion = systemProperties.getProperty("java.vm.version"); string vmInfoString = systemProperties.getProperty("java.vm.info"); StringBuilder sb = new StringBuilder(); sb.Append(string.Format("Full thread dump {0} ({1} {2}):\n\n", vmName, vmVersion, vmInfoString)); foreach (ThreadInfo threadInfo in threadInfos) { sb.Append(string.Format("\"{0}\" #{1:D}\n", threadInfo.ThreadName, threadInfo.ThreadId)); sb.Append(" java.lang.Thread.State: ").Append(threadInfo.ThreadState).Append("\n"); StackTraceElement[] stackTrace = threadInfo.StackTrace; for (int i = 0; i < stackTrace.Length; i++) { StackTraceElement e = stackTrace[i]; sb.Append("\tat ").Append(e.ToString()).Append('\n'); // First stack element info can be found in the thread state if (i == 0 && threadInfo.LockInfo != null) { Thread.State ts = threadInfo.ThreadState; switch (ts) { case BLOCKED: sb.Append("\t- blocked on ").Append(threadInfo.LockInfo).Append('\n'); break; case WAITING: sb.Append("\t- waiting on ").Append(threadInfo.LockInfo).Append('\n'); break; case TIMED_WAITING: sb.Append("\t- waiting on ").Append(threadInfo.LockInfo).Append('\n'); break; default: break; } } foreach (MonitorInfo mi in threadInfo.LockedMonitors) { if (mi.LockedStackDepth == i) { sb.Append("\t- locked ").Append(mi).Append('\n'); } } } } return(sb.ToString()); }
/* * Constructs a <tt>ThreadInfo</tt> object from a * {@link CompositeData CompositeData}. */ private ThreadInfo(CompositeData cd) { ThreadInfoCompositeData ticd = ThreadInfoCompositeData.getInstance(cd); ThreadId_Renamed = ticd.threadId(); ThreadName_Renamed = ticd.threadName(); BlockedTime_Renamed = ticd.blockedTime(); BlockedCount_Renamed = ticd.blockedCount(); WaitedTime_Renamed = ticd.waitedTime(); WaitedCount_Renamed = ticd.waitedCount(); LockName_Renamed = ticd.lockName(); LockOwnerId_Renamed = ticd.lockOwnerId(); LockOwnerName_Renamed = ticd.lockOwnerName(); ThreadState_Renamed = ticd.threadState(); Suspended_Renamed = ticd.suspended(); InNative_Renamed = ticd.inNative(); StackTrace_Renamed = ticd.stackTrace(); // 6.0 attributes if (ticd.CurrentVersion) { @lock = ticd.lockInfo(); LockedMonitors_Renamed = ticd.lockedMonitors(); LockedSynchronizers_Renamed = ticd.lockedSynchronizers(); } else { // lockInfo is a new attribute added in 1.6 ThreadInfo // If cd is a 5.0 version, construct the LockInfo object // from the lockName value. if (LockName_Renamed != null) { String[] result = LockName_Renamed.Split("@"); if (result.Length == 2) { int identityHashCode = Convert.ToInt32(result[1], 16); @lock = new LockInfo(result[0], identityHashCode); } else { Debug.Assert(result.Length == 2); @lock = null; } } else { @lock = null; } LockedMonitors_Renamed = EMPTY_MONITORS; LockedSynchronizers_Renamed = EMPTY_SYNCS; } }
/// <summary>Print all of the thread's information and stack traces.</summary> /// <param name="stream">the stream to</param> /// <param name="title">a string title for the stack trace</param> public static void PrintThreadInfo(TextWriter stream, string title) { lock (typeof(ReflectionUtils)) { int StackDepth = 20; bool contention = threadBean.IsThreadContentionMonitoringEnabled(); long[] threadIds = threadBean.GetAllThreadIds(); stream.WriteLine("Process Thread Dump: " + title); stream.WriteLine(threadIds.Length + " active threads"); foreach (long tid in threadIds) { ThreadInfo info = threadBean.GetThreadInfo(tid, StackDepth); if (info == null) { stream.WriteLine(" Inactive"); continue; } stream.WriteLine("Thread " + GetTaskName(info.GetThreadId(), info.GetThreadName() ) + ":"); Thread.State state = info.GetThreadState(); stream.WriteLine(" State: " + state); stream.WriteLine(" Blocked count: " + info.GetBlockedCount()); stream.WriteLine(" Waited count: " + info.GetWaitedCount()); if (contention) { stream.WriteLine(" Blocked time: " + info.GetBlockedTime()); stream.WriteLine(" Waited time: " + info.GetWaitedTime()); } if (state == Thread.State.Waiting) { stream.WriteLine(" Waiting on " + info.GetLockName()); } else { if (state == Thread.State.Blocked) { stream.WriteLine(" Blocked on " + info.GetLockName()); stream.WriteLine(" Blocked by " + GetTaskName(info.GetLockOwnerId(), info.GetLockOwnerName ())); } } stream.WriteLine(" Stack:"); foreach (StackTraceElement frame in info.GetStackTrace()) { stream.WriteLine(" " + frame.ToString()); } } stream.Flush(); } }
public static void AwaitThreadState(Thread thread, long maxWaitMillis, Thread.State first, params Thread.State[] rest) { EnumSet <Thread.State> set = EnumSet.of(first, rest); long deadline = maxWaitMillis + DateTimeHelper.CurrentUnixTimeMillis(); Thread.State currentState; do { currentState = thread.State; if (DateTimeHelper.CurrentUnixTimeMillis() > deadline) { throw new AssertionError("Timed out waiting for thread state of <" + set + ">: " + thread + " (state = " + thread.State + ")"); } } while (!set.contains(currentState)); }
/// <summary> /// Initialize ThreadInfo object /// </summary> /// <param name="t"> Thread </param> /// <param name="state"> Thread state </param> /// <param name="lockObj"> Object on which the thread is blocked </param> /// <param name="lockOwner"> the thread holding the lock </param> /// <param name="blockedCount"> Number of times blocked to enter a lock </param> /// <param name="blockedTime"> Approx time blocked to enter a lock </param> /// <param name="waitedCount"> Number of times waited on a lock </param> /// <param name="waitedTime"> Approx time waited on a lock </param> /// <param name="stackTrace"> Thread stack trace </param> /// <param name="lockedMonitors"> List of locked monitors </param> /// <param name="lockedSynchronizers"> List of locked synchronizers </param> private void Initialize(Thread t, int state, Object lockObj, Thread lockOwner, long blockedCount, long blockedTime, long waitedCount, long waitedTime, StackTraceElement[] stackTrace, MonitorInfo[] lockedMonitors, LockInfo[] lockedSynchronizers) { this.ThreadId_Renamed = t.Id; this.ThreadName_Renamed = t.Name; this.ThreadState_Renamed = ManagementFactoryHelper.toThreadState(state); this.Suspended_Renamed = ManagementFactoryHelper.isThreadSuspended(state); this.InNative_Renamed = ManagementFactoryHelper.isThreadRunningNative(state); this.BlockedCount_Renamed = blockedCount; this.BlockedTime_Renamed = blockedTime; this.WaitedCount_Renamed = waitedCount; this.WaitedTime_Renamed = waitedTime; if (lockObj == null) { this.@lock = null; this.LockName_Renamed = null; } else { this.@lock = new LockInfo(lockObj); this.LockName_Renamed = @lock.ClassName + '@' + @lock.IdentityHashCode.ToString("x"); } if (lockOwner == null) { this.LockOwnerId_Renamed = -1; this.LockOwnerName_Renamed = null; } else { this.LockOwnerId_Renamed = lockOwner.Id; this.LockOwnerName_Renamed = lockOwner.Name; } if (stackTrace == null) { this.StackTrace_Renamed = NO_STACK_TRACE; } else { this.StackTrace_Renamed = stackTrace; } this.LockedMonitors_Renamed = lockedMonitors; this.LockedSynchronizers_Renamed = lockedSynchronizers; }
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; foreach (ThreadInfo threadInfo in 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 == Thread.State.New) { threadsNew++; } else { if (state == Thread.State.Runnable) { threadsRunnable++; } else { if (state == Thread.State.Blocked) { threadsBlocked++; } else { if (state == Thread.State.Waiting) { threadsWaiting++; } else { if (state == Thread.State.TimedWaiting) { threadsTimedWaiting++; } else { if (state == Thread.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); }
/// <summary> /// Returns a string representation of this thread info. /// The format of this string depends on the implementation. /// The returned string will typically include /// the <seealso cref="#getThreadName thread name"/>, /// the <seealso cref="#getThreadId thread ID"/>, /// its <seealso cref="#getThreadState state"/>, /// and a <seealso cref="#getStackTrace stack trace"/> if any. /// </summary> /// <returns> a string representation of this thread info. </returns> public override String ToString() { StringBuilder sb = new StringBuilder("\"" + ThreadName + "\"" + " Id=" + ThreadId + " " + ThreadState); if (LockName != null) { sb.Append(" on " + LockName); } if (LockOwnerName != null) { sb.Append(" owned by \"" + LockOwnerName + "\" Id=" + LockOwnerId); } if (Suspended) { sb.Append(" (suspended)"); } if (InNative) { sb.Append(" (in native)"); } sb.Append('\n'); int i = 0; for (; i < StackTrace_Renamed.Length && i < MAX_FRAMES; i++) { StackTraceElement ste = StackTrace_Renamed[i]; sb.Append("\tat " + ste.ToString()); sb.Append('\n'); if (i == 0 && LockInfo != null) { Thread.State ts = ThreadState; switch (ts.InnerEnumValue()) { case Thread.State.InnerEnum.BLOCKED: sb.Append("\t- blocked on " + LockInfo); sb.Append('\n'); break; case Thread.State.InnerEnum.WAITING: sb.Append("\t- waiting on " + LockInfo); sb.Append('\n'); break; case Thread.State.InnerEnum.TIMED_WAITING: sb.Append("\t- waiting on " + LockInfo); sb.Append('\n'); break; default: break; } } foreach (MonitorInfo mi in LockedMonitors_Renamed) { if (mi.LockedStackDepth == i) { sb.Append("\t- locked " + mi); sb.Append('\n'); } } } if (i < StackTrace_Renamed.Length) { sb.Append("\t..."); sb.Append('\n'); } LockInfo[] locks = LockedSynchronizers; if (locks.Length > 0) { sb.Append("\n\tNumber of locked synchronizers = " + locks.Length); sb.Append('\n'); foreach (LockInfo li in locks) { sb.Append("\t- " + li); sb.Append('\n'); } } sb.Append('\n'); return(sb.ToString()); }
public override bool Test(Thread thread) { Thread.State threadState = thread.State; SeenStates.Add(threadState); return(PossibleStates.Contains(threadState)); }