加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:39:18 所属栏目:百科 来源:网络整理
导读:有没有办法找出一个英雄网站dyno正在使用多少内存?假设我想编写一个定期运行的rake任务,并检查每个dyno的内存使用情况.我该怎么做?谢谢! 解决方法 我接受了接受的答案的建议,并实现了一个/ proc文件系统解析器与聚合基于阈值的差异.我发现在调试Heroku上
有没有办法找出一个英雄网站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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读