13

有没有办法找出heroku web dyno正在使用多少内存?假设我想编写一个定期运行并检查每个测功机的内存使用情况的 rake 任务。我怎么能这样做?谢谢!

4

3 回答 3

8

现在还有一种本地 Heroku 方法可以做到这一点。Heroku 推出了一个实验室功能日志运行时指标,它将 CPU 负载和内存使用信息注入日志流。这些日志带有源 ID(例如“web.1”)和测功机唯一 ID,以便您可以区分测功机。它看起来像这样:

source=web.1 dyno=heroku.2808254.d97d0ea7-cf3d-411b-b453-d2943a50b456 sample#load_avg_1m=2.46 sample#load_avg_5m=1.06 sample#load_avg_15m=0.99
source=web.1 dyno=heroku.2808254.d97d0ea7-cf3d-411b-b453-d2943a50b456 sample#memory_total=21.00MB sample#memory_rss=21.22MB sample#memory_cache=0.00MB sample#memory_swap=0.00MB sample#memory_pgpgin=348836pages sample#memory_pgpgout=343403pages

要打开它,只需运行:

heroku labs:enable log-runtime-metrics
heroku restart
于 2014-01-30T17:38:25.077 回答
6

我从接受的答案中接受了建议,并实现了一个 /proc 文件系统解析器,它具有聚合和基于阈值的差异。我发现它在调试 Heroku 上的一些 Ruby 2.0 内存问题时非常有用。获取代码,为方便起见,也包含在此处。

# 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
于 2013-07-18T07:12:49.047 回答
4

由于 Heroku 在带有 Linux 的 Amazon 实例上运行,因此您可以使用proc文件系统来获取运行时系统数据。该/proc/<pid>/smaps文件包含有关正在运行的进程及其加载的所有库的内存信息。对于常驻进程大小,请执行以下操作:

f = File.open("/proc/#{Process.pid}/smaps")
f.gets # Throw away first line
l = f.gets # Contains a string like "Size:               2148 kB\n"
l =~ /(\d+) kB/  # match the size in kB 
f.close
$1.to_i  # returns matched size as integer

更新:proc 文件系统有一个更好的来源,/proc/<pid>/status. VmRSS:总驻留内存部分和VmHWM:使用的最高驻留内存(高水位线)都有一个条目。有关这些和其他字段的更多详细信息,请参阅proc 文件系统的 Linux 内核文档

于 2013-01-08T04:40:32.883 回答