Sha256: 52728e200bb9b83cabbd85e434cb944022c1fbe6fb09bfb2a7f5bee849cdd22b

Contents?: true

Size: 1.77 KB

Versions: 55

Compression:

Stored size: 1.77 KB

Contents

# encoding: utf-8
module LogStash
  module Util
    class ThreadDump
      SKIPPED_THREADS             = [ "Finalizer", "Reference Handler", "Signal Dispatcher" ].freeze
      THREADS_COUNT_DEFAULT       = 3.freeze
      IGNORE_IDLE_THREADS_DEFAULT = true.freeze

      attr_reader :top_count, :ignore, :dump

      def initialize(options={})
        @options   = options
        @dump = options.fetch(:dump, JRMonitor.threads.generate({}))
        @top_count = options.fetch(:threads, THREADS_COUNT_DEFAULT)
        @ignore    = options.fetch(:ignore_idle_threads, IGNORE_IDLE_THREADS_DEFAULT)
      end

      def each(&block)
        i=0
        dump.each_pair do |thread_name, _hash|
          break if i >= top_count
          if ignore
            next if idle_thread?(thread_name, _hash)
          end
          block.call(thread_name, _hash)
          i += 1
        end
      end

      def idle_thread?(thread_name, data)
        idle = false
        if SKIPPED_THREADS.include?(thread_name)
          # these are likely JVM dependent
          idle = true
        elsif thread_name.match(/Ruby-\d+-JIT-\d+/)
          # This are internal JRuby JIT threads, 
          # see java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor for details.
          idle = true
        elsif thread_name.match(/pool-\d+-thread-\d+/)
          # This are threads used by the internal JRuby implementation to dispatch
          # calls and tasks, see prg.jruby.internal.runtime.methods.DynamicMethod.call
          idle = true
        else
          data["thread.stacktrace"].each do |trace|
            if trace.start_with?("java.util.concurrent.ThreadPoolExecutor.getTask")
              idle = true
              break
            end
          end
        end
        idle
      end
    end
  end
end

Version data entries

55 entries across 54 versions & 11 rubygems

Version Path
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.3.0/gems/logstash-core-5.2.1-java/lib/logstash/util/thread_dump.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.3.0/gems/logstash-core-5.2.1-java/lib/logstash/util/thread_dump.rb
mrcooper-logstash-output-azuresearch-0.2.2 vendor/jruby/2.5.0/gems/logstash-core-5.0.0-java/lib/logstash/util/thread_dump.rb
logstash-output-icinga-1.1.0 vendor/jruby/2.3.0/gems/logstash-core-5.2.1-java/lib/logstash/util/thread_dump.rb
logstash-output-icinga-1.1.0 vendor/jruby/1.9/gems/logstash-core-5.2.1-java/lib/logstash/util/thread_dump.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/logstash-core-5.4.0-java/lib/logstash/util/thread_dump.rb
logstash-core-5.4.3-java lib/logstash/util/thread_dump.rb
logstash-core-5.4.2-java lib/logstash/util/thread_dump.rb
logstash-filter-csharp-0.1.0 vendor/bundle/jruby/2.3.0/gems/logstash-core-5.2.1-java/lib/logstash/util/thread_dump.rb
logstash-core-5.4.1-java lib/logstash/util/thread_dump.rb
logstash-core-5.3.3-java lib/logstash/util/thread_dump.rb
logstash-filter-htmlentities-0.1.0 vendor/bundle/jruby/1.9/gems/logstash-core-5.4.0-java/lib/logstash/util/thread_dump.rb
logstash-output-icinga-1.0.0 vendor/jruby/1.9/gems/logstash-core-5.2.1-java/lib/logstash/util/thread_dump.rb
logstash-core-6.0.0.alpha1-java lib/logstash/util/thread_dump.rb
logstash-core-5.4.0-java lib/logstash/util/thread_dump.rb
logstash-core-5.3.2-java lib/logstash/util/thread_dump.rb
logstash-core-5.3.1-java lib/logstash/util/thread_dump.rb
logstash-core-5.3.0-java lib/logstash/util/thread_dump.rb
logstash-core-5.2.2-java lib/logstash/util/thread_dump.rb
logstash-input-fifo-0.9.1 vendor/bundle/jruby/1.9/gems/logstash-core-5.1.1.1-java/lib/logstash/util/thread_dump.rb