ruby-on-rails-3 – Rails 3 Heroku:找出dyno的内存使用情况

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – Rails 3 Heroku:找出dyno的内存使用情况前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法找出一个英雄网站dyno正在使用多少内存?假设我想编写一个定期运行的rake任务,并检查每个dyno的内存使用情况.我该怎么做?谢谢!

解决方法

我接受了接受的答案的建议,并实现了一个/ proc文件系统解析器与聚合&基于阈值的差异.我发现在调试Heroku上的一些 Ruby 2.0内存问题上非常有用.获得 code,也包括在这里为了方便.
# Memory snapshot analyzer which parses the /proc file system on *nix
#
# Example (run in Heroku console):
#
#    ms = MemorySnapshot.new
#    1.upto(10000).map { |i| Array.new(i) }; nil
#    ms.snapshot!; nil
#    ms.diff 10
#    => {"lib/ld-2.11.1.so"=>156,"heap"=>2068,"all"=>2224}
#
class MemorySnapshot

  attr_reader :prevIoUs
  attr_reader :current

  def initialize
    snapshot!
    @prevIoUs = @current
  end

  # Generates a Hash of memory elements mapped to sizes of the elements in Kb
  def snapshot!
    @prevIoUs = @current
    @current = reduce(names_with_sizes)
  end

  # Calculates the difference between the prevIoUs and the current snapshot
  # Threshold is a minimum delta in kilobytes required to include an entry
  def diff(threshold = 0)
    self.class.diff_between prevIoUs,current,threshold
  end

  # Calculates the difference between two memory snapshots
  # Threshold is a minimum delta in kilobytes required to include an entry
  def self.diff_between(before,after,threshold)
    names = (before.keys + after.keys).uniq
    names.reduce({}) do |memo,name|
      delta = after.fetch(name) { 0 } - before.fetch(name) { 0 }
      memo[name] = delta if delta.abs >= threshold
      memo
    end
  end

  private

  def reduce(matches)
    total = 0
    current_name = nil
    matches.reduce(Hash.new { 0 }) do |memo,match|
      current_name = match[:name] || current_name
      size = match[:size].to_i
      total += size
      memo[current_name] += size
      memo
    end.tap { |snapshot| snapshot['all'] = total }
  end

  def names_with_sizes
    smap_entries.map do |line|
      /((^(\/|\[)(?<name>[^ \]]+)\]?\s+)|(^))(?<size>\d+)\s/.match(line)
    end
  end

  def smap_entries
    smaps.
        gsub(/^(([^Sa-f0-9])|(S[^i]))[^\n]+\n/m,'').
        gsub(/\nSize:/m,'').
        gsub(/[0-9a-f]+-[0-9a-f]+.{6}[0-9a-f]+ [0-9a-f]+:[0-9a-f]+ [0-9a-f]+\s+/i,'').
        split("\n")
  end

  def smaps
    File.read("/proc/#{Process.pid}/smaps")
  end
end
原文链接:https://www.f2er.com/ruby/271596.html

猜你在找的Ruby相关文章