[前][次][番号順一覧][スレッド一覧]

ruby-changes:22444

From: nobu <ko1@a...>
Date: Wed, 8 Feb 2012 22:35:42 +0900 (JST)
Subject: [ruby-changes:22444] nobu:r34493 (trunk, ruby_1_9_3): * test/-ext-/string/test_modify_expand.rb: test for r34492.

nobu	2012-02-08 22:35:27 +0900 (Wed, 08 Feb 2012)

  New Revision: 34493

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=34493

  Log:
    * test/-ext-/string/test_modify_expand.rb: test for r34492.

  Added files:
    branches/ruby_1_9_3/test/-ext-/string/test_modify_expand.rb
    branches/ruby_1_9_3/test/ruby/memory_status.rb
    trunk/test/-ext-/string/test_modify_expand.rb
    trunk/test/ruby/memory_status.rb
  Modified files:
    branches/ruby_1_9_3/version.h

Index: test/ruby/memory_status.rb
===================================================================
--- test/ruby/memory_status.rb	(revision 0)
+++ test/ruby/memory_status.rb	(revision 34493)
@@ -0,0 +1,92 @@
+module Memory
+  keys = []
+  vals = []
+
+  case
+  when File.exist?(procfile = "/proc/self/status")
+    PROC_FILE = procfile
+    VM_PAT = /^Vm(\w+):\s+(\d+)/
+    def self.read_status
+      IO.foreach(PROC_FILE) do |l|
+        yield($1.downcase.intern, $2.to_i * 1024) if VM_PAT =~ l
+      end
+    end
+
+    read_status {|k, v| keys << k; vals << v}
+
+  when /mswin|mingw/ =~ RUBY_PLATFORM
+    require 'dl/import'
+    require 'dl/types'
+
+    module Win32
+      extend DL::Importer
+      dlload "kernel32.dll", "psapi.dll"
+      include DL::Win32Types
+      typealias "SIZE_T", "DWORD"
+
+      PROCESS_MEMORY_COUNTERS = struct [
+        "DWORD  cb",
+        "DWORD  PageFaultCount",
+        "SIZE_T PeakWorkingSetSize",
+        "SIZE_T WorkingSetSize",
+        "SIZE_T QuotaPeakPagedPoolUsage",
+        "SIZE_T QuotaPagedPoolUsage",
+        "SIZE_T QuotaPeakNonPagedPoolUsage",
+        "SIZE_T QuotaNonPagedPoolUsage",
+        "SIZE_T PagefileUsage",
+        "SIZE_T PeakPagefileUsage",
+      ]
+
+      typealias "PPROCESS_MEMORY_COUNTERS", "PROCESS_MEMORY_COUNTERS*"
+
+      extern "HANDLE GetCurrentProcess()", :stdcall
+      extern "BOOL GetProcessMemoryInfo(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD)", :stdcall
+
+      module_function
+      def memory_info
+        size = PROCESS_MEMORY_COUNTERS.size
+        data = PROCESS_MEMORY_COUNTERS.malloc
+        data.cb = size
+        data if GetProcessMemoryInfo(GetCurrentProcess(), data, size)
+      end
+    end
+
+    keys << :peak << :size
+    def self.read_status
+      if info = Win32.memory_info
+        yield :peak, info.PeakPagefileUsage
+        yield :size, info.PagefileUsage
+      end
+    end
+  else
+    PSCMD = ["ps", "-o", "vsz=,rss=", "-p"]
+    PAT = /^\s*(\d+)\s+(\d+)$/
+
+    keys << :size << :rss
+    def self.read_status
+      if PAT =~ IO.popen(PSCMD + [$$.to_s], "r", err: [:child, :out], &:read)
+        yield :size, $1.to_i*1024
+        yield :rss, $2.to_i*1024
+      end
+    end
+  end
+
+  Status = Struct.new(*keys)
+
+  class Status
+    def _update
+      Memory.read_status do |key, val|
+        self[key] = val
+      end
+    end
+  end
+
+  class Status
+    Header = members.map {|k| k.to_s.upcase.rjust(6)}.join('')
+    Format = "%6d"
+
+    def initialize
+      _update
+    end
+  end
+end

Property changes on: test/ruby/memory_status.rb
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: test/-ext-/string/test_modify_expand.rb
===================================================================
--- test/-ext-/string/test_modify_expand.rb	(revision 0)
+++ test/-ext-/string/test_modify_expand.rb	(revision 34493)
@@ -0,0 +1,29 @@
+require 'test/unit'
+require "-test-/string/string"
+require_relative '../../ruby/envutil'
+
+class Test_StringModifyExpand < Test::Unit::TestCase
+  def test_modify_expand_memory_leak
+    before = after = nil
+    args = [
+      "--disable=gems", "-r-test-/string/string",
+      "-I"+File.expand_path("../../..", __FILE__),
+      "-rruby/memory_status",
+      "-e", <<-CMD
+      s=Bug::String.new
+      size=Memory::Status.new.size
+      puts size
+      10.times{s.modify_expand!(size)}
+      s.replace("")
+      puts Memory::Status.new.size
+    CMD
+    ]
+    status = EnvUtil.invoke_ruby(args, "", true) do |in_p, out_p, err_p, pid|
+      before, after = out_p.readlines.map(&:to_i)
+      Process.wait(pid)
+      $?
+    end
+    assert_equal(true, status.success?)
+    assert_operator after.fdiv(before), :<, 2
+  end
+end

Property changes on: test/-ext-/string/test_modify_expand.rb
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: ruby_1_9_3/version.h
===================================================================
--- ruby_1_9_3/version.h	(revision 34492)
+++ ruby_1_9_3/version.h	(revision 34493)
@@ -1,5 +1,5 @@
 #define RUBY_VERSION "1.9.3"
-#define RUBY_PATCHLEVEL 67
+#define RUBY_PATCHLEVEL 68
 
 #define RUBY_RELEASE_DATE "2012-02-08"
 #define RUBY_RELEASE_YEAR 2012
Index: ruby_1_9_3/test/ruby/memory_status.rb
===================================================================
--- ruby_1_9_3/test/ruby/memory_status.rb	(revision 0)
+++ ruby_1_9_3/test/ruby/memory_status.rb	(revision 34493)
@@ -0,0 +1,92 @@
+module Memory
+  keys = []
+  vals = []
+
+  case
+  when File.exist?(procfile = "/proc/self/status")
+    PROC_FILE = procfile
+    VM_PAT = /^Vm(\w+):\s+(\d+)/
+    def self.read_status
+      IO.foreach(PROC_FILE) do |l|
+        yield($1.downcase.intern, $2.to_i * 1024) if VM_PAT =~ l
+      end
+    end
+
+    read_status {|k, v| keys << k; vals << v}
+
+  when /mswin|mingw/ =~ RUBY_PLATFORM
+    require 'dl/import'
+    require 'dl/types'
+
+    module Win32
+      extend DL::Importer
+      dlload "kernel32.dll", "psapi.dll"
+      include DL::Win32Types
+      typealias "SIZE_T", "DWORD"
+
+      PROCESS_MEMORY_COUNTERS = struct [
+        "DWORD  cb",
+        "DWORD  PageFaultCount",
+        "SIZE_T PeakWorkingSetSize",
+        "SIZE_T WorkingSetSize",
+        "SIZE_T QuotaPeakPagedPoolUsage",
+        "SIZE_T QuotaPagedPoolUsage",
+        "SIZE_T QuotaPeakNonPagedPoolUsage",
+        "SIZE_T QuotaNonPagedPoolUsage",
+        "SIZE_T PagefileUsage",
+        "SIZE_T PeakPagefileUsage",
+      ]
+
+      typealias "PPROCESS_MEMORY_COUNTERS", "PROCESS_MEMORY_COUNTERS*"
+
+      extern "HANDLE GetCurrentProcess()", :stdcall
+      extern "BOOL GetProcessMemoryInfo(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD)", :stdcall
+
+      module_function
+      def memory_info
+        size = PROCESS_MEMORY_COUNTERS.size
+        data = PROCESS_MEMORY_COUNTERS.malloc
+        data.cb = size
+        data if GetProcessMemoryInfo(GetCurrentProcess(), data, size)
+      end
+    end
+
+    keys << :peak << :size
+    def self.read_status
+      if info = Win32.memory_info
+        yield :peak, info.PeakPagefileUsage
+        yield :size, info.PagefileUsage
+      end
+    end
+  else
+    PSCMD = ["ps", "-o", "vsz=,rss=", "-p"]
+    PAT = /^\s*(\d+)\s+(\d+)$/
+
+    keys << :size << :rss
+    def self.read_status
+      if PAT =~ IO.popen(PSCMD + [$$.to_s], "r", err: [:child, :out], &:read)
+        yield :size, $1.to_i*1024
+        yield :rss, $2.to_i*1024
+      end
+    end
+  end
+
+  Status = Struct.new(*keys)
+
+  class Status
+    def _update
+      Memory.read_status do |key, val|
+        self[key] = val
+      end
+    end
+  end
+
+  class Status
+    Header = members.map {|k| k.to_s.upcase.rjust(6)}.join('')
+    Format = "%6d"
+
+    def initialize
+      _update
+    end
+  end
+end

Property changes on: ruby_1_9_3/test/ruby/memory_status.rb
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: ruby_1_9_3/test/-ext-/string/test_modify_expand.rb
===================================================================
--- ruby_1_9_3/test/-ext-/string/test_modify_expand.rb	(revision 0)
+++ ruby_1_9_3/test/-ext-/string/test_modify_expand.rb	(revision 34493)
@@ -0,0 +1,29 @@
+require 'test/unit'
+require "-test-/string/string"
+require_relative '../../ruby/envutil'
+
+class Test_StringModifyExpand < Test::Unit::TestCase
+  def test_modify_expand_memory_leak
+    before = after = nil
+    args = [
+      "--disable=gems", "-r-test-/string/string",
+      "-I"+File.expand_path("../../..", __FILE__),
+      "-rruby/memory_status",
+      "-e", <<-CMD
+      s=Bug::String.new
+      size=Memory::Status.new.size
+      puts size
+      10.times{s.modify_expand!(size)}
+      s.replace("")
+      puts Memory::Status.new.size
+    CMD
+    ]
+    status = EnvUtil.invoke_ruby(args, "", true) do |in_p, out_p, err_p, pid|
+      before, after = out_p.readlines.map(&:to_i)
+      Process.wait(pid)
+      $?
+    end
+    assert_equal(true, status.success?)
+    assert_operator after.fdiv(before), :<, 2
+  end
+end

Property changes on: ruby_1_9_3/test/-ext-/string/test_modify_expand.rb
___________________________________________________________________
Added: svn:eol-style
   + LF


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]