Sha256: d9f5271c1c66aad129aa58573b9d4ab7969bd04c170fb7afdd88e0007742940a

Contents?: true

Size: 1.83 KB

Versions: 7

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8
java_import 'org.logstash.instrument.reports.ThreadsReport'

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, ThreadsReport.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

7 entries across 7 versions & 2 rubygems

Version Path
logstash-core-5.5.1-java lib/logstash/util/thread_dump.rb
logstash-filter-cache-redis-0.3.1 vendor/bundle/jruby/1.9/gems/logstash-core-5.5.1.snapshot1-java/lib/logstash/util/thread_dump.rb
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/logstash-core-5.5.1.snapshot1-java/lib/logstash/util/thread_dump.rb
logstash-core-5.5.1.snapshot1-java lib/logstash/util/thread_dump.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/logstash-core-5.5.0-java/lib/logstash/util/thread_dump.rb
logstash-core-5.5.0-java lib/logstash/util/thread_dump.rb
logstash-core-6.0.0.alpha2-java lib/logstash/util/thread_dump.rb