Class: LogStash::Util::ThreadDump

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/util/thread_dump.rb

Constant Summary collapse

SKIPPED_THREADS =
[ "Finalizer", "Reference Handler", "Signal Dispatcher" ].freeze
THREADS_COUNT_DEFAULT =
3.freeze
IGNORE_IDLE_THREADS_DEFAULT =
true.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ThreadDump

Returns a new instance of ThreadDump.



11
12
13
14
15
16
# File 'lib/logstash/util/thread_dump.rb', line 11

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

Instance Attribute Details

#dumpObject (readonly)

Returns the value of attribute dump.



9
10
11
# File 'lib/logstash/util/thread_dump.rb', line 9

def dump
  @dump
end

#ignoreObject (readonly)

Returns the value of attribute ignore.



9
10
11
# File 'lib/logstash/util/thread_dump.rb', line 9

def ignore
  @ignore
end

#top_countObject (readonly)

Returns the value of attribute top_count.



9
10
11
# File 'lib/logstash/util/thread_dump.rb', line 9

def top_count
  @top_count
end

Instance Method Details

#each(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/logstash/util/thread_dump.rb', line 18

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

#idle_thread?(thread_name, data) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/logstash/util/thread_dump.rb', line 30

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