[Groonga-commit] groonga/groonga at 2f3f8ec [master] Add a tool to analyze memory usage from memory debug log

Back to archive index

Kouhei Sutou null+****@clear*****
Sat Mar 19 14:50:42 JST 2016


Kouhei Sutou	2016-03-19 14:50:42 +0900 (Sat, 19 Mar 2016)

  New Revision: 2f3f8ecc8197107fb550b29972517779b0c1c8f4
  https://github.com/groonga/groonga/commit/2f3f8ecc8197107fb550b29972517779b0c1c8f4

  Message:
    Add a tool to analyze memory usage from memory debug log

  Added files:
    tools/groonga-memory-usage-analyzer.rb

  Added: tools/groonga-memory-usage-analyzer.rb (+86 -0) 100755
===================================================================
--- /dev/null
+++ tools/groonga-memory-usage-analyzer.rb    2016-03-19 14:50:42 +0900 (5c23264)
@@ -0,0 +1,86 @@
+#!/usr/bin/env ruby
+
+class Memory < Struct.new(:size, :file, :line, :function)
+  def location
+    "#{file}:#{line}"
+  end
+end
+
+class LocationGroup
+  attr_reader :location
+  attr_reader :memories
+  def initialize(location)
+    @location = location
+    @memories = []
+  end
+
+  def add(memory)
+    @memories << memory
+  end
+
+  def total_size
+    @memories.inject(0) do |sum, memory|
+      sum + memory.size
+    end
+  end
+end
+
+class Statistics
+  def initialize
+    @location_groups = {}
+  end
+
+  def add(memory)
+    group = location_group(memory.location)
+    group.add(memory)
+  end
+
+  def sort_by_size
+    @location_groups.values.sort_by do |group|
+      group.total_size
+    end
+  end
+
+  private
+  def location_group(location)
+    @location_groups[location] ||= LocationGroup.new(location)
+  end
+end
+
+statistics = Statistics.new
+
+ARGF.each_line do |line|
+  case line
+  when /\Aaddress\[\d+\]\[not-freed\]:\s
+          (?:0x)?[\da-fA-F]+\((\d+)\):\s
+          (.+?):(\d+):\s(\S+)/x
+    size = $1.to_i
+    file = $2
+    line = $3.to_i
+    function = $4.strip
+    memory = Memory.new(size, file, line, function)
+    statistics.add(memory)
+  end
+end
+
+def format_size(size)
+  if size < 1024
+    "#{size}B"
+  elsif size < (1024 * 1024)
+    "%.3fKiB" % (size / 1024.0)
+  elsif size < (1024 * 1024 * 1024)
+    "%.3fMiB" % (size / 1024.0 / 1024.0)
+  elsif size < (1024 * 1024 * 1024 * 1024)
+    "%.3fGiB" % (size / 1024.0 / 1024.0 / 1024.0)
+  else
+    "#{size}B"
+  end
+end
+
+statistics.sort_by_size.reverse[0, 10].each do |group|
+  puts("%10s: %s(%d)" % [
+         format_size(group.total_size),
+         group.location,
+         group.memories.size,
+       ])
+end
-------------- next part --------------
HTML����������������������������...
Descargar 



More information about the Groonga-commit mailing list
Back to archive index