Example #1
0
 /// <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();
     }
 }
Example #2
0
            public bool Get()
            {
                ThreadMXBean threadBean = ManagementFactory.GetThreadMXBean();

                ThreadInfo[] threads = threadBean.GetThreadInfo(threadBean.GetAllThreadIds(), 1);
                foreach (ThreadInfo thread in threads)
                {
                    if (thread.GetThreadName().StartsWith("TransferFsImageUpload"))
                    {
                        return(false);
                    }
                }
                return(true);
            }
Example #3
0
        /// <summary>
        /// Assert that there are no threads running whose name matches the
        /// given regular expression.
        /// </summary>
        /// <param name="regex">the regex to match against</param>
        public static void AssertNoThreadsMatching(string regex)
        {
            Pattern      pattern    = Pattern.Compile(regex);
            ThreadMXBean threadBean = ManagementFactory.GetThreadMXBean();

            ThreadInfo[] infos = threadBean.GetThreadInfo(threadBean.GetAllThreadIds(), 20);
            foreach (ThreadInfo info in infos)
            {
                if (info == null)
                {
                    continue;
                }
                if (pattern.Matcher(info.GetThreadName()).Matches())
                {
                    NUnit.Framework.Assert.Fail("Leaked thread: " + info + "\n" + Joiner.On("\n").Join
                                                    (info.GetStackTrace()));
                }
            }
        }
Example #4
0
        private static int CountTimerThreads()
        {
            ThreadMXBean threadBean = ManagementFactory.GetThreadMXBean();
            int          count      = 0;

            ThreadInfo[] infos = threadBean.GetThreadInfo(threadBean.GetAllThreadIds(), 20);
            foreach (ThreadInfo info in infos)
            {
                if (info == null)
                {
                    continue;
                }
                foreach (StackTraceElement elem in info.GetStackTrace())
                {
                    if (elem.GetClassName().Contains("Timer"))
                    {
                        count++;
                        break;
                    }
                }
            }
            return(count);
        }
Example #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;

            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);
        }
Example #6
0
        private void GetThreadUsage(MetricsRecordBuilder rb)
        {
            int threadsNew          = 0;
            int threadsRunnable     = 0;
            int threadsBlocked      = 0;
            int threadsWaiting      = 0;
            int threadsTimedWaiting = 0;
            int threadsTerminated   = 0;

            long[] threadIds = threadMXBean.GetAllThreadIds();
            foreach (ThreadInfo threadInfo in threadMXBean.GetThreadInfo(threadIds, 0))
            {
                if (threadInfo == null)
                {
                    continue;
                }
                switch (threadInfo.GetThreadState())
                {
                case Thread.State.New:
                {
                    // race protection
                    threadsNew++;
                    break;
                }

                case Thread.State.Runnable:
                {
                    threadsRunnable++;
                    break;
                }

                case Thread.State.Blocked:
                {
                    threadsBlocked++;
                    break;
                }

                case Thread.State.Waiting:
                {
                    threadsWaiting++;
                    break;
                }

                case Thread.State.TimedWaiting:
                {
                    threadsTimedWaiting++;
                    break;
                }

                case Thread.State.Terminated:
                {
                    threadsTerminated++;
                    break;
                }
                }
            }
            rb.AddGauge(JvmMetricsInfo.ThreadsNew, threadsNew).AddGauge(JvmMetricsInfo.ThreadsRunnable
                                                                        , threadsRunnable).AddGauge(JvmMetricsInfo.ThreadsBlocked, threadsBlocked).AddGauge
                (JvmMetricsInfo.ThreadsWaiting, threadsWaiting).AddGauge(JvmMetricsInfo.ThreadsTimedWaiting
                                                                         , threadsTimedWaiting).AddGauge(JvmMetricsInfo.ThreadsTerminated, threadsTerminated
                                                                                                         );
        }