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 =
10.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.



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

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

Instance Attribute Details

#dumpObject (readonly)

Returns the value of attribute dump.



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

def dump
  @dump
end

#ignoreObject (readonly)

Returns the value of attribute ignore.



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

def ignore
  @ignore
end

#top_countObject (readonly)

Returns the value of attribute top_count.



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

def top_count
  @top_count
end

Instance Method Details

#each(&block) ⇒ Object



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

def each(&block)
  i=0
  dump.each do |hash|
    thread_name = hash["thread.name"]
    break if i >= top_count
    if ignore
      next if idle_thread?(thread_name, hash)
    end
    block.call(hash)
    i += 1
  end
end

#idle_thread?(thread_name, data) ⇒ Boolean

Returns:

  • (Boolean)


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

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