Class: Fluent::Plugin::WatchObjectspaceInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_watch_objectspace.rb

Instance Method Summary collapse

Instance Method Details

#check_threshold(record) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fluent/plugin/in_watch_objectspace.rb', line 127

def check_threshold(record)
  return unless @threshold

  if @threshold.res_of_top
    if @source["res"] * @threshold.res_of_top < record["res"]
      record["memory_leaks"] = true
      message = sprintf("Memory usage is over than expected, threshold res_of_top rate <%f>: %f > %f * %f",
                        @threshold.res_of_top, record["res"],
                        @source["res"], @threshold.res_of_top)
      raise message
    end
  end
  if @threshold.memsize_of_all
    if @source["memsize_of_all"] * @threshold.memsize_of_all < record["memsize_of_all"]
      record["memory_leaks"] = true
      message = sprintf("Memory usage is over than expected, threshold of memsize_of_all rate <%f>: %f > %f * %f",
                        @threshold.memsize_of_all, record["memsize_of_all"],
                        @source["memsize_of_all"], @threshold.memsize_of_all)
      raise message
    end
  end
end

#configure(conf) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fluent/plugin/in_watch_objectspace.rb', line 49

def configure(conf)
  super(conf)
  if @modules
    @modules.each do |mod|
      begin
        require mod
      rescue LoadError
        raise Fluent::ConfigError.new("BUG: module <#{mod}> can't be loaded")
      end
    end
  end
  @warmup_time = Time.now + @watch_delay
  @source = {}
  GC::Profiler.enable
end

#parse_top_result(content) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fluent/plugin/in_watch_objectspace.rb', line 70

def parse_top_result(content)
  record = {}
  fields = content.split("\n")[-2].split
  values = content.split("\n").last.split
  fields.each_with_index do |field, index|
    next unless @top_fields.include?(field)
    case field
    when "USER", "S", "TIME+", "COMMAND"
      record[field.downcase] = values[index]
    when "PID", "PR", "NI", "VIRT", "RES", "SHR"
      record[field.downcase] = values[index].to_i
    else
      record[field.downcase] = values[index].to_f
    end
  end
  record
end

#refresh_watchersObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fluent/plugin/in_watch_objectspace.rb', line 88

def refresh_watchers
  return if Time.now < @warmup_time
  
  pid = Process.pid
  record = {
    "pid" => pid,
    "count" => {},
    "memory_leaks" => false,
    "memsize_of_all" => ObjectSpace.memsize_of_all
  }

  begin
    content = IO.popen("top -p #{pid} -b -n 1") do |io|
      io.read
    end
    record.merge!(parse_top_result(content))

    if @gc_raw_data
      record["gc_raw_data"] = GC::Profiler.raw_data
    end
    if @watch_class
      @watch_class.each do |klass|
        record["count"]["#{klass.downcase}"] = ObjectSpace.each_object(Object.const_get(klass)) { |x| x }
      end
    end

    if @source.empty?
      record["memsize_of_all"] = ObjectSpace.memsize_of_all
      @source = record
    end

    check_threshold(record)
    es = OneEventStream.new(Fluent::EventTime.now, record)
    router.emit_stream(@tag, es)
  rescue => e
    $log.error(e.message)
  end
end

#shutdownObject



150
151
# File 'lib/fluent/plugin/in_watch_objectspace.rb', line 150

def shutdown
end

#startObject



65
66
67
68
# File 'lib/fluent/plugin/in_watch_objectspace.rb', line 65

def start
  super
  timer_execute(:execute_watch_objectspace, @watch_interval, &method(:refresh_watchers))
end